summaryrefslogtreecommitdiff
path: root/lib/str.c
blob: 18a5d8edd841a9263b27e4fb227e51d83a2ad355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * zebra string function
 *
 * these functions are just very basic wrappers around exiting ones and
 * do not offer the protection that might be expected against buffer
 * overruns etc
 */

#include <zebra.h>

#ifndef HAVE_SNPRINTF
/*
 * snprint() is a real basic wrapper around the standard sprintf()
 * without any bounds checking
 */
int
snprintf(char *str, size_t size, const char *format, ...)
{
  va_list args;

  va_start (args, format);

  return vsprintf (str, format, args);
}
#endif

#ifndef HAVE_STRLCPY
/*
 * strlcpy is a safer version of strncpy(), checking the total
 * size of the buffer
 */
size_t
strlcpy(char *dst, const char *src, size_t size)
{
  strncpy(dst, src, size);

  return (strlen(dst));
}
#endif

#ifndef HAVE_STRLCAT
/*
 * strlcat is a safer version of strncat(), checking the total
 * size of the buffer
 */
size_t
strlcat(char *dst, const char *src, size_t size)
{
  /* strncpy(dst, src, size - strlen(dst)); */

  /* I've just added below code only for workable under Linux.  So
     need rewrite -- Kunihiro. */
  if (strlen (dst) + strlen (src) >= size)
    return -1;

  strcat (dst, src);

  return (strlen(dst));
}
#endif

#ifndef HAVE_STRNLEN
size_t
strnlen(const char *s, size_t maxlen)
{
  const char *p;
  return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen;
}
#endif