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.
19 ** We allocate space for messages (msgs array)
20 ** this number of elements at a time.
28 mh_xmalloc(size_t size)
33 adios(EX_SOFTWARE, NULL, "Tried to malloc 0 bytes");
36 memory = malloc(size);
38 adios(EX_OSERR, NULL, "Malloc failed");
45 ** Safely call realloc
48 mh_xrealloc(void *ptr, size_t size)
52 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
54 return mh_xcalloc((size_t) 1, size);
57 adios(EX_SOFTWARE, NULL, "Tried to realloc 0 bytes");
60 memory = realloc(ptr, size);
62 adios(EX_OSERR, NULL, "Realloc failed");
72 mh_xcalloc(size_t nmemb, size_t size)
76 if (nmemb == 0 || size == 0) {
77 adios(EX_SOFTWARE, NULL, "Tried to calloc 0 bytes");
80 if ((memory = calloc(nmemb, size))) {
83 adios(EX_OSERR, NULL, "calloc failed");
88 ** Return the present working directory, if the current directory does not
89 ** exist, or is too long, make / the pwd.
95 static char curwd[PATH_MAX];
97 if (!getcwd(curwd, PATH_MAX)) {
98 admonish(NULL, "unable to determine working directory");
99 if (!mypath || !*mypath ||
100 (strcpy(curwd, mypath), chdir(curwd)) == -1) {
107 if ((cp = curwd + strlen(curwd) - 1) > curwd && *cp == '/') {
108 /* strip trailing slash */
116 ** add -- If "s1" is NULL, this routine just creates a
117 ** -- copy of "s2" into newly malloc'ed memory.
118 ** -- (use getcpy() instead in this case)
120 ** -- If "s1" is not NULL, then copy the concatenation
121 ** -- of "s1" and "s2" (note the order) into newly
122 ** -- malloc'ed memory. Then free "s1".
125 add(char *s2, char *s1)
128 size_t len1 = 0, len2 = 0;
137 cp = mh_xmalloc(len1 + len2 + 1);
139 /* Copy s1 and free it */
141 memcpy(cp, s1, len1);
147 memcpy(cp + len1, s2, len2);
150 /* Now NULL terminate the string */
151 cp[len1 + len2] = '\0';
159 ** Check to see if a folder exists, if not, prompt the user to create it.
162 create_folder(char *folder, int autocreate, void (*done_callback)(int))
168 if (stat(folder, &st) == -1) {
169 if (errno != ENOENT) {
170 adios(EX_IOERR, folder, "error on folder");
172 if (autocreate == 0) {
173 /* ask before creating folder */
174 cp = concat("Create folder \"", folder, "\"? ", NULL);
175 if (!getanswer(cp)) {
176 done_callback(EX_CANTCREAT);
179 } else if (autocreate == -1) {
180 /* do not create, so exit */
181 done_callback(EX_CANTCREAT);
183 if (!makedir(folder)) {
184 adios(EX_CANTCREAT, NULL, "unable to create folder %s", folder);
191 ** Return the number of digits in a nonnegative integer.
200 adios(EX_SOFTWARE, NULL, "oops, num_digits called with negative value");
216 ** Append a message arg to an array of them, resizing it if necessary.
217 ** The function is written to suit the arg parsing code it was extracted
218 ** from, and will probably be changed when the other code is cleaned up.
221 app_msgarg(struct msgs_array *msgs, char *cp)
223 if(msgs->size >= msgs->max) {
224 msgs->msgs = mh_xrealloc(msgs->msgs,
225 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
227 msgs->msgs[msgs->size++] = cp;