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