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