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 | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 292 | #ifdef 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 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * Read all of the supplied buffer from a file. |
| 347 | * This does multiple reads as necessary. |
| 348 | * Returns the amount read, or -1 on an error. |
| 349 | * A short read is returned on an end of file. |
| 350 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 351 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 352 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 353 | int cc; |
| 354 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 355 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 356 | total = 0; |
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 | while (len > 0) { |
| 359 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 360 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 361 | if (cc < 0) |
| 362 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 363 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 364 | if (cc == 0) |
| 365 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 366 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 367 | buf += cc; |
| 368 | total += cc; |
| 369 | len -= cc; |
| 370 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 371 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 372 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 373 | } |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 374 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 375 | |
| 376 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 377 | #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] | 378 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 379 | * Walk down all the directories under the specified |
| 380 | * location, and do something (something specified |
| 381 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 382 | * |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 383 | * Unfortunatly, while nftw(3) could replace this and reduce |
| 384 | * 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] | 385 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 386 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 387 | */ |
| 388 | int |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 389 | recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst, |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 390 | int (*fileAction) (const char *fileName, struct stat* statbuf), |
| 391 | int (*dirAction) (const char *fileName, struct stat* statbuf)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 392 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 393 | int status; |
| 394 | struct stat statbuf; |
| 395 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 396 | |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 397 | if (followLinks == TRUE) |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 398 | status = stat(fileName, &statbuf); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 399 | else |
| 400 | status = lstat(fileName, &statbuf); |
| 401 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 402 | if (status < 0) { |
| 403 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 404 | return (FALSE); |
| 405 | } |
| 406 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 407 | if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) { |
| 408 | if (fileAction == NULL) |
| 409 | return (TRUE); |
| 410 | else |
| 411 | return (fileAction(fileName, &statbuf)); |
| 412 | } |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 413 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 414 | if (recurse == FALSE) { |
| 415 | if (S_ISDIR(statbuf.st_mode)) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 416 | if (dirAction != NULL) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 417 | return (dirAction(fileName, &statbuf)); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 418 | else |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 419 | return (TRUE); |
| 420 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 423 | if (S_ISDIR(statbuf.st_mode)) { |
| 424 | DIR *dir; |
| 425 | dir = opendir(fileName); |
| 426 | if (!dir) { |
| 427 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 428 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 429 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 430 | if (dirAction != NULL && depthFirst == FALSE) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 431 | status = dirAction(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 432 | if (status == FALSE) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 433 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 434 | return (FALSE); |
| 435 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 436 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 437 | while ((next = readdir(dir)) != NULL) { |
| 438 | char nextFile[NAME_MAX]; |
| 439 | if ((strcmp(next->d_name, "..") == 0) |
| 440 | || (strcmp(next->d_name, ".") == 0)) { |
| 441 | continue; |
| 442 | } |
| 443 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 444 | status = |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 445 | recursiveAction(nextFile, TRUE, followLinks, depthFirst, |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 446 | fileAction, dirAction); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 447 | if (status < 0) { |
| 448 | closedir(dir); |
| 449 | return (FALSE); |
| 450 | } |
| 451 | } |
| 452 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 453 | if (status < 0) { |
| 454 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 455 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 456 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 457 | if (dirAction != NULL && depthFirst == TRUE) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 458 | status = dirAction(fileName, &statbuf); |
| 459 | if (status == FALSE) { |
| 460 | perror(fileName); |
| 461 | return (FALSE); |
| 462 | } |
| 463 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 464 | } else { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 465 | if (fileAction == NULL) |
| 466 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 467 | else |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 468 | return (fileAction(fileName, &statbuf)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 469 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 470 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 473 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 474 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 475 | |
| 476 | |
| 477 | #if defined (BB_TAR) || defined (BB_MKDIR) |
| 478 | /* |
| 479 | * Attempt to create the directories along the specified path, except for |
| 480 | * the final component. The mode is given for the final directory only, |
| 481 | * while all previous ones get default protections. Errors are not reported |
| 482 | * here, as failures to restore files can be reported later. |
| 483 | */ |
| 484 | extern void createPath (const char *name, int mode) |
| 485 | { |
| 486 | char *cp; |
| 487 | char *cpOld; |
| 488 | char buf[NAME_MAX]; |
| 489 | |
Eric Andersen | cb41c2e | 1999-11-22 07:41:00 +0000 | [diff] [blame] | 490 | strcpy( buf, name); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 491 | cp = strchr (buf, '/'); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 492 | while (cp) { |
| 493 | cpOld = cp; |
| 494 | cp = strchr (cp + 1, '/'); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 495 | *cpOld = '\0'; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 496 | mkdir (buf, cp ? 0777 : mode); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 497 | *cpOld = '/'; |
| 498 | } |
| 499 | } |
| 500 | #endif |
| 501 | |
| 502 | |
| 503 | |
| 504 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 505 | /* [ugoa]{+|-|=}[rwxst] */ |
| 506 | |
| 507 | |
| 508 | |
| 509 | extern int |
| 510 | parse_mode( const char* s, mode_t* theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 511 | { |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 512 | mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 513 | mode_t orMode = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 514 | mode_t mode = 0; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 515 | mode_t groups = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 516 | char type; |
| 517 | char c; |
| 518 | |
| 519 | do { |
| 520 | for ( ; ; ) { |
| 521 | switch ( c = *s++ ) { |
| 522 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 523 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 524 | case 'u': |
| 525 | groups |= S_ISUID|S_IRWXU; |
| 526 | continue; |
| 527 | case 'g': |
| 528 | groups |= S_ISGID|S_IRWXG; |
| 529 | continue; |
| 530 | case 'o': |
| 531 | groups |= S_IRWXO; |
| 532 | continue; |
| 533 | case 'a': |
| 534 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 535 | continue; |
| 536 | case '+': |
| 537 | case '=': |
| 538 | case '-': |
| 539 | type = c; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 540 | if ( groups == 0 ) /* The default is "all" */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 541 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 542 | break; |
| 543 | default: |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 544 | if ( isdigit(c) && c >= '0' && c <= '7' && |
| 545 | mode == 0 && groups == 0 ) { |
| 546 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 547 | return (TRUE); |
| 548 | } |
| 549 | else |
| 550 | return (FALSE); |
| 551 | } |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | while ( (c = *s++) != '\0' ) { |
| 556 | switch ( c ) { |
| 557 | case ',': |
| 558 | break; |
| 559 | case 'r': |
| 560 | mode |= S_IRUSR|S_IRGRP|S_IROTH; |
| 561 | continue; |
| 562 | case 'w': |
| 563 | mode |= S_IWUSR|S_IWGRP|S_IWOTH; |
| 564 | continue; |
| 565 | case 'x': |
| 566 | mode |= S_IXUSR|S_IXGRP|S_IXOTH; |
| 567 | continue; |
| 568 | case 's': |
| 569 | mode |= S_IXGRP|S_ISUID|S_ISGID; |
| 570 | continue; |
| 571 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 572 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 573 | continue; |
| 574 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 575 | *theMode &= andMode; |
| 576 | *theMode |= orMode; |
| 577 | return( TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 578 | } |
| 579 | break; |
| 580 | } |
| 581 | switch ( type ) { |
| 582 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 583 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 584 | /* fall through */ |
| 585 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 586 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 587 | break; |
| 588 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 589 | andMode &= ~(mode & groups); |
| 590 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } while ( c == ',' ); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 594 | *theMode &= andMode; |
| 595 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 596 | return (TRUE); |
| 597 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 598 | |
| 599 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 600 | #endif |
| 601 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 602 | |
| 603 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS) |
| 609 | |
| 610 | /* Use this to avoid needing the glibc NSS stuff |
| 611 | * This uses storage buf to hold things. |
| 612 | * */ |
| 613 | uid_t |
| 614 | my_getid(const char *filename, char *name, uid_t id) |
| 615 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 616 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 617 | char *rname, *start, *end, buf[128]; |
| 618 | uid_t rid; |
| 619 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 620 | file=fopen(filename,"r"); |
| 621 | if (file == NULL) { |
| 622 | perror(filename); |
| 623 | return (-1); |
| 624 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 625 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 626 | while (fgets (buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 627 | if (buf[0] == '#') |
| 628 | continue; |
| 629 | |
| 630 | start = buf; |
| 631 | end = strchr (start, ':'); |
| 632 | if (end == NULL) |
| 633 | continue; |
| 634 | *end = '\0'; |
| 635 | rname = start; |
| 636 | |
| 637 | start = end + 1; |
| 638 | end = strchr (start, ':'); |
| 639 | if (end == NULL) |
| 640 | continue; |
| 641 | |
| 642 | start = end + 1; |
| 643 | rid = (uid_t) strtol (start, &end, 10); |
| 644 | if (end == start) |
| 645 | continue; |
| 646 | |
| 647 | if (name) { |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame^] | 648 | if (0 == strcmp(rname, name)) { |
| 649 | fclose( file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 650 | return( rid); |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame^] | 651 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 652 | } |
| 653 | if ( id != -1 && id == rid ) { |
| 654 | strncpy(name, rname, 8); |
Eric Andersen | 48091fb | 1999-12-09 01:15:52 +0000 | [diff] [blame^] | 655 | fclose( file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 656 | return( TRUE); |
| 657 | } |
| 658 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 659 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 660 | return (-1); |
| 661 | } |
| 662 | |
| 663 | uid_t |
| 664 | my_getpwnam(char *name) |
| 665 | { |
| 666 | return my_getid("/etc/passwd", name, -1); |
| 667 | } |
| 668 | |
| 669 | gid_t |
| 670 | my_getgrnam(char *name) |
| 671 | { |
| 672 | return my_getid("/etc/group", name, -1); |
| 673 | } |
| 674 | |
| 675 | void |
| 676 | my_getpwuid(char* name, uid_t uid) |
| 677 | { |
| 678 | my_getid("/etc/passwd", name, uid); |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | my_getgrgid(char* group, gid_t gid) |
| 683 | { |
| 684 | my_getid("/etc/group", group, gid); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | #endif |
| 689 | |
| 690 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 691 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 692 | |
| 693 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
| 694 | |
| 695 | |
| 696 | #include <linux/kd.h> |
| 697 | #include <sys/ioctl.h> |
| 698 | |
| 699 | int is_a_console(int fd) |
| 700 | { |
| 701 | char arg; |
| 702 | |
| 703 | arg = 0; |
| 704 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 705 | && ((arg == KB_101) || (arg == KB_84))); |
| 706 | } |
| 707 | |
| 708 | static int open_a_console(char *fnam) |
| 709 | { |
| 710 | int fd; |
| 711 | |
| 712 | /* try read-only */ |
| 713 | fd = open(fnam, O_RDWR); |
| 714 | |
| 715 | /* if failed, try read-only */ |
| 716 | if (fd < 0 && errno == EACCES) |
| 717 | fd = open(fnam, O_RDONLY); |
| 718 | |
| 719 | /* if failed, try write-only */ |
| 720 | if (fd < 0 && errno == EACCES) |
| 721 | fd = open(fnam, O_WRONLY); |
| 722 | |
| 723 | /* if failed, fail */ |
| 724 | if (fd < 0) |
| 725 | return -1; |
| 726 | |
| 727 | /* if not a console, fail */ |
| 728 | if (! is_a_console(fd)) |
| 729 | { |
| 730 | close(fd); |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | /* success */ |
| 735 | return fd; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Get an fd for use with kbd/console ioctls. |
| 740 | * We try several things because opening /dev/console will fail |
| 741 | * if someone else used X (which does a chown on /dev/console). |
| 742 | * |
| 743 | * if tty_name is non-NULL, try this one instead. |
| 744 | */ |
| 745 | |
| 746 | int get_console_fd(char* tty_name) |
| 747 | { |
| 748 | int fd; |
| 749 | |
| 750 | if (tty_name) |
| 751 | { |
| 752 | if (-1 == (fd = open_a_console(tty_name))) |
| 753 | return -1; |
| 754 | else |
| 755 | return fd; |
| 756 | } |
| 757 | |
| 758 | fd = open_a_console("/dev/tty"); |
| 759 | if (fd >= 0) |
| 760 | return fd; |
| 761 | |
| 762 | fd = open_a_console("/dev/tty0"); |
| 763 | if (fd >= 0) |
| 764 | return fd; |
| 765 | |
| 766 | fd = open_a_console("/dev/console"); |
| 767 | if (fd >= 0) |
| 768 | return fd; |
| 769 | |
| 770 | for (fd = 0; fd < 3; fd++) |
| 771 | if (is_a_console(fd)) |
| 772 | return fd; |
| 773 | |
| 774 | fprintf(stderr, |
| 775 | "Couldnt get a file descriptor referring to the console\n"); |
| 776 | return -1; /* total failure */ |
| 777 | } |
| 778 | |
| 779 | |
| 780 | #endif |
| 781 | |
| 782 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 783 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 784 | |
| 785 | /* Do a case insensitive strstr() */ |
| 786 | char* stristr(char *haystack, const char *needle) |
| 787 | { |
| 788 | int len = strlen( needle ); |
| 789 | while( *haystack ) { |
| 790 | if( !strncasecmp( haystack, needle, len ) ) |
| 791 | break; |
| 792 | haystack++; |
| 793 | } |
| 794 | |
| 795 | if( !(*haystack) ) |
| 796 | haystack = NULL; |
| 797 | |
| 798 | return haystack; |
| 799 | } |
| 800 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 801 | /* This tries to find a needle in a haystack, but does so by |
| 802 | * only trying to match literal strings (look 'ma, no regexps!) |
| 803 | * This is short, sweet, and carries _very_ little baggage, |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 804 | * unlike its beefier cousin in regexp.c |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 805 | * -Erik Andersen |
| 806 | */ |
| 807 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 808 | { |
| 809 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 810 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 811 | haystack = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 812 | else |
| 813 | haystack = stristr (haystack, needle); |
| 814 | if (haystack == NULL) |
| 815 | return FALSE; |
| 816 | return TRUE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 820 | /* This performs substitutions after a string match has been found. */ |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 821 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
| 822 | { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 823 | int foundOne=0; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 824 | char *where, *slider, *slider1, *oldhayStack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 825 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 826 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 827 | where = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 828 | else |
| 829 | where = stristr (haystack, needle); |
| 830 | |
| 831 | if (strcmp(needle, newNeedle)==0) |
| 832 | return FALSE; |
| 833 | |
| 834 | oldhayStack = (char*)malloc((unsigned)(strlen(haystack))); |
| 835 | while(where!=NULL) { |
| 836 | foundOne++; |
| 837 | strcpy(oldhayStack, haystack); |
| 838 | #if 0 |
| 839 | if ( strlen(newNeedle) > strlen(needle)) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 840 | haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - |
| 841 | strlen(needle) + strlen(newNeedle))); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 842 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 843 | #endif |
| 844 | for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++); |
| 845 | *slider=0; |
| 846 | haystack=strcat(haystack, newNeedle); |
| 847 | slider1+=strlen(needle); |
| 848 | haystack = strcat(haystack, slider1); |
| 849 | where = strstr (slider, needle); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 850 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 851 | free( oldhayStack); |
| 852 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 853 | if (foundOne > 0) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 854 | return TRUE; |
| 855 | else |
| 856 | return FALSE; |
| 857 | } |
| 858 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 859 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 860 | #endif |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 861 | |
| 862 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 863 | #if defined BB_FIND |
| 864 | /* |
| 865 | * Routine to see if a text string is matched by a wildcard pattern. |
| 866 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 867 | * or if the pattern is invalid. |
| 868 | * * matches zero or more characters |
| 869 | * ? matches a single character |
| 870 | * [abc] matches 'a', 'b' or 'c' |
| 871 | * \c quotes character c |
| 872 | * Adapted from code written by Ingo Wilken, and |
| 873 | * then taken from sash, Copyright (c) 1999 by David I. Bell |
| 874 | * Permission is granted to use, distribute, or modify this source, |
| 875 | * provided that this copyright notice remains intact. |
| 876 | * Permission to distribute this code under the GPL has been granted. |
| 877 | */ |
| 878 | extern int |
| 879 | check_wildcard_match(const char* text, const char* pattern) |
| 880 | { |
| 881 | const char* retryPat; |
| 882 | const char* retryText; |
| 883 | int ch; |
| 884 | int found; |
| 885 | |
| 886 | retryPat = NULL; |
| 887 | retryText = NULL; |
| 888 | |
| 889 | while (*text || *pattern) |
| 890 | { |
| 891 | ch = *pattern++; |
| 892 | |
| 893 | switch (ch) |
| 894 | { |
| 895 | case '*': |
| 896 | retryPat = pattern; |
| 897 | retryText = text; |
| 898 | break; |
| 899 | |
| 900 | case '[': |
| 901 | found = FALSE; |
| 902 | |
| 903 | while ((ch = *pattern++) != ']') |
| 904 | { |
| 905 | if (ch == '\\') |
| 906 | ch = *pattern++; |
| 907 | |
| 908 | if (ch == '\0') |
| 909 | return FALSE; |
| 910 | |
| 911 | if (*text == ch) |
| 912 | found = TRUE; |
| 913 | } |
| 914 | |
| 915 | //if (!found) |
| 916 | if (found==TRUE) |
| 917 | { |
| 918 | pattern = retryPat; |
| 919 | text = ++retryText; |
| 920 | } |
| 921 | |
| 922 | /* fall into next case */ |
| 923 | |
| 924 | case '?': |
| 925 | if (*text++ == '\0') |
| 926 | return FALSE; |
| 927 | |
| 928 | break; |
| 929 | |
| 930 | case '\\': |
| 931 | ch = *pattern++; |
| 932 | |
| 933 | if (ch == '\0') |
| 934 | return FALSE; |
| 935 | |
| 936 | /* fall into next case */ |
| 937 | |
| 938 | default: |
| 939 | if (*text == ch) |
| 940 | { |
| 941 | if (*text) |
| 942 | text++; |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | if (*text) |
| 947 | { |
| 948 | pattern = retryPat; |
| 949 | text = ++retryText; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | return FALSE; |
| 954 | } |
| 955 | |
| 956 | if (pattern == NULL) |
| 957 | return FALSE; |
| 958 | } |
| 959 | |
| 960 | return TRUE; |
| 961 | } |
| 962 | #endif |
| 963 | |
| 964 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 965 | |
| 966 | |
| 967 | #if defined BB_DF | defined BB_MTAB |
| 968 | /* |
| 969 | * Given a block device, find the mount table entry if that block device |
| 970 | * is mounted. |
| 971 | * |
| 972 | * Given any other file (or directory), find the mount table entry for its |
| 973 | * filesystem. |
| 974 | */ |
| 975 | extern struct mntent *findMountPoint(const char *name, const char *table) |
| 976 | { |
| 977 | struct stat s; |
| 978 | dev_t mountDevice; |
| 979 | FILE *mountTable; |
| 980 | struct mntent *mountEntry; |
| 981 | |
| 982 | if (stat(name, &s) != 0) |
| 983 | return 0; |
| 984 | |
| 985 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 986 | mountDevice = s.st_rdev; |
| 987 | else |
| 988 | mountDevice = s.st_dev; |
| 989 | |
| 990 | |
| 991 | if ((mountTable = setmntent(table, "r")) == 0) |
| 992 | return 0; |
| 993 | |
| 994 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 995 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 996 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 997 | break; |
| 998 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 999 | break; |
| 1000 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 1001 | break; |
| 1002 | } |
| 1003 | endmntent(mountTable); |
| 1004 | return mountEntry; |
| 1005 | } |
| 1006 | |
| 1007 | #endif |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF ) |
| 1012 | extern void whine_if_fstab_is_missing() |
| 1013 | { |
| 1014 | struct stat statBuf; |
| 1015 | if (stat("/etc/fstab", &statBuf) < 0) |
| 1016 | fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n"); |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1021 | /* END CODE */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 1022 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1023 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |