summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2012-04-11 12:37:59 +0200
committerKarel Zak2012-04-11 12:37:59 +0200
commit6259c257d873798ea87341548d5e9c1d7821fce3 (patch)
tree8ef976bde5eabf8f549f2cd1407f321ac5032f2d
parentlibuuid: use randutils (diff)
parentdisk-utils: verify writing to streams was successful (diff)
downloadkernel-qcow2-util-linux-6259c257d873798ea87341548d5e9c1d7821fce3.tar.gz
kernel-qcow2-util-linux-6259c257d873798ea87341548d5e9c1d7821fce3.tar.xz
kernel-qcow2-util-linux-6259c257d873798ea87341548d5e9c1d7821fce3.zip
Merge branch 'close_stream' of git://github.com/kerolasa/lelux-utiliteetit
* 'close_stream' of git://github.com/kerolasa/lelux-utiliteetit: disk-utils: verify writing to streams was successful fdisk: verify writing to streams was successful getopt: verify writing to streams was successful hwclock: verify writing to streams was successful login-utils: verify writing to streams was successful misc-utils: verify writing to streams was successful mount: verify writing to streams was successful partx: verify writing to streams was successful schedutils: verify writing to streams was successful sys-utils: verify writing to streams was successful term-utils: verify writing to streams was successful text-utils: verify writing to streams was successful include: add stream error checking facility Conflicts: fdisk/fdisk.c
-rw-r--r--disk-utils/blockdev.c2
-rw-r--r--disk-utils/elvtune.c2
-rw-r--r--disk-utils/fdformat.c2
-rw-r--r--disk-utils/fsck.c2
-rw-r--r--disk-utils/fsck.cramfs.c2
-rw-r--r--disk-utils/fsck.minix.c2
-rw-r--r--disk-utils/isosize.c2
-rw-r--r--disk-utils/mkfs.bfs.c2
-rw-r--r--disk-utils/mkfs.c2
-rw-r--r--disk-utils/mkfs.cramfs.c2
-rw-r--r--disk-utils/mkfs.minix.c2
-rw-r--r--disk-utils/mkswap.c2
-rw-r--r--disk-utils/raw.c2
-rw-r--r--disk-utils/swaplabel.c2
-rw-r--r--fdisk/cfdisk.c2
-rw-r--r--fdisk/fdisk.c2
-rw-r--r--fdisk/gpt.c2
-rw-r--r--fdisk/partitiontype.c3
-rw-r--r--fdisk/sfdisk.c2
-rw-r--r--getopt/getopt.c2
-rw-r--r--hwclock/hwclock.c7
-rw-r--r--include/Makefile.am1
-rw-r--r--include/closestream.h41
-rw-r--r--login-utils/chfn.c2
-rw-r--r--login-utils/chsh.c2
-rw-r--r--login-utils/islocal.c2
-rw-r--r--login-utils/last.c2
-rw-r--r--login-utils/logindefs.c2
-rw-r--r--login-utils/newgrp.c2
-rw-r--r--login-utils/setpwnam.c6
-rw-r--r--login-utils/sulogin.c2
-rw-r--r--login-utils/vipw.c6
-rw-r--r--misc-utils/blkid.c2
-rw-r--r--misc-utils/cal.c2
-rw-r--r--misc-utils/ddate.c2
-rw-r--r--misc-utils/findfs.c2
-rw-r--r--misc-utils/findmnt.c2
-rw-r--r--misc-utils/kill.c2
-rw-r--r--misc-utils/logger.c2
-rw-r--r--misc-utils/look.c2
-rw-r--r--misc-utils/lsblk.c2
-rw-r--r--misc-utils/lslocks.c2
-rw-r--r--misc-utils/mcookie.c11
-rw-r--r--misc-utils/namei.c2
-rw-r--r--misc-utils/rename.c2
-rw-r--r--misc-utils/uuidd.c2
-rw-r--r--misc-utils/uuidgen.c2
-rw-r--r--misc-utils/whereis.c2
-rw-r--r--misc-utils/wipefs.c2
-rw-r--r--mount/mount.c2
-rw-r--r--mount/mount_mntent.c6
-rw-r--r--mount/umount.c2
-rw-r--r--partx/partx.c2
-rw-r--r--schedutils/chrt.c3
-rw-r--r--schedutils/ionice.c2
-rw-r--r--schedutils/taskset.c2
-rw-r--r--sys-utils/arch.c2
-rw-r--r--sys-utils/chcpu.c2
-rw-r--r--sys-utils/ctrlaltdel.c2
-rw-r--r--sys-utils/cytune.c2
-rw-r--r--sys-utils/dmesg.c2
-rw-r--r--sys-utils/fallocate.c3
-rw-r--r--sys-utils/flock.c2
-rw-r--r--sys-utils/fsfreeze.c2
-rw-r--r--sys-utils/fstrim.c2
-rw-r--r--sys-utils/ipcmk.c2
-rw-r--r--sys-utils/ipcrm.c2
-rw-r--r--sys-utils/ipcs.c2
-rw-r--r--sys-utils/ldattach.c2
-rw-r--r--sys-utils/losetup.c2
-rw-r--r--sys-utils/lscpu.c2
-rw-r--r--sys-utils/mount.c2
-rw-r--r--sys-utils/mountpoint.c2
-rw-r--r--sys-utils/pivot_root.c2
-rw-r--r--sys-utils/prlimit.c2
-rw-r--r--sys-utils/readprofile.c2
-rw-r--r--sys-utils/renice.c2
-rw-r--r--sys-utils/rtcwake.c5
-rw-r--r--sys-utils/setarch.c2
-rw-r--r--sys-utils/setsid.c2
-rw-r--r--sys-utils/swapon.c2
-rw-r--r--sys-utils/switch_root.c2
-rw-r--r--sys-utils/tunelp.c2
-rw-r--r--sys-utils/umount.c2
-rw-r--r--sys-utils/unshare.c2
-rw-r--r--term-utils/mesg.c3
-rw-r--r--term-utils/script.c16
-rw-r--r--term-utils/scriptreplay.c2
-rw-r--r--term-utils/setterm.c5
-rw-r--r--term-utils/ttymsg.c3
-rw-r--r--term-utils/wall.c5
-rw-r--r--term-utils/write.c3
-rw-r--r--text-utils/col.c4
-rw-r--r--text-utils/colcrt.c4
-rw-r--r--text-utils/colrm.c5
-rw-r--r--text-utils/column.c5
-rw-r--r--text-utils/hexdump.c2
-rw-r--r--text-utils/more.c18
-rw-r--r--text-utils/pg.c2
-rw-r--r--text-utils/rev.c2
-rw-r--r--text-utils/tailf.c2
-rw-r--r--text-utils/ul.c5
102 files changed, 278 insertions, 48 deletions
diff --git a/disk-utils/blockdev.c b/disk-utils/blockdev.c
index 2275f0c58..ba06ad035 100644
--- a/disk-utils/blockdev.c
+++ b/disk-utils/blockdev.c
@@ -15,6 +15,7 @@
#include "nls.h"
#include "blkdev.h"
#include "pathnames.h"
+#include "closestream.h"
struct bdc {
long ioc; /* ioctl code */
@@ -221,6 +222,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 2)
usage(stderr);
diff --git a/disk-utils/elvtune.c b/disk-utils/elvtune.c
index 7c074ffa6..9f7fbf327 100644
--- a/disk-utils/elvtune.c
+++ b/disk-utils/elvtune.c
@@ -38,6 +38,7 @@
#include <sys/utsname.h>
#include "nls.h"
#include "blkdev.h"
+#include "closestream.h"
#include "linux_version.h"
/* this has to match with the kernel structure */
@@ -79,6 +80,7 @@ main(int argc, char * argv[]) {
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
for (;;) {
int opt;
diff --git a/disk-utils/fdformat.c b/disk-utils/fdformat.c
index 29782ec9a..ec2f6170e 100644
--- a/disk-utils/fdformat.c
+++ b/disk-utils/fdformat.c
@@ -12,6 +12,7 @@
#include <unistd.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "xalloc.h"
@@ -119,6 +120,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "nVh", longopts, NULL)) != -1)
switch (ch) {
diff --git a/disk-utils/fsck.c b/disk-utils/fsck.c
index 44200d08f..46646a99b 100644
--- a/disk-utils/fsck.c
+++ b/disk-utils/fsck.c
@@ -52,6 +52,7 @@
#include "pathnames.h"
#include "exitcodes.h"
#include "c.h"
+#include "closestream.h"
#define XALLOC_EXIT_CODE FSCK_EX_ERROR
#include "xalloc.h"
@@ -1478,6 +1479,7 @@ int main(int argc, char *argv[])
setlocale(LC_CTYPE, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
mnt_init_debug(0); /* init libmount debug mask */
mntcache = mnt_new_cache(); /* no fatal error if failed */
diff --git a/disk-utils/fsck.cramfs.c b/disk-utils/fsck.cramfs.c
index 036f2c7c0..9d7256c21 100644
--- a/disk-utils/fsck.cramfs.c
+++ b/disk-utils/fsck.cramfs.c
@@ -59,6 +59,7 @@
#include "blkdev.h"
#include "c.h"
#include "exitcodes.h"
+#include "closestream.h"
#define XALLOC_EXIT_CODE FSCK_EX_ERROR
#include "xalloc.h"
@@ -644,6 +645,7 @@ int main(int argc, char **argv)
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
page_size = getpagesize();
diff --git a/disk-utils/fsck.minix.c b/disk-utils/fsck.minix.c
index 2a39e5be4..8b3ce62fa 100644
--- a/disk-utils/fsck.minix.c
+++ b/disk-utils/fsck.minix.c
@@ -110,6 +110,7 @@
#include "bitops.h"
#include "ismounted.h"
#include "writeall.h"
+#include "closestream.h"
#define ROOT_INO 1
#define YESNO_LENGTH 64
@@ -1242,6 +1243,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc == 2 &&
(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
diff --git a/disk-utils/isosize.c b/disk-utils/isosize.c
index 7bfa81d4a..7a8d71301 100644
--- a/disk-utils/isosize.c
+++ b/disk-utils/isosize.c
@@ -28,6 +28,7 @@
#include "nls.h"
#include "c.h"
#include "strutils.h"
+#include "closestream.h"
#define ISODCL(from, to) (to - from + 1)
@@ -181,6 +182,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((opt = getopt_long(argc, argv, "d:xVh", longopts, NULL)) != -1)
switch (opt) {
diff --git a/disk-utils/mkfs.bfs.c b/disk-utils/mkfs.bfs.c
index 6e5f012e8..b37a94729 100644
--- a/disk-utils/mkfs.bfs.c
+++ b/disk-utils/mkfs.bfs.c
@@ -16,6 +16,7 @@
#include "blkdev.h"
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "strutils.h"
#include "xalloc.h"
@@ -118,6 +119,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 2)
usage(stderr);
diff --git a/disk-utils/mkfs.c b/disk-utils/mkfs.c
index ce845a571..dc2d2fdd9 100644
--- a/disk-utils/mkfs.c
+++ b/disk-utils/mkfs.c
@@ -21,6 +21,7 @@
#include <unistd.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "xalloc.h"
@@ -80,6 +81,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc == 2 && !strcmp(argv[1], "-V"))
print_version();
diff --git a/disk-utils/mkfs.cramfs.c b/disk-utils/mkfs.cramfs.c
index 9777eaf97..8e38a8c89 100644
--- a/disk-utils/mkfs.cramfs.c
+++ b/disk-utils/mkfs.cramfs.c
@@ -40,6 +40,7 @@
#include "c.h"
#include "cramfs.h"
+#include "closestream.h"
#include "md5.h"
#include "nls.h"
#include "exitcodes.h"
@@ -718,6 +719,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* command line options */
while ((c = getopt(argc, argv, "hb:Ee:i:n:N:psVvz")) != EOF) {
diff --git a/disk-utils/mkfs.minix.c b/disk-utils/mkfs.minix.c
index a1ec2960c..051cd5f68 100644
--- a/disk-utils/mkfs.minix.c
+++ b/disk-utils/mkfs.minix.c
@@ -85,6 +85,7 @@
#include "exitcodes.h"
#include "strutils.h"
#include "writeall.h"
+#include "closestream.h"
#define MINIX_ROOT_INO 1
#define MINIX_BAD_INO 2
@@ -660,6 +661,7 @@ int main(int argc, char ** argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc == 2 &&
(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c
index b963da265..51a0c890b 100644
--- a/disk-utils/mkswap.c
+++ b/disk-utils/mkswap.c
@@ -57,6 +57,7 @@
#include "writeall.h"
#include "xalloc.h"
#include "c.h"
+#include "closestream.h"
#ifdef HAVE_LIBUUID
# include <uuid.h>
@@ -496,6 +497,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) {
switch (c) {
diff --git a/disk-utils/raw.c b/disk-utils/raw.c
index 0a43c907c..1e7a6fbd7 100644
--- a/disk-utils/raw.c
+++ b/disk-utils/raw.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "pathnames.h"
@@ -106,6 +107,7 @@ int main(int argc, char *argv[])
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "qaVh", longopts, NULL)) != -1)
switch (c) {
diff --git a/disk-utils/swaplabel.c b/disk-utils/swaplabel.c
index cc9a1e6aa..716db9e50 100644
--- a/disk-utils/swaplabel.c
+++ b/disk-utils/swaplabel.c
@@ -26,6 +26,7 @@
#endif
#include "c.h"
+#include "closestream.h"
#include "writeall.h"
#include "swapheader.h"
#include "strutils.h"
@@ -183,6 +184,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "hVL:U:", longopts, NULL)) != -1) {
switch (c) {
diff --git a/fdisk/cfdisk.c b/fdisk/cfdisk.c
index a128ed757..a6cf01e5c 100644
--- a/fdisk/cfdisk.c
+++ b/fdisk/cfdisk.c
@@ -97,6 +97,7 @@
#include <wctype.h>
#endif
+#include "closestream.h"
#include "nls.h"
#include "rpmatch.h"
#include "blkdev.h"
@@ -2767,6 +2768,7 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt(argc, argv, "ac:gh:s:vzP:")) != -1)
switch (c) {
diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index 7c1486184..cf64fa776 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -34,6 +34,7 @@
#include "canonicalize.h"
#include "strutils.h"
#include "randutils.h"
+#include "closestream.h"
#include "fdisksunlabel.h"
#include "fdisksgilabel.h"
@@ -2904,6 +2905,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt(argc, argv, "b:c::C:hH:lsS:u::vV")) != -1) {
switch (c) {
diff --git a/fdisk/gpt.c b/fdisk/gpt.c
index aeacd6624..bb6911a21 100644
--- a/fdisk/gpt.c
+++ b/fdisk/gpt.c
@@ -39,6 +39,7 @@
#include "gpt.h"
#include "blkdev.h"
#include "bitops.h"
+#include "closestream.h"
#define GPT_HEADER_SIGNATURE 0x5452415020494645LL
#define GPT_PRIMARY_PARTITION_TABLE_LBA 1
@@ -202,6 +203,7 @@ gpt_probe_signature_devname(char *devname)
int
main(int argc, char **argv)
{
+ atexit(close_stdout);
if (argc!=2)
{
fprintf(stderr, "usage: %s <dev>\n", argv[0]);
diff --git a/fdisk/partitiontype.c b/fdisk/partitiontype.c
index 0ee44a0ba..02a22a595 100644
--- a/fdisk/partitiontype.c
+++ b/fdisk/partitiontype.c
@@ -10,6 +10,8 @@
#include <fcntl.h>
#include <stdlib.h>
+#include "closestream.h"
+
struct aix_label {
unsigned int magic;
/* more ... */
@@ -53,6 +55,7 @@ main(int argc, char **argv) {
struct sgi_label *psgi;
struct sun_label *psun;
+ atexit(close_stdout);
if (argc != 2) {
fprintf(stderr, "call: %s device\n", argv[0]);
exit(1);
diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c
index 350bf1d53..95e58289e 100644
--- a/fdisk/sfdisk.c
+++ b/fdisk/sfdisk.c
@@ -54,6 +54,7 @@
#include "pathnames.h"
#include "canonicalize.h"
#include "rpmatch.h"
+#include "closestream.h"
/*
* Table of contents:
@@ -2574,6 +2575,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 1)
errx(EXIT_FAILURE, _("no command?"));
diff --git a/getopt/getopt.c b/getopt/getopt.c
index 51ff19e95..4e5043aaa 100644
--- a/getopt/getopt.c
+++ b/getopt/getopt.c
@@ -58,6 +58,7 @@
#include <ctype.h>
#include <getopt.h>
+#include "closestream.h"
#include "nls.h"
#include "xalloc.h"
@@ -363,6 +364,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
init_longopt();
getopt_long_fp = getopt_long;
diff --git a/hwclock/hwclock.c b/hwclock/hwclock.c
index 4da2dca79..3cedd1de3 100644
--- a/hwclock/hwclock.c
+++ b/hwclock/hwclock.c
@@ -72,6 +72,7 @@
#include "c.h"
#include "clock.h"
+#include "closestream.h"
#include "nls.h"
#include "pathnames.h"
#include "strutils.h"
@@ -164,7 +165,8 @@ static void write_date_to_file(struct tm *tm)
if ((fp = fopen(_PATH_LASTDATE, "w"))) {
fprintf(fp, "%02d.%02d.%04d\n", tm->tm_mday, tm->tm_mon + 1,
tm->tm_year + 1900);
- fclose(fp);
+ if (close_stream(fp) != 0)
+ warn(_("cannot write %s"), _PATH_LASTDATE);
} else
warn(_("cannot write %s"), _PATH_LASTDATE);
}
@@ -1026,7 +1028,7 @@ static void save_adjtime(const struct adjtime adjtime, const bool testing)
adj_file_name);
err = 1;
}
- if (fclose(adjfile) < 0) {
+ if (close_stream(adjfile) != 0) {
warn(_
("Could not update file with the clock adjustment "
"parameters (%s) in it"),
@@ -1521,6 +1523,7 @@ int main(int argc, char **argv)
#endif
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* Set option defaults */
show = set = systohc = hctosys = systz = adjust = noadjfile = predict =
diff --git a/include/Makefile.am b/include/Makefile.am
index b939f89fe..f6934ecfe 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -7,6 +7,7 @@ dist_noinst_HEADERS = \
c.h \
canonicalize.h \
carefulputc.h \
+ closestream.h \
cpuset.h \
crc32.h \
env.h \
diff --git a/include/closestream.h b/include/closestream.h
new file mode 100644
index 000000000..fb507eab8
--- /dev/null
+++ b/include/closestream.h
@@ -0,0 +1,41 @@
+#ifndef UTIL_LINUX_CLOSESTREAM_H
+#define UTIL_LINUX_CLOSESTREAM_H
+
+#include <stdio.h>
+#include <stdio_ext.h>
+#include <unistd.h>
+
+#include "c.h"
+#include "nls.h"
+
+static inline int
+close_stream(FILE * stream)
+{
+ const int some_pending = (__fpending(stream) != 0);
+ const int prev_fail = (ferror(stream) != 0);
+ const int fclose_fail = (fclose(stream) != 0);
+ if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) {
+ if (!fclose_fail)
+ errno = 0;
+ return EOF;
+ }
+ return 0;
+}
+
+/* Meant to be used atexit(close_stdout); */
+static inline void
+close_stdout(void)
+{
+ if (close_stream(stdout) != 0 && !(errno == EPIPE)) {
+ if (errno)
+ warn(_("write error"));
+ else
+ warnx(_("write error"));
+ _exit(EXIT_FAILURE);
+ }
+
+ if (close_stream(stderr) != 0)
+ _exit(EXIT_FAILURE);
+}
+
+#endif /* UTIL_LINUX_CLOSESTREAM_H */
diff --git a/login-utils/chfn.c b/login-utils/chfn.c
index 2d9a092cb..02014c7ce 100644
--- a/login-utils/chfn.c
+++ b/login-utils/chfn.c
@@ -33,6 +33,7 @@
#include "c.h"
#include "env.h"
+#include "closestream.h"
#include "islocal.h"
#include "nls.h"
#include "pamfail.h"
@@ -96,6 +97,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, ""); /* both for messages and for iscntrl() below */
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/*
* "oldf" contains the users original finger information.
diff --git a/login-utils/chsh.c b/login-utils/chsh.c
index 4f0615c08..7d944e19f 100644
--- a/login-utils/chsh.c
+++ b/login-utils/chsh.c
@@ -34,6 +34,7 @@
#include "c.h"
#include "env.h"
+#include "closestream.h"
#include "islocal.h"
#include "nls.h"
#include "pamfail.h"
@@ -82,6 +83,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
uid = getuid();
memset(&info, 0, sizeof(info));
diff --git a/login-utils/islocal.c b/login-utils/islocal.c
index 91dda0d29..e23ca44e0 100644
--- a/login-utils/islocal.c
+++ b/login-utils/islocal.c
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include "closestream.h"
#include "islocal.h"
#include "nls.h"
#include "pathnames.h"
@@ -94,6 +95,7 @@ int is_local(const char *user)
#ifdef TEST_PROGRAM
int main(int argc, char *argv[])
{
+ atexit(close_stdout);
if (argc <= 2) {
fprintf(stderr, "usage: %s <passwdfile> <username> [...]\n",
argv[0]);
diff --git a/login-utils/last.c b/login-utils/last.c
index 417314ee5..34558bb9d 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -56,6 +56,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
+#include "closestream.h"
#include "pathnames.h"
#include "nls.h"
#include "xalloc.h"
@@ -121,6 +122,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt(argc, argv, "0123456789yli:f:h:t:")) != -1)
switch((char)ch) {
diff --git a/login-utils/logindefs.c b/login-utils/logindefs.c
index 7b801cf70..ff7266ea1 100644
--- a/login-utils/logindefs.c
+++ b/login-utils/logindefs.c
@@ -29,6 +29,7 @@
#include <sys/syslog.h>
#include "c.h"
+#include "closestream.h"
#include "logindefs.h"
#include "nls.h"
#include "pathnames.h"
@@ -252,6 +253,7 @@ int logindefs_setenv(const char *name, const char *conf, const char *dflt)
int main(int argc, char *argv[])
{
char *name, *type;
+ atexit(close_stdout);
if (argc <= 1)
errx(EXIT_FAILURE, "usage: %s <filename> "
diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c
index 707c5893a..4f6de12a1 100644
--- a/login-utils/newgrp.c
+++ b/login-utils/newgrp.c
@@ -29,6 +29,7 @@
#endif
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "pathnames.h"
@@ -132,6 +133,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
switch (ch) {
diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c
index 7459fe298..ba15bc131 100644
--- a/login-utils/setpwnam.c
+++ b/login-utils/setpwnam.c
@@ -59,7 +59,7 @@
#include <unistd.h>
#include "c.h"
-#include "fileutils.h"
+#include "closestream.h"
#include "setpwnam.h"
static void pw_init(void);
@@ -143,9 +143,7 @@ int setpwnam(struct passwd *pwd)
/* xfmkstemp is too restrictive by default for passwd file */
if (fchmod(fileno(fp), 0644) < 0)
goto fail;
- rc = fclose(fp);
- fp = NULL;
- if (rc < 0)
+ if (close_stream(fp) != 0)
goto fail;
fclose(pwf); /* I don't think I want to know if this failed */
diff --git a/login-utils/sulogin.c b/login-utils/sulogin.c
index 616442ee4..ee725b588 100644
--- a/login-utils/sulogin.c
+++ b/login-utils/sulogin.c
@@ -47,6 +47,7 @@
#endif
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "pathnames.h"
#include "strutils.h"
@@ -476,6 +477,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/*
* See if we have a timeout flag.
diff --git a/login-utils/vipw.c b/login-utils/vipw.c
index 29e485cc4..2d16999d2 100644
--- a/login-utils/vipw.c
+++ b/login-utils/vipw.c
@@ -72,7 +72,7 @@
#include <unistd.h>
#include "c.h"
-#include "fileutils.h"
+#include "closestream.h"
#include "nls.h"
#include "setpwnam.h"
#include "strutils.h"
@@ -283,7 +283,8 @@ static void edit_file(int is_shadow)
ch_ret = fchmod(fileno(tmp_fd), 0400);
if (ch_ret < 0)
err(EXIT_FAILURE, "%s: %s", _("cannot chmod file"), orig_file);
- fclose(tmp_fd);
+ if (close_stream(tmp_fd) != 0)
+ err(EXIT_FAILURE, _("write error"));
pw_write();
close(passwd_file);
ulckpwdf();
@@ -294,6 +295,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (!strcmp(program_invocation_short_name, "vigr")) {
program = VIGR;
diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c
index 978661903..3793c524f 100644
--- a/misc-utils/blkid.c
+++ b/misc-utils/blkid.c
@@ -44,6 +44,7 @@ extern int optind;
#define STRTOXX_EXIT_CODE 4 /* strtoxx_or_err() */
#include "strutils.h"
+#include "closestream.h"
const char *progname = "blkid";
@@ -691,6 +692,7 @@ int main(int argc, char **argv)
uintmax_t offset = 0, size = 0;
show[0] = NULL;
+ atexit(close_stdout);
while ((c = getopt (argc, argv, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF)
switch (c) {
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index c9efdf074..918cbab09 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -67,6 +67,7 @@
#include <errno.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "mbsalign.h"
#include "strutils.h"
@@ -278,6 +279,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
if ((term = getenv("TERM"))) {
diff --git a/misc-utils/ddate.c b/misc-utils/ddate.c
index 323cb9e2d..c0b4ce41d 100644
--- a/misc-utils/ddate.c
+++ b/misc-utils/ddate.c
@@ -63,6 +63,7 @@
#include <stdio.h>
#include "nls.h"
+#include "closestream.h"
#include "c.h"
#ifndef __GNUC__
@@ -169,6 +170,7 @@ main (int argc, char *argv[]) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
srandom(time(NULL));
/* do args here */
diff --git a/misc-utils/findfs.c b/misc-utils/findfs.c
index 761c6e91f..bc4a84313 100644
--- a/misc-utils/findfs.c
+++ b/misc-utils/findfs.c
@@ -12,6 +12,7 @@
#include <blkid.h>
#include "nls.h"
+#include "closestream.h"
#include "c.h"
static void __attribute__((__noreturn__)) usage(int rc)
@@ -35,6 +36,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc != 2)
/* we return '2' for backward compatibility
diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
index fe91abb5f..172f38915 100644
--- a/misc-utils/findmnt.c
+++ b/misc-utils/findmnt.c
@@ -37,6 +37,7 @@
#include "pathnames.h"
#include "nls.h"
+#include "closestream.h"
#include "c.h"
#include "tt.h"
#include "strutils.h"
@@ -991,6 +992,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* default output format */
tt_flags |= TT_FL_TREE;
diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 15bb47051..dc024924c 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -53,6 +53,7 @@
#include "c.h"
#include "kill.h"
#include "nls.h"
+#include "closestream.h"
#include "strutils.h"
struct signv {
@@ -172,6 +173,7 @@ int main (int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
numsig = SIGTERM;
do_pid = (! strcmp (progname, "pid")); /* Yecch */
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c89fca70a..e3b67d2a2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -52,6 +52,7 @@
#include <getopt.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "strutils.h"
@@ -188,6 +189,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
tag = NULL;
pri = LOG_NOTICE;
diff --git a/misc-utils/look.c b/misc-utils/look.c
index 3d9623bfb..de231c96e 100644
--- a/misc-utils/look.c
+++ b/misc-utils/look.c
@@ -60,6 +60,7 @@
#include "nls.h"
#include "xalloc.h"
#include "pathnames.h"
+#include "closestream.h"
#define EQUAL 0
#define GREATER 1
@@ -99,6 +100,7 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
setlocale(LC_ALL, "");
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 76356efbb..22c8e82d2 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -58,6 +58,7 @@
#include "strutils.h"
#include "at.h"
#include "sysfs.h"
+#include "closestream.h"
/* column IDs */
enum {
@@ -1186,6 +1187,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
lsblk = &_ls;
memset(lsblk, 0, sizeof(*lsblk));
diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c
index 0631f684a..dd7183f9d 100644
--- a/misc-utils/lslocks.c
+++ b/misc-utils/lslocks.c
@@ -40,6 +40,7 @@
#include "at.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
/* column IDs */
enum {
@@ -515,6 +516,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv,
"p:o:nruhV", long_opts, NULL)) != -1) {
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 5bef4c541..b10f5c6fa 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -21,6 +21,8 @@
#include "c.h"
#include "md5.h"
#include "nls.h"
+#include "closestream.h"
+
#include <fcntl.h>
#include <getopt.h>
#include <stddef.h>
@@ -102,6 +104,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c =
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
@@ -183,13 +186,5 @@ int main(int argc, char **argv)
printf("%02x", digest[i]);
putchar('\n');
- /*
- * The following is important for cases like disk full,
- * so shell scripts can bomb out properly rather than
- * think they succeeded.
- */
- if (fflush(stdout) < 0 || fclose(stdout) < 0)
- return EXIT_FAILURE;
-
return EXIT_SUCCESS;
}
diff --git a/misc-utils/namei.c b/misc-utils/namei.c
index e53b4c9a4..9b8ede901 100644
--- a/misc-utils/namei.c
+++ b/misc-utils/namei.c
@@ -38,6 +38,7 @@
#include "nls.h"
#include "widechar.h"
#include "strutils.h"
+#include "closestream.h"
#ifndef MAXSYMLINKS
#define MAXSYMLINKS 256
@@ -448,6 +449,7 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
switch(c) {
diff --git a/misc-utils/rename.c b/misc-utils/rename.c
index f7c88cdaa..b17e03bb1 100644
--- a/misc-utils/rename.c
+++ b/misc-utils/rename.c
@@ -22,6 +22,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
#include "nls.h"
#include "xalloc.h"
#include "c.h"
+#include "closestream.h"
static int do_rename(char *from, char *to, char *s, int verbose)
{
@@ -89,6 +90,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "vVh", longopts, NULL)) != -1)
switch (c) {
diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
index 492ae4d03..7c19615b5 100644
--- a/misc-utils/uuidd.c
+++ b/misc-utils/uuidd.c
@@ -35,6 +35,7 @@ extern int optind;
#include "uuidd.h"
#include "writeall.h"
+#include "closestream.h"
#include "nls.h"
#ifdef __GNUC__
@@ -455,6 +456,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c =
getopt_long(argc, argv, "p:s:T:krtn:dqVh", longopts,
diff --git a/misc-utils/uuidgen.c b/misc-utils/uuidgen.c
index 2a98bc8be..219be8b62 100644
--- a/misc-utils/uuidgen.c
+++ b/misc-utils/uuidgen.c
@@ -24,6 +24,7 @@ extern int optind;
#include "uuid.h"
#include "nls.h"
#include "c.h"
+#include "closestream.h"
#define DO_TYPE_TIME 1
#define DO_TYPE_RANDOM 2
@@ -62,6 +63,7 @@ main (int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "rtVh", longopts, NULL)) != -1)
switch (c) {
diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c
index 4a123f1a7..6807b8a68 100644
--- a/misc-utils/whereis.c
+++ b/misc-utils/whereis.c
@@ -53,6 +53,7 @@
#include "xalloc.h"
#include "nls.h"
#include "c.h"
+#include "closestream.h"
static char *bindirs[] = {
"/bin",
@@ -417,6 +418,7 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
argc--, argv++;
if (argc == 0)
diff --git a/misc-utils/wipefs.c b/misc-utils/wipefs.c
index bd20997a1..a9d74f043 100644
--- a/misc-utils/wipefs.c
+++ b/misc-utils/wipefs.c
@@ -38,6 +38,7 @@
#include "writeall.h"
#include "match.h"
#include "c.h"
+#include "closestream.h"
struct wipe_desc {
loff_t offset; /* magic string offset */
@@ -391,6 +392,7 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "ahno:pqt:V", longopts, NULL)) != -1) {
switch(c) {
diff --git a/mount/mount.c b/mount/mount.c
index 396f357d9..19fe42e7f 100644
--- a/mount/mount.c
+++ b/mount/mount.c
@@ -41,6 +41,7 @@
#include "nls.h"
#include "blkdev.h"
#include "strutils.h"
+#include "closestream.h"
#define DO_PS_FIDDLING
@@ -2519,6 +2520,7 @@ main(int argc, char *argv[]) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
progname = argv[0];
if ((p = strrchr(progname, '/')) != NULL)
diff --git a/mount/mount_mntent.c b/mount/mount_mntent.c
index f42c0adff..88dab1025 100644
--- a/mount/mount_mntent.c
+++ b/mount/mount_mntent.c
@@ -14,6 +14,7 @@
#include "sundries.h" /* for xmalloc */
#include "nls.h"
#include "mangle.h"
+#include "closestream.h"
static int
is_space_or_tab (char c) {
@@ -50,10 +51,11 @@ my_setmntent (const char *file, char *mode) {
}
void
-my_endmntent (mntFILE *mfp) {
+my_endmntent(mntFILE * mfp) {
if (mfp) {
if (mfp->mntent_fp)
- fclose(mfp->mntent_fp);
+ if (close_stream(mfp->mntent_fp))
+ fprintf(stderr, _("write error"));
free(mfp->mntent_file);
free(mfp);
}
diff --git a/mount/umount.c b/mount/umount.c
index 71cc0ee58..d3375df83 100644
--- a/mount/umount.c
+++ b/mount/umount.c
@@ -21,6 +21,7 @@
#include "env.h"
#include "nls.h"
#include "strutils.h"
+#include "closestream.h"
#if defined(MNT_FORCE)
/* Interesting ... it seems libc knows about MNT_FORCE and presumably
@@ -778,6 +779,7 @@ main (int argc, char *argv[]) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
progname = argv[0];
if ((p = strrchr(progname, '/')) != NULL)
diff --git a/partx/partx.c b/partx/partx.c
index 0c46af6eb..f19b91928 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -33,6 +33,7 @@
#include "sysfs.h"
#include "loopdev.h"
#include "at.h"
+#include "closestream.h"
/* this is the default upper limit, could be modified by --nr */
#define SLICES_MAX 256
@@ -667,6 +668,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv,
"abdglrsvn:t:o:PhV", long_opts, NULL)) != -1) {
diff --git a/schedutils/chrt.c b/schedutils/chrt.c
index 944cd7748..cbf2417e8 100644
--- a/schedutils/chrt.c
+++ b/schedutils/chrt.c
@@ -31,7 +31,7 @@
#include "c.h"
#include "nls.h"
-
+#include "closestream.h"
#include "strutils.h"
#include "procutils.h"
@@ -218,6 +218,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while((i = getopt_long(argc, argv, "+abfiphmoRrvV", longopts, NULL)) != -1)
{
diff --git a/schedutils/ionice.c b/schedutils/ionice.c
index 18529753f..5fdeccf8d 100644
--- a/schedutils/ionice.c
+++ b/schedutils/ionice.c
@@ -17,6 +17,7 @@
#include "nls.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
static int tolerant;
@@ -139,6 +140,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "+n:c:p:tVh", longopts, NULL)) != EOF)
switch (c) {
diff --git a/schedutils/taskset.c b/schedutils/taskset.c
index b7aee5064..908ac8e2d 100644
--- a/schedutils/taskset.c
+++ b/schedutils/taskset.c
@@ -34,6 +34,7 @@
#include "xalloc.h"
#include "procutils.h"
#include "c.h"
+#include "closestream.h"
struct taskset {
pid_t pid; /* task PID */
@@ -146,6 +147,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
memset(&ts, 0, sizeof(ts));
diff --git a/sys-utils/arch.c b/sys-utils/arch.c
index 470b12b9a..a7305895a 100644
--- a/sys-utils/arch.c
+++ b/sys-utils/arch.c
@@ -34,6 +34,7 @@
#include <sys/utsname.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
static void __attribute__ ((__noreturn__)) usage(FILE * out)
@@ -61,6 +62,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
switch (ch) {
diff --git a/sys-utils/chcpu.c b/sys-utils/chcpu.c
index 58e40f90d..18655927b 100644
--- a/sys-utils/chcpu.c
+++ b/sys-utils/chcpu.c
@@ -40,6 +40,7 @@
#include "strutils.h"
#include "bitops.h"
#include "path.h"
+#include "closestream.h"
#define _PATH_SYS_CPU "/sys/devices/system/cpu"
#define _PATH_SYS_CPU_ONLINE _PATH_SYS_CPU "/online"
@@ -246,6 +247,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
maxcpus = get_max_number_of_cpus();
if (maxcpus < 1)
diff --git a/sys-utils/ctrlaltdel.c b/sys-utils/ctrlaltdel.c
index ef963226e..2ad56a1bb 100644
--- a/sys-utils/ctrlaltdel.c
+++ b/sys-utils/ctrlaltdel.c
@@ -13,6 +13,7 @@
#include "linux_reboot.h"
#include "nls.h"
#include "c.h"
+#include "closestream.h"
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
@@ -37,6 +38,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
switch (ch) {
diff --git a/sys-utils/cytune.c b/sys-utils/cytune.c
index 96d2d6404..ecacff2a1 100644
--- a/sys-utils/cytune.c
+++ b/sys-utils/cytune.c
@@ -52,6 +52,7 @@
#include "c.h"
#include "cyclades.h"
+#include "closestream.h"
#include "strutils.h"
#if 0
@@ -311,6 +312,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((i =
getopt_long(argc, argv, "qs:S:t:T:gGi:Vh", longopts,
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index 9cbe51658..de4b3abc6 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -29,6 +29,7 @@
#include "widechar.h"
#include "writeall.h"
#include "bitops.h"
+#include "closestream.h"
/* Close the log. Currently a NOP. */
#define SYSLOG_ACTION_CLOSE 0
@@ -688,6 +689,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "CcDdEF:f:hkl:n:rs:TtuVx",
longopts, NULL)) != -1) {
diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c
index 5fc13fe62..adfaa70f7 100644
--- a/sys-utils/fallocate.c
+++ b/sys-utils/fallocate.c
@@ -51,7 +51,7 @@
#include "nls.h"
#include "strutils.h"
#include "c.h"
-
+#include "closestream.h"
static void __attribute__((__noreturn__)) usage(FILE *out)
{
@@ -104,6 +104,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "hVnpl:o:", longopts, NULL)) != -1) {
switch(c) {
diff --git a/sys-utils/flock.c b/sys-utils/flock.c
index 50cea1392..1db28a294 100644
--- a/sys-utils/flock.c
+++ b/sys-utils/flock.c
@@ -42,6 +42,7 @@
#include "c.h"
#include "nls.h"
#include "strutils.h"
+#include "closestream.h"
static void __attribute__((__noreturn__)) usage(int ex)
{
@@ -161,6 +162,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 2)
usage(EX_USAGE);
diff --git a/sys-utils/fsfreeze.c b/sys-utils/fsfreeze.c
index 3f03a5241..5d1702158 100644
--- a/sys-utils/fsfreeze.c
+++ b/sys-utils/fsfreeze.c
@@ -23,6 +23,7 @@
#include "blkdev.h"
#include "nls.h"
+#include "closestream.h"
#include "c.h"
static int freeze_f(int fd)
@@ -68,6 +69,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "hfu", longopts, NULL)) != -1) {
switch(c) {
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index 4673e94ab..bb88bb804 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -40,6 +40,7 @@
#include "nls.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
#ifndef FITRIM
struct fstrim_range {
@@ -87,6 +88,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
memset(&range, 0, sizeof(range));
range.len = ULLONG_MAX;
diff --git a/sys-utils/ipcmk.c b/sys-utils/ipcmk.c
index 3b3ebb9ba..6919d0dea 100644
--- a/sys-utils/ipcmk.c
+++ b/sys-utils/ipcmk.c
@@ -33,6 +33,7 @@
#include "c.h"
#include "nls.h"
#include "strutils.h"
+#include "closestream.h"
key_t create_key(void)
{
@@ -98,6 +99,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while((opt = getopt_long(argc, argv, "hM:QS:p:Vh", longopts, NULL)) != -1) {
switch(opt) {
diff --git a/sys-utils/ipcrm.c b/sys-utils/ipcrm.c
index c091de349..b452b51ec 100644
--- a/sys-utils/ipcrm.c
+++ b/sys-utils/ipcrm.c
@@ -22,6 +22,7 @@
#include "c.h"
#include "nls.h"
#include "strutils.h"
+#include "closestream.h"
#ifndef HAVE_UNION_SEMUN
/* according to X/OPEN we have to define it ourselves */
@@ -316,6 +317,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* check to see if the command is being invoked in the old way if so
* then remove argument list */
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
index 48a4dac4f..fc4164823 100644
--- a/sys-utils/ipcs.c
+++ b/sys-utils/ipcs.c
@@ -33,6 +33,7 @@
#include "c.h"
#include "nls.h"
+#include "closestream.h"
/*
* SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
@@ -162,6 +163,7 @@ int main (int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((opt = getopt_long(argc, argv, options, longopts, NULL)) != -1) {
switch (opt) {
diff --git a/sys-utils/ldattach.c b/sys-utils/ldattach.c
index d331413fd..2917d4e83 100644
--- a/sys-utils/ldattach.c
+++ b/sys-utils/ldattach.c
@@ -27,6 +27,7 @@
#include "c.h"
#include "nls.h"
#include "strutils.h"
+#include "closestream.h"
#ifndef N_GIGASET_M101
# define N_GIGASET_M101 16
@@ -234,6 +235,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* parse options */
progname = program_invocation_short_name;
diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c
index b8f6762b1..7059d2f89 100644
--- a/sys-utils/losetup.c
+++ b/sys-utils/losetup.c
@@ -19,6 +19,7 @@
#include "strutils.h"
#include "loopdev.h"
#include "xgetpass.h"
+#include "closestream.h"
enum {
A_CREATE = 1, /* setup a new device */
@@ -217,6 +218,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
loopcxt_init(&lc, 0);
loopcxt_enable_debug(&lc, getenv("LOOPDEV_DEBUG") ? TRUE : FALSE);
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 6e17b2eb5..1c2d9b61a 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -41,6 +41,7 @@
#include "bitops.h"
#include "tt.h"
#include "path.h"
+#include "closestream.h"
#define CACHE_MAX 100
@@ -1255,6 +1256,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "abce::hp::s:xV", longopts, NULL)) != -1) {
diff --git a/sys-utils/mount.c b/sys-utils/mount.c
index 664c9865a..0f6140690 100644
--- a/sys-utils/mount.c
+++ b/sys-utils/mount.c
@@ -39,6 +39,7 @@
#include "xgetpass.h"
#include "exitcodes.h"
#include "xalloc.h"
+#include "closestream.h"
/*** TODO: DOCS:
*
@@ -701,6 +702,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
mnt_init_debug(0);
cxt = mnt_new_context();
diff --git a/sys-utils/mountpoint.c b/sys-utils/mountpoint.c
index 63a54469f..6ab813d72 100644
--- a/sys-utils/mountpoint.c
+++ b/sys-utils/mountpoint.c
@@ -37,6 +37,7 @@
#include "nls.h"
#include "xalloc.h"
#include "c.h"
+#include "closestream.h"
static int quiet;
@@ -147,6 +148,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
mnt_init_debug(0);
diff --git a/sys-utils/pivot_root.c b/sys-utils/pivot_root.c
index 044aab226..31ceabb78 100644
--- a/sys-utils/pivot_root.c
+++ b/sys-utils/pivot_root.c
@@ -23,6 +23,7 @@
#include "c.h"
#include "nls.h"
+#include "closestream.h"
#define pivot_root(new_root,put_old) syscall(SYS_pivot_root,new_root,put_old)
@@ -50,6 +51,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
switch (ch) {
diff --git a/sys-utils/prlimit.c b/sys-utils/prlimit.c
index c45b85efb..12271a079 100644
--- a/sys-utils/prlimit.c
+++ b/sys-utils/prlimit.c
@@ -33,6 +33,7 @@
#include "xalloc.h"
#include "strutils.h"
#include "list.h"
+#include "closestream.h"
#ifndef RLIMIT_RTTIME
# define RLIMIT_RTTIME 15
@@ -513,6 +514,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
INIT_LIST_HEAD(&lims);
diff --git a/sys-utils/readprofile.c b/sys-utils/readprofile.c
index 52e64e8e5..04d9c7375 100644
--- a/sys-utils/readprofile.c
+++ b/sys-utils/readprofile.c
@@ -53,6 +53,7 @@
#include "nls.h"
#include "xalloc.h"
+#include "closestream.h"
#define S_LEN 128
@@ -166,6 +167,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
proFile = defaultpro;
mapFile = defaultmap;
diff --git a/sys-utils/renice.c b/sys-utils/renice.c
index cd4cafab3..f5d51b66a 100644
--- a/sys-utils/renice.c
+++ b/sys-utils/renice.c
@@ -46,6 +46,7 @@
#include <errno.h>
#include "nls.h"
#include "c.h"
+#include "closestream.h"
static int donice(int,int,int);
@@ -86,6 +87,7 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
argc--;
argv++;
diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
index c18e5e2fc..4ae2cef29 100644
--- a/sys-utils/rtcwake.c
+++ b/sys-utils/rtcwake.c
@@ -41,6 +41,7 @@
#include "usleep.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
/* constants from legacy PC/AT hardware */
#define RTC_PF 0x40
@@ -285,7 +286,8 @@ static void suspend_system(const char *suspend)
}
/* this executes after wake from suspend */
- fclose(f);
+ if (close_stream(f))
+ errx(EXIT_FAILURE, _("write error"));
}
@@ -394,6 +396,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((t = getopt_long(argc, argv, "ahd:lm:ns:t:uVv",
long_options, NULL)) != EOF) {
diff --git a/sys-utils/setarch.c b/sys-utils/setarch.c
index a61fe391e..97269f45a 100644
--- a/sys-utils/setarch.c
+++ b/sys-utils/setarch.c
@@ -35,6 +35,7 @@
#include <sys/utsname.h>
#include "nls.h"
#include "c.h"
+#include "closestream.h"
#define set_pers(pers) ((long)syscall(SYS_personality, pers))
@@ -251,6 +252,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 1)
show_usage(_("Not enough arguments"));
diff --git a/sys-utils/setsid.c b/sys-utils/setsid.c
index 132f4a2ab..6d1eeda42 100644
--- a/sys-utils/setsid.c
+++ b/sys-utils/setsid.c
@@ -19,6 +19,7 @@
#include "c.h"
#include "nls.h"
+#include "closestream.h"
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
@@ -53,6 +54,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "+Vhc", longopts, NULL)) != -1)
switch (ch) {
diff --git a/sys-utils/swapon.c b/sys-utils/swapon.c
index 25915b32a..e1e8455a7 100644
--- a/sys-utils/swapon.c
+++ b/sys-utils/swapon.c
@@ -26,6 +26,7 @@
#include "canonicalize.h"
#include "xalloc.h"
#include "c.h"
+#include "closestream.h"
#define PATH_MKSWAP "/sbin/mkswap"
@@ -890,6 +891,7 @@ main(int argc, char *argv[]) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
progname = program_invocation_short_name;
if (!progname) {
diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 0da784295..4ff3bff8f 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -35,6 +35,7 @@
#include "c.h"
#include "nls.h"
+#include "closestream.h"
#ifndef MS_MOVE
#define MS_MOVE 8192
@@ -194,6 +195,7 @@ static void __attribute__((__noreturn__)) usage(FILE *output)
int main(int argc, char *argv[])
{
char *newroot, *init, **initargs;
+ atexit(close_stdout);
if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
usage(stdout);
diff --git a/sys-utils/tunelp.c b/sys-utils/tunelp.c
index 98b00f6ef..4d9038aec 100644
--- a/sys-utils/tunelp.c
+++ b/sys-utils/tunelp.c
@@ -63,6 +63,7 @@
#include "lp.h"
#include "nls.h"
#include "xalloc.h"
+#include "closestream.h"
#define EXIT_BAD_VALUE 3
#define EXIT_LP_IO_ERR 4
@@ -142,6 +143,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 2)
print_usage(stderr);
diff --git a/sys-utils/umount.c b/sys-utils/umount.c
index cce4b5375..97b2f443b 100644
--- a/sys-utils/umount.c
+++ b/sys-utils/umount.c
@@ -34,6 +34,7 @@
#include "env.h"
#include "optutils.h"
#include "exitcodes.h"
+#include "closestream.h"
static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
const char *filename, int line)
@@ -309,6 +310,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
mnt_init_debug(0);
cxt = mnt_new_context();
diff --git a/sys-utils/unshare.c b/sys-utils/unshare.c
index 728b86037..9de997bdc 100644
--- a/sys-utils/unshare.c
+++ b/sys-utils/unshare.c
@@ -27,6 +27,7 @@
#include "nls.h"
#include "c.h"
+#include "closestream.h"
#ifndef CLONE_NEWSNS
# define CLONE_NEWNS 0x00020000
@@ -91,6 +92,7 @@ int main(int argc, char *argv[])
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while((c = getopt_long(argc, argv, "hVmuin", longopts, NULL)) != -1) {
switch(c) {
diff --git a/term-utils/mesg.c b/term-utils/mesg.c
index 4fad6f57a..366c679ac 100644
--- a/term-utils/mesg.c
+++ b/term-utils/mesg.c
@@ -54,6 +54,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
+
+#include "closestream.h"
#include "nls.h"
#include "c.h"
@@ -88,6 +90,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
static const struct option longopts[] = {
{ "verbose", no_argument, 0, 'v' },
diff --git a/term-utils/script.c b/term-utils/script.c
index effedac4f..07be7e987 100644
--- a/term-utils/script.c
+++ b/term-utils/script.c
@@ -59,6 +59,7 @@
#include <locale.h>
#include <stddef.h>
+#include "closestream.h"
#include "nls.h"
#include "c.h"
@@ -180,6 +181,7 @@ main(int argc, char **argv) {
setlocale(LC_NUMERIC, "C"); /* see comment above */
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "ac:efqt::Vh", longopts, NULL)) != -1)
switch(ch) {
@@ -283,7 +285,8 @@ main(int argc, char **argv) {
}
doinput();
- fclose(timingfd);
+ if (close_stream(timingfd) != 0)
+ errx(EXIT_FAILURE, _("write error"));
return EXIT_SUCCESS;
}
@@ -292,7 +295,8 @@ doinput(void) {
ssize_t cc;
char ibuf[BUFSIZ];
- fclose(fscript);
+ if (close_stream(fscript) != 0)
+ errx(EXIT_FAILURE, _("write error"));
while (die == 0) {
if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
@@ -404,6 +408,8 @@ dooutput(FILE *timingfd) {
if (flgs)
fcntl(master, F_SETFL, flgs);
+ if (close_stream(timingfd) != 0)
+ errx(EXIT_FAILURE, _("write error"));
done();
}
@@ -423,7 +429,8 @@ doshell(void) {
getslave();
close(master);
- fclose(fscript);
+ if (close_stream(fscript) != 0)
+ errx(EXIT_FAILURE, _("write error"));
dup2(slave, STDIN_FILENO);
dup2(slave, STDOUT_FILENO);
dup2(slave, STDERR_FILENO);
@@ -484,7 +491,8 @@ done(void) {
my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
fprintf(fscript, _("\nScript done on %s"), buf);
}
- fclose(fscript);
+ if (close_stream(fscript) != 0)
+ errx(EXIT_FAILURE, _("write error"));
close(master);
master = -1;
diff --git a/term-utils/scriptreplay.c b/term-utils/scriptreplay.c
index 5aa8d42af..63dc6fce1 100644
--- a/term-utils/scriptreplay.c
+++ b/term-utils/scriptreplay.c
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <getopt.h>
+#include "closestream.h"
#include "nls.h"
#include "c.h"
@@ -153,6 +154,7 @@ main(int argc, char *argv[])
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((ch = getopt_long(argc, argv, "t:s:d:Vh", longopts, NULL)) != -1)
switch(ch) {
diff --git a/term-utils/setterm.c b/term-utils/setterm.c
index 64fe0eb6a..5a652931b 100644
--- a/term-utils/setterm.c
+++ b/term-utils/setterm.c
@@ -121,6 +121,7 @@
#include "c.h"
#include "xalloc.h"
#include "nls.h"
+#include "closestream.h"
#if __GNU_LIBRARY__ < 5
#ifndef __alpha__
@@ -1113,7 +1114,8 @@ perform_sequence(int vcterm) {
err(EXIT_DUMPFILE, _("can not open dump file %s for output"),
opt_sn_name);
screendump(opt_sn_num, F);
- fclose(F);
+ if (close_stream(F) != 0)
+ errx(EXIT_FAILURE, _("write error"));
}
/* -msg [on|off]. */
@@ -1225,6 +1227,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (argc < 2)
bad_arg = TRUE;
diff --git a/term-utils/ttymsg.c b/term-utils/ttymsg.c
index 5253f7a5c..aea6c2656 100644
--- a/term-utils/ttymsg.c
+++ b/term-utils/ttymsg.c
@@ -51,8 +51,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include "nls.h"
+#include "nls.h"
+#include "closestream.h"
#include "pathnames.h"
#include "ttymsg.h"
diff --git a/term-utils/wall.c b/term-utils/wall.c
index fb3db2119..f302d454f 100644
--- a/term-utils/wall.c
+++ b/term-utils/wall.c
@@ -67,6 +67,7 @@
#include "carefulputc.h"
#include "c.h"
#include "fileutils.h"
+#include "closestream.h"
#define IGNOREUSER "sleeper"
#define WRITE_TIME_OUT 300 /* in seconds */
@@ -114,6 +115,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
static const struct option longopts[] = {
{ "nobanner", no_argument, 0, 'n' },
@@ -281,6 +283,7 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
if (fread(mbuf, 1, *mbufsize, fp) != *mbufsize)
err(EXIT_FAILURE, _("fread failed"));
- fclose(fp);
+ if (close_stream(fp) != 0)
+ errx(EXIT_FAILURE, _("write error"));
return mbuf;
}
diff --git a/term-utils/write.c b/term-utils/write.c
index a70eb7bd1..2a94792e0 100644
--- a/term-utils/write.c
+++ b/term-utils/write.c
@@ -59,8 +59,10 @@
#include <paths.h>
#include <asm/param.h>
#include <getopt.h>
+
#include "c.h"
#include "carefulputc.h"
+#include "closestream.h"
#include "nls.h"
static void __attribute__ ((__noreturn__)) usage(FILE * out);
@@ -103,6 +105,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
switch (c) {
diff --git a/text-utils/col.c b/text-utils/col.c
index baec1945a..dc9e9c8b2 100644
--- a/text-utils/col.c
+++ b/text-utils/col.c
@@ -56,6 +56,7 @@
#include "xalloc.h"
#include "widechar.h"
#include "strutils.h"
+#include "closestream.h"
#define BS '\b' /* backspace */
#define TAB '\t' /* tab */
@@ -174,6 +175,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
max_bufd_lines = 128 * 2;
compress_spaces = 1; /* compress spaces into tabs */
@@ -388,8 +390,6 @@ int main(int argc, char **argv)
/* missing a \n on the last line? */
nblank_lines = 2;
flush_blanks();
- if (ferror(stdout) || fclose(stdout))
- return EXIT_FAILURE;
return ret;
}
diff --git a/text-utils/colcrt.c b/text-utils/colcrt.c
index d9806515e..3393d0cf3 100644
--- a/text-utils/colcrt.c
+++ b/text-utils/colcrt.c
@@ -47,6 +47,7 @@
#include "widechar.h"
#include "c.h"
+#include "closestream.h"
int plus(wchar_t c, wchar_t d);
void move(int l, int m);
@@ -93,6 +94,7 @@ int main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
/* Take care of lonely hyphen option. */
for (i = 0; i < argc; i++)
@@ -141,8 +143,6 @@ int main(int argc, char **argv) {
fclose(f);
} while (argc > 0);
fflush(stdout);
- if (ferror(stdout) || fclose(stdout))
- return EXIT_FAILURE;
return EXIT_SUCCESS;
}
diff --git a/text-utils/colrm.c b/text-utils/colrm.c
index d454643a0..910d933a2 100644
--- a/text-utils/colrm.c
+++ b/text-utils/colrm.c
@@ -41,11 +41,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
+#include <unistd.h>
#include "nls.h"
#include "widechar.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
/*
COLRM removes unwanted columns from a file
@@ -162,6 +164,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
while ((opt =
getopt_long(argc, argv, "bfhl:pxVH", longopts,
@@ -187,7 +190,5 @@ int main(int argc, char **argv)
;
fflush(stdout);
- if (ferror(stdout) || fclose(stdout))
- return EXIT_FAILURE;
return EXIT_SUCCESS;
}
diff --git a/text-utils/column.c b/text-utils/column.c
index 9d5befc13..b210a9381 100644
--- a/text-utils/column.c
+++ b/text-utils/column.c
@@ -55,6 +55,7 @@
#include "c.h"
#include "xalloc.h"
#include "strutils.h"
+#include "closestream.h"
#ifdef HAVE_WIDECHAR
#define wcs_width(s) wcswidth(s,wcslen(s))
@@ -131,6 +132,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
char *p;
@@ -203,9 +205,6 @@ int main(int argc, char **argv)
free(list[i]);
free(list);
- if (ferror(stdout) || fclose(stdout))
- eval += EXIT_FAILURE;
-
if (eval == 0)
return EXIT_SUCCESS;
else
diff --git a/text-utils/hexdump.c b/text-utils/hexdump.c
index 7edb053db..6f5335900 100644
--- a/text-utils/hexdump.c
+++ b/text-utils/hexdump.c
@@ -43,6 +43,7 @@
#include "nls.h"
#include "c.h"
+#include "closestream.h"
FS *fshead; /* head of format strings */
int blocksize; /* data block size */
@@ -57,6 +58,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (!(p = strrchr(argv[0], 'o')) || strcmp(p, "od")) {
newsyntax(argc, &argv);
diff --git a/text-utils/more.c b/text-utils/more.c
index 32eb765e9..aa1e4cc5c 100644
--- a/text-utils/more.c
+++ b/text-utils/more.c
@@ -56,11 +56,12 @@
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>
-#include "strutils.h"
+#include "strutils.h"
#include "nls.h"
#include "xalloc.h"
#include "widechar.h"
+#include "closestream.h"
#define _REGEX_RE_COMP
#include <regex.h>
@@ -90,7 +91,7 @@ void doclear(void);
void cleareol(void);
void clreos(void);
void home(void);
-void error (char *mess);
+void more_error (char *mess);
void do_shell (char *filename);
int colon (char *filename, int cmd, int nlines);
int expand (char **outbuf, char *inbuf);
@@ -330,6 +331,7 @@ int main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
nfiles = argc;
fnames = argv;
@@ -1589,7 +1591,7 @@ void search(char buf[], FILE *file, register int n)
context.chrctr = startline;
lncount = 0;
if ((s = re_comp (buf)) != 0)
- error (s);
+ more_error (s);
while (!feof (file)) {
line3 = line2;
line2 = line1;
@@ -1632,7 +1634,7 @@ void search(char buf[], FILE *file, register int n)
break;
}
} else if (rv == -1)
- error (_("Regular expression botch"));
+ more_error (_("Regular expression botch"));
}
if (feof (file)) {
if (!no_intty) {
@@ -1643,7 +1645,7 @@ void search(char buf[], FILE *file, register int n)
putsout(_("\nPattern not found\n"));
end_it (0);
}
- error (_("Pattern not found"));
+ more_error (_("Pattern not found"));
}
}
@@ -2025,7 +2027,7 @@ void ttyin (char buf[], register int nmax, char pchar) {
*--sp = '\0';
if (!eraseln) promptlen = maxlen;
if (sp - buf >= nmax - 1)
- error (_("Line too long"));
+ more_error (_("Line too long"));
}
/* return: 0 - unchanged, 1 - changed, -1 - overflow (unchanged) */
@@ -2060,7 +2062,7 @@ int expand (char **outbuf, char *inbuf) {
break;
case '!':
if (!shellp)
- error (_("No previous command to substitute for"));
+ more_error (_("No previous command to substitute for"));
strcpy (outstr, shell_line);
outstr += strlen (shell_line);
changed++;
@@ -2089,7 +2091,7 @@ void show (char c) {
promptlen++;
}
-void error (char *mess)
+void more_error (char *mess)
{
if (clreol)
cleareol ();
diff --git a/text-utils/pg.c b/text-utils/pg.c
index b61b1504f..2b5144252 100644
--- a/text-utils/pg.c
+++ b/text-utils/pg.c
@@ -63,6 +63,7 @@
#include "xalloc.h"
#include "widechar.h"
#include "writeall.h"
+#include "closestream.h"
#define READBUF LINE_MAX /* size of input buffer */
#define CMDBUF 255 /* size of command buffer */
@@ -1594,6 +1595,7 @@ main(int argc, char **argv)
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
if (tcgetattr(1, &otio) == 0) {
ontty = 1;
diff --git a/text-utils/rev.c b/text-utils/rev.c
index 8a1666800..3c64c12d1 100644
--- a/text-utils/rev.c
+++ b/text-utils/rev.c
@@ -62,6 +62,7 @@
#include "xalloc.h"
#include "widechar.h"
#include "c.h"
+#include "closestream.h"
wchar_t *buf;
@@ -96,6 +97,7 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
diff --git a/text-utils/tailf.c b/text-utils/tailf.c
index c8f3e91db..10589d8bc 100644
--- a/text-utils/tailf.c
+++ b/text-utils/tailf.c
@@ -45,6 +45,7 @@
#include "usleep.h"
#include "strutils.h"
#include "c.h"
+#include "closestream.h"
#define DEFAULT_LINES 10
@@ -240,6 +241,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
static const struct option longopts[] = {
{ "lines", required_argument, 0, 'n' },
diff --git a/text-utils/ul.c b/text-utils/ul.c
index 6132166d0..c15e48091 100644
--- a/text-utils/ul.c
+++ b/text-utils/ul.c
@@ -54,6 +54,7 @@
#include "xalloc.h"
#include "widechar.h"
#include "c.h"
+#include "closestream.h"
#ifdef HAVE_WIDECHAR
/* Output an ASCII character as a wide character */
@@ -162,6 +163,7 @@ int main(int argc, char **argv)
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
@@ -231,9 +233,6 @@ int main(int argc, char **argv)
filter(f);
fclose(f);
}
- if (ferror(stdout) || fclose(stdout))
- return EXIT_FAILURE;
-
free(obuf);
return EXIT_SUCCESS;
}