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) {
82 if (chdir (curwd
) < 0) {
83 advise (curwd
, "chdir");
89 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
96 * add -- If "s1" is NULL, this routine just creates a
97 * -- copy of "s2" into newly malloc'ed memory.
99 * -- If "s1" is not NULL, then copy the concatenation
100 * -- of "s1" and "s2" (note the order) into newly
101 * -- malloc'ed memory. Then free "s1".
104 add (const char *s2
, char *s1
)
107 size_t len1
= 0, len2
= 0;
114 cp
= mh_xmalloc (len1
+ len2
+ 1);
116 /* Copy s1 and free it */
118 memcpy (cp
, s1
, len1
);
124 memcpy (cp
+ len1
, s2
, len2
);
126 /* Now NULL terminate the string */
127 cp
[len1
+ len2
] = '\0';
134 * Append an item to a comma separated list
137 addlist (char *list
, const char *item
)
140 list
= add (", ", list
);
142 return add (item
, list
);
147 * Check to see if a folder exists.
149 int folder_exists(const char *folder
)
154 if (stat (folder
, &st
) == -1) {
155 /* The folder either doesn't exist, or we hit an error. Either way
160 /* We can see a folder with the right name */
170 * Check to see if a folder exists, if not, prompt the user to create
173 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
179 if (stat (folder
, &st
) == -1) {
181 adios (folder
, "error on folder");
182 if (autocreate
== 0) {
183 /* ask before creating folder */
184 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
188 } else if (autocreate
== -1) {
189 /* do not create, so exit */
192 if (!makedir (folder
))
193 adios (NULL
, "unable to create folder %s", folder
);
199 * Return the number of digits in a nonnegative integer.
208 adios (NULL
, "oops, num_digits called with negative value");
222 * Append a message arg to an array of them, resizing it if necessary.
223 * Really a simple vector-of-(char *) maintenance routine.
226 app_msgarg(struct msgs_array
*msgs
, char *cp
)
228 if(msgs
->size
>= msgs
->max
) {
229 msgs
->max
+= MAXMSGS
;
230 msgs
->msgs
= mh_xrealloc(msgs
->msgs
,
231 msgs
->max
* sizeof(*msgs
->msgs
));
233 msgs
->msgs
[msgs
->size
++] = cp
;
237 * Append a message number to an array of them, resizing it if necessary.
238 * Like app_msgarg, but with a vector-of-ints instead.
242 app_msgnum(struct msgnum_array
*msgs
, int msgnum
)
244 if (msgs
->size
>= msgs
->max
) {
245 msgs
->max
+= MAXMSGS
;
246 msgs
->msgnums
= mh_xrealloc(msgs
->msgnums
,
247 msgs
->max
* sizeof(*msgs
->msgnums
));
249 msgs
->msgnums
[msgs
->size
++] = msgnum
;
252 /* Open a form or components file */
254 open_form(char **form
, char *def
)
258 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
259 adios (*form
, "unable to open form file");
261 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
262 adios (def
, "unable to open default components file");
270 * Finds first occurrence of str in buf. buf is not a C string but a
271 * byte array of length buflen. str is a null-terminated C string.
272 * find_str() does not modify buf but passes back a non-const char *
273 * pointer so that the caller can modify it.
276 find_str (const char buf
[], size_t buflen
, const char *str
) {
277 const size_t len
= strlen (str
);
280 for (i
= 0; i
+ len
<= buflen
; ++i
, ++buf
) {
281 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
289 * Finds last occurrence of str in buf. buf is not a C string but a
290 * byte array of length buflen. str is a null-terminated C string.
291 * find_str() does not modify buf but passes back a non-const char *
292 * pointer so that the caller can modify it.
295 rfind_str (const char buf
[], size_t buflen
, const char *str
) {
296 const size_t len
= strlen (str
);
299 for (i
= 0, buf
+= buflen
- len
; i
+ len
<= buflen
; ++i
, --buf
) {
300 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
307 /* POSIX doesn't have strcasestr() so emulate it. */
309 nmh_strcasestr (const char *s1
, const char *s2
) {
310 const size_t len
= strlen (s2
);
312 if (isupper ((unsigned char) s2
[0]) || islower ((unsigned char)s2
[0])) {
314 first
[0] = (char) toupper ((unsigned char) s2
[0]);
315 first
[1] = (char) tolower ((unsigned char) s2
[0]);
318 for (s1
= strpbrk (s1
, first
); s1
; s1
= strpbrk (++s1
, first
)) {
319 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
322 for (s1
= strchr (s1
, s2
[0]); s1
; s1
= strchr (++s1
, s2
[0])) {
323 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
332 nmh_init(const char *argv0
, int read_context
) {
333 if (! setlocale(LC_ALL
, "")) {
334 admonish(NULL
, "setlocale failed, check your LC_ALL, LC_CTYPE, and "
335 "LANG environment variables");
338 invo_name
= r1bindex ((char *) argv0
, '/');
340 if (setup_signal_handlers()) {
341 admonish("sigaction", "unable to set up signal handlers");
344 /* POSIX atexit() does not define any error conditions. */
345 if (atexit(remove_registered_files_atexit
)) {
346 admonish("atexit", "unable to register atexit function");
353 int status
= context_foil(NULL
);
355 advise("", "failed to create minimal profile/conext");
362 /* Returns copy of argument str with all characters converted to upper
363 case, and trimmed whitespace (see cpytrim()) . */
365 upcase (const char *str
) {
366 char *up
= cpytrim (str
);
369 for (cp
= up
; *cp
; ++cp
) { *cp
= toupper ((unsigned char) *cp
); }
376 * Scan for any 8-bit characters. Return 1 if they exist.
378 * Scan up until the given endpoint (but not the actual endpoint itself).
379 * If the endpoint is NULL, scan until a '\0' is reached.
383 contains8bit(const char *start
, const char *end
)
388 while (*start
!= '\0' && (!end
|| (start
< end
)))
389 if (! isascii((unsigned char) *start
++))