Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) tons of folks. Tracking down who wrote what |
| 6 | * isn't something I'm going to worry about... If you wrote something |
| 7 | * here, please feel free to acknowledge your work. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 24 | * Permission has been granted to redistribute this code under the GPL. |
| 25 | * |
| 26 | */ |
| 27 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 29 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ |
| 30 | || defined (BB_CP_MV) \ |
| 31 | || defined (BB_FIND) \ |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 32 | || defined (BB_INSMOD) \ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 33 | || defined (BB_LS) \ |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 34 | || defined (BB_RM) \ |
| 35 | || defined (BB_TAR) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 36 | /* same conditions as recursive_action */ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 37 | #define bb_need_name_too_long |
| 38 | #endif |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 39 | #define bb_need_memory_exhausted |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 40 | #define bb_need_full_version |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 41 | #define BB_DECLARE_EXTERN |
| 42 | #include "messages.c" |
| 43 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #include <errno.h> |
| 47 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 48 | #include <dirent.h> |
| 49 | #include <time.h> |
| 50 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 51 | #include <unistd.h> |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 52 | #include <ctype.h> |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 53 | #include <sys/ioctl.h> |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 54 | #include <sys/utsname.h> /* for uname(2) */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 56 | /* Busybox mount uses either /proc/filesystems or /dev/mtab to get the |
| 57 | * list of available filesystems used for the -t auto option */ |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 58 | #if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 59 | //#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again. |
| 60 | #error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again." |
| 61 | #endif |
| 62 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 63 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 64 | #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 65 | # if defined BB_MTAB |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 66 | const char mtab_file[] = "/etc/mtab"; |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 67 | # else |
| 68 | # if defined BB_FEATURE_USE_PROCFS |
| 69 | const char mtab_file[] = "/proc/mounts"; |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 70 | # else |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 71 | # if defined BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 72 | const char mtab_file[] = "/dev/mtab"; |
| 73 | # else |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 74 | # error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_PATCH) |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 75 | # endif |
| 76 | # endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 77 | # endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 80 | extern void usage(const char *usage) |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 81 | { |
Pavel Roskin | 9c5fcc3 | 2000-07-17 23:45:12 +0000 | [diff] [blame] | 82 | fprintf(stderr, "%s\n\n", full_version); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | fprintf(stderr, "Usage: %s\n", usage); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 84 | exit(EXIT_FAILURE); |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 87 | static void verror_msg(const char *s, va_list p) |
Mark Whitley | cf61fe1 | 2000-12-05 19:27:13 +0000 | [diff] [blame] | 88 | { |
| 89 | fflush(stdout); |
| 90 | fprintf(stderr, "%s: ", applet_name); |
| 91 | vfprintf(stderr, s, p); |
| 92 | fflush(stderr); |
| 93 | } |
| 94 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 95 | extern void error_msg(const char *s, ...) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 96 | { |
| 97 | va_list p; |
| 98 | |
| 99 | va_start(p, s); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 100 | verror_msg(s, p); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 101 | va_end(p); |
| 102 | } |
| 103 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 104 | extern void error_msg_and_die(const char *s, ...) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 105 | { |
| 106 | va_list p; |
| 107 | |
| 108 | va_start(p, s); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 109 | verror_msg(s, p); |
Mark Whitley | cf61fe1 | 2000-12-05 19:27:13 +0000 | [diff] [blame] | 110 | va_end(p); |
| 111 | exit(EXIT_FAILURE); |
| 112 | } |
| 113 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 114 | static void vperror_msg(const char *s, va_list p) |
Mark Whitley | cf61fe1 | 2000-12-05 19:27:13 +0000 | [diff] [blame] | 115 | { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 116 | fflush(stdout); |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 117 | fprintf(stderr, "%s: ", applet_name); |
Mark Whitley | cf61fe1 | 2000-12-05 19:27:13 +0000 | [diff] [blame] | 118 | if (s && *s) { |
| 119 | vfprintf(stderr, s, p); |
| 120 | fputs(": ", stderr); |
| 121 | } |
| 122 | fprintf(stderr, "%s\n", strerror(errno)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 123 | fflush(stderr); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 124 | } |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 125 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 126 | extern void perror_msg(const char *s, ...) |
Matt Kraai | ef5529b | 2000-10-25 17:00:36 +0000 | [diff] [blame] | 127 | { |
| 128 | va_list p; |
| 129 | |
| 130 | va_start(p, s); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 131 | vperror_msg(s, p); |
Matt Kraai | ef5529b | 2000-10-25 17:00:36 +0000 | [diff] [blame] | 132 | va_end(p); |
Matt Kraai | ef5529b | 2000-10-25 17:00:36 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 135 | extern void perror_msg_and_die(const char *s, ...) |
Matt Kraai | 324a778 | 2000-10-25 15:10:08 +0000 | [diff] [blame] | 136 | { |
| 137 | va_list p; |
| 138 | |
| 139 | va_start(p, s); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 140 | vperror_msg(s, p); |
Matt Kraai | 324a778 | 2000-10-25 15:10:08 +0000 | [diff] [blame] | 141 | va_end(p); |
Matt Kraai | 324a778 | 2000-10-25 15:10:08 +0000 | [diff] [blame] | 142 | exit(EXIT_FAILURE); |
| 143 | } |
| 144 | |
Eric Andersen | a2d61e7 | 2000-12-12 16:55:56 +0000 | [diff] [blame] | 145 | #if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 146 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, |
| 147 | * so, for example, to check if the kernel is greater than 2.2.11: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 148 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 149 | */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 150 | extern int get_kernel_revision(void) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 151 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 152 | struct utsname name; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 153 | int major = 0, minor = 0, patch = 0; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 154 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 155 | if (uname(&name) == -1) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 156 | perror_msg("cannot get system information"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | return (0); |
| 158 | } |
Eric Andersen | ef936da | 2000-10-30 17:22:04 +0000 | [diff] [blame] | 159 | major = atoi(strtok(name.release, ".")); |
| 160 | minor = atoi(strtok(NULL, ".")); |
| 161 | patch = atoi(strtok(NULL, ".")); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | return major * 65536 + minor * 256 + patch; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 163 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 164 | #endif /* BB_INIT */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 165 | |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | #if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 169 | _syscall1(int, sysinfo, struct sysinfo *, info); |
| 170 | #endif /* BB_INIT */ |
| 171 | |
Eric Andersen | a57ba4d | 2000-07-08 19:20:49 +0000 | [diff] [blame] | 172 | #if defined BB_MOUNT || defined BB_UMOUNT |
Eric Andersen | a57ba4d | 2000-07-08 19:20:49 +0000 | [diff] [blame] | 173 | |
| 174 | #ifndef __NR_umount2 |
| 175 | #define __NR_umount2 52 |
| 176 | #endif |
| 177 | |
| 178 | /* Include our own version of <sys/mount.h>, since libc5 doesn't |
| 179 | * know about umount2 */ |
| 180 | extern _syscall1(int, umount, const char *, special_file); |
| 181 | extern _syscall2(int, umount2, const char *, special_file, int, flags); |
| 182 | extern _syscall5(int, mount, const char *, special_file, const char *, dir, |
| 183 | const char *, fstype, unsigned long int, rwflag, const void *, data); |
| 184 | #endif |
| 185 | |
Eric Andersen | c86e5e6 | 2000-08-21 19:38:49 +0000 | [diff] [blame] | 186 | #if defined BB_INSMOD || defined BB_LSMOD |
| 187 | #ifndef __NR_query_module |
| 188 | #define __NR_query_module 167 |
| 189 | #endif |
| 190 | _syscall5(int, query_module, const char *, name, int, which, |
| 191 | void *, buf, size_t, bufsize, size_t*, ret); |
| 192 | #endif |
Eric Andersen | 10dc9d4 | 2000-06-26 10:45:52 +0000 | [diff] [blame] | 193 | |
| 194 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 195 | #if defined (BB_CP_MV) || defined (BB_DU) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 196 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 197 | #define HASH_SIZE 311 /* Should be prime */ |
| 198 | #define hash_inode(i) ((i) % HASH_SIZE) |
| 199 | |
| 200 | static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; |
| 201 | |
| 202 | /* |
| 203 | * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in |
| 204 | * `ino_dev_hashtable', else return 0 |
| 205 | * |
| 206 | * If NAME is a non-NULL pointer to a character pointer, and there is |
| 207 | * a match, then set *NAME to the value of the name slot in that |
| 208 | * bucket. |
| 209 | */ |
| 210 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name) |
| 211 | { |
| 212 | ino_dev_hashtable_bucket_t *bucket; |
| 213 | |
| 214 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 215 | while (bucket != NULL) { |
| 216 | if ((bucket->ino == statbuf->st_ino) && |
| 217 | (bucket->dev == statbuf->st_dev)) |
| 218 | { |
| 219 | if (name) *name = bucket->name; |
| 220 | return 1; |
| 221 | } |
| 222 | bucket = bucket->next; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Add statbuf to statbuf hash table */ |
| 228 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
| 229 | { |
| 230 | int i; |
| 231 | size_t s; |
| 232 | ino_dev_hashtable_bucket_t *bucket; |
| 233 | |
| 234 | i = hash_inode(statbuf->st_ino); |
| 235 | s = name ? strlen(name) : 0; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 236 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 237 | bucket->ino = statbuf->st_ino; |
| 238 | bucket->dev = statbuf->st_dev; |
| 239 | if (name) |
| 240 | strcpy(bucket->name, name); |
| 241 | else |
| 242 | bucket->name[0] = '\0'; |
| 243 | bucket->next = ino_dev_hashtable[i]; |
| 244 | ino_dev_hashtable[i] = bucket; |
| 245 | } |
| 246 | |
| 247 | /* Clear statbuf hash table */ |
| 248 | void reset_ino_dev_hashtable(void) |
| 249 | { |
| 250 | int i; |
| 251 | ino_dev_hashtable_bucket_t *bucket; |
| 252 | |
| 253 | for (i = 0; i < HASH_SIZE; i++) { |
| 254 | while (ino_dev_hashtable[i] != NULL) { |
| 255 | bucket = ino_dev_hashtable[i]->next; |
| 256 | free(ino_dev_hashtable[i]); |
| 257 | ino_dev_hashtable[i] = bucket; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 262 | #endif /* BB_CP_MV || BB_DU */ |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 263 | |
Glenn L McGrath | e699b9f | 2000-12-15 06:50:54 +0000 | [diff] [blame] | 264 | #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 265 | /* |
| 266 | * Return TRUE if a fileName is a directory. |
| 267 | * Nonexistant files return FALSE. |
| 268 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 269 | int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 270 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 271 | int status; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 272 | int didMalloc = 0; |
| 273 | |
| 274 | if (statBuf == NULL) { |
| 275 | statBuf = (struct stat *)xmalloc(sizeof(struct stat)); |
| 276 | ++didMalloc; |
| 277 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 278 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 279 | if (followLinks == TRUE) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 280 | status = stat(fileName, statBuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 281 | else |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 282 | status = lstat(fileName, statBuf); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 283 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 284 | if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { |
| 285 | status = FALSE; |
| 286 | } |
| 287 | else status = TRUE; |
| 288 | |
| 289 | if (didMalloc) { |
| 290 | free(statBuf); |
| 291 | statBuf = NULL; |
| 292 | } |
| 293 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 294 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 295 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 296 | |
Eric Andersen | ebee301 | 2000-09-05 22:17:22 +0000 | [diff] [blame] | 297 | #if defined (BB_AR) || defined BB_CP_MV |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 298 | /* |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 299 | * Copy chunksize bytes between two file descriptors |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 300 | */ |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 301 | int copy_file_chunk(int srcfd, int dstfd, size_t chunksize) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 302 | { |
| 303 | size_t size; |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 304 | char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */ |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 305 | |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 306 | while (chunksize > 0) { |
| 307 | if (chunksize > BUFSIZ) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 308 | size = BUFSIZ; |
| 309 | else |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 310 | size = chunksize; |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 311 | if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 312 | return(FALSE); |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 313 | chunksize -= size; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 314 | } |
| 315 | return (TRUE); |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 320 | #if defined (BB_CP_MV) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 321 | /* |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 322 | * Copy one file to another, while possibly preserving its modes, times, and |
| 323 | * modes. Returns TRUE if successful, or FALSE on a failure with an error |
| 324 | * message output. (Failure is not indicated if attributes cannot be set.) |
| 325 | * -Erik Andersen |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 326 | */ |
| 327 | int |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 328 | copy_file(const char *srcName, const char *destName, |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 329 | int setModes, int followLinks, int forceFlag) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 330 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 331 | int rfd; |
| 332 | int wfd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 333 | int status; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 334 | struct stat srcStatBuf; |
| 335 | struct stat dstStatBuf; |
| 336 | struct utimbuf times; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 337 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 338 | if (followLinks == TRUE) |
| 339 | status = stat(srcName, &srcStatBuf); |
| 340 | else |
| 341 | status = lstat(srcName, &srcStatBuf); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 342 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 343 | if (status < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 344 | perror_msg("%s", srcName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 345 | return FALSE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 346 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 347 | |
| 348 | if (followLinks == TRUE) |
| 349 | status = stat(destName, &dstStatBuf); |
| 350 | else |
| 351 | status = lstat(destName, &dstStatBuf); |
| 352 | |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 353 | if (status < 0 || forceFlag==TRUE) { |
| 354 | unlink(destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 355 | dstStatBuf.st_ino = -1; |
| 356 | dstStatBuf.st_dev = -1; |
| 357 | } |
| 358 | |
| 359 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 360 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 361 | error_msg("Copying file \"%s\" to itself\n", srcName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 362 | return FALSE; |
| 363 | } |
| 364 | |
| 365 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 366 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 367 | /* Make sure the directory is writable */ |
| 368 | status = mkdir(destName, 0777777 ^ umask(0)); |
| 369 | if (status < 0 && errno != EEXIST) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 370 | perror_msg("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 371 | return FALSE; |
| 372 | } |
| 373 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 374 | char link_val[BUFSIZ + 1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 375 | int link_size; |
| 376 | |
| 377 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 378 | /* Warning: This could possibly truncate silently, to BUFSIZ chars */ |
| 379 | link_size = readlink(srcName, &link_val[0], BUFSIZ); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | if (link_size < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 381 | perror_msg("%s", srcName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 382 | return FALSE; |
| 383 | } |
| 384 | link_val[link_size] = '\0'; |
| 385 | status = symlink(link_val, destName); |
| 386 | if (status < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 387 | perror_msg("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | return FALSE; |
| 389 | } |
Erik Andersen | 8e759aa | 2000-01-29 05:52:40 +0000 | [diff] [blame] | 390 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 391 | if (setModes == TRUE) { |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 392 | /* Try to set owner, but fail silently like GNU cp */ |
| 393 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 394 | } |
Erik Andersen | 8e759aa | 2000-01-29 05:52:40 +0000 | [diff] [blame] | 395 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 396 | return TRUE; |
| 397 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 398 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 399 | if (mkfifo(destName, 0644) < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 400 | perror_msg("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 401 | return FALSE; |
| 402 | } |
| 403 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 404 | || S_ISSOCK(srcStatBuf.st_mode)) { |
| 405 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 406 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 407 | perror_msg("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 408 | return FALSE; |
| 409 | } |
| 410 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 411 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 412 | rfd = open(srcName, O_RDONLY); |
| 413 | if (rfd < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 414 | perror_msg("%s", srcName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 415 | return FALSE; |
| 416 | } |
| 417 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 418 | wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 419 | srcStatBuf.st_mode); |
| 420 | if (wfd < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 421 | perror_msg("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 422 | close(rfd); |
| 423 | return FALSE; |
| 424 | } |
| 425 | |
Mark Whitley | 4758368 | 2000-12-05 20:03:17 +0000 | [diff] [blame] | 426 | if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 427 | goto error_exit; |
| 428 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | close(rfd); |
| 430 | if (close(wfd) < 0) { |
| 431 | return FALSE; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (setModes == TRUE) { |
| 436 | /* This is fine, since symlinks never get here */ |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 437 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) |
| 438 | perror_msg_and_die("%s", destName); |
| 439 | if (chmod(destName, srcStatBuf.st_mode) < 0) |
| 440 | perror_msg_and_die("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 441 | times.actime = srcStatBuf.st_atime; |
| 442 | times.modtime = srcStatBuf.st_mtime; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 443 | if (utime(destName, ×) < 0) |
| 444 | perror_msg_and_die("%s", destName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 447 | return TRUE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | error_exit: |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 450 | perror_msg("%s", destName); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 451 | close(rfd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | close(wfd); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 453 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 454 | return FALSE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 455 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 456 | #endif /* BB_CP_MV */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 457 | |
| 458 | |
| 459 | |
Glenn L McGrath | ac19b7e | 2000-09-11 05:06:13 +0000 | [diff] [blame] | 460 | #if defined BB_TAR || defined BB_LS ||defined BB_AR |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 461 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 462 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 463 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 464 | |
| 465 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ |
| 466 | static const mode_t SBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 467 | 0, 0, S_ISUID, |
| 468 | 0, 0, S_ISGID, |
| 469 | 0, 0, S_ISVTX |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | /* The 9 mode bits to test */ |
| 473 | static const mode_t MBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 474 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 475 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 476 | S_IROTH, S_IWOTH, S_IXOTH |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
Mark Whitley | 26a3adb | 2000-12-05 20:10:27 +0000 | [diff] [blame] | 479 | static const char MODE1[] = "rwxrwxrwx"; |
| 480 | static const char MODE0[] = "---------"; |
| 481 | static const char SMODE1[] = "..s..s..t"; |
| 482 | static const char SMODE0[] = "..S..S..T"; |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 483 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 484 | /* |
| 485 | * Return the standard ls-like mode string from a file mode. |
| 486 | * This is static and so is overwritten on each call. |
| 487 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 488 | const char *mode_string(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 489 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 490 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 491 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 492 | int i; |
| 493 | |
| 494 | buf[0] = TYPECHAR(mode); |
| 495 | for (i = 0; i < 9; i++) { |
| 496 | if (mode & SBIT[i]) |
| 497 | buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; |
| 498 | else |
| 499 | buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; |
| 500 | } |
| 501 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 502 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 503 | #endif /* BB_TAR || BB_LS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 504 | |
| 505 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 506 | #if defined BB_TAR || defined BB_AR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 507 | /* |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 508 | * Return the standard ls-like time string from a time_t |
| 509 | * This is static and so is overwritten on each call. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 510 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 511 | const char *time_string(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 512 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | time_t now; |
| 514 | char *str; |
| 515 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 516 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 517 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 518 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 519 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 520 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 521 | strcpy(buf, &str[4]); |
| 522 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 523 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 524 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 525 | strcpy(&buf[7], &str[20]); |
| 526 | buf[11] = '\0'; |
| 527 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 528 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 530 | } |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 531 | #endif /* BB_TAR || BB_AR */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 532 | |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 533 | #if defined BB_AR || defined BB_CP_MV || defined BB_DD || defined BB_NC || defined BB_TAR |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 534 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 535 | * Write all of the supplied buffer out to a file. |
| 536 | * This does multiple writes as necessary. |
| 537 | * Returns the amount written, or -1 on an error. |
| 538 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 539 | int full_write(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 540 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 541 | int cc; |
| 542 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 543 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 544 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 545 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 546 | while (len > 0) { |
| 547 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 548 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 549 | if (cc < 0) |
| 550 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 551 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 552 | buf += cc; |
| 553 | total += cc; |
| 554 | len -= cc; |
| 555 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 556 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 557 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 558 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 559 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 560 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 561 | #if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 562 | /* |
| 563 | * Read all of the supplied buffer from a file. |
| 564 | * This does multiple reads as necessary. |
| 565 | * Returns the amount read, or -1 on an error. |
| 566 | * A short read is returned on an end of file. |
| 567 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 568 | int full_read(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 569 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 570 | int cc; |
| 571 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 572 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 573 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 574 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 575 | while (len > 0) { |
| 576 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 577 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 578 | if (cc < 0) |
| 579 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 580 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 581 | if (cc == 0) |
| 582 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 583 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 584 | buf += cc; |
| 585 | total += cc; |
| 586 | len -= cc; |
| 587 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 588 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 589 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 590 | } |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 591 | #endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 592 | |
| 593 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 594 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ |
| 595 | || defined (BB_CP_MV) \ |
| 596 | || defined (BB_FIND) \ |
| 597 | || defined (BB_INSMOD) \ |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 598 | || defined (BB_LS) \ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 599 | || defined (BB_RM) \ |
| 600 | || defined (BB_TAR) |
| 601 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 602 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 603 | * Walk down all the directories under the specified |
| 604 | * location, and do something (something specified |
| 605 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 606 | * |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 607 | * Unfortunately, while nftw(3) could replace this and reduce |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 608 | * code size a bit, nftw() wasn't supported before GNU libc 2.1, |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 609 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 610 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 611 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 612 | int recursive_action(const char *fileName, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 613 | int recurse, int followLinks, int depthFirst, |
| 614 | int (*fileAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 615 | struct stat * statbuf, |
| 616 | void* userData), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 617 | int (*dirAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 618 | struct stat * statbuf, |
| 619 | void* userData), |
| 620 | void* userData) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 621 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 622 | int status; |
| 623 | struct stat statbuf; |
| 624 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 625 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 626 | if (followLinks == TRUE) |
| 627 | status = stat(fileName, &statbuf); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 628 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 629 | status = lstat(fileName, &statbuf); |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 630 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 631 | if (status < 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 632 | #ifdef BB_DEBUG_PRINT_SCAFFOLD |
| 633 | fprintf(stderr, |
| 634 | "status=%d followLinks=%d TRUE=%d\n", |
| 635 | status, followLinks, TRUE); |
| 636 | #endif |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 637 | perror_msg("%s", fileName); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 638 | return FALSE; |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 639 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 640 | |
| 641 | if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) { |
| 642 | if (fileAction == NULL) |
| 643 | return TRUE; |
| 644 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 645 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | if (recurse == FALSE) { |
| 649 | if (S_ISDIR(statbuf.st_mode)) { |
| 650 | if (dirAction != NULL) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 651 | return (dirAction(fileName, &statbuf, userData)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 652 | else |
| 653 | return TRUE; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (S_ISDIR(statbuf.st_mode)) { |
| 658 | DIR *dir; |
| 659 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 660 | if (dirAction != NULL && depthFirst == FALSE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 661 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 662 | if (status == FALSE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 663 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 664 | return FALSE; |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 665 | } else if (status == SKIP) |
| 666 | return TRUE; |
| 667 | } |
| 668 | dir = opendir(fileName); |
| 669 | if (!dir) { |
| 670 | perror_msg("%s", fileName); |
| 671 | return FALSE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 672 | } |
| 673 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 674 | char nextFile[BUFSIZ + 1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 675 | |
| 676 | if ((strcmp(next->d_name, "..") == 0) |
| 677 | || (strcmp(next->d_name, ".") == 0)) { |
| 678 | continue; |
| 679 | } |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 680 | if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 681 | error_msg(name_too_long); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 682 | return FALSE; |
| 683 | } |
Erik Andersen | c366050 | 2000-04-18 21:18:57 +0000 | [diff] [blame] | 684 | memset(nextFile, 0, sizeof(nextFile)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 685 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 686 | status = |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 687 | recursive_action(nextFile, TRUE, followLinks, depthFirst, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 688 | fileAction, dirAction, userData); |
Matt Kraai | 0f8f7b8 | 2000-08-01 18:16:56 +0000 | [diff] [blame] | 689 | if (status == FALSE) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 690 | closedir(dir); |
| 691 | return FALSE; |
| 692 | } |
| 693 | } |
| 694 | status = closedir(dir); |
| 695 | if (status < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 696 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 697 | return FALSE; |
| 698 | } |
| 699 | if (dirAction != NULL && depthFirst == TRUE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 700 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 701 | if (status == FALSE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 702 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | return FALSE; |
| 704 | } |
| 705 | } |
| 706 | } else { |
| 707 | if (fileAction == NULL) |
| 708 | return TRUE; |
| 709 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 710 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 711 | } |
| 712 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 715 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 716 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 717 | |
| 718 | |
Glenn L McGrath | e699b9f | 2000-12-15 06:50:54 +0000 | [diff] [blame] | 719 | #if defined (BB_TAR) || defined (BB_MKDIR) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 720 | /* |
| 721 | * Attempt to create the directories along the specified path, except for |
| 722 | * the final component. The mode is given for the final directory only, |
| 723 | * while all previous ones get default protections. Errors are not reported |
| 724 | * here, as failures to restore files can be reported later. |
| 725 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 726 | extern int create_path(const char *name, int mode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 727 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 728 | char *cp; |
| 729 | char *cpOld; |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 730 | char buf[BUFSIZ + 1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 731 | int retVal = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 732 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 733 | strcpy(buf, name); |
| 734 | for (cp = buf; *cp == '/'; cp++); |
| 735 | cp = strchr(cp, '/'); |
| 736 | while (cp) { |
| 737 | cpOld = cp; |
| 738 | cp = strchr(cp + 1, '/'); |
| 739 | *cpOld = '\0'; |
| 740 | retVal = mkdir(buf, cp ? 0777 : mode); |
| 741 | if (retVal != 0 && errno != EEXIST) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 742 | perror_msg("%s", buf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 743 | return FALSE; |
| 744 | } |
| 745 | *cpOld = '/'; |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 746 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 747 | return TRUE; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 748 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 749 | #endif /* BB_TAR || BB_MKDIR */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 750 | |
| 751 | |
| 752 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 753 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \ |
Glenn L McGrath | ac19b7e | 2000-09-11 05:06:13 +0000 | [diff] [blame] | 754 | || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 755 | /* [ugoa]{+|-|=}[rwxst] */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 756 | |
| 757 | |
| 758 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 759 | extern int parse_mode(const char *s, mode_t * theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 760 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 761 | mode_t andMode = |
| 762 | |
| 763 | S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 764 | mode_t orMode = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 765 | mode_t mode = 0; |
| 766 | mode_t groups = 0; |
| 767 | char type; |
| 768 | char c; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 769 | |
Eric Andersen | 4e61db2 | 2000-09-01 00:06:17 +0000 | [diff] [blame] | 770 | if (s==NULL) |
| 771 | return (FALSE); |
| 772 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 773 | do { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 774 | for (;;) { |
| 775 | switch (c = *s++) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 776 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 777 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 778 | case 'u': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 779 | groups |= S_ISUID | S_IRWXU; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 780 | continue; |
| 781 | case 'g': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 782 | groups |= S_ISGID | S_IRWXG; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 783 | continue; |
| 784 | case 'o': |
| 785 | groups |= S_IRWXO; |
| 786 | continue; |
| 787 | case 'a': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 788 | groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 789 | continue; |
| 790 | case '+': |
| 791 | case '=': |
| 792 | case '-': |
| 793 | type = c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 794 | if (groups == 0) /* The default is "all" */ |
| 795 | groups |= |
| 796 | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 797 | break; |
| 798 | default: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 799 | if (isdigit(c) && c >= '0' && c <= '7' && |
| 800 | mode == 0 && groups == 0) { |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 801 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 802 | return (TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 803 | } else |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 804 | return (FALSE); |
| 805 | } |
| 806 | break; |
| 807 | } |
| 808 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 809 | while ((c = *s++) != '\0') { |
| 810 | switch (c) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 811 | case ',': |
| 812 | break; |
| 813 | case 'r': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 814 | mode |= S_IRUSR | S_IRGRP | S_IROTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 815 | continue; |
| 816 | case 'w': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 817 | mode |= S_IWUSR | S_IWGRP | S_IWOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 818 | continue; |
| 819 | case 'x': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 820 | mode |= S_IXUSR | S_IXGRP | S_IXOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 821 | continue; |
| 822 | case 's': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 823 | mode |= S_IXGRP | S_ISUID | S_ISGID; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 824 | continue; |
| 825 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 826 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 827 | continue; |
| 828 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 829 | *theMode &= andMode; |
| 830 | *theMode |= orMode; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 831 | return (TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 832 | } |
| 833 | break; |
| 834 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 835 | switch (type) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 836 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 837 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 838 | /* fall through */ |
| 839 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 840 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 841 | break; |
| 842 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 843 | andMode &= ~(mode & groups); |
| 844 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 845 | break; |
| 846 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 847 | } while (c == ','); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 848 | *theMode &= andMode; |
| 849 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 850 | return (TRUE); |
| 851 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 852 | |
| 853 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 854 | #endif |
| 855 | /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 856 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 857 | |
| 858 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 859 | |
| 860 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 861 | #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \ |
| 862 | || defined BB_TAR || defined BB_ID || defined BB_LOGGER \ |
Eric Andersen | a2d61e7 | 2000-12-12 16:55:56 +0000 | [diff] [blame] | 863 | || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 864 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 865 | /* This parses entries in /etc/passwd and /etc/group. This is desirable |
| 866 | * for BusyBox, since we want to avoid using the glibc NSS stuff, which |
| 867 | * increases target size and is often not needed or wanted for embedded |
| 868 | * systems. |
| 869 | * |
| 870 | * /etc/passwd entries look like this: |
| 871 | * root:x:0:0:root:/root:/bin/bash |
| 872 | * and /etc/group entries look like this: |
| 873 | * root:x:0: |
| 874 | * |
| 875 | * This uses buf as storage to hold things. |
| 876 | * |
| 877 | */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 878 | unsigned long my_getid(const char *filename, char *name, long id, long *gid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 879 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 880 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 881 | char *rname, *start, *end, buf[128]; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 882 | long rid; |
| 883 | long rgid = 0; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 884 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 885 | file = fopen(filename, "r"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 886 | if (file == NULL) { |
Eric Andersen | 3199bda | 2000-06-02 17:38:18 +0000 | [diff] [blame] | 887 | /* Do not complain. It is ok for /etc/passwd and |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 888 | * friends to be missing... */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 889 | return (-1); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 890 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 891 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 892 | while (fgets(buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 893 | if (buf[0] == '#') |
| 894 | continue; |
| 895 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 896 | /* username/group name */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 897 | start = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 898 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 899 | if (end == NULL) |
| 900 | continue; |
| 901 | *end = '\0'; |
| 902 | rname = start; |
| 903 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 904 | /* password */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 905 | start = end + 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 906 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 907 | if (end == NULL) |
| 908 | continue; |
| 909 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 910 | /* uid in passwd, gid in group */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 911 | start = end + 1; |
Erik Andersen | 5b46d79 | 2000-05-17 05:24:24 +0000 | [diff] [blame] | 912 | rid = (unsigned long) strtol(start, &end, 10); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 913 | if (end == start) |
| 914 | continue; |
| 915 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 916 | /* gid in passwd */ |
| 917 | start = end + 1; |
Erik Andersen | 5b46d79 | 2000-05-17 05:24:24 +0000 | [diff] [blame] | 918 | rgid = (unsigned long) strtol(start, &end, 10); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 919 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 920 | if (name) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 921 | if (0 == strcmp(rname, name)) { |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 922 | if (gid) *gid = rgid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 923 | fclose(file); |
| 924 | return (rid); |
| 925 | } |
| 926 | } |
| 927 | if (id != -1 && id == rid) { |
| 928 | strncpy(name, rname, 8); |
Eric Andersen | 2d6282b | 2000-12-11 20:33:46 +0000 | [diff] [blame] | 929 | name[8]='\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 930 | if (gid) *gid = rgid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 931 | fclose(file); |
| 932 | return (TRUE); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 935 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 936 | return (-1); |
| 937 | } |
| 938 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 939 | /* returns a uid given a username */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 940 | long my_getpwnam(char *name) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 941 | { |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 942 | return my_getid("/etc/passwd", name, -1, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 945 | /* returns a gid given a group name */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 946 | long my_getgrnam(char *name) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 947 | { |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 948 | return my_getid("/etc/group", name, -1, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 951 | /* gets a username given a uid */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 952 | void my_getpwuid(char *name, long uid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 953 | { |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 954 | name[0] = '\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 955 | my_getid("/etc/passwd", name, uid, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 958 | /* gets a groupname given a gid */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 959 | void my_getgrgid(char *group, long gid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 960 | { |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 961 | group[0] = '\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 962 | my_getid("/etc/group", group, gid, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 965 | /* gets a gid given a user name */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 966 | long my_getpwnamegid(char *name) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 967 | { |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 968 | long gid; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 969 | my_getid("/etc/passwd", name, -1, &gid); |
| 970 | return gid; |
| 971 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 972 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 973 | #endif |
| 974 | /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \ |
| 975 | || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 976 | |
| 977 | |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 978 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 979 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 980 | /* From <linux/kd.h> */ |
| 981 | #define KDGKBTYPE 0x4B33 /* get keyboard type */ |
| 982 | #define KB_84 0x01 |
| 983 | #define KB_101 0x02 /* this is what we always answer */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 984 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 985 | int is_a_console(int fd) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 986 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 987 | char arg; |
| 988 | |
| 989 | arg = 0; |
| 990 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 991 | && ((arg == KB_101) || (arg == KB_84))); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 994 | static int open_a_console(char *fnam) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 995 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 996 | int fd; |
| 997 | |
| 998 | /* try read-only */ |
| 999 | fd = open(fnam, O_RDWR); |
| 1000 | |
| 1001 | /* if failed, try read-only */ |
| 1002 | if (fd < 0 && errno == EACCES) |
| 1003 | fd = open(fnam, O_RDONLY); |
| 1004 | |
| 1005 | /* if failed, try write-only */ |
| 1006 | if (fd < 0 && errno == EACCES) |
| 1007 | fd = open(fnam, O_WRONLY); |
| 1008 | |
| 1009 | /* if failed, fail */ |
| 1010 | if (fd < 0) |
| 1011 | return -1; |
| 1012 | |
| 1013 | /* if not a console, fail */ |
| 1014 | if (!is_a_console(fd)) { |
| 1015 | close(fd); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | |
| 1019 | /* success */ |
| 1020 | return fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * Get an fd for use with kbd/console ioctls. |
| 1025 | * We try several things because opening /dev/console will fail |
| 1026 | * if someone else used X (which does a chown on /dev/console). |
| 1027 | * |
| 1028 | * if tty_name is non-NULL, try this one instead. |
| 1029 | */ |
| 1030 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1031 | int get_console_fd(char *tty_name) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1032 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1033 | int fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1034 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1035 | if (tty_name) { |
| 1036 | if (-1 == (fd = open_a_console(tty_name))) |
| 1037 | return -1; |
| 1038 | else |
| 1039 | return fd; |
| 1040 | } |
| 1041 | |
| 1042 | fd = open_a_console("/dev/tty"); |
| 1043 | if (fd >= 0) |
| 1044 | return fd; |
| 1045 | |
| 1046 | fd = open_a_console("/dev/tty0"); |
| 1047 | if (fd >= 0) |
| 1048 | return fd; |
| 1049 | |
| 1050 | fd = open_a_console("/dev/console"); |
| 1051 | if (fd >= 0) |
| 1052 | return fd; |
| 1053 | |
| 1054 | for (fd = 0; fd < 3; fd++) |
| 1055 | if (is_a_console(fd)) |
| 1056 | return fd; |
| 1057 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1058 | error_msg("Couldnt get a file descriptor referring to the console\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1059 | return -1; /* total failure */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 1063 | #endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1064 | |
| 1065 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1066 | #if defined BB_FIND || defined BB_INSMOD |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1067 | /* |
| 1068 | * Routine to see if a text string is matched by a wildcard pattern. |
| 1069 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 1070 | * or if the pattern is invalid. |
| 1071 | * * matches zero or more characters |
| 1072 | * ? matches a single character |
| 1073 | * [abc] matches 'a', 'b' or 'c' |
| 1074 | * \c quotes character c |
| 1075 | * Adapted from code written by Ingo Wilken, and |
| 1076 | * then taken from sash, Copyright (c) 1999 by David I. Bell |
| 1077 | * Permission is granted to use, distribute, or modify this source, |
| 1078 | * provided that this copyright notice remains intact. |
| 1079 | * Permission to distribute this code under the GPL has been granted. |
| 1080 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1081 | extern int check_wildcard_match(const char *text, const char *pattern) |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1082 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1083 | const char *retryPat; |
| 1084 | const char *retryText; |
| 1085 | int ch; |
| 1086 | int found; |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1087 | int len; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1088 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1089 | retryPat = NULL; |
| 1090 | retryText = NULL; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1091 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1092 | while (*text || *pattern) { |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1093 | ch = *pattern++; |
| 1094 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1095 | switch (ch) { |
| 1096 | case '*': |
| 1097 | retryPat = pattern; |
| 1098 | retryText = text; |
| 1099 | break; |
| 1100 | |
| 1101 | case '[': |
| 1102 | found = FALSE; |
| 1103 | |
| 1104 | while ((ch = *pattern++) != ']') { |
| 1105 | if (ch == '\\') |
| 1106 | ch = *pattern++; |
| 1107 | |
| 1108 | if (ch == '\0') |
| 1109 | return FALSE; |
| 1110 | |
| 1111 | if (*text == ch) |
| 1112 | found = TRUE; |
| 1113 | } |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1114 | len=strlen(text); |
| 1115 | if (found == FALSE && len!=0) { |
| 1116 | return FALSE; |
| 1117 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1118 | if (found == TRUE) { |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1119 | if (strlen(pattern)==0 && len==1) { |
| 1120 | return TRUE; |
| 1121 | } |
| 1122 | if (len!=0) { |
| 1123 | text++; |
| 1124 | continue; |
Erik Andersen | c366050 | 2000-04-18 21:18:57 +0000 | [diff] [blame] | 1125 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* fall into next case */ |
| 1129 | |
| 1130 | case '?': |
| 1131 | if (*text++ == '\0') |
| 1132 | return FALSE; |
| 1133 | |
| 1134 | break; |
| 1135 | |
| 1136 | case '\\': |
| 1137 | ch = *pattern++; |
| 1138 | |
| 1139 | if (ch == '\0') |
| 1140 | return FALSE; |
| 1141 | |
| 1142 | /* fall into next case */ |
| 1143 | |
| 1144 | default: |
| 1145 | if (*text == ch) { |
| 1146 | if (*text) |
| 1147 | text++; |
| 1148 | break; |
| 1149 | } |
| 1150 | |
| 1151 | if (*text) { |
| 1152 | pattern = retryPat; |
| 1153 | text = ++retryText; |
| 1154 | break; |
| 1155 | } |
| 1156 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1157 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1160 | if (pattern == NULL) |
| 1161 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1164 | return TRUE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1165 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1166 | #endif /* BB_FIND || BB_INSMOD */ |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1167 | |
| 1168 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1169 | |
| 1170 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1171 | #if defined BB_DF || defined BB_MTAB |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1172 | /* |
| 1173 | * Given a block device, find the mount table entry if that block device |
| 1174 | * is mounted. |
| 1175 | * |
| 1176 | * Given any other file (or directory), find the mount table entry for its |
| 1177 | * filesystem. |
| 1178 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1179 | extern struct mntent *find_mount_point(const char *name, const char *table) |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1180 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1181 | struct stat s; |
| 1182 | dev_t mountDevice; |
| 1183 | FILE *mountTable; |
| 1184 | struct mntent *mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1185 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1186 | if (stat(name, &s) != 0) |
| 1187 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1188 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1189 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 1190 | mountDevice = s.st_rdev; |
| 1191 | else |
| 1192 | mountDevice = s.st_dev; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1193 | |
| 1194 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1195 | if ((mountTable = setmntent(table, "r")) == 0) |
| 1196 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1197 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1198 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 1199 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 1200 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 1201 | break; |
| 1202 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 1203 | break; |
| 1204 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 1205 | break; |
| 1206 | } |
| 1207 | endmntent(mountTable); |
| 1208 | return mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1209 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1210 | #endif /* BB_DF || BB_MTAB */ |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1211 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 1212 | #if defined BB_INIT || defined BB_SYSLOGD |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1213 | /* try to open up the specified device */ |
| 1214 | extern int device_open(char *device, int mode) |
| 1215 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1216 | int m, f, fd = -1; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1217 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1218 | m = mode | O_NONBLOCK; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1219 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1220 | /* Retry up to 5 times */ |
| 1221 | for (f = 0; f < 5; f++) |
| 1222 | if ((fd = open(device, m, 0600)) >= 0) |
| 1223 | break; |
| 1224 | if (fd < 0) |
| 1225 | return fd; |
| 1226 | /* Reset original flags. */ |
| 1227 | if (m != mode) |
| 1228 | fcntl(fd, F_SETFL, mode); |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1229 | return fd; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1230 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1231 | #endif /* BB_INIT BB_SYSLOGD */ |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1232 | |
| 1233 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1234 | #if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF )) |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1235 | #ifdef BB_FEATURE_USE_DEVPS_PATCH |
Eric Andersen | c674d70 | 2000-07-10 22:57:14 +0000 | [diff] [blame] | 1236 | #include <linux/devps.h> /* For Erik's nifty devps device driver */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1237 | #endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1238 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1239 | #if defined BB_FEATURE_USE_DEVPS_PATCH |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1240 | /* find_pid_by_name() |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1241 | * |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1242 | * This finds the pid of the specified process, |
| 1243 | * by using the /dev/ps device driver. |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1244 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1245 | * Returns a list of all matching PIDs |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1246 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1247 | extern pid_t* find_pid_by_name( char* pidName) |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1248 | { |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1249 | int fd, i, j; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1250 | char device[] = "/dev/ps"; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1251 | pid_t num_pids; |
| 1252 | pid_t* pid_array = NULL; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1253 | pid_t* pidList=NULL; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* open device */ |
| 1256 | fd = open(device, O_RDONLY); |
| 1257 | if (fd < 0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1258 | perror_msg_and_die("open failed for `%s'", device); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* Find out how many processes there are */ |
| 1261 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1262 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* Allocate some memory -- grab a few extras just in case |
| 1265 | * some new processes start up while we wait. The kernel will |
| 1266 | * just ignore any extras if we give it too many, and will trunc. |
| 1267 | * the list if we give it too few. */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1268 | pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1269 | pid_array[0] = num_pids+10; |
| 1270 | |
| 1271 | /* Now grab the pid list */ |
| 1272 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1273 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1274 | |
| 1275 | /* Now search for a match */ |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 1276 | for (i=1, j=0; i<pid_array[0] ; i++) { |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1277 | char* p; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1278 | struct pid_info info; |
| 1279 | |
| 1280 | info.pid = pid_array[i]; |
| 1281 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1282 | perror_msg_and_die("\nDEVPS_GET_PID_INFO"); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1283 | |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1284 | /* Make sure we only match on the process name */ |
| 1285 | p=info.command_line+1; |
| 1286 | while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { |
| 1287 | (p)++; |
| 1288 | } |
| 1289 | if (isspace(*(p))) |
| 1290 | *p='\0'; |
| 1291 | |
Erik Andersen | b9167cb | 2000-04-18 22:41:30 +0000 | [diff] [blame] | 1292 | if ((strstr(info.command_line, pidName) != NULL) |
| 1293 | && (strlen(pidName) == strlen(info.command_line))) { |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1294 | pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1295 | pidList[j++]=info.pid; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1298 | if (pidList) |
| 1299 | pidList[j]=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* Free memory */ |
| 1302 | free( pid_array); |
| 1303 | |
| 1304 | /* close device */ |
| 1305 | if (close (fd) != 0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1306 | perror_msg_and_die("close failed for `%s'", device); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1307 | |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1308 | return pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1309 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1310 | #else /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1311 | #if ! defined BB_FEATURE_USE_PROCFS |
| 1312 | #error Sorry, I depend on the /proc filesystem right now. |
| 1313 | #endif |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1314 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1315 | /* find_pid_by_name() |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1316 | * |
| 1317 | * This finds the pid of the specified process. |
| 1318 | * Currently, it's implemented by rummaging through |
| 1319 | * the proc filesystem. |
| 1320 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1321 | * Returns a list of all matching PIDs |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1322 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1323 | extern pid_t* find_pid_by_name( char* pidName) |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1324 | { |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1325 | DIR *dir; |
| 1326 | struct dirent *next; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1327 | pid_t* pidList=NULL; |
| 1328 | int i=0; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1329 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1330 | dir = opendir("/proc"); |
| 1331 | if (!dir) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1332 | perror_msg_and_die("Cannot open /proc"); |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1333 | |
| 1334 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1335 | FILE *status; |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1336 | char filename[256]; |
| 1337 | char buffer[256]; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1338 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1339 | /* If it isn't a number, we don't want it */ |
| 1340 | if (!isdigit(*next->d_name)) |
| 1341 | continue; |
| 1342 | |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1343 | sprintf(filename, "/proc/%s/cmdline", next->d_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1344 | status = fopen(filename, "r"); |
| 1345 | if (!status) { |
| 1346 | continue; |
| 1347 | } |
| 1348 | fgets(buffer, 256, status); |
| 1349 | fclose(status); |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1350 | |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1351 | if (strstr(get_last_path_component(buffer), pidName) != NULL) { |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1352 | pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1353 | pidList[i++]=strtol(next->d_name, NULL, 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1354 | } |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1355 | } |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1356 | |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1357 | if (pidList) |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 1358 | pidList[i]=0; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1359 | return pidList; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1360 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1361 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1362 | #endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */ |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1363 | |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1364 | #ifndef DMALLOC |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1365 | /* this should really be farmed out to libbusybox.a */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1366 | extern void *xmalloc(size_t size) |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1367 | { |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1368 | void *ptr = malloc(size); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1369 | |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1370 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1371 | error_msg_and_die(memory_exhausted); |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1372 | return ptr; |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1375 | extern void *xrealloc(void *old, size_t size) |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1376 | { |
| 1377 | void *ptr = realloc(old, size); |
| 1378 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1379 | error_msg_and_die(memory_exhausted); |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1380 | return ptr; |
| 1381 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1382 | |
| 1383 | extern void *xcalloc(size_t nmemb, size_t size) |
| 1384 | { |
| 1385 | void *ptr = calloc(nmemb, size); |
| 1386 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1387 | error_msg_and_die(memory_exhausted); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1388 | return ptr; |
| 1389 | } |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1390 | #endif |
| 1391 | |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 1392 | #if defined BB_FEATURE_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1393 | # ifndef DMALLOC |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1394 | extern char * xstrdup (const char *s) { |
| 1395 | char *t; |
| 1396 | |
| 1397 | if (s == NULL) |
| 1398 | return NULL; |
| 1399 | |
| 1400 | t = strdup (s); |
| 1401 | |
| 1402 | if (t == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1403 | error_msg_and_die(memory_exhausted); |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1404 | |
| 1405 | return t; |
| 1406 | } |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1407 | # endif |
Matt Kraai | 88cc305 | 2000-08-26 16:52:28 +0000 | [diff] [blame] | 1408 | #endif |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1409 | |
Matt Kraai | 88cc305 | 2000-08-26 16:52:28 +0000 | [diff] [blame] | 1410 | #if defined BB_FEATURE_NFSMOUNT |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1411 | extern char * xstrndup (const char *s, int n) { |
| 1412 | char *t; |
| 1413 | |
| 1414 | if (s == NULL) |
Matt Kraai | 0dab829 | 2000-12-18 03:08:29 +0000 | [diff] [blame] | 1415 | error_msg_and_die("xstrndup bug\n"); |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1416 | |
| 1417 | t = xmalloc(n+1); |
| 1418 | strncpy(t,s,n); |
| 1419 | t[n] = 0; |
| 1420 | |
| 1421 | return t; |
| 1422 | } |
| 1423 | #endif |
| 1424 | |
| 1425 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1426 | #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1427 | extern int vdprintf(int d, const char *format, va_list ap) |
| 1428 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1429 | char buf[BUF_SIZE]; |
| 1430 | int len; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1431 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1432 | len = vsprintf(buf, format, ap); |
| 1433 | return write(d, buf, len); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1434 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1435 | #endif /* BB_SYSLOGD */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1436 | |
Eric Andersen | 624cc77 | 2000-09-21 02:04:51 +0000 | [diff] [blame] | 1437 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1438 | #if defined BB_FEATURE_MOUNT_LOOP |
Eric Andersen | 624cc77 | 2000-09-21 02:04:51 +0000 | [diff] [blame] | 1439 | #include <fcntl.h> |
| 1440 | #include "loop.h" /* Pull in loop device support */ |
| 1441 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1442 | extern int del_loop(const char *device) |
| 1443 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1444 | int fd; |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1445 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1446 | if ((fd = open(device, O_RDONLY)) < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1447 | perror_msg("%s", device); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1448 | return (FALSE); |
| 1449 | } |
| 1450 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1451 | perror_msg("ioctl: LOOP_CLR_FD"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1452 | return (FALSE); |
| 1453 | } |
| 1454 | close(fd); |
| 1455 | return (TRUE); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1456 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1457 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1458 | extern int set_loop(const char *device, const char *file, int offset, |
| 1459 | int *loopro) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1460 | { |
| 1461 | struct loop_info loopinfo; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1462 | int fd, ffd, mode; |
| 1463 | |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1464 | mode = *loopro ? O_RDONLY : O_RDWR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1465 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 1466 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1467 | perror_msg("%s", file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1468 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1469 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1470 | if ((fd = open(device, mode)) < 0) { |
| 1471 | close(ffd); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1472 | perror_msg("%s", device); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1473 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1474 | } |
| 1475 | *loopro = (mode == O_RDONLY); |
| 1476 | |
| 1477 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 1478 | strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1479 | loopinfo.lo_name[LO_NAME_SIZE - 1] = 0; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1480 | |
| 1481 | loopinfo.lo_offset = offset; |
| 1482 | |
| 1483 | loopinfo.lo_encrypt_key_size = 0; |
| 1484 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1485 | perror_msg("ioctl: LOOP_SET_FD"); |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1486 | close(fd); |
| 1487 | close(ffd); |
| 1488 | return 1; |
| 1489 | } |
| 1490 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 1491 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1492 | perror_msg("ioctl: LOOP_SET_STATUS"); |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1493 | close(fd); |
| 1494 | close(ffd); |
| 1495 | return 1; |
| 1496 | } |
| 1497 | close(fd); |
| 1498 | close(ffd); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1502 | extern char *find_unused_loop_device(void) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1503 | { |
| 1504 | char dev[20]; |
| 1505 | int i, fd; |
| 1506 | struct stat statbuf; |
| 1507 | struct loop_info loopinfo; |
| 1508 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1509 | for (i = 0; i <= 7; i++) { |
| 1510 | sprintf(dev, "/dev/loop%d", i); |
| 1511 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 1512 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 1513 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) { |
| 1514 | if (errno == ENXIO) { /* probably free */ |
| 1515 | close(fd); |
| 1516 | return strdup(dev); |
| 1517 | } |
| 1518 | } |
| 1519 | close(fd); |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1520 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1521 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1522 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1523 | return NULL; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1524 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1525 | #endif /* BB_FEATURE_MOUNT_LOOP */ |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1526 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1527 | #if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB) |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1528 | extern int find_real_root_device_name(char* name) |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1529 | { |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1530 | DIR *dir; |
| 1531 | struct dirent *entry; |
| 1532 | struct stat statBuf, rootStat; |
| 1533 | char fileName[BUFSIZ]; |
| 1534 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1535 | if (stat("/", &rootStat) != 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1536 | error_msg("could not stat '/'\n"); |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1537 | return( FALSE); |
| 1538 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1539 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1540 | dir = opendir("/dev"); |
| 1541 | if (!dir) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1542 | error_msg("could not open '/dev'\n"); |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1543 | return( FALSE); |
| 1544 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1545 | |
| 1546 | while((entry = readdir(dir)) != NULL) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1547 | |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1548 | /* Must skip ".." since that is "/", and so we |
| 1549 | * would get a false positive on ".." */ |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1550 | if (strcmp(entry->d_name, "..") == 0) |
| 1551 | continue; |
| 1552 | |
Eric Andersen | f9ca653 | 2000-08-11 17:45:02 +0000 | [diff] [blame] | 1553 | snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1554 | |
| 1555 | if (stat(fileName, &statBuf) != 0) |
| 1556 | continue; |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1557 | /* Some char devices have the same dev_t as block |
| 1558 | * devices, so make sure this is a block device */ |
| 1559 | if (! S_ISBLK(statBuf.st_mode)) |
| 1560 | continue; |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1561 | if (statBuf.st_rdev == rootStat.st_rdev) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1562 | strcpy(name, fileName); |
| 1563 | return ( TRUE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1567 | return( FALSE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1568 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1569 | #endif |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1570 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* get_line_from_file() - This function reads an entire line from a text file |
| 1573 | * up to a newline. It returns a malloc'ed char * which must be stored and |
| 1574 | * free'ed by the caller. */ |
| 1575 | extern char *get_line_from_file(FILE *file) |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1576 | { |
Mark Whitley | 9a8243a | 2000-07-10 19:31:31 +0000 | [diff] [blame] | 1577 | static const int GROWBY = 80; /* how large we will grow strings by */ |
| 1578 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1579 | int ch; |
| 1580 | int idx = 0; |
| 1581 | char *linebuf = NULL; |
| 1582 | int linebufsz = 0; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1583 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1584 | while (1) { |
| 1585 | ch = fgetc(file); |
| 1586 | if (ch == EOF) |
| 1587 | break; |
| 1588 | /* grow the line buffer as necessary */ |
Eric Andersen | 285b446 | 2000-09-23 19:56:46 +0000 | [diff] [blame] | 1589 | while (idx > linebufsz-2) |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1590 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1591 | linebuf[idx++] = (char)ch; |
| 1592 | if ((char)ch == '\n') |
| 1593 | break; |
| 1594 | } |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1595 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1596 | if (idx == 0) |
| 1597 | return NULL; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1598 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1599 | linebuf[idx] = 0; |
| 1600 | return linebuf; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1601 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1602 | |
Eric Andersen | 944be90 | 2000-09-25 16:58:39 +0000 | [diff] [blame] | 1603 | #if defined BB_CAT |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1604 | extern void print_file(FILE *file) |
| 1605 | { |
| 1606 | int c; |
| 1607 | |
| 1608 | while ((c = getc(file)) != EOF) |
| 1609 | putc(c, stdout); |
| 1610 | fclose(file); |
| 1611 | fflush(stdout); |
| 1612 | } |
| 1613 | |
| 1614 | extern int print_file_by_name(char *filename) |
| 1615 | { |
| 1616 | FILE *file; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1617 | if ((file = wfopen(filename, "r")) == NULL) |
| 1618 | return FALSE; |
| 1619 | print_file(file); |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1620 | return TRUE; |
| 1621 | } |
Matt Kraai | e7c1af1 | 2000-09-27 03:01:40 +0000 | [diff] [blame] | 1622 | #endif /* BB_CAT */ |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1623 | |
Matt Kraai | cabafae | 2000-11-30 04:55:40 +0000 | [diff] [blame] | 1624 | #if defined BB_ECHO || defined BB_SH || defined BB_TR |
Eric Andersen | f7cf2f7 | 2000-07-05 17:26:35 +0000 | [diff] [blame] | 1625 | char process_escape_sequence(char **ptr) |
| 1626 | { |
| 1627 | char c; |
| 1628 | |
| 1629 | switch (c = *(*ptr)++) { |
| 1630 | case 'a': |
| 1631 | c = '\a'; |
| 1632 | break; |
| 1633 | case 'b': |
| 1634 | c = '\b'; |
| 1635 | break; |
| 1636 | case 'f': |
| 1637 | c = '\f'; |
| 1638 | break; |
| 1639 | case 'n': |
| 1640 | c = '\n'; |
| 1641 | break; |
Eric Andersen | 9bd71e8 | 2000-11-29 23:07:57 +0000 | [diff] [blame] | 1642 | case 'r': |
| 1643 | c = '\r'; |
| 1644 | break; |
Eric Andersen | f7cf2f7 | 2000-07-05 17:26:35 +0000 | [diff] [blame] | 1645 | case 't': |
| 1646 | c = '\t'; |
| 1647 | break; |
| 1648 | case 'v': |
| 1649 | c = '\v'; |
| 1650 | break; |
| 1651 | case '\\': |
| 1652 | c = '\\'; |
| 1653 | break; |
| 1654 | case '0': case '1': case '2': case '3': |
| 1655 | case '4': case '5': case '6': case '7': |
| 1656 | c -= '0'; |
| 1657 | if ('0' <= **ptr && **ptr <= '7') { |
| 1658 | c = c * 8 + (*(*ptr)++ - '0'); |
| 1659 | if ('0' <= **ptr && **ptr <= '7') |
| 1660 | c = c * 8 + (*(*ptr)++ - '0'); |
| 1661 | } |
| 1662 | break; |
| 1663 | default: |
| 1664 | (*ptr)--; |
| 1665 | c = '\\'; |
| 1666 | break; |
| 1667 | } |
| 1668 | return c; |
| 1669 | } |
| 1670 | #endif |
| 1671 | |
Eric Andersen | ba1ff9b | 2000-10-01 17:01:36 +0000 | [diff] [blame] | 1672 | #if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || defined BB_FEATURE_USE_PROCFS |
Eric Andersen | 0b4551f | 2000-07-10 16:44:03 +0000 | [diff] [blame] | 1673 | char *get_last_path_component(char *path) |
| 1674 | { |
| 1675 | char *s=path+strlen(path)-1; |
| 1676 | |
| 1677 | /* strip trailing slashes */ |
Eric Andersen | 69a6b2d | 2000-12-12 23:13:54 +0000 | [diff] [blame] | 1678 | while (s != path && *s == '/') { |
Eric Andersen | 0b4551f | 2000-07-10 16:44:03 +0000 | [diff] [blame] | 1679 | *s-- = '\0'; |
| 1680 | } |
| 1681 | |
| 1682 | /* find last component */ |
| 1683 | s = strrchr(path, '/'); |
Eric Andersen | 69a6b2d | 2000-12-12 23:13:54 +0000 | [diff] [blame] | 1684 | if (s == NULL || s[1] == '\0') |
| 1685 | return path; |
| 1686 | else |
| 1687 | return s+1; |
Eric Andersen | 0b4551f | 2000-07-10 16:44:03 +0000 | [diff] [blame] | 1688 | } |
| 1689 | #endif |
| 1690 | |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1691 | #if defined BB_GREP || defined BB_SED |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 1692 | void xregcomp(regex_t *preg, const char *regex, int cflags) |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1693 | { |
| 1694 | int ret; |
| 1695 | if ((ret = regcomp(preg, regex, cflags)) != 0) { |
| 1696 | int errmsgsz = regerror(ret, preg, NULL, 0); |
| 1697 | char *errmsg = xmalloc(errmsgsz); |
| 1698 | regerror(ret, preg, errmsg, errmsgsz); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1699 | error_msg_and_die("xregcomp: %s\n", errmsg); |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1700 | } |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1701 | } |
| 1702 | #endif |
| 1703 | |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 1704 | #if defined BB_CAT || defined BB_HEAD || defined BB_WC |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1705 | FILE *wfopen(const char *path, const char *mode) |
| 1706 | { |
| 1707 | FILE *fp; |
| 1708 | if ((fp = fopen(path, mode)) == NULL) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 1709 | perror_msg("%s", path); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1710 | errno = 0; |
| 1711 | } |
| 1712 | return fp; |
| 1713 | } |
| 1714 | #endif |
| 1715 | |
Eric Andersen | e790ddb | 2000-11-17 06:50:42 +0000 | [diff] [blame] | 1716 | #if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \ |
| 1717 | || defined BB_SED || defined BB_SH || defined BB_UNIQ \ |
| 1718 | || defined BB_WC || defined BB_CMP |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 1719 | FILE *xfopen(const char *path, const char *mode) |
| 1720 | { |
| 1721 | FILE *fp; |
| 1722 | if ((fp = fopen(path, mode)) == NULL) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 1723 | perror_msg_and_die("%s", path); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 1724 | return fp; |
| 1725 | } |
| 1726 | #endif |
| 1727 | |
Eric Andersen | e5aef92 | 2000-11-17 18:25:26 +0000 | [diff] [blame] | 1728 | int applet_name_compare(const void *x, const void *y) |
| 1729 | { |
| 1730 | const struct BB_applet *applet1 = x; |
| 1731 | const struct BB_applet *applet2 = y; |
| 1732 | |
| 1733 | return strcmp(applet1->name, applet2->name); |
| 1734 | } |
| 1735 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 1736 | #if defined BB_DD || defined BB_TAIL |
| 1737 | unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes) |
| 1738 | { |
| 1739 | struct suffix_mult *sm; |
| 1740 | unsigned long int ret; |
| 1741 | int len; |
| 1742 | char *end; |
| 1743 | |
| 1744 | ret = strtoul(numstr, &end, 10); |
| 1745 | if (numstr == end) |
| 1746 | error_msg_and_die("invalid number `%s'\n", numstr); |
| 1747 | while (end[0] != '\0') { |
| 1748 | for (sm = suffixes; sm->suffix != NULL; sm++) { |
| 1749 | len = strlen(sm->suffix); |
| 1750 | if (strncmp(sm->suffix, end, len) == 0) { |
| 1751 | ret *= sm->mult; |
| 1752 | end += len; |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
| 1756 | if (sm->suffix == NULL) |
| 1757 | error_msg_and_die("invalid number `%s'\n", numstr); |
| 1758 | } |
| 1759 | return ret; |
| 1760 | } |
| 1761 | #endif |
| 1762 | |
| 1763 | #if defined BB_DD || defined BB_NC |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 1764 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 1765 | { |
| 1766 | ssize_t n; |
| 1767 | |
| 1768 | do { |
| 1769 | n = read(fd, buf, count); |
| 1770 | } while (n < 0 && errno == EINTR); |
| 1771 | |
| 1772 | return n; |
| 1773 | } |
| 1774 | #endif |
| 1775 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1776 | /* END CODE */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 1777 | /* |
| 1778 | Local Variables: |
| 1779 | c-file-style: "linux" |
| 1780 | c-basic-offset: 4 |
| 1781 | tab-width: 4 |
| 1782 | End: |
| 1783 | */ |