aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-02-05 22:31:48 +0000
committerkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-02-05 22:31:48 +0000
commit046683f4f723c5e94b4886134ba2193defaf476c (patch)
tree7c3cfb740d90bcdaffe7f09ddbd90531ac2e53e9
parentb962948eff2fdc3aa013502a9285580a482fb6b4 (diff)
downloadbusybox-w32-046683f4f723c5e94b4886134ba2193defaf476c.tar.gz
busybox-w32-046683f4f723c5e94b4886134ba2193defaf476c.tar.bz2
busybox-w32-046683f4f723c5e94b4886134ba2193defaf476c.zip
* archival/gzip.c (ifname, ofname): Delete.
(gzip_main): Handle multiple files. * include/usage.h (gzip_trivial_usage): Allow multiple FILEs. (gzip_full_usage): Ditto. * testsuite/gzip/gzip-accepts-multiple-files: New. * testsuite/gzip/gzip-removes-original-file: New. git-svn-id: svn://busybox.net/trunk/busybox@4191 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--archival/gzip.c129
-rw-r--r--include/usage.h4
-rw-r--r--testsuite/gzip/gzip-accepts-multiple-files3
-rw-r--r--testsuite/gzip/gzip-removes-original-file3
4 files changed, 70 insertions, 69 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index 732a83e90..fed93907b 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -317,8 +317,6 @@ static long ifile_size; /* input file size, -1 for devices (debug only) */
317static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ 317static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
318static int z_len; /* strlen(z_suffix) */ 318static int z_len; /* strlen(z_suffix) */
319 319
320static char ifname[MAX_PATH_LEN]; /* input file name */
321static char ofname[MAX_PATH_LEN]; /* output file name */
322static int ifd; /* input file descriptor */ 320static int ifd; /* input file descriptor */
323static int ofd; /* output file descriptor */ 321static int ofd; /* output file descriptor */
324static unsigned insize; /* valid bytes in inbuf */ 322static unsigned insize; /* valid bytes in inbuf */
@@ -1214,7 +1212,6 @@ int gzip_main(int argc, char **argv)
1214 struct stat statBuf; 1212 struct stat statBuf;
1215 char *delFileName; 1213 char *delFileName;
1216 int tostdout = 0; 1214 int tostdout = 0;
1217 int fromstdin = 0;
1218 int force = 0; 1215 int force = 0;
1219 int opt; 1216 int opt;
1220 1217
@@ -1241,16 +1238,6 @@ int gzip_main(int argc, char **argv)
1241 show_usage(); 1238 show_usage();
1242 } 1239 }
1243 } 1240 }
1244 if ((optind == argc) || (strcmp(argv[optind], "-") == 0)) {
1245 fromstdin = 1;
1246 tostdout = 1;
1247 }
1248
1249 if (argc - optind > 1)
1250 show_usage ();
1251
1252 if (isatty(fileno(stdout)) && tostdout==1 && force==0)
1253 error_msg_and_die( "compressed data not written to terminal. Use -f to force it.");
1254 1241
1255 foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; 1242 foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
1256 if (foreground) { 1243 if (foreground) {
@@ -1277,70 +1264,78 @@ int gzip_main(int argc, char **argv)
1277 ALLOC(uch, window, 2L * WSIZE); 1264 ALLOC(uch, window, 2L * WSIZE);
1278 ALLOC(ush, tab_prefix, 1L << BITS); 1265 ALLOC(ush, tab_prefix, 1L << BITS);
1279 1266
1280 if (fromstdin == 1) { 1267 clear_bufs();
1281 strcpy(ofname, "stdin"); 1268 part_nb = 0;
1282 1269
1283 inFileNum = fileno(stdin); 1270 if (optind == argc) {
1284 time_stamp = 0; /* time unknown by default */ 1271 time_stamp = 0;
1285 ifile_size = -1L; /* convention for unknown size */ 1272 ifile_size = -1L;
1273 zip(STDIN_FILENO, STDOUT_FILENO);
1286 } else { 1274 } else {
1287 /* Open up the input file */ 1275 int i;
1288 strncpy(ifname, argv[optind], MAX_PATH_LEN);
1289
1290 /* Open input file */
1291 inFileNum = open(ifname, O_RDONLY);
1292 if (inFileNum < 0 || stat(ifname, &statBuf) < 0)
1293 perror_msg_and_die("%s", ifname);
1294 /* Get the time stamp on the input file. */
1295 time_stamp = statBuf.st_ctime;
1296 ifile_size = statBuf.st_size;
1297 }
1298 1276
1277 for (i = optind; i < argc; i++) {
1278 char *path = NULL;
1299 1279
1300 if (tostdout == 1) { 1280 if (strcmp(argv[i], "-") == 0) {
1301 /* And get to work */ 1281 time_stamp = 0;
1302 strcpy(ofname, "stdout"); 1282 ifile_size = -1L;
1303 outFileNum = fileno(stdout); 1283 inFileNum = STDIN_FILENO;
1284 outFileNum = STDOUT_FILENO;
1285 } else {
1286 inFileNum = open(argv[i], O_RDONLY);
1287 if (inFileNum < 0 || fstat (inFileNum, &statBuf) < 0)
1288 perror_msg_and_die("%s", argv[i]);
1289 time_stamp = statBuf.st_ctime;
1290 ifile_size = statBuf.st_size;
1291
1292 if (!tostdout) {
1293 path = xmalloc(strlen(argv[i]) + 4);
1294 strcpy(path, argv[i]);
1295 strcat(path, ".gz");
1296
1297 /* Open output file */
1298#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
1299 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
1300#else
1301 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL);
1302#endif
1303 if (outFileNum < 0) {
1304 perror_msg("%s", path);
1305 free(path);
1306 continue;
1307 }
1308
1309 /* Set permissions on the file */
1310 fchmod(outFileNum, statBuf.st_mode);
1311 } else
1312 outFileNum = STDOUT_FILENO;
1313 }
1304 1314
1305 clear_bufs(); /* clear input and output buffers */ 1315 if (path == NULL && force == 0) {
1306 part_nb = 0; 1316 perror_msg("compressed data not written to a terminal. Use -f to force compression.");
1317 free(path);
1318 continue;
1319 }
1307 1320
1308 /* Actually do the compression/decompression. */ 1321 result = zip(inFileNum, outFileNum);
1309 zip(inFileNum, outFileNum);
1310 1322
1311 } else { 1323 if (path != NULL) {
1324 close (inFileNum);
1325 close (outFileNum);
1312 1326
1313 /* And get to work */ 1327 /* Delete the original file */
1314 strncpy(ofname, ifname, MAX_PATH_LEN - 4); 1328 if (result == OK)
1315 strcat(ofname, ".gz"); 1329 delFileName = argv[i];
1330 else
1331 delFileName = path;
1316 1332
1333 if (unlink(delFileName) < 0)
1334 perror_msg("%s", delFileName);
1335 }
1317 1336
1318 /* Open output fille */ 1337 free(path);
1319#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) 1338 }
1320 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
1321#else
1322 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
1323#endif
1324 if (outFileNum < 0)
1325 perror_msg_and_die("%s", ofname);
1326 /* Set permissions on the file */
1327 fchmod(outFileNum, statBuf.st_mode);
1328
1329 clear_bufs(); /* clear input and output buffers */
1330 part_nb = 0;
1331
1332 /* Actually do the compression/decompression. */
1333 result = zip(inFileNum, outFileNum);
1334 close(outFileNum);
1335 close(inFileNum);
1336 /* Delete the original file */
1337 if (result == OK)
1338 delFileName = ifname;
1339 else
1340 delFileName = ofname;
1341
1342 if (unlink(delFileName) < 0)
1343 perror_msg_and_die("%s", delFileName);
1344 } 1339 }
1345 1340
1346 return(exit_code); 1341 return(exit_code);
diff --git a/include/usage.h b/include/usage.h
index 2e65c6f45..c4ad0440b 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -628,9 +628,9 @@
628 "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n" 628 "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
629 629
630#define gzip_trivial_usage \ 630#define gzip_trivial_usage \
631 "[OPTION]... [FILE]" 631 "[OPTION]... [FILE]..."
632#define gzip_full_usage \ 632#define gzip_full_usage \
633 "Compress FILE with maximum compression.\n" \ 633 "Compress FILE(s) with maximum compression.\n" \
634 "When FILE is '-' or unspecified, reads standard input. Implies -c.\n\n" \ 634 "When FILE is '-' or unspecified, reads standard input. Implies -c.\n\n" \
635 "Options:\n" \ 635 "Options:\n" \
636 "\t-c\tWrite output to standard output instead of FILE.gz\n" \ 636 "\t-c\tWrite output to standard output instead of FILE.gz\n" \
diff --git a/testsuite/gzip/gzip-accepts-multiple-files b/testsuite/gzip/gzip-accepts-multiple-files
new file mode 100644
index 000000000..8f0d9c845
--- /dev/null
+++ b/testsuite/gzip/gzip-accepts-multiple-files
@@ -0,0 +1,3 @@
1touch foo bar
2busybox gzip foo bar
3test -f foo.gz -a -f bar.gz
diff --git a/testsuite/gzip/gzip-removes-original-file b/testsuite/gzip/gzip-removes-original-file
new file mode 100644
index 000000000..b9cb995bc
--- /dev/null
+++ b/testsuite/gzip/gzip-removes-original-file
@@ -0,0 +1,3 @@
1touch foo
2busybox gzip foo
3test ! -f foo