Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines. |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * Copyright (C) tons of folks. Tracking down who wrote what |
| 5 | * isn't something I'm going to worry about... If you wrote something |
| 6 | * here, please feel free to acknowledge your work. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 23 | * Permission has been granted to redistribute this code under the GPL. |
| 24 | * |
| 25 | */ |
| 26 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 27 | #include "internal.h" |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 32 | #include <dirent.h> |
| 33 | #include <time.h> |
| 34 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 37 | #include <ctype.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 39 | #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 40 | # if defined BB_FEATURE_USE_PROCFS |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 41 | const char mtab_file[] = "/proc/mounts"; |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 42 | # else |
| 43 | # if defined BB_MTAB |
| 44 | const char mtab_file[] = "/etc/mtab"; |
| 45 | # else |
| 46 | # error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS |
| 47 | # endif |
| 48 | # endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 52 | extern void usage(const char *usage) |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 53 | { |
Eric Andersen | a07f0b0 | 1999-10-22 19:49:09 +0000 | [diff] [blame] | 54 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT); |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 55 | fprintf(stderr, "Usage: %s\n", usage); |
| 56 | exit(FALSE); |
| 57 | } |
| 58 | |
| 59 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 60 | #if defined (BB_INIT) || defined (BB_PS) |
| 61 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 62 | #if ! defined BB_FEATURE_USE_PROCFS |
| 63 | #error Sorry, I depend on the /proc filesystem right now. |
| 64 | #endif |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 65 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, |
| 66 | * so, for example, to check if the kernel is greater than 2.2.11: |
| 67 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
| 68 | */ |
| 69 | int |
| 70 | get_kernel_revision() |
| 71 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 72 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 73 | int major=0, minor=0, patch=0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 74 | char* filename="/proc/sys/kernel/osrelease"; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 75 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 76 | file = fopen(filename,"r"); |
| 77 | if (file == NULL) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 78 | /* bummer, /proc must not be mounted... */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 79 | return( 0); |
| 80 | } |
| 81 | fscanf(file,"%d.%d.%d",&major,&minor,&patch); |
| 82 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 83 | return major*65536 + minor*256 + patch; |
| 84 | } |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 89 | |
| 90 | #if defined (BB_CP) || defined (BB_MV) |
| 91 | /* |
| 92 | * Return TRUE if a fileName is a directory. |
| 93 | * Nonexistant files return FALSE. |
| 94 | */ |
| 95 | int isDirectory(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 97 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 99 | if (stat(name, &statBuf) < 0) |
| 100 | return FALSE; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 101 | if (S_ISDIR(statBuf.st_mode)) |
| 102 | return TRUE; |
| 103 | return(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Copy one file to another, while possibly preserving its modes, times, |
| 109 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
| 110 | * error message output. (Failure is not indicted if the attributes cannot |
| 111 | * be set.) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 112 | * -Erik Andersen |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 113 | */ |
| 114 | int |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 115 | copyFile( const char *srcName, const char *destName, |
| 116 | int setModes, int followLinks) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 117 | { |
| 118 | int rfd; |
| 119 | int wfd; |
| 120 | int rcc; |
| 121 | int result; |
| 122 | char buf[BUF_SIZE]; |
| 123 | struct stat srcStatBuf; |
| 124 | struct stat dstStatBuf; |
| 125 | struct utimbuf times; |
| 126 | |
| 127 | if (followLinks == FALSE) |
| 128 | result = stat(srcName, &srcStatBuf); |
| 129 | else |
| 130 | result = lstat(srcName, &srcStatBuf); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 131 | if (result < 0) { |
| 132 | perror(srcName); |
| 133 | return FALSE; |
| 134 | } |
| 135 | |
| 136 | if (followLinks == FALSE) |
| 137 | result = stat(destName, &dstStatBuf); |
| 138 | else |
| 139 | result = lstat(destName, &dstStatBuf); |
| 140 | if (result < 0) { |
| 141 | dstStatBuf.st_ino = -1; |
| 142 | dstStatBuf.st_dev = -1; |
| 143 | } |
| 144 | |
| 145 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 146 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
| 147 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 148 | return FALSE; |
| 149 | } |
| 150 | |
| 151 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 152 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 153 | /* Make sure the directory is writable */ |
| 154 | if (mkdir(destName, 0777777 ^ umask(0))) { |
| 155 | perror(destName); |
| 156 | return (FALSE); |
| 157 | } |
| 158 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
| 159 | char *link_val; |
| 160 | int link_size; |
| 161 | |
| 162 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
| 163 | link_val = (char *) alloca(PATH_MAX + 2); |
| 164 | link_size = readlink(srcName, link_val, PATH_MAX + 1); |
| 165 | if (link_size < 0) { |
| 166 | perror(srcName); |
| 167 | return (FALSE); |
| 168 | } |
| 169 | link_val[link_size] = '\0'; |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 170 | link_size = symlink(link_val, destName); |
| 171 | if (link_size != 0) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 172 | perror(destName); |
| 173 | return (FALSE); |
| 174 | } |
| 175 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 176 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 177 | if (mkfifo(destName, 644)) { |
| 178 | perror(destName); |
| 179 | return (FALSE); |
| 180 | } |
| 181 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 182 | || S_ISSOCK (srcStatBuf.st_mode)) { |
| 183 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 184 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) { |
| 185 | perror(destName); |
| 186 | return (FALSE); |
| 187 | } |
| 188 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 189 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 190 | rfd = open(srcName, O_RDONLY); |
| 191 | if (rfd < 0) { |
| 192 | perror(srcName); |
| 193 | return FALSE; |
| 194 | } |
| 195 | |
| 196 | wfd = creat(destName, srcStatBuf.st_mode); |
| 197 | if (wfd < 0) { |
| 198 | perror(destName); |
| 199 | close(rfd); |
| 200 | return FALSE; |
| 201 | } |
| 202 | |
| 203 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 204 | if (fullWrite(wfd, buf, rcc) < 0) |
| 205 | goto error_exit; |
| 206 | } |
| 207 | if (rcc < 0) { |
| 208 | goto error_exit; |
| 209 | } |
| 210 | |
| 211 | close(rfd); |
| 212 | if (close(wfd) < 0) { |
| 213 | return FALSE; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (setModes == TRUE) { |
| 218 | //fprintf(stderr, "Setting permissions for %s\n", destName); |
| 219 | chmod(destName, srcStatBuf.st_mode); |
| 220 | if (followLinks == TRUE) |
| 221 | chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 222 | else |
| 223 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 224 | |
| 225 | times.actime = srcStatBuf.st_atime; |
| 226 | times.modtime = srcStatBuf.st_mtime; |
| 227 | |
| 228 | utime(destName, ×); |
| 229 | } |
| 230 | |
| 231 | return TRUE; |
| 232 | |
| 233 | |
| 234 | error_exit: |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 235 | perror(destName); |
| 236 | close(rfd); |
| 237 | close(wfd); |
| 238 | |
| 239 | return FALSE; |
| 240 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
| 243 | |
| 244 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 245 | #if defined BB_TAR || defined BB_LS |
| 246 | |
| 247 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 248 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 249 | |
| 250 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ |
| 251 | static const mode_t SBIT[] = { |
| 252 | 0, 0, S_ISUID, |
| 253 | 0, 0, S_ISGID, |
| 254 | 0, 0, S_ISVTX |
| 255 | }; |
| 256 | |
| 257 | /* The 9 mode bits to test */ |
| 258 | static const mode_t MBIT[] = { |
| 259 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 260 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 261 | S_IROTH, S_IWOTH, S_IXOTH |
| 262 | }; |
| 263 | |
| 264 | #define MODE1 "rwxrwxrwx" |
| 265 | #define MODE0 "---------" |
| 266 | #define SMODE1 "..s..s..t" |
| 267 | #define SMODE0 "..S..S..T" |
| 268 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 269 | /* |
| 270 | * Return the standard ls-like mode string from a file mode. |
| 271 | * This is static and so is overwritten on each call. |
| 272 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 273 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 274 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 275 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 276 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 277 | int i; |
| 278 | buf[0] = TYPECHAR(mode); |
| 279 | for (i=0; i<9; i++) { |
| 280 | if (mode & SBIT[i]) |
| 281 | buf[i+1] = (mode & MBIT[i])? |
| 282 | SMODE1[i] : SMODE0[i]; |
| 283 | else |
| 284 | buf[i+1] = (mode & MBIT[i])? |
| 285 | MODE1[i] : MODE0[i]; |
| 286 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 287 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 288 | } |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 289 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 290 | |
| 291 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 292 | #if defined BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 293 | /* |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 294 | * Return the standard ls-like time string from a time_t |
| 295 | * This is static and so is overwritten on each call. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 296 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 297 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 298 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 299 | time_t now; |
| 300 | char *str; |
| 301 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 302 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 303 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 304 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 305 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 306 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 307 | strcpy(buf, &str[4]); |
| 308 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 309 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 310 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 311 | strcpy(&buf[7], &str[20]); |
| 312 | buf[11] = '\0'; |
| 313 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 314 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 315 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 318 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 319 | * Write all of the supplied buffer out to a file. |
| 320 | * This does multiple writes as necessary. |
| 321 | * Returns the amount written, or -1 on an error. |
| 322 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 323 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 324 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 325 | int cc; |
| 326 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 327 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 328 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 329 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 330 | while (len > 0) { |
| 331 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 332 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 333 | if (cc < 0) |
| 334 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 335 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 336 | buf += cc; |
| 337 | total += cc; |
| 338 | len -= cc; |
| 339 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 340 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 341 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 342 | } |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 343 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 344 | |
| 345 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 346 | #if defined BB_TAR || defined BB_TAIL |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 347 | /* |
| 348 | * Read all of the supplied buffer from a file. |
| 349 | * This does multiple reads as necessary. |
| 350 | * Returns the amount read, or -1 on an error. |
| 351 | * A short read is returned on an end of file. |
| 352 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 353 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 354 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 355 | int cc; |
| 356 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 357 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 358 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 359 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 360 | while (len > 0) { |
| 361 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 362 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 363 | if (cc < 0) |
| 364 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 365 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 366 | if (cc == 0) |
| 367 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 368 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 369 | buf += cc; |
| 370 | total += cc; |
| 371 | len -= cc; |
| 372 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 373 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 374 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 375 | } |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 376 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 377 | |
| 378 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 379 | #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 380 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 381 | * Walk down all the directories under the specified |
| 382 | * location, and do something (something specified |
| 383 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 384 | * |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 385 | * Unfortunatly, while nftw(3) could replace this and reduce |
| 386 | * 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] | 387 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 388 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | */ |
| 390 | int |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 391 | recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst, |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 392 | int (*fileAction) (const char *fileName, struct stat* statbuf), |
| 393 | int (*dirAction) (const char *fileName, struct stat* statbuf)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 394 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 395 | int status; |
| 396 | struct stat statbuf; |
| 397 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 398 | |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 399 | if (followLinks == TRUE) |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 400 | status = stat(fileName, &statbuf); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 401 | else |
| 402 | status = lstat(fileName, &statbuf); |
| 403 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 404 | if (status < 0) { |
| 405 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 406 | return (FALSE); |
| 407 | } |
| 408 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 409 | if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) { |
| 410 | if (fileAction == NULL) |
| 411 | return (TRUE); |
| 412 | else |
| 413 | return (fileAction(fileName, &statbuf)); |
| 414 | } |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 415 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 416 | if (recurse == FALSE) { |
| 417 | if (S_ISDIR(statbuf.st_mode)) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 418 | if (dirAction != NULL) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 419 | return (dirAction(fileName, &statbuf)); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 420 | else |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 421 | return (TRUE); |
| 422 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 425 | if (S_ISDIR(statbuf.st_mode)) { |
| 426 | DIR *dir; |
| 427 | dir = opendir(fileName); |
| 428 | if (!dir) { |
| 429 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 430 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 431 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 432 | if (dirAction != NULL && depthFirst == FALSE) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 433 | status = dirAction(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 434 | if (status == FALSE) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 435 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 436 | return (FALSE); |
| 437 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 438 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 439 | while ((next = readdir(dir)) != NULL) { |
| 440 | char nextFile[NAME_MAX]; |
| 441 | if ((strcmp(next->d_name, "..") == 0) |
| 442 | || (strcmp(next->d_name, ".") == 0)) { |
| 443 | continue; |
| 444 | } |
| 445 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 446 | status = |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 447 | recursiveAction(nextFile, TRUE, followLinks, depthFirst, |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 448 | fileAction, dirAction); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 449 | if (status < 0) { |
| 450 | closedir(dir); |
| 451 | return (FALSE); |
| 452 | } |
| 453 | } |
| 454 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 455 | if (status < 0) { |
| 456 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 457 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 458 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 459 | if (dirAction != NULL && depthFirst == TRUE) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 460 | status = dirAction(fileName, &statbuf); |
| 461 | if (status == FALSE) { |
| 462 | perror(fileName); |
| 463 | return (FALSE); |
| 464 | } |
| 465 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 466 | } else { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 467 | if (fileAction == NULL) |
| 468 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 469 | else |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 470 | return (fileAction(fileName, &statbuf)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 471 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 472 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 475 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 476 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 477 | |
| 478 | |
| 479 | #if defined (BB_TAR) || defined (BB_MKDIR) |
| 480 | /* |
| 481 | * Attempt to create the directories along the specified path, except for |
| 482 | * the final component. The mode is given for the final directory only, |
| 483 | * while all previous ones get default protections. Errors are not reported |
| 484 | * here, as failures to restore files can be reported later. |
| 485 | */ |
| 486 | extern void createPath (const char *name, int mode) |
| 487 | { |
| 488 | char *cp; |
| 489 | char *cpOld; |
| 490 | char buf[NAME_MAX]; |
| 491 | |
Eric Andersen | cb41c2e | 1999-11-22 07:41:00 +0000 | [diff] [blame] | 492 | strcpy( buf, name); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 493 | cp = strchr (buf, '/'); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 494 | while (cp) { |
| 495 | cpOld = cp; |
| 496 | cp = strchr (cp + 1, '/'); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 497 | *cpOld = '\0'; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 498 | mkdir (buf, cp ? 0777 : mode); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 499 | *cpOld = '/'; |
| 500 | } |
| 501 | } |
| 502 | #endif |
| 503 | |
| 504 | |
| 505 | |
| 506 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 507 | /* [ugoa]{+|-|=}[rwxst] */ |
| 508 | |
| 509 | |
| 510 | |
| 511 | extern int |
| 512 | parse_mode( const char* s, mode_t* theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 513 | { |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 514 | mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 515 | mode_t orMode = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 516 | mode_t mode = 0; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 517 | mode_t groups = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 518 | char type; |
| 519 | char c; |
| 520 | |
| 521 | do { |
| 522 | for ( ; ; ) { |
| 523 | switch ( c = *s++ ) { |
| 524 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 525 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 526 | case 'u': |
| 527 | groups |= S_ISUID|S_IRWXU; |
| 528 | continue; |
| 529 | case 'g': |
| 530 | groups |= S_ISGID|S_IRWXG; |
| 531 | continue; |
| 532 | case 'o': |
| 533 | groups |= S_IRWXO; |
| 534 | continue; |
| 535 | case 'a': |
| 536 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 537 | continue; |
| 538 | case '+': |
| 539 | case '=': |
| 540 | case '-': |
| 541 | type = c; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 542 | if ( groups == 0 ) /* The default is "all" */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 543 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 544 | break; |
| 545 | default: |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 546 | if ( isdigit(c) && c >= '0' && c <= '7' && |
| 547 | mode == 0 && groups == 0 ) { |
| 548 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 549 | return (TRUE); |
| 550 | } |
| 551 | else |
| 552 | return (FALSE); |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | while ( (c = *s++) != '\0' ) { |
| 558 | switch ( c ) { |
| 559 | case ',': |
| 560 | break; |
| 561 | case 'r': |
| 562 | mode |= S_IRUSR|S_IRGRP|S_IROTH; |
| 563 | continue; |
| 564 | case 'w': |
| 565 | mode |= S_IWUSR|S_IWGRP|S_IWOTH; |
| 566 | continue; |
| 567 | case 'x': |
| 568 | mode |= S_IXUSR|S_IXGRP|S_IXOTH; |
| 569 | continue; |
| 570 | case 's': |
| 571 | mode |= S_IXGRP|S_ISUID|S_ISGID; |
| 572 | continue; |
| 573 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 574 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 575 | continue; |
| 576 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 577 | *theMode &= andMode; |
| 578 | *theMode |= orMode; |
| 579 | return( TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 580 | } |
| 581 | break; |
| 582 | } |
| 583 | switch ( type ) { |
| 584 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 585 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 586 | /* fall through */ |
| 587 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 588 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 589 | break; |
| 590 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 591 | andMode &= ~(mode & groups); |
| 592 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | } while ( c == ',' ); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 596 | *theMode &= andMode; |
| 597 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 598 | return (TRUE); |
| 599 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 600 | |
| 601 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 602 | #endif |
| 603 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 604 | |
| 605 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS) |
| 611 | |
| 612 | /* Use this to avoid needing the glibc NSS stuff |
| 613 | * This uses storage buf to hold things. |
| 614 | * */ |
| 615 | uid_t |
| 616 | my_getid(const char *filename, char *name, uid_t id) |
| 617 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 618 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 619 | char *rname, *start, *end, buf[128]; |
| 620 | uid_t rid; |
| 621 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 622 | file=fopen(filename,"r"); |
| 623 | if (file == NULL) { |
| 624 | perror(filename); |
| 625 | return (-1); |
| 626 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 627 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 628 | while (fgets (buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 629 | if (buf[0] == '#') |
| 630 | continue; |
| 631 | |
| 632 | start = buf; |
| 633 | end = strchr (start, ':'); |
| 634 | if (end == NULL) |
| 635 | continue; |
| 636 | *end = '\0'; |
| 637 | rname = start; |
| 638 | |
| 639 | start = end + 1; |
| 640 | end = strchr (start, ':'); |
| 641 | if (end == NULL) |
| 642 | continue; |
| 643 | |
| 644 | start = end + 1; |
| 645 | rid = (uid_t) strtol (start, &end, 10); |
| 646 | if (end == start) |
| 647 | continue; |
| 648 | |
| 649 | if (name) { |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame] | 650 | if (0 == strcmp(rname, name)) { |
| 651 | fclose( file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 652 | return( rid); |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame] | 653 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 654 | } |
| 655 | if ( id != -1 && id == rid ) { |
| 656 | strncpy(name, rname, 8); |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame] | 657 | fclose( file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 658 | return( TRUE); |
| 659 | } |
| 660 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 661 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 662 | return (-1); |
| 663 | } |
| 664 | |
| 665 | uid_t |
| 666 | my_getpwnam(char *name) |
| 667 | { |
| 668 | return my_getid("/etc/passwd", name, -1); |
| 669 | } |
| 670 | |
| 671 | gid_t |
| 672 | my_getgrnam(char *name) |
| 673 | { |
| 674 | return my_getid("/etc/group", name, -1); |
| 675 | } |
| 676 | |
| 677 | void |
| 678 | my_getpwuid(char* name, uid_t uid) |
| 679 | { |
| 680 | my_getid("/etc/passwd", name, uid); |
| 681 | } |
| 682 | |
| 683 | void |
| 684 | my_getgrgid(char* group, gid_t gid) |
| 685 | { |
| 686 | my_getid("/etc/group", group, gid); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | #endif |
| 691 | |
| 692 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 693 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 694 | |
| 695 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
| 696 | |
| 697 | |
| 698 | #include <linux/kd.h> |
| 699 | #include <sys/ioctl.h> |
| 700 | |
| 701 | int is_a_console(int fd) |
| 702 | { |
| 703 | char arg; |
| 704 | |
| 705 | arg = 0; |
| 706 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 707 | && ((arg == KB_101) || (arg == KB_84))); |
| 708 | } |
| 709 | |
| 710 | static int open_a_console(char *fnam) |
| 711 | { |
| 712 | int fd; |
| 713 | |
| 714 | /* try read-only */ |
| 715 | fd = open(fnam, O_RDWR); |
| 716 | |
| 717 | /* if failed, try read-only */ |
| 718 | if (fd < 0 && errno == EACCES) |
| 719 | fd = open(fnam, O_RDONLY); |
| 720 | |
| 721 | /* if failed, try write-only */ |
| 722 | if (fd < 0 && errno == EACCES) |
| 723 | fd = open(fnam, O_WRONLY); |
| 724 | |
| 725 | /* if failed, fail */ |
| 726 | if (fd < 0) |
| 727 | return -1; |
| 728 | |
| 729 | /* if not a console, fail */ |
| 730 | if (! is_a_console(fd)) |
| 731 | { |
| 732 | close(fd); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | /* success */ |
| 737 | return fd; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Get an fd for use with kbd/console ioctls. |
| 742 | * We try several things because opening /dev/console will fail |
| 743 | * if someone else used X (which does a chown on /dev/console). |
| 744 | * |
| 745 | * if tty_name is non-NULL, try this one instead. |
| 746 | */ |
| 747 | |
| 748 | int get_console_fd(char* tty_name) |
| 749 | { |
| 750 | int fd; |
| 751 | |
| 752 | if (tty_name) |
| 753 | { |
| 754 | if (-1 == (fd = open_a_console(tty_name))) |
| 755 | return -1; |
| 756 | else |
| 757 | return fd; |
| 758 | } |
| 759 | |
| 760 | fd = open_a_console("/dev/tty"); |
| 761 | if (fd >= 0) |
| 762 | return fd; |
| 763 | |
| 764 | fd = open_a_console("/dev/tty0"); |
| 765 | if (fd >= 0) |
| 766 | return fd; |
| 767 | |
| 768 | fd = open_a_console("/dev/console"); |
| 769 | if (fd >= 0) |
| 770 | return fd; |
| 771 | |
| 772 | for (fd = 0; fd < 3; fd++) |
| 773 | if (is_a_console(fd)) |
| 774 | return fd; |
| 775 | |
| 776 | fprintf(stderr, |
| 777 | "Couldnt get a file descriptor referring to the console\n"); |
| 778 | return -1; /* total failure */ |
| 779 | } |
| 780 | |
| 781 | |
| 782 | #endif |
| 783 | |
| 784 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 785 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 786 | |
| 787 | /* Do a case insensitive strstr() */ |
| 788 | char* stristr(char *haystack, const char *needle) |
| 789 | { |
| 790 | int len = strlen( needle ); |
| 791 | while( *haystack ) { |
| 792 | if( !strncasecmp( haystack, needle, len ) ) |
| 793 | break; |
| 794 | haystack++; |
| 795 | } |
| 796 | |
| 797 | if( !(*haystack) ) |
| 798 | haystack = NULL; |
| 799 | |
| 800 | return haystack; |
| 801 | } |
| 802 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 803 | /* This tries to find a needle in a haystack, but does so by |
| 804 | * only trying to match literal strings (look 'ma, no regexps!) |
| 805 | * This is short, sweet, and carries _very_ little baggage, |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 806 | * unlike its beefier cousin in regexp.c |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 807 | * -Erik Andersen |
| 808 | */ |
| 809 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 810 | { |
| 811 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 812 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 813 | haystack = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 814 | else |
| 815 | haystack = stristr (haystack, needle); |
| 816 | if (haystack == NULL) |
| 817 | return FALSE; |
| 818 | return TRUE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 822 | /* This performs substitutions after a string match has been found. */ |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 823 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
| 824 | { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 825 | int foundOne=0; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 826 | char *where, *slider, *slider1, *oldhayStack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 827 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 828 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 829 | where = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 830 | else |
| 831 | where = stristr (haystack, needle); |
| 832 | |
| 833 | if (strcmp(needle, newNeedle)==0) |
| 834 | return FALSE; |
| 835 | |
| 836 | oldhayStack = (char*)malloc((unsigned)(strlen(haystack))); |
| 837 | while(where!=NULL) { |
| 838 | foundOne++; |
| 839 | strcpy(oldhayStack, haystack); |
| 840 | #if 0 |
| 841 | if ( strlen(newNeedle) > strlen(needle)) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 842 | haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - |
| 843 | strlen(needle) + strlen(newNeedle))); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 844 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 845 | #endif |
| 846 | for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++); |
| 847 | *slider=0; |
| 848 | haystack=strcat(haystack, newNeedle); |
| 849 | slider1+=strlen(needle); |
| 850 | haystack = strcat(haystack, slider1); |
| 851 | where = strstr (slider, needle); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 852 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 853 | free( oldhayStack); |
| 854 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 855 | if (foundOne > 0) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 856 | return TRUE; |
| 857 | else |
| 858 | return FALSE; |
| 859 | } |
| 860 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 861 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 862 | #endif |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 863 | |
| 864 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 865 | #if defined BB_FIND |
| 866 | /* |
| 867 | * Routine to see if a text string is matched by a wildcard pattern. |
| 868 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 869 | * or if the pattern is invalid. |
| 870 | * * matches zero or more characters |
| 871 | * ? matches a single character |
| 872 | * [abc] matches 'a', 'b' or 'c' |
| 873 | * \c quotes character c |
| 874 | * Adapted from code written by Ingo Wilken, and |
| 875 | * then taken from sash, Copyright (c) 1999 by David I. Bell |
| 876 | * Permission is granted to use, distribute, or modify this source, |
| 877 | * provided that this copyright notice remains intact. |
| 878 | * Permission to distribute this code under the GPL has been granted. |
| 879 | */ |
| 880 | extern int |
| 881 | check_wildcard_match(const char* text, const char* pattern) |
| 882 | { |
| 883 | const char* retryPat; |
| 884 | const char* retryText; |
| 885 | int ch; |
| 886 | int found; |
| 887 | |
| 888 | retryPat = NULL; |
| 889 | retryText = NULL; |
| 890 | |
| 891 | while (*text || *pattern) |
| 892 | { |
| 893 | ch = *pattern++; |
| 894 | |
| 895 | switch (ch) |
| 896 | { |
| 897 | case '*': |
| 898 | retryPat = pattern; |
| 899 | retryText = text; |
| 900 | break; |
| 901 | |
| 902 | case '[': |
| 903 | found = FALSE; |
| 904 | |
| 905 | while ((ch = *pattern++) != ']') |
| 906 | { |
| 907 | if (ch == '\\') |
| 908 | ch = *pattern++; |
| 909 | |
| 910 | if (ch == '\0') |
| 911 | return FALSE; |
| 912 | |
| 913 | if (*text == ch) |
| 914 | found = TRUE; |
| 915 | } |
| 916 | |
| 917 | //if (!found) |
| 918 | if (found==TRUE) |
| 919 | { |
| 920 | pattern = retryPat; |
| 921 | text = ++retryText; |
| 922 | } |
| 923 | |
| 924 | /* fall into next case */ |
| 925 | |
| 926 | case '?': |
| 927 | if (*text++ == '\0') |
| 928 | return FALSE; |
| 929 | |
| 930 | break; |
| 931 | |
| 932 | case '\\': |
| 933 | ch = *pattern++; |
| 934 | |
| 935 | if (ch == '\0') |
| 936 | return FALSE; |
| 937 | |
| 938 | /* fall into next case */ |
| 939 | |
| 940 | default: |
| 941 | if (*text == ch) |
| 942 | { |
| 943 | if (*text) |
| 944 | text++; |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | if (*text) |
| 949 | { |
| 950 | pattern = retryPat; |
| 951 | text = ++retryText; |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | return FALSE; |
| 956 | } |
| 957 | |
| 958 | if (pattern == NULL) |
| 959 | return FALSE; |
| 960 | } |
| 961 | |
| 962 | return TRUE; |
| 963 | } |
| 964 | #endif |
| 965 | |
| 966 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 967 | |
| 968 | |
| 969 | #if defined BB_DF | defined BB_MTAB |
| 970 | /* |
| 971 | * Given a block device, find the mount table entry if that block device |
| 972 | * is mounted. |
| 973 | * |
| 974 | * Given any other file (or directory), find the mount table entry for its |
| 975 | * filesystem. |
| 976 | */ |
| 977 | extern struct mntent *findMountPoint(const char *name, const char *table) |
| 978 | { |
| 979 | struct stat s; |
| 980 | dev_t mountDevice; |
| 981 | FILE *mountTable; |
| 982 | struct mntent *mountEntry; |
| 983 | |
| 984 | if (stat(name, &s) != 0) |
| 985 | return 0; |
| 986 | |
| 987 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 988 | mountDevice = s.st_rdev; |
| 989 | else |
| 990 | mountDevice = s.st_dev; |
| 991 | |
| 992 | |
| 993 | if ((mountTable = setmntent(table, "r")) == 0) |
| 994 | return 0; |
| 995 | |
| 996 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 997 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 998 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 999 | break; |
| 1000 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 1001 | break; |
| 1002 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 1003 | break; |
| 1004 | } |
| 1005 | endmntent(mountTable); |
| 1006 | return mountEntry; |
| 1007 | } |
| 1008 | |
| 1009 | #endif |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF ) |
| 1014 | extern void whine_if_fstab_is_missing() |
| 1015 | { |
| 1016 | struct stat statBuf; |
| 1017 | if (stat("/etc/fstab", &statBuf) < 0) |
| 1018 | fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n"); |
| 1019 | } |
| 1020 | #endif |
| 1021 | |
| 1022 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1023 | #if defined BB_DD || defined BB_TAIL |
| 1024 | /* |
| 1025 | * Read a number with a possible multiplier. |
| 1026 | * Returns -1 if the number format is illegal. |
| 1027 | */ |
| 1028 | extern long getNum (const char *cp) |
| 1029 | { |
| 1030 | long value; |
| 1031 | |
| 1032 | if (!isDecimal (*cp)) |
| 1033 | return -1; |
| 1034 | |
| 1035 | value = 0; |
| 1036 | |
| 1037 | while (isDecimal (*cp)) |
| 1038 | value = value * 10 + *cp++ - '0'; |
| 1039 | |
| 1040 | switch (*cp++) { |
| 1041 | case 'm': |
| 1042 | value *= 1048576; |
| 1043 | break; |
| 1044 | |
| 1045 | case 'k': |
| 1046 | value *= 1024; |
| 1047 | break; |
| 1048 | |
| 1049 | case 'b': |
| 1050 | value *= 512; |
| 1051 | break; |
| 1052 | |
| 1053 | case 'w': |
| 1054 | value *= 2; |
| 1055 | break; |
| 1056 | |
| 1057 | case '\0': |
| 1058 | return value; |
| 1059 | |
| 1060 | default: |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | if (*cp) |
| 1065 | return -1; |
| 1066 | |
| 1067 | return value; |
| 1068 | } |
| 1069 | #endif |
| 1070 | |
Eric Andersen | 6805d5d | 1999-12-09 22:39:55 +0000 | [diff] [blame] | 1071 | |
| 1072 | #if defined BB_INIT || defined BB_HALT || defined BB_REBOOT |
| 1073 | |
| 1074 | #if ! defined BB_FEATURE_USE_PROCFS |
| 1075 | #error Sorry, I depend on the /proc filesystem right now. |
| 1076 | #endif |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1077 | /* findInitPid() |
| 1078 | * |
| 1079 | * This finds the pid of init (which is not always 1). |
| 1080 | * Currently, it's implemented by rummaging through the proc filesystem. |
| 1081 | * |
| 1082 | * [return] |
| 1083 | * 0 failure |
| 1084 | * pid when init's pid is found. |
| 1085 | */ |
| 1086 | extern pid_t |
| 1087 | findInitPid() |
| 1088 | { |
| 1089 | pid_t init_pid; |
| 1090 | char filename[256]; |
| 1091 | char buffer[256]; |
| 1092 | |
| 1093 | /* no need to opendir ;) */ |
| 1094 | for (init_pid = 1; init_pid < 65536; init_pid++) { |
| 1095 | FILE *status; |
| 1096 | |
| 1097 | sprintf(filename, "/proc/%d/status", init_pid); |
| 1098 | status = fopen(filename, "r"); |
| 1099 | if (!status) { continue; } |
| 1100 | fgets(buffer, 256, status); |
| 1101 | fclose(status); |
| 1102 | |
Eric Andersen | 6805d5d | 1999-12-09 22:39:55 +0000 | [diff] [blame] | 1103 | if ( (strstr(buffer, "init\n") != NULL )) { |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1104 | return init_pid; |
| 1105 | } |
| 1106 | } |
| 1107 | return 0; |
| 1108 | } |
| 1109 | #endif |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1110 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1111 | /* END CODE */ |