]>
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.
16 * We allocate space for messages (msgs array)
17 * this number of elements at a time.
25 mh_xmalloc(size_t size
)
30 adios(NULL
, "Tried to malloc 0 bytes");
32 memory
= malloc(size
);
34 adios(NULL
, "Malloc failed");
43 mh_xrealloc(void *ptr
, size_t size
)
47 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
49 return mh_xmalloc(size
);
52 adios(NULL
, "Tried to realloc 0bytes");
54 memory
= realloc(ptr
, size
);
56 adios(NULL
, "Realloc failed");
62 * Return the present working directory, if the current directory does not
63 * exist, or is too long, make / the pwd.
69 static char curwd
[PATH_MAX
];
71 if (!getcwd (curwd
, PATH_MAX
)) {
72 admonish (NULL
, "unable to determine working directory");
73 if (!mypath
|| !*mypath
74 || (strcpy (curwd
, mypath
), chdir (curwd
)) == -1) {
81 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
88 * add -- If "s1" is NULL, this routine just creates a
89 * -- copy of "s2" into newly malloc'ed memory.
91 * -- If "s1" is not NULL, then copy the concatenation
92 * -- of "s1" and "s2" (note the order) into newly
93 * -- malloc'ed memory. Then free "s1".
96 add (const char *s2
, char *s1
)
99 size_t len1
= 0, len2
= 0;
106 cp
= mh_xmalloc (len1
+ len2
+ 1);
108 /* Copy s1 and free it */
110 memcpy (cp
, s1
, len1
);
116 memcpy (cp
+ len1
, s2
, len2
);
118 /* Now NULL terminate the string */
119 cp
[len1
+ len2
] = '\0';
126 * Append an item to a comma separated list
129 addlist (char *list
, const char *item
)
132 list
= add (", ", list
);
134 return add (item
, list
);
139 * Check to see if a folder exists.
141 int folder_exists(const char *folder
)
146 if (stat (folder
, &st
) == -1) {
147 /* The folder either doesn't exist, or we hit an error. Either way
152 /* We can see a folder with the right name */
162 * Check to see if a folder exists, if not, prompt the user to create
165 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
171 if (stat (folder
, &st
) == -1) {
173 adios (folder
, "error on folder");
174 if (autocreate
== 0) {
175 /* ask before creating folder */
176 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
180 } else if (autocreate
== -1) {
181 /* do not create, so exit */
184 if (!makedir (folder
))
185 adios (NULL
, "unable to create folder %s", folder
);
191 * Return the number of digits in a nonnegative integer.
200 adios (NULL
, "oops, num_digits called with negative value");
214 * Append a message arg to an array of them, resizing it if necessary.
215 * Really a simple vector-of-(char *) maintenance routine.
218 app_msgarg(struct msgs_array
*msgs
, char *cp
)
220 if(msgs
->size
>= msgs
->max
) {
221 msgs
->max
+= MAXMSGS
;
222 msgs
->msgs
= mh_xrealloc(msgs
->msgs
,
223 msgs
->max
* sizeof(*msgs
->msgs
));
225 msgs
->msgs
[msgs
->size
++] = cp
;
229 * Append a message number to an array of them, resizing it if necessary.
230 * Like app_msgarg, but with a vector-of-ints instead.
234 app_msgnum(struct msgnum_array
*msgs
, int msgnum
)
236 if (msgs
->size
>= msgs
->max
) {
237 msgs
->max
+= MAXMSGS
;
238 msgs
->msgnums
= mh_xrealloc(msgs
->msgnums
,
239 msgs
->max
* sizeof(*msgs
->msgnums
));
241 msgs
->msgnums
[msgs
->size
++] = msgnum
;
244 /* Open a form or components file */
246 open_form(char **form
, char *def
)
250 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
251 adios (*form
, "unable to open form file");
253 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
254 adios (def
, "unable to open default components file");
262 * Finds first occurrence of str in buf. buf is not a C string but a
263 * byte array of length buflen. str is a null-terminated C string.
264 * find_str() does not modify buf but passes back a non-const char *
265 * pointer so that the caller can modify it.
268 find_str (const char buf
[], size_t buflen
, const char *str
) {
269 const size_t len
= strlen (str
);
272 for (i
= 0; i
+ len
<= buflen
; ++i
, ++buf
) {
273 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
281 * Finds last occurrence of str in buf. buf is not a C string but a
282 * byte array of length buflen. str is a null-terminated C string.
283 * find_str() does not modify buf but passes back a non-const char *
284 * pointer so that the caller can modify it.
287 rfind_str (const char buf
[], size_t buflen
, const char *str
) {
288 const size_t len
= strlen (str
);
291 for (i
= 0, buf
+= buflen
- len
; i
+ len
<= buflen
; ++i
, --buf
) {
292 if (! memcmp (buf
, str
, len
)) return (char *) buf
;
299 /* POSIX doesn't have strcasestr() so emulate it. */
301 nmh_strcasestr (const char *s1
, const char *s2
) {
302 const size_t len
= strlen (s2
);
304 if (isupper ((unsigned char) s2
[0]) || islower ((unsigned char)s2
[0])) {
306 first
[0] = (char) toupper ((unsigned char) s2
[0]);
307 first
[1] = (char) tolower ((unsigned char) s2
[0]);
310 for (s1
= strpbrk (s1
, first
); s1
; s1
= strpbrk (++s1
, first
)) {
311 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;
314 for (s1
= strchr (s1
, s2
[0]); s1
; s1
= strchr (++s1
, s2
[0])) {
315 if (! strncasecmp (s1
, s2
, len
)) return (char *) s1
;