summaryrefslogtreecommitdiff
path: root/src/port/string.c
blob: 398788c97c9288c865d5cbab9051564c8471903b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "port/string.h"

/**
 * strdup is not always part of the C library or they need insane flags to
 * be set which would enable too many non-standard things.
 */

#if !defined HAVE_STRDUP || defined TEST_STRDUP

#include <stdlib.h>		/* for malloc, NULL */

char *wolf_port_strdup( const char *s ) {
	char *d;
	d = (char *)malloc( strlen( s ) + 1 );
	if( d == NULL ) return NULL;
	strcpy( d, s );
	return d;
}

#endif /* !defined HAVE_STRDUP || defined TEST_STRDUP */

/**
 * strerror_r exists in various fassions (XSI and GNU flavour in glibc). We stick to
 * the API of POSIX.1 of course (XSI-compliant)..
 */

#if !defined HAVE_STRERROR_R || defined TEST_STRERROR_R

#include "port/stdio.h"		/* for snprintf */
#include "port/stdbool.h"	/* for bool */
#include <sys/types.h>		/* for size_t */
#include <errno.h>		/* for errno */
#include "threads/mutex.h"	/* for mutexes */

static bool mutex_initialized = false;
static wolf_mutex_t mutex;

int wolf_port_strerror_r( int num, char *buf, size_t buflen ) {
	int safe_errno = errno;
	const char *msg;

	/* critical section as strerror is not thread-safe */
	if( !mutex_initialized ) {
		wolf_mutex_init( &mutex );
		mutex_initialized = true;
	}
	wolf_mutex_lock( &mutex );

	msg = strerror( num );
	/* TODO: can we detect illegal error numbers? For sure on Linux NULL
	 * is never returned and a strcmp with prefix "Unknown error" or similar
	 * is not really acceptable. 
	*/
	if( msg == NULL /* Solaris 8 */ ||
	    strncmp( msg, "Unknown error:", strlen( "Unknown error:" ) ) == 0 /* FreeBSD */ ) {
		/* Linux returns an empty string in this case, why? */
		(void)snprintf( buf, buflen, "Unknown error %d", num );
		errno = EINVAL;
		wolf_mutex_unlock( &mutex );
		return -1;
	}

	strncpy( buf, msg, buflen-1 );
	errno = safe_errno;
	wolf_mutex_unlock( &mutex );
	return 0;
}

#endif /* !defined HAVE_STRERROR_R || defined TEST_STRERROR_R */

/**
 * str(n)casecmp is always around but on very old or strance C libraries.
 * We should only use it in 7-bit ASCII places like in the protocol. In
 * the Windows API they are called stri(n)cmp, so we have to map them
 * anyway.
 */

#if !defined HAVE_STRCASECMP || defined TEST_STRCASECMP

#include <ctype.h>		/* for tolower */

int wolf_port_strcasecmp( const char *s1, const char *s2 ) {
	int c1, c2;

	do {
		c1 = tolower( (unsigned char)*s1 );
		c2 = tolower( (unsigned char)*s2 );
		s1++;
		s2++;
	} while( c1 == c2 && c1 != '\0' && c2 != '\0' );

	return( c2 > c1 ? -1 : c1 > c2 );
}

#endif /* !defined HAVE_STRCASECMP || defined TEST_STRCASECMP */

#if !defined HAVE_STRNCASECMP || defined TEST_STRNCASECMP

#include <ctype.h>		/* for tolower */

int wolf_port_strncasecmp( const char *s1, const char *s2, size_t n ) {
	int c1, c2;

	if( n == 0 )
		return 0;

	do {
		c1 = tolower( (unsigned char)*s1 );
		c2 = tolower( (unsigned char)*s2 );
		s1++;
		s2++;
		n--;
	} while( n > 0 && c1 == c2 && c1 != '\0' && c2 != '\0' );

	return( c2 > c1 ? -1 : c1 > c2 );
}

#endif /* !defined HAVE_STRNCASECMP || defined TEST_STRNCASECMP */

#if !defined HAVE_STRLCPY || defined TEST_STRLCPY
size_t wolf_port_strlcpy( char *d, const char *s, size_t bufsize ) {
	size_t len;
	size_t ret;

	if( !d || !s || bufsize <= 0 ) return 0;

	len = strlen( s );
	ret = len;
	if( len >= bufsize ) {
		len = bufsize-1;
	}

	memcpy( d, s, len );
	d[len] = 0;

	return ret;
}
#endif /* !defined HAVE_STRLCPY || defined TEST_STRLCPY */

#if !defined HAVE_STRLCAT || defined TEST_STRLCAT
size_t wolf_port_strlcat( char *d, const char *s, size_t bufsize ) {
	size_t len1;
	size_t len2;
	size_t ret;

	if( !d || !s || bufsize <= 0 ) return 0;

	len1 = strlen( d );
	len2 = strlen( s );
	ret = len1+len2;
	if( len1+len2 >= bufsize ) {
		len2 = bufsize - (len1+1);
	}
	if( (int)len2 >= 0 ) {
		memcpy( d+len1, s, len2 );
		d[len1+len2] = 0;
	}

	return ret;
}
#endif /* !defined HAVE_STRLCAT || defined TEST_STRLCAT */