Replaced folder_exists() by a call to create_folder().
[mmh] / sbr / utils.c
1 /*
2 ** utils.c -- various utility routines
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15 /*
16 ** We allocate space for messages (msgs array)
17 ** this number of elements at a time.
18 */
19 #define MAXMSGS 256
20
21 /*
22 ** Safely call malloc
23 */
24 void *
25 mh_xmalloc(size_t size)
26 {
27         void *memory;
28
29         if (size == 0)
30                 adios(NULL, "Tried to malloc 0 bytes");
31
32         memory = malloc(size);
33         if (!memory)
34                 adios(NULL, "Malloc failed");
35
36         return memory;
37 }
38
39 /*
40 ** Safely call realloc
41 */
42 void *
43 mh_xrealloc(void *ptr, size_t size)
44 {
45         void *memory;
46
47         /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
48         if (!ptr) {
49                 return mh_xmalloc(size);
50         }
51
52         if (size == 0)
53                 adios(NULL, "Tried to realloc 0 bytes");
54
55         memory = realloc(ptr, size);
56         if (!memory)
57                 adios(NULL, "Realloc failed");
58
59         return memory;
60 }
61
62 /*
63 ** Return the present working directory, if the current directory does not
64 ** exist, or is too long, make / the pwd.
65 */
66 char *
67 pwd(void)
68 {
69         register char *cp;
70         static char curwd[PATH_MAX];
71
72         if (!getcwd(curwd, PATH_MAX)) {
73                 admonish(NULL, "unable to determine working directory");
74                 if (!mypath || !*mypath ||
75                                 (strcpy(curwd, mypath), chdir(curwd)) == -1) {
76                         strcpy(curwd, "/");
77                         chdir(curwd);
78                 }
79                 return curwd;
80         }
81
82         if ((cp = curwd + strlen(curwd) - 1) > curwd && *cp == '/') {
83                 /* strip trailing slash */
84                 *cp = '\0';
85         }
86
87         return curwd;
88 }
89
90 /*
91 ** add   -- If "s1" is NULL, this routine just creates a
92 **       -- copy of "s2" into newly malloc'ed memory.
93 **       -- (use getcpy() instead in this case)
94 **       --
95 **       -- If "s1" is not NULL, then copy the concatenation
96 **       -- of "s1" and "s2" (note the order) into newly
97 **       -- malloc'ed memory.  Then free "s1".
98 */
99 char *
100 add(char *s2, char *s1)
101 {
102         char *cp;
103         size_t len1 = 0, len2 = 0;
104
105         if (s1)
106                 len1 = strlen(s1);
107         if (s2)
108                 len2 = strlen(s2);
109
110         cp = mh_xmalloc(len1 + len2 + 1);
111
112         /* Copy s1 and free it */
113         if (s1) {
114                 memcpy(cp, s1, len1);
115                 free(s1);
116         }
117
118         /* Copy s2 */
119         if (s2)
120                 memcpy(cp + len1, s2, len2);
121
122         /* Now NULL terminate the string */
123         cp[len1 + len2] = '\0';
124
125         return cp;
126 }
127
128
129 /*
130 ** create_folder
131 **     Check to see if a folder exists, if not, prompt the user to create it.
132 */
133 void
134 create_folder(char *folder, int autocreate, void (*done_callback)(int))
135 {
136         struct stat st;
137         extern int errno;
138         char *cp;
139
140         if (stat(folder, &st) == -1) {
141                 if (errno != ENOENT)
142                         adios(folder, "error on folder");
143                 if (autocreate == 0) {
144                         /* ask before creating folder */
145                         cp = concat("Create folder \"", folder, "\"? ", NULL);
146                         if (!getanswer(cp))
147                                 done_callback(1);
148                         free(cp);
149                 } else if (autocreate == -1) {
150                         /* do not create, so exit */
151                         done_callback(1);
152                 }
153                 if (!makedir(folder))
154                         adios(NULL, "unable to create folder %s", folder);
155         }
156 }
157
158 /*
159 ** num_digits
160 **     Return the number of digits in a nonnegative integer.
161 */
162 int
163 num_digits(int n)
164 {
165         int ndigits = 0;
166
167         /* Sanity check */
168         if (n < 0)
169                 adios(NULL, "oops, num_digits called with negative value");
170
171         if (n == 0)
172                 return 1;
173
174         while (n) {
175                 n /= 10;
176                 ndigits++;
177         }
178
179         return ndigits;
180 }
181
182 /*
183 ** Append a message arg to an array of them, resizing it if necessary.
184 ** The function is written to suit the arg parsing code it was extracted
185 ** from, and will probably be changed when the other code is cleaned up.
186 */
187 void
188 app_msgarg(struct msgs_array *msgs, char *cp)
189 {
190         if(msgs->size >= msgs->max)
191                 msgs->msgs = mh_xrealloc(msgs->msgs,
192                                 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
193         msgs->msgs[msgs->size++] = cp;
194 }