1 /*
2 * utils.c
3 *
4 * Oliver Fromme <olli@fromme.com>
5 *
6 * Copyright (C) 1997,1998,1999
7 * Oliver Fromme. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY OLIVER FROMME AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL OLIVER FROMME OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)$Id: utils.c,v 1.3 1999/01/01 23:32:05 olli Exp $
34 */
35
36 static const char cvsid[]
37 = "@(#)$Id: utils.c,v 1.3 1999/01/01 23:32:05 olli Exp $";
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42
43 #include "utils.h"
44
45 char *me;
46
out_of_memory(void)47 void out_of_memory (void)
48 {
49 fprintf (stderr, "%s: Out of memory.\n", me);
50 exit (1);
51 }
52
tmalloc(size_t size)53 void *tmalloc (size_t size)
54 {
55 void *mem;
56
57 if (!(mem = malloc(size)))
58 out_of_memory();
59 return (mem);
60 }
61
62 #if defined(__FreeBSD__)
63 #include <osreldate.h>
64 #if __FreeBSD_version <= 800057 && __FreeBSD_version > 800000 || __FreeBSD_version <= 701100
strndup(char * src,int num)65 char *strndup (char *src, int num)
66 {
67 char *dst;
68
69 if (!(dst = (char *) malloc(num+1)))
70 return (NULL);
71 dst[num] = '\0';
72 return (strncpy(dst, src, num));
73 }
74 #endif
75 #endif
76
justify(char * str)77 char *justify (char *str)
78 {
79 int si = 0, di = 0, havespc = TRUE;
80 char c;
81
82 if (!str)
83 return (str);
84 while ((c = str[si])) {
85 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
86 if (havespc) {
87 si++;
88 continue;
89 }
90 else
91 havespc = TRUE;
92 else
93 havespc = FALSE;
94 str[di++] = str[si++];
95 }
96 if (di && havespc)
97 di --;
98 str[di] = '\0';
99 return (str);
100 }
101
102 char *me;
103
die(const char * func)104 void die (const char *func)
105 {
106 fprintf (stderr, "%s: %s: %s\n", me, func, strerror(errno));
107 exit (1);
108 }
109
utils_init(char * argv0)110 void utils_init (char *argv0)
111 {
112 (me = strrchr(argv0, '/')) ? me++ : (me = argv0);
113 }
114
115 /* EOF */
116