3db526b85811203ac4fce960feab063cacdda7a1
[oweals/busybox.git] / libbb / xfuncs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17
18 /* Since gcc always inlines strlen(), this saves a byte or two, but we need
19  * the #undef here to avoid endless loop from #define strlen bb_strlen */
20 #ifdef L_strlen
21 #define BB_STRLEN_IMPLEMENTATION
22 #endif
23
24 #include "libbb.h"
25
26
27 #ifndef DMALLOC
28 #ifdef L_xmalloc
29 void *xmalloc(size_t size)
30 {
31         void *ptr = malloc(size);
32         if (ptr == NULL && size != 0)
33                 bb_error_msg_and_die(bb_msg_memory_exhausted);
34         return ptr;
35 }
36 #endif
37
38 #ifdef L_xrealloc
39 void *xrealloc(void *ptr, size_t size)
40 {
41         ptr = realloc(ptr, size);
42         if (ptr == NULL && size != 0)
43                 bb_error_msg_and_die(bb_msg_memory_exhausted);
44         return ptr;
45 }
46 #endif
47
48 #ifdef L_xcalloc
49 void *xcalloc(size_t nmemb, size_t size)
50 {
51         void *ptr = calloc(nmemb, size);
52         if (ptr == NULL && nmemb != 0 && size != 0)
53                 bb_error_msg_and_die(bb_msg_memory_exhausted);
54         return ptr;
55 }
56 #endif
57 #endif /* DMALLOC */
58
59 #ifdef L_xstrdup
60 char * bb_xstrdup (const char *s)
61 {
62         char *t;
63
64         if (s == NULL)
65                 return NULL;
66
67         t = strdup (s);
68
69         if (t == NULL)
70                 bb_error_msg_and_die(bb_msg_memory_exhausted);
71
72         return t;
73 }
74 #endif
75
76 #ifdef L_xstrndup
77 char * bb_xstrndup (const char *s, int n)
78 {
79         char *t;
80
81         if (s == NULL)
82                 bb_error_msg_and_die("bb_xstrndup bug");
83
84         t = xmalloc(++n);
85
86         return safe_strncpy(t,s,n);
87 }
88 #endif
89
90 #ifdef L_xfopen
91 FILE *bb_xfopen(const char *path, const char *mode)
92 {
93         FILE *fp;
94         if ((fp = fopen(path, mode)) == NULL)
95                 bb_perror_msg_and_die("%s", path);
96         return fp;
97 }
98 #endif
99
100 #ifdef L_xopen
101 int bb_xopen(const char *pathname, int flags)
102 {
103         return bb_xopen3(pathname, flags, 0777);
104 }
105 #endif
106
107 #ifdef L_xopen3
108 int bb_xopen3(const char *pathname, int flags, int mode)
109 {
110         int ret;
111
112         ret = open(pathname, flags, mode);
113         if (ret < 0) {
114                 bb_perror_msg_and_die("%s", pathname);
115         }
116         return ret;
117 }
118 #endif
119
120 #ifdef L_xread
121 ssize_t bb_xread(int fd, void *buf, size_t count)
122 {
123         ssize_t size;
124
125         size = read(fd, buf, count);
126         if (size < 0) {
127                 bb_perror_msg_and_die(bb_msg_read_error);
128         }
129         return(size);
130 }
131 #endif
132
133 #ifdef L_xread_all
134 void bb_xread_all(int fd, void *buf, size_t count)
135 {
136         ssize_t size;
137
138         while (count) {
139                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
140                         bb_error_msg_and_die("Short read");
141                 }
142                 count -= size;
143                 buf = ((char *) buf) + size;
144         }
145         return;
146 }
147 #endif
148
149 #ifdef L_xread_char
150 unsigned char bb_xread_char(int fd)
151 {
152         char tmp;
153
154         bb_xread_all(fd, &tmp, 1);
155
156         return(tmp);
157 }
158 #endif
159
160 #ifdef L_xferror
161 void bb_xferror(FILE *fp, const char *fn)
162 {
163         if (ferror(fp)) {
164                 bb_error_msg_and_die("%s", fn);
165         }
166 }
167 #endif
168
169 #ifdef L_xferror_stdout
170 void bb_xferror_stdout(void)
171 {
172         bb_xferror(stdout, bb_msg_standard_output);
173 }
174 #endif
175
176 #ifdef L_xfflush_stdout
177 void bb_xfflush_stdout(void)
178 {
179         if (fflush(stdout)) {
180                 bb_perror_msg_and_die(bb_msg_standard_output);
181         }
182 }
183 #endif
184
185 #ifdef L_strlen
186 size_t bb_strlen(const char *string)
187 {
188             return(strlen(string));
189 }
190 #endif
191
192 /* END CODE */
193 /*
194 Local Variables:
195 c-file-style: "linux"
196 c-basic-offset: 4
197 tab-width: 4
198 End:
199 */