]> diplodocus.org Git - nmh/blob - sbr/utils.c
A bug fix and an enhancement to mhfixmsg based on patches
[nmh] / sbr / utils.c
1
2 /*
3 * utils.c -- various utility routines
4 *
5 * This code is Copyright (c) 2006, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 /*
17 * We allocate space for messages (msgs array)
18 * this number of elements at a time.
19 */
20 #define MAXMSGS 256
21
22 /*
23 * Safely call malloc
24 */
25 void *
26 mh_xmalloc(size_t size)
27 {
28 void *memory;
29
30 if (size == 0)
31 adios(NULL, "Tried to malloc 0 bytes");
32
33 memory = malloc(size);
34 if (!memory)
35 adios(NULL, "Malloc failed");
36
37 return memory;
38 }
39
40 /*
41 * Safely call realloc
42 */
43 void *
44 mh_xrealloc(void *ptr, size_t size)
45 {
46 void *memory;
47
48 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
49 if (!ptr)
50 return mh_xmalloc(size);
51
52 if (size == 0)
53 adios(NULL, "Tried to realloc 0bytes");
54
55 memory = realloc(ptr, size);
56 if (!memory)
57 adios(NULL, "Realloc failed");
58
59 return memory;
60 }
61
62 /*
63 * Return the present working directory, if the current directory does not
64 * exist, or is too long, make / the pwd.
65 */
66 char *
67 pwd(void)
68 {
69 register char *cp;
70 static char curwd[PATH_MAX];
71
72 if (!getcwd (curwd, PATH_MAX)) {
73 admonish (NULL, "unable to determine working directory");
74 if (!mypath || !*mypath
75 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
76 strcpy (curwd, "/");
77 chdir (curwd);
78 }
79 return curwd;
80 }
81
82 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
83 *cp = '\0';
84
85 return curwd;
86 }
87
88 /*
89 * add -- If "s1" is NULL, this routine just creates a
90 * -- copy of "s2" into newly malloc'ed memory.
91 * --
92 * -- If "s1" is not NULL, then copy the concatenation
93 * -- of "s1" and "s2" (note the order) into newly
94 * -- malloc'ed memory. Then free "s1".
95 */
96 char *
97 add (const char *s2, char *s1)
98 {
99 char *cp;
100 size_t len1 = 0, len2 = 0;
101
102 if (s1)
103 len1 = strlen (s1);
104 if (s2)
105 len2 = strlen (s2);
106
107 cp = mh_xmalloc (len1 + len2 + 1);
108
109 /* Copy s1 and free it */
110 if (s1) {
111 memcpy (cp, s1, len1);
112 free (s1);
113 }
114
115 /* Copy s2 */
116 if (s2)
117 memcpy (cp + len1, s2, len2);
118
119 /* Now NULL terminate the string */
120 cp[len1 + len2] = '\0';
121
122 return cp;
123 }
124
125 /*
126 * addlist
127 * Append an item to a comma separated list
128 */
129 char *
130 addlist (char *list, const char *item)
131 {
132 if (list)
133 list = add (", ", list);
134
135 return add (item, list);
136 }
137
138 /*
139 * folder_exists
140 * Check to see if a folder exists.
141 */
142 int folder_exists(const char *folder)
143 {
144 struct stat st;
145 int exists = 0;
146
147 if (stat (folder, &st) == -1) {
148 /* The folder either doesn't exist, or we hit an error. Either way
149 * return a failure.
150 */
151 exists = 0;
152 } else {
153 /* We can see a folder with the right name */
154 exists = 1;
155 }
156
157 return exists;
158 }
159
160
161 /*
162 * create_folder
163 * Check to see if a folder exists, if not, prompt the user to create
164 * it.
165 */
166 void create_folder(char *folder, int autocreate, void (*done_callback)(int))
167 {
168 struct stat st;
169 extern int errno;
170 char *cp;
171
172 if (stat (folder, &st) == -1) {
173 if (errno != ENOENT)
174 adios (folder, "error on folder");
175 if (autocreate == 0) {
176 /* ask before creating folder */
177 cp = concat ("Create folder \"", folder, "\"? ", NULL);
178 if (!getanswer (cp))
179 done_callback (1);
180 free (cp);
181 } else if (autocreate == -1) {
182 /* do not create, so exit */
183 done_callback (1);
184 }
185 if (!makedir (folder))
186 adios (NULL, "unable to create folder %s", folder);
187 }
188 }
189
190 /*
191 * num_digits
192 * Return the number of digits in a nonnegative integer.
193 */
194 int
195 num_digits (int n)
196 {
197 int ndigits = 0;
198
199 /* Sanity check */
200 if (n < 0)
201 adios (NULL, "oops, num_digits called with negative value");
202
203 if (n == 0)
204 return 1;
205
206 while (n) {
207 n /= 10;
208 ndigits++;
209 }
210
211 return ndigits;
212 }
213
214 /*
215 * Append a message arg to an array of them, resizing it if necessary.
216 * Really a simple vector-of-(char *) maintenance routine.
217 */
218 void
219 app_msgarg(struct msgs_array *msgs, char *cp)
220 {
221 if(msgs->size >= msgs->max) {
222 msgs->max += MAXMSGS;
223 msgs->msgs = mh_xrealloc(msgs->msgs,
224 msgs->max * sizeof(*msgs->msgs));
225 }
226 msgs->msgs[msgs->size++] = cp;
227 }
228
229 /*
230 * Append a message number to an array of them, resizing it if necessary.
231 * Like app_msgarg, but with a vector-of-ints instead.
232 */
233
234 void
235 app_msgnum(struct msgnum_array *msgs, int msgnum)
236 {
237 if (msgs->size >= msgs->max) {
238 msgs->max += MAXMSGS;
239 msgs->msgnums = mh_xrealloc(msgs->msgnums,
240 msgs->max * sizeof(*msgs->msgnums));
241 }
242 msgs->msgnums[msgs->size++] = msgnum;
243 }
244
245 /* Open a form or components file */
246 int
247 open_form(char **form, char *def)
248 {
249 int in;
250 if (*form) {
251 if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK)
252 adios (*form, "unable to open form file");
253 } else {
254 if ((in = open (etcpath (def), O_RDONLY)) == NOTOK)
255 adios (def, "unable to open default components file");
256 *form = def;
257 }
258 return in;
259 }
260
261
262 /*
263 * Finds first occurrence of str in buf. buf is not a C string but a
264 * byte array of length buflen. str is a null-terminated C string.
265 * find_str() does not modify buf but passes back a non-const char *
266 * pointer so that the caller can modify it.
267 */
268 char *
269 find_str (const char buf[], size_t buflen, const char *str) {
270 const size_t len = strlen (str);
271 size_t i;
272
273 for (i = 0; i + len <= buflen; ++i, ++buf) {
274 if (! memcmp (buf, str, len)) return (char *) buf;
275 }
276
277 return NULL;
278 }
279
280
281 /*
282 * Finds last occurrence of str in buf. buf is not a C string but a
283 * byte array of length buflen. str is a null-terminated C string.
284 * find_str() does not modify buf but passes back a non-const char *
285 * pointer so that the caller can modify it.
286 */
287 char *
288 rfind_str (const char buf[], size_t buflen, const char *str) {
289 const size_t len = strlen (str);
290 size_t i;
291
292 for (i = 0, buf += buflen - len; i + len <= buflen; ++i, --buf) {
293 if (! memcmp (buf, str, len)) return (char *) buf;
294 }
295
296 return NULL;
297 }
298
299
300 /* POSIX doesn't have strcasestr() so emulate it. */
301 char *
302 nmh_strcasestr (const char *s1, const char *s2) {
303 const size_t len = strlen (s2);
304
305 if (isupper ((unsigned char) s2[0]) || islower ((unsigned char)s2[0])) {
306 char first[3];
307 first[0] = (char) toupper ((unsigned char) s2[0]);
308 first[1] = (char) tolower ((unsigned char) s2[0]);
309 first[2] = '\0';
310
311 for (s1 = strpbrk (s1, first); s1; s1 = strpbrk (++s1, first)) {
312 if (! strncasecmp (s1, s2, len)) return (char *) s1;
313 }
314 } else {
315 for (s1 = strchr (s1, s2[0]); s1; s1 = strchr (++s1, s2[0])) {
316 if (! strncasecmp (s1, s2, len)) return (char *) s1;
317 }
318 }
319
320 return NULL;
321 }