]>
diplodocus.org Git - nmh/blob - sbr/utils.c
3 * utils.c -- various utility routines
5 * This code is Copyright (c) 2006, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
15 * We allocate space for messages (msgs array)
16 * this number of elements at a time.
24 mh_xmalloc(size_t size
)
29 adios(NULL
, "Tried to malloc 0 bytes");
31 memory
= malloc(size
);
33 adios(NULL
, "Malloc failed");
42 mh_xrealloc(void *ptr
, size_t size
)
46 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
48 return mh_xmalloc(size
);
51 adios(NULL
, "Tried to realloc 0bytes");
53 memory
= realloc(ptr
, size
);
55 adios(NULL
, "Realloc failed");
61 * Return the present working directory, if the current directory does not
62 * exist, or is too long, make / the pwd.
68 static char curwd
[PATH_MAX
];
70 if (!getcwd (curwd
, PATH_MAX
)) {
71 admonish (NULL
, "unable to determine working directory");
72 if (!mypath
|| !*mypath
73 || (strcpy (curwd
, mypath
), chdir (curwd
)) == -1) {
80 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
87 * add -- If "s1" is NULL, this routine just creates a
88 * -- copy of "s2" into newly malloc'ed memory.
90 * -- If "s1" is not NULL, then copy the concatenation
91 * -- of "s1" and "s2" (note the order) into newly
92 * -- malloc'ed memory. Then free "s1".
95 add (const char *s2
, char *s1
)
98 size_t len1
= 0, len2
= 0;
105 cp
= mh_xmalloc (len1
+ len2
+ 1);
107 /* Copy s1 and free it */
109 memcpy (cp
, s1
, len1
);
115 memcpy (cp
+ len1
, s2
, len2
);
117 /* Now NULL terminate the string */
118 cp
[len1
+ len2
] = '\0';
125 * Append an item to a comma separated list
128 addlist (char *list
, const char *item
)
131 list
= add (", ", list
);
133 return add (item
, list
);
138 * Check to see if a folder exists.
140 int folder_exists(const char *folder
)
145 if (stat (folder
, &st
) == -1) {
146 /* The folder either doesn't exist, or we hit an error. Either way
151 /* We can see a folder with the right name */
161 * Check to see if a folder exists, if not, prompt the user to create
164 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
170 if (stat (folder
, &st
) == -1) {
172 adios (folder
, "error on folder");
173 if (autocreate
== 0) {
174 /* ask before creating folder */
175 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
179 } else if (autocreate
== -1) {
180 /* do not create, so exit */
183 if (!makedir (folder
))
184 adios (NULL
, "unable to create folder %s", folder
);
190 * Return the number of digits in a nonnegative integer.
199 adios (NULL
, "oops, num_digits called with negative value");
213 * Append a message arg to an array of them, resizing it if necessary.
214 * Really a simple vector-of-(char *) maintenance routine.
217 app_msgarg(struct msgs_array
*msgs
, char *cp
)
219 if(msgs
->size
>= msgs
->max
) {
220 msgs
->max
+= MAXMSGS
;
221 msgs
->msgs
= mh_xrealloc(msgs
->msgs
,
222 msgs
->max
* sizeof(*msgs
->msgs
));
224 msgs
->msgs
[msgs
->size
++] = cp
;
228 * Append a message number to an array of them, resizing it if necessary.
229 * Like app_msgarg, but with a vector-of-ints instead.
233 app_msgnum(struct msgnum_array
*msgs
, int msgnum
)
235 if (msgs
->size
>= msgs
->max
) {
236 msgs
->max
+= MAXMSGS
;
237 msgs
->msgnums
= mh_xrealloc(msgs
->msgnums
,
238 msgs
->max
* sizeof(*msgs
->msgnums
));
240 msgs
->msgnums
[msgs
->size
++] = msgnum
;
243 /* Open a form or components file */
245 open_form(char **form
, char *def
)
249 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
250 adios (*form
, "unable to open form file");
252 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
253 adios (def
, "unable to open default components file");
261 * Finds first occurrence of str in buf. buf is not a C string but a
262 * byte array of length buflen. str is a null-terminated C string.
263 * find_str() does not modify buf but passes back a non-const char *
264 * pointer so that the caller can modify it.
267 find_str (const char buf
[], size_t buflen
, const char *str
) {
268 const size_t len
= strlen (str
);
271 for (i
= 0; i
+ len
<= buflen
; ++i
, ++buf
) {
272 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
280 * Finds last occurrence of str in buf. buf is not a C string but a
281 * byte array of length buflen. str is a null-terminated C string.
282 * find_str() does not modify buf but passes back a non-const char *
283 * pointer so that the caller can modify it.
286 rfind_str (const char buf
[], size_t buflen
, const char *str
) {
287 const size_t len
= strlen (str
);
290 for (i
= 0, buf
+= buflen
- len
; i
+ len
<= buflen
; ++i
, --buf
) {
291 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
298 /* POSIX doesn't have strcasestr() so emulate it. */
300 nmh_strcasestr (const char *s1
, const char *s2
) {
301 const size_t len
= strlen (s2
);
303 if (isupper ((unsigned char) s2
[0]) || islower ((unsigned char)s2
[0])) {
305 first
[0] = (char) toupper ((unsigned char) s2
[0]);
306 first
[1] = (char) tolower ((unsigned char) s2
[0]);
309 for (s1
= strpbrk (s1
, first
); s1
; s1
= strpbrk (++s1
, first
)) {
310 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
313 for (s1
= strchr (s1
, s2
[0]); s1
; s1
= strchr (++s1
, s2
[0])) {
314 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;