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");
68 * Return the present working directory, if the current directory does not
69 * exist, or is too long, make / the pwd.
75 static char curwd
[PATH_MAX
];
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) {
87 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
94 * add -- If "s1" is NULL, this routine just creates a
95 * -- copy of "s2" into newly malloc'ed memory.
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".
102 add (const char *s2
, char *s1
)
105 size_t len1
= 0, len2
= 0;
112 cp
= mh_xmalloc (len1
+ len2
+ 1);
114 /* Copy s1 and free it */
116 memcpy (cp
, s1
, len1
);
122 memcpy (cp
+ len1
, s2
, len2
);
124 /* Now NULL terminate the string */
125 cp
[len1
+ len2
] = '\0';
132 * Append an item to a comma separated list
135 addlist (char *list
, const char *item
)
138 list
= add (", ", list
);
140 return add (item
, list
);
145 * Check to see if a folder exists.
147 int folder_exists(const char *folder
)
152 if (stat (folder
, &st
) == -1) {
153 /* The folder either doesn't exist, or we hit an error. Either way
158 /* We can see a folder with the right name */
168 * Check to see if a folder exists, if not, prompt the user to create
171 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
177 if (stat (folder
, &st
) == -1) {
179 adios (folder
, "error on folder");
180 if (autocreate
== 0) {
181 /* ask before creating folder */
182 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
186 } else if (autocreate
== -1) {
187 /* do not create, so exit */
190 if (!makedir (folder
))
191 adios (NULL
, "unable to create folder %s", folder
);
197 * Return the number of digits in a nonnegative integer.
206 adios (NULL
, "oops, num_digits called with negative value");
220 * Append a message arg to an array of them, resizing it if necessary.
221 * Really a simple vector-of-(char *) maintenance routine.
224 app_msgarg(struct msgs_array
*msgs
, char *cp
)
226 if(msgs
->size
>= msgs
->max
) {
227 msgs
->max
+= MAXMSGS
;
228 msgs
->msgs
= mh_xrealloc(msgs
->msgs
,
229 msgs
->max
* sizeof(*msgs
->msgs
));
231 msgs
->msgs
[msgs
->size
++] = cp
;
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.
240 app_msgnum(struct msgnum_array
*msgs
, int msgnum
)
242 if (msgs
->size
>= msgs
->max
) {
243 msgs
->max
+= MAXMSGS
;
244 msgs
->msgnums
= mh_xrealloc(msgs
->msgnums
,
245 msgs
->max
* sizeof(*msgs
->msgnums
));
247 msgs
->msgnums
[msgs
->size
++] = msgnum
;
250 /* Open a form or components file */
252 open_form(char **form
, char *def
)
256 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
257 adios (*form
, "unable to open form file");
259 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
260 adios (def
, "unable to open default components file");
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.
274 find_str (const char buf
[], size_t buflen
, const char *str
) {
275 const size_t len
= strlen (str
);
278 for (i
= 0; i
+ len
<= buflen
; ++i
, ++buf
) {
279 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
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.
293 rfind_str (const char buf
[], size_t buflen
, const char *str
) {
294 const size_t len
= strlen (str
);
297 for (i
= 0, buf
+= buflen
- len
; i
+ len
<= buflen
; ++i
, --buf
) {
298 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
305 /* POSIX doesn't have strcasestr() so emulate it. */
307 nmh_strcasestr (const char *s1
, const char *s2
) {
308 const size_t len
= strlen (s2
);
310 if (isupper ((unsigned char) s2
[0]) || islower ((unsigned char)s2
[0])) {
312 first
[0] = (char) toupper ((unsigned char) s2
[0]);
313 first
[1] = (char) tolower ((unsigned char) s2
[0]);
316 for (s1
= strpbrk (s1
, first
); s1
; s1
= strpbrk (++s1
, first
)) {
317 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
320 for (s1
= strchr (s1
, s2
[0]); s1
; s1
= strchr (++s1
, s2
[0])) {
321 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
330 nmh_init(const char *argv0
, int read_context
) {
331 setlocale(LC_ALL
, "");
333 invo_name
= r1bindex ((char *) argv0
, '/');
335 if (setup_signal_handlers()) {
336 admonish("sigaction", "unable to set up signal handlers");
339 /* POSIX atexit() does not define any error conditions. */
340 if (atexit(remove_registered_files_atexit
)) {
341 admonish("atexit", "unable to register atexit function");
348 int status
= context_foil(NULL
);
350 advise("", "failed to create minimal profile/conext");
357 /* Returns copy of argument str with all characters converted to upper
358 case, and trimmed whitespace (see cpytrim()) . */
360 upcase (const char *str
) {
361 char *up
= cpytrim (str
);
364 for (cp
= up
; *cp
; ++cp
) { *cp
= toupper ((unsigned char) *cp
); }
371 * Scan for any 8-bit characters. Return 1 if they exist.
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.
378 contains8bit(const char *start
, const char *end
)
383 while (*start
!= '\0' && (!end
|| (start
< end
)))
384 if (! isascii((unsigned char) *start
++))