X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fopen.c;h=13af274602661bb5b628c6d225d51ad2f4673007;hb=a5bd4a560247e63db562041f9d136ba83a8b95fb;hp=317fb8afac217dd229d62bb67a7973bcdb6f13f8;hpb=955345ebebdae2b894b5a3d8e21036335cb13e5d;p=gnulib.git diff --git a/lib/open.c b/lib/open.c index 317fb8afa..13af27460 100644 --- a/lib/open.c +++ b/lib/open.c @@ -18,21 +18,29 @@ #include -/* Specification. */ +/* Get the original definition of open. It might be defined as a macro. */ +#define __need_system_fcntl_h #include +#undef __need_system_fcntl_h +#include -/* If the fchdir replacement is used, open() is defined in fchdir.c. */ -#ifndef FCHDIR_REPLACEMENT +static inline int +orig_open (const char *filename, int flags, mode_t mode) +{ + return open (filename, flags, mode); +} -# include -# include -# include -# include -# include +/* Specification. */ +#include + +#include +#include +#include +#include +#include int open (const char *filename, int flags, ...) -# undef open { mode_t mode; int fd; @@ -53,12 +61,12 @@ open (const char *filename, int flags, ...) va_end (arg); } -# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ if (strcmp (filename, "/dev/null") == 0) filename = "NUL"; -# endif +#endif -# if OPEN_TRAILING_SLASH_BUG +#if OPEN_TRAILING_SLASH_BUG /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR is specified, then fail. Rationale: POSIX @@ -89,11 +97,11 @@ open (const char *filename, int flags, ...) return -1; } } -# endif +#endif - fd = open (filename, flags, mode); + fd = orig_open (filename, flags, mode); -# if OPEN_TRAILING_SLASH_BUG +#if OPEN_TRAILING_SLASH_BUG /* If the filename ends in a slash and fd does not refer to a directory, then fail. Rationale: POSIX @@ -115,13 +123,18 @@ open (const char *filename, int flags, ...) if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) { + close (fd); errno = ENOTDIR; return -1; } } } -# endif +#endif + +#ifdef FCHDIR_REPLACEMENT + if (fd >= 0) + _gl_register_fd (fd, filename); +#endif return fd; } -#endif