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) { |
| 156 | perror("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) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 344 | perror(srcName); |
| 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) { |
| 370 | perror(destName); |
| 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) { |
| 381 | perror(srcName); |
| 382 | return FALSE; |
| 383 | } |
| 384 | link_val[link_size] = '\0'; |
| 385 | status = symlink(link_val, destName); |
| 386 | if (status < 0) { |
| 387 | perror(destName); |
| 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) { |
| 400 | perror(destName); |
| 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) { |
| 407 | perror(destName); |
| 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) { |
| 414 | perror(srcName); |
| 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) { |
| 421 | perror(destName); |
| 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 */ |
| 437 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) { |
| 438 | perror(destName); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 439 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 440 | } |
| 441 | if (chmod(destName, srcStatBuf.st_mode) < 0) { |
| 442 | perror(destName); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 443 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 444 | } |
| 445 | times.actime = srcStatBuf.st_atime; |
| 446 | times.modtime = srcStatBuf.st_mtime; |
| 447 | if (utime(destName, ×) < 0) { |
| 448 | perror(destName); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 449 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 453 | return TRUE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 454 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 455 | error_exit: |
| 456 | perror(destName); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 457 | close(rfd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | close(wfd); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 459 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 460 | return FALSE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 461 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 462 | #endif /* BB_CP_MV */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | |
Glenn L McGrath | ac19b7e | 2000-09-11 05:06:13 +0000 | [diff] [blame] | 466 | #if defined BB_TAR || defined BB_LS ||defined BB_AR |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 467 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 468 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 469 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 470 | |
| 471 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ |
| 472 | static const mode_t SBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 473 | 0, 0, S_ISUID, |
| 474 | 0, 0, S_ISGID, |
| 475 | 0, 0, S_ISVTX |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | /* The 9 mode bits to test */ |
| 479 | static const mode_t MBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 480 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 481 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 482 | S_IROTH, S_IWOTH, S_IXOTH |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
Mark Whitley | 26a3adb | 2000-12-05 20:10:27 +0000 | [diff] [blame] | 485 | static const char MODE1[] = "rwxrwxrwx"; |
| 486 | static const char MODE0[] = "---------"; |
| 487 | static const char SMODE1[] = "..s..s..t"; |
| 488 | static const char SMODE0[] = "..S..S..T"; |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 489 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 490 | /* |
| 491 | * Return the standard ls-like mode string from a file mode. |
| 492 | * This is static and so is overwritten on each call. |
| 493 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 494 | const char *mode_string(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 495 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 496 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 497 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | int i; |
| 499 | |
| 500 | buf[0] = TYPECHAR(mode); |
| 501 | for (i = 0; i < 9; i++) { |
| 502 | if (mode & SBIT[i]) |
| 503 | buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; |
| 504 | else |
| 505 | buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; |
| 506 | } |
| 507 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 508 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 509 | #endif /* BB_TAR || BB_LS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 510 | |
| 511 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 512 | #if defined BB_TAR || defined BB_AR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 513 | /* |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 514 | * Return the standard ls-like time string from a time_t |
| 515 | * This is static and so is overwritten on each call. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 516 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 517 | const char *time_string(time_t timeVal) |
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 | time_t now; |
| 520 | char *str; |
| 521 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 522 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 523 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 524 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 525 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 526 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 527 | strcpy(buf, &str[4]); |
| 528 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 529 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 530 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 531 | strcpy(&buf[7], &str[20]); |
| 532 | buf[11] = '\0'; |
| 533 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 534 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 535 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 536 | } |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 537 | #endif /* BB_TAR || BB_AR */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 538 | |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 539 | #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] | 540 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 541 | * Write all of the supplied buffer out to a file. |
| 542 | * This does multiple writes as necessary. |
| 543 | * Returns the amount written, or -1 on an error. |
| 544 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 545 | int full_write(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 546 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 547 | int cc; |
| 548 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 549 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 550 | total = 0; |
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 | while (len > 0) { |
| 553 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 554 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 555 | if (cc < 0) |
| 556 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 557 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 558 | buf += cc; |
| 559 | total += cc; |
| 560 | len -= cc; |
| 561 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 562 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 563 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 564 | } |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 565 | #endif /* BB_TAR || BB_CP_MV || BB_AR */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 566 | |
| 567 | |
Eric Andersen | a2d61e7 | 2000-12-12 16:55:56 +0000 | [diff] [blame] | 568 | #if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV || defined BB_DD |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 569 | /* |
| 570 | * Read all of the supplied buffer from a file. |
| 571 | * This does multiple reads as necessary. |
| 572 | * Returns the amount read, or -1 on an error. |
| 573 | * A short read is returned on an end of file. |
| 574 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 575 | int full_read(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 576 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 577 | int cc; |
| 578 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 579 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 580 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 581 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 582 | while (len > 0) { |
| 583 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 584 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 585 | if (cc < 0) |
| 586 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 587 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 588 | if (cc == 0) |
| 589 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 590 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 591 | buf += cc; |
| 592 | total += cc; |
| 593 | len -= cc; |
| 594 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 595 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 596 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 597 | } |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 598 | #endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 599 | |
| 600 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 601 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ |
| 602 | || defined (BB_CP_MV) \ |
| 603 | || defined (BB_FIND) \ |
| 604 | || defined (BB_INSMOD) \ |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 605 | || defined (BB_LS) \ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 606 | || defined (BB_RM) \ |
| 607 | || defined (BB_TAR) |
| 608 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 609 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 610 | * Walk down all the directories under the specified |
| 611 | * location, and do something (something specified |
| 612 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 613 | * |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 614 | * Unfortunately, while nftw(3) could replace this and reduce |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 615 | * 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] | 616 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 617 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 618 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 619 | int recursive_action(const char *fileName, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 620 | int recurse, int followLinks, int depthFirst, |
| 621 | int (*fileAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 622 | struct stat * statbuf, |
| 623 | void* userData), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 624 | int (*dirAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 625 | struct stat * statbuf, |
| 626 | void* userData), |
| 627 | void* userData) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 628 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 629 | int status; |
| 630 | struct stat statbuf; |
| 631 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 632 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 633 | if (followLinks == TRUE) |
| 634 | status = stat(fileName, &statbuf); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 635 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 636 | status = lstat(fileName, &statbuf); |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 637 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 638 | if (status < 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 639 | #ifdef BB_DEBUG_PRINT_SCAFFOLD |
| 640 | fprintf(stderr, |
| 641 | "status=%d followLinks=%d TRUE=%d\n", |
| 642 | status, followLinks, TRUE); |
| 643 | #endif |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 644 | perror_msg("%s", fileName); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 645 | return FALSE; |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 646 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 647 | |
| 648 | if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) { |
| 649 | if (fileAction == NULL) |
| 650 | return TRUE; |
| 651 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 652 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | if (recurse == FALSE) { |
| 656 | if (S_ISDIR(statbuf.st_mode)) { |
| 657 | if (dirAction != NULL) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 658 | return (dirAction(fileName, &statbuf, userData)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 659 | else |
| 660 | return TRUE; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if (S_ISDIR(statbuf.st_mode)) { |
| 665 | DIR *dir; |
| 666 | |
| 667 | dir = opendir(fileName); |
| 668 | if (!dir) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 669 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 670 | return FALSE; |
| 671 | } |
| 672 | if (dirAction != NULL && depthFirst == FALSE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 673 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 674 | if (status == FALSE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 675 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 676 | return FALSE; |
| 677 | } |
| 678 | } |
| 679 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 680 | char nextFile[BUFSIZ + 1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 681 | |
| 682 | if ((strcmp(next->d_name, "..") == 0) |
| 683 | || (strcmp(next->d_name, ".") == 0)) { |
| 684 | continue; |
| 685 | } |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 686 | if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 687 | error_msg(name_too_long); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 688 | return FALSE; |
| 689 | } |
Erik Andersen | c366050 | 2000-04-18 21:18:57 +0000 | [diff] [blame] | 690 | memset(nextFile, 0, sizeof(nextFile)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 691 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 692 | status = |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 693 | recursive_action(nextFile, TRUE, followLinks, depthFirst, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 694 | fileAction, dirAction, userData); |
Matt Kraai | 0f8f7b8 | 2000-08-01 18:16:56 +0000 | [diff] [blame] | 695 | if (status == FALSE) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | closedir(dir); |
| 697 | return FALSE; |
| 698 | } |
| 699 | } |
| 700 | status = closedir(dir); |
| 701 | if (status < 0) { |
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 | if (dirAction != NULL && depthFirst == TRUE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 706 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 707 | if (status == FALSE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 708 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 709 | return FALSE; |
| 710 | } |
| 711 | } |
| 712 | } else { |
| 713 | if (fileAction == NULL) |
| 714 | return TRUE; |
| 715 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 716 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 717 | } |
| 718 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 721 | #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] | 722 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 723 | |
| 724 | |
Glenn L McGrath | e699b9f | 2000-12-15 06:50:54 +0000 | [diff] [blame] | 725 | #if defined (BB_TAR) || defined (BB_MKDIR) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 726 | /* |
| 727 | * Attempt to create the directories along the specified path, except for |
| 728 | * the final component. The mode is given for the final directory only, |
| 729 | * while all previous ones get default protections. Errors are not reported |
| 730 | * here, as failures to restore files can be reported later. |
| 731 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 732 | extern int create_path(const char *name, int mode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 733 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 734 | char *cp; |
| 735 | char *cpOld; |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 736 | char buf[BUFSIZ + 1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 737 | int retVal = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 738 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 739 | strcpy(buf, name); |
| 740 | for (cp = buf; *cp == '/'; cp++); |
| 741 | cp = strchr(cp, '/'); |
| 742 | while (cp) { |
| 743 | cpOld = cp; |
| 744 | cp = strchr(cp + 1, '/'); |
| 745 | *cpOld = '\0'; |
| 746 | retVal = mkdir(buf, cp ? 0777 : mode); |
| 747 | if (retVal != 0 && errno != EEXIST) { |
| 748 | perror(buf); |
| 749 | return FALSE; |
| 750 | } |
| 751 | *cpOld = '/'; |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 752 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 753 | return TRUE; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 754 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 755 | #endif /* BB_TAR || BB_MKDIR */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 756 | |
| 757 | |
| 758 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 759 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \ |
Glenn L McGrath | ac19b7e | 2000-09-11 05:06:13 +0000 | [diff] [blame] | 760 | || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 761 | /* [ugoa]{+|-|=}[rwxst] */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 762 | |
| 763 | |
| 764 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 765 | extern int parse_mode(const char *s, mode_t * theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 766 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 767 | mode_t andMode = |
| 768 | |
| 769 | S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 770 | mode_t orMode = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 771 | mode_t mode = 0; |
| 772 | mode_t groups = 0; |
| 773 | char type; |
| 774 | char c; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 775 | |
Eric Andersen | 4e61db2 | 2000-09-01 00:06:17 +0000 | [diff] [blame] | 776 | if (s==NULL) |
| 777 | return (FALSE); |
| 778 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 779 | do { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 780 | for (;;) { |
| 781 | switch (c = *s++) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 782 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 783 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 784 | case 'u': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 785 | groups |= S_ISUID | S_IRWXU; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 786 | continue; |
| 787 | case 'g': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 788 | groups |= S_ISGID | S_IRWXG; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 789 | continue; |
| 790 | case 'o': |
| 791 | groups |= S_IRWXO; |
| 792 | continue; |
| 793 | case 'a': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 794 | groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 795 | continue; |
| 796 | case '+': |
| 797 | case '=': |
| 798 | case '-': |
| 799 | type = c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 800 | if (groups == 0) /* The default is "all" */ |
| 801 | groups |= |
| 802 | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 803 | break; |
| 804 | default: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 805 | if (isdigit(c) && c >= '0' && c <= '7' && |
| 806 | mode == 0 && groups == 0) { |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 807 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 808 | return (TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 809 | } else |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 810 | return (FALSE); |
| 811 | } |
| 812 | break; |
| 813 | } |
| 814 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 815 | while ((c = *s++) != '\0') { |
| 816 | switch (c) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 817 | case ',': |
| 818 | break; |
| 819 | case 'r': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 820 | mode |= S_IRUSR | S_IRGRP | S_IROTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 821 | continue; |
| 822 | case 'w': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 823 | mode |= S_IWUSR | S_IWGRP | S_IWOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 824 | continue; |
| 825 | case 'x': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 826 | mode |= S_IXUSR | S_IXGRP | S_IXOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 827 | continue; |
| 828 | case 's': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 829 | mode |= S_IXGRP | S_ISUID | S_ISGID; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 830 | continue; |
| 831 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 832 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 833 | continue; |
| 834 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 835 | *theMode &= andMode; |
| 836 | *theMode |= orMode; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 837 | return (TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 838 | } |
| 839 | break; |
| 840 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 841 | switch (type) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 842 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 843 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 844 | /* fall through */ |
| 845 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 846 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 847 | break; |
| 848 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 849 | andMode &= ~(mode & groups); |
| 850 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 851 | break; |
| 852 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 853 | } while (c == ','); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 854 | *theMode &= andMode; |
| 855 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 856 | return (TRUE); |
| 857 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 858 | |
| 859 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 860 | #endif |
| 861 | /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 862 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 863 | |
| 864 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 865 | |
| 866 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 867 | #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \ |
| 868 | || defined BB_TAR || defined BB_ID || defined BB_LOGGER \ |
Eric Andersen | a2d61e7 | 2000-12-12 16:55:56 +0000 | [diff] [blame] | 869 | || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 870 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 871 | /* This parses entries in /etc/passwd and /etc/group. This is desirable |
| 872 | * for BusyBox, since we want to avoid using the glibc NSS stuff, which |
| 873 | * increases target size and is often not needed or wanted for embedded |
| 874 | * systems. |
| 875 | * |
| 876 | * /etc/passwd entries look like this: |
| 877 | * root:x:0:0:root:/root:/bin/bash |
| 878 | * and /etc/group entries look like this: |
| 879 | * root:x:0: |
| 880 | * |
| 881 | * This uses buf as storage to hold things. |
| 882 | * |
| 883 | */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 884 | 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] | 885 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 886 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 887 | char *rname, *start, *end, buf[128]; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 888 | long rid; |
| 889 | long rgid = 0; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 890 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 891 | file = fopen(filename, "r"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 892 | if (file == NULL) { |
Eric Andersen | 3199bda | 2000-06-02 17:38:18 +0000 | [diff] [blame] | 893 | /* Do not complain. It is ok for /etc/passwd and |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 894 | * friends to be missing... */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 895 | return (-1); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 896 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 897 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 898 | while (fgets(buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 899 | if (buf[0] == '#') |
| 900 | continue; |
| 901 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 902 | /* username/group name */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 903 | start = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 904 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 905 | if (end == NULL) |
| 906 | continue; |
| 907 | *end = '\0'; |
| 908 | rname = start; |
| 909 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 910 | /* password */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 911 | start = end + 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 912 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 913 | if (end == NULL) |
| 914 | continue; |
| 915 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 916 | /* uid in passwd, gid in group */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 917 | start = end + 1; |
Erik Andersen | 5b46d79 | 2000-05-17 05:24:24 +0000 | [diff] [blame] | 918 | rid = (unsigned long) strtol(start, &end, 10); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 919 | if (end == start) |
| 920 | continue; |
| 921 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 922 | /* gid in passwd */ |
| 923 | start = end + 1; |
Erik Andersen | 5b46d79 | 2000-05-17 05:24:24 +0000 | [diff] [blame] | 924 | rgid = (unsigned long) strtol(start, &end, 10); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 925 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 926 | if (name) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 927 | if (0 == strcmp(rname, name)) { |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 928 | if (gid) *gid = rgid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 929 | fclose(file); |
| 930 | return (rid); |
| 931 | } |
| 932 | } |
| 933 | if (id != -1 && id == rid) { |
| 934 | strncpy(name, rname, 8); |
Eric Andersen | 2d6282b | 2000-12-11 20:33:46 +0000 | [diff] [blame] | 935 | name[8]='\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 936 | if (gid) *gid = rgid; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 937 | fclose(file); |
| 938 | return (TRUE); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 941 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 942 | return (-1); |
| 943 | } |
| 944 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 945 | /* returns a uid given a username */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 946 | long my_getpwnam(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/passwd", 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 | /* returns a gid given a group name */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 952 | long my_getgrnam(char *name) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 953 | { |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 954 | return my_getid("/etc/group", name, -1, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 957 | /* gets a username given a uid */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 958 | void my_getpwuid(char *name, long uid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 959 | { |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 960 | name[0] = '\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 961 | my_getid("/etc/passwd", name, uid, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 964 | /* gets a groupname given a gid */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 965 | void my_getgrgid(char *group, long gid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 966 | { |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 967 | group[0] = '\0'; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 968 | my_getid("/etc/group", group, gid, NULL); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 971 | /* gets a gid given a user name */ |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 972 | long my_getpwnamegid(char *name) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 973 | { |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 974 | long gid; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 975 | my_getid("/etc/passwd", name, -1, &gid); |
| 976 | return gid; |
| 977 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 978 | |
Pavel Roskin | 21ddb38 | 2000-07-17 19:31:54 +0000 | [diff] [blame] | 979 | #endif |
| 980 | /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \ |
| 981 | || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 982 | |
| 983 | |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 984 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 985 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 986 | /* From <linux/kd.h> */ |
| 987 | #define KDGKBTYPE 0x4B33 /* get keyboard type */ |
| 988 | #define KB_84 0x01 |
| 989 | #define KB_101 0x02 /* this is what we always answer */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 990 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 991 | int is_a_console(int fd) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 992 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 993 | char arg; |
| 994 | |
| 995 | arg = 0; |
| 996 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 997 | && ((arg == KB_101) || (arg == KB_84))); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1000 | static int open_a_console(char *fnam) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1001 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1002 | int fd; |
| 1003 | |
| 1004 | /* try read-only */ |
| 1005 | fd = open(fnam, O_RDWR); |
| 1006 | |
| 1007 | /* if failed, try read-only */ |
| 1008 | if (fd < 0 && errno == EACCES) |
| 1009 | fd = open(fnam, O_RDONLY); |
| 1010 | |
| 1011 | /* if failed, try write-only */ |
| 1012 | if (fd < 0 && errno == EACCES) |
| 1013 | fd = open(fnam, O_WRONLY); |
| 1014 | |
| 1015 | /* if failed, fail */ |
| 1016 | if (fd < 0) |
| 1017 | return -1; |
| 1018 | |
| 1019 | /* if not a console, fail */ |
| 1020 | if (!is_a_console(fd)) { |
| 1021 | close(fd); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | |
| 1025 | /* success */ |
| 1026 | return fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Get an fd for use with kbd/console ioctls. |
| 1031 | * We try several things because opening /dev/console will fail |
| 1032 | * if someone else used X (which does a chown on /dev/console). |
| 1033 | * |
| 1034 | * if tty_name is non-NULL, try this one instead. |
| 1035 | */ |
| 1036 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1037 | int get_console_fd(char *tty_name) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1038 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1039 | int fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1040 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1041 | if (tty_name) { |
| 1042 | if (-1 == (fd = open_a_console(tty_name))) |
| 1043 | return -1; |
| 1044 | else |
| 1045 | return fd; |
| 1046 | } |
| 1047 | |
| 1048 | fd = open_a_console("/dev/tty"); |
| 1049 | if (fd >= 0) |
| 1050 | return fd; |
| 1051 | |
| 1052 | fd = open_a_console("/dev/tty0"); |
| 1053 | if (fd >= 0) |
| 1054 | return fd; |
| 1055 | |
| 1056 | fd = open_a_console("/dev/console"); |
| 1057 | if (fd >= 0) |
| 1058 | return fd; |
| 1059 | |
| 1060 | for (fd = 0; fd < 3; fd++) |
| 1061 | if (is_a_console(fd)) |
| 1062 | return fd; |
| 1063 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1064 | error_msg("Couldnt get a file descriptor referring to the console\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1065 | return -1; /* total failure */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 1069 | #endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 1070 | |
| 1071 | |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1072 | #if defined BB_FIND || defined BB_INSMOD |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | * Routine to see if a text string is matched by a wildcard pattern. |
| 1075 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 1076 | * or if the pattern is invalid. |
| 1077 | * * matches zero or more characters |
| 1078 | * ? matches a single character |
| 1079 | * [abc] matches 'a', 'b' or 'c' |
| 1080 | * \c quotes character c |
| 1081 | * Adapted from code written by Ingo Wilken, and |
| 1082 | * then taken from sash, Copyright (c) 1999 by David I. Bell |
| 1083 | * Permission is granted to use, distribute, or modify this source, |
| 1084 | * provided that this copyright notice remains intact. |
| 1085 | * Permission to distribute this code under the GPL has been granted. |
| 1086 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1087 | extern int check_wildcard_match(const char *text, const char *pattern) |
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 | const char *retryPat; |
| 1090 | const char *retryText; |
| 1091 | int ch; |
| 1092 | int found; |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1093 | int len; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1094 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1095 | retryPat = NULL; |
| 1096 | retryText = NULL; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1097 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1098 | while (*text || *pattern) { |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1099 | ch = *pattern++; |
| 1100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1101 | switch (ch) { |
| 1102 | case '*': |
| 1103 | retryPat = pattern; |
| 1104 | retryText = text; |
| 1105 | break; |
| 1106 | |
| 1107 | case '[': |
| 1108 | found = FALSE; |
| 1109 | |
| 1110 | while ((ch = *pattern++) != ']') { |
| 1111 | if (ch == '\\') |
| 1112 | ch = *pattern++; |
| 1113 | |
| 1114 | if (ch == '\0') |
| 1115 | return FALSE; |
| 1116 | |
| 1117 | if (*text == ch) |
| 1118 | found = TRUE; |
| 1119 | } |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1120 | len=strlen(text); |
| 1121 | if (found == FALSE && len!=0) { |
| 1122 | return FALSE; |
| 1123 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1124 | if (found == TRUE) { |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 1125 | if (strlen(pattern)==0 && len==1) { |
| 1126 | return TRUE; |
| 1127 | } |
| 1128 | if (len!=0) { |
| 1129 | text++; |
| 1130 | continue; |
Erik Andersen | c366050 | 2000-04-18 21:18:57 +0000 | [diff] [blame] | 1131 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /* fall into next case */ |
| 1135 | |
| 1136 | case '?': |
| 1137 | if (*text++ == '\0') |
| 1138 | return FALSE; |
| 1139 | |
| 1140 | break; |
| 1141 | |
| 1142 | case '\\': |
| 1143 | ch = *pattern++; |
| 1144 | |
| 1145 | if (ch == '\0') |
| 1146 | return FALSE; |
| 1147 | |
| 1148 | /* fall into next case */ |
| 1149 | |
| 1150 | default: |
| 1151 | if (*text == ch) { |
| 1152 | if (*text) |
| 1153 | text++; |
| 1154 | break; |
| 1155 | } |
| 1156 | |
| 1157 | if (*text) { |
| 1158 | pattern = retryPat; |
| 1159 | text = ++retryText; |
| 1160 | break; |
| 1161 | } |
| 1162 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1163 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1166 | if (pattern == NULL) |
| 1167 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1170 | return TRUE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1171 | } |
Eric Andersen | 9f16d61 | 2000-06-12 23:11:16 +0000 | [diff] [blame] | 1172 | #endif /* BB_FIND || BB_INSMOD */ |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1173 | |
| 1174 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1175 | |
| 1176 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1177 | #if defined BB_DF || defined BB_MTAB |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1178 | /* |
| 1179 | * Given a block device, find the mount table entry if that block device |
| 1180 | * is mounted. |
| 1181 | * |
| 1182 | * Given any other file (or directory), find the mount table entry for its |
| 1183 | * filesystem. |
| 1184 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1185 | extern struct mntent *find_mount_point(const char *name, const char *table) |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1186 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1187 | struct stat s; |
| 1188 | dev_t mountDevice; |
| 1189 | FILE *mountTable; |
| 1190 | struct mntent *mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1191 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1192 | if (stat(name, &s) != 0) |
| 1193 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1194 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1195 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 1196 | mountDevice = s.st_rdev; |
| 1197 | else |
| 1198 | mountDevice = s.st_dev; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1199 | |
| 1200 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1201 | if ((mountTable = setmntent(table, "r")) == 0) |
| 1202 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1203 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1204 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 1205 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 1206 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 1207 | break; |
| 1208 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 1209 | break; |
| 1210 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 1211 | break; |
| 1212 | } |
| 1213 | endmntent(mountTable); |
| 1214 | return mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1215 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1216 | #endif /* BB_DF || BB_MTAB */ |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1217 | |
| 1218 | |
| 1219 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1220 | #if defined BB_DD || defined BB_TAIL |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | * Read a number with a possible multiplier. |
| 1223 | * Returns -1 if the number format is illegal. |
| 1224 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1225 | extern long atoi_w_units(const char *cp) |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1226 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1227 | long value; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1228 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1229 | if (!is_decimal(*cp)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1230 | return -1; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1231 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1232 | value = 0; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1233 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1234 | while (is_decimal(*cp)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1235 | value = value * 10 + *cp++ - '0'; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1236 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1237 | switch (*cp++) { |
| 1238 | case 'M': |
| 1239 | case 'm': /* `tail' uses it traditionally */ |
| 1240 | value *= 1048576; |
| 1241 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1242 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1243 | case 'k': |
| 1244 | value *= 1024; |
| 1245 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1246 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1247 | case 'b': |
| 1248 | value *= 512; |
| 1249 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1250 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1251 | case 'w': |
| 1252 | value *= 2; |
| 1253 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1254 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1255 | case '\0': |
| 1256 | return value; |
| 1257 | |
| 1258 | default: |
| 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | if (*cp) |
| 1263 | return -1; |
| 1264 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1265 | return value; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1266 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1267 | #endif /* BB_DD || BB_TAIL */ |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1268 | |
Eric Andersen | 6805d5d | 1999-12-09 22:39:55 +0000 | [diff] [blame] | 1269 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 1270 | #if defined BB_INIT || defined BB_SYSLOGD |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1271 | /* try to open up the specified device */ |
| 1272 | extern int device_open(char *device, int mode) |
| 1273 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1274 | int m, f, fd = -1; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1275 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1276 | m = mode | O_NONBLOCK; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1277 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1278 | /* Retry up to 5 times */ |
| 1279 | for (f = 0; f < 5; f++) |
| 1280 | if ((fd = open(device, m, 0600)) >= 0) |
| 1281 | break; |
| 1282 | if (fd < 0) |
| 1283 | return fd; |
| 1284 | /* Reset original flags. */ |
| 1285 | if (m != mode) |
| 1286 | fcntl(fd, F_SETFL, mode); |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1287 | return fd; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1288 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1289 | #endif /* BB_INIT BB_SYSLOGD */ |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1290 | |
| 1291 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1292 | #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] | 1293 | #ifdef BB_FEATURE_USE_DEVPS_PATCH |
Eric Andersen | c674d70 | 2000-07-10 22:57:14 +0000 | [diff] [blame] | 1294 | #include <linux/devps.h> /* For Erik's nifty devps device driver */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1295 | #endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1296 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1297 | #if defined BB_FEATURE_USE_DEVPS_PATCH |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1298 | /* find_pid_by_name() |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1299 | * |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1300 | * This finds the pid of the specified process, |
| 1301 | * by using the /dev/ps device driver. |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1302 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1303 | * Returns a list of all matching PIDs |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1304 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1305 | extern pid_t* find_pid_by_name( char* pidName) |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1306 | { |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1307 | int fd, i, j; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1308 | char device[] = "/dev/ps"; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1309 | pid_t num_pids; |
| 1310 | pid_t* pid_array = NULL; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1311 | pid_t* pidList=NULL; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1312 | |
| 1313 | /* open device */ |
| 1314 | fd = open(device, O_RDONLY); |
| 1315 | if (fd < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1316 | error_msg_and_die( "open failed for `%s': %s\n", device, strerror (errno)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* Find out how many processes there are */ |
| 1319 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1320 | error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1321 | |
| 1322 | /* Allocate some memory -- grab a few extras just in case |
| 1323 | * some new processes start up while we wait. The kernel will |
| 1324 | * just ignore any extras if we give it too many, and will trunc. |
| 1325 | * the list if we give it too few. */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1326 | pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1327 | pid_array[0] = num_pids+10; |
| 1328 | |
| 1329 | /* Now grab the pid list */ |
| 1330 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1331 | error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1332 | |
| 1333 | /* Now search for a match */ |
Eric Andersen | f86aee7 | 2000-07-06 17:20:46 +0000 | [diff] [blame] | 1334 | for (i=1, j=0; i<pid_array[0] ; i++) { |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1335 | char* p; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1336 | struct pid_info info; |
| 1337 | |
| 1338 | info.pid = pid_array[i]; |
| 1339 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1340 | error_msg_and_die( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1341 | |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1342 | /* Make sure we only match on the process name */ |
| 1343 | p=info.command_line+1; |
| 1344 | while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { |
| 1345 | (p)++; |
| 1346 | } |
| 1347 | if (isspace(*(p))) |
| 1348 | *p='\0'; |
| 1349 | |
Erik Andersen | b9167cb | 2000-04-18 22:41:30 +0000 | [diff] [blame] | 1350 | if ((strstr(info.command_line, pidName) != NULL) |
| 1351 | && (strlen(pidName) == strlen(info.command_line))) { |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1352 | pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1353 | pidList[j++]=info.pid; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1356 | if (pidList) |
| 1357 | pidList[j]=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1358 | |
| 1359 | /* Free memory */ |
| 1360 | free( pid_array); |
| 1361 | |
| 1362 | /* close device */ |
| 1363 | if (close (fd) != 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1364 | error_msg_and_die( "close failed for `%s': %s\n",device, strerror (errno)); |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1365 | |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1366 | return pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1367 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1368 | #else /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1369 | #if ! defined BB_FEATURE_USE_PROCFS |
| 1370 | #error Sorry, I depend on the /proc filesystem right now. |
| 1371 | #endif |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1372 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1373 | /* find_pid_by_name() |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1374 | * |
| 1375 | * This finds the pid of the specified process. |
| 1376 | * Currently, it's implemented by rummaging through |
| 1377 | * the proc filesystem. |
| 1378 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1379 | * Returns a list of all matching PIDs |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1380 | */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1381 | extern pid_t* find_pid_by_name( char* pidName) |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1382 | { |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1383 | DIR *dir; |
| 1384 | struct dirent *next; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1385 | pid_t* pidList=NULL; |
| 1386 | int i=0; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1387 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1388 | dir = opendir("/proc"); |
| 1389 | if (!dir) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1390 | error_msg_and_die( "Cannot open /proc: %s\n", strerror (errno)); |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1391 | |
| 1392 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1393 | FILE *status; |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1394 | char filename[256]; |
| 1395 | char buffer[256]; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1396 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1397 | /* If it isn't a number, we don't want it */ |
| 1398 | if (!isdigit(*next->d_name)) |
| 1399 | continue; |
| 1400 | |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1401 | sprintf(filename, "/proc/%s/cmdline", next->d_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1402 | status = fopen(filename, "r"); |
| 1403 | if (!status) { |
| 1404 | continue; |
| 1405 | } |
| 1406 | fgets(buffer, 256, status); |
| 1407 | fclose(status); |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1408 | |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1409 | if (strstr(get_last_path_component(buffer), pidName) != NULL) { |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1410 | pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1411 | pidList[i++]=strtol(next->d_name, NULL, 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1412 | } |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1413 | } |
Eric Andersen | 31e6829 | 2000-09-14 21:12:20 +0000 | [diff] [blame] | 1414 | |
Erik Andersen | 0a64de9 | 2000-04-18 20:00:03 +0000 | [diff] [blame] | 1415 | if (pidList) |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 1416 | pidList[i]=0; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1417 | return pidList; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1418 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1419 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1420 | #endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */ |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1421 | |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1422 | #ifndef DMALLOC |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1423 | /* this should really be farmed out to libbusybox.a */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1424 | extern void *xmalloc(size_t size) |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1425 | { |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1426 | void *ptr = malloc(size); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1427 | |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1428 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1429 | error_msg_and_die(memory_exhausted); |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1430 | return ptr; |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1433 | extern void *xrealloc(void *old, size_t size) |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1434 | { |
| 1435 | void *ptr = realloc(old, size); |
| 1436 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1437 | error_msg_and_die(memory_exhausted); |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1438 | return ptr; |
| 1439 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1440 | |
| 1441 | extern void *xcalloc(size_t nmemb, size_t size) |
| 1442 | { |
| 1443 | void *ptr = calloc(nmemb, size); |
| 1444 | if (!ptr) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1445 | error_msg_and_die(memory_exhausted); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1446 | return ptr; |
| 1447 | } |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1448 | #endif |
| 1449 | |
Eric Andersen | f3b2b52 | 2000-12-07 22:42:11 +0000 | [diff] [blame] | 1450 | #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] | 1451 | # ifndef DMALLOC |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1452 | extern char * xstrdup (const char *s) { |
| 1453 | char *t; |
| 1454 | |
| 1455 | if (s == NULL) |
| 1456 | return NULL; |
| 1457 | |
| 1458 | t = strdup (s); |
| 1459 | |
| 1460 | if (t == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1461 | error_msg_and_die(memory_exhausted); |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1462 | |
| 1463 | return t; |
| 1464 | } |
Eric Andersen | ae6eae0 | 2000-07-19 17:35:54 +0000 | [diff] [blame] | 1465 | # endif |
Matt Kraai | 88cc305 | 2000-08-26 16:52:28 +0000 | [diff] [blame] | 1466 | #endif |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1467 | |
Matt Kraai | 88cc305 | 2000-08-26 16:52:28 +0000 | [diff] [blame] | 1468 | #if defined BB_FEATURE_NFSMOUNT |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1469 | extern char * xstrndup (const char *s, int n) { |
| 1470 | char *t; |
| 1471 | |
| 1472 | if (s == NULL) |
Matt Kraai | 0dab829 | 2000-12-18 03:08:29 +0000 | [diff] [blame^] | 1473 | error_msg_and_die("xstrndup bug\n"); |
Eric Andersen | da1d1e7 | 2000-07-10 23:39:44 +0000 | [diff] [blame] | 1474 | |
| 1475 | t = xmalloc(n+1); |
| 1476 | strncpy(t,s,n); |
| 1477 | t[n] = 0; |
| 1478 | |
| 1479 | return t; |
| 1480 | } |
| 1481 | #endif |
| 1482 | |
| 1483 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1484 | #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1485 | extern int vdprintf(int d, const char *format, va_list ap) |
| 1486 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1487 | char buf[BUF_SIZE]; |
| 1488 | int len; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1489 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1490 | len = vsprintf(buf, format, ap); |
| 1491 | return write(d, buf, len); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1492 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1493 | #endif /* BB_SYSLOGD */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1494 | |
Eric Andersen | 624cc77 | 2000-09-21 02:04:51 +0000 | [diff] [blame] | 1495 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1496 | #if defined BB_FEATURE_MOUNT_LOOP |
Eric Andersen | 624cc77 | 2000-09-21 02:04:51 +0000 | [diff] [blame] | 1497 | #include <fcntl.h> |
| 1498 | #include "loop.h" /* Pull in loop device support */ |
| 1499 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1500 | extern int del_loop(const char *device) |
| 1501 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1502 | int fd; |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1503 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1504 | if ((fd = open(device, O_RDONLY)) < 0) { |
| 1505 | perror(device); |
| 1506 | return (FALSE); |
| 1507 | } |
| 1508 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
| 1509 | perror("ioctl: LOOP_CLR_FD"); |
| 1510 | return (FALSE); |
| 1511 | } |
| 1512 | close(fd); |
| 1513 | return (TRUE); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1514 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1515 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1516 | extern int set_loop(const char *device, const char *file, int offset, |
| 1517 | int *loopro) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1518 | { |
| 1519 | struct loop_info loopinfo; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1520 | int fd, ffd, mode; |
| 1521 | |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1522 | mode = *loopro ? O_RDONLY : O_RDWR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1523 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 1524 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
| 1525 | perror(file); |
| 1526 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1527 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1528 | if ((fd = open(device, mode)) < 0) { |
| 1529 | close(ffd); |
| 1530 | perror(device); |
| 1531 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1532 | } |
| 1533 | *loopro = (mode == O_RDONLY); |
| 1534 | |
| 1535 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 1536 | strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1537 | loopinfo.lo_name[LO_NAME_SIZE - 1] = 0; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1538 | |
| 1539 | loopinfo.lo_offset = offset; |
| 1540 | |
| 1541 | loopinfo.lo_encrypt_key_size = 0; |
| 1542 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
| 1543 | perror("ioctl: LOOP_SET_FD"); |
| 1544 | close(fd); |
| 1545 | close(ffd); |
| 1546 | return 1; |
| 1547 | } |
| 1548 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 1549 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
| 1550 | perror("ioctl: LOOP_SET_STATUS"); |
| 1551 | close(fd); |
| 1552 | close(ffd); |
| 1553 | return 1; |
| 1554 | } |
| 1555 | close(fd); |
| 1556 | close(ffd); |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1560 | extern char *find_unused_loop_device(void) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1561 | { |
| 1562 | char dev[20]; |
| 1563 | int i, fd; |
| 1564 | struct stat statbuf; |
| 1565 | struct loop_info loopinfo; |
| 1566 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1567 | for (i = 0; i <= 7; i++) { |
| 1568 | sprintf(dev, "/dev/loop%d", i); |
| 1569 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 1570 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 1571 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) { |
| 1572 | if (errno == ENXIO) { /* probably free */ |
| 1573 | close(fd); |
| 1574 | return strdup(dev); |
| 1575 | } |
| 1576 | } |
| 1577 | close(fd); |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1578 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1579 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1580 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1581 | return NULL; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1582 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1583 | #endif /* BB_FEATURE_MOUNT_LOOP */ |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1584 | |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1585 | #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] | 1586 | extern int find_real_root_device_name(char* name) |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1587 | { |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1588 | DIR *dir; |
| 1589 | struct dirent *entry; |
| 1590 | struct stat statBuf, rootStat; |
| 1591 | char fileName[BUFSIZ]; |
| 1592 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1593 | if (stat("/", &rootStat) != 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1594 | error_msg("could not stat '/'\n"); |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1595 | return( FALSE); |
| 1596 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1597 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1598 | dir = opendir("/dev"); |
| 1599 | if (!dir) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1600 | error_msg("could not open '/dev'\n"); |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1601 | return( FALSE); |
| 1602 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1603 | |
| 1604 | while((entry = readdir(dir)) != NULL) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1605 | |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1606 | /* Must skip ".." since that is "/", and so we |
| 1607 | * would get a false positive on ".." */ |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1608 | if (strcmp(entry->d_name, "..") == 0) |
| 1609 | continue; |
| 1610 | |
Eric Andersen | f9ca653 | 2000-08-11 17:45:02 +0000 | [diff] [blame] | 1611 | snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (stat(fileName, &statBuf) != 0) |
| 1614 | continue; |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1615 | /* Some char devices have the same dev_t as block |
| 1616 | * devices, so make sure this is a block device */ |
| 1617 | if (! S_ISBLK(statBuf.st_mode)) |
| 1618 | continue; |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1619 | if (statBuf.st_rdev == rootStat.st_rdev) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1620 | strcpy(name, fileName); |
| 1621 | return ( TRUE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1625 | return( FALSE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1626 | } |
Erik Andersen | 23dea9b | 2000-05-13 00:28:25 +0000 | [diff] [blame] | 1627 | #endif |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1628 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* get_line_from_file() - This function reads an entire line from a text file |
| 1631 | * up to a newline. It returns a malloc'ed char * which must be stored and |
| 1632 | * free'ed by the caller. */ |
| 1633 | extern char *get_line_from_file(FILE *file) |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1634 | { |
Mark Whitley | 9a8243a | 2000-07-10 19:31:31 +0000 | [diff] [blame] | 1635 | static const int GROWBY = 80; /* how large we will grow strings by */ |
| 1636 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1637 | int ch; |
| 1638 | int idx = 0; |
| 1639 | char *linebuf = NULL; |
| 1640 | int linebufsz = 0; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1641 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1642 | while (1) { |
| 1643 | ch = fgetc(file); |
| 1644 | if (ch == EOF) |
| 1645 | break; |
| 1646 | /* grow the line buffer as necessary */ |
Eric Andersen | 285b446 | 2000-09-23 19:56:46 +0000 | [diff] [blame] | 1647 | while (idx > linebufsz-2) |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1648 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1649 | linebuf[idx++] = (char)ch; |
| 1650 | if ((char)ch == '\n') |
| 1651 | break; |
| 1652 | } |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1653 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1654 | if (idx == 0) |
| 1655 | return NULL; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1656 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 1657 | linebuf[idx] = 0; |
| 1658 | return linebuf; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 1659 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1660 | |
Eric Andersen | 944be90 | 2000-09-25 16:58:39 +0000 | [diff] [blame] | 1661 | #if defined BB_CAT |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1662 | extern void print_file(FILE *file) |
| 1663 | { |
| 1664 | int c; |
| 1665 | |
| 1666 | while ((c = getc(file)) != EOF) |
| 1667 | putc(c, stdout); |
| 1668 | fclose(file); |
| 1669 | fflush(stdout); |
| 1670 | } |
| 1671 | |
| 1672 | extern int print_file_by_name(char *filename) |
| 1673 | { |
| 1674 | FILE *file; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1675 | if ((file = wfopen(filename, "r")) == NULL) |
| 1676 | return FALSE; |
| 1677 | print_file(file); |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1678 | return TRUE; |
| 1679 | } |
Matt Kraai | e7c1af1 | 2000-09-27 03:01:40 +0000 | [diff] [blame] | 1680 | #endif /* BB_CAT */ |
Pavel Roskin | cc8a66b | 2000-07-17 20:18:42 +0000 | [diff] [blame] | 1681 | |
Matt Kraai | cabafae | 2000-11-30 04:55:40 +0000 | [diff] [blame] | 1682 | #if defined BB_ECHO || defined BB_SH || defined BB_TR |
Eric Andersen | f7cf2f7 | 2000-07-05 17:26:35 +0000 | [diff] [blame] | 1683 | char process_escape_sequence(char **ptr) |
| 1684 | { |
| 1685 | char c; |
| 1686 | |
| 1687 | switch (c = *(*ptr)++) { |
| 1688 | case 'a': |
| 1689 | c = '\a'; |
| 1690 | break; |
| 1691 | case 'b': |
| 1692 | c = '\b'; |
| 1693 | break; |
| 1694 | case 'f': |
| 1695 | c = '\f'; |
| 1696 | break; |
| 1697 | case 'n': |
| 1698 | c = '\n'; |
| 1699 | break; |
Eric Andersen | 9bd71e8 | 2000-11-29 23:07:57 +0000 | [diff] [blame] | 1700 | case 'r': |
| 1701 | c = '\r'; |
| 1702 | break; |
Eric Andersen | f7cf2f7 | 2000-07-05 17:26:35 +0000 | [diff] [blame] | 1703 | case 't': |
| 1704 | c = '\t'; |
| 1705 | break; |
| 1706 | case 'v': |
| 1707 | c = '\v'; |
| 1708 | break; |
| 1709 | case '\\': |
| 1710 | c = '\\'; |
| 1711 | break; |
| 1712 | case '0': case '1': case '2': case '3': |
| 1713 | case '4': case '5': case '6': case '7': |
| 1714 | c -= '0'; |
| 1715 | if ('0' <= **ptr && **ptr <= '7') { |
| 1716 | c = c * 8 + (*(*ptr)++ - '0'); |
| 1717 | if ('0' <= **ptr && **ptr <= '7') |
| 1718 | c = c * 8 + (*(*ptr)++ - '0'); |
| 1719 | } |
| 1720 | break; |
| 1721 | default: |
| 1722 | (*ptr)--; |
| 1723 | c = '\\'; |
| 1724 | break; |
| 1725 | } |
| 1726 | return c; |
| 1727 | } |
| 1728 | #endif |
| 1729 | |
Eric Andersen | ba1ff9b | 2000-10-01 17:01:36 +0000 | [diff] [blame] | 1730 | #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] | 1731 | char *get_last_path_component(char *path) |
| 1732 | { |
| 1733 | char *s=path+strlen(path)-1; |
| 1734 | |
| 1735 | /* strip trailing slashes */ |
Eric Andersen | 69a6b2d | 2000-12-12 23:13:54 +0000 | [diff] [blame] | 1736 | while (s != path && *s == '/') { |
Eric Andersen | 0b4551f | 2000-07-10 16:44:03 +0000 | [diff] [blame] | 1737 | *s-- = '\0'; |
| 1738 | } |
| 1739 | |
| 1740 | /* find last component */ |
| 1741 | s = strrchr(path, '/'); |
Eric Andersen | 69a6b2d | 2000-12-12 23:13:54 +0000 | [diff] [blame] | 1742 | if (s == NULL || s[1] == '\0') |
| 1743 | return path; |
| 1744 | else |
| 1745 | return s+1; |
Eric Andersen | 0b4551f | 2000-07-10 16:44:03 +0000 | [diff] [blame] | 1746 | } |
| 1747 | #endif |
| 1748 | |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1749 | #if defined BB_GREP || defined BB_SED |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 1750 | void xregcomp(regex_t *preg, const char *regex, int cflags) |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1751 | { |
| 1752 | int ret; |
| 1753 | if ((ret = regcomp(preg, regex, cflags)) != 0) { |
| 1754 | int errmsgsz = regerror(ret, preg, NULL, 0); |
| 1755 | char *errmsg = xmalloc(errmsgsz); |
| 1756 | regerror(ret, preg, errmsg, errmsgsz); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1757 | error_msg_and_die("xregcomp: %s\n", errmsg); |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1758 | } |
Mark Whitley | df5f6ba | 2000-07-11 16:53:56 +0000 | [diff] [blame] | 1759 | } |
| 1760 | #endif |
| 1761 | |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 1762 | #if defined BB_CAT || defined BB_HEAD || defined BB_WC |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1763 | FILE *wfopen(const char *path, const char *mode) |
| 1764 | { |
| 1765 | FILE *fp; |
| 1766 | if ((fp = fopen(path, mode)) == NULL) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1767 | error_msg("%s: %s\n", path, strerror(errno)); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 1768 | errno = 0; |
| 1769 | } |
| 1770 | return fp; |
| 1771 | } |
| 1772 | #endif |
| 1773 | |
Eric Andersen | e790ddb | 2000-11-17 06:50:42 +0000 | [diff] [blame] | 1774 | #if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \ |
| 1775 | || defined BB_SED || defined BB_SH || defined BB_UNIQ \ |
| 1776 | || defined BB_WC || defined BB_CMP |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 1777 | FILE *xfopen(const char *path, const char *mode) |
| 1778 | { |
| 1779 | FILE *fp; |
| 1780 | if ((fp = fopen(path, mode)) == NULL) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1781 | error_msg_and_die("%s: %s\n", path, strerror(errno)); |
Matt Kraai | e0bcce0 | 2000-09-27 02:29:39 +0000 | [diff] [blame] | 1782 | return fp; |
| 1783 | } |
| 1784 | #endif |
| 1785 | |
Eric Andersen | e5aef92 | 2000-11-17 18:25:26 +0000 | [diff] [blame] | 1786 | int applet_name_compare(const void *x, const void *y) |
| 1787 | { |
| 1788 | const struct BB_applet *applet1 = x; |
| 1789 | const struct BB_applet *applet2 = y; |
| 1790 | |
| 1791 | return strcmp(applet1->name, applet2->name); |
| 1792 | } |
| 1793 | |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 1794 | #if defined BB_NC |
| 1795 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 1796 | { |
| 1797 | ssize_t n; |
| 1798 | |
| 1799 | do { |
| 1800 | n = read(fd, buf, count); |
| 1801 | } while (n < 0 && errno == EINTR); |
| 1802 | |
| 1803 | return n; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1807 | /* END CODE */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 1808 | /* |
| 1809 | Local Variables: |
| 1810 | c-file-style: "linux" |
| 1811 | c-basic-offset: 4 |
| 1812 | tab-width: 4 |
| 1813 | End: |
| 1814 | */ |