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