X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fopen.c;h=08ecaff44b49efb636931828e54b6b89861445d8;hb=4a60ba548bf42233384fa86fd97cf0091514c553;hp=9a35f5b2a6add92db03e40da7b7c63a70f0d894a;hpb=e2600ffdcf6c35561545d553e07e52e0db8f9a38;p=gnulib.git diff --git a/lib/open.c b/lib/open.c index 9a35f5b2a..08ecaff44 100644 --- a/lib/open.c +++ b/lib/open.c @@ -1,5 +1,5 @@ /* Open a descriptor to a file. - Copyright (C) 2007-2008 Free Software Foundation, Inc. + Copyright (C) 2007-2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -33,14 +33,16 @@ orig_open (const char *filename, int flags, mode_t mode) /* Specification. */ #include -/* If the fchdir replacement is used, open() is defined in fchdir.c. */ -#ifndef FCHDIR_REPLACEMENT +#include +#include +#include +#include +#include +#include -# include -# include -# include -# include -# include +#ifndef REPLACE_OPEN_DIRECTORY +# define REPLACE_OPEN_DIRECTORY 0 +#endif int open (const char *filename, int flags, ...) @@ -54,22 +56,19 @@ open (const char *filename, int flags, ...) va_list arg; va_start (arg, flags); - /* If mode_t is narrower than int, use the promoted type (int), - not mode_t. Use sizeof to guess whether mode_t is narrower; - we don't know of any practical counterexamples. */ - mode = (sizeof (mode_t) < sizeof (int) - ? va_arg (arg, int) - : va_arg (arg, mode_t)); + /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4 + creates crashing code when 'mode_t' is smaller than 'int'. */ + mode = va_arg (arg, PROMOTED_MODE_T); 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 @@ -100,11 +99,34 @@ open (const char *filename, int flags, ...) return -1; } } -# endif +#endif fd = orig_open (filename, flags, mode); -# if OPEN_TRAILING_SLASH_BUG +#if REPLACE_FCHDIR + /* Implementing fchdir and fdopendir requires the ability to open a + directory file descriptor. If open doesn't support that (as on + mingw), we use a dummy file that behaves the same as directories + on Linux (ie. always reports EOF on attempts to read()), and + override fstat() in fchdir.c to hide the fact that we have a + dummy. */ + if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES + && (mode & O_ACCMODE) == O_RDONLY) + { + struct stat statbuf; + if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)) + { + /* Maximum recursion depth of 1. */ + fd = open ("/dev/null", flags, mode); + if (0 <= fd) + fd = _gl_register_fd (fd, filename); + } + else + errno = EACCES; + } +#endif + +#if OPEN_TRAILING_SLASH_BUG /* If the filename ends in a slash and fd does not refer to a directory, then fail. Rationale: POSIX @@ -132,8 +154,12 @@ open (const char *filename, int flags, ...) } } } -# endif +#endif + +#if REPLACE_FCHDIR + if (!REPLACE_OPEN_DIRECTORY && 0 <= fd) + fd = _gl_register_fd (fd, filename); +#endif return fd; } -#endif