Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines. |
| 3 | * |
| 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 21 | * Permission has been granted to redistribute this code under the GPL. |
| 22 | * |
| 23 | */ |
| 24 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 30 | #include <dirent.h> |
| 31 | #include <time.h> |
| 32 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 37 | /* volatile so gcc knows this is the enod of the line */ |
| 38 | volatile void usage(const char *usage) |
| 39 | { |
| 40 | fprintf(stderr, "Usage: %s\n", usage); |
| 41 | exit(FALSE); |
| 42 | } |
| 43 | |
| 44 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 45 | |
| 46 | #if defined (BB_CP) || defined (BB_MV) |
| 47 | /* |
| 48 | * Return TRUE if a fileName is a directory. |
| 49 | * Nonexistant files return FALSE. |
| 50 | */ |
| 51 | int isDirectory(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 53 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 55 | if (stat(name, &statBuf) < 0) |
| 56 | return FALSE; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 57 | if (S_ISDIR(statBuf.st_mode)) |
| 58 | return TRUE; |
| 59 | return(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Copy one file to another, while possibly preserving its modes, times, |
| 65 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
| 66 | * error message output. (Failure is not indicted if the attributes cannot |
| 67 | * be set.) |
| 68 | */ |
| 69 | int |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 70 | copyFile( const char *srcName, const char *destName, |
| 71 | int setModes, int followLinks) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 72 | { |
| 73 | int rfd; |
| 74 | int wfd; |
| 75 | int rcc; |
| 76 | int result; |
| 77 | char buf[BUF_SIZE]; |
| 78 | struct stat srcStatBuf; |
| 79 | struct stat dstStatBuf; |
| 80 | struct utimbuf times; |
| 81 | |
| 82 | if (followLinks == FALSE) |
| 83 | result = stat(srcName, &srcStatBuf); |
| 84 | else |
| 85 | result = lstat(srcName, &srcStatBuf); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 86 | if (result < 0) { |
| 87 | perror(srcName); |
| 88 | return FALSE; |
| 89 | } |
| 90 | |
| 91 | if (followLinks == FALSE) |
| 92 | result = stat(destName, &dstStatBuf); |
| 93 | else |
| 94 | result = lstat(destName, &dstStatBuf); |
| 95 | if (result < 0) { |
| 96 | dstStatBuf.st_ino = -1; |
| 97 | dstStatBuf.st_dev = -1; |
| 98 | } |
| 99 | |
| 100 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 101 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
| 102 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 103 | return FALSE; |
| 104 | } |
| 105 | |
| 106 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 107 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 108 | /* Make sure the directory is writable */ |
| 109 | if (mkdir(destName, 0777777 ^ umask(0))) { |
| 110 | perror(destName); |
| 111 | return (FALSE); |
| 112 | } |
| 113 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
| 114 | char *link_val; |
| 115 | int link_size; |
| 116 | |
| 117 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
| 118 | link_val = (char *) alloca(PATH_MAX + 2); |
| 119 | link_size = readlink(srcName, link_val, PATH_MAX + 1); |
| 120 | if (link_size < 0) { |
| 121 | perror(srcName); |
| 122 | return (FALSE); |
| 123 | } |
| 124 | link_val[link_size] = '\0'; |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 125 | link_size = symlink(link_val, destName); |
| 126 | if (link_size != 0) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 127 | perror(destName); |
| 128 | return (FALSE); |
| 129 | } |
| 130 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 131 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 132 | if (mkfifo(destName, 644)) { |
| 133 | perror(destName); |
| 134 | return (FALSE); |
| 135 | } |
| 136 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 137 | || S_ISSOCK (srcStatBuf.st_mode)) { |
| 138 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 139 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) { |
| 140 | perror(destName); |
| 141 | return (FALSE); |
| 142 | } |
| 143 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 144 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 145 | rfd = open(srcName, O_RDONLY); |
| 146 | if (rfd < 0) { |
| 147 | perror(srcName); |
| 148 | return FALSE; |
| 149 | } |
| 150 | |
| 151 | wfd = creat(destName, srcStatBuf.st_mode); |
| 152 | if (wfd < 0) { |
| 153 | perror(destName); |
| 154 | close(rfd); |
| 155 | return FALSE; |
| 156 | } |
| 157 | |
| 158 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 159 | if (fullWrite(wfd, buf, rcc) < 0) |
| 160 | goto error_exit; |
| 161 | } |
| 162 | if (rcc < 0) { |
| 163 | goto error_exit; |
| 164 | } |
| 165 | |
| 166 | close(rfd); |
| 167 | if (close(wfd) < 0) { |
| 168 | return FALSE; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (setModes == TRUE) { |
| 173 | //fprintf(stderr, "Setting permissions for %s\n", destName); |
| 174 | chmod(destName, srcStatBuf.st_mode); |
| 175 | if (followLinks == TRUE) |
| 176 | chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 177 | else |
| 178 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 179 | |
| 180 | times.actime = srcStatBuf.st_atime; |
| 181 | times.modtime = srcStatBuf.st_mtime; |
| 182 | |
| 183 | utime(destName, ×); |
| 184 | } |
| 185 | |
| 186 | return TRUE; |
| 187 | |
| 188 | |
| 189 | error_exit: |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 190 | perror(destName); |
| 191 | close(rfd); |
| 192 | close(wfd); |
| 193 | |
| 194 | return FALSE; |
| 195 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | |
| 199 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 200 | #ifdef BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | /* |
| 202 | * Return the standard ls-like mode string from a file mode. |
| 203 | * This is static and so is overwritten on each call. |
| 204 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 205 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 206 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 207 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 208 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 209 | strcpy(buf, "----------"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 210 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Fill in the file type. |
| 213 | */ |
| 214 | if (S_ISDIR(mode)) |
| 215 | buf[0] = 'd'; |
| 216 | if (S_ISCHR(mode)) |
| 217 | buf[0] = 'c'; |
| 218 | if (S_ISBLK(mode)) |
| 219 | buf[0] = 'b'; |
| 220 | if (S_ISFIFO(mode)) |
| 221 | buf[0] = 'p'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 222 | if (S_ISLNK(mode)) |
| 223 | buf[0] = 'l'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 224 | if (S_ISSOCK(mode)) |
| 225 | buf[0] = 's'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 226 | /* |
| 227 | * Now fill in the normal file permissions. |
| 228 | */ |
| 229 | if (mode & S_IRUSR) |
| 230 | buf[1] = 'r'; |
| 231 | if (mode & S_IWUSR) |
| 232 | buf[2] = 'w'; |
| 233 | if (mode & S_IXUSR) |
| 234 | buf[3] = 'x'; |
| 235 | if (mode & S_IRGRP) |
| 236 | buf[4] = 'r'; |
| 237 | if (mode & S_IWGRP) |
| 238 | buf[5] = 'w'; |
| 239 | if (mode & S_IXGRP) |
| 240 | buf[6] = 'x'; |
| 241 | if (mode & S_IROTH) |
| 242 | buf[7] = 'r'; |
| 243 | if (mode & S_IWOTH) |
| 244 | buf[8] = 'w'; |
| 245 | if (mode & S_IXOTH) |
| 246 | buf[9] = 'x'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 247 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 248 | /* |
| 249 | * Finally fill in magic stuff like suid and sticky text. |
| 250 | */ |
| 251 | if (mode & S_ISUID) |
| 252 | buf[3] = ((mode & S_IXUSR) ? 's' : 'S'); |
| 253 | if (mode & S_ISGID) |
| 254 | buf[6] = ((mode & S_IXGRP) ? 's' : 'S'); |
| 255 | if (mode & S_ISVTX) |
| 256 | buf[9] = ((mode & S_IXOTH) ? 't' : 'T'); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 257 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 258 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 262 | /* |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 263 | * Get the time string to be used for a file. |
| 264 | * This is down to the minute for new files, but only the date for old files. |
| 265 | * The string is returned from a static buffer, and so is overwritten for |
| 266 | * each call. |
| 267 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 268 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 269 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 270 | time_t now; |
| 271 | char *str; |
| 272 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 273 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 274 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 275 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 276 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 277 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 278 | strcpy(buf, &str[4]); |
| 279 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 280 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 281 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 282 | strcpy(&buf[7], &str[20]); |
| 283 | buf[11] = '\0'; |
| 284 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 285 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 286 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | /* |
| 291 | * Routine to see if a text string is matched by a wildcard pattern. |
| 292 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 293 | * or if the pattern is invalid. |
| 294 | * * matches zero or more characters |
| 295 | * ? matches a single character |
| 296 | * [abc] matches 'a', 'b' or 'c' |
| 297 | * \c quotes character c |
| 298 | * Adapted from code written by Ingo Wilken. |
| 299 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 300 | int match(const char *text, const char *pattern) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 301 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 302 | const char *retryPat; |
| 303 | const char *retryText; |
| 304 | int ch; |
| 305 | int found; |
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 | retryPat = NULL; |
| 308 | retryText = NULL; |
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 | while (*text || *pattern) { |
| 311 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 312 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 313 | switch (ch) { |
| 314 | case '*': |
| 315 | retryPat = pattern; |
| 316 | retryText = text; |
| 317 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 318 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 319 | case '[': |
| 320 | found = FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 321 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 322 | while ((ch = *pattern++) != ']') { |
| 323 | if (ch == '\\') |
| 324 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 325 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 326 | if (ch == '\0') |
| 327 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 328 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 329 | if (*text == ch) |
| 330 | found = TRUE; |
| 331 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 332 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 333 | if (!found) { |
| 334 | pattern = retryPat; |
| 335 | text = ++retryText; |
| 336 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 337 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 338 | /* fall into next case */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 339 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 340 | case '?': |
| 341 | if (*text++ == '\0') |
| 342 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 343 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 344 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 345 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 346 | case '\\': |
| 347 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 348 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 349 | if (ch == '\0') |
| 350 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 351 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 352 | /* fall into next case */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 353 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 354 | default: |
| 355 | if (*text == ch) { |
| 356 | if (*text) |
| 357 | text++; |
| 358 | break; |
| 359 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 360 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 361 | if (*text) { |
| 362 | pattern = retryPat; |
| 363 | text = ++retryText; |
| 364 | break; |
| 365 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 366 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 367 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 370 | if (pattern == NULL) |
| 371 | return FALSE; |
| 372 | } |
| 373 | |
| 374 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| 378 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 379 | * Write all of the supplied buffer out to a file. |
| 380 | * This does multiple writes as necessary. |
| 381 | * Returns the amount written, or -1 on an error. |
| 382 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 383 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 384 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 385 | int cc; |
| 386 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 387 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 388 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 390 | while (len > 0) { |
| 391 | cc = write(fd, buf, len); |
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 | if (cc < 0) |
| 394 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 395 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 396 | buf += cc; |
| 397 | total += cc; |
| 398 | len -= cc; |
| 399 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 400 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 401 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | /* |
| 406 | * Read all of the supplied buffer from a file. |
| 407 | * This does multiple reads as necessary. |
| 408 | * Returns the amount read, or -1 on an error. |
| 409 | * A short read is returned on an end of file. |
| 410 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 411 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 412 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 413 | int cc; |
| 414 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 415 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 416 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 417 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 418 | while (len > 0) { |
| 419 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 420 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 421 | if (cc < 0) |
| 422 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 424 | if (cc == 0) |
| 425 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 426 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 427 | buf += cc; |
| 428 | total += cc; |
| 429 | len -= cc; |
| 430 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 431 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 432 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 433 | } |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 434 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 435 | |
| 436 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 437 | #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] | 438 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 439 | * Walk down all the directories under the specified |
| 440 | * location, and do something (something specified |
| 441 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 442 | * |
| 443 | * TODO: check if ftw(3) can replace this to reduce code size... |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 444 | */ |
| 445 | int |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 446 | recursiveAction(const char *fileName, int recurse, int followLinks, int delayDirAction, |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 447 | int (*fileAction) (const char *fileName, struct stat* statbuf), |
| 448 | int (*dirAction) (const char *fileName, struct stat* statbuf)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 449 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 450 | int status; |
| 451 | struct stat statbuf; |
| 452 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 453 | |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 454 | if (followLinks == FALSE) |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 455 | status = stat(fileName, &statbuf); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 456 | else |
| 457 | status = lstat(fileName, &statbuf); |
| 458 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 459 | if (status < 0) { |
| 460 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 461 | return (FALSE); |
| 462 | } |
| 463 | |
| 464 | if (recurse == FALSE) { |
| 465 | if (S_ISDIR(statbuf.st_mode)) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 466 | if (dirAction != NULL) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 467 | return (dirAction(fileName, &statbuf)); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 468 | else |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 469 | return (TRUE); |
| 470 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 473 | if (S_ISDIR(statbuf.st_mode)) { |
| 474 | DIR *dir; |
| 475 | dir = opendir(fileName); |
| 476 | if (!dir) { |
| 477 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 478 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 479 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 480 | if (dirAction != NULL && delayDirAction == FALSE) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 481 | status = dirAction(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 482 | if (status == FALSE) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 483 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 484 | return (FALSE); |
| 485 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 486 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 487 | while ((next = readdir(dir)) != NULL) { |
| 488 | char nextFile[NAME_MAX]; |
| 489 | if ((strcmp(next->d_name, "..") == 0) |
| 490 | || (strcmp(next->d_name, ".") == 0)) { |
| 491 | continue; |
| 492 | } |
| 493 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 494 | status = |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 495 | recursiveAction(nextFile, TRUE, followLinks, delayDirAction, |
| 496 | fileAction, dirAction); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 497 | if (status < 0) { |
| 498 | closedir(dir); |
| 499 | return (FALSE); |
| 500 | } |
| 501 | } |
| 502 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 503 | if (status < 0) { |
| 504 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 505 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 506 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 507 | if (dirAction != NULL && delayDirAction == TRUE) { |
| 508 | status = dirAction(fileName, &statbuf); |
| 509 | if (status == FALSE) { |
| 510 | perror(fileName); |
| 511 | return (FALSE); |
| 512 | } |
| 513 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 514 | } else { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 515 | if (fileAction == NULL) |
| 516 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 517 | else |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 518 | return (fileAction(fileName, &statbuf)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 519 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 520 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 523 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 524 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 525 | |
| 526 | |
| 527 | #if defined (BB_TAR) || defined (BB_MKDIR) |
| 528 | /* |
| 529 | * Attempt to create the directories along the specified path, except for |
| 530 | * the final component. The mode is given for the final directory only, |
| 531 | * while all previous ones get default protections. Errors are not reported |
| 532 | * here, as failures to restore files can be reported later. |
| 533 | */ |
| 534 | extern void createPath (const char *name, int mode) |
| 535 | { |
| 536 | char *cp; |
| 537 | char *cpOld; |
| 538 | char buf[NAME_MAX]; |
| 539 | |
| 540 | strcpy (buf, name); |
| 541 | |
| 542 | cp = strchr (buf, '/'); |
| 543 | |
| 544 | while (cp) { |
| 545 | cpOld = cp; |
| 546 | cp = strchr (cp + 1, '/'); |
| 547 | |
| 548 | *cpOld = '\0'; |
| 549 | |
| 550 | if (mkdir (buf, cp ? 0777 : mode) == 0) |
| 551 | printf ("Directory \"%s\" created\n", buf); |
| 552 | |
| 553 | *cpOld = '/'; |
| 554 | } |
| 555 | } |
| 556 | #endif |
| 557 | |
| 558 | |
| 559 | |
| 560 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 561 | /* [ugoa]{+|-|=}[rwxst] */ |
| 562 | |
| 563 | |
| 564 | |
| 565 | extern int |
| 566 | parse_mode( const char* s, mode_t* theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 567 | { |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 568 | mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 569 | mode_t orMode = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 570 | mode_t mode = 0; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 571 | mode_t groups = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 572 | char type; |
| 573 | char c; |
| 574 | |
| 575 | do { |
| 576 | for ( ; ; ) { |
| 577 | switch ( c = *s++ ) { |
| 578 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 579 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 580 | case 'u': |
| 581 | groups |= S_ISUID|S_IRWXU; |
| 582 | continue; |
| 583 | case 'g': |
| 584 | groups |= S_ISGID|S_IRWXG; |
| 585 | continue; |
| 586 | case 'o': |
| 587 | groups |= S_IRWXO; |
| 588 | continue; |
| 589 | case 'a': |
| 590 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 591 | continue; |
| 592 | case '+': |
| 593 | case '=': |
| 594 | case '-': |
| 595 | type = c; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 596 | if ( groups == 0 ) /* The default is "all" */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 597 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 598 | break; |
| 599 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 600 | if ( isdigit(c) && c >= '0' && c <= '7' && mode == 0 && groups == 0 ) { |
| 601 | andMode = 0; |
| 602 | orMode = strtol(--s, NULL, 8); |
| 603 | *theMode &= andMode; |
| 604 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 605 | return (TRUE); |
| 606 | } |
| 607 | else |
| 608 | return (FALSE); |
| 609 | } |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | while ( (c = *s++) != '\0' ) { |
| 614 | switch ( c ) { |
| 615 | case ',': |
| 616 | break; |
| 617 | case 'r': |
| 618 | mode |= S_IRUSR|S_IRGRP|S_IROTH; |
| 619 | continue; |
| 620 | case 'w': |
| 621 | mode |= S_IWUSR|S_IWGRP|S_IWOTH; |
| 622 | continue; |
| 623 | case 'x': |
| 624 | mode |= S_IXUSR|S_IXGRP|S_IXOTH; |
| 625 | continue; |
| 626 | case 's': |
| 627 | mode |= S_IXGRP|S_ISUID|S_ISGID; |
| 628 | continue; |
| 629 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 630 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 631 | continue; |
| 632 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 633 | *theMode &= andMode; |
| 634 | *theMode |= orMode; |
| 635 | return( TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 636 | } |
| 637 | break; |
| 638 | } |
| 639 | switch ( type ) { |
| 640 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 641 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 642 | /* fall through */ |
| 643 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 644 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 645 | break; |
| 646 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 647 | andMode &= ~(mode & groups); |
| 648 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | } while ( c == ',' ); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 652 | *theMode &= andMode; |
| 653 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 654 | return (TRUE); |
| 655 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 656 | |
| 657 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 658 | #endif |
| 659 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 660 | |
| 661 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 662 | /* END CODE */ |