Fixup compile problem with dmalloc
[oweals/busybox.git] / libbb / xfuncs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include "libbb.h"
30
31
32 #ifndef DMALLOC
33 #ifdef L_xmalloc
34 extern void *xmalloc(size_t size)
35 {
36         void *ptr = malloc(size);
37         if (ptr == NULL && size != 0)
38                 bb_error_msg_and_die(bb_msg_memory_exhausted);
39         return ptr;
40 }
41 #endif
42
43 #ifdef L_xrealloc
44 extern void *xrealloc(void *ptr, size_t size)
45 {
46         ptr = realloc(ptr, size);
47         if (ptr == NULL && size != 0)
48                 bb_error_msg_and_die(bb_msg_memory_exhausted);
49         return ptr;
50 }
51 #endif
52
53 #ifdef L_xcalloc
54 extern void *xcalloc(size_t nmemb, size_t size)
55 {
56         void *ptr = calloc(nmemb, size);
57         if (ptr == NULL && nmemb != 0 && size != 0)
58                 bb_error_msg_and_die(bb_msg_memory_exhausted);
59         return ptr;
60 }
61 #endif
62 #endif /* DMALLOC */
63
64 #ifdef L_xstrdup
65 extern char * bb_xstrdup (const char *s) {
66         char *t;
67
68         if (s == NULL)
69                 return NULL;
70
71         t = strdup (s);
72
73         if (t == NULL)
74                 bb_error_msg_and_die(bb_msg_memory_exhausted);
75
76         return t;
77 }
78 #endif
79
80 #ifdef L_xstrndup
81 extern char * bb_xstrndup (const char *s, int n) {
82         char *t;
83
84         if (s == NULL)
85                 bb_error_msg_and_die("bb_xstrndup bug");
86
87         t = xmalloc(++n);
88         
89         return safe_strncpy(t,s,n);
90 }
91 #endif
92
93 #ifdef L_xfopen
94 FILE *bb_xfopen(const char *path, const char *mode)
95 {
96         FILE *fp;
97         if ((fp = fopen(path, mode)) == NULL)
98                 bb_perror_msg_and_die("%s", path);
99         return fp;
100 }
101 #endif
102
103 #ifdef L_xopen
104 extern int bb_xopen(const char *pathname, int flags)
105 {
106         int ret;
107         
108         ret = open(pathname, flags, 0777);
109         if (ret == -1) {
110                 bb_perror_msg_and_die("%s", pathname);
111         }
112         return ret;
113 }
114 #endif
115
116 #ifdef L_xread
117 extern ssize_t bb_xread(int fd, void *buf, size_t count)
118 {
119         ssize_t size;
120
121         size = read(fd, buf, count);
122         if (size == -1) {
123                 bb_perror_msg_and_die("Read error");
124         }
125         return(size);
126 }
127 #endif
128
129 #ifdef L_xread_all
130 extern void bb_xread_all(int fd, void *buf, size_t count)
131 {
132         ssize_t size;
133
134         while (count) {
135                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
136                         bb_error_msg_and_die("Short read");
137                 }
138                 count -= size;
139         }
140         return;
141 }
142 #endif
143
144 #ifdef L_xread_char
145 extern unsigned char bb_xread_char(int fd)
146 {
147         char tmp;
148         
149         bb_xread_all(fd, &tmp, 1);
150
151         return(tmp);    
152 }
153 #endif
154
155 #ifdef L_xferror
156 extern void bb_xferror(FILE *fp, const char *fn)
157 {
158         if (ferror(fp)) {
159                 bb_error_msg_and_die("%s", fn);
160         }
161 }
162 #endif
163
164 #ifdef L_xferror_stdout
165 extern void bb_xferror_stdout(void)
166 {
167         bb_xferror(stdout, bb_msg_standard_output);
168 }
169 #endif
170
171 #ifdef L_xfflush_stdout
172 extern void bb_xfflush_stdout(void)
173 {
174         if (fflush(stdout)) {
175                 bb_perror_msg_and_die(bb_msg_standard_output);
176         }
177 }
178 #endif
179
180 #ifdef L_strlen
181 /* Stupid gcc always includes its own builtin strlen()... */
182 #undef strlen
183 size_t bb_strlen(const char *string)
184 {
185             return(strlen(string));
186 }
187 #endif
188
189 /* END CODE */
190 /*
191 Local Variables:
192 c-file-style: "linux"
193 c-basic-offset: 4
194 tab-width: 4
195 End:
196 */