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