X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffopen.c;h=81c861724ec751a5b51ada8a7b6834c474ecd050;hb=825499de077435754651ff49b3f3dba576a2ebc6;hp=d6e048b79b6b43fa90ded109811a95926be9a740;hpb=810a812bee90d6bab12e9628dc7745eea49f188c;p=gnulib.git diff --git a/lib/fopen.c b/lib/fopen.c index d6e048b79..81c861724 100644 --- a/lib/fopen.c +++ b/lib/fopen.c @@ -1,5 +1,5 @@ /* Open a stream to a file. - Copyright (C) 2007-2008 Free Software Foundation, Inc. + Copyright (C) 2007-2011 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 @@ -18,6 +18,17 @@ #include +/* Get the original definition of fopen. It might be defined as a macro. */ +#define __need_FILE +#include +#undef __need_FILE + +static inline FILE * +orig_fopen (const char *filename, const char *mode) +{ + return fopen (filename, mode); +} + /* Specification. */ #include @@ -25,10 +36,11 @@ #include #include #include +#include +#include FILE * rpl_fopen (const char *filename, const char *mode) -#undef fopen { #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ if (strcmp (filename, "/dev/null") == 0) @@ -56,38 +68,38 @@ rpl_fopen (const char *filename, const char *mode) size_t len = strlen (filename); if (len > 0 && filename[len - 1] == '/') { - int fd; - struct stat statbuf; - FILE *fp; - - if (mode[0] == 'w' || mode[0] == 'a') - { - errno = EISDIR; - return NULL; - } - - fd = open (filename, O_RDONLY); - if (fd < 0) - return NULL; - - if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) - { - close (fd); - errno = ENOTDIR; - return NULL; - } - - fp = fdopen (fd, mode); - if (fp == NULL) - { - int saved_errno = errno; - close (fd); - errno = saved_errno; - } - return fp; + int fd; + struct stat statbuf; + FILE *fp; + + if (mode[0] == 'w' || mode[0] == 'a') + { + errno = EISDIR; + return NULL; + } + + fd = open (filename, O_RDONLY); + if (fd < 0) + return NULL; + + if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode)) + { + close (fd); + errno = ENOTDIR; + return NULL; + } + + fp = fdopen (fd, mode); + if (fp == NULL) + { + int saved_errno = errno; + close (fd); + errno = saved_errno; + } + return fp; } } # endif - return fopen (filename, mode); + return orig_fopen (filename, mode); }