Make sure the correct errno is reported by warn* or err* and not
authorguenther <guenther@openbsd.org>
Sun, 20 Jul 2014 02:03:21 +0000 (02:03 +0000)
committerguenther <guenther@openbsd.org>
Sun, 20 Jul 2014 02:03:21 +0000 (02:03 +0000)
the errno of an intervening cleanup operation like close/unlink/etc.
Also, the format string for warn* and err* shouldn't end with a newline.

Diff from Doug Hogan (doug (at) acyclic.org)

sys/dev/microcode/bwi/build/build.c

index 2869b3a..41aa6fa 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: build.c,v 1.3 2014/07/12 19:01:49 tedu Exp $ */
+/*     $OpenBSD: build.c,v 1.4 2014/07/20 02:03:21 guenther Exp $ */
 
 /*
  * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org>
@@ -20,6 +20,7 @@
 #include <sys/stat.h>
 
 #include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -104,12 +105,14 @@ main(int argc, char *argv[])
 
        /* write header */
        if (write(fdout, &nfiles, sizeof(nfiles)) < 1) {
+               int saved_errno = errno;
                close(fdout);
-               err(1, "write header 1 to output file failed\n");
+               errc(1, saved_errno, "write header 1 to output file failed");
        }
        if (write(fdout, h, headersize - sizeof(nfiles)) < 1) {
+               int saved_errno = errno;
                close(fdout);
-               err(1, "write header 2 to output file failed\n");
+               errc(1, saved_errno, "write header 2 to output file failed");
        }
 
        /* network to host byte order */
@@ -122,25 +125,29 @@ main(int argc, char *argv[])
        /* write each file */
        for (i = 0; i < nfiles; i++) {
                if ((fdin = open(h[i].filename, O_RDONLY)) == -1) {
+                       int saved_errno = errno;
                        close(fdout);
-                       err(1, "open input file failed\n");
+                       errc(1, saved_errno, "open input file failed");
                }
                if ((p = malloc(h[i].filesize)) == NULL) {
+                       int saved_errno = errno;
                        close(fdout);
                        close(fdin);
-                       err(1, "malloc");
+                       errc(1, saved_errno, "malloc");
                }
                if (read(fdin, p, h[i].filesize) < 1) {
+                       int saved_errno = errno;
                        free(p);
                        close(fdout);
                        close(fdin);
-                       err(1, "read input file failed\n");
+                       errc(1, saved_errno, "read input file failed");
                }
                if (write(fdout, p, h[i].filesize) < 1) {
+                       int saved_errno = errno;
                        free(p);
                        close(fdout);
                        close(fdin);
-                       err(1, "write to output file failed\n");
+                       errc(1, saved_errno, "write to output file failed");
                }
                free(p);
                close(fdin);