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