2 ** utils.c -- various utility routines
4 ** This code is Copyright (c) 2006, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
16 ** We allocate space for messages (msgs array)
17 ** this number of elements at a time.
25 mh_xmalloc(size_t size)
30 adios(NULL, "Tried to malloc 0 bytes");
32 memory = malloc(size);
34 adios(NULL, "Malloc failed");
40 ** Safely call realloc
43 mh_xrealloc(void *ptr, size_t size)
47 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
49 return mh_xmalloc(size);
52 adios(NULL, "Tried to realloc 0bytes");
54 memory = realloc(ptr, size);
56 adios(NULL, "Realloc failed");
62 ** Return the present working directory, if the current directory does not
63 ** exist, or is too long, make / the pwd.
69 static char curwd[PATH_MAX];
71 if (!getcwd(curwd, PATH_MAX)) {
72 admonish(NULL, "unable to determine working directory");
73 if (!mypath || !*mypath
74 || (strcpy(curwd, mypath), chdir(curwd)) == -1) {
81 if ((cp = curwd + strlen(curwd) - 1) > curwd && *cp == '/')
88 ** add -- If "s1" is NULL, this routine just creates a
89 ** -- copy of "s2" into newly malloc'ed memory.
91 ** -- If "s1" is not NULL, then copy the concatenation
92 ** -- of "s1" and "s2" (note the order) into newly
93 ** -- malloc'ed memory. Then free "s1".
96 add(char *s2, char *s1)
99 size_t len1 = 0, len2 = 0;
106 cp = mh_xmalloc(len1 + len2 + 1);
108 /* Copy s1 and free it */
110 memcpy(cp, s1, len1);
116 memcpy(cp + len1, s2, len2);
118 /* Now NULL terminate the string */
119 cp[len1 + len2] = '\0';
126 ** Check to see if a folder exists.
129 folder_exists(char *folder)
134 if (stat(folder, &st) == -1) {
136 ** The folder either doesn't exist, or we hit an error.
137 ** Either way return a failure.
141 /* We can see a folder with the right name */
151 ** Check to see if a folder exists, if not, prompt the user to create it.
154 create_folder(char *folder, int autocreate, void (*done_callback)(int))
160 if (stat(folder, &st) == -1) {
162 adios(folder, "error on folder");
163 if (autocreate == 0) {
164 /* ask before creating folder */
165 cp = concat("Create folder \"", folder, "\"? ", NULL);
169 } else if (autocreate == -1) {
170 /* do not create, so exit */
173 if (!makedir(folder))
174 adios(NULL, "unable to create folder %s", folder);
180 ** Return the number of digits in a nonnegative integer.
189 adios(NULL, "oops, num_digits called with negative value");
203 ** Append a message arg to an array of them, resizing it if necessary.
204 ** The function is written to suit the arg parsing code it was extracted
205 ** from, and will probably be changed when the other code is cleaned up.
208 app_msgarg(struct msgs_array *msgs, char *cp)
210 if(msgs->size >= msgs->max)
211 msgs->msgs = mh_xrealloc(msgs->msgs,
212 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
213 msgs->msgs[msgs->size++] = cp;
216 /* Open a form or components file */
218 open_form(char **form, char *def)
222 if ((in = open(etcpath(*form), O_RDONLY)) == NOTOK)
223 adios(*form, "unable to open form file");
225 if ((in = open(etcpath(def), O_RDONLY)) == NOTOK)
226 adios(def, "unable to open default components file");