]>
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.
17 * We allocate space for messages (msgs array)
18 * this number of elements at a time.
26 mh_xmalloc(size_t size
)
31 adios(NULL
, "Tried to malloc 0 bytes");
33 memory
= malloc(size
);
35 adios(NULL
, "Malloc failed");
44 mh_xrealloc(void *ptr
, size_t size
)
48 /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
50 return mh_xmalloc(size
);
53 adios(NULL
, "Tried to realloc 0bytes");
55 memory
= realloc(ptr
, size
);
57 adios(NULL
, "Realloc failed");
63 * Return the present working directory, if the current directory does not
64 * exist, or is too long, make / the pwd.
70 static char curwd
[PATH_MAX
];
72 if (!getcwd (curwd
, PATH_MAX
)) {
73 admonish (NULL
, "unable to determine working directory");
74 if (!mypath
|| !*mypath
75 || (strcpy (curwd
, mypath
), chdir (curwd
)) == -1) {
82 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
89 * add -- If "s1" is NULL, this routine just creates a
90 * -- copy of "s2" into newly malloc'ed memory.
92 * -- If "s1" is not NULL, then copy the concatenation
93 * -- of "s1" and "s2" (note the order) into newly
94 * -- malloc'ed memory. Then free "s1".
97 add (char *s2
, char *s1
)
100 size_t len1
= 0, len2
= 0;
107 cp
= mh_xmalloc (len1
+ len2
+ 1);
109 /* Copy s1 and free it */
111 memcpy (cp
, s1
, len1
);
117 memcpy (cp
+ len1
, s2
, len2
);
119 /* Now NULL terminate the string */
120 cp
[len1
+ len2
] = '\0';
127 * Append an item to a comma separated list
130 addlist (char *list
, char *item
)
133 list
= add (", ", list
);
135 return add (item
, list
);
140 * Check to see if a folder exists.
142 int folder_exists(char *folder
)
147 if (stat (folder
, &st
) == -1) {
148 /* The folder either doesn't exist, or we hit an error. Either way
153 /* We can see a folder with the right name */
163 * Check to see if a folder exists, if not, prompt the user to create
166 void create_folder(char *folder
, int autocreate
, void (*done_callback
)(int))
172 if (stat (folder
, &st
) == -1) {
174 adios (folder
, "error on folder");
175 if (autocreate
== 0) {
176 /* ask before creating folder */
177 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
181 } else if (autocreate
== -1) {
182 /* do not create, so exit */
185 if (!makedir (folder
))
186 adios (NULL
, "unable to create folder %s", folder
);
192 * Return the number of digits in a nonnegative integer.
201 adios (NULL
, "oops, num_digits called with negative value");
215 * Append a message arg to an array of them, resizing it if necessary.
216 * The function is written to suit the arg parsing code it was extracted
217 * from, and will probably be changed when the other code is cleaned up.
220 app_msgarg(struct msgs_array
*msgs
, char *cp
)
222 if(msgs
->size
>= msgs
->max
)
223 msgs
->msgs
= mh_xrealloc(msgs
->msgs
, (msgs
->max
+=MAXMSGS
)*sizeof(*msgs
->msgs
));
224 msgs
->msgs
[msgs
->size
++] = cp
;
227 /* Open a form or components file */
229 open_form(char **form
, char *def
)
233 if ((in
= open (etcpath (*form
), O_RDONLY
)) == NOTOK
)
234 adios (*form
, "unable to open form file");
236 if ((in
= open (etcpath (def
), O_RDONLY
)) == NOTOK
)
237 adios (def
, "unable to open default components file");