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