1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28 /**
29  * @file
30  * Platform independent functions for string manipulation.
31  *
32  * @author Jose Fonseca <jfonseca@vmware.com>
33  */
34 
35 #ifndef U_STRING_H_
36 #define U_STRING_H_
37 
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <limits.h>
44 
45 #include "util/macros.h" // PRINTFLIKE
46 
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #if !defined(_GNU_SOURCE) || defined(__APPLE__)
53 
54 #define strchrnul util_strchrnul
55 static inline char *
util_strchrnul(const char * s,char c)56 util_strchrnul(const char *s, char c)
57 {
58    for (; *s && *s != c; ++s);
59 
60    return (char *)s;
61 }
62 
63 #endif
64 
65 #ifdef _WIN32
66 
67 #define sprintf util_sprintf
68 static inline int
69    PRINTFLIKE(2, 3)
util_sprintf(char * str,const char * format,...)70 util_sprintf(char *str, const char *format, ...)
71 {
72    va_list ap;
73    va_start(ap, format);
74    int r = vsnprintf(str, INT_MAX, format, ap);
75    va_end(ap);
76    return r;
77 }
78 
79 #define vasprintf util_vasprintf
80 static inline int
util_vasprintf(char ** ret,const char * format,va_list ap)81 util_vasprintf(char **ret, const char *format, va_list ap)
82 {
83    va_list ap_copy;
84 
85    /* Compute length of output string first */
86    va_copy(ap_copy, ap);
87    int r = vsnprintf(NULL, 0, format, ap_copy);
88    va_end(ap_copy);
89 
90    if (r < 0)
91       return -1;
92 
93    *ret = (char *) malloc(r + 1);
94    if (!*ret)
95       return -1;
96 
97    /* Print to buffer */
98    return vsnprintf(*ret, r + 1, format, ap);
99 }
100 
101 #define asprintf util_asprintf
102 static inline int
util_asprintf(char ** str,const char * fmt,...)103 util_asprintf(char **str, const char *fmt, ...)
104 {
105    int ret;
106    va_list args;
107    va_start(args, fmt);
108    ret = vasprintf(str, fmt, args);
109    va_end(args);
110    return ret;
111 }
112 
113 #ifndef strcasecmp
114 #define strcasecmp stricmp
115 #endif
116 
117 #define strdup _strdup
118 
119 #if !defined(HAVE_STRTOK_R)
120 #define strtok_r strtok_s
121 #endif
122 
123 #endif /* _WIN32 */
124 
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* U_STRING_H_ */
131