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