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