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_HEADER_ERROR:
53 return _("Unsupported options");
55 case LZMA_UNSUPPORTED_CHECK:
56 return _("Unsupported integrity check type");
65 set_exit_status(exit_status_type new_status)
67 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
68 pthread_mutex_lock(&mutex);
70 if (new_status != WARNING || exit_status == SUCCESS)
71 exit_status = new_status;
73 pthread_mutex_unlock(&mutex);
78 extern void lzma_attribute((noreturn))
81 // Close stdout. If something goes wrong, print an error message
84 const int ferror_err = ferror(stdout);
85 const int fclose_err = fclose(stdout);
87 errmsg(V_ERROR, _("Writing to standard output "
88 "failed: %s"), strerror(errno));
90 } else if (ferror_err) {
91 // Some error has occurred but we have no clue about
92 // the reason since fclose() succeeded.
93 errmsg(V_ERROR, _("Writing to standard output "
94 "failed: %s"), "Unknown error");
99 // Close stderr. If something goes wrong, there's nothing where we
100 // could print an error message. Just set the exit status.
102 const int ferror_err = ferror(stderr);
103 const int fclose_err = fclose(stderr);
104 if (fclose_err || ferror_err)
112 extern void lzma_attribute((format(printf, 2, 3)))
113 errmsg(verbosity_type v, const char *fmt, ...)
117 if (v <= verbosity) {
120 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
121 pthread_mutex_lock(&mutex);
123 fprintf(stderr, "%s: ", argv0);
124 vfprintf(stderr, fmt, ap);
125 fprintf(stderr, "\n");
127 pthread_mutex_unlock(&mutex);
133 set_exit_status(ERROR);
134 else if (v == V_WARNING)
135 set_exit_status(WARNING);
144 errmsg(V_ERROR, "%s", strerror(ENOMEM));
153 errmsg(V_ERROR, _("Internal error (bug)"));