Add itoa and utoa to see what Denis Vlasenko thinks.
[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 <sys/wait.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include "busybox.h"
19
20 #ifndef DMALLOC
21 #ifdef L_xmalloc
22 void *xmalloc(size_t size)
23 {
24         void *ptr = malloc(size);
25         if (ptr == NULL && size != 0)
26                 bb_error_msg_and_die(bb_msg_memory_exhausted);
27         return ptr;
28 }
29 #endif
30
31 #ifdef L_xrealloc
32 void *xrealloc(void *ptr, size_t size)
33 {
34         ptr = realloc(ptr, size);
35         if (ptr == NULL && size != 0)
36                 bb_error_msg_and_die(bb_msg_memory_exhausted);
37         return ptr;
38 }
39 #endif
40
41 #ifdef L_xzalloc
42 void *xzalloc(size_t size)
43 {
44         void *ptr = xmalloc(size);
45         memset(ptr, 0, size);
46         return ptr;
47 }
48 #endif
49
50 #ifdef L_xcalloc
51 void *xcalloc(size_t nmemb, size_t size)
52 {
53         void *ptr = calloc(nmemb, size);
54         if (ptr == NULL && nmemb != 0 && size != 0)
55                 bb_error_msg_and_die(bb_msg_memory_exhausted);
56         return ptr;
57 }
58 #endif
59 #endif /* DMALLOC */
60
61 #ifdef L_xstrdup
62 char * bb_xstrdup (const char *s)
63 {
64         char *t;
65
66         if (s == NULL)
67                 return NULL;
68
69         t = strdup (s);
70
71         if (t == NULL)
72                 bb_error_msg_and_die(bb_msg_memory_exhausted);
73
74         return t;
75 }
76 #endif
77
78 #ifdef L_xstrndup
79 char * bb_xstrndup (const char *s, int n)
80 {
81         char *t;
82
83         if (ENABLE_DEBUG && s == NULL)
84                 bb_error_msg_and_die("bb_xstrndup bug");
85
86         t = xmalloc(++n);
87
88         return safe_strncpy(t,s,n);
89 }
90 #endif
91
92 #ifdef L_xfopen
93 FILE *bb_xfopen(const char *path, const char *mode)
94 {
95         FILE *fp;
96         if ((fp = fopen(path, mode)) == NULL)
97                 bb_perror_msg_and_die("%s", path);
98         return fp;
99 }
100 #endif
101
102 #ifdef L_xopen
103 int bb_xopen(const char *pathname, int flags)
104 {
105         return bb_xopen3(pathname, flags, 0777);
106 }
107 #endif
108
109 #ifdef L_xopen3
110 int bb_xopen3(const char *pathname, int flags, int mode)
111 {
112         int ret;
113
114         ret = open(pathname, flags, mode);
115         if (ret < 0) {
116                 bb_perror_msg_and_die("%s", pathname);
117         }
118         return ret;
119 }
120 #endif
121
122 #ifdef L_xread
123 ssize_t bb_xread(int fd, void *buf, size_t count)
124 {
125         ssize_t size;
126
127         size = read(fd, buf, count);
128         if (size < 0) {
129                 bb_perror_msg_and_die(bb_msg_read_error);
130         }
131         return(size);
132 }
133 #endif
134
135 #ifdef L_xread_all
136 void bb_xread_all(int fd, void *buf, size_t count)
137 {
138         ssize_t size;
139
140         while (count) {
141                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
142                         bb_error_msg_and_die("Short read");
143                 }
144                 count -= size;
145                 buf = ((char *) buf) + size;
146         }
147         return;
148 }
149 #endif
150
151 #ifdef L_xread_char
152 unsigned char bb_xread_char(int fd)
153 {
154         char tmp;
155
156         bb_xread_all(fd, &tmp, 1);
157
158         return(tmp);
159 }
160 #endif
161
162 #ifdef L_xferror
163 void bb_xferror(FILE *fp, const char *fn)
164 {
165         if (ferror(fp)) {
166                 bb_error_msg_and_die("%s", fn);
167         }
168 }
169 #endif
170
171 #ifdef L_xferror_stdout
172 void bb_xferror_stdout(void)
173 {
174         bb_xferror(stdout, bb_msg_standard_output);
175 }
176 #endif
177
178 #ifdef L_xfflush_stdout
179 void bb_xfflush_stdout(void)
180 {
181         if (fflush(stdout)) {
182                 bb_perror_msg_and_die(bb_msg_standard_output);
183         }
184 }
185 #endif
186
187 #ifdef L_spawn
188 // This does a fork/exec in one call, using vfork().
189 pid_t bb_spawn(char **argv)
190 {
191         static int failed;
192         pid_t pid;
193         void *app = find_applet_by_name(argv[0]);
194
195         // Be nice to nommu machines.
196         failed = 0;
197         pid = vfork();
198         if (pid < 0) return pid;
199         if (!pid) {
200                 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
201
202                 // We're sharing a stack with blocked parent, let parent know we failed
203                 // and then exit to unblock parent (but don't run atexit() stuff, which
204                 // would screw up parent.)
205
206                 failed = -1;
207                 _exit(0);
208         }
209         return failed ? failed : pid;
210 }
211 #endif
212
213 #ifdef L_xspawn
214 pid_t bb_xspawn(char **argv)
215 {
216         pid_t pid = bb_spawn(argv);
217         if (pid < 0) bb_perror_msg_and_die("%s", *argv);
218         return pid;
219 }
220 #endif
221
222 #ifdef L_wait4
223 int wait4pid(int pid)
224 {
225         int status;
226
227         if (pid == -1 || waitpid(pid, &status, 0) == -1) return -1;
228         if (WIFEXITED(status)) return WEXITSTATUS(status);
229         if (WIFSIGNALED(status)) return WTERMSIG(status);
230         return 0;
231 }
232 #endif
233
234 #ifdef L_itoa
235 // Largest 32 bit integer is -2 billion plus null terminator.
236 // Int should always be 32 bits on a Unix-oid system, see
237 // http://www.unix.org/whitepapers/64bit.html
238 static char local_buf[12];
239
240 void utoa_to_buf(unsigned n, char *buf, int buflen)
241 {
242         int i, out = 0;
243         for (i=1000000000; i; i/=10) {
244                 int res = n/i;
245
246                 if (res || out || i == 1) {
247                         out++;
248                         n -= res*i;
249                         *buf++ = '0' + res;
250                 }
251         }
252         *buf = 0;
253 }
254
255 // Note: uses static buffer, calling it twice in a row will overwrite.
256
257 char *utoa(unsigned n)
258 {
259         utoa_to_buf(n, local_buf, sizeof(local_buf));
260
261         return local_buf;
262 }
263
264 void itoa_to_buf(int n, char *buf, int buflen)
265 {
266         if (n<0) {
267                 n = -n;
268                 *buf++ = '-';
269         }
270         utoa_to_buf((unsigned)n, buf, buflen);
271 }
272
273 // Note: uses static buffer, calling it twice in a row will overwrite.
274
275 char *itoa(int n)
276 {
277         itoa_to_buf(n, local_buf, sizeof(local_buf));
278
279         return local_buf;
280 }
281 #endif