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