]> diplodocus.org Git - nmh/blob - sbr/utils.c
c25e02d40bd293a52f683b33eae4f90cca43dfc1
[nmh] / 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
16 void *
17 mh_xmalloc(size_t size)
18 {
19 void *memory;
20
21 if (size == 0)
22 adios(NULL, "Tried to malloc 0 bytes");
23
24 memory = malloc(size);
25 if (!memory)
26 adios(NULL, "Malloc failed");
27
28 return memory;
29 }
30
31 void *
32 mh_xrealloc(void *ptr, size_t size)
33 {
34 void *memory;
35
36 if (size == 0)
37 adios(NULL, "Tried to realloc 0bytes");
38
39 memory = realloc(ptr, size);
40 if (!memory)
41 adios(NULL, "Realloc failed");
42
43 return memory;
44 }
45 char *
46 pwd(void)
47 {
48 register char *cp;
49 static char curwd[PATH_MAX];
50
51 if (!getcwd (curwd, PATH_MAX)) {
52 admonish (NULL, "unable to determine working directory");
53 if (!mypath || !*mypath
54 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
55 strcpy (curwd, "/");
56 chdir (curwd);
57 }
58 return curwd;
59 }
60
61 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
62 *cp = '\0';
63
64 return curwd;
65 }