Mercurial > dovecot > core-2.2
annotate src/lib/strfuncs.c @ 896:21ffcce83c70 HEAD
Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly
take memory, maybe also a bit faster. This caused pretty large changes all
around.
Also moved all string (un)escaping code to lib/strescape.c.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 03 Jan 2003 17:57:12 +0200 |
parents | c4754c5770f3 |
children | 0d5be52d7131 |
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 |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
100 va_list args, args2; |
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); |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
109 VA_COPY(args2, args); |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
110 |
574
703bbeceb248
Added printf_string_fix_format() which currently just replaces %m with
Timo Sirainen <tss@iki.fi>
parents:
483
diff
changeset
|
111 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
|
112 len = printf_string_upper_bound(format, args); |
0 | 113 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
114 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
|
115 |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
116 #ifdef HAVE_VSNPRINTF |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
117 len = vsnprintf(dest, max_chars, format, args2); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
118 #else |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
119 buf = t_buffer_get(len); |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
120 len = vsprintf(buf, format, args2); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
121 #endif |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
122 va_end(args); |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
123 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
124 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
|
125 /* 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
|
126 len = 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
127 ret = -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
128 } 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
|
129 /* too large */ |
0 | 130 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
|
131 ret = -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
132 } else { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
133 ret = 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
134 } |
0 | 135 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
136 #ifndef HAVE_VSNPRINTF |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
137 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
|
138 #endif |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
139 dest[len] = '\0'; |
236
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
140 |
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
141 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
|
142 return ret; |
0 | 143 } |
144 | |
145 char *p_strdup(Pool pool, const char *str) | |
146 { | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
147 void *mem; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
148 size_t len; |
0 | 149 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
150 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
|
151 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
|
152 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
153 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
|
154 len++; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
155 len++; |
0 | 156 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
157 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
|
158 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
|
159 return mem; |
0 | 160 } |
161 | |
162 char *p_strdup_empty(Pool pool, const char *str) | |
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 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
|
165 return NULL; |
0 | 166 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
167 return p_strdup(pool, str); |
0 | 168 } |
169 | |
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 *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
|
171 { |
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
|
172 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
|
173 char *mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
174 |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
175 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
|
176 |
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
|
177 size = (size_t) (end-start); |
8 | 178 |
179 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
|
180 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
|
181 return mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
182 } |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
183 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
184 char *p_strndup(Pool pool, const char *str, size_t max_chars) |
0 | 185 { |
186 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
|
187 size_t len; |
0 | 188 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
189 i_assert(max_chars != (size_t)-1); |
0 | 190 |
191 if (str == NULL) | |
192 return NULL; | |
193 | |
194 len = 0; | |
195 while (str[len] != '\0' && len < max_chars) | |
196 len++; | |
197 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
198 mem = pool->malloc(pool, len+1); |
0 | 199 memcpy(mem, str, len); |
200 mem[len] = '\0'; | |
201 return mem; | |
202 } | |
203 | |
204 char *p_strdup_printf(Pool pool, const char *format, ...) | |
205 { | |
206 va_list args; | |
207 char *ret; | |
208 | |
209 va_start(args, format); | |
210 ret = p_strdup_vprintf(pool, format, args); | |
211 va_end(args); | |
212 | |
213 return ret; | |
214 } | |
215 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
216 char *p_strdup_vprintf(Pool pool, const char *format, va_list args) |
0 | 217 { |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
218 char *ret; |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
219 va_list args2; |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
220 size_t len; |
0 | 221 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
222 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
|
223 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
224 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
|
225 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
|
226 |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
227 VA_COPY(args2, args); |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
228 |
574
703bbeceb248
Added printf_string_fix_format() which currently just replaces %m with
Timo Sirainen <tss@iki.fi>
parents:
483
diff
changeset
|
229 format = printf_string_fix_format(format); |
0 | 230 |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
231 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
|
232 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
|
233 |
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
234 #ifdef HAVE_VSNPRINTF |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
235 vsnprintf(ret, len, format, args2); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
236 #else |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
237 vsprintf(ret, format, args2); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
238 #endif |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
239 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
|
240 t_pop(); |
236
400ac5f9ed40
previous t_push()/t_pop() changes break things. fixed :)
Timo Sirainen <tss@iki.fi>
parents:
233
diff
changeset
|
241 return ret; |
0 | 242 } |
243 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
244 const char *_vstrconcat(const char *str1, va_list args, size_t *ret_len) |
0 | 245 { |
246 const char *str; | |
842 | 247 char *temp, *temp_end, *p; |
248 size_t bufsize, pos; | |
0 | 249 |
250 if (str1 == NULL) | |
251 return NULL; | |
252 | |
842 | 253 str = str1; |
254 bufsize = STRCONCAT_BUFSIZE; | |
255 temp = t_buffer_get(bufsize); | |
0 | 256 |
842 | 257 pos = 0; |
258 do { | |
259 temp_end = temp + bufsize - 1; /* leave 1 for \0 */ | |
260 | |
261 p = temp + pos; | |
262 while (*str != '\0' && p != temp_end) | |
263 *p++ = *str++; | |
264 pos = (size_t)(p - temp); | |
0 | 265 |
842 | 266 if (p == temp_end) { |
267 /* need more memory */ | |
268 bufsize = nearest_power(bufsize+1); | |
0 | 269 temp = t_buffer_reget(temp, bufsize); |
842 | 270 } else { |
271 /* next string */ | |
272 str = va_arg(args, const char *); | |
0 | 273 } |
842 | 274 } while (str != NULL); |
0 | 275 |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
276 i_assert(pos < bufsize); |
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
277 |
842 | 278 temp[pos] = '\0'; |
279 *ret_len = pos+1; | |
0 | 280 return temp; |
281 } | |
282 | |
283 char *p_strconcat(Pool pool, const char *str1, ...) | |
284 { | |
285 va_list args; | |
286 const char *temp; | |
287 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
|
288 size_t len; |
0 | 289 |
290 va_start(args, str1); | |
291 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
292 temp = _vstrconcat(str1, args, &len); |
0 | 293 if (temp == NULL) |
294 ret = NULL; | |
295 else { | |
296 ret = p_malloc(pool, len); | |
297 memcpy(ret, temp, len); | |
298 } | |
299 | |
300 va_end(args); | |
301 return ret; | |
302 } | |
303 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
304 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
|
305 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
306 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
|
307 } |
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 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
|
310 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
311 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
|
312 } |
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 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
|
315 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
316 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
|
317 } |
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 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
|
320 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
321 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
|
322 } |
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 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
|
325 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
326 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
|
327 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
328 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
329 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
|
330 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
331 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
|
332 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
|
333 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
334 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
|
335 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
|
336 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
|
337 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
338 return ret; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
339 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
340 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
341 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
|
342 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
343 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
|
344 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
345 |
0 | 346 const char *t_strconcat(const char *str1, ...) |
347 { | |
348 va_list args; | |
349 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
|
350 size_t len; |
0 | 351 |
352 va_start(args, str1); | |
353 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
354 ret = _vstrconcat(str1, args, &len); |
0 | 355 if (ret != NULL) |
356 t_buffer_alloc(len); | |
357 | |
358 va_end(args); | |
359 return ret; | |
360 } | |
361 | |
362 const char *t_strcut(const char *str, char cutchar) | |
363 { | |
364 const char *p; | |
365 | |
366 for (p = str; *p != '\0'; p++) { | |
367 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
|
368 return t_strdup_until(str, p); |
0 | 369 } |
370 | |
371 return str; | |
372 } | |
373 | |
374 int is_numeric(const char *str, char end_char) | |
375 { | |
376 if (*str == '\0' || *str == end_char) | |
377 return FALSE; | |
378 | |
379 while (*str != '\0' && *str != end_char) { | |
380 if (!i_isdigit(*str)) | |
381 return FALSE; | |
382 str++; | |
383 } | |
384 | |
385 return TRUE; | |
386 } | |
387 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
388 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
|
389 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
390 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
|
391 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
392 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
393 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
|
394 *dest++ = *src++; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
395 dstsize--; |
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 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
398 *dest++ = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
399 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
|
400 } |
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 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
|
403 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
404 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
|
405 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
406 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
|
407 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
|
408 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
409 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
|
410 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
|
411 *dest = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
412 errno = ENAMETOOLONG; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
413 return -1; |
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 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
|
417 dest[dirlen] = '/'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
418 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
|
419 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
|
420 return 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
421 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
422 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
423 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
|
424 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
|
425 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
426 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
|
427 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
428 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
|
429 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
|
430 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
|
431 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
432 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
|
433 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
|
434 *dest = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
435 errno = ENAMETOOLONG; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
436 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
437 } |
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 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
|
440 dest[dirlen] = '/'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
441 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
|
442 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
|
443 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
|
444 return 0; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
445 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
446 |
0 | 447 char *str_ucase(char *str) |
448 { | |
449 char *p; | |
450 | |
451 for (p = str; *p != '\0'; p++) | |
452 *p = i_toupper(*p); | |
453 return str; | |
454 } | |
455 | |
456 char *str_lcase(char *str) | |
457 { | |
458 char *p; | |
459 | |
460 for (p = str; *p != '\0'; p++) | |
461 *p = i_tolower(*p); | |
462 return str; | |
463 } | |
464 | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
465 const char **t_strsplit(const char *data, const char *separators) |
0 | 466 { |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
467 const char **array; |
0 | 468 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
|
469 size_t alloc_len, len; |
0 | 470 |
471 i_assert(*separators != '\0'); | |
472 | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
473 str = t_strdup_noconst(data); |
0 | 474 |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
475 alloc_len = 32; |
0 | 476 array = t_buffer_get(sizeof(const char *) * alloc_len); |
477 | |
478 array[0] = str; len = 1; | |
479 while (*str != '\0') { | |
480 if (strchr(separators, *str) != NULL) { | |
481 /* separator found */ | |
482 if (len+1 >= alloc_len) { | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
483 alloc_len = nearest_power(alloc_len+1); |
0 | 484 array = t_buffer_reget(array, |
485 sizeof(const char *) * | |
486 alloc_len); | |
487 } | |
488 | |
489 *str = '\0'; | |
490 array[len++] = str+1; | |
491 } | |
492 | |
493 str++; | |
494 } | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
495 |
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
496 i_assert(len < alloc_len); |
0 | 497 array[len] = NULL; |
498 | |
499 t_buffer_alloc(sizeof(const char *) * (len+1)); | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
500 return array; |
0 | 501 } |
502 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
503 const char *dec2str(uintmax_t number) |
0 | 504 { |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
505 char *buffer; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
506 int pos; |
0 | 507 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
508 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
|
509 buffer = t_malloc(pos); |
0 | 510 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
511 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
|
512 do { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
513 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
|
514 number /= 10; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
515 } while (number != 0 && pos >= 0); |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
516 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
517 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
|
518 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
|
519 } |