1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file tuklib_exit.c
6 /// \brief Close stdout and stderr, and exit
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tuklib_common.h"
18 #include "tuklib_gettext.h"
19 #include "tuklib_progname.h"
20 #include "tuklib_exit.h"
24 tuklib_exit(int status
, int err_status
, int show_error
)
26 if (status
!= err_status
) {
27 // Close stdout. If something goes wrong,
28 // print an error message to stderr.
29 const int ferror_err
= ferror(stdout
);
30 const int fclose_err
= fclose(stdout
);
31 if (ferror_err
|| fclose_err
) {
34 // If it was fclose() that failed, we have the reason
35 // in errno. If only ferror() indicated an error,
36 // we have no idea what the reason was.
38 fprintf(stderr
, "%s: %s: %s\n", progname
,
39 _("Writing to standard "
41 fclose_err
? strerror(errno
)
42 : _("Unknown error"));
46 if (status
!= err_status
) {
47 // Close stderr. If something goes wrong, there's
48 // nothing where we could print an error message.
49 // Just set the exit status.
50 const int ferror_err
= ferror(stderr
);
51 const int fclose_err
= fclose(stderr
);
52 if (fclose_err
|| ferror_err
)