changeset 4011:323c09be5f00 HEAD

Moved dupliated close_save_errno() code to public close_keep_errno() function.
author Timo Sirainen <timo.sirainen@movial.fi>
date Tue, 14 Feb 2006 15:41:58 +0200
parents 6519f3c7bd63
children d8c1e641b435
files src/lib/Makefile.am src/lib/close-keep-errno.c src/lib/close-keep-errno.h src/lib/network.c src/lib/unlink-directory.c
diffstat 5 files changed, 32 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Makefile.am	Tue Feb 14 15:06:58 2006 +0200
+++ b/src/lib/Makefile.am	Tue Feb 14 15:41:58 2006 +0200
@@ -4,6 +4,7 @@
 	base64.c \
 	bsearch-insert-pos.c \
 	buffer.c \
+	close-keep-errno.c \
 	compat.c \
 	data-stack.c \
 	env-util.c \
@@ -85,6 +86,7 @@
 	base64.h \
 	bsearch-insert-pos.h \
 	buffer.h \
+	close-keep-errno.h \
 	compat.h \
 	data-stack.h \
 	env-util.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/close-keep-errno.c	Tue Feb 14 15:41:58 2006 +0200
@@ -0,0 +1,13 @@
+/* Copyright (c) 2006 Timo Sirainen */
+
+#include "lib.h"
+#include "close-keep-errno.h"
+
+#include <unistd.h>
+
+void close_keep_errno(int fd)
+{
+	int old_errno = errno;
+	(void)close(fd);
+	errno = old_errno;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/close-keep-errno.h	Tue Feb 14 15:41:58 2006 +0200
@@ -0,0 +1,7 @@
+#ifndef __CLOSE_KEEP_ERRNO_H
+#define __CLOSE_KEEP_ERRNO_H
+
+/* Close the file handle without changing errno. */
+void close_keep_errno(int fd);
+
+#endif
--- a/src/lib/network.c	Tue Feb 14 15:06:58 2006 +0200
+++ b/src/lib/network.c	Tue Feb 14 15:41:58 2006 +0200
@@ -1,6 +1,7 @@
 /* Copyright (c) 1999-2005 Timo Sirainen */
 
 #include "lib.h"
+#include "close-keep-errno.h"
 #include "fd-set-nonblock.h"
 #include "network.h"
 
@@ -101,13 +102,6 @@
 	return 0;
 }
 
-static inline void close_save_errno(int fd)
-{
-	int old_errno = errno;
-	(void)close(fd);
-	errno = old_errno;
-}
-
 /* Connect to socket with ip address */
 int net_connect_ip(const struct ip_addr *ip, unsigned int port,
 		   const struct ip_addr *my_ip)
@@ -141,7 +135,7 @@
 		if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
 			/* failed, set it back to INADDR_ANY */
 			i_error("bind(%s) failed: %m", net_ip2addr(my_ip));
-			close_save_errno(fd);
+			close_keep_errno(fd);
 			return -1;
 		}
 	}
@@ -157,7 +151,7 @@
 	if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
 #endif
 	{
-                close_save_errno(fd);
+                close_keep_errno(fd);
 		return -1;
 	}
 
@@ -189,7 +183,7 @@
 	/* connect */
 	ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
 	if (ret < 0 && errno != EINPROGRESS) {
-                close_save_errno(fd);
+                close_keep_errno(fd);
 		return -1;
 	}
 
@@ -305,7 +299,7 @@
 	}
 
         /* error */
-	close_save_errno(fd);
+	close_keep_errno(fd);
 	return -1;
 }
 
@@ -342,7 +336,7 @@
 			i_error("listen() failed: %m");
 	}
 
-	close_save_errno(fd);
+	close_keep_errno(fd);
 	return -1;
 }
 
--- a/src/lib/unlink-directory.c	Tue Feb 14 15:06:58 2006 +0200
+++ b/src/lib/unlink-directory.c	Tue Feb 14 15:41:58 2006 +0200
@@ -33,6 +33,7 @@
 #define _GNU_SOURCE /* for O_NOFOLLOW with Linux */
 
 #include "lib.h"
+#include "close-keep-errno.h"
 #include "unlink-directory.h"
 
 #include <fcntl.h>
@@ -40,13 +41,6 @@
 #include <dirent.h>
 #include <sys/stat.h>
 
-#define close_save_errno(fd) \
-	STMT_START { \
-		old_errno = errno; \
-		(void)close(fd); \
-		errno = old_errno; \
-	} STMT_END
-
 static int unlink_directory_r(const char *dir)
 {
 	DIR *dirp;
@@ -74,7 +68,7 @@
 		return -1;
 
 	if (fstat(dir_fd, &st2) < 0) {
-		close_save_errno(dir_fd);
+		close_keep_errno(dir_fd);
 		return -1;
 	}
 
@@ -87,13 +81,13 @@
 	}
 #endif
 	if (fchdir(dir_fd) < 0) {
-                close_save_errno(dir_fd);
+                close_keep_errno(dir_fd);
 		return -1;
 	}
 
 	dirp = opendir(".");
 	if (dirp == NULL) {
-		close_save_errno(dir_fd);
+		close_keep_errno(dir_fd);
 		return -1;
 	}