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