/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
- Copyright (C) 1999, 2001, 2002, 2006, 2007 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2001-2002, 2006-2007, 2009 Free Software Foundation,
+ Inc.
Copyright (C) 1992-1993 Jean-loup Gailly
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, or (at your option)
+ the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
* For the meaning of all compilation flags, see comments in Makefile.in.
*/
-#ifdef RCSID
-static char rcsid[] = "$Id: gzip.c,v 1.16 2007/03/20 05:09:51 eggert Exp $";
-#endif
-
#include <config.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
+#include "closein.h"
#include "tailor.h"
#include "gzip.h"
#include "lzw.h"
unsigned inptr; /* index of next byte to be processed in inbuf */
unsigned outcnt; /* bytes in output buffer */
+static int handled_sig[] =
+ {
+ /* SIGINT must be first, as 'foreground' depends on it. */
+ SIGINT
+
+#ifdef SIGHUP
+ , SIGHUP
+#endif
+#ifdef SIGPIPE
+ , SIGPIPE
+#else
+# define SIGPIPE 0
+#endif
+#ifdef SIGTERM
+ , SIGTERM
+#endif
+#ifdef SIGXCPU
+ , SIGXCPU
+#endif
+#ifdef SIGXFSZ
+ , SIGXFSZ
+#endif
+ };
+
struct option longopts[] =
{
/* { name has_arg *flag val } */
program_name = gzip_base_name (argv[0]);
proglen = strlen (program_name);
+ atexit (close_stdin);
+
/* Suppress .exe for MSDOS, OS/2 and VMS: */
if (4 < proglen && strequ (program_name + proglen - 4, ".exe"))
program_name[proglen - 4] = '\0';
clear_bufs(); /* clear input and output buffers */
to_stdout = 1;
part_nb = 0;
+ ifd = fileno(stdin);
if (decompress) {
method = get_method(ifd);
}
/* ofname might be changed later if infile contains an original name */
- } else if (suff != NULL) {
+ } else if (suff && ! force) {
/* Avoid annoying messages with -r (see treat_dir()) */
if (verbose || (!recursive && !quiet)) {
/* Don't use WARN, as it affects exit status. */
/* If try_byte returned EOF, magic[1] == (char) EOF. */
} else {
magic[0] = (char)get_byte();
- magic[1] = (char)get_byte();
- imagic1 = 0; /* avoid lint warning */
+ if (magic[0]) {
+ magic[1] = (char)get_byte();
+ imagic1 = 0; /* avoid lint warning */
+ } else {
+ imagic1 = try_byte ();
+ magic[1] = (char) imagic1;
+ }
}
method = -1; /* unknown yet */
part_nb++; /* number of parts in gzip file */
}
if ((flags & CONTINUATION) != 0) {
fprintf(stderr,
- "%s: %s is a a multi-part gzip file -- not supported\n",
+ "%s: %s is a multi-part gzip file -- not supported\n",
program_name, ifname);
exit_code = ERROR;
if (force <= 1) return -1;
}
}
- if (futimens (ofd, ofname, timespec) != 0)
+ if (gl_futimens (ofd, ofname, timespec) != 0)
{
int e = errno;
WARN ((stderr, "%s: ", program_name));
static void
install_signal_handlers ()
{
- static int sig[] =
- {
- /* SIGINT must be first, as 'foreground' depends on it. */
- SIGINT
-
-#ifdef SIGHUP
- , SIGHUP
-#endif
-#ifdef SIGPIPE
- , SIGPIPE
-#else
-# define SIGPIPE 0
-#endif
-#ifdef SIGTERM
- , SIGTERM
-#endif
-#ifdef SIGXCPU
- , SIGXCPU
-#endif
-#ifdef SIGXFSZ
- , SIGXFSZ
-#endif
- };
- int nsigs = sizeof sig / sizeof sig[0];
+ int nsigs = sizeof handled_sig / sizeof handled_sig[0];
int i;
#if SA_NOCLDSTOP
sigemptyset (&caught_signals);
for (i = 0; i < nsigs; i++)
{
- sigaction (sig[i], NULL, &act);
+ sigaction (handled_sig[i], NULL, &act);
if (act.sa_handler != SIG_IGN)
- sigaddset (&caught_signals, sig[i]);
+ sigaddset (&caught_signals, handled_sig[i]);
}
act.sa_handler = abort_gzip_signal;
act.sa_flags = 0;
for (i = 0; i < nsigs; i++)
- if (sigismember (&caught_signals, sig[i]))
+ if (sigismember (&caught_signals, handled_sig[i]))
{
if (i == 0)
foreground = 1;
- sigaction (sig[i], &act, NULL);
+ sigaction (handled_sig[i], &act, NULL);
}
#else
for (i = 0; i < nsigs; i++)
- if (signal (sig[i], SIG_IGN) != SIG_IGN)
+ if (signal (handled_sig[i], SIG_IGN) != SIG_IGN)
{
if (i == 0)
foreground = 1;
- signal (sig[i], abort_gzip_signal);
- siginterrupt (sig[i], 1);
+ signal (handled_sig[i], abort_gzip_signal);
+ siginterrupt (handled_sig[i], 1);
}
#endif
}
if (in_exit) exit(exitcode);
in_exit = 1;
- if (env != NULL) free(env), env = NULL;
- if (args != NULL) free((char*)args), args = NULL;
+ free(env);
+ env = NULL;
+ free(args);
+ args = NULL;
FREE(inbuf);
FREE(outbuf);
FREE(d_buf);