blob: b404ab0256afd9a7447c5cf2bc45252622fbdb1b [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
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 Andersen3cf52d11999-10-12 22:26:06 +000010 * Modified for busybox by Erik Andersen <andersee@debian.org>
Eric Andersencc8ed391999-10-05 16:24:54 +000011 */
12
13
14#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000015#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 Andersene77ae3a1999-10-19 20:03:34 +000022
23static 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 Andersencc8ed391999-10-05 16:24:54 +000031/*
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 Andersen3cf52d11999-10-12 22:26:06 +000043typedef 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 Andersencc8ed391999-10-05 16:24:54 +000060} 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 Andersen3cf52d11999-10-12 22:26:06 +000073static int listFlag;
74static int extractFlag;
75static int createFlag;
76static int verboseFlag;
77static int tostdoutFlag;
Eric Andersencc8ed391999-10-05 16:24:54 +000078
Eric Andersen3cf52d11999-10-12 22:26:06 +000079static int inHeader;
80static int badHeader;
81static int errorFlag;
82static int skipFileFlag;
83static int warnedRoot;
84static int eofFlag;
85static long dataCc;
86static int outFd;
87static char outName[TAR_NAME_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +000088
89
90/*
91 * Static data associated with the tar file.
92 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000093static const char *tarName;
94static int tarFd;
95static dev_t tarDev;
96static ino_t tarInode;
Eric Andersencc8ed391999-10-05 16:24:54 +000097
98
99/*
100 * Local procedures to restore files from a tar file.
101 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000102static void readTarFile (int fileCount, char **fileTable);
103static void readData (const char *cp, int count);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000104static long getOctal (const char *cp, int len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Eric Andersen3cf52d11999-10-12 22:26:06 +0000106static void readHeader (const TarHeader * hp,
107 int fileCount, char **fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
109
110/*
111 * Local procedures to save files into a tar file.
112 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000113static void saveFile (const char *fileName, int seeLinks);
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115static void saveRegularFile (const char *fileName,
116 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118static void saveDirectory (const char *fileName,
119 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000120
Eric Andersen3cf52d11999-10-12 22:26:06 +0000121static int wantFileName (const char *fileName,
122 int fileCount, char **fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000123
Eric Andersen3cf52d11999-10-12 22:26:06 +0000124static void writeHeader (const char *fileName, const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Eric Andersen3cf52d11999-10-12 22:26:06 +0000126static void writeTarFile (int fileCount, char **fileTable);
127static void writeTarBlock (const char *buf, int len);
128static int putOctal (char *cp, int len, long value);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
130
Eric Andersen3cf52d11999-10-12 22:26:06 +0000131extern int tar_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000132{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000133 const char *options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Eric Andersen3cf52d11999-10-12 22:26:06 +0000135 argc--;
136 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000137
Eric Andersen3cf52d11999-10-12 22:26:06 +0000138 if (argc < 1) {
139 fprintf (stderr, "%s", tar_usage);
140 exit (FALSE);
141 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000142
143
Eric Andersen3cf52d11999-10-12 22:26:06 +0000144 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 Andersencc8ed391999-10-05 16:24:54 +0000154
Eric Andersen3cf52d11999-10-12 22:26:06 +0000155 /*
156 * Parse the options.
157 */
158 options = *argv++;
159 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000160
Eric Andersen3cf52d11999-10-12 22:26:06 +0000161 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 Andersencc8ed391999-10-05 16:24:54 +0000167
Eric Andersen3cf52d11999-10-12 22:26:06 +0000168 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000170
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 Andersencc8ed391999-10-05 16:24:54 +0000204 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000205 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000206
Eric Andersen3cf52d11999-10-12 22:26:06 +0000207 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000213
Eric Andersen3cf52d11999-10-12 22:26:06 +0000214 exit (FALSE);
215 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000216
Eric Andersen3cf52d11999-10-12 22:26:06 +0000217 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000228}
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 Andersen3cf52d11999-10-12 22:26:06 +0000235static void readTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000236{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000237 const char *cp;
238 int cc;
239 int inCc;
240 int blockSize;
241 char buf[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000242
Eric Andersen3cf52d11999-10-12 22:26:06 +0000243 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 Andersencc8ed391999-10-05 16:24:54 +0000253
Eric Andersen3cf52d11999-10-12 22:26:06 +0000254 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000277 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000278 if (inCc <= 0) {
279 cp = buf;
280 inCc = fullRead (tarFd, buf, blockSize);
Eric Andersencc8ed391999-10-05 16:24:54 +0000281
Eric Andersen3cf52d11999-10-12 22:26:06 +0000282 if (inCc < 0) {
283 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000284 errorFlag = TRUE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000285 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 Andersencc8ed391999-10-05 16:24:54 +0000294 }
295
Eric Andersen3cf52d11999-10-12 22:26:06 +0000296 /*
297 * If we are expecting a header block then examine it.
Eric Andersencc8ed391999-10-05 16:24:54 +0000298 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000299 if (inHeader) {
300 readHeader ((const TarHeader *) cp, fileCount, fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000301
Eric Andersen3cf52d11999-10-12 22:26:06 +0000302 cp += TAR_BLOCK_SIZE;
303 inCc -= TAR_BLOCK_SIZE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000304
Eric Andersen3cf52d11999-10-12 22:26:06 +0000305 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000306 }
307
Eric Andersen3cf52d11999-10-12 22:26:06 +0000308 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000312 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000313 cc = inCc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000314
Eric Andersen3cf52d11999-10-12 22:26:06 +0000315 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 Andersencc8ed391999-10-05 16:24:54 +0000324 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000325 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 Andersencc8ed391999-10-05 16:24:54 +0000348}
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 */
356static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000357readHeader (const TarHeader * hp, int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000358{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000359 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 Andersencc8ed391999-10-05 16:24:54 +0000369
Eric Andersen3cf52d11999-10-12 22:26:06 +0000370 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000375
Eric Andersen3cf52d11999-10-12 22:26:06 +0000376 if (*name == '\0') {
377 for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) {
378 if (*name++)
Eric Andersencc8ed391999-10-05 16:24:54 +0000379 return;
380 }
381
Eric Andersen3cf52d11999-10-12 22:26:06 +0000382 eofFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000383
Eric Andersen3cf52d11999-10-12 22:26:06 +0000384 return;
385 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000386
Eric Andersen3cf52d11999-10-12 22:26:06 +0000387 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000397
Eric Andersen3cf52d11999-10-12 22:26:06 +0000398 if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) {
399 if (!badHeader)
400 fprintf (stderr, "Bad tar header, skipping\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000401
Eric Andersen3cf52d11999-10-12 22:26:06 +0000402 badHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000403
Eric Andersen3cf52d11999-10-12 22:26:06 +0000404 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 Andersencc8ed391999-10-05 16:24:54 +0000414 (hp->typeFlag == TAR_TYPE_HARD_LINK - '0'));
415
Eric Andersen3cf52d11999-10-12 22:26:06 +0000416 softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) ||
Eric Andersencc8ed391999-10-05 16:24:54 +0000417 (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0'));
418
Eric Andersen3cf52d11999-10-12 22:26:06 +0000419 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000426
Eric Andersen3cf52d11999-10-12 22:26:06 +0000427 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000434
Eric Andersen3cf52d11999-10-12 22:26:06 +0000435 if (!warnedRoot) {
436 fprintf (stderr,
437 "Absolute path detected, removing leading slashes\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000438 }
439
Eric Andersen3cf52d11999-10-12 22:26:06 +0000440 warnedRoot = TRUE;
441 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000442
Eric Andersen3cf52d11999-10-12 22:26:06 +0000443 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000451 }
452
Eric Andersen3cf52d11999-10-12 22:26:06 +0000453 skipFileFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000454
Eric Andersen3cf52d11999-10-12 22:26:06 +0000455 return;
456 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000457
Eric Andersen3cf52d11999-10-12 22:26:06 +0000458 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000468
469 if (hardLink)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000470 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 Andersencc8ed391999-10-05 16:24:54 +0000476 }
477
Eric Andersen3cf52d11999-10-12 22:26:06 +0000478 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 Andersencc8ed391999-10-05 16:24:54 +0000497#ifdef S_ISLNK
Eric Andersen3cf52d11999-10-12 22:26:06 +0000498 if (symlink (hp->linkName, name) < 0)
499 perror (name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000500#else
Eric Andersen3cf52d11999-10-12 22:26:06 +0000501 fprintf (stderr, "Cannot create symbolic links\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000502#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000503 return;
504 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Eric Andersen3cf52d11999-10-12 22:26:06 +0000506 /*
507 * If the file is a directory, then just create the path.
508 */
509 if (S_ISDIR (mode)) {
510 createPath (name, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Eric Andersen3cf52d11999-10-12 22:26:06 +0000512 return;
513 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000514
Eric Andersen3cf52d11999-10-12 22:26:06 +0000515 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000520
Eric Andersen3cf52d11999-10-12 22:26:06 +0000521 inHeader = (size == 0);
522 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
Eric Andersen3cf52d11999-10-12 22:26:06 +0000524 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000531
Eric Andersen3cf52d11999-10-12 22:26:06 +0000532 if (outFd < 0) {
533 perror (name);
534 skipFileFlag = TRUE;
535 return;
536 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000537
Eric Andersen3cf52d11999-10-12 22:26:06 +0000538 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000545}
546
547
548/*
549 * Handle a data block of some specified size that was read.
550 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000551static void readData (const char *cp, int count)
Eric Andersencc8ed391999-10-05 16:24:54 +0000552{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000553 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000559
Eric Andersen3cf52d11999-10-12 22:26:06 +0000560 if (dataCc <= 0)
561 inHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000562
Eric Andersen3cf52d11999-10-12 22:26:06 +0000563 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000569
Eric Andersen3cf52d11999-10-12 22:26:06 +0000570 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000578 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000579 skipFileFlag = TRUE;
580 return;
581 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000582
Eric Andersen3cf52d11999-10-12 22:26:06 +0000583 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000590
Eric Andersen3cf52d11999-10-12 22:26:06 +0000591 outFd = -1;
592 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000593}
594
595
596/*
597 * Write a tar file containing the specified files.
598 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000599static void writeTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000600{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000601 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000602
Eric Andersen3cf52d11999-10-12 22:26:06 +0000603 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000610
Eric Andersen3cf52d11999-10-12 22:26:06 +0000611 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000619
Eric Andersen3cf52d11999-10-12 22:26:06 +0000620 if (tarFd < 0) {
621 perror (tarName);
622 errorFlag = TRUE;
623 return;
624 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000625
Eric Andersen3cf52d11999-10-12 22:26:06 +0000626 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000634
Eric Andersen3cf52d11999-10-12 22:26:06 +0000635 tarDev = statbuf.st_dev;
636 tarInode = statbuf.st_ino;
Eric Andersencc8ed391999-10-05 16:24:54 +0000637
Eric Andersen3cf52d11999-10-12 22:26:06 +0000638 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000645
Eric Andersen3cf52d11999-10-12 22:26:06 +0000646 /*
647 * Now write an empty block of zeroes to end the archive.
648 */
649 writeTarBlock ("", 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000650
651
Eric Andersen3cf52d11999-10-12 22:26:06 +0000652 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 Andersencc8ed391999-10-05 16:24:54 +0000658}
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 Andersen3cf52d11999-10-12 22:26:06 +0000668static void saveFile (const char *fileName, int seeLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000669{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000670 int status;
671 int mode;
672 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000673
Eric Andersen3cf52d11999-10-12 22:26:06 +0000674 if (verboseFlag)
675 printf ("a %s\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000676
Eric Andersen3cf52d11999-10-12 22:26:06 +0000677 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000682
Eric Andersen3cf52d11999-10-12 22:26:06 +0000683 return;
684 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000685
Eric Andersen3cf52d11999-10-12 22:26:06 +0000686 /*
687 * Find out about the file.
688 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000689#ifdef S_ISLNK
Eric Andersen3cf52d11999-10-12 22:26:06 +0000690 if (seeLinks)
691 status = lstat (fileName, &statbuf);
692 else
Eric Andersencc8ed391999-10-05 16:24:54 +0000693#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000694 status = stat (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000695
Eric Andersen3cf52d11999-10-12 22:26:06 +0000696 if (status < 0) {
697 perror (fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000698
Eric Andersen3cf52d11999-10-12 22:26:06 +0000699 return;
700 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000701
Eric Andersen3cf52d11999-10-12 22:26:06 +0000702 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000707
Eric Andersen3cf52d11999-10-12 22:26:06 +0000708 return;
709 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000710
Eric Andersen3cf52d11999-10-12 22:26:06 +0000711 /*
712 * Check the type of file.
713 */
714 mode = statbuf.st_mode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000715
Eric Andersen3cf52d11999-10-12 22:26:06 +0000716 if (S_ISDIR (mode)) {
717 saveDirectory (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000718
Eric Andersen3cf52d11999-10-12 22:26:06 +0000719 return;
720 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000721
Eric Andersen3cf52d11999-10-12 22:26:06 +0000722 if (S_ISREG (mode)) {
723 saveRegularFile (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000724
Eric Andersen3cf52d11999-10-12 22:26:06 +0000725 return;
726 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000727
Eric Andersen3cf52d11999-10-12 22:26:06 +0000728 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000732}
733
734
735/*
736 * Save a regular file to the tar file.
737 */
738static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000739saveRegularFile (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000740{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000741 int sawEof;
742 int fileFd;
743 int cc;
744 int dataCount;
745 long fullDataCount;
746 char data[TAR_BLOCK_SIZE * 16];
Eric Andersencc8ed391999-10-05 16:24:54 +0000747
Eric Andersen3cf52d11999-10-12 22:26:06 +0000748 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000778 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000779 dataCount = sizeof (data);
Eric Andersencc8ed391999-10-05 16:24:54 +0000780
Eric Andersen3cf52d11999-10-12 22:26:06 +0000781 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 Andersencc8ed391999-10-05 16:24:54 +0000798
799 return;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000800 }
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 Andersencc8ed391999-10-05 16:24:54 +0000812 }
813
Eric Andersen3cf52d11999-10-12 22:26:06 +0000814 /*
815 * Zero fill the rest of the data if necessary.
Eric Andersencc8ed391999-10-05 16:24:54 +0000816 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000817 if (cc < dataCount)
818 memset (data + cc, 0, dataCount - cc);
Eric Andersencc8ed391999-10-05 16:24:54 +0000819
Eric Andersen3cf52d11999-10-12 22:26:06 +0000820 /*
821 * Write the buffer to the TAR file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000822 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000823 writeTarBlock (data, dataCount);
Eric Andersencc8ed391999-10-05 16:24:54 +0000824
Eric Andersen3cf52d11999-10-12 22:26:06 +0000825 fullDataCount -= dataCount;
826 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000827
Eric Andersen3cf52d11999-10-12 22:26:06 +0000828 /*
829 * Close the file.
830 */
831 if ((tostdoutFlag == FALSE) && close (fileFd) < 0)
832 fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +0000833}
834
835
836/*
837 * Save a directory and all of its files to the tar file.
838 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000839static void saveDirectory (const char *dirName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000840{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000841 DIR *dir;
842 struct dirent *entry;
843 int needSlash;
Eric Andersen1b61f411999-10-13 18:56:42 +0000844 char fullName[NAME_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +0000845
Eric Andersen3cf52d11999-10-12 22:26:06 +0000846 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000852
Eric Andersen3cf52d11999-10-12 22:26:06 +0000853 /*
854 * Write out the header for the directory entry.
855 */
856 writeHeader (fullName, statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000857
Eric Andersen3cf52d11999-10-12 22:26:06 +0000858 /*
859 * Open the directory.
860 */
861 dir = opendir (dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000862
Eric Andersen3cf52d11999-10-12 22:26:06 +0000863 if (dir == NULL) {
864 fprintf (stderr, "Cannot read directory \"%s\": %s\n",
865 dirName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +0000866
Eric Andersen3cf52d11999-10-12 22:26:06 +0000867 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 Andersencc8ed391999-10-05 16:24:54 +0000883 }
884
Eric Andersen3cf52d11999-10-12 22:26:06 +0000885 /*
886 * Build the full path name to the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000887 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000888 strcpy (fullName, dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000889
Eric Andersen3cf52d11999-10-12 22:26:06 +0000890 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 Andersencc8ed391999-10-05 16:24:54 +0000898 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000899 saveFile (fullName, TRUE);
900 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000901
Eric Andersen3cf52d11999-10-12 22:26:06 +0000902 /*
903 * All done, close the directory.
904 */
905 closedir (dir);
Eric Andersencc8ed391999-10-05 16:24:54 +0000906}
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 Andersen3cf52d11999-10-12 22:26:06 +0000913static void writeHeader (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000914{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000915 long checkSum;
916 const unsigned char *cp;
917 int len;
918 TarHeader header;
Eric Andersencc8ed391999-10-05 16:24:54 +0000919
Eric Andersen3cf52d11999-10-12 22:26:06 +0000920 /*
921 * Zero the header block in preparation for filling it in.
922 */
923 memset ((char *) &header, 0, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +0000924
Eric Andersen3cf52d11999-10-12 22:26:06 +0000925 /*
926 * Fill in the header.
927 */
928 strcpy (header.name, fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000929
Eric Andersen3cf52d11999-10-12 22:26:06 +0000930 strncpy (header.magic, TAR_MAGIC, sizeof (header.magic));
931 strncpy (header.version, TAR_VERSION, sizeof (header.version));
Eric Andersencc8ed391999-10-05 16:24:54 +0000932
Eric Andersen3cf52d11999-10-12 22:26:06 +0000933 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 Andersencc8ed391999-10-05 16:24:54 +0000938
Eric Andersen3cf52d11999-10-12 22:26:06 +0000939 header.typeFlag = TAR_TYPE_REGULAR;
Eric Andersencc8ed391999-10-05 16:24:54 +0000940
Eric Andersen3cf52d11999-10-12 22:26:06 +0000941 /*
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 Andersencc8ed391999-10-05 16:24:54 +0000947
Eric Andersen3cf52d11999-10-12 22:26:06 +0000948 cp = (const unsigned char *) &header;
949 len = sizeof (header);
950 checkSum = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000951
Eric Andersen3cf52d11999-10-12 22:26:06 +0000952 while (len-- > 0)
953 checkSum += *cp++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000954
Eric Andersen3cf52d11999-10-12 22:26:06 +0000955 putOctal (header.checkSum, sizeof (header.checkSum), checkSum);
Eric Andersencc8ed391999-10-05 16:24:54 +0000956
Eric Andersen3cf52d11999-10-12 22:26:06 +0000957 /*
958 * Write the tar header.
959 */
960 writeTarBlock ((const char *) &header, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +0000961}
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 Andersen3cf52d11999-10-12 22:26:06 +0000969static void writeTarBlock (const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000970{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000971 int partialLength;
972 int completeLength;
973 char fullBlock[TAR_BLOCK_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000974
Eric Andersen3cf52d11999-10-12 22:26:06 +0000975 /*
976 * If we had a write error before, then do nothing more.
977 */
978 if (errorFlag)
979 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000980
Eric Andersen3cf52d11999-10-12 22:26:06 +0000981 /*
982 * Get the amount of complete and partial blocks.
983 */
984 partialLength = len % TAR_BLOCK_SIZE;
985 completeLength = len - partialLength;
Eric Andersencc8ed391999-10-05 16:24:54 +0000986
Eric Andersen3cf52d11999-10-12 22:26:06 +0000987 /*
988 * Write all of the complete blocks.
989 */
990 if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) {
991 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000992
Eric Andersen3cf52d11999-10-12 22:26:06 +0000993 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000994
Eric Andersen3cf52d11999-10-12 22:26:06 +0000995 return;
996 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000997
Eric Andersen3cf52d11999-10-12 22:26:06 +0000998 /*
999 * If there are no partial blocks left, we are done.
1000 */
1001 if (partialLength == 0)
1002 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001003
Eric Andersen3cf52d11999-10-12 22:26:06 +00001004 /*
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 Andersencc8ed391999-10-05 16:24:54 +00001010
Eric Andersen3cf52d11999-10-12 22:26:06 +00001011 /*
1012 * Write the last complete block.
1013 */
1014 if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) {
1015 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001016
Eric Andersen3cf52d11999-10-12 22:26:06 +00001017 errorFlag = TRUE;
1018 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001019}
1020
1021
1022/*
Eric Andersencc8ed391999-10-05 16:24:54 +00001023 * 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 Andersen3cf52d11999-10-12 22:26:06 +00001027static long getOctal (const char *cp, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +00001028{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001029 long val;
Eric Andersencc8ed391999-10-05 16:24:54 +00001030
Eric Andersen3cf52d11999-10-12 22:26:06 +00001031 while ((len > 0) && (*cp == ' ')) {
1032 cp++;
1033 len--;
1034 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001035
Eric Andersen3cf52d11999-10-12 22:26:06 +00001036 if ((len == 0) || !isOctal (*cp))
1037 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001038
Eric Andersen3cf52d11999-10-12 22:26:06 +00001039 val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +00001040
Eric Andersen3cf52d11999-10-12 22:26:06 +00001041 while ((len > 0) && isOctal (*cp)) {
1042 val = val * 8 + *cp++ - '0';
1043 len--;
1044 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001045
Eric Andersen3cf52d11999-10-12 22:26:06 +00001046 while ((len > 0) && (*cp == ' ')) {
1047 cp++;
1048 len--;
1049 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001050
Eric Andersen3cf52d11999-10-12 22:26:06 +00001051 if ((len > 0) && *cp)
1052 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001053
Eric Andersen3cf52d11999-10-12 22:26:06 +00001054 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +00001055}
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 Andersen3cf52d11999-10-12 22:26:06 +00001063static int putOctal (char *cp, int len, long value)
Eric Andersencc8ed391999-10-05 16:24:54 +00001064{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001065 int tempLength;
1066 char *tempString;
1067 char tempBuffer[32];
Eric Andersencc8ed391999-10-05 16:24:54 +00001068
Eric Andersen3cf52d11999-10-12 22:26:06 +00001069 /*
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 Andersencc8ed391999-10-05 16:24:54 +00001074
Eric Andersen3cf52d11999-10-12 22:26:06 +00001075 sprintf (tempString, " %0*lo", len - 2, value);
Eric Andersencc8ed391999-10-05 16:24:54 +00001076
Eric Andersen3cf52d11999-10-12 22:26:06 +00001077 tempLength = strlen (tempString) + 1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001078
Eric Andersen3cf52d11999-10-12 22:26:06 +00001079 /*
1080 * If the string is too large, suppress the leading space.
1081 */
1082 if (tempLength > len) {
1083 tempLength--;
1084 tempString++;
1085 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001086
Eric Andersen3cf52d11999-10-12 22:26:06 +00001087 /*
1088 * If the string is still too large, suppress the trailing null.
1089 */
1090 if (tempLength > len)
1091 tempLength--;
Eric Andersencc8ed391999-10-05 16:24:54 +00001092
Eric Andersen3cf52d11999-10-12 22:26:06 +00001093 /*
1094 * If the string is still too large, fail.
1095 */
1096 if (tempLength > len)
1097 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001098
Eric Andersen3cf52d11999-10-12 22:26:06 +00001099 /*
1100 * Copy the string to the field.
1101 */
1102 memcpy (cp, tempString, len);
Eric Andersencc8ed391999-10-05 16:24:54 +00001103
Eric Andersen3cf52d11999-10-12 22:26:06 +00001104 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001105}
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 Andersenf811e071999-10-09 00:25:00 +00001113static int
Eric Andersen3cf52d11999-10-12 22:26:06 +00001114wantFileName (const char *fileName, int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +00001115{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001116 const char *pathName;
1117 int fileLength;
1118 int pathLength;
Eric Andersencc8ed391999-10-05 16:24:54 +00001119
Eric Andersen3cf52d11999-10-12 22:26:06 +00001120 /*
1121 * If there are no files in the list, then the file is wanted.
1122 */
1123 if (fileCount == 0)
1124 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001125
Eric Andersen3cf52d11999-10-12 22:26:06 +00001126 fileLength = strlen (fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001127
Eric Andersen3cf52d11999-10-12 22:26:06 +00001128 /*
1129 * Check each of the test paths.
1130 */
1131 while (fileCount-- > 0) {
1132 pathName = *fileTable++;
Eric Andersencc8ed391999-10-05 16:24:54 +00001133
Eric Andersen3cf52d11999-10-12 22:26:06 +00001134 pathLength = strlen (pathName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001135
Eric Andersen3cf52d11999-10-12 22:26:06 +00001136 if (fileLength < pathLength)
1137 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +00001138
Eric Andersen3cf52d11999-10-12 22:26:06 +00001139 if (memcmp (fileName, pathName, pathLength) != 0)
1140 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +00001141
Eric Andersen3cf52d11999-10-12 22:26:06 +00001142 if ((fileLength == pathLength) || (fileName[pathLength] == '/')) {
1143 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001144 }
Eric Andersen3cf52d11999-10-12 22:26:06 +00001145 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001146
Eric Andersen3cf52d11999-10-12 22:26:06 +00001147 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001148}
1149
1150
1151
Eric Andersencc8ed391999-10-05 16:24:54 +00001152/* END CODE */