mhl and mhbuild ignore to long lines
[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 ** Free a pointer and set it to NULL.
69 */
70 void
71 mh_free0(void * ptr)
72 {
73         void ** p;
74         p = ptr;
75         free(*p);
76         *p = NULL;
77 }
78
79 /*
80 ** Return the present working directory, if the current directory does not
81 ** exist, or is too long, make / the pwd.
82 */
83 char *
84 pwd(void)
85 {
86         char *cp;
87         static char curwd[PATH_MAX];
88
89         if (!getcwd(curwd, PATH_MAX)) {
90                 admonish(NULL, "unable to determine working directory");
91                 if (!mypath || !*mypath ||
92                                 (strcpy(curwd, mypath), chdir(curwd)) == -1) {
93                         strcpy(curwd, "/");
94                         chdir(curwd);
95                 }
96                 return curwd;
97         }
98
99         if ((cp = curwd + strlen(curwd) - 1) > curwd && *cp == '/') {
100                 /* strip trailing slash */
101                 *cp = '\0';
102         }
103
104         return curwd;
105 }
106
107 /*
108 ** add   -- If "s1" is NULL, this routine just creates a
109 **       -- copy of "s2" into newly malloc'ed memory.
110 **       -- (use mh_xstrdup() instead in this case)
111 **       --
112 **       -- If "s1" is not NULL, then copy the concatenation
113 **       -- of "s1" and "s2" (note the order) into newly
114 **       -- malloc'ed memory.  Then free "s1".
115 */
116 char *
117 add(const char *s2, char *s1)
118 {
119         char *cp;
120         size_t len1 = 0, len2 = 0;
121
122         if (s1) {
123                 len1 = strlen(s1);
124         }
125         if (s2) {
126                 len2 = strlen(s2);
127         }
128
129         cp = mh_xcalloc(len1 + len2 + 1, sizeof(char));
130
131         /* Copy s1 and free it */
132         if (s1) {
133                 memcpy(cp, s1, len1);
134                 mh_free0(&s1);
135         }
136
137         /* Copy s2 */
138         if (s2) {
139                 memcpy(cp + len1, s2, len2);
140         }
141
142         /* Now NULL terminate the string */
143         cp[len1 + len2] = '\0';
144
145         return cp;
146 }
147
148
149 /*
150 ** create_folder
151 **     Check to see if a folder exists, if not, prompt the user to create it.
152 */
153 void
154 create_folder(char *folder, int autocreate, void (*done_callback)(int))
155 {
156         struct stat st;
157         extern int errno;
158         char *cp;
159
160         if (stat(folder, &st) == -1) {
161                 if (errno != ENOENT) {
162                         adios(EX_IOERR, folder, "error on folder");
163                 }
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                         }
170                         mh_free0(&cp);
171                 } else if (autocreate == -1) {
172                         /* do not create, so exit */
173                         done_callback(EX_CANTCREAT);
174                 }
175                 if (!makedir(folder)) {
176                         adios(EX_CANTCREAT, NULL, "unable to create folder %s", folder);
177                 }
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(EX_SOFTWARE, NULL, "oops, num_digits called with negative value");
193         }
194
195         if (n == 0) {
196                 return 1;
197         }
198
199         while (n) {
200                 n /= 10;
201                 ndigits++;
202         }
203
204         return ndigits;
205 }
206
207 /*
208 ** Append a message arg to an array of them, resizing it if necessary.
209 ** The function is written to suit the arg parsing code it was extracted
210 ** from, and will probably be changed when the other code is cleaned up.
211 */
212 void
213 app_msgarg(struct msgs_array *msgs, char *cp)
214 {
215         if(msgs->size >= msgs->max) {
216                 msgs->msgs = mh_xrealloc(msgs->msgs,
217                                 (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
218         }
219         msgs->msgs[msgs->size++] = cp;
220 }
221
222 /*
223 ** mh_xstrdup() is a wrapper of strdup() to replace getcpy().  It returns
224 ** a copy of its argument if this is nonnull; otherwise, it returns a
225 ** string of length 0.
226 */
227 char *
228 mh_xstrdup(const char * s)
229 {
230         char * tmp;
231         tmp = strdup(s ? s : "");
232         if (!tmp) {
233                 adios(EX_OSERR, "strdup", "can't copy string");
234         }
235         return tmp;
236 }