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 | #ifdef BB_MTAB |
| 40 | const char mtab_file[] = "/etc/mtab"; |
| 41 | #else |
| 42 | #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF |
| 43 | const char mtab_file[] = "/proc/mounts"; |
| 44 | #endif |
| 45 | #endif |
| 46 | |
| 47 | |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 48 | /* volatile so gcc knows this is the enod of the line */ |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 49 | extern void usage(const char *usage) |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 50 | { |
Eric Andersen | a07f0b0 | 1999-10-22 19:49:09 +0000 | [diff] [blame] | 51 | 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] | 52 | fprintf(stderr, "Usage: %s\n", usage); |
| 53 | exit(FALSE); |
| 54 | } |
| 55 | |
| 56 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 57 | #if defined (BB_INIT) || defined (BB_PS) |
| 58 | |
| 59 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, |
| 60 | * so, for example, to check if the kernel is greater than 2.2.11: |
| 61 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
| 62 | */ |
| 63 | int |
| 64 | get_kernel_revision() |
| 65 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 66 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 67 | int major=0, minor=0, patch=0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 68 | char* filename="/proc/sys/kernel/osrelease"; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 69 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 70 | file = fopen(filename,"r"); |
| 71 | if (file == NULL) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 72 | /* bummer, /proc must not be mounted... */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 73 | return( 0); |
| 74 | } |
| 75 | fscanf(file,"%d.%d.%d",&major,&minor,&patch); |
| 76 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 77 | return major*65536 + minor*256 + patch; |
| 78 | } |
| 79 | |
| 80 | #endif |
| 81 | |
| 82 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 83 | |
| 84 | #if defined (BB_CP) || defined (BB_MV) |
| 85 | /* |
| 86 | * Return TRUE if a fileName is a directory. |
| 87 | * Nonexistant files return FALSE. |
| 88 | */ |
| 89 | int isDirectory(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 91 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 93 | if (stat(name, &statBuf) < 0) |
| 94 | return FALSE; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 95 | if (S_ISDIR(statBuf.st_mode)) |
| 96 | return TRUE; |
| 97 | return(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Copy one file to another, while possibly preserving its modes, times, |
| 103 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
| 104 | * error message output. (Failure is not indicted if the attributes cannot |
| 105 | * be set.) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 106 | * -Erik Andersen |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 107 | */ |
| 108 | int |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 109 | copyFile( const char *srcName, const char *destName, |
| 110 | int setModes, int followLinks) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 111 | { |
| 112 | int rfd; |
| 113 | int wfd; |
| 114 | int rcc; |
| 115 | int result; |
| 116 | char buf[BUF_SIZE]; |
| 117 | struct stat srcStatBuf; |
| 118 | struct stat dstStatBuf; |
| 119 | struct utimbuf times; |
| 120 | |
| 121 | if (followLinks == FALSE) |
| 122 | result = stat(srcName, &srcStatBuf); |
| 123 | else |
| 124 | result = lstat(srcName, &srcStatBuf); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 125 | if (result < 0) { |
| 126 | perror(srcName); |
| 127 | return FALSE; |
| 128 | } |
| 129 | |
| 130 | if (followLinks == FALSE) |
| 131 | result = stat(destName, &dstStatBuf); |
| 132 | else |
| 133 | result = lstat(destName, &dstStatBuf); |
| 134 | if (result < 0) { |
| 135 | dstStatBuf.st_ino = -1; |
| 136 | dstStatBuf.st_dev = -1; |
| 137 | } |
| 138 | |
| 139 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 140 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
| 141 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 142 | return FALSE; |
| 143 | } |
| 144 | |
| 145 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 146 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 147 | /* Make sure the directory is writable */ |
| 148 | if (mkdir(destName, 0777777 ^ umask(0))) { |
| 149 | perror(destName); |
| 150 | return (FALSE); |
| 151 | } |
| 152 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
| 153 | char *link_val; |
| 154 | int link_size; |
| 155 | |
| 156 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
| 157 | link_val = (char *) alloca(PATH_MAX + 2); |
| 158 | link_size = readlink(srcName, link_val, PATH_MAX + 1); |
| 159 | if (link_size < 0) { |
| 160 | perror(srcName); |
| 161 | return (FALSE); |
| 162 | } |
| 163 | link_val[link_size] = '\0'; |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 164 | link_size = symlink(link_val, destName); |
| 165 | if (link_size != 0) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 166 | perror(destName); |
| 167 | return (FALSE); |
| 168 | } |
| 169 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 170 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 171 | if (mkfifo(destName, 644)) { |
| 172 | perror(destName); |
| 173 | return (FALSE); |
| 174 | } |
| 175 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 176 | || S_ISSOCK (srcStatBuf.st_mode)) { |
| 177 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 178 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) { |
| 179 | perror(destName); |
| 180 | return (FALSE); |
| 181 | } |
| 182 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 183 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 184 | rfd = open(srcName, O_RDONLY); |
| 185 | if (rfd < 0) { |
| 186 | perror(srcName); |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | wfd = creat(destName, srcStatBuf.st_mode); |
| 191 | if (wfd < 0) { |
| 192 | perror(destName); |
| 193 | close(rfd); |
| 194 | return FALSE; |
| 195 | } |
| 196 | |
| 197 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 198 | if (fullWrite(wfd, buf, rcc) < 0) |
| 199 | goto error_exit; |
| 200 | } |
| 201 | if (rcc < 0) { |
| 202 | goto error_exit; |
| 203 | } |
| 204 | |
| 205 | close(rfd); |
| 206 | if (close(wfd) < 0) { |
| 207 | return FALSE; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (setModes == TRUE) { |
| 212 | //fprintf(stderr, "Setting permissions for %s\n", destName); |
| 213 | chmod(destName, srcStatBuf.st_mode); |
| 214 | if (followLinks == TRUE) |
| 215 | chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 216 | else |
| 217 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 218 | |
| 219 | times.actime = srcStatBuf.st_atime; |
| 220 | times.modtime = srcStatBuf.st_mtime; |
| 221 | |
| 222 | utime(destName, ×); |
| 223 | } |
| 224 | |
| 225 | return TRUE; |
| 226 | |
| 227 | |
| 228 | error_exit: |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 229 | perror(destName); |
| 230 | close(rfd); |
| 231 | close(wfd); |
| 232 | |
| 233 | return FALSE; |
| 234 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | |
| 238 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 239 | #if defined BB_TAR || defined BB_LS |
| 240 | |
| 241 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 242 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 243 | |
| 244 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ |
| 245 | static const mode_t SBIT[] = { |
| 246 | 0, 0, S_ISUID, |
| 247 | 0, 0, S_ISGID, |
| 248 | 0, 0, S_ISVTX |
| 249 | }; |
| 250 | |
| 251 | /* The 9 mode bits to test */ |
| 252 | static const mode_t MBIT[] = { |
| 253 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 254 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 255 | S_IROTH, S_IWOTH, S_IXOTH |
| 256 | }; |
| 257 | |
| 258 | #define MODE1 "rwxrwxrwx" |
| 259 | #define MODE0 "---------" |
| 260 | #define SMODE1 "..s..s..t" |
| 261 | #define SMODE0 "..S..S..T" |
| 262 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 263 | /* |
| 264 | * Return the standard ls-like mode string from a file mode. |
| 265 | * This is static and so is overwritten on each call. |
| 266 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 267 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 268 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 269 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 270 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 271 | int i; |
| 272 | buf[0] = TYPECHAR(mode); |
| 273 | for (i=0; i<9; i++) { |
| 274 | if (mode & SBIT[i]) |
| 275 | buf[i+1] = (mode & MBIT[i])? |
| 276 | SMODE1[i] : SMODE0[i]; |
| 277 | else |
| 278 | buf[i+1] = (mode & MBIT[i])? |
| 279 | MODE1[i] : MODE0[i]; |
| 280 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 281 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 282 | } |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 283 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 284 | |
| 285 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 286 | #ifdef BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | /* |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 288 | * Return the standard ls-like time string from a time_t |
| 289 | * This is static and so is overwritten on each call. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 290 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 291 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 292 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 293 | time_t now; |
| 294 | char *str; |
| 295 | static char buf[26]; |
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 | time(&now); |
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 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 300 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 301 | strcpy(buf, &str[4]); |
| 302 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 303 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 304 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 305 | strcpy(&buf[7], &str[20]); |
| 306 | buf[11] = '\0'; |
| 307 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 308 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 309 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 312 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 313 | * Write all of the supplied buffer out to a file. |
| 314 | * This does multiple writes as necessary. |
| 315 | * Returns the amount written, or -1 on an error. |
| 316 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 317 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 318 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 319 | int cc; |
| 320 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 321 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 322 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 323 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 324 | while (len > 0) { |
| 325 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 326 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 327 | if (cc < 0) |
| 328 | return -1; |
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 | buf += cc; |
| 331 | total += cc; |
| 332 | len -= cc; |
| 333 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 334 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 335 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | * Read all of the supplied buffer from a file. |
| 341 | * This does multiple reads as necessary. |
| 342 | * Returns the amount read, or -1 on an error. |
| 343 | * A short read is returned on an end of file. |
| 344 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 345 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 346 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 347 | int cc; |
| 348 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 349 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 350 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 351 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 352 | while (len > 0) { |
| 353 | cc = read(fd, buf, 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 | if (cc < 0) |
| 356 | return -1; |
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 | if (cc == 0) |
| 359 | break; |
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 | buf += cc; |
| 362 | total += cc; |
| 363 | len -= cc; |
| 364 | } |
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 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 367 | } |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 368 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 369 | |
| 370 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 371 | #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] | 372 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 373 | * Walk down all the directories under the specified |
| 374 | * location, and do something (something specified |
| 375 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 376 | * |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 377 | * Unfortunatly, while nftw(3) could replace this and reduce |
| 378 | * 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] | 379 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 380 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 381 | */ |
| 382 | int |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 383 | recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst, |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 384 | int (*fileAction) (const char *fileName, struct stat* statbuf), |
| 385 | int (*dirAction) (const char *fileName, struct stat* statbuf)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 386 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 387 | int status; |
| 388 | struct stat statbuf; |
| 389 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 390 | |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 391 | if (followLinks == TRUE) |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 392 | status = stat(fileName, &statbuf); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 393 | else |
| 394 | status = lstat(fileName, &statbuf); |
| 395 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 396 | if (status < 0) { |
| 397 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 398 | return (FALSE); |
| 399 | } |
| 400 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 401 | if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) { |
| 402 | if (fileAction == NULL) |
| 403 | return (TRUE); |
| 404 | else |
| 405 | return (fileAction(fileName, &statbuf)); |
| 406 | } |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 407 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 408 | if (recurse == FALSE) { |
| 409 | if (S_ISDIR(statbuf.st_mode)) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 410 | if (dirAction != NULL) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 411 | return (dirAction(fileName, &statbuf)); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 412 | else |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 413 | return (TRUE); |
| 414 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 417 | if (S_ISDIR(statbuf.st_mode)) { |
| 418 | DIR *dir; |
| 419 | dir = opendir(fileName); |
| 420 | if (!dir) { |
| 421 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 422 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 424 | if (dirAction != NULL && depthFirst == FALSE) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 425 | status = dirAction(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 426 | if (status == FALSE) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 427 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 428 | return (FALSE); |
| 429 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 430 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 431 | while ((next = readdir(dir)) != NULL) { |
| 432 | char nextFile[NAME_MAX]; |
| 433 | if ((strcmp(next->d_name, "..") == 0) |
| 434 | || (strcmp(next->d_name, ".") == 0)) { |
| 435 | continue; |
| 436 | } |
| 437 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 438 | status = |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 439 | recursiveAction(nextFile, TRUE, followLinks, depthFirst, |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 440 | fileAction, dirAction); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 441 | if (status < 0) { |
| 442 | closedir(dir); |
| 443 | return (FALSE); |
| 444 | } |
| 445 | } |
| 446 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 447 | if (status < 0) { |
| 448 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 449 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 450 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 451 | if (dirAction != NULL && depthFirst == TRUE) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 452 | status = dirAction(fileName, &statbuf); |
| 453 | if (status == FALSE) { |
| 454 | perror(fileName); |
| 455 | return (FALSE); |
| 456 | } |
| 457 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 458 | } else { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 459 | if (fileAction == NULL) |
| 460 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 461 | else |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 462 | return (fileAction(fileName, &statbuf)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 463 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 464 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 467 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 468 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 469 | |
| 470 | |
| 471 | #if defined (BB_TAR) || defined (BB_MKDIR) |
| 472 | /* |
| 473 | * Attempt to create the directories along the specified path, except for |
| 474 | * the final component. The mode is given for the final directory only, |
| 475 | * while all previous ones get default protections. Errors are not reported |
| 476 | * here, as failures to restore files can be reported later. |
| 477 | */ |
| 478 | extern void createPath (const char *name, int mode) |
| 479 | { |
| 480 | char *cp; |
| 481 | char *cpOld; |
| 482 | char buf[NAME_MAX]; |
| 483 | |
| 484 | strcpy (buf, name); |
| 485 | |
| 486 | cp = strchr (buf, '/'); |
| 487 | |
| 488 | while (cp) { |
| 489 | cpOld = cp; |
| 490 | cp = strchr (cp + 1, '/'); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 491 | *cpOld = '\0'; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 492 | mkdir (buf, cp ? 0777 : mode); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 493 | *cpOld = '/'; |
| 494 | } |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | |
| 499 | |
| 500 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 501 | /* [ugoa]{+|-|=}[rwxst] */ |
| 502 | |
| 503 | |
| 504 | |
| 505 | extern int |
| 506 | parse_mode( const char* s, mode_t* theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 507 | { |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 508 | mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 509 | mode_t orMode = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 510 | mode_t mode = 0; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 511 | mode_t groups = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 512 | char type; |
| 513 | char c; |
| 514 | |
| 515 | do { |
| 516 | for ( ; ; ) { |
| 517 | switch ( c = *s++ ) { |
| 518 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 519 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 520 | case 'u': |
| 521 | groups |= S_ISUID|S_IRWXU; |
| 522 | continue; |
| 523 | case 'g': |
| 524 | groups |= S_ISGID|S_IRWXG; |
| 525 | continue; |
| 526 | case 'o': |
| 527 | groups |= S_IRWXO; |
| 528 | continue; |
| 529 | case 'a': |
| 530 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 531 | continue; |
| 532 | case '+': |
| 533 | case '=': |
| 534 | case '-': |
| 535 | type = c; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 536 | if ( groups == 0 ) /* The default is "all" */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 537 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 538 | break; |
| 539 | default: |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 540 | if ( isdigit(c) && c >= '0' && c <= '7' && |
| 541 | mode == 0 && groups == 0 ) { |
| 542 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 543 | return (TRUE); |
| 544 | } |
| 545 | else |
| 546 | return (FALSE); |
| 547 | } |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | while ( (c = *s++) != '\0' ) { |
| 552 | switch ( c ) { |
| 553 | case ',': |
| 554 | break; |
| 555 | case 'r': |
| 556 | mode |= S_IRUSR|S_IRGRP|S_IROTH; |
| 557 | continue; |
| 558 | case 'w': |
| 559 | mode |= S_IWUSR|S_IWGRP|S_IWOTH; |
| 560 | continue; |
| 561 | case 'x': |
| 562 | mode |= S_IXUSR|S_IXGRP|S_IXOTH; |
| 563 | continue; |
| 564 | case 's': |
| 565 | mode |= S_IXGRP|S_ISUID|S_ISGID; |
| 566 | continue; |
| 567 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 568 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 569 | continue; |
| 570 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 571 | *theMode &= andMode; |
| 572 | *theMode |= orMode; |
| 573 | return( TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 574 | } |
| 575 | break; |
| 576 | } |
| 577 | switch ( type ) { |
| 578 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 579 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 580 | /* fall through */ |
| 581 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 582 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 583 | break; |
| 584 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 585 | andMode &= ~(mode & groups); |
| 586 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | } while ( c == ',' ); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 590 | *theMode &= andMode; |
| 591 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 592 | return (TRUE); |
| 593 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 594 | |
| 595 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 596 | #endif |
| 597 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 598 | |
| 599 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS) |
| 605 | |
| 606 | /* Use this to avoid needing the glibc NSS stuff |
| 607 | * This uses storage buf to hold things. |
| 608 | * */ |
| 609 | uid_t |
| 610 | my_getid(const char *filename, char *name, uid_t id) |
| 611 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 612 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 613 | char *rname, *start, *end, buf[128]; |
| 614 | uid_t rid; |
| 615 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 616 | file=fopen(filename,"r"); |
| 617 | if (file == NULL) { |
| 618 | perror(filename); |
| 619 | return (-1); |
| 620 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 621 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 622 | while (fgets (buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 623 | if (buf[0] == '#') |
| 624 | continue; |
| 625 | |
| 626 | start = buf; |
| 627 | end = strchr (start, ':'); |
| 628 | if (end == NULL) |
| 629 | continue; |
| 630 | *end = '\0'; |
| 631 | rname = start; |
| 632 | |
| 633 | start = end + 1; |
| 634 | end = strchr (start, ':'); |
| 635 | if (end == NULL) |
| 636 | continue; |
| 637 | |
| 638 | start = end + 1; |
| 639 | rid = (uid_t) strtol (start, &end, 10); |
| 640 | if (end == start) |
| 641 | continue; |
| 642 | |
| 643 | if (name) { |
| 644 | if (0 == strcmp(rname, name)) |
| 645 | return( rid); |
| 646 | } |
| 647 | if ( id != -1 && id == rid ) { |
| 648 | strncpy(name, rname, 8); |
| 649 | return( TRUE); |
| 650 | } |
| 651 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 652 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 653 | return (-1); |
| 654 | } |
| 655 | |
| 656 | uid_t |
| 657 | my_getpwnam(char *name) |
| 658 | { |
| 659 | return my_getid("/etc/passwd", name, -1); |
| 660 | } |
| 661 | |
| 662 | gid_t |
| 663 | my_getgrnam(char *name) |
| 664 | { |
| 665 | return my_getid("/etc/group", name, -1); |
| 666 | } |
| 667 | |
| 668 | void |
| 669 | my_getpwuid(char* name, uid_t uid) |
| 670 | { |
| 671 | my_getid("/etc/passwd", name, uid); |
| 672 | } |
| 673 | |
| 674 | void |
| 675 | my_getgrgid(char* group, gid_t gid) |
| 676 | { |
| 677 | my_getid("/etc/group", group, gid); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | #endif |
| 682 | |
| 683 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 684 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 685 | |
| 686 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
| 687 | |
| 688 | |
| 689 | #include <linux/kd.h> |
| 690 | #include <sys/ioctl.h> |
| 691 | |
| 692 | int is_a_console(int fd) |
| 693 | { |
| 694 | char arg; |
| 695 | |
| 696 | arg = 0; |
| 697 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 698 | && ((arg == KB_101) || (arg == KB_84))); |
| 699 | } |
| 700 | |
| 701 | static int open_a_console(char *fnam) |
| 702 | { |
| 703 | int fd; |
| 704 | |
| 705 | /* try read-only */ |
| 706 | fd = open(fnam, O_RDWR); |
| 707 | |
| 708 | /* if failed, try read-only */ |
| 709 | if (fd < 0 && errno == EACCES) |
| 710 | fd = open(fnam, O_RDONLY); |
| 711 | |
| 712 | /* if failed, try write-only */ |
| 713 | if (fd < 0 && errno == EACCES) |
| 714 | fd = open(fnam, O_WRONLY); |
| 715 | |
| 716 | /* if failed, fail */ |
| 717 | if (fd < 0) |
| 718 | return -1; |
| 719 | |
| 720 | /* if not a console, fail */ |
| 721 | if (! is_a_console(fd)) |
| 722 | { |
| 723 | close(fd); |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | /* success */ |
| 728 | return fd; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Get an fd for use with kbd/console ioctls. |
| 733 | * We try several things because opening /dev/console will fail |
| 734 | * if someone else used X (which does a chown on /dev/console). |
| 735 | * |
| 736 | * if tty_name is non-NULL, try this one instead. |
| 737 | */ |
| 738 | |
| 739 | int get_console_fd(char* tty_name) |
| 740 | { |
| 741 | int fd; |
| 742 | |
| 743 | if (tty_name) |
| 744 | { |
| 745 | if (-1 == (fd = open_a_console(tty_name))) |
| 746 | return -1; |
| 747 | else |
| 748 | return fd; |
| 749 | } |
| 750 | |
| 751 | fd = open_a_console("/dev/tty"); |
| 752 | if (fd >= 0) |
| 753 | return fd; |
| 754 | |
| 755 | fd = open_a_console("/dev/tty0"); |
| 756 | if (fd >= 0) |
| 757 | return fd; |
| 758 | |
| 759 | fd = open_a_console("/dev/console"); |
| 760 | if (fd >= 0) |
| 761 | return fd; |
| 762 | |
| 763 | for (fd = 0; fd < 3; fd++) |
| 764 | if (is_a_console(fd)) |
| 765 | return fd; |
| 766 | |
| 767 | fprintf(stderr, |
| 768 | "Couldnt get a file descriptor referring to the console\n"); |
| 769 | return -1; /* total failure */ |
| 770 | } |
| 771 | |
| 772 | |
| 773 | #endif |
| 774 | |
| 775 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 776 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 777 | |
| 778 | /* Do a case insensitive strstr() */ |
| 779 | char* stristr(char *haystack, const char *needle) |
| 780 | { |
| 781 | int len = strlen( needle ); |
| 782 | while( *haystack ) { |
| 783 | if( !strncasecmp( haystack, needle, len ) ) |
| 784 | break; |
| 785 | haystack++; |
| 786 | } |
| 787 | |
| 788 | if( !(*haystack) ) |
| 789 | haystack = NULL; |
| 790 | |
| 791 | return haystack; |
| 792 | } |
| 793 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 794 | /* This tries to find a needle in a haystack, but does so by |
| 795 | * only trying to match literal strings (look 'ma, no regexps!) |
| 796 | * This is short, sweet, and carries _very_ little baggage, |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 797 | * unlike its beefier cousin in regexp.c |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 798 | * -Erik Andersen |
| 799 | */ |
| 800 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 801 | { |
| 802 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 803 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 804 | haystack = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 805 | else |
| 806 | haystack = stristr (haystack, needle); |
| 807 | if (haystack == NULL) |
| 808 | return FALSE; |
| 809 | return TRUE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 813 | /* This performs substitutions after a string match has been found. */ |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 814 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
| 815 | { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 816 | int foundOne=0; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 817 | char *where, *slider, *slider1, *oldhayStack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 818 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 819 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 820 | where = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 821 | else |
| 822 | where = stristr (haystack, needle); |
| 823 | |
| 824 | if (strcmp(needle, newNeedle)==0) |
| 825 | return FALSE; |
| 826 | |
| 827 | oldhayStack = (char*)malloc((unsigned)(strlen(haystack))); |
| 828 | while(where!=NULL) { |
| 829 | foundOne++; |
| 830 | strcpy(oldhayStack, haystack); |
| 831 | #if 0 |
| 832 | if ( strlen(newNeedle) > strlen(needle)) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 833 | haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - |
| 834 | strlen(needle) + strlen(newNeedle))); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 835 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 836 | #endif |
| 837 | for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++); |
| 838 | *slider=0; |
| 839 | haystack=strcat(haystack, newNeedle); |
| 840 | slider1+=strlen(needle); |
| 841 | haystack = strcat(haystack, slider1); |
| 842 | where = strstr (slider, needle); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 843 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 844 | free( oldhayStack); |
| 845 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 846 | if (foundOne > 0) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 847 | return TRUE; |
| 848 | else |
| 849 | return FALSE; |
| 850 | } |
| 851 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 852 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 853 | #endif |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | #if defined BB_DF | defined BB_MTAB |
| 859 | /* |
| 860 | * Given a block device, find the mount table entry if that block device |
| 861 | * is mounted. |
| 862 | * |
| 863 | * Given any other file (or directory), find the mount table entry for its |
| 864 | * filesystem. |
| 865 | */ |
| 866 | extern struct mntent *findMountPoint(const char *name, const char *table) |
| 867 | { |
| 868 | struct stat s; |
| 869 | dev_t mountDevice; |
| 870 | FILE *mountTable; |
| 871 | struct mntent *mountEntry; |
| 872 | |
| 873 | if (stat(name, &s) != 0) |
| 874 | return 0; |
| 875 | |
| 876 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 877 | mountDevice = s.st_rdev; |
| 878 | else |
| 879 | mountDevice = s.st_dev; |
| 880 | |
| 881 | |
| 882 | if ((mountTable = setmntent(table, "r")) == 0) |
| 883 | return 0; |
| 884 | |
| 885 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 886 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 887 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 888 | break; |
| 889 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 890 | break; |
| 891 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 892 | break; |
| 893 | } |
| 894 | endmntent(mountTable); |
| 895 | return mountEntry; |
| 896 | } |
| 897 | |
| 898 | #endif |
| 899 | |
| 900 | |
| 901 | |
| 902 | #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF ) |
| 903 | extern void whine_if_fstab_is_missing() |
| 904 | { |
| 905 | struct stat statBuf; |
| 906 | if (stat("/etc/fstab", &statBuf) < 0) |
| 907 | fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n"); |
| 908 | } |
| 909 | #endif |
| 910 | |
| 911 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 912 | /* END CODE */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 913 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 914 | |