* sbr/m_draft.c, sbr/utils.c, uip/folder.c, uip/inc.c, uip/mhstoresbr.c,
[mmh] / 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  * Safely call malloc
19  */
20 void *
21 mh_xmalloc(size_t size)
22 {
23     void *memory;
24
25     if (size == 0)
26         adios(NULL, "Tried to malloc 0 bytes");
27
28     memory = malloc(size);
29     if (!memory)
30         adios(NULL, "Malloc failed");
31
32     return memory;
33 }
34
35 /*
36  * Safely call realloc
37  */
38 void *
39 mh_xrealloc(void *ptr, size_t size)
40 {
41     void *memory;
42
43     if (size == 0)
44         adios(NULL, "Tried to realloc 0bytes");
45
46     memory = realloc(ptr, size);
47     if (!memory)
48         adios(NULL, "Realloc failed");
49
50     return memory;
51 }
52
53 /*
54  * Return the present working directory, if the current directory does not
55  * exist, or is too long, make / the pwd.
56  */
57 char *
58 pwd(void)
59 {
60     register char *cp;
61     static char curwd[PATH_MAX];
62
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) {
67             strcpy (curwd, "/");
68             chdir (curwd);
69         }
70         return curwd;
71     }
72
73     if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
74         *cp = '\0';
75
76     return curwd;
77 }
78
79 /*
80  * add   -- If "s1" is NULL, this routine just creates a
81  *       -- copy of "s2" into newly malloc'ed memory.
82  *       --
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".
86  */
87 char *
88 add (char *s2, char *s1)
89 {
90     char *cp;
91     size_t len1 = 0, len2 = 0;
92
93     if (s1)
94         len1 = strlen (s1);
95     if (s2)
96         len2 = strlen (s2);
97
98     cp = mh_xmalloc (len1 + len2 + 1);
99
100     /* Copy s1 and free it */
101     if (s1) {
102         memcpy (cp, s1, len1);
103         free (s1);
104     }
105
106     /* Copy s2 */
107     if (s2)
108         memcpy (cp + len1, s2, len2);
109
110     /* Now NULL terminate the string */
111     cp[len1 + len2] = '\0';
112
113     return cp;
114 }
115
116 /*
117  * create_folder
118  *      Check to see if a folder exists, if not, prompt the user to create
119  *      it.
120  */
121 void create_folder(char *folder, int autocreate, void (*done_callback)())
122 {
123     struct stat st;
124     extern int errno;
125     char *cp;
126
127     if (stat (folder, &st) == -1) {
128         if (errno != ENOENT)
129             adios (folder, "error on folder");
130         if (autocreate == 0) {
131             /* ask before creating folder */
132             cp = concat ("Create folder \"", folder, "\"? ", NULL);
133             if (!getanswer (cp))
134                 done_callback (1);
135             free (cp);
136         } else if (autocreate == -1) {
137             /* do not create, so exit */
138             done_callback (1);
139         }
140         if (!makedir (folder))
141             adios (NULL, "unable to create folder %s", folder);
142     }
143 }