open3.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Defines for Sys V style 3-argument open call.
  2. Copyright (C) 1988, 1994, 1995, 1996 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #if EMUL_OPEN3
  16. /* open3.h -- #defines for the various flags for the Sys V style 3-argument
  17. open() call. On BSD or System 5, the system already has this in an
  18. include file. This file is needed for V7 and MINIX systems for the
  19. benefit of open3() in port.c, a routine that emulates the 3-argument call
  20. using system calls available on V7/MINIX.
  21. Written 1987-06-10 by Richard Todd.
  22. The names have been changed by John Gilmore, 1987-07-31, since Richard
  23. called it "bsdopen", and really this change was introduced in AT&T Unix
  24. systems before BSD picked it up. */
  25. /*-----------------------------------------------------------------------.
  26. | open3 -- routine to emulate the 3-argument open system. |
  27. | |
  28. | open3 (path, flag, mode); |
  29. | |
  30. | Attempts to open the file specified by the given pathname. The |
  31. | following flag bits specify options to the routine. Needless to say, |
  32. | you should only specify one of the first three. Function returns file |
  33. | descriptor if successful, -1 and errno if not. |
  34. `-----------------------------------------------------------------------*/
  35. /* The routine obeys the following mode arguments:
  36. O_RDONLY file open for read only
  37. O_WRONLY file open for write only
  38. O_RDWR file open for both read & write
  39. O_CREAT file is created with specified mode if it needs to be
  40. O_TRUNC if file exists, it is truncated to 0 bytes
  41. O_EXCL used with O_CREAT--routine returns error if file exists */
  42. /* Call that if present in most modern Unix systems. This version attempts
  43. to support all the flag bits except for O_NDELAY and O_APPEND, which are
  44. silently ignored. The emulation is not as efficient as the real thing
  45. (at worst, 4 system calls instead of one), but there's not much I can do
  46. about that. */
  47. /* Array to give arguments to access for various modes FIXME, this table
  48. depends on the specific integer values of O_*, and also contains
  49. integers (args to 'access') that should be #define's. */
  50. static int modes[] =
  51. {
  52. 04, /* O_RDONLY */
  53. 02, /* O_WRONLY */
  54. 06, /* O_RDWR */
  55. 06, /* invalid, just cope: O_WRONLY+O_RDWR */
  56. };
  57. /* Shut off the automatic emulation of open(), we'll need it. */
  58. #undef open
  59. int
  60. open3 (char *path, int flags, int mode)
  61. {
  62. int exists = 1;
  63. int call_creat = 0;
  64. /* We actually do the work by calling the open() or creat() system
  65. call, depending on the flags. Call_creat is true if we will use
  66. creat(), false if we will use open(). */
  67. /* See if the file exists and is accessible in the requested mode.
  68. Strictly speaking we shouldn't be using access, since access checks
  69. against real uid, and the open call should check against euid. Most
  70. cases real uid == euid, so it won't matter. FIXME. FIXME, the
  71. construction "flags & 3" and the modes table depends on the specific
  72. integer values of the O_* #define's. Foo! */
  73. if (access (path, modes[flags & 3]) < 0)
  74. {
  75. if (errno == ENOENT)
  76. {
  77. /* The file does not exist. */
  78. exists = 0;
  79. }
  80. else
  81. {
  82. /* Probably permission violation. */
  83. if (flags & O_EXCL)
  84. {
  85. /* Oops, the file exists, we didn't want it. No matter
  86. what the error, claim EEXIST. */
  87. errno = EEXIST; /* FIXME: errno should be read-only */
  88. }
  89. return -1;
  90. }
  91. }
  92. /* If we have the O_CREAT bit set, check for O_EXCL. */
  93. if (flags & O_CREAT)
  94. {
  95. if ((flags & O_EXCL) && exists)
  96. {
  97. /* Oops, the file exists and we didn't want it to. */
  98. errno = EEXIST; /* FIXME: errno should be read-only */
  99. return -1;
  100. }
  101. /* If the file doesn't exist, be sure to call creat() so that it
  102. will be created with the proper mode. */
  103. if (!exists)
  104. call_creat = 1;
  105. }
  106. else
  107. {
  108. /* If O_CREAT isn't set and the file doesn't exist, error. */
  109. if (!exists)
  110. {
  111. errno = ENOENT; /* FIXME: errno should be read-only */
  112. return -1;
  113. }
  114. }
  115. /* If the O_TRUNC flag is set and the file exists, we want to call
  116. creat() anyway, since creat() guarantees that the file will be
  117. truncated and open()-for-writing doesn't. (If the file doesn't
  118. exist, we're calling creat() anyway and the file will be created
  119. with zero length.) */
  120. if ((flags & O_TRUNC) && exists)
  121. call_creat = 1;
  122. /* Actually do the call. */
  123. if (call_creat)
  124. /* Call creat. May have to close and reopen the file if we want
  125. O_RDONLY or O_RDWR access -- creat() only gives O_WRONLY. */
  126. {
  127. int fd = creat (path, mode);
  128. if (fd < 0 || (flags & O_WRONLY))
  129. return fd;
  130. if (close (fd) < 0)
  131. return -1;
  132. /* Fall out to reopen the file we've created. */
  133. }
  134. /* Calling old open, we strip most of the new flags just in case. */
  135. return open (path, flags & (O_RDONLY | O_WRONLY | O_RDWR | O_BINARY));
  136. }
  137. #endif /* EMUL_OPEN3 */