3 * utils.c -- various utility routines
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.
15 extern int setup_signal_handlers();
18 extern void remove_registered_files_atexit();
22 * We allocate space for messages (msgs array)
23 * this number of elements at a time.
31 mh_xmalloc(size_t size
)
36 adios(NULL
, "Tried to malloc 0 bytes");
38 memory
= malloc(size
);
40 adios(NULL
, "Malloc failed");
49 mh_xrealloc(void *ptr
, size_t size
)
53 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
55 return mh_xmalloc(size
);
58 adios(NULL
, "Tried to realloc 0bytes");
60 memory
= realloc(ptr
, size
);
62 adios(NULL
, "Realloc failed");
71 mh_xcalloc(size_t nmemb
, size_t size
)
75 if (nmemb
== 0 || size
== 0)
76 adios(NULL
, "Tried to calloc 0 bytes");
78 if ((memory
= calloc(nmemb
, size
))) {
81 adios(NULL
, "calloc failed");
86 * Return the present working directory, if the current directory does not
87 * exist, or is too long, make / the pwd.
93 static char curwd
[PATH_MAX
];
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) {
100 if (chdir (curwd
) < 0) {
101 advise (curwd
, "chdir");
107 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
114 * add -- If "s1" is NULL, this routine just creates a
115 * -- copy of "s2" into newly malloc'ed memory.
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".
122 add (const char *s2
, char *s1
)
125 size_t len1
= 0, len2
= 0;
132 cp
= mh_xmalloc (len1
+ len2
+ 1);
134 /* Copy s1 and free it */
136 memcpy (cp
, s1
, len1
);
142 memcpy (cp
+ len1
, s2
, len2
);
144 /* Now NULL terminate the string */
145 cp
[len1
+ len2
] = '\0';
152 * Append an item to a comma separated list
155 addlist (char *list
, const char *item
)
158 list
= add (", ", list
);
160 return add (item
, list
);
165 * Check to see if a folder exists.
167 int folder_exists(const char *folder
)
172 if (stat (folder
, &st
) == -1) {
173 /* The folder either doesn't exist, or we hit an error. Either way
178 /* We can see a folder with the right name */
188 * Check to see if a folder exists, if not, prompt the user to create
191 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
197 if (stat (folder
, &st
) == -1) {
199 adios (folder
, "error on folder");
200 if (autocreate
== 0) {
201 /* ask before creating folder */
202 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
206 } else if (autocreate
== -1) {
207 /* do not create, so exit */
210 if (!makedir (folder
))
211 adios (NULL
, "unable to create folder %s", folder
);
217 * Return the number of digits in a nonnegative integer.
226 adios (NULL
, "oops, num_digits called with negative value");
240 * Append a message arg to an array of them, resizing it if necessary.
241 * Really a simple vector-of-(char *) maintenance routine.
244 app_msgarg(struct msgs_array
*msgs
, char *cp
)
246 if(msgs
->size
>= msgs
->max
) {
247 msgs
->max
+= MAXMSGS
;
248 msgs
->msgs
= mh_xrealloc(msgs
->msgs
,
249 msgs
->max
* sizeof(*msgs
->msgs
));
251 msgs
->msgs
[msgs
->size
++] = cp
;
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.
260 app_msgnum(struct msgnum_array
*msgs
, int msgnum
)
262 if (msgs
->size
>= msgs
->max
) {
263 msgs
->max
+= MAXMSGS
;
264 msgs
->msgnums
= mh_xrealloc(msgs
->msgnums
,
265 msgs
->max
* sizeof(*msgs
->msgnums
));
267 msgs
->msgnums
[msgs
->size
++] = msgnum
;
270 /* Open a form or components file */
272 open_form(char **form
, char *def
)
276 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
277 adios (*form
, "unable to open form file");
279 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
280 adios (def
, "unable to open default components file");
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.
294 find_str (const char buf
[], size_t buflen
, const char *str
) {
295 const size_t len
= strlen (str
);
298 for (i
= 0; i
+ len
<= buflen
; ++i
, ++buf
) {
299 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
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.
313 rfind_str (const char buf
[], size_t buflen
, const char *str
) {
314 const size_t len
= strlen (str
);
317 for (i
= 0, buf
+= buflen
- len
; i
+ len
<= buflen
; ++i
, --buf
) {
318 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
325 /* POSIX doesn't have strcasestr() so emulate it. */
327 nmh_strcasestr (const char *s1
, const char *s2
) {
328 const size_t len
= strlen (s2
);
330 if (isupper ((unsigned char) s2
[0]) || islower ((unsigned char)s2
[0])) {
332 first
[0] = (char) toupper ((unsigned char) s2
[0]);
333 first
[1] = (char) tolower ((unsigned char) s2
[0]);
336 for (s1
= strpbrk (s1
, first
); s1
; s1
= strpbrk (++s1
, first
)) {
337 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
340 for (s1
= strchr (s1
, s2
[0]); s1
; s1
= strchr (++s1
, s2
[0])) {
341 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
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");
356 invo_name
= r1bindex ((char *) argv0
, '/');
358 if (setup_signal_handlers()) {
359 admonish("sigaction", "unable to set up signal handlers");
362 /* POSIX atexit() does not define any error conditions. */
363 if (atexit(remove_registered_files_atexit
)) {
364 admonish("atexit", "unable to register atexit function");
371 int status
= context_foil(NULL
);
373 advise("", "failed to create minimal profile/conext");
380 /* Returns copy of argument str with all characters converted to upper
381 case, and trimmed whitespace (see cpytrim()) . */
383 upcase (const char *str
) {
384 char *up
= cpytrim (str
);
387 for (cp
= up
; *cp
; ++cp
) { *cp
= toupper ((unsigned char) *cp
); }
394 * Scan for any 8-bit characters. Return 1 if they exist.
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.
401 contains8bit(const char *start
, const char *end
)
406 while (*start
!= '\0' && (!end
|| (start
< end
)))
407 if (! isascii((unsigned char) *start
++))