Added comment
[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         if (size == 0)
52                 adios(NULL, "Tried to realloc 0bytes");
53
54         memory = realloc(ptr, size);
55         if (!memory)
56                 adios(NULL, "Realloc failed");
57
58         return memory;
59 }
60
61 /*
62 ** Return the present working directory, if the current directory does not
63 ** exist, or is too long, make / the pwd.
64 */
65 char *
66 pwd(void)
67 {
68         register char *cp;
69         static char curwd[PATH_MAX];
70
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) {
75                         strcpy(curwd, "/");
76                         chdir(curwd);
77                 }
78                 return curwd;
79         }
80
81         if ((cp = curwd + strlen(curwd) - 1) > curwd && *cp == '/') {
82                 /* strip trailing slash */
83                 *cp = '\0';
84         }
85
86         return curwd;
87 }
88
89 /*
90 ** add   -- If "s1" is NULL, this routine just creates a
91 **       -- copy of "s2" into newly malloc'ed memory.
92 **       --
93 **       -- If "s1" is not NULL, then copy the concatenation
94 **       -- of "s1" and "s2" (note the order) into newly
95 **       -- malloc'ed memory.  Then free "s1".
96 */
97 char *
98 add(char *s2, char *s1)
99 {
100         char *cp;
101         size_t len1 = 0, len2 = 0;
102
103         if (s1)
104                 len1 = strlen(s1);
105         if (s2)
106                 len2 = strlen(s2);
107
108         cp = mh_xmalloc(len1 + len2 + 1);
109
110         /* Copy s1 and free it */
111         if (s1) {
112                 memcpy(cp, s1, len1);
113                 free(s1);
114         }
115
116         /* Copy s2 */
117         if (s2)
118                 memcpy(cp + len1, s2, len2);
119
120         /* Now NULL terminate the string */
121         cp[len1 + len2] = '\0';
122
123         return cp;
124 }
125
126 /*
127 ** folder_exists
128 **     Check to see if a folder exists.
129 */
130 int
131 folder_exists(char *folder)
132 {
133         struct stat st;
134         int exists = 0;
135
136         if (stat(folder, &st) == -1) {
137                 /*
138                 ** The folder either doesn't exist, or we hit an error.
139                 ** Either way return a failure.
140                 */
141                 exists = 0;
142         } else {
143                 /* We can see a folder with the right name */
144                 exists = 1;
145         }
146
147         return exists;
148 }
149
150
151 /*
152 ** create_folder
153 **     Check to see if a folder exists, if not, prompt the user to create it.
154 */
155 void
156 create_folder(char *folder, int autocreate, void (*done_callback)(int))
157 {
158         struct stat st;
159         extern int errno;
160         char *cp;
161
162         if (stat(folder, &st) == -1) {
163                 if (errno != ENOENT)
164                         adios(folder, "error on folder");
165                 if (autocreate == 0) {
166                         /* ask before creating folder */
167                         cp = concat("Create folder \"", folder, "\"? ", NULL);
168                         if (!getanswer(cp))
169                                 done_callback(1);
170                         free(cp);
171                 } else if (autocreate == -1) {
172                         /* do not create, so exit */
173                         done_callback(1);
174                 }
175                 if (!makedir(folder))
176                         adios(NULL, "unable to create folder %s", folder);
177         }
178 }
179
180 /*
181 ** num_digits
182 **     Return the number of digits in a nonnegative integer.
183 */
184 int
185 num_digits(int n)
186 {
187         int ndigits = 0;
188
189         /* Sanity check */
190         if (n < 0)
191                 adios(NULL, "oops, num_digits called with negative value");
192
193         if (n == 0)
194                 return 1;
195
196         while (n) {
197                 n /= 10;
198                 ndigits++;
199         }
200
201         return ndigits;
202 }
203
204 /*
205 ** Append a message arg to an array of them, resizing it if necessary.
206 ** The function is written to suit the arg parsing code it was extracted
207 ** from, and will probably be changed when the other code is cleaned up.
208 */
209 void
210 app_msgarg(struct msgs_array *msgs, char *cp)
211 {
212         if(msgs->size >= msgs->max)
213                 msgs->msgs = mh_xrealloc(msgs->msgs,
214                                 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
215         msgs->msgs[msgs->size++] = cp;
216 }
217
218 /* Open a form or components file */
219 int
220 open_form(char **form, char *def)
221 {
222         int in;
223         if (*form) {
224                 if ((in = open(etcpath(*form), O_RDONLY)) == NOTOK)
225                         adios(*form, "unable to open form file");
226         } else {
227                 if ((in = open(etcpath(def), O_RDONLY)) == NOTOK)
228                         adios(def, "unable to open default components file");
229                 *form = def;
230         }
231         return in;
232 }