* docs/MAIL.FILTERING: added note on removing procmail -f or
[mmh] / sbr / utils.c
1
2 /*
3  * utils.c -- various utility routines
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2006, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/utils.h>
14 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <errno.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(NULL, "Tried to malloc 0 bytes");
34
35     memory = malloc(size);
36     if (!memory)
37         adios(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_xmalloc(size);
53
54     if (size == 0)
55         adios(NULL, "Tried to realloc 0bytes");
56
57     memory = realloc(ptr, size);
58     if (!memory)
59         adios(NULL, "Realloc failed");
60
61     return memory;
62 }
63
64 /*
65  * Return the present working directory, if the current directory does not
66  * exist, or is too long, make / the pwd.
67  */
68 char *
69 pwd(void)
70 {
71     register char *cp;
72     static char curwd[PATH_MAX];
73
74     if (!getcwd (curwd, PATH_MAX)) {
75         admonish (NULL, "unable to determine working directory");
76         if (!mypath || !*mypath
77                 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
78             strcpy (curwd, "/");
79             chdir (curwd);
80         }
81         return curwd;
82     }
83
84     if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
85         *cp = '\0';
86
87     return curwd;
88 }
89
90 /*
91  * add   -- If "s1" is NULL, this routine just creates a
92  *       -- copy of "s2" into newly malloc'ed memory.
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 folder_exists(char *folder)
132 {
133     struct stat st;
134     int exists = 0;
135
136     if (stat (folder, &st) == -1) {
137         /* The folder either doesn't exist, or we hit an error.  Either way
138          * return a failure.
139          */
140         exists = 0;
141     } else {
142         /* We can see a folder with the right name */
143         exists = 1;
144     }
145
146     return exists;
147 }
148
149
150 /*
151  * create_folder
152  *      Check to see if a folder exists, if not, prompt the user to create
153  *      it.
154  */
155 void 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 (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 (1);
169             free (cp);
170         } else if (autocreate == -1) {
171             /* do not create, so exit */
172             done_callback (1);
173         }
174         if (!makedir (folder))
175             adios (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 (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, (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs));
213         msgs->msgs[msgs->size++] = cp;
214 }
215
216 /* Open a form or components file */
217 int
218 open_form(char **form, char *def)
219 {
220         int in;
221         if (*form) {
222                 if ((in = open (etcpath (*form), O_RDONLY)) == NOTOK)
223                         adios (*form, "unable to open form file");
224         } else {
225                 if ((in = open (etcpath (def), O_RDONLY)) == NOTOK)
226                         adios (def, "unable to open default components file");
227                 *form = def;
228         }
229         return in;
230 }