comparison lib/xprintf.c @ 9364:559ef0e161fe

New module: xprintf * modules/xprintf, lib/xprintf.c, lib/xprintf.h: New files.
author Jim Meyering <meyering@redhat.com>
date Fri, 19 Oct 2007 17:09:37 +0200
parents
children 73ad59013feb
comparison
equal deleted inserted replaced
9363:4d4d1419d4ab 9364:559ef0e161fe
1 /* printf wrappers that fail immediately for non-file-related errors
2 Copyright (C) 2007 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #include "xprintf.h"
22
23 #include <errno.h>
24 #include <stdarg.h>
25
26 #include "error.h"
27 #include "exitfail.h"
28 #include "gettext.h"
29
30 /* written by Jim Meyering */
31
32 /* Just like printf, but call error if it fails for any reason
33 for which printf does not set the stream error indicator. */
34 int
35 xprintf (char const *restrict format, ...)
36 {
37 va_list args;
38 va_start (args, format);
39 int err = vprintf (format, args);
40 if (err && (errno == EILSEQ || errno == EINVAL || errno == ENOMEM))
41 error (exit_failure, errno, gettext ("write error"));
42
43 return err;
44 }
45
46 /* Just like fprintf, but call error if it fails for any reason
47 for which printf does not set the stream error indicator. */
48 int
49 xfprintf (FILE *restrict stream, char const *restrict format, ...)
50 {
51 va_list args;
52 va_start (args, format);
53 int err = vfprintf (stream, format, args);
54 if (err && (errno == EILSEQ || errno == EINVAL || errno == ENOMEM))
55 error (exit_failure, errno, gettext ("write error"));
56
57 return err;
58 }