]> diplodocus.org Git - mdeliver/commitdiff
(save_errno): New macro.
authorepg <>
Fri, 6 Sep 2002 07:08:49 +0000 (07:08 +0000)
committerepg <>
Fri, 6 Sep 2002 07:08:49 +0000 (07:08 +0000)
Use save_errno to protect errno in the 101 places i was calling
unlink(2) and then err(3), possibly reporting the wrong error message.

mdeliver.c

index 0073c47..5d4b900 100644 (file)
  * LEN_ULONGs for time and pid. */
 #define TMPNAMLEN 1 + 4 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG
 
+/* Use this macro to perform some operation and saving the value errno
+ * had before that operation.
+ */
+#define save_errno(op) {                                                \
+    int olderrno = errno;                                               \
+    op;                                                                 \
+    errno = olderrno;                                                   \
+}
+
 static char rcsid[] = "$Id$";
 
 /* Write all of buf, even if write(2) is interrupted. */
@@ -146,7 +155,7 @@ strip_from(int fd, char *buf, char *fn)
 
     r = read(STDIN_FILENO, buf, 5);
     if (r != 5) {
-        unlink(fn);
+        save_errno( unlink(fn) );
         err(MDELIVER_EXITCODE, "Failed read from stdin");
     }
 
@@ -159,12 +168,12 @@ strip_from(int fd, char *buf, char *fn)
         }
 
         if (r == -1) {
-            unlink(fn);
+            save_errno( unlink(fn) );
             err(MDELIVER_EXITCODE, "Failed read from stdin");
         }
     } else {
         if (full_write(fd, buf, r) != r) {
-            unlink(fn);
+            save_errno( unlink(fn) );
             err(MDELIVER_EXITCODE, "Failed write(%s)", fn);
         }
     }
@@ -180,23 +189,23 @@ copy_message(int fd, char *buf, char *fn)
     while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0
            || (r == -1 && errno == EINTR)) {
         if (full_write(fd, buf, r) != r) {
-            unlink(fn);
+            save_errno( unlink(fn) );
             err(MDELIVER_EXITCODE, "Failed write(%s)", fn);
         }
     }
 
     if (r == -1) {
-        unlink(fn);
+        save_errno( unlink(fn) );
         err(MDELIVER_EXITCODE, "Failed read from stdin");
     }
 
     if (fsync(fd) < 0) {
-        unlink(fn);
+        save_errno( unlink(fn) );
         err(MDELIVER_EXITCODE, "Failed fsync(%s)", fn);
     }
 
     if (close(fd) < 0) {
-        unlink(fn);
+        save_errno( unlink(fn) );
         err(MDELIVER_EXITCODE, "Failed close(%s)", fn);
     }
 }
@@ -230,7 +239,7 @@ deliver(char *maildir, char *newfn)
     memcpy(newfn, "new", 3);
 
     if (rename(fn, newfn) != 0) {
-        unlink(fn);
+        save_errno( unlink(fn) );
         err(MDELIVER_EXITCODE, "Failed rename(%s, new)", fn);
     }
 }