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 | * |
| 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 |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
| 30 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | |
| 32 | #ifndef DMALLOC |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | #ifdef L_xmalloc |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | extern void *xmalloc(size_t size) |
| 35 | { |
| 36 | void *ptr = malloc(size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 37 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | return ptr; |
| 40 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 42 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | #ifdef L_xrealloc |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 44 | extern void *xrealloc(void *ptr, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | { |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 46 | ptr = realloc(ptr, size); |
| 47 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 49 | return ptr; |
| 50 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | #ifdef L_xcalloc |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | extern void *xcalloc(size_t nmemb, size_t size) |
| 55 | { |
| 56 | void *ptr = calloc(nmemb, size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 57 | if (ptr == NULL && nmemb != 0 && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 59 | return ptr; |
| 60 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | #endif |
Eric Andersen | 9f894f4 | 2003-07-05 22:15:43 +0000 | [diff] [blame] | 62 | #endif /* DMALLOC */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | #ifdef L_xstrdup |
| 65 | extern char * bb_xstrdup (const char *s) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 66 | char *t; |
| 67 | |
| 68 | if (s == NULL) |
| 69 | return NULL; |
| 70 | |
| 71 | t = strdup (s); |
| 72 | |
| 73 | if (t == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | |
| 76 | return t; |
| 77 | } |
| 78 | #endif |
| 79 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | #ifdef L_xstrndup |
| 81 | extern char * bb_xstrndup (const char *s, int n) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 82 | char *t; |
| 83 | |
| 84 | if (s == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | bb_error_msg_and_die("bb_xstrndup bug"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
| 87 | t = xmalloc(++n); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 88 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | return safe_strncpy(t,s,n); |
| 90 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | #ifdef L_xfopen |
| 94 | FILE *bb_xfopen(const char *path, const char *mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 95 | { |
| 96 | FILE *fp; |
| 97 | if ((fp = fopen(path, mode)) == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | bb_perror_msg_and_die("%s", path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 99 | return fp; |
| 100 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 102 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | #ifdef L_xopen |
| 104 | extern int bb_xopen(const char *pathname, int flags) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 105 | { |
| 106 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 107 | |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 108 | ret = open(pathname, flags, 0777); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 109 | if (ret == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | bb_perror_msg_and_die("%s", pathname); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | } |
| 112 | return ret; |
| 113 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 115 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | #ifdef L_xread |
| 117 | extern ssize_t bb_xread(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 118 | { |
| 119 | ssize_t size; |
| 120 | |
| 121 | size = read(fd, buf, count); |
| 122 | if (size == -1) { |
"Vladimir N. Oleynik" | 4eb2fd6 | 2005-11-25 11:36:36 +0000 | [diff] [blame] | 123 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | } |
| 125 | return(size); |
| 126 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 128 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | #ifdef L_xread_all |
| 130 | extern void bb_xread_all(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 131 | { |
| 132 | ssize_t size; |
| 133 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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; |
Manuel Novoa III | 948d490 | 2004-03-08 05:44:30 +0000 | [diff] [blame] | 139 | buf = ((char *) buf) + size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 140 | } |
| 141 | return; |
| 142 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 144 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | #ifdef L_xread_char |
| 146 | extern unsigned char bb_xread_char(int fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 147 | { |
| 148 | char tmp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 149 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | bb_xread_all(fd, &tmp, 1); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 151 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 152 | return(tmp); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 153 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 154 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 155 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 156 | #ifdef L_xferror |
| 157 | extern void bb_xferror(FILE *fp, const char *fn) |
| 158 | { |
| 159 | if (ferror(fp)) { |
| 160 | bb_error_msg_and_die("%s", fn); |
| 161 | } |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | #ifdef L_xferror_stdout |
| 166 | extern void bb_xferror_stdout(void) |
| 167 | { |
| 168 | bb_xferror(stdout, bb_msg_standard_output); |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #ifdef L_xfflush_stdout |
| 173 | extern void bb_xfflush_stdout(void) |
| 174 | { |
| 175 | if (fflush(stdout)) { |
| 176 | bb_perror_msg_and_die(bb_msg_standard_output); |
| 177 | } |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #ifdef L_strlen |
Eric Andersen | db7d5fc | 2002-04-13 13:02:03 +0000 | [diff] [blame] | 182 | /* Stupid gcc always includes its own builtin strlen()... */ |
Eric Andersen | c8459a5 | 2002-04-13 14:44:42 +0000 | [diff] [blame] | 183 | #undef strlen |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 184 | size_t bb_strlen(const char *string) |
Eric Andersen | db7d5fc | 2002-04-13 13:02:03 +0000 | [diff] [blame] | 185 | { |
| 186 | return(strlen(string)); |
| 187 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 188 | #endif |
Eric Andersen | db7d5fc | 2002-04-13 13:02:03 +0000 | [diff] [blame] | 189 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 190 | /* END CODE */ |
| 191 | /* |
| 192 | Local Variables: |
| 193 | c-file-style: "linux" |
| 194 | c-basic-offset: 4 |
| 195 | tab-width: 4 |
| 196 | End: |
| 197 | */ |