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