a46a3102703c59f2b6e63e4eff49d9bc993309d9
3 * $Id: utils.c,v 1.1 1997/07/21 13:47:42 mdw Exp $
5 * Miscellaneous useful bits of code.
7 * (c) 1997 Mark Wooding
10 /*----- Licencing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1997/07/21 13:47:42 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
45 /* --- Local headers --- */
50 /*----- Static data -------------------------------------------------------*/
52 static const char *myname
= 0; /* What's my name? */
54 /*----- Program name handling ---------------------------------------------*/
60 * Returns: Pointer to the program name.
62 * Use: Returns the program name.
65 const char *quis(void)
72 * Arguments: @const char *p@ = pointer to program name
76 * Use: Tells the utils library what the program's name is.
80 # if defined(__riscos)
82 # elif defined(__unix) || defined(unix)
89 void ego(const char *p
)
99 /*----- Error reporting ---------------------------------------------------*/
103 * Arguments: @const char *f@ = a @printf@-style format string
104 * @...@ = other arguments
108 * Use: Reports an error.
111 void moan(const char *f
, ...)
115 fprintf(stderr
, "%s: ", myname
);
116 vfprintf(stderr
, f
, ap
);
123 * Arguments: @const char *f@ = a @printf@-style format string
124 * @...@ = other arguments
128 * Use: Reports an error and hari-kiris. Like @moan@ above, only
132 void die(const char *f
, ...)
136 fprintf(stderr
, "%s: ", myname
);
137 vfprintf(stderr
, f
, ap
);
143 /*----- Memory management functions ---------------------------------------*/
145 /* --- @xmalloc@ --- *
147 * Arguments: @size_t sz@ = size of block to allocate
149 * Returns: Pointer to allocated block.
151 * Use: Allocates memory. If the memory isn't available, we don't
152 * hang aroung long enough for a normal function return.
155 void *xmalloc(size_t sz
)
157 void *p
= malloc(sz
);
159 die("not enough memory");
163 /* --- @xstrdup@ --- *
165 * Arguments: @const char *s@ = pointer to a string
167 * Returns: Pointer to a copy of the string.
169 * Use: Copies a string (like @strdup@ would, if it existed).
172 char *xstrdup(const char *s
)
174 size_t sz
= strlen(s
) + 1;
175 char *p
= xmalloc(sz
);
180 /* --- @xrealloc@ --- *
182 * Arguments: @void *p@ = pointer to a block of memory
183 * @size_t sz@ = new size desired for the block
185 * Returns: Pointer to the resized memory block (which is almost
186 * certainly not in the same place any more).
188 * Use: Resizes a memory block.
191 void *xrealloc(void *p
, size_t sz
)
195 die("not enough memory");
199 /*----- That's all, folks -------------------------------------------------*/