Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1999 by David I. Bell |
| 3 | * Permission is granted to use, distribute, or modify this source, |
| 4 | * provided that this copyright notice remains intact. |
| 5 | * |
| 6 | * The "tar" command, taken from sash. |
| 7 | * This allows creation, extraction, and listing of tar files. |
| 8 | * |
| 9 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 10 | * Modified for busybox by Erik Andersen <andersee@debian.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | |
| 14 | #include "internal.h" |
| 15 | |
| 16 | #ifdef BB_TAR |
| 17 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 18 | const char tar_usage[] = |
| 19 | "Create, extract, or list files from a TAR file\n\n" |
| 20 | "usage: tar -[cxtvOf] [tarFileName] [FILE] ...\n" |
| 21 | "\tc=create, x=extract, t=list contents, v=verbose,\n" |
| 22 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <dirent.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <signal.h> |
| 31 | #include <time.h> |
| 32 | |
| 33 | /* |
| 34 | * Tar file constants. |
| 35 | */ |
| 36 | #define TAR_BLOCK_SIZE 512 |
| 37 | #define TAR_NAME_SIZE 100 |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * The POSIX (and basic GNU) tar header format. |
| 42 | * This structure is always embedded in a TAR_BLOCK_SIZE sized block |
| 43 | * with zero padding. We only process this information minimally. |
| 44 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 45 | typedef struct { |
| 46 | char name[TAR_NAME_SIZE]; |
| 47 | char mode[8]; |
| 48 | char uid[8]; |
| 49 | char gid[8]; |
| 50 | char size[12]; |
| 51 | char mtime[12]; |
| 52 | char checkSum[8]; |
| 53 | char typeFlag; |
| 54 | char linkName[TAR_NAME_SIZE]; |
| 55 | char magic[6]; |
| 56 | char version[2]; |
| 57 | char uname[32]; |
| 58 | char gname[32]; |
| 59 | char devMajor[8]; |
| 60 | char devMinor[8]; |
| 61 | char prefix[155]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | } TarHeader; |
| 63 | |
| 64 | #define TAR_MAGIC "ustar" |
| 65 | #define TAR_VERSION "00" |
| 66 | |
| 67 | #define TAR_TYPE_REGULAR '0' |
| 68 | #define TAR_TYPE_HARD_LINK '1' |
| 69 | #define TAR_TYPE_SOFT_LINK '2' |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * Static data. |
| 74 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 75 | static int listFlag; |
| 76 | static int extractFlag; |
| 77 | static int createFlag; |
| 78 | static int verboseFlag; |
| 79 | static int tostdoutFlag; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 81 | static int inHeader; |
| 82 | static int badHeader; |
| 83 | static int errorFlag; |
| 84 | static int skipFileFlag; |
| 85 | static int warnedRoot; |
| 86 | static int eofFlag; |
| 87 | static long dataCc; |
| 88 | static int outFd; |
| 89 | static char outName[TAR_NAME_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | /* |
| 93 | * Static data associated with the tar file. |
| 94 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 95 | static const char *tarName; |
| 96 | static int tarFd; |
| 97 | static dev_t tarDev; |
| 98 | static ino_t tarInode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | /* |
| 102 | * Local procedures to restore files from a tar file. |
| 103 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 104 | static void readTarFile (int fileCount, char **fileTable); |
| 105 | static void readData (const char *cp, int count); |
| 106 | static void createPath (const char *name, int mode); |
| 107 | static long getOctal (const char *cp, int len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 109 | static void readHeader (const TarHeader * hp, |
| 110 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | /* |
| 114 | * Local procedures to save files into a tar file. |
| 115 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 116 | static void saveFile (const char *fileName, int seeLinks); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 118 | static void saveRegularFile (const char *fileName, |
| 119 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 120 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 121 | static void saveDirectory (const char *fileName, |
| 122 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 124 | static int wantFileName (const char *fileName, |
| 125 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 127 | static void writeHeader (const char *fileName, const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 129 | static void writeTarFile (int fileCount, char **fileTable); |
| 130 | static void writeTarBlock (const char *buf, int len); |
| 131 | static int putOctal (char *cp, int len, long value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 132 | |
| 133 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 134 | extern int tar_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 135 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 136 | const char *options; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 138 | argc--; |
| 139 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 141 | if (argc < 1) { |
| 142 | fprintf (stderr, "%s", tar_usage); |
| 143 | exit (FALSE); |
| 144 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | |
| 146 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 147 | errorFlag = FALSE; |
| 148 | extractFlag = FALSE; |
| 149 | createFlag = FALSE; |
| 150 | listFlag = FALSE; |
| 151 | verboseFlag = FALSE; |
| 152 | tostdoutFlag = FALSE; |
| 153 | tarName = NULL; |
| 154 | tarDev = 0; |
| 155 | tarInode = 0; |
| 156 | tarFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 158 | /* |
| 159 | * Parse the options. |
| 160 | */ |
| 161 | options = *argv++; |
| 162 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 163 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 164 | if (**argv == '-') { |
| 165 | for (; *options; options++) { |
| 166 | switch (*options) { |
| 167 | case 'f': |
| 168 | if (tarName != NULL) { |
| 169 | fprintf (stderr, "Only one 'f' option allowed\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 170 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 171 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 173 | |
| 174 | tarName = *argv++; |
| 175 | argc--; |
| 176 | |
| 177 | break; |
| 178 | |
| 179 | case 't': |
| 180 | listFlag = TRUE; |
| 181 | break; |
| 182 | |
| 183 | case 'x': |
| 184 | extractFlag = TRUE; |
| 185 | break; |
| 186 | |
| 187 | case 'c': |
| 188 | createFlag = TRUE; |
| 189 | break; |
| 190 | |
| 191 | case 'v': |
| 192 | verboseFlag = TRUE; |
| 193 | break; |
| 194 | |
| 195 | case 'O': |
| 196 | tostdoutFlag = TRUE; |
| 197 | break; |
| 198 | |
| 199 | case '-': |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | fprintf (stderr, "Unknown tar flag '%c'\n", *options); |
| 204 | |
| 205 | exit (FALSE); |
| 206 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 207 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 208 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 209 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Validate the options. |
| 212 | */ |
| 213 | if (extractFlag + listFlag + createFlag != 1) { |
| 214 | fprintf (stderr, |
| 215 | "Exactly one of 'c', 'x' or 't' must be specified\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 216 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 217 | exit (FALSE); |
| 218 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 219 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Do the correct type of action supplying the rest of the |
| 222 | * command line arguments as the list of files to process. |
| 223 | */ |
| 224 | if (createFlag) |
| 225 | writeTarFile (argc, argv); |
| 226 | else |
| 227 | readTarFile (argc, argv); |
| 228 | if (errorFlag) |
| 229 | fprintf (stderr, "\n"); |
| 230 | exit (errorFlag); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * Read a tar file and extract or list the specified files within it. |
| 236 | * If the list is empty than all files are extracted or listed. |
| 237 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 238 | static void readTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 239 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 240 | const char *cp; |
| 241 | int cc; |
| 242 | int inCc; |
| 243 | int blockSize; |
| 244 | char buf[BUF_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 245 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 246 | skipFileFlag = FALSE; |
| 247 | badHeader = FALSE; |
| 248 | warnedRoot = FALSE; |
| 249 | eofFlag = FALSE; |
| 250 | inHeader = TRUE; |
| 251 | inCc = 0; |
| 252 | dataCc = 0; |
| 253 | outFd = -1; |
| 254 | blockSize = sizeof (buf); |
| 255 | cp = buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 256 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 257 | /* |
| 258 | * Open the tar file for reading. |
| 259 | */ |
| 260 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
| 261 | tarFd = STDIN; |
| 262 | } else |
| 263 | tarFd = open (tarName, O_RDONLY); |
| 264 | |
| 265 | if (tarFd < 0) { |
| 266 | perror (tarName); |
| 267 | errorFlag = TRUE; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Read blocks from the file until an end of file header block |
| 273 | * has been seen. (A real end of file from a read is an error.) |
| 274 | */ |
| 275 | while (!eofFlag) { |
| 276 | /* |
| 277 | * Read the next block of data if necessary. |
| 278 | * This will be a large block if possible, which we will |
| 279 | * then process in the small tar blocks. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 280 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 281 | if (inCc <= 0) { |
| 282 | cp = buf; |
| 283 | inCc = fullRead (tarFd, buf, blockSize); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 284 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 285 | if (inCc < 0) { |
| 286 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | errorFlag = TRUE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 288 | goto done; |
| 289 | } |
| 290 | |
| 291 | if (inCc == 0) { |
| 292 | fprintf (stderr, |
| 293 | "Unexpected end of file from \"%s\"", tarName); |
| 294 | errorFlag = TRUE; |
| 295 | goto done; |
| 296 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 299 | /* |
| 300 | * If we are expecting a header block then examine it. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 301 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 302 | if (inHeader) { |
| 303 | readHeader ((const TarHeader *) cp, fileCount, fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 304 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 305 | cp += TAR_BLOCK_SIZE; |
| 306 | inCc -= TAR_BLOCK_SIZE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 307 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 308 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 311 | /* |
| 312 | * We are currently handling the data for a file. |
| 313 | * Process the minimum of the amount of data we have available |
| 314 | * and the amount left to be processed for the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 315 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 316 | cc = inCc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 317 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 318 | if (cc > dataCc) |
| 319 | cc = dataCc; |
| 320 | |
| 321 | readData (cp, cc); |
| 322 | |
| 323 | /* |
| 324 | * If the amount left isn't an exact multiple of the tar block |
| 325 | * size then round it up to the next block boundary since there |
| 326 | * is padding at the end of the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 327 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 328 | if (cc % TAR_BLOCK_SIZE) |
| 329 | cc += TAR_BLOCK_SIZE - (cc % TAR_BLOCK_SIZE); |
| 330 | |
| 331 | cp += cc; |
| 332 | inCc -= cc; |
| 333 | } |
| 334 | |
| 335 | done: |
| 336 | /* |
| 337 | * Close the tar file if needed. |
| 338 | */ |
| 339 | if ((tarFd >= 0) && (close (tarFd) < 0)) |
| 340 | perror (tarName); |
| 341 | |
| 342 | /* |
| 343 | * Close the output file if needed. |
| 344 | * This is only done here on a previous error and so no |
| 345 | * message is required on errors. |
| 346 | */ |
| 347 | if (tostdoutFlag == FALSE) { |
| 348 | if (outFd >= 0) |
| 349 | (void) close (outFd); |
| 350 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | /* |
| 355 | * Examine the header block that was just read. |
| 356 | * This can specify the information for another file, or it can mark |
| 357 | * the end of the tar file. |
| 358 | */ |
| 359 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 360 | readHeader (const TarHeader * hp, int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 361 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 362 | int mode; |
| 363 | int uid; |
| 364 | int gid; |
| 365 | int checkSum; |
| 366 | long size; |
| 367 | time_t mtime; |
| 368 | const char *name; |
| 369 | int cc; |
| 370 | int hardLink; |
| 371 | int softLink; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 372 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 373 | /* |
| 374 | * If the block is completely empty, then this is the end of the |
| 375 | * archive file. If the name is null, then just skip this header. |
| 376 | */ |
| 377 | name = hp->name; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 378 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 379 | if (*name == '\0') { |
| 380 | for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) { |
| 381 | if (*name++) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 385 | eofFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 386 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 390 | /* |
| 391 | * There is another file in the archive to examine. |
| 392 | * Extract the encoded information and check it. |
| 393 | */ |
| 394 | mode = getOctal (hp->mode, sizeof (hp->mode)); |
| 395 | uid = getOctal (hp->uid, sizeof (hp->uid)); |
| 396 | gid = getOctal (hp->gid, sizeof (hp->gid)); |
| 397 | size = getOctal (hp->size, sizeof (hp->size)); |
| 398 | mtime = getOctal (hp->mtime, sizeof (hp->mtime)); |
| 399 | checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 400 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 401 | if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) { |
| 402 | if (!badHeader) |
| 403 | fprintf (stderr, "Bad tar header, skipping\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 404 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 405 | badHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 406 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
| 410 | badHeader = FALSE; |
| 411 | skipFileFlag = FALSE; |
| 412 | |
| 413 | /* |
| 414 | * Check for the file modes. |
| 415 | */ |
| 416 | hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 417 | (hp->typeFlag == TAR_TYPE_HARD_LINK - '0')); |
| 418 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 419 | softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 420 | (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); |
| 421 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 422 | /* |
| 423 | * Check for a directory or a regular file. |
| 424 | */ |
| 425 | if (name[strlen (name) - 1] == '/') |
| 426 | mode |= S_IFDIR; |
| 427 | else if ((mode & S_IFMT) == 0) |
| 428 | mode |= S_IFREG; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 429 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 430 | /* |
| 431 | * Check for absolute paths in the file. |
| 432 | * If we find any, then warn the user and make them relative. |
| 433 | */ |
| 434 | if (*name == '/') { |
| 435 | while (*name == '/') |
| 436 | name++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 437 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 438 | if (!warnedRoot) { |
| 439 | fprintf (stderr, |
| 440 | "Absolute path detected, removing leading slashes\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 443 | warnedRoot = TRUE; |
| 444 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 445 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 446 | /* |
| 447 | * See if we want this file to be restored. |
| 448 | * If not, then set up to skip it. |
| 449 | */ |
| 450 | if (!wantFileName (name, fileCount, fileTable)) { |
| 451 | if (!hardLink && !softLink && S_ISREG (mode)) { |
| 452 | inHeader = (size == 0); |
| 453 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 456 | skipFileFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 457 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 458 | return; |
| 459 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 460 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 461 | /* |
| 462 | * This file is to be handled. |
| 463 | * If we aren't extracting then just list information about the file. |
| 464 | */ |
| 465 | if (!extractFlag) { |
| 466 | if (verboseFlag) { |
| 467 | printf ("%s %3d/%-d %9ld %s %s", modeString (mode), |
| 468 | uid, gid, size, timeString (mtime), name); |
| 469 | } else |
| 470 | printf ("%s", name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 471 | |
| 472 | if (hardLink) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 473 | printf (" (link to \"%s\")", hp->linkName); |
| 474 | else if (softLink) |
| 475 | printf (" (symlink to \"%s\")", hp->linkName); |
| 476 | else if (S_ISREG (mode)) { |
| 477 | inHeader = (size == 0); |
| 478 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 481 | printf ("\n"); |
| 482 | |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * We really want to extract the file. |
| 488 | */ |
| 489 | if (verboseFlag) |
| 490 | printf ("x %s\n", name); |
| 491 | |
| 492 | if (hardLink) { |
| 493 | if (link (hp->linkName, name) < 0) |
| 494 | perror (name); |
| 495 | |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | if (softLink) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 500 | #ifdef S_ISLNK |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 501 | if (symlink (hp->linkName, name) < 0) |
| 502 | perror (name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 503 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 504 | fprintf (stderr, "Cannot create symbolic links\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 505 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 506 | return; |
| 507 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 508 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 509 | /* |
| 510 | * If the file is a directory, then just create the path. |
| 511 | */ |
| 512 | if (S_ISDIR (mode)) { |
| 513 | createPath (name, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 514 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 517 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 518 | /* |
| 519 | * There is a file to write. |
| 520 | * First create the path to it if necessary with a default permission. |
| 521 | */ |
| 522 | createPath (name, 0777); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 523 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 524 | inHeader = (size == 0); |
| 525 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 526 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 527 | /* |
| 528 | * Start the output file. |
| 529 | */ |
| 530 | if (tostdoutFlag == TRUE) |
| 531 | outFd = STDOUT; |
| 532 | else |
| 533 | outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 534 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 535 | if (outFd < 0) { |
| 536 | perror (name); |
| 537 | skipFileFlag = TRUE; |
| 538 | return; |
| 539 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 540 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 541 | /* |
| 542 | * If the file is empty, then that's all we need to do. |
| 543 | */ |
| 544 | if (size == 0 && tostdoutFlag == FALSE) { |
| 545 | (void) close (outFd); |
| 546 | outFd = -1; |
| 547 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | /* |
| 552 | * Handle a data block of some specified size that was read. |
| 553 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 554 | static void readData (const char *cp, int count) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 555 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Reduce the amount of data left in this file. |
| 558 | * If there is no more data left, then we need to read |
| 559 | * the header again. |
| 560 | */ |
| 561 | dataCc -= count; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 562 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 563 | if (dataCc <= 0) |
| 564 | inHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 565 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 566 | /* |
| 567 | * If we aren't extracting files or this file is being |
| 568 | * skipped then do nothing more. |
| 569 | */ |
| 570 | if (!extractFlag || skipFileFlag) |
| 571 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 572 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 573 | /* |
| 574 | * Write the data to the output file. |
| 575 | */ |
| 576 | if (fullWrite (outFd, cp, count) < 0) { |
| 577 | perror (outName); |
| 578 | if (tostdoutFlag == FALSE) { |
| 579 | (void) close (outFd); |
| 580 | outFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 581 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 582 | skipFileFlag = TRUE; |
| 583 | return; |
| 584 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 585 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 586 | /* |
| 587 | * If the write failed, close the file and disable further |
| 588 | * writes to this file. |
| 589 | */ |
| 590 | if (dataCc <= 0 && tostdoutFlag == FALSE) { |
| 591 | if (close (outFd)) |
| 592 | perror (outName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 593 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 594 | outFd = -1; |
| 595 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
| 599 | /* |
| 600 | * Write a tar file containing the specified files. |
| 601 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 602 | static void writeTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 603 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 604 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 605 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 606 | /* |
| 607 | * Make sure there is at least one file specified. |
| 608 | */ |
| 609 | if (fileCount <= 0) { |
| 610 | fprintf (stderr, "No files specified to be saved\n"); |
| 611 | errorFlag = TRUE; |
| 612 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 613 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 614 | /* |
| 615 | * Create the tar file for writing. |
| 616 | */ |
| 617 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
| 618 | tostdoutFlag = TRUE; |
| 619 | tarFd = STDOUT; |
| 620 | } else |
| 621 | tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 622 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 623 | if (tarFd < 0) { |
| 624 | perror (tarName); |
| 625 | errorFlag = TRUE; |
| 626 | return; |
| 627 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 628 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 629 | /* |
| 630 | * Get the device and inode of the tar file for checking later. |
| 631 | */ |
| 632 | if (fstat (tarFd, &statbuf) < 0) { |
| 633 | perror (tarName); |
| 634 | errorFlag = TRUE; |
| 635 | goto done; |
| 636 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 637 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 638 | tarDev = statbuf.st_dev; |
| 639 | tarInode = statbuf.st_ino; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 640 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 641 | /* |
| 642 | * Append each file name into the archive file. |
| 643 | * Follow symbolic links for these top level file names. |
| 644 | */ |
| 645 | while (!errorFlag && (fileCount-- > 0)) { |
| 646 | saveFile (*fileTable++, FALSE); |
| 647 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 648 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 649 | /* |
| 650 | * Now write an empty block of zeroes to end the archive. |
| 651 | */ |
| 652 | writeTarBlock ("", 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 653 | |
| 654 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 655 | done: |
| 656 | /* |
| 657 | * Close the tar file and check for errors if it was opened. |
| 658 | */ |
| 659 | if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close (tarFd) < 0)) |
| 660 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
| 664 | /* |
| 665 | * Save one file into the tar file. |
| 666 | * If the file is a directory, then this will recursively save all of |
| 667 | * the files and directories within the directory. The seeLinks |
| 668 | * flag indicates whether or not we want to see symbolic links as |
| 669 | * they really are, instead of blindly following them. |
| 670 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 671 | static void saveFile (const char *fileName, int seeLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 672 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 673 | int status; |
| 674 | int mode; |
| 675 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 676 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 677 | if (verboseFlag) |
| 678 | printf ("a %s\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 679 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 680 | /* |
| 681 | * Check that the file name will fit in the header. |
| 682 | */ |
| 683 | if (strlen (fileName) >= TAR_NAME_SIZE) { |
| 684 | fprintf (stderr, "%s: File name is too long\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 685 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 688 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 689 | /* |
| 690 | * Find out about the file. |
| 691 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 692 | #ifdef S_ISLNK |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 693 | if (seeLinks) |
| 694 | status = lstat (fileName, &statbuf); |
| 695 | else |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 696 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 697 | status = stat (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 698 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 699 | if (status < 0) { |
| 700 | perror (fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 701 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 702 | return; |
| 703 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 704 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 705 | /* |
| 706 | * Make sure we aren't trying to save our file into itself. |
| 707 | */ |
| 708 | if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) { |
| 709 | fprintf (stderr, "Skipping saving of archive file itself\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 710 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 713 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 714 | /* |
| 715 | * Check the type of file. |
| 716 | */ |
| 717 | mode = statbuf.st_mode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 718 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 719 | if (S_ISDIR (mode)) { |
| 720 | saveDirectory (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 721 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 722 | return; |
| 723 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 724 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 725 | if (S_ISREG (mode)) { |
| 726 | saveRegularFile (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 727 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 730 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 731 | /* |
| 732 | * The file is a strange type of file, ignore it. |
| 733 | */ |
| 734 | fprintf (stderr, "%s: not a directory or regular file\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
| 738 | /* |
| 739 | * Save a regular file to the tar file. |
| 740 | */ |
| 741 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 742 | saveRegularFile (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 743 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 744 | int sawEof; |
| 745 | int fileFd; |
| 746 | int cc; |
| 747 | int dataCount; |
| 748 | long fullDataCount; |
| 749 | char data[TAR_BLOCK_SIZE * 16]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 750 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 751 | /* |
| 752 | * Open the file for reading. |
| 753 | */ |
| 754 | fileFd = open (fileName, O_RDONLY); |
| 755 | |
| 756 | if (fileFd < 0) { |
| 757 | perror (fileName); |
| 758 | |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Write out the header for the file. |
| 764 | */ |
| 765 | writeHeader (fileName, statbuf); |
| 766 | |
| 767 | /* |
| 768 | * Write the data blocks of the file. |
| 769 | * We must be careful to write the amount of data that the stat |
| 770 | * buffer indicated, even if the file has changed size. Otherwise |
| 771 | * the tar file will be incorrect. |
| 772 | */ |
| 773 | fullDataCount = statbuf->st_size; |
| 774 | sawEof = FALSE; |
| 775 | |
| 776 | while (fullDataCount > 0) { |
| 777 | /* |
| 778 | * Get the amount to write this iteration which is |
| 779 | * the minumum of the amount left to write and the |
| 780 | * buffer size. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 781 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 782 | dataCount = sizeof (data); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 783 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 784 | if (dataCount > fullDataCount) |
| 785 | dataCount = (int) fullDataCount; |
| 786 | |
| 787 | /* |
| 788 | * Read the data from the file if we haven't seen the |
| 789 | * end of file yet. |
| 790 | */ |
| 791 | cc = 0; |
| 792 | |
| 793 | if (!sawEof) { |
| 794 | cc = fullRead (fileFd, data, dataCount); |
| 795 | |
| 796 | if (cc < 0) { |
| 797 | perror (fileName); |
| 798 | |
| 799 | (void) close (fileFd); |
| 800 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 801 | |
| 802 | return; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | * If the file ended too soon, complain and set |
| 807 | * a flag so we will zero fill the rest of it. |
| 808 | */ |
| 809 | if (cc < dataCount) { |
| 810 | fprintf (stderr, |
| 811 | "%s: Short read - zero filling", fileName); |
| 812 | |
| 813 | sawEof = TRUE; |
| 814 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 817 | /* |
| 818 | * Zero fill the rest of the data if necessary. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 819 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 820 | if (cc < dataCount) |
| 821 | memset (data + cc, 0, dataCount - cc); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 822 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 823 | /* |
| 824 | * Write the buffer to the TAR file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 825 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 826 | writeTarBlock (data, dataCount); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 827 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 828 | fullDataCount -= dataCount; |
| 829 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 830 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 831 | /* |
| 832 | * Close the file. |
| 833 | */ |
| 834 | if ((tostdoutFlag == FALSE) && close (fileFd) < 0) |
| 835 | fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | |
| 839 | /* |
| 840 | * Save a directory and all of its files to the tar file. |
| 841 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 842 | static void saveDirectory (const char *dirName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 843 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 844 | DIR *dir; |
| 845 | struct dirent *entry; |
| 846 | int needSlash; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame^] | 847 | char fullName[NAME_MAX]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 848 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 849 | /* |
| 850 | * Construct the directory name as used in the tar file by appending |
| 851 | * a slash character to it. |
| 852 | */ |
| 853 | strcpy (fullName, dirName); |
| 854 | strcat (fullName, "/"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 855 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 856 | /* |
| 857 | * Write out the header for the directory entry. |
| 858 | */ |
| 859 | writeHeader (fullName, statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 860 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 861 | /* |
| 862 | * Open the directory. |
| 863 | */ |
| 864 | dir = opendir (dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 865 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 866 | if (dir == NULL) { |
| 867 | fprintf (stderr, "Cannot read directory \"%s\": %s\n", |
| 868 | dirName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 869 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * See if a slash is needed. |
| 875 | */ |
| 876 | needSlash = (*dirName && (dirName[strlen (dirName) - 1] != '/')); |
| 877 | |
| 878 | /* |
| 879 | * Read all of the directory entries and check them, |
| 880 | * except for the current and parent directory entries. |
| 881 | */ |
| 882 | while (!errorFlag && ((entry = readdir (dir)) != NULL)) { |
| 883 | if ((strcmp (entry->d_name, ".") == 0) || |
| 884 | (strcmp (entry->d_name, "..") == 0)) { |
| 885 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 888 | /* |
| 889 | * Build the full path name to the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 890 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 891 | strcpy (fullName, dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 892 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 893 | if (needSlash) |
| 894 | strcat (fullName, "/"); |
| 895 | |
| 896 | strcat (fullName, entry->d_name); |
| 897 | |
| 898 | /* |
| 899 | * Write this file to the tar file, noticing whether or not |
| 900 | * the file is a symbolic link. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 901 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 902 | saveFile (fullName, TRUE); |
| 903 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 904 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 905 | /* |
| 906 | * All done, close the directory. |
| 907 | */ |
| 908 | closedir (dir); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
| 912 | /* |
| 913 | * Write a tar header for the specified file name and status. |
| 914 | * It is assumed that the file name fits. |
| 915 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 916 | static void writeHeader (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 917 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 918 | long checkSum; |
| 919 | const unsigned char *cp; |
| 920 | int len; |
| 921 | TarHeader header; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 922 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 923 | /* |
| 924 | * Zero the header block in preparation for filling it in. |
| 925 | */ |
| 926 | memset ((char *) &header, 0, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 927 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 928 | /* |
| 929 | * Fill in the header. |
| 930 | */ |
| 931 | strcpy (header.name, fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 932 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 933 | strncpy (header.magic, TAR_MAGIC, sizeof (header.magic)); |
| 934 | strncpy (header.version, TAR_VERSION, sizeof (header.version)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 935 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 936 | putOctal (header.mode, sizeof (header.mode), statbuf->st_mode & 0777); |
| 937 | putOctal (header.uid, sizeof (header.uid), statbuf->st_uid); |
| 938 | putOctal (header.gid, sizeof (header.gid), statbuf->st_gid); |
| 939 | putOctal (header.size, sizeof (header.size), statbuf->st_size); |
| 940 | putOctal (header.mtime, sizeof (header.mtime), statbuf->st_mtime); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 941 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 942 | header.typeFlag = TAR_TYPE_REGULAR; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 943 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 944 | /* |
| 945 | * Calculate and store the checksum. |
| 946 | * This is the sum of all of the bytes of the header, |
| 947 | * with the checksum field itself treated as blanks. |
| 948 | */ |
| 949 | memset (header.checkSum, ' ', sizeof (header.checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 950 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 951 | cp = (const unsigned char *) &header; |
| 952 | len = sizeof (header); |
| 953 | checkSum = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 954 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 955 | while (len-- > 0) |
| 956 | checkSum += *cp++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 957 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 958 | putOctal (header.checkSum, sizeof (header.checkSum), checkSum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 959 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 960 | /* |
| 961 | * Write the tar header. |
| 962 | */ |
| 963 | writeTarBlock ((const char *) &header, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
| 967 | /* |
| 968 | * Write data to one or more blocks of the tar file. |
| 969 | * The data is always padded out to a multiple of TAR_BLOCK_SIZE. |
| 970 | * The errorFlag static variable is set on an error. |
| 971 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 972 | static void writeTarBlock (const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 973 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 974 | int partialLength; |
| 975 | int completeLength; |
| 976 | char fullBlock[TAR_BLOCK_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 977 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 978 | /* |
| 979 | * If we had a write error before, then do nothing more. |
| 980 | */ |
| 981 | if (errorFlag) |
| 982 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 983 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 984 | /* |
| 985 | * Get the amount of complete and partial blocks. |
| 986 | */ |
| 987 | partialLength = len % TAR_BLOCK_SIZE; |
| 988 | completeLength = len - partialLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 989 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 990 | /* |
| 991 | * Write all of the complete blocks. |
| 992 | */ |
| 993 | if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) { |
| 994 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 995 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 996 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 997 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1000 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | * If there are no partial blocks left, we are done. |
| 1003 | */ |
| 1004 | if (partialLength == 0) |
| 1005 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1006 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1007 | /* |
| 1008 | * Copy the partial data into a complete block, and pad the rest |
| 1009 | * of it with zeroes. |
| 1010 | */ |
| 1011 | memcpy (fullBlock, buf + completeLength, partialLength); |
| 1012 | memset (fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1013 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1014 | /* |
| 1015 | * Write the last complete block. |
| 1016 | */ |
| 1017 | if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) { |
| 1018 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1019 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1020 | errorFlag = TRUE; |
| 1021 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | /* |
| 1026 | * Attempt to create the directories along the specified path, except for |
| 1027 | * the final component. The mode is given for the final directory only, |
| 1028 | * while all previous ones get default protections. Errors are not reported |
| 1029 | * here, as failures to restore files can be reported later. |
| 1030 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1031 | static void createPath (const char *name, int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1032 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1033 | char *cp; |
| 1034 | char *cpOld; |
| 1035 | char buf[TAR_NAME_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1036 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1037 | strcpy (buf, name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1038 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1039 | cp = strchr (buf, '/'); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1040 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1041 | while (cp) { |
| 1042 | cpOld = cp; |
| 1043 | cp = strchr (cp + 1, '/'); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1044 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1045 | *cpOld = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1046 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1047 | if (mkdir (buf, cp ? 0777 : mode) == 0) |
| 1048 | printf ("Directory \"%s\" created\n", buf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1049 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1050 | *cpOld = '/'; |
| 1051 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | /* |
| 1056 | * Read an octal value in a field of the specified width, with optional |
| 1057 | * spaces on both sides of the number and with an optional null character |
| 1058 | * at the end. Returns -1 on an illegal format. |
| 1059 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1060 | static long getOctal (const char *cp, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1061 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1062 | long val; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1063 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1064 | while ((len > 0) && (*cp == ' ')) { |
| 1065 | cp++; |
| 1066 | len--; |
| 1067 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1068 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1069 | if ((len == 0) || !isOctal (*cp)) |
| 1070 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1071 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1072 | val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1073 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1074 | while ((len > 0) && isOctal (*cp)) { |
| 1075 | val = val * 8 + *cp++ - '0'; |
| 1076 | len--; |
| 1077 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1078 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1079 | while ((len > 0) && (*cp == ' ')) { |
| 1080 | cp++; |
| 1081 | len--; |
| 1082 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1083 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1084 | if ((len > 0) && *cp) |
| 1085 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1086 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1087 | return val; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | /* |
| 1092 | * Put an octal string into the specified buffer. |
| 1093 | * The number is zero and space padded and possibly null padded. |
| 1094 | * Returns TRUE if successful. |
| 1095 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1096 | static int putOctal (char *cp, int len, long value) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1097 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1098 | int tempLength; |
| 1099 | char *tempString; |
| 1100 | char tempBuffer[32]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1101 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1102 | /* |
| 1103 | * Create a string of the specified length with an initial space, |
| 1104 | * leading zeroes and the octal number, and a trailing null. |
| 1105 | */ |
| 1106 | tempString = tempBuffer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1107 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1108 | sprintf (tempString, " %0*lo", len - 2, value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1109 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1110 | tempLength = strlen (tempString) + 1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1111 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1112 | /* |
| 1113 | * If the string is too large, suppress the leading space. |
| 1114 | */ |
| 1115 | if (tempLength > len) { |
| 1116 | tempLength--; |
| 1117 | tempString++; |
| 1118 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1119 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1120 | /* |
| 1121 | * If the string is still too large, suppress the trailing null. |
| 1122 | */ |
| 1123 | if (tempLength > len) |
| 1124 | tempLength--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1125 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1126 | /* |
| 1127 | * If the string is still too large, fail. |
| 1128 | */ |
| 1129 | if (tempLength > len) |
| 1130 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1131 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1132 | /* |
| 1133 | * Copy the string to the field. |
| 1134 | */ |
| 1135 | memcpy (cp, tempString, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1136 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1137 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | /* |
| 1142 | * See if the specified file name belongs to one of the specified list |
| 1143 | * of path prefixes. An empty list implies that all files are wanted. |
| 1144 | * Returns TRUE if the file is selected. |
| 1145 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 1146 | static int |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1147 | wantFileName (const char *fileName, int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1148 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1149 | const char *pathName; |
| 1150 | int fileLength; |
| 1151 | int pathLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1152 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * If there are no files in the list, then the file is wanted. |
| 1155 | */ |
| 1156 | if (fileCount == 0) |
| 1157 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1158 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1159 | fileLength = strlen (fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1160 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1161 | /* |
| 1162 | * Check each of the test paths. |
| 1163 | */ |
| 1164 | while (fileCount-- > 0) { |
| 1165 | pathName = *fileTable++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1166 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1167 | pathLength = strlen (pathName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1168 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1169 | if (fileLength < pathLength) |
| 1170 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1171 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1172 | if (memcmp (fileName, pathName, pathLength) != 0) |
| 1173 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1174 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1175 | if ((fileLength == pathLength) || (fileName[pathLength] == '/')) { |
| 1176 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1177 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1178 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1179 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1180 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1185 | #endif |
| 1186 | /* END CODE */ |