summaryrefslogtreecommitdiffstats
path: root/cpukit/shttpd/string.c
blob: d77f32c417d5ac58136dfab48484f1262c44f87e (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
 * All rights reserved
 *
 * "THE BEER-WARE LICENSE" (Revision 42):
 * Sergey Lyubka wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.
 */

#include "defs.h"

#ifndef HAVE_STRLCPY
void
my_strlcpy(register char *dst, register const char *src, size_t n)
{
	for (; *src != '\0' && n > 1; n--)
		*dst++ = *src++;
	*dst = '\0';
}
#endif

#ifndef HAVE_STRNCASECMP
int
my_strncasecmp(const char *str1, const char *str2, size_t len)
{
	register const unsigned char	*s1 = (unsigned char *) str1,
		 			*s2 = (unsigned char *) str2, *e;
	int				ret;

	for (e = s1 + len - 1; s1 < e && *s1 != '\0' && *s2 != '\0' &&
	    tolower(*s1) == tolower(*s2); s1++, s2++) ;
	ret = tolower(*s1) - tolower(*s2);

	return (ret);
}
#endif

#ifndef HAVE_STRNDUP
char *
my_strndup(const char *ptr, size_t len)
{
	char	*p;

	if ((p = malloc(len + 1)) != NULL)
		my_strlcpy(p, ptr, len + 1);

	return (p);

}
#endif

#ifndef HAVE_STRDUP
char *
my_strdup(const char *str)
{
	return (my_strndup(str, strlen(str)));
}
#endif

/*
 * Sane snprintf(). Acts like snprintf(), but never return -1 or the
 * value bigger than supplied buffer.
 * Thanks Adam Zeldis to pointing snprintf()-caused vulnerability
 * in his audit report.
 */
int
my_snprintf(char *buf, size_t buflen, const char *fmt, ...)
{
	va_list		ap;
	int		n;

	if (buflen == 0)
		return (0);

	va_start(ap, fmt);
	n = vsnprintf(buf, buflen, fmt, ap);
	va_end(ap);

	if (n < 0 || (size_t) n >= buflen)
		n = buflen - 1;
	buf[n] = '\0';

	return (n);
}