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