1 ///////////////////////////////////////////////////////////////////////////////
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
21 #include "open_stdxxx.h"
24 static sig_atomic_t exit_signal = 0;
28 signal_handler(int sig)
30 // FIXME Is this thread-safe together with main()?
39 establish_signal_handlers(void)
42 sa.sa_handler = &signal_handler;
43 sigfillset(&sa.sa_mask);
46 static const int sigs[] = {
55 for (size_t i = 0; i < sizeof(sigs) / sizeof(sigs[0]); ++i) {
56 if (sigaction(sigs[i], &sa, NULL)) {
57 errmsg(V_ERROR, _("Cannot establish signal handlers"));
63 SIGINFO/SIGUSR1 for status reporting?
71 const bool ret = isatty(STDIN_FILENO);
73 // FIXME: Other threads may print between these lines.
74 // Maybe that should be fixed. Not a big issue in practice.
75 errmsg(V_ERROR, _("Compressed data not read from "
77 errmsg(V_ERROR, _("Use `--force' to force decompression."));
88 const bool ret = isatty(STDOUT_FILENO);
90 errmsg(V_ERROR, _("Compressed data not written to "
92 errmsg(V_ERROR, _("Use `--force' to force decompression."));
105 char *name = malloc(size);
112 const int c = fgetc(opt_files_file);
116 if (ferror(opt_files_file))
117 errmsg(V_ERROR, _("%s: Error reading "
122 errmsg(V_ERROR, _("%s: Unexpected end of "
123 "input when reading "
124 "filenames"), opt_files_name);
129 if (c == '\0' || c == opt_files_split)
136 char *tmp = realloc(name, size);
155 main(int argc, char **argv)
157 // Make sure that stdin, stdout, and and stderr are connected to
158 // a valid file descriptor. Exit immediatelly with exit code ERROR
159 // if we cannot make the file descriptors valid. Maybe we should
160 // print an error message, but our stderr could be screwed anyway.
163 // Set the program invocation name used in various messages.
166 setlocale(LC_ALL, "en_US.UTF-8");
167 bindtextdomain(PACKAGE, LOCALEDIR);
170 // Set hardware-dependent default values. These can be overriden
171 // on the command line, thus this must be done before parse_args().
174 char **files = parse_args(argc, argv);
176 if (opt_mode == MODE_COMPRESS && opt_stdout && is_tty_stdout())
179 if (opt_mode == MODE_COMPRESS)
187 if (opt_mode == MODE_LIST) {
188 errmsg(V_ERROR, "--list is not implemented yet.");
192 // Hook the signal handlers. We don't need these before we start
193 // the actual action, so this is done after parsing the command
195 establish_signal_handlers();
197 while (*files != NULL && !user_abort) {
198 if (strcmp("-", *files) == 0) {
200 if (opt_mode == MODE_COMPRESS) {
201 if (is_tty_stdout()) {
205 } else if (is_tty_stdin()) {
211 if (opt_files_name == stdin_filename) {
212 errmsg(V_ERROR, _("Cannot read data from "
213 "standard input when "
215 "from standard input"));
220 *files = (char *)stdin_filename;
223 process_file(*files++);
226 if (opt_files_name != NULL) {
228 char *name = read_name();
238 if (opt_files_name != stdin_filename)
239 (void)fclose(opt_files_file);
244 if (exit_signal != 0) {
246 sa.sa_handler = SIG_DFL;
247 sigfillset(&sa.sa_mask);
249 sigaction(exit_signal, &sa, NULL);
253 my_exit(exit_status);