open.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Open a descriptor to a file.
  2. Copyright (C) 2007-2008 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. int
  33. open (const char *filename, int flags, ...)
  34. {
  35. mode_t mode;
  36. int fd;
  37. mode = 0;
  38. if (flags & O_CREAT)
  39. {
  40. va_list arg;
  41. va_start (arg, flags);
  42. /* If mode_t is narrower than int, use the promoted type (int),
  43. not mode_t. Use sizeof to guess whether mode_t is narrower;
  44. we don't know of any practical counterexamples. */
  45. mode = (sizeof (mode_t) < sizeof (int)
  46. ? va_arg (arg, int)
  47. : va_arg (arg, mode_t));
  48. va_end (arg);
  49. }
  50. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  51. if (strcmp (filename, "/dev/null") == 0)
  52. filename = "NUL";
  53. #endif
  54. #if OPEN_TRAILING_SLASH_BUG
  55. /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
  56. is specified, then fail.
  57. Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
  58. says that
  59. "A pathname that contains at least one non-slash character and that
  60. ends with one or more trailing slashes shall be resolved as if a
  61. single dot character ( '.' ) were appended to the pathname."
  62. and
  63. "The special filename dot shall refer to the directory specified by
  64. its predecessor."
  65. If the named file already exists as a directory, then
  66. - if O_CREAT is specified, open() must fail because of the semantics
  67. of O_CREAT,
  68. - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
  69. <http://www.opengroup.org/susv3/functions/open.html> says that it
  70. fails with errno = EISDIR in this case.
  71. If the named file does not exist or does not name a directory, then
  72. - if O_CREAT is specified, open() must fail since open() cannot create
  73. directories,
  74. - if O_WRONLY or O_RDWR is specified, open() must fail because the
  75. file does not contain a '.' directory. */
  76. if (flags & (O_CREAT | O_WRONLY | O_RDWR))
  77. {
  78. size_t len = strlen (filename);
  79. if (len > 0 && filename[len - 1] == '/')
  80. {
  81. errno = EISDIR;
  82. return -1;
  83. }
  84. }
  85. #endif
  86. fd = orig_open (filename, flags, mode);
  87. #if OPEN_TRAILING_SLASH_BUG
  88. /* If the filename ends in a slash and fd does not refer to a directory,
  89. then fail.
  90. Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
  91. says that
  92. "A pathname that contains at least one non-slash character and that
  93. ends with one or more trailing slashes shall be resolved as if a
  94. single dot character ( '.' ) were appended to the pathname."
  95. and
  96. "The special filename dot shall refer to the directory specified by
  97. its predecessor."
  98. If the named file without the slash is not a directory, open() must fail
  99. with ENOTDIR. */
  100. if (fd >= 0)
  101. {
  102. size_t len = strlen (filename);
  103. if (len > 0 && filename[len - 1] == '/')
  104. {
  105. struct stat statbuf;
  106. if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
  107. {
  108. close (fd);
  109. errno = ENOTDIR;
  110. return -1;
  111. }
  112. }
  113. }
  114. #endif
  115. #ifdef FCHDIR_REPLACEMENT
  116. if (fd >= 0)
  117. _gl_register_fd (fd, filename);
  118. #endif
  119. return fd;
  120. }