open.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Open a descriptor 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 <fcntl.h>, let it include only
  15. the system's <fcntl.h> here, so that orig_open doesn't recurse to
  16. rpl_open. */
  17. #define __need_system_fcntl_h
  18. #include <config.h>
  19. /* Get the original definition of open. It might be defined as a macro. */
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #undef __need_system_fcntl_h
  23. static int
  24. orig_open (const char *filename, int flags, mode_t mode)
  25. {
  26. return open (filename, flags, mode);
  27. }
  28. /* Specification. */
  29. /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
  30. this include because of the preliminary #include <fcntl.h> above. */
  31. #include "fcntl.h"
  32. #include <errno.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <unistd.h>
  38. #ifndef REPLACE_OPEN_DIRECTORY
  39. # define REPLACE_OPEN_DIRECTORY 0
  40. #endif
  41. int
  42. open (const char *filename, int flags, ...)
  43. {
  44. mode_t mode;
  45. int fd;
  46. mode = 0;
  47. if (flags & O_CREAT)
  48. {
  49. va_list arg;
  50. va_start (arg, flags);
  51. /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
  52. creates crashing code when 'mode_t' is smaller than 'int'. */
  53. mode = va_arg (arg, PROMOTED_MODE_T);
  54. va_end (arg);
  55. }
  56. #if GNULIB_defined_O_NONBLOCK
  57. /* The only known platform that lacks O_NONBLOCK is mingw, but it
  58. also lacks named pipes and Unix sockets, which are the only two
  59. file types that require non-blocking handling in open().
  60. Therefore, it is safe to ignore O_NONBLOCK here. It is handy
  61. that mingw also lacks openat(), so that is also covered here. */
  62. flags &= ~O_NONBLOCK;
  63. #endif
  64. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  65. if (strcmp (filename, "/dev/null") == 0)
  66. filename = "NUL";
  67. #endif
  68. #if OPEN_TRAILING_SLASH_BUG
  69. /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
  70. is specified, then fail.
  71. Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
  72. says that
  73. "A pathname that contains at least one non-slash character and that
  74. ends with one or more trailing slashes shall be resolved as if a
  75. single dot character ( '.' ) were appended to the pathname."
  76. and
  77. "The special filename dot shall refer to the directory specified by
  78. its predecessor."
  79. If the named file already exists as a directory, then
  80. - if O_CREAT is specified, open() must fail because of the semantics
  81. of O_CREAT,
  82. - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
  83. <http://www.opengroup.org/susv3/functions/open.html> says that it
  84. fails with errno = EISDIR in this case.
  85. If the named file does not exist or does not name a directory, then
  86. - if O_CREAT is specified, open() must fail since open() cannot create
  87. directories,
  88. - if O_WRONLY or O_RDWR is specified, open() must fail because the
  89. file does not contain a '.' directory. */
  90. if (flags & (O_CREAT | O_WRONLY | O_RDWR))
  91. {
  92. size_t len = strlen (filename);
  93. if (len > 0 && filename[len - 1] == '/')
  94. {
  95. errno = EISDIR;
  96. return -1;
  97. }
  98. }
  99. #endif
  100. fd = orig_open (filename, flags, mode);
  101. #if REPLACE_FCHDIR
  102. /* Implementing fchdir and fdopendir requires the ability to open a
  103. directory file descriptor. If open doesn't support that (as on
  104. mingw), we use a dummy file that behaves the same as directories
  105. on Linux (ie. always reports EOF on attempts to read()), and
  106. override fstat() in fchdir.c to hide the fact that we have a
  107. dummy. */
  108. if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
  109. && ((flags & O_ACCMODE) == O_RDONLY
  110. || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
  111. {
  112. struct stat statbuf;
  113. if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
  114. {
  115. /* Maximum recursion depth of 1. */
  116. fd = open ("/dev/null", flags, mode);
  117. if (0 <= fd)
  118. fd = _gl_register_fd (fd, filename);
  119. }
  120. else
  121. errno = EACCES;
  122. }
  123. #endif
  124. #if OPEN_TRAILING_SLASH_BUG
  125. /* If the filename ends in a slash and fd does not refer to a directory,
  126. then fail.
  127. Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
  128. says that
  129. "A pathname that contains at least one non-slash character and that
  130. ends with one or more trailing slashes shall be resolved as if a
  131. single dot character ( '.' ) were appended to the pathname."
  132. and
  133. "The special filename dot shall refer to the directory specified by
  134. its predecessor."
  135. If the named file without the slash is not a directory, open() must fail
  136. with ENOTDIR. */
  137. if (fd >= 0)
  138. {
  139. /* We know len is positive, since open did not fail with ENOENT. */
  140. size_t len = strlen (filename);
  141. if (filename[len - 1] == '/')
  142. {
  143. struct stat statbuf;
  144. if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
  145. {
  146. close (fd);
  147. errno = ENOTDIR;
  148. return -1;
  149. }
  150. }
  151. }
  152. #endif
  153. #if REPLACE_FCHDIR
  154. if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
  155. fd = _gl_register_fd (fd, filename);
  156. #endif
  157. return fd;
  158. }