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