fopen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Open a stream to a file.
  2. Copyright (C) 2007-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
  14. /* If the user's config.h happens to include <stdio.h>, let it include only
  15. the system's <stdio.h> here, so that orig_fopen doesn't recurse to
  16. rpl_fopen. */
  17. #define __need_FILE
  18. #include <config.h>
  19. /* Get the original definition of fopen. It might be defined as a macro. */
  20. #include <stdio.h>
  21. #undef __need_FILE
  22. static FILE *
  23. orig_fopen (const char *filename, const char *mode)
  24. {
  25. return fopen (filename, mode);
  26. }
  27. /* Specification. */
  28. /* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
  29. this include because of the preliminary #include <stdio.h> above. */
  30. #include "stdio.h"
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. FILE *
  38. rpl_fopen (const char *filename, const char *mode)
  39. {
  40. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  41. if (strcmp (filename, "/dev/null") == 0)
  42. filename = "NUL";
  43. #endif
  44. #if FOPEN_TRAILING_SLASH_BUG
  45. /* If the filename ends in a slash and a mode that requires write access is
  46. specified, then fail.
  47. Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
  48. says that
  49. "A pathname that contains at least one non-slash character and that
  50. ends with one or more trailing slashes shall be resolved as if a
  51. single dot character ( '.' ) were appended to the pathname."
  52. and
  53. "The special filename dot shall refer to the directory specified by
  54. its predecessor."
  55. If the named file already exists as a directory, then if a mode that
  56. requires write access is specified, fopen() must fail because POSIX
  57. <http://www.opengroup.org/susv3/functions/fopen.html> says that it
  58. fails with errno = EISDIR in this case.
  59. If the named file does not exist or does not name a directory, then
  60. fopen() must fail since the file does not contain a '.' directory. */
  61. {
  62. size_t len = strlen (filename);
  63. if (len > 0 && filename[len - 1] == '/')
  64. {
  65. int fd;
  66. struct stat statbuf;
  67. FILE *fp;
  68. if (mode[0] == 'w' || mode[0] == 'a')
  69. {
  70. errno = EISDIR;
  71. return NULL;
  72. }
  73. fd = open (filename, O_RDONLY);
  74. if (fd < 0)
  75. return NULL;
  76. if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
  77. {
  78. close (fd);
  79. errno = ENOTDIR;
  80. return NULL;
  81. }
  82. fp = fdopen (fd, mode);
  83. if (fp == NULL)
  84. {
  85. int saved_errno = errno;
  86. close (fd);
  87. errno = saved_errno;
  88. }
  89. return fp;
  90. }
  91. }
  92. # endif
  93. return orig_fopen (filename, mode);
  94. }