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