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.
25 ** Safely call realloc
28 mh_xrealloc(void *ptr, size_t size)
32 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
34 return mh_xcalloc(1, size);
37 adios(EX_SOFTWARE, NULL, "Tried to realloc 0 bytes");
40 memory = realloc(ptr, size);
42 adios(EX_OSERR, NULL, "Realloc failed");
52 mh_xcalloc(size_t nmemb, size_t size)
56 if (nmemb == 0 || size == 0) {
57 adios(EX_SOFTWARE, NULL, "Tried to calloc 0 bytes");
60 if ((memory = calloc(nmemb, size))) {
63 adios(EX_OSERR, NULL, "calloc 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 == '/') {
88 /* strip trailing slash */
96 ** add -- If "s1" is NULL, this routine just creates a
97 ** -- copy of "s2" into newly malloc'ed memory.
98 ** -- (use getcpy() instead in this case)
100 ** -- If "s1" is not NULL, then copy the concatenation
101 ** -- of "s1" and "s2" (note the order) into newly
102 ** -- malloc'ed memory. Then free "s1".
105 add(char *s2, char *s1)
108 size_t len1 = 0, len2 = 0;
117 cp = mh_xcalloc(len1 + len2 + 1, sizeof(char));
119 /* Copy s1 and free it */
121 memcpy(cp, s1, len1);
127 memcpy(cp + len1, s2, len2);
130 /* Now NULL terminate the string */
131 cp[len1 + len2] = '\0';
139 ** Check to see if a folder exists, if not, prompt the user to create it.
142 create_folder(char *folder, int autocreate, void (*done_callback)(int))
148 if (stat(folder, &st) == -1) {
149 if (errno != ENOENT) {
150 adios(EX_IOERR, folder, "error on folder");
152 if (autocreate == 0) {
153 /* ask before creating folder */
154 cp = concat("Create folder \"", folder, "\"? ", NULL);
155 if (!getanswer(cp)) {
156 done_callback(EX_CANTCREAT);
159 } else if (autocreate == -1) {
160 /* do not create, so exit */
161 done_callback(EX_CANTCREAT);
163 if (!makedir(folder)) {
164 adios(EX_CANTCREAT, NULL, "unable to create folder %s", folder);
171 ** Return the number of digits in a nonnegative integer.
180 adios(EX_SOFTWARE, NULL, "oops, num_digits called with negative value");
196 ** Append a message arg to an array of them, resizing it if necessary.
197 ** The function is written to suit the arg parsing code it was extracted
198 ** from, and will probably be changed when the other code is cleaned up.
201 app_msgarg(struct msgs_array *msgs, char *cp)
203 if(msgs->size >= msgs->max) {
204 msgs->msgs = mh_xrealloc(msgs->msgs,
205 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
207 msgs->msgs[msgs->size++] = cp;