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