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