]> diplodocus.org Git - nmh/blob - sbr/utils.c
Another pass at cleaning up (some of) the manpages.
[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 <fcntl.h>
13
14 /* sbr/signals.c */
15 extern int setup_signal_handlers();
16
17 /* sbr/m_mktemp.c */
18 extern void remove_registered_files_atexit();
19
20
21 /*
22 * We allocate space for messages (msgs array)
23 * this number of elements at a time.
24 */
25 #define MAXMSGS 256
26
27 /*
28 * Safely call malloc
29 */
30 void *
31 mh_xmalloc(size_t size)
32 {
33 void *memory;
34
35 if (size == 0)
36 adios(NULL, "Tried to malloc 0 bytes");
37
38 memory = malloc(size);
39 if (!memory)
40 adios(NULL, "Malloc failed");
41
42 return memory;
43 }
44
45 /*
46 * Safely call realloc
47 */
48 void *
49 mh_xrealloc(void *ptr, size_t size)
50 {
51 void *memory;
52
53 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
54 if (!ptr)
55 return mh_xmalloc(size);
56
57 if (size == 0)
58 adios(NULL, "Tried to realloc 0bytes");
59
60 memory = realloc(ptr, size);
61 if (!memory)
62 adios(NULL, "Realloc failed");
63
64 return memory;
65 }
66
67 /*
68 * Safely call calloc
69 */
70 void *
71 mh_xcalloc(size_t nmemb, size_t size)
72 {
73 void *memory;
74
75 if (nmemb == 0 || size == 0)
76 adios(NULL, "Tried to calloc 0 bytes");
77
78 if ((memory = calloc(nmemb, size))) {
79 return memory;
80 } else {
81 adios(NULL, "calloc failed");
82 }
83 }
84
85 /*
86 * Return the present working directory, if the current directory does not
87 * exist, or is too long, make / the pwd.
88 */
89 char *
90 pwd(void)
91 {
92 register char *cp;
93 static char curwd[PATH_MAX];
94
95 if (!getcwd (curwd, PATH_MAX)) {
96 admonish (NULL, "unable to determine working directory");
97 if (!mypath || !*mypath
98 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
99 strcpy (curwd, "/");
100 if (chdir (curwd) < 0) {
101 advise (curwd, "chdir");
102 }
103 }
104 return curwd;
105 }
106
107 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
108 *cp = '\0';
109
110 return curwd;
111 }
112
113 /*
114 * add -- If "s1" is NULL, this routine just creates a
115 * -- copy of "s2" into newly malloc'ed memory.
116 * --
117 * -- If "s1" is not NULL, then copy the concatenation
118 * -- of "s1" and "s2" (note the order) into newly
119 * -- malloc'ed memory. Then free "s1".
120 */
121 char *
122 add (const char *s2, char *s1)
123 {
124 char *cp;
125 size_t len1 = 0, len2 = 0;
126
127 if (s1)
128 len1 = strlen (s1);
129 if (s2)
130 len2 = strlen (s2);
131
132 cp = mh_xmalloc (len1 + len2 + 1);
133
134 /* Copy s1 and free it */
135 if (s1) {
136 memcpy (cp, s1, len1);
137 free (s1);
138 }
139
140 /* Copy s2 */
141 if (s2)
142 memcpy (cp + len1, s2, len2);
143
144 /* Now NULL terminate the string */
145 cp[len1 + len2] = '\0';
146
147 return cp;
148 }
149
150 /*
151 * addlist
152 * Append an item to a comma separated list
153 */
154 char *
155 addlist (char *list, const char *item)
156 {
157 if (list)
158 list = add (", ", list);
159
160 return add (item, list);
161 }
162
163 /*
164 * folder_exists
165 * Check to see if a folder exists.
166 */
167 int folder_exists(const char *folder)
168 {
169 struct stat st;
170 int exists = 0;
171
172 if (stat (folder, &st) == -1) {
173 /* The folder either doesn't exist, or we hit an error. Either way
174 * return a failure.
175 */
176 exists = 0;
177 } else {
178 /* We can see a folder with the right name */
179 exists = 1;
180 }
181
182 return exists;
183 }
184
185
186 /*
187 * create_folder
188 * Check to see if a folder exists, if not, prompt the user to create
189 * it.
190 */
191 void create_folder(char *folder, int autocreate, void (*done_callback)(int))
192 {
193 struct stat st;
194 extern int errno;
195 char *cp;
196
197 if (stat (folder, &st) == -1) {
198 if (errno != ENOENT)
199 adios (folder, "error on folder");
200 if (autocreate == 0) {
201 /* ask before creating folder */
202 cp = concat ("Create folder \"", folder, "\"? ", NULL);
203 if (!getanswer (cp))
204 done_callback (1);
205 free (cp);
206 } else if (autocreate == -1) {
207 /* do not create, so exit */
208 done_callback (1);
209 }
210 if (!makedir (folder))
211 adios (NULL, "unable to create folder %s", folder);
212 }
213 }
214
215 /*
216 * num_digits
217 * Return the number of digits in a nonnegative integer.
218 */
219 int
220 num_digits (int n)
221 {
222 int ndigits = 0;
223
224 /* Sanity check */
225 if (n < 0)
226 adios (NULL, "oops, num_digits called with negative value");
227
228 if (n == 0)
229 return 1;
230
231 while (n) {
232 n /= 10;
233 ndigits++;
234 }
235
236 return ndigits;
237 }
238
239 /*
240 * Append a message arg to an array of them, resizing it if necessary.
241 * Really a simple vector-of-(char *) maintenance routine.
242 */
243 void
244 app_msgarg(struct msgs_array *msgs, char *cp)
245 {
246 if(msgs->size >= msgs->max) {
247 msgs->max += MAXMSGS;
248 msgs->msgs = mh_xrealloc(msgs->msgs,
249 msgs->max * sizeof(*msgs->msgs));
250 }
251 msgs->msgs[msgs->size++] = cp;
252 }
253
254 /*
255 * Append a message number to an array of them, resizing it if necessary.
256 * Like app_msgarg, but with a vector-of-ints instead.
257 */
258
259 void
260 app_msgnum(struct msgnum_array *msgs, int msgnum)
261 {
262 if (msgs->size >= msgs->max) {
263 msgs->max += MAXMSGS;
264 msgs->msgnums = mh_xrealloc(msgs->msgnums,
265 msgs->max * sizeof(*msgs->msgnums));
266 }
267 msgs->msgnums[msgs->size++] = msgnum;
268 }
269
270 /* Open a form or components file */
271 int
272 open_form(char **form, char *def)
273 {
274 int in;
275 if (*form) {
276 if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK)
277 adios (*form, "unable to open form file");
278 } else {
279 if ((in = open (etcpath (def), O_RDONLY)) == NOTOK)
280 adios (def, "unable to open default components file");
281 *form = def;
282 }
283 return in;
284 }
285
286
287 /*
288 * Finds first occurrence of str in buf. buf is not a C string but a
289 * byte array of length buflen. str is a null-terminated C string.
290 * find_str() does not modify buf but passes back a non-const char *
291 * pointer so that the caller can modify it.
292 */
293 char *
294 find_str (const char buf[], size_t buflen, const char *str) {
295 const size_t len = strlen (str);
296 size_t i;
297
298 for (i = 0; i + len <= buflen; ++i, ++buf) {
299 if (! memcmp (buf, str, len)) return (char *) buf;
300 }
301
302 return NULL;
303 }
304
305
306 /*
307 * Finds last occurrence of str in buf. buf is not a C string but a
308 * byte array of length buflen. str is a null-terminated C string.
309 * find_str() does not modify buf but passes back a non-const char *
310 * pointer so that the caller can modify it.
311 */
312 char *
313 rfind_str (const char buf[], size_t buflen, const char *str) {
314 const size_t len = strlen (str);
315 size_t i;
316
317 for (i = 0, buf += buflen - len; i + len <= buflen; ++i, --buf) {
318 if (! memcmp (buf, str, len)) return (char *) buf;
319 }
320
321 return NULL;
322 }
323
324
325 /* POSIX doesn't have strcasestr() so emulate it. */
326 char *
327 nmh_strcasestr (const char *s1, const char *s2) {
328 const size_t len = strlen (s2);
329
330 if (isupper ((unsigned char) s2[0]) || islower ((unsigned char)s2[0])) {
331 char first[3];
332 first[0] = (char) toupper ((unsigned char) s2[0]);
333 first[1] = (char) tolower ((unsigned char) s2[0]);
334 first[2] = '\0';
335
336 for (s1 = strpbrk (s1, first); s1; s1 = strpbrk (++s1, first)) {
337 if (! strncasecmp (s1, s2, len)) return (char *) s1;
338 }
339 } else {
340 for (s1 = strchr (s1, s2[0]); s1; s1 = strchr (++s1, s2[0])) {
341 if (! strncasecmp (s1, s2, len)) return (char *) s1;
342 }
343 }
344
345 return NULL;
346 }
347
348
349 int
350 nmh_init(const char *argv0, int read_context) {
351 if (! setlocale(LC_ALL, "")) {
352 admonish(NULL, "setlocale failed, check your LC_ALL, LC_CTYPE, and "
353 "LANG environment variables");
354 }
355
356 invo_name = r1bindex ((char *) argv0, '/');
357
358 if (setup_signal_handlers()) {
359 admonish("sigaction", "unable to set up signal handlers");
360 }
361
362 /* POSIX atexit() does not define any error conditions. */
363 if (atexit(remove_registered_files_atexit)) {
364 admonish("atexit", "unable to register atexit function");
365 }
366
367 if (read_context) {
368 context_read();
369 return OK;
370 } else {
371 int status = context_foil(NULL);
372 if (status != OK) {
373 advise("", "failed to create minimal profile/conext");
374 }
375 return status;
376 }
377 }
378
379
380 /* Returns copy of argument str with all characters converted to upper
381 case, and trimmed whitespace (see cpytrim()) . */
382 char *
383 upcase (const char *str) {
384 char *up = cpytrim (str);
385 char *cp;
386
387 for (cp = up; *cp; ++cp) { *cp = toupper ((unsigned char) *cp); }
388
389 return up;
390 }
391
392
393 /*
394 * Scan for any 8-bit characters. Return 1 if they exist.
395 *
396 * Scan up until the given endpoint (but not the actual endpoint itself).
397 * If the endpoint is NULL, scan until a '\0' is reached.
398 */
399
400 int
401 contains8bit(const char *start, const char *end)
402 {
403 if (! start)
404 return 0;
405
406 while (*start != '\0' && (!end || (start < end)))
407 if (! isascii((unsigned char) *start++))
408 return 1;
409
410 return 0;
411 }