Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 4e9deec | 2006-02-20 02:44:30 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Rob Landley | 552b56d | 2006-05-04 21:22:27 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | |
| 19 | #ifndef DMALLOC |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | #ifdef L_xmalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | void *xmalloc(size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | { |
| 23 | void *ptr = malloc(size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 24 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | return ptr; |
| 27 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | #ifdef L_xrealloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 31 | void *xrealloc(void *ptr, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 32 | { |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 33 | ptr = realloc(ptr, size); |
| 34 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | return ptr; |
| 37 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | #ifdef L_xcalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 41 | void *xcalloc(size_t nmemb, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 42 | { |
| 43 | void *ptr = calloc(nmemb, size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 44 | if (ptr == NULL && nmemb != 0 && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 46 | return ptr; |
| 47 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | #endif |
Eric Andersen | 9f894f4 | 2003-07-05 22:15:43 +0000 | [diff] [blame] | 49 | #endif /* DMALLOC */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | #ifdef L_xstrdup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 52 | char * bb_xstrdup (const char *s) |
| 53 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | char *t; |
| 55 | |
| 56 | if (s == NULL) |
| 57 | return NULL; |
| 58 | |
| 59 | t = strdup (s); |
| 60 | |
| 61 | if (t == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | |
| 64 | return t; |
| 65 | } |
| 66 | #endif |
| 67 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | #ifdef L_xstrndup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 69 | char * bb_xstrndup (const char *s, int n) |
| 70 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 71 | char *t; |
| 72 | |
| 73 | if (s == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | bb_error_msg_and_die("bb_xstrndup bug"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | |
| 76 | t = xmalloc(++n); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | return safe_strncpy(t,s,n); |
| 79 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | #ifdef L_xfopen |
| 83 | FILE *bb_xfopen(const char *path, const char *mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 84 | { |
| 85 | FILE *fp; |
| 86 | if ((fp = fopen(path, mode)) == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | bb_perror_msg_and_die("%s", path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 88 | return fp; |
| 89 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 91 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | #ifdef L_xopen |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 93 | int bb_xopen(const char *pathname, int flags) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 94 | { |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 95 | return bb_xopen3(pathname, flags, 0777); |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | #ifdef L_xopen3 |
| 100 | int bb_xopen3(const char *pathname, int flags, int mode) |
| 101 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 102 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 103 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 104 | ret = open(pathname, flags, mode); |
| 105 | if (ret < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | bb_perror_msg_and_die("%s", pathname); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 107 | } |
| 108 | return ret; |
| 109 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | #ifdef L_xread |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 113 | ssize_t bb_xread(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 114 | { |
| 115 | ssize_t size; |
| 116 | |
| 117 | size = read(fd, buf, count); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 118 | if (size < 0) { |
"Vladimir N. Oleynik" | 4eb2fd6 | 2005-11-25 11:36:36 +0000 | [diff] [blame] | 119 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 120 | } |
| 121 | return(size); |
| 122 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | #ifdef L_xread_all |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 126 | void bb_xread_all(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 127 | { |
| 128 | ssize_t size; |
| 129 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 130 | while (count) { |
| 131 | if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ |
| 132 | bb_error_msg_and_die("Short read"); |
| 133 | } |
| 134 | count -= size; |
Manuel Novoa III | 948d490 | 2004-03-08 05:44:30 +0000 | [diff] [blame] | 135 | buf = ((char *) buf) + size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 136 | } |
| 137 | return; |
| 138 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 140 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | #ifdef L_xread_char |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 142 | unsigned char bb_xread_char(int fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 143 | { |
| 144 | char tmp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 145 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | bb_xread_all(fd, &tmp, 1); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 147 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 148 | return(tmp); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 149 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 151 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | #ifdef L_xferror |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 153 | void bb_xferror(FILE *fp, const char *fn) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 154 | { |
| 155 | if (ferror(fp)) { |
| 156 | bb_error_msg_and_die("%s", fn); |
| 157 | } |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | #ifdef L_xferror_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 162 | void bb_xferror_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | { |
| 164 | bb_xferror(stdout, bb_msg_standard_output); |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | #ifdef L_xfflush_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 169 | void bb_xfflush_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 170 | { |
| 171 | if (fflush(stdout)) { |
| 172 | bb_perror_msg_and_die(bb_msg_standard_output); |
| 173 | } |
| 174 | } |
| 175 | #endif |
| 176 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 177 | /* END CODE */ |
| 178 | /* |
| 179 | Local Variables: |
| 180 | c-file-style: "linux" |
| 181 | c-basic-offset: 4 |
| 182 | tab-width: 4 |
| 183 | End: |
| 184 | */ |