open.c 5.2 KB

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