Thinko spotted by Vladimir Dronnikov.
[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  * Copyright (C) 2006 Rob Landley
7  * Copyright (C) 2006 Denis Vlasenko
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "busybox.h"
13
14 #ifndef DMALLOC
15 #ifdef L_xmalloc
16 void *xmalloc(size_t size)
17 {
18         void *ptr = malloc(size);
19         if (ptr == NULL && size != 0)
20                 bb_error_msg_and_die(bb_msg_memory_exhausted);
21         return ptr;
22 }
23 #endif
24
25 #ifdef L_xrealloc
26 void *xrealloc(void *ptr, size_t size)
27 {
28         ptr = realloc(ptr, size);
29         if (ptr == NULL && size != 0)
30                 bb_error_msg_and_die(bb_msg_memory_exhausted);
31         return ptr;
32 }
33 #endif
34
35 #ifdef L_xzalloc
36 void *xzalloc(size_t size)
37 {
38         void *ptr = xmalloc(size);
39         memset(ptr, 0, size);
40         return ptr;
41 }
42 #endif
43
44 #endif /* DMALLOC */
45
46 #ifdef L_xstrdup
47 char * xstrdup (const char *s)
48 {
49         char *t;
50
51         if (s == NULL)
52                 return NULL;
53
54         t = strdup (s);
55
56         if (t == NULL)
57                 bb_error_msg_and_die(bb_msg_memory_exhausted);
58
59         return t;
60 }
61 #endif
62
63 #ifdef L_xstrndup
64 char * xstrndup (const char *s, int n)
65 {
66         char *t;
67
68         if (ENABLE_DEBUG && s == NULL)
69                 bb_error_msg_and_die("xstrndup bug");
70
71         t = xmalloc(++n);
72
73         return safe_strncpy(t,s,n);
74 }
75 #endif
76
77 #ifdef L_xfopen
78 FILE *xfopen(const char *path, const char *mode)
79 {
80         FILE *fp;
81         if ((fp = fopen(path, mode)) == NULL)
82                 bb_perror_msg_and_die("%s", path);
83         return fp;
84 }
85 #endif
86
87 #ifdef L_xopen
88 int xopen(const char *pathname, int flags)
89 {
90         return xopen3(pathname, flags, 0777);
91 }
92 #endif
93
94 #ifdef L_xopen3
95 int xopen3(const char *pathname, int flags, int mode)
96 {
97         int ret;
98
99         ret = open(pathname, flags, mode);
100         if (ret < 0) {
101                 bb_perror_msg_and_die("%s", pathname);
102         }
103         return ret;
104 }
105 #endif
106
107 #ifdef L_xread
108
109 // Die with an error message if we can't read the entire buffer.
110
111 void xread(int fd, void *buf, size_t count)
112 {
113         while (count) {
114                 ssize_t size;
115
116                 if ((size = safe_read(fd, buf, count)) < 1)
117                         bb_error_msg_and_die("Short read");
118                 count -= size;
119                 buf = ((char *) buf) + size;
120         }
121 }
122 #endif
123
124 #ifdef L_xwrite
125
126 // Die with an error message if we can't write the entire buffer.
127
128 void xwrite(int fd, void *buf, size_t count)
129 {
130         while (count) {
131                 ssize_t size;
132
133                 if ((size = safe_write(fd, buf, count)) < 1)
134                         bb_error_msg_and_die("Short write");
135                 count -= size;
136                 buf = ((char *) buf) + size;
137         }
138 }
139 #endif
140
141 #ifdef L_xlseek
142
143 // Die if we can't lseek to the right spot.
144
145 void xlseek(int fd, off_t offset, int whence)
146 {
147         if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
148 }
149 #endif
150
151 #ifdef L_xread_char
152 unsigned char xread_char(int fd)
153 {
154         char tmp;
155
156         xread(fd, &tmp, 1);
157
158         return(tmp);
159 }
160 #endif
161
162 #ifdef L_xferror
163 void 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 xferror_stdout(void)
173 {
174         xferror(stdout, bb_msg_standard_output);
175 }
176 #endif
177
178 #ifdef L_xfflush_stdout
179 void 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 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 xspawn(char **argv)
215 {
216         pid_t pid = 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, unsigned buflen)
241 {
242         int i, out = 0;
243         if (buflen) {
244                 for (i=1000000000; i; i/=10) {
245                         int res = n/i;
246
247                         if ((res || out || i == 1) && --buflen>0) {
248                                 out++;
249                                 n -= res*i;
250                                 *buf++ = '0' + res;
251                         }
252                 }
253                 *buf = 0;
254         }
255 }
256
257 // Note: uses static buffer, calling it twice in a row will overwrite.
258
259 char *utoa(unsigned n)
260 {
261         utoa_to_buf(n, local_buf, sizeof(local_buf));
262
263         return local_buf;
264 }
265
266 void itoa_to_buf(int n, char *buf, unsigned buflen)
267 {
268         if (buflen && n<0) {
269                 n = -n;
270                 *buf++ = '-';
271                 buflen--;
272         }
273         utoa_to_buf((unsigned)n, buf, buflen);
274 }
275
276 // Note: uses static buffer, calling it twice in a row will overwrite.
277
278 char *itoa(int n)
279 {
280         itoa_to_buf(n, local_buf, sizeof(local_buf));
281
282         return local_buf;
283 }
284 #endif
285
286 #ifdef L_setuid
287 void xsetgid(gid_t gid)
288 {
289         if (setgid(gid)) bb_error_msg_and_die("setgid");
290 }
291
292 void xsetuid(uid_t uid)
293 {
294         if (setuid(uid)) bb_error_msg_and_die("setuid");
295 }
296 #endif
297
298 #ifdef L_fdlength
299 off_t fdlength(int fd)
300 {
301         off_t bottom = 0, top = 0, pos;
302         long size;
303
304         // If the ioctl works for this, return it.
305
306         if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
307
308         // If not, do a binary search for the last location we can read.
309
310         do {
311                 char temp;
312
313                 pos = bottom + (top - bottom) / 2;;
314
315                 // If we can read from the current location, it's bigger.
316
317                 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
318                         if (bottom == top) bottom = top = (top+1) * 2;
319                         else bottom = pos;
320
321                 // If we can't, it's smaller.
322
323                 } else {
324                         if (bottom == top) {
325                                 if (!top) return 0;
326                                 bottom = top/2;
327                         }
328                         else top = pos;
329                 }
330         } while (bottom + 1 != top);
331
332         return pos + 1;
333 }
334 #endif
335
336 #ifdef L_xasprintf
337 char *xasprintf(const char *format, ...)
338 {
339         va_list p;
340         int r;
341         char *string_ptr;
342
343 #if 1
344         // GNU extension
345         va_start(p, format);
346         r = vasprintf(&string_ptr, format, p);
347         va_end(p);
348 #else
349         // Bloat for systems that haven't got the GNU extension.
350         va_start(p, format);
351         r = vsnprintf(NULL, 0, format, p);
352         va_end(p);
353         string_ptr = xmalloc(r+1);
354         va_start(p, format);
355         r = vsnprintf(string_ptr, r+1, format, p);
356         va_end(p);
357 #endif
358
359         if (r < 0) bb_perror_msg_and_die("xasprintf");
360         return string_ptr;
361 }
362 #endif
363
364 #ifdef L_xprint_and_close_file
365 void xprint_and_close_file(FILE *file)
366 {
367         // copyfd outputs error messages for us.
368         if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval);
369
370         fclose(file);
371 }
372 #endif
373
374 #ifdef L_xchdir
375 void xchdir(const char *path)
376 {
377         if (chdir(path))
378                 bb_perror_msg_and_die("chdir(%s)", path);
379 }
380 #endif
381
382 #ifdef L_warn_opendir
383 DIR *warn_opendir(const char *path)
384 {
385         DIR *dp;
386
387         if ((dp = opendir(path)) == NULL) {
388                 bb_perror_msg("unable to open `%s'", path);
389                 return NULL;
390         }
391         return dp;
392 }
393 #endif
394
395 #ifdef L_xopendir
396 DIR *xopendir(const char *path)
397 {
398         DIR *dp;
399
400         if ((dp = opendir(path)) == NULL)
401                 bb_perror_msg_and_die("unable to open `%s'", path);
402         return dp;
403 }
404 #endif
405
406 #ifdef L_xdaemon
407 #ifndef BB_NOMMU
408 void xdaemon(int nochdir, int noclose)
409 {
410             if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon");
411 }
412 #endif
413 #endif
414
415 #ifdef L_xbind
416 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
417 {
418         if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
419 }
420 #endif
421
422 #ifdef L_xsocket
423 int xsocket(int domain, int type, int protocol)
424 {
425         int r = socket(domain, type, protocol);
426
427         if (r < 0) bb_perror_msg_and_die("socket");
428
429         return r;
430 }
431 #endif
432
433 #ifdef L_xlisten
434 void xlisten(int s, int backlog)
435 {
436         if (listen(s, backlog)) bb_perror_msg_and_die("listen");
437 }
438 #endif