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