From 96bcfd346b436aef16b29d9157b80fd4148b1421 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 12 Nov 1999 01:30:18 +0000 Subject: Latest and greatest -Erik --- archival/gzip.c | 98 +++++++++++++++++++++++++++++++++++++-------------------- archival/tar.c | 41 ++++++++++++++++++------ 2 files changed, 96 insertions(+), 43 deletions(-) (limited to 'archival') diff --git a/archival/gzip.c b/archival/gzip.c index 500d6d7..8f2c1c4 100644 --- a/archival/gzip.c +++ b/archival/gzip.c @@ -12,10 +12,11 @@ //#endif static const char gzip_usage[] = - "gzip [OPTION]... [FILE]...\n\n" - "Compress FILEs with maximum compression.\n\n" + "gzip [OPTION]... FILE\n\n" + "Compress FILE with maximum compression.\n" + "When FILE is -, reads standard input. Implies -c.\n\n" "Options:\n" - "\t-c\tWrite output on standard output\n"; + "\t-c\tWrite output to standard output instead of FILE.gz\n"; /* gzip.h -- common declarations for all gzip modules @@ -1731,7 +1732,6 @@ DECLARE(uch, window, 2L*WSIZE); int ascii = 0; /* convert end-of-lines to local OS conventions */ int decompress = 0; /* decompress (-d) */ -int tostdout = 0; /* uncompress to stdout (-c) */ int no_name = -1; /* don't save or restore the original file name */ int no_time = -1; /* don't save or restore the original file time */ int foreground; /* set if program run in foreground */ @@ -1770,13 +1770,25 @@ unsigned outcnt; /* bytes in output buffer */ // char **argv; int gzip_main(int argc, char ** argv) { - + int result; int inFileNum; int outFileNum; + struct stat statBuf; + char* delFileName; + int tostdout = 0; + int fromstdin = 0; + + if (argc==1) + usage(gzip_usage); /* Parse any options */ while (--argc > 0 && **(++argv) == '-') { + if (*((*argv)+1) == '\0') { + fromstdin = 1; + tostdout = 1; + } while (*(++(*argv))) { + fprintf(stderr, "**argv='%c'\n", **argv); switch (**argv) { case 'c': tostdout = 1; @@ -1817,64 +1829,81 @@ int gzip_main(int argc, char ** argv) ALLOC(ush, tab_prefix1, 1L<<(BITS-1)); #endif - if (tostdout==1) { - /* And get to work */ - SET_BINARY_MODE(fileno(stdout)); - strcpy(ifname, "stdin"); - strcpy(ofname, "stdout"); - inFileNum=fileno(stdin); - outFileNum=fileno(stdout); + if (fromstdin==1) { + strcpy(ofname, "stdin"); - /* Get the time stamp on the input file. */ + inFileNum=fileno(stdin); time_stamp = 0; /* time unknown by default */ - ifile_size = -1L; /* convention for unknown size */ - - clear_bufs(); /* clear input and output buffers */ - part_nb = 0; - - /* Actually do the compression/decompression. */ - zip(inFileNum, outFileNum); - } else { - int result; - struct stat statBuf; - - /* And get to work */ + /* Open up the input file */ if (*argv=='\0') usage(gzip_usage); strncpy(ifname, *argv, MAX_PATH_LEN); - strncpy(ofname, *argv, MAX_PATH_LEN-4); - strcat(ofname, ".gz"); + /* Open input fille */ inFileNum=open( ifname, O_RDONLY); if (inFileNum < 0) { perror(ifname); do_exit(WARNING); } + /* Get the time stamp on the input file. */ result = stat(ifname, &statBuf); if (result < 0) { perror(ifname); do_exit(WARNING); } + time_stamp = statBuf.st_ctime; + ifile_size = statBuf.st_size; + } + - outFileNum=open( ofname, O_RDONLY); + if (tostdout==1) { + /* And get to work */ + strcpy(ofname, "stdout"); + outFileNum=fileno(stdout); + SET_BINARY_MODE(fileno(stdout)); + + clear_bufs(); /* clear input and output buffers */ + part_nb = 0; + + /* Actually do the compression/decompression. */ + zip(inFileNum, outFileNum); + + } else { + + /* And get to work */ + strncpy(ofname, ifname, MAX_PATH_LEN-4); + strcat(ofname, ".gz"); + + + /* Open output fille */ + outFileNum=open( ofname, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW); if (outFileNum < 0) { perror(ofname); do_exit(WARNING); } SET_BINARY_MODE(outFileNum); - - /* Get the time stamp on the input file. */ - time_stamp = statBuf.st_ctime; /* time unknown by default */ - - ifile_size = statBuf.st_size; /* convention for unknown size */ + /* Set permissions on the file */ + fchmod(outFileNum, statBuf.st_mode); clear_bufs(); /* clear input and output buffers */ part_nb = 0; /* Actually do the compression/decompression. */ - zip(inFileNum, outFileNum); + result=zip(inFileNum, outFileNum); + close( outFileNum); + close( inFileNum); + /* Delete the original file */ + if (result == OK) + delFileName=ifname; + else + delFileName=ofname; + + if (unlink (delFileName) < 0) { + perror (delFileName); + exit( FALSE); + } } do_exit(exit_code); @@ -3198,6 +3227,7 @@ int zip(in, out) /* Write the header to the gzip file. See algorithm.doc for the format */ + method = DEFLATED; put_byte(GZIP_MAGIC[0]); /* magic header */ put_byte(GZIP_MAGIC[1]); diff --git a/archival/tar.c b/archival/tar.c index ed6f3b6..5478af8 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -10,6 +10,9 @@ * Modified for busybox by Erik Andersen * Adjusted to grok stdin/stdout options. * + * Modified to handle device special files by Matt Porter + * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -34,6 +37,7 @@ #include #include #include +#include static const char tar_usage[] = @@ -377,12 +381,15 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) int uid; int gid; int checkSum; + int major; + int minor; long size; time_t mtime; const char *name; int cc; int hardLink; int softLink; + int devFileFlag; /* * If the block is completely empty, then this is the end of the @@ -411,6 +418,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) size = getOctal (hp->size, sizeof (hp->size)); mtime = getOctal (hp->mtime, sizeof (hp->mtime)); checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum)); + major = getOctal (hp->devMajor, sizeof (hp->devMajor)); + minor = getOctal (hp->devMinor, sizeof (hp->devMinor)); if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) { if (badHeader==FALSE) @@ -423,6 +432,7 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) badHeader = FALSE; skipFileFlag = FALSE; + devFileFlag = FALSE; /* * Check for the file modes. @@ -434,12 +444,10 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); /* - * Check for a directory or a regular file. + * Check for a directory. */ if (name[strlen (name) - 1] == '/') mode |= S_IFDIR; - else if ((mode & S_IFMT) == 0) - mode |= S_IFREG; /* * Check for absolute paths in the file. @@ -462,7 +470,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) * If not, then set up to skip it. */ if (wantFileName (name, fileCount, fileTable) == FALSE) { - if (!hardLink && !softLink && S_ISREG (mode)) { + if ( !hardLink && !softLink && (S_ISREG (mode) || S_ISCHR (mode) + || S_ISBLK (mode) || S_ISSOCK(mode) || S_ISFIFO(mode) ) ) { inHeader = (size == 0)? TRUE : FALSE; dataCc = size; } @@ -487,7 +496,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) printf (" (link to \"%s\")", hp->linkName); else if (softLink) printf (" (symlink to \"%s\")", hp->linkName); - else if (S_ISREG (mode)) { + else if (S_ISREG (mode) || S_ISCHR (mode) || S_ISBLK (mode) || + S_ISSOCK(mode) || S_ISFIFO(mode) ) { inHeader = (size == 0)? TRUE : FALSE; dataCc = size; } @@ -543,8 +553,17 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) */ if (tostdoutFlag == TRUE) outFd = STDOUT; - else - outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode); + else { + if ( S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) ) { + devFileFlag = TRUE; + outFd = mknod (name, mode, makedev(major, minor) ); + } + else if (S_ISFIFO(mode) ) { + outFd = mkfifo(name, mode); + } else { + outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode); + } + } if (outFd < 0) { perror (name); @@ -555,7 +574,7 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable) /* * If the file is empty, then that's all we need to do. */ - if (size == 0 && tostdoutFlag == FALSE) { + if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) { (void) close (outFd); outFd = -1; } @@ -735,12 +754,16 @@ static void saveFile (const char *fileName, int seeLinks) return; } - if (S_ISREG (mode)) { saveRegularFile (fileName, &statbuf); return; } + + /* Some day add support for tarring these up... but not today. :) */ +// if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) { +// fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName); +// } /* * The file is a strange type of file, ignore it. -- cgit v1.1