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