diff options
Diffstat (limited to 'gzip.c')
-rw-r--r-- | gzip.c | 98 |
1 files changed, 64 insertions, 34 deletions
@@ -12,10 +12,11 @@ | |||
12 | //#endif | 12 | //#endif |
13 | 13 | ||
14 | static const char gzip_usage[] = | 14 | static const char gzip_usage[] = |
15 | "gzip [OPTION]... [FILE]...\n\n" | 15 | "gzip [OPTION]... FILE\n\n" |
16 | "Compress FILEs with maximum compression.\n\n" | 16 | "Compress FILE with maximum compression.\n" |
17 | "When FILE is -, reads standard input. Implies -c.\n\n" | ||
17 | "Options:\n" | 18 | "Options:\n" |
18 | "\t-c\tWrite output on standard output\n"; | 19 | "\t-c\tWrite output to standard output instead of FILE.gz\n"; |
19 | 20 | ||
20 | 21 | ||
21 | /* gzip.h -- common declarations for all gzip modules | 22 | /* gzip.h -- common declarations for all gzip modules |
@@ -1731,7 +1732,6 @@ DECLARE(uch, window, 2L*WSIZE); | |||
1731 | 1732 | ||
1732 | int ascii = 0; /* convert end-of-lines to local OS conventions */ | 1733 | int ascii = 0; /* convert end-of-lines to local OS conventions */ |
1733 | int decompress = 0; /* decompress (-d) */ | 1734 | int decompress = 0; /* decompress (-d) */ |
1734 | int tostdout = 0; /* uncompress to stdout (-c) */ | ||
1735 | int no_name = -1; /* don't save or restore the original file name */ | 1735 | int no_name = -1; /* don't save or restore the original file name */ |
1736 | int no_time = -1; /* don't save or restore the original file time */ | 1736 | int no_time = -1; /* don't save or restore the original file time */ |
1737 | int foreground; /* set if program run in foreground */ | 1737 | int foreground; /* set if program run in foreground */ |
@@ -1770,13 +1770,25 @@ unsigned outcnt; /* bytes in output buffer */ | |||
1770 | // char **argv; | 1770 | // char **argv; |
1771 | int gzip_main(int argc, char ** argv) | 1771 | int gzip_main(int argc, char ** argv) |
1772 | { | 1772 | { |
1773 | 1773 | int result; | |
1774 | int inFileNum; | 1774 | int inFileNum; |
1775 | int outFileNum; | 1775 | int outFileNum; |
1776 | struct stat statBuf; | ||
1777 | char* delFileName; | ||
1778 | int tostdout = 0; | ||
1779 | int fromstdin = 0; | ||
1780 | |||
1781 | if (argc==1) | ||
1782 | usage(gzip_usage); | ||
1776 | 1783 | ||
1777 | /* Parse any options */ | 1784 | /* Parse any options */ |
1778 | while (--argc > 0 && **(++argv) == '-') { | 1785 | while (--argc > 0 && **(++argv) == '-') { |
1786 | if (*((*argv)+1) == '\0') { | ||
1787 | fromstdin = 1; | ||
1788 | tostdout = 1; | ||
1789 | } | ||
1779 | while (*(++(*argv))) { | 1790 | while (*(++(*argv))) { |
1791 | fprintf(stderr, "**argv='%c'\n", **argv); | ||
1780 | switch (**argv) { | 1792 | switch (**argv) { |
1781 | case 'c': | 1793 | case 'c': |
1782 | tostdout = 1; | 1794 | tostdout = 1; |
@@ -1817,64 +1829,81 @@ int gzip_main(int argc, char ** argv) | |||
1817 | ALLOC(ush, tab_prefix1, 1L<<(BITS-1)); | 1829 | ALLOC(ush, tab_prefix1, 1L<<(BITS-1)); |
1818 | #endif | 1830 | #endif |
1819 | 1831 | ||
1820 | if (tostdout==1) { | 1832 | if (fromstdin==1) { |
1821 | /* And get to work */ | 1833 | strcpy(ofname, "stdin"); |
1822 | SET_BINARY_MODE(fileno(stdout)); | ||
1823 | strcpy(ifname, "stdin"); | ||
1824 | strcpy(ofname, "stdout"); | ||
1825 | inFileNum=fileno(stdin); | ||
1826 | outFileNum=fileno(stdout); | ||
1827 | 1834 | ||
1828 | /* Get the time stamp on the input file. */ | 1835 | inFileNum=fileno(stdin); |
1829 | time_stamp = 0; /* time unknown by default */ | 1836 | time_stamp = 0; /* time unknown by default */ |
1830 | |||
1831 | ifile_size = -1L; /* convention for unknown size */ | 1837 | ifile_size = -1L; /* convention for unknown size */ |
1832 | |||
1833 | clear_bufs(); /* clear input and output buffers */ | ||
1834 | part_nb = 0; | ||
1835 | |||
1836 | /* Actually do the compression/decompression. */ | ||
1837 | zip(inFileNum, outFileNum); | ||
1838 | |||
1839 | } else { | 1838 | } else { |
1840 | int result; | 1839 | /* Open up the input file */ |
1841 | struct stat statBuf; | ||
1842 | |||
1843 | /* And get to work */ | ||
1844 | if (*argv=='\0') | 1840 | if (*argv=='\0') |
1845 | usage(gzip_usage); | 1841 | usage(gzip_usage); |
1846 | strncpy(ifname, *argv, MAX_PATH_LEN); | 1842 | strncpy(ifname, *argv, MAX_PATH_LEN); |
1847 | strncpy(ofname, *argv, MAX_PATH_LEN-4); | ||
1848 | strcat(ofname, ".gz"); | ||
1849 | 1843 | ||
1844 | /* Open input fille */ | ||
1850 | inFileNum=open( ifname, O_RDONLY); | 1845 | inFileNum=open( ifname, O_RDONLY); |
1851 | if (inFileNum < 0) { | 1846 | if (inFileNum < 0) { |
1852 | perror(ifname); | 1847 | perror(ifname); |
1853 | do_exit(WARNING); | 1848 | do_exit(WARNING); |
1854 | } | 1849 | } |
1850 | /* Get the time stamp on the input file. */ | ||
1855 | result = stat(ifname, &statBuf); | 1851 | result = stat(ifname, &statBuf); |
1856 | if (result < 0) { | 1852 | if (result < 0) { |
1857 | perror(ifname); | 1853 | perror(ifname); |
1858 | do_exit(WARNING); | 1854 | do_exit(WARNING); |
1859 | } | 1855 | } |
1856 | time_stamp = statBuf.st_ctime; | ||
1857 | ifile_size = statBuf.st_size; | ||
1858 | } | ||
1859 | |||
1860 | 1860 | ||
1861 | outFileNum=open( ofname, O_RDONLY); | 1861 | if (tostdout==1) { |
1862 | /* And get to work */ | ||
1863 | strcpy(ofname, "stdout"); | ||
1864 | outFileNum=fileno(stdout); | ||
1865 | SET_BINARY_MODE(fileno(stdout)); | ||
1866 | |||
1867 | clear_bufs(); /* clear input and output buffers */ | ||
1868 | part_nb = 0; | ||
1869 | |||
1870 | /* Actually do the compression/decompression. */ | ||
1871 | zip(inFileNum, outFileNum); | ||
1872 | |||
1873 | } else { | ||
1874 | |||
1875 | /* And get to work */ | ||
1876 | strncpy(ofname, ifname, MAX_PATH_LEN-4); | ||
1877 | strcat(ofname, ".gz"); | ||
1878 | |||
1879 | |||
1880 | /* Open output fille */ | ||
1881 | outFileNum=open( ofname, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW); | ||
1862 | if (outFileNum < 0) { | 1882 | if (outFileNum < 0) { |
1863 | perror(ofname); | 1883 | perror(ofname); |
1864 | do_exit(WARNING); | 1884 | do_exit(WARNING); |
1865 | } | 1885 | } |
1866 | SET_BINARY_MODE(outFileNum); | 1886 | SET_BINARY_MODE(outFileNum); |
1867 | 1887 | /* Set permissions on the file */ | |
1868 | /* Get the time stamp on the input file. */ | 1888 | fchmod(outFileNum, statBuf.st_mode); |
1869 | time_stamp = statBuf.st_ctime; /* time unknown by default */ | ||
1870 | |||
1871 | ifile_size = statBuf.st_size; /* convention for unknown size */ | ||
1872 | 1889 | ||
1873 | clear_bufs(); /* clear input and output buffers */ | 1890 | clear_bufs(); /* clear input and output buffers */ |
1874 | part_nb = 0; | 1891 | part_nb = 0; |
1875 | 1892 | ||
1876 | /* Actually do the compression/decompression. */ | 1893 | /* Actually do the compression/decompression. */ |
1877 | zip(inFileNum, outFileNum); | 1894 | result=zip(inFileNum, outFileNum); |
1895 | close( outFileNum); | ||
1896 | close( inFileNum); | ||
1897 | /* Delete the original file */ | ||
1898 | if (result == OK) | ||
1899 | delFileName=ifname; | ||
1900 | else | ||
1901 | delFileName=ofname; | ||
1902 | |||
1903 | if (unlink (delFileName) < 0) { | ||
1904 | perror (delFileName); | ||
1905 | exit( FALSE); | ||
1906 | } | ||
1878 | } | 1907 | } |
1879 | 1908 | ||
1880 | do_exit(exit_code); | 1909 | do_exit(exit_code); |
@@ -3198,6 +3227,7 @@ int zip(in, out) | |||
3198 | 3227 | ||
3199 | /* Write the header to the gzip file. See algorithm.doc for the format */ | 3228 | /* Write the header to the gzip file. See algorithm.doc for the format */ |
3200 | 3229 | ||
3230 | |||
3201 | method = DEFLATED; | 3231 | method = DEFLATED; |
3202 | put_byte(GZIP_MAGIC[0]); /* magic header */ | 3232 | put_byte(GZIP_MAGIC[0]); /* magic header */ |
3203 | put_byte(GZIP_MAGIC[1]); | 3233 | put_byte(GZIP_MAGIC[1]); |