diff options
Diffstat (limited to 'archival')
-rw-r--r-- | archival/gzip.c | 129 |
1 files changed, 62 insertions, 67 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) */ | |||
317 | static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ | 317 | static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ |
318 | static int z_len; /* strlen(z_suffix) */ | 318 | static int z_len; /* strlen(z_suffix) */ |
319 | 319 | ||
320 | static char ifname[MAX_PATH_LEN]; /* input file name */ | ||
321 | static char ofname[MAX_PATH_LEN]; /* output file name */ | ||
322 | static int ifd; /* input file descriptor */ | 320 | static int ifd; /* input file descriptor */ |
323 | static int ofd; /* output file descriptor */ | 321 | static int ofd; /* output file descriptor */ |
324 | static unsigned insize; /* valid bytes in inbuf */ | 322 | static 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); |