annotate src/strgen.c @ 764:dd5b87ddd51e

Make *all* random strings produced the same length
author Ben Schmidt
date Wed, 20 Oct 2010 12:34:28 +1100
parents e29893b9f581
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
1 /* Copyright (C) 2003 Mads Martin Joergensen <mmj at mmj.dk>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
2 *
21ce01de8109 Initial revision
mmj
parents:
diff changeset
3 * $Id$
21ce01de8109 Initial revision
mmj
parents:
diff changeset
4 *
225
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
6 * of this software and associated documentation files (the "Software"), to
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
7 * deal in the Software without restriction, including without limitation the
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
9 * sell copies of the Software, and to permit persons to whom the Software is
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
10 * furnished to do so, subject to the following conditions:
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
11 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
12 * The above copyright notice and this permission notice shall be included in
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
13 * all copies or substantial portions of the Software.
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
14 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 184
diff changeset
21 * IN THE SOFTWARE.
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
22 */
21ce01de8109 Initial revision
mmj
parents:
diff changeset
23
21ce01de8109 Initial revision
mmj
parents:
diff changeset
24 #include <stdio.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
25 #include <stdlib.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
26 #include <string.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
27 #include <stdarg.h>
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
28 #include <unistd.h>
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
29 #include <netdb.h>
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
30 #include <libgen.h>
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
31 #include <time.h>
597
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
32 #include <ctype.h>
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
33 #include <errno.h>
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
34
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
35 #include "mlmmj.h"
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
36 #include "strgen.h"
21ce01de8109 Initial revision
mmj
parents:
diff changeset
37 #include "wrappers.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
38 #include "memory.h"
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
39 #include "log_error.h"
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
40
21ce01de8109 Initial revision
mmj
parents:
diff changeset
41 char *random_str()
21ce01de8109 Initial revision
mmj
parents:
diff changeset
42 {
731
e29893b9f581 Make the random strings produced always the same length rather the
Ben Schmidt
parents: 674
diff changeset
43 size_t len = 17;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
44 char *dest = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
45
731
e29893b9f581 Make the random strings produced always the same length rather the
Ben Schmidt
parents: 674
diff changeset
46 snprintf(dest, len, "%08x%08x", random_int(), random_int());
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
47
21ce01de8109 Initial revision
mmj
parents:
diff changeset
48 return dest;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
49 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
50
21ce01de8109 Initial revision
mmj
parents:
diff changeset
51 char *random_plus_addr(const char *addr)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
52 {
314
2ec35c398074 Brown paper bag bug commit
mmj
parents: 310
diff changeset
53 size_t len = strlen(addr) + 128;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
54 char *dest = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
55 char *atsign;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
56 char *tmpstr;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
57
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
58 tmpstr = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
59 snprintf(tmpstr, len, "%s", addr);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
60
328
80129335dbbb Replace index with strchr since it warns a lot on older SunOS versions
mmj
parents: 320
diff changeset
61 atsign = strchr(tmpstr, '@');
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
62 MY_ASSERT(atsign);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
63 *atsign = '=';
21ce01de8109 Initial revision
mmj
parents:
diff changeset
64
764
dd5b87ddd51e Make *all* random strings produced the same length
Ben Schmidt
parents: 731
diff changeset
65 snprintf(dest, len, "%08x%08x-%s", random_int(), random_int(), tmpstr);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
66
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
67 myfree(tmpstr);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
68
21ce01de8109 Initial revision
mmj
parents:
diff changeset
69 return dest;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
70 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
71
21ce01de8109 Initial revision
mmj
parents:
diff changeset
72 char *headerstr(const char *headertoken, const char *str)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
73 {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
74 size_t len = strlen(headertoken) + strlen(str) + 2;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
75 char *dest = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
76
21ce01de8109 Initial revision
mmj
parents:
diff changeset
77 snprintf(dest, len, "%s%s\n", headertoken, str);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
78
21ce01de8109 Initial revision
mmj
parents:
diff changeset
79 return dest;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
80 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
81
21ce01de8109 Initial revision
mmj
parents:
diff changeset
82 char *genlistname(const char *listaddr)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
83 {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
84 size_t len;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
85 char *dest, *atsign;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
86
328
80129335dbbb Replace index with strchr since it warns a lot on older SunOS versions
mmj
parents: 320
diff changeset
87 atsign = strchr(listaddr, '@');
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
88 MY_ASSERT(atsign);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
89 len = atsign - listaddr + 1;
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
90 dest = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
91
21ce01de8109 Initial revision
mmj
parents:
diff changeset
92 snprintf(dest, len, "%s", listaddr);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
93
21ce01de8109 Initial revision
mmj
parents:
diff changeset
94 return dest;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
95 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
96
21ce01de8109 Initial revision
mmj
parents:
diff changeset
97 char *genlistfqdn(const char *listaddr)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
98 {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
99 size_t len;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
100 char *dest, *atsign;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
101
328
80129335dbbb Replace index with strchr since it warns a lot on older SunOS versions
mmj
parents: 320
diff changeset
102 atsign = strchr(listaddr, '@');
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
103 MY_ASSERT(atsign);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
104 len = strlen(listaddr) - (atsign - listaddr);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
105 dest = mymalloc(len);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
106 snprintf(dest, len, "%s", atsign + 1);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
107
21ce01de8109 Initial revision
mmj
parents:
diff changeset
108 return dest;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
109 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
110
21ce01de8109 Initial revision
mmj
parents:
diff changeset
111 char *concatstr(int count, ...)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
112 {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
113 va_list arg;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
114 const char *str;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
115 int i;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
116 size_t len = 0;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
117 char *retstr;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
118
21ce01de8109 Initial revision
mmj
parents:
diff changeset
119 va_start(arg, count);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
120
21ce01de8109 Initial revision
mmj
parents:
diff changeset
121 for(i = 0; i < count; i++) {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
122 str = va_arg(arg, const char *);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
123 if(str)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
124 len += strlen(str);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
125 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
126
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
127 retstr = mymalloc(len + 1);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
128 retstr[0] = retstr[len] = 0;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
129
21ce01de8109 Initial revision
mmj
parents:
diff changeset
130 va_start(arg, count);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
131
21ce01de8109 Initial revision
mmj
parents:
diff changeset
132 for(i = 0; i < count; i++) {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
133 str = va_arg(arg, const char *);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
134 if(str)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
135 strcat(retstr, str);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
136 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
137
21ce01de8109 Initial revision
mmj
parents:
diff changeset
138 va_end(arg);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
139
21ce01de8109 Initial revision
mmj
parents:
diff changeset
140 return retstr;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
141 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
142
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
143 char *hostnamestr()
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
144 {
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
145 struct hostent *hostlookup;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
146 char *hostname = NULL;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
147 size_t len = 512;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
148
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
149 for (;;) {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
150 len *= 2;
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
151 myfree(hostname);
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
152
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
153 hostname = mymalloc(len);
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
154 hostname[len-1] = '\0';
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
155
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
156 /* gethostname() is allowed to:
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
157 * a) return -1 and undefined in hostname
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
158 * b) return 0 and an unterminated string in hostname
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
159 * c) return 0 and a NUL-terminated string in hostname
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
160 *
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
161 * We keep expanding the buffer until the hostname is
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
162 * NUL-terminated (and pray that it is not truncated)
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
163 * or an error occurs.
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
164 */
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
165 if (gethostname(hostname, len - 1)) {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
166 if (errno == ENAMETOOLONG) {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
167 continue;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
168 }
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
169 myfree(hostname);
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
170 return mystrdup("localhost");
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
171 }
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
172
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
173 if (hostname[len-1] == '\0') {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
174 break;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
175 }
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
176 }
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
177
605
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
178 if (strchr(hostname, '.')) {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
179 /* hostname is FQDN */
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
180 return hostname;
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
181 }
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
182
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
183 if ((hostlookup = gethostbyname(hostname))) {
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
184 myfree(hostname);
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
185 return mystrdup(hostlookup->h_name);
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
186 }
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
187
eb2985b8de7e Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoit Dolez)
mortenp
parents: 597
diff changeset
188 return hostname;
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
189 }
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 1
diff changeset
190
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
191 char *mydirname(const char *path)
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
192 {
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
193 char *mypath, *dname, *ret;
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
194
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
195 mypath = mystrdup(path);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
196 dname = dirname(mypath);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
197 ret = mystrdup(dname);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
198
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
199 /* We don't free mypath until we have strdup()'ed dname, because
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
200 * dirname() returns a pointer into mypath -- mortenp 20040527 */
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
201 myfree(mypath);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
202
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
203 return ret;
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
204 }
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
205
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
206 char *mybasename(const char *path)
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
207 {
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
208 char *mypath, *bname, *ret;
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
209
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
210 mypath = mystrdup(path);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
211 bname = basename(mypath);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
212 ret = mystrdup(bname);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
213
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
214 /* We don't free mypath until we have strdup()'ed bname, because
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
215 * basename() returns a pointer into mypath -- mortenp 20040527 */
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
216 myfree(mypath);
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
217
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
218 return ret;
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 100
diff changeset
219 }
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
220
351
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
221 char *cleanquotedp(const char *qpstr)
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
222 {
351
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
223 char *retstr;
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
224 char qc[3];
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
225 const char *c = qpstr;
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
226 long qcval;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
227 int i = 0;
351
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
228 size_t len;
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
229
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
230 /* XXX: We only use this function for checking whether the subject
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
231 * prefix is only present, so the recoding is neither guaranteed
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
232 * complete nor correct */
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
233
351
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
234 len = strlen(qpstr);
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
235 retstr = mymalloc(len + 1);
370a584ade9d Fix invalid write by 1 in cleanquotedp.
mmj
parents: 328
diff changeset
236 retstr[len] = '\0';
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
237 qc[2] = '\0';
597
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
238 while(c < qpstr+len) {
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
239 switch(*c) {
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
240 case '=':
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
241 c++;
597
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
242 if (!isxdigit(*c))
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
243 break;
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
244 qc[0] = *(c++);
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
245 if (!isxdigit(*c))
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
246 break;
ec9e2908f569 Fixed invalid read in cleanquotedp()
mortenp
parents: 508
diff changeset
247 qc[1] = *(c++);
265
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
248 qcval = strtol(qc, NULL, 16);
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
249 if(qcval)
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
250 retstr[i++] = (char)qcval;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
251 break;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
252 case '_':
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
253 retstr[i++] = ' ';
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
254 c++;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
255 break;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
256 default:
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
257 retstr[i++] = *(c++);
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
258 break;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
259 }
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
260 }
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
261
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
262 retstr[i] = '\0';
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
263
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
264 return retstr;
f28ab75abba7 Quoted printable subjects are now matched against prefix as well
mmj
parents: 245
diff changeset
265 }
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
266
641
436eea81bc20 Changed Message-ID headers to include FQDN (Ansgar Burchardt)
mortenp
parents: 605
diff changeset
267 char *genmsgid(const char *fqdn)
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
268 {
641
436eea81bc20 Changed Message-ID headers to include FQDN (Ansgar Burchardt)
mortenp
parents: 605
diff changeset
269 char buf[256];
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
270
764
dd5b87ddd51e Make *all* random strings produced the same length
Ben Schmidt
parents: 731
diff changeset
271 snprintf(buf, sizeof(buf), "Message-ID: <%ld-%d-mlmmj-%08x@%s>\n",
641
436eea81bc20 Changed Message-ID headers to include FQDN (Ansgar Burchardt)
mortenp
parents: 605
diff changeset
272 (long int)time(NULL), (int)getpid(), random_int(), fqdn);
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
273
641
436eea81bc20 Changed Message-ID headers to include FQDN (Ansgar Burchardt)
mortenp
parents: 605
diff changeset
274 return mystrdup(buf);
320
b2fe9c6cb9fe Bouncelife and crontab entry fix and a commented out genmsgid() function
mmj
parents: 316
diff changeset
275 }
422
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
276
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
277 char *gendatestr()
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
278 {
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
279 time_t t;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
280 struct tm gmttm, lttm;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
281 int dayyear;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
282 char *timestr;
448
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
283 const char *weekday = NULL, *month = NULL;
422
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
284
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
285 /* 6 + 26 + ' ' + timezone which is 5 + '\n\0' == 40 */
674
a81c74ff2995 Added more sanity checks (Thomas Jarosch)
mortenp
parents: 641
diff changeset
286 timestr = (char *)mymalloc(40);
422
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
287 t = time(NULL);
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
288
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
289 localtime_r(&t, &lttm);
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
290 gmtime_r(&t, &gmttm);
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
291
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
292 t = (((lttm.tm_hour - gmttm.tm_hour) * 60) +
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
293 (lttm.tm_min - gmttm.tm_min)) * 60;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
294
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
295 dayyear = lttm.tm_yday - gmttm.tm_yday;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
296 if(dayyear) {
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
297 if (dayyear == -1 || dayyear > 1)
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
298 t -= 24 * 60 * 60;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
299 else
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
300 t += 24 * 60 * 60;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
301 }
448
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
302
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
303 switch(lttm.tm_wday) {
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
304 case 0: weekday = "Sun";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
305 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
306 case 1: weekday = "Mon";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
307 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
308 case 2: weekday = "Tue";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
309 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
310 case 3: weekday = "Wed";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
311 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
312 case 4: weekday = "Thu";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
313 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
314 case 5: weekday = "Fri";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
315 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
316 case 6: weekday = "Sat";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
317 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
318 default:
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
319 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
320 }
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
321 switch(lttm.tm_mon) {
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
322 case 0: month = "Jan";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
323 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
324 case 1: month = "Feb";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
325 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
326 case 2: month = "Mar";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
327 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
328 case 3: month = "Apr";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
329 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
330 case 4: month = "May";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
331 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
332 case 5: month = "Jun";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
333 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
334 case 6: month = "Jul";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
335 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
336 case 7: month = "Aug";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
337 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
338 case 8: month = "Sep";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
339 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
340 case 9: month = "Oct";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
341 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
342 case 10: month = "Nov";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
343 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
344 case 11: month = "Dec";
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
345 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
346 default:
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
347 break;
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
348 }
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
349
422
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
350
508
10852fdfb3aa 1.2.7 postcommit
mmj
parents: 448
diff changeset
351 snprintf(timestr, 40, "Date: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",
448
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
352 weekday, lttm.tm_mday, month, lttm.tm_year + 1900,
2bb0ace27d96 Date: rfc conformance
mmj
parents: 422
diff changeset
353 lttm.tm_hour, lttm.tm_min, lttm.tm_sec, ((int)t)/36);
422
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
354
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
355 return timestr;
6952423ca1dd Lets add Message-Id: and Date:
mmj
parents: 351
diff changeset
356 }