5c5b0f5149a01dfd19cf1044e9b799acbb0b26b9
[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 ** folder_exists
130 **     Check to see if a folder exists.
131 */
132 int
133 folder_exists(char *folder)
134 {
135         struct stat st;
136         int exists = 0;
137
138         if (stat(folder, &st) == -1) {
139                 /*
140                 ** The folder either doesn't exist, or we hit an error.
141                 ** Either way return a failure.
142                 */
143                 exists = 0;
144         } else {
145                 /* We can see a folder with the right name */
146                 exists = 1;
147         }
148
149         return exists;
150 }
151
152
153 /*
154 ** create_folder
155 **     Check to see if a folder exists, if not, prompt the user to create it.
156 */
157 void
158 create_folder(char *folder, int autocreate, void (*done_callback)(int))
159 {
160         struct stat st;
161         extern int errno;
162         char *cp;
163
164         if (stat(folder, &st) == -1) {
165                 if (errno != ENOENT)
166                         adios(folder, "error on folder");
167                 if (autocreate == 0) {
168                         /* ask before creating folder */
169                         cp = concat("Create folder \"", folder, "\"? ", NULL);
170                         if (!getanswer(cp))
171                                 done_callback(1);
172                         free(cp);
173                 } else if (autocreate == -1) {
174                         /* do not create, so exit */
175                         done_callback(1);
176                 }
177                 if (!makedir(folder))
178                         adios(NULL, "unable to create folder %s", folder);
179         }
180 }
181
182 /*
183 ** num_digits
184 **     Return the number of digits in a nonnegative integer.
185 */
186 int
187 num_digits(int n)
188 {
189         int ndigits = 0;
190
191         /* Sanity check */
192         if (n < 0)
193                 adios(NULL, "oops, num_digits called with negative value");
194
195         if (n == 0)
196                 return 1;
197
198         while (n) {
199                 n /= 10;
200                 ndigits++;
201         }
202
203         return ndigits;
204 }
205
206 /*
207 ** Append a message arg to an array of them, resizing it if necessary.
208 ** The function is written to suit the arg parsing code it was extracted
209 ** from, and will probably be changed when the other code is cleaned up.
210 */
211 void
212 app_msgarg(struct msgs_array *msgs, char *cp)
213 {
214         if(msgs->size >= msgs->max)
215                 msgs->msgs = mh_xrealloc(msgs->msgs,
216                                 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
217         msgs->msgs[msgs->size++] = cp;
218 }