a46a3102703c59f2b6e63e4eff49d9bc993309d9
[become] / src / utils.c
1 /* -*-c-*-
2 *
3 * $Id: utils.c,v 1.1 1997/07/21 13:47:42 mdw Exp $
4 *
5 * Miscellaneous useful bits of code.
6 *
7 * (c) 1997 Mark Wooding
8 */
9
10 /*----- Licencing notice --------------------------------------------------*
11 *
12 * This file is part of `become'
13 *
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: utils.c,v $
32 * Revision 1.1 1997/07/21 13:47:42 mdw
33 * Initial revision
34 *
35 */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 /* --- ANSI headers --- */
40
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 /* --- Local headers --- */
46
47 #include "config.h"
48 #include "utils.h"
49
50 /*----- Static data -------------------------------------------------------*/
51
52 static const char *myname = 0; /* What's my name? */
53
54 /*----- Program name handling ---------------------------------------------*/
55
56 /* --- @quis@ --- *
57 *
58 * Arguments: ---
59 *
60 * Returns: Pointer to the program name.
61 *
62 * Use: Returns the program name.
63 */
64
65 const char *quis(void)
66 {
67 return (myname);
68 }
69
70 /* --- @ego@ --- *
71 *
72 * Arguments: @const char *p@ = pointer to program name
73 *
74 * Returns: ---
75 *
76 * Use: Tells the utils library what the program's name is.
77 */
78
79 #ifndef PATHSEP
80 # if defined(__riscos)
81 # define PATHSEP '.'
82 # elif defined(__unix) || defined(unix)
83 # define PATHSEP '/'
84 # else
85 # define PATHSEP '\\'
86 # endif
87 #endif
88
89 void ego(const char *p)
90 {
91 const char *q = p;
92 while (*q) {
93 if (*q++ == PATHSEP)
94 p = q;
95 }
96 myname = p;
97 }
98
99 /*----- Error reporting ---------------------------------------------------*/
100
101 /* --- @moan@ --- *
102 *
103 * Arguments: @const char *f@ = a @printf@-style format string
104 * @...@ = other arguments
105 *
106 * Returns: ---
107 *
108 * Use: Reports an error.
109 */
110
111 void moan(const char *f, ...)
112 {
113 va_list ap;
114 va_start(ap, f);
115 fprintf(stderr, "%s: ", myname);
116 vfprintf(stderr, f, ap);
117 va_end(ap);
118 putc('\n', stderr);
119 }
120
121 /* --- @die@ --- *
122 *
123 * Arguments: @const char *f@ = a @printf@-style format string
124 * @...@ = other arguments
125 *
126 * Returns: Never.
127 *
128 * Use: Reports an error and hari-kiris. Like @moan@ above, only
129 * more permanent.
130 */
131
132 void die(const char *f, ...)
133 {
134 va_list ap;
135 va_start(ap, f);
136 fprintf(stderr, "%s: ", myname);
137 vfprintf(stderr, f, ap);
138 va_end(ap);
139 putc('\n', stderr);
140 exit(EXIT_FAILURE);
141 }
142
143 /*----- Memory management functions ---------------------------------------*/
144
145 /* --- @xmalloc@ --- *
146 *
147 * Arguments: @size_t sz@ = size of block to allocate
148 *
149 * Returns: Pointer to allocated block.
150 *
151 * Use: Allocates memory. If the memory isn't available, we don't
152 * hang aroung long enough for a normal function return.
153 */
154
155 void *xmalloc(size_t sz)
156 {
157 void *p = malloc(sz);
158 if (!p)
159 die("not enough memory");
160 return (p);
161 }
162
163 /* --- @xstrdup@ --- *
164 *
165 * Arguments: @const char *s@ = pointer to a string
166 *
167 * Returns: Pointer to a copy of the string.
168 *
169 * Use: Copies a string (like @strdup@ would, if it existed).
170 */
171
172 char *xstrdup(const char *s)
173 {
174 size_t sz = strlen(s) + 1;
175 char *p = xmalloc(sz);
176 memcpy(p, s, sz);
177 return (p);
178 }
179
180 /* --- @xrealloc@ --- *
181 *
182 * Arguments: @void *p@ = pointer to a block of memory
183 * @size_t sz@ = new size desired for the block
184 *
185 * Returns: Pointer to the resized memory block (which is almost
186 * certainly not in the same place any more).
187 *
188 * Use: Resizes a memory block.
189 */
190
191 void *xrealloc(void *p, size_t sz)
192 {
193 p = realloc(p, sz);
194 if (!p)
195 die("not enough memory");
196 return (p);
197 }
198
199 /*----- That's all, folks -------------------------------------------------*/