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