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