1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Platform independent functions for string manipulation.
32 * @author Jose Fonseca <jfonseca@vmware.com>
38 #if !defined(XF86_LIBC_H)
44 #include "util/macros.h" // PRINTFLIKE
53 #define util_strchrnul strchrnul
58 util_strchrnul(const char *s
, char c
)
60 for (; *s
&& *s
!= c
; ++s
);
70 util_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
)
72 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
73 * if the number of characters to write is greater than count.
78 ret
= _vsnprintf(str
, size
, format
, ap
);
80 ret
= _vscprintf(format
, ap_copy
);
87 util_snprintf(char *str
, size_t size
, const char *format
, ...)
92 ret
= util_vsnprintf(str
, size
, format
, ap
);
98 util_vsprintf(char *str
, const char *format
, va_list ap
)
100 util_vsnprintf(str
, (size_t)-1, format
, ap
);
105 util_sprintf(char *str
, const char *format
, ...)
108 va_start(ap
, format
);
109 util_vsnprintf(str
, (size_t)-1, format
, ap
);
114 util_strchr(const char *s
, char c
)
116 char *p
= util_strchrnul(s
, c
);
118 return *p
? p
: NULL
;
122 util_strncat(char *dst
, const char *src
, size_t n
)
124 char *p
= dst
+ strlen(dst
);
128 for (i
= 0; i
< n
&& *q
!= '\0'; ++i
)
136 util_strcmp(const char *s1
, const char *s2
)
138 unsigned char u1
, u2
;
141 u1
= (unsigned char) *s1
++;
142 u2
= (unsigned char) *s2
++;
152 util_strncmp(const char *s1
, const char *s2
, size_t n
)
154 unsigned char u1
, u2
;
157 u1
= (unsigned char) *s1
++;
158 u2
= (unsigned char) *s2
++;
168 util_strstr(const char *haystack
, const char *needle
)
170 const char *p
= haystack
;
171 size_t len
= strlen(needle
);
173 for (; (p
= util_strchr(p
, *needle
)) != 0; p
++) {
174 if (util_strncmp(p
, needle
, len
) == 0) {
182 #define util_strcasecmp stricmp
186 #define util_vsnprintf vsnprintf
187 #define util_snprintf snprintf
188 #define util_vsprintf vsprintf
189 #define util_sprintf sprintf
190 #define util_strchr strchr
191 #define util_strcmp strcmp
192 #define util_strncmp strncmp
193 #define util_strncat strncat
194 #define util_strstr strstr
195 #define util_strcasecmp strcasecmp
204 #endif /* U_STRING_H_ */