]>
diplodocus.org Git - nmh/blob - sbr/utils.c
3 * utils.c -- various utility routines
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.
21 mh_xmalloc(size_t size
)
26 adios(NULL
, "Tried to malloc 0 bytes");
28 memory
= malloc(size
);
30 adios(NULL
, "Malloc failed");
39 mh_xrealloc(void *ptr
, size_t size
)
44 adios(NULL
, "Tried to realloc 0bytes");
46 memory
= realloc(ptr
, size
);
48 adios(NULL
, "Realloc failed");
54 * Return the present working directory, if the current directory does not
55 * exist, or is too long, make / the pwd.
61 static char curwd
[PATH_MAX
];
63 if (!getcwd (curwd
, PATH_MAX
)) {
64 admonish (NULL
, "unable to determine working directory");
65 if (!mypath
|| !*mypath
66 || (strcpy (curwd
, mypath
), chdir (curwd
)) == -1) {
73 if ((cp
= curwd
+ strlen (curwd
) - 1) > curwd
&& *cp
== '/')
80 * add -- If "s1" is NULL, this routine just creates a
81 * -- copy of "s2" into newly malloc'ed memory.
83 * -- If "s1" is not NULL, then copy the concatenation
84 * -- of "s1" and "s2" (note the order) into newly
85 * -- malloc'ed memory. Then free "s1".
88 add (char *s2
, char *s1
)
91 size_t len1
= 0, len2
= 0;
98 cp
= mh_xmalloc (len1
+ len2
+ 1);
100 /* Copy s1 and free it */
102 memcpy (cp
, s1
, len1
);
108 memcpy (cp
+ len1
, s2
, len2
);
110 /* Now NULL terminate the string */
111 cp
[len1
+ len2
] = '\0';
118 * Check to see if a folder exists, if not, prompt the user to create
121 void create_folder(char *folder
, int autocreate
, void (*done_callback
)())
127 if (stat (folder
, &st
) == -1) {
129 adios (folder
, "error on folder");
130 if (autocreate
== 0) {
131 /* ask before creating folder */
132 cp
= concat ("Create folder \"", folder
, "\"? ", NULL
);
136 } else if (autocreate
== -1) {
137 /* do not create, so exit */
140 if (!makedir (folder
))
141 adios (NULL
, "unable to create folder %s", folder
);
147 * Return the number of digits in a nonnegative integer.
156 adios (NULL
, "oops, num_digits called with negative value");