1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Error message printing
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 ///////////////////////////////////////////////////////////////////////////////
24 exit_status_type exit_status = SUCCESS;
25 verbosity_type verbosity = V_WARNING;
27 volatile sig_atomic_t user_abort = 0;
31 str_strm_error(lzma_ret code)
35 return _("Operation successful");
38 return _("Operation finished successfully");
41 return _("Internal error (bug)");
44 return _("Compressed data is corrupt");
47 return strerror(ENOMEM);
50 return _("Unexpected end of input");
52 case LZMA_OPTIONS_ERROR:
53 return _("Unsupported options");
55 case LZMA_UNSUPPORTED_CHECK:
56 return _("Unsupported integrity check type");
58 case LZMA_MEMLIMIT_ERROR:
59 return _("Memory usage limit reached");
61 case LZMA_FORMAT_ERROR:
62 return _("File format not recognized");
71 set_exit_status(exit_status_type new_status)
73 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
74 pthread_mutex_lock(&mutex);
76 if (new_status != WARNING || exit_status == SUCCESS)
77 exit_status = new_status;
79 pthread_mutex_unlock(&mutex);
84 extern void lzma_attribute((noreturn))
87 // Close stdout. If something goes wrong, print an error message
90 const int ferror_err = ferror(stdout);
91 const int fclose_err = fclose(stdout);
93 errmsg(V_ERROR, _("Writing to standard output "
94 "failed: %s"), strerror(errno));
96 } else if (ferror_err) {
97 // Some error has occurred but we have no clue about
98 // the reason since fclose() succeeded.
99 errmsg(V_ERROR, _("Writing to standard output "
100 "failed: %s"), "Unknown error");
105 // Close stderr. If something goes wrong, there's nothing where we
106 // could print an error message. Just set the exit status.
108 const int ferror_err = ferror(stderr);
109 const int fclose_err = fclose(stderr);
110 if (fclose_err || ferror_err)
118 extern void lzma_attribute((format(printf, 2, 3)))
119 errmsg(verbosity_type v, const char *fmt, ...)
123 if (v <= verbosity) {
126 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
127 pthread_mutex_lock(&mutex);
129 fprintf(stderr, "%s: ", argv0);
130 vfprintf(stderr, fmt, ap);
131 fprintf(stderr, "\n");
133 pthread_mutex_unlock(&mutex);
139 set_exit_status(ERROR);
140 else if (v == V_WARNING)
141 set_exit_status(WARNING);
150 errmsg(V_ERROR, "%s", strerror(ENOMEM));
159 errmsg(V_ERROR, _("Internal error (bug)"));