Mercurial > dovecot > core-2.2
annotate src/lib/strfuncs.c @ 839:34cb1d196d2b HEAD
String function cleanups. Allocating 0 bytes of memory is treated as error
now, it could be an integer overflow. Moved printf_string_upper_bound() to
it's own file, so strfuncs.c can be with MIT license.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 22 Dec 2002 09:06:16 +0200 |
parents | ec6dd72cb8e3 |
children | 42176571725b |
rev | line source |
---|---|
0 | 1 /* |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
2 strfuncs.c : String manipulation functions |
0 | 3 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
4 Copyright (c) 2002 Timo Sirainen |
0 | 5 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
6 Permission is hereby granted, free of charge, to any person obtaining |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
7 a copy of this software and associated documentation files (the |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
8 "Software"), to deal in the Software without restriction, including |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
9 without limitation the rights to use, copy, modify, merge, publish, |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
10 distribute, sublicense, and/or sell copies of the Software, and to |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
11 permit persons to whom the Software is furnished to do so, subject to |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
12 the following conditions: |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
13 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
14 The above copyright notice and this permission notice shall be |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
15 included in all copies or substantial portions of the Software. |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
16 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 | 24 */ |
25 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
678
diff
changeset
|
26 /* @UNSAFE: whole file */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
678
diff
changeset
|
27 |
0 | 28 #include "lib.h" |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
29 #include "printf-upper-bound.h" |
0 | 30 #include "strfuncs.h" |
31 | |
32 #include <stdio.h> | |
33 #include <limits.h> | |
34 #include <ctype.h> | |
35 | |
36 #define STRCONCAT_BUFSIZE 512 | |
37 | |
38 static const char *fix_format_real(const char *fmt, const char *p) | |
39 { | |
40 const char *errstr; | |
41 char *buf; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
42 size_t pos, alloc, errlen; |
0 | 43 |
44 errstr = strerror(errno); | |
45 errlen = strlen(errstr); | |
46 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
47 pos = (size_t) (p-fmt); |
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
48 i_assert(pos < SSIZE_T_MAX); |
0 | 49 |
50 alloc = pos + errlen + 128; | |
51 buf = t_buffer_get(alloc); | |
52 | |
53 memcpy(buf, fmt, pos); | |
54 | |
55 while (*p != '\0') { | |
56 if (*p == '%' && p[1] == 'm') { | |
57 if (pos+errlen+1 > alloc) { | |
58 alloc += errlen+1 + 128; | |
59 buf = t_buffer_get(alloc); | |
60 } | |
61 | |
62 memcpy(buf+pos, errstr, errlen); | |
63 pos += errlen; | |
64 p += 2; | |
65 } else { | |
66 /* p + \0 */ | |
67 if (pos+2 > alloc) { | |
68 alloc += 128; | |
69 buf = t_buffer_get(alloc); | |
70 } | |
71 | |
72 buf[pos++] = *p; | |
73 p++; | |
74 } | |
75 } | |
76 | |
77 buf[pos++] = '\0'; | |
78 t_buffer_alloc(pos); | |
79 return buf; | |
80 } | |
81 | |
82 /* replace %m with strerror() */ | |
574
703bbeceb248
Added printf_string_fix_format() which currently just replaces %m with
Timo Sirainen <tss@iki.fi>
parents:
483
diff
changeset
|
83 const char *printf_string_fix_format(const char *fmt) |
0 | 84 { |
85 const char *p; | |
86 | |
87 for (p = fmt; *p != '\0'; p++) { | |
88 if (*p == '%' && p[1] == 'm') | |
89 return fix_format_real(fmt, p); | |
90 } | |
91 | |
92 return fmt; | |
93 } | |
94 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
95 int i_snprintf(char *dest, size_t max_chars, const char *format, ...) |
0 | 96 { |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
97 #ifndef HAVE_VSNPRINTF |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
98 char *buf; |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
99 #endif |
0 | 100 va_list args; |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
101 ssize_t len; |
0 | 102 int ret; |
103 | |
483
990dae663bc3
Assert i_snprintf() string lengths to INT_MAX instead of SSIZE_T_MAX since
Timo Sirainen <tss@iki.fi>
parents:
482
diff
changeset
|
104 i_assert(max_chars < INT_MAX); |
0 | 105 |
233
3f92df43cfa7
t_push()/t_pop() around various *sprintf() functions, so "%m" format won't
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
106 t_push(); |
236
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
107 |
0 | 108 va_start(args, format); |
574
703bbeceb248
Added printf_string_fix_format() which currently just replaces %m with
Timo Sirainen <tss@iki.fi>
parents:
483
diff
changeset
|
109 format = printf_string_fix_format(format); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
110 len = printf_string_upper_bound(format, args); |
0 | 111 va_end(args); |
112 | |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
113 i_assert(len >= 0); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
114 |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
115 #ifdef HAVE_VSNPRINTF |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
116 len = vsnprintf(dest, max_chars, format, args); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
117 #else |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
118 buf = t_buffer_get(len); |
0 | 119 len = vsprintf(buf, format, args); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
120 #endif |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
121 if (len < 0) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
122 /* some error occured */ |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
123 len = 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
124 ret = -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
125 } else if ((size_t)len >= max_chars) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
126 /* too large */ |
0 | 127 len = max_chars-1; |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
128 ret = -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
129 } else { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
130 ret = 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
131 } |
0 | 132 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
133 #ifndef HAVE_VSNPRINTF |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
134 memcpy(dest, buf, len); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
135 #endif |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
136 dest[len] = '\0'; |
236
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
137 |
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
138 t_pop(); |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
139 return ret; |
0 | 140 } |
141 | |
142 char *p_strdup(Pool pool, const char *str) | |
143 { | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
144 void *mem; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
145 size_t len; |
0 | 146 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
147 if (str == NULL) |
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
148 return NULL; |
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
149 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
150 for (len = 0; (str)[len] != '\0'; ) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
151 len++; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
152 len++; |
0 | 153 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
154 mem = p_malloc(pool, len); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
155 memcpy(mem, str, len); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
156 return mem; |
0 | 157 } |
158 | |
159 char *p_strdup_empty(Pool pool, const char *str) | |
160 { | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
161 if (str == NULL || *str == '\0') |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
162 return NULL; |
0 | 163 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
164 return p_strdup(pool, str); |
0 | 165 } |
166 | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
167 char *p_strdup_until(Pool pool, const char *start, const char *end) |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
168 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
169 size_t size; |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
170 char *mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
171 |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
172 i_assert(start <= end); |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
173 |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
174 size = (size_t) (end-start); |
8 | 175 |
176 mem = p_malloc(pool, size + 1); | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
177 memcpy(mem, start, size); |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
178 return mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
179 } |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
180 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
181 char *p_strndup(Pool pool, const char *str, size_t max_chars) |
0 | 182 { |
183 char *mem; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
184 size_t len; |
0 | 185 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
186 i_assert(max_chars != (size_t)-1); |
0 | 187 |
188 if (str == NULL) | |
189 return NULL; | |
190 | |
191 len = 0; | |
192 while (str[len] != '\0' && len < max_chars) | |
193 len++; | |
194 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
195 mem = pool->malloc(pool, len+1); |
0 | 196 memcpy(mem, str, len); |
197 mem[len] = '\0'; | |
198 return mem; | |
199 } | |
200 | |
201 char *p_strdup_printf(Pool pool, const char *format, ...) | |
202 { | |
203 va_list args; | |
204 char *ret; | |
205 | |
206 va_start(args, format); | |
207 ret = p_strdup_vprintf(pool, format, args); | |
208 va_end(args); | |
209 | |
210 return ret; | |
211 } | |
212 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
213 char *p_strdup_vprintf(Pool pool, const char *format, va_list args) |
0 | 214 { |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
215 char *ret; |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
216 size_t len; |
0 | 217 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
218 i_assert(format != NULL); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
219 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
220 if (pool != data_stack_pool) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
221 t_push(); |
233
3f92df43cfa7
t_push()/t_pop() around various *sprintf() functions, so "%m" format won't
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
222 |
574
703bbeceb248
Added printf_string_fix_format() which currently just replaces %m with
Timo Sirainen <tss@iki.fi>
parents:
483
diff
changeset
|
223 format = printf_string_fix_format(format); |
0 | 224 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
225 len = printf_string_upper_bound(format, args); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
226 ret = p_malloc(pool, len); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
227 |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
228 #ifdef HAVE_VSNPRINTF |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
229 vsnprintf(ret, len, format, args); |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
230 #else |
0 | 231 vsprintf(ret, format, args); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
232 #endif |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
233 if (pool != data_stack_pool) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
234 t_pop(); |
236
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
235 return ret; |
0 | 236 } |
237 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
238 const char *_vstrconcat(const char *str1, va_list args, size_t *ret_len) |
0 | 239 { |
240 const char *str; | |
241 char *temp; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
242 size_t full_len, len, bufsize; |
0 | 243 |
244 if (str1 == NULL) | |
245 return NULL; | |
246 | |
247 /* put str1 to buffer */ | |
248 len = strlen(str1); | |
249 bufsize = len <= STRCONCAT_BUFSIZE ? STRCONCAT_BUFSIZE : | |
250 nearest_power(len+1); | |
251 temp = t_buffer_get(bufsize); | |
252 | |
253 memcpy(temp, str1, len); | |
254 full_len = len; | |
255 | |
256 /* put rest of the strings to buffer */ | |
257 while ((str = va_arg(args, char *)) != NULL) { | |
258 len = strlen(str); | |
259 if (len == 0) | |
260 continue; | |
261 | |
262 if (bufsize < full_len+len+1) { | |
263 bufsize = nearest_power(bufsize+len+1); | |
264 temp = t_buffer_reget(temp, bufsize); | |
265 } | |
266 | |
267 memcpy(temp+full_len, str, len); | |
268 full_len += len; | |
269 } | |
270 | |
271 temp[full_len] = '\0'; | |
272 *ret_len = full_len+1; | |
273 return temp; | |
274 } | |
275 | |
276 char *p_strconcat(Pool pool, const char *str1, ...) | |
277 { | |
278 va_list args; | |
279 const char *temp; | |
280 char *ret; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
281 size_t len; |
0 | 282 |
283 va_start(args, str1); | |
284 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
285 temp = _vstrconcat(str1, args, &len); |
0 | 286 if (temp == NULL) |
287 ret = NULL; | |
288 else { | |
289 ret = p_malloc(pool, len); | |
290 memcpy(ret, temp, len); | |
291 } | |
292 | |
293 va_end(args); | |
294 return ret; | |
295 } | |
296 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
297 const char *t_strdup(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
298 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
299 return p_strdup(data_stack_pool, str); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
300 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
301 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
302 char *t_strdup_noconst(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
303 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
304 return p_strdup(data_stack_pool, str); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
305 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
306 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
307 const char *t_strdup_empty(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
308 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
309 return p_strdup_empty(data_stack_pool, str); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
310 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
311 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
312 const char *t_strdup_until(const char *start, const char *end) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
313 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
314 return p_strdup_until(data_stack_pool, start, end); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
315 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
316 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
317 const char *t_strndup(const char *str, size_t max_chars) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
318 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
319 return p_strndup(data_stack_pool, str, max_chars); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
320 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
321 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
322 const char *t_strdup_printf(const char *format, ...) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
323 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
324 va_list args; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
325 const char *ret; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
326 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
327 va_start(args, format); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
328 ret = p_strdup_vprintf(data_stack_pool, format, args); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
329 va_end(args); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
330 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
331 return ret; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
332 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
333 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
334 const char *t_strdup_vprintf(const char *format, va_list args) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
335 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
336 return p_strdup_vprintf(data_stack_pool, format, args); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
337 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
338 |
0 | 339 const char *t_strconcat(const char *str1, ...) |
340 { | |
341 va_list args; | |
342 const char *ret; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
343 size_t len; |
0 | 344 |
345 va_start(args, str1); | |
346 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
347 ret = _vstrconcat(str1, args, &len); |
0 | 348 if (ret != NULL) |
349 t_buffer_alloc(len); | |
350 | |
351 va_end(args); | |
352 return ret; | |
353 } | |
354 | |
355 const char *t_strcut(const char *str, char cutchar) | |
356 { | |
357 const char *p; | |
358 | |
359 for (p = str; *p != '\0'; p++) { | |
360 if (*p == cutchar) | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
361 return t_strdup_until(str, p); |
0 | 362 } |
363 | |
364 return str; | |
365 } | |
366 | |
367 int is_numeric(const char *str, char end_char) | |
368 { | |
369 if (*str == '\0' || *str == end_char) | |
370 return FALSE; | |
371 | |
372 while (*str != '\0' && *str != end_char) { | |
373 if (!i_isdigit(*str)) | |
374 return FALSE; | |
375 str++; | |
376 } | |
377 | |
378 return TRUE; | |
379 } | |
380 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
381 int strocpy(char *dest, const char *src, size_t dstsize) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
382 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
383 if (dstsize == 0) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
384 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
385 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
386 while (*src != '\0' && dstsize > 1) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
387 *dest++ = *src++; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
388 dstsize--; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
389 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
390 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
391 *dest++ = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
392 return *src == '\0' ? 0 : -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
393 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
394 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
395 int str_path(char *dest, size_t dstsize, const char *dir, const char *file) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
396 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
397 size_t dirlen, filelen; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
398 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
399 dirlen = strlen(dir); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
400 filelen = strlen(file); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
401 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
402 if (dirlen+1+filelen >= dstsize) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
403 if (dstsize > 0) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
404 *dest = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
405 errno = ENAMETOOLONG; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
406 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
407 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
408 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
409 memcpy(dest, dir, dirlen); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
410 dest[dirlen] = '/'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
411 memcpy(dest + dirlen + 1, file, filelen); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
412 dest[dirlen + 1 + filelen] = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
413 return 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
414 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
415 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
416 int str_ppath(char *dest, size_t dstsize, const char *dir, |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
417 const char *file_prefix, const char *file) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
418 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
419 size_t dirlen, prefixlen, filelen; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
420 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
421 dirlen = strlen(dir); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
422 prefixlen = strlen(file_prefix); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
423 filelen = strlen(file); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
424 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
425 if (dirlen+1+prefixlen+filelen >= dstsize) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
426 if (dstsize > 0) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
427 *dest = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
428 errno = ENAMETOOLONG; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
429 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
430 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
431 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
432 memcpy(dest, dir, dirlen); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
433 dest[dirlen] = '/'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
434 memcpy(dest + dirlen + 1, file_prefix, prefixlen); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
435 memcpy(dest + dirlen + prefixlen + 1, file, filelen); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
436 dest[dirlen + 1 + prefixlen + filelen] = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
437 return 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
438 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
439 |
0 | 440 char *str_ucase(char *str) |
441 { | |
442 char *p; | |
443 | |
444 for (p = str; *p != '\0'; p++) | |
445 *p = i_toupper(*p); | |
446 return str; | |
447 } | |
448 | |
449 char *str_lcase(char *str) | |
450 { | |
451 char *p; | |
452 | |
453 for (p = str; *p != '\0'; p++) | |
454 *p = i_tolower(*p); | |
455 return str; | |
456 } | |
457 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
458 void str_remove_escapes(char *str) |
0 | 459 { |
460 char *dest; | |
461 | |
462 for (dest = str; *str != '\0'; str++) { | |
463 if (*str != '\\' || str[1] == '\0') | |
464 *dest++ = *str; | |
465 } | |
466 | |
467 *dest = '\0'; | |
468 } | |
469 | |
470 int strarray_length(char *const array[]) | |
471 { | |
472 int len; | |
473 | |
474 len = 0; | |
475 while (*array) { | |
476 len++; | |
477 array++; | |
478 } | |
479 return len; | |
480 } | |
481 | |
482 int strarray_find(char *const array[], const char *item) | |
483 { | |
484 int index; | |
485 | |
486 i_assert(item != NULL); | |
487 | |
488 for (index = 0; *array != NULL; index++, array++) { | |
489 if (strcasecmp(*array, item) == 0) | |
490 return index; | |
491 } | |
492 | |
493 return -1; | |
494 } | |
495 | |
496 char *const *t_strsplit(const char *data, const char *separators) | |
497 { | |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
498 char **array; |
0 | 499 char *str; |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
500 size_t alloc_len, len; |
0 | 501 |
502 i_assert(*separators != '\0'); | |
503 | |
68 | 504 len = strlen(data)+1; |
63 | 505 str = t_malloc(len); |
506 memcpy(str, data, len); | |
0 | 507 |
508 alloc_len = 20; | |
509 array = t_buffer_get(sizeof(const char *) * alloc_len); | |
510 | |
511 array[0] = str; len = 1; | |
512 while (*str != '\0') { | |
513 if (strchr(separators, *str) != NULL) { | |
514 /* separator found */ | |
515 if (len+1 >= alloc_len) { | |
516 alloc_len *= 2; | |
517 array = t_buffer_reget(array, | |
518 sizeof(const char *) * | |
519 alloc_len); | |
520 } | |
521 | |
522 *str = '\0'; | |
523 array[len++] = str+1; | |
524 } | |
525 | |
526 str++; | |
527 } | |
528 array[len] = NULL; | |
529 | |
530 t_buffer_alloc(sizeof(const char *) * (len+1)); | |
531 return (char *const *) array; | |
532 } | |
533 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
534 const char *dec2str(uintmax_t number) |
0 | 535 { |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
536 char *buffer; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
537 int pos; |
0 | 538 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
539 pos = MAX_INT_STRLEN; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
540 buffer = t_malloc(pos); |
0 | 541 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
542 buffer[--pos] = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
543 do { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
544 buffer[--pos] = (number % 10) + '0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
545 number /= 10; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
546 } while (number != 0 && pos >= 0); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
547 i_assert(pos >= 0); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
548 return buffer + pos; |
179
0ffecd4e7e1a
added dec2str() function and largest_t type, removed itoa() and ltoa()
Timo Sirainen <tss@iki.fi>
parents:
68
diff
changeset
|
549 } |