flock_compat.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include <errno.h>
  18. #include "ext/standard/flock_compat.h"
  19. #ifndef HAVE_FLOCK
  20. PHPAPI int flock(int fd, int operation)
  21. {
  22. return php_flock(fd, operation);
  23. }
  24. #endif /* !defined(HAVE_FLOCK) */
  25. PHPAPI int php_flock(int fd, int operation)
  26. #if HAVE_STRUCT_FLOCK /* {{{ */
  27. {
  28. struct flock flck;
  29. int ret;
  30. flck.l_start = flck.l_len = 0;
  31. flck.l_whence = SEEK_SET;
  32. if (operation & LOCK_SH)
  33. flck.l_type = F_RDLCK;
  34. else if (operation & LOCK_EX)
  35. flck.l_type = F_WRLCK;
  36. else if (operation & LOCK_UN)
  37. flck.l_type = F_UNLCK;
  38. else {
  39. errno = EINVAL;
  40. return -1;
  41. }
  42. ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
  43. if ((operation & LOCK_NB) && ret == -1 &&
  44. (errno == EACCES || errno == EAGAIN))
  45. errno = EWOULDBLOCK;
  46. if (ret != -1) ret = 0;
  47. return ret;
  48. }
  49. /* }}} */
  50. #elif defined(PHP_WIN32) /* {{{ */
  51. /*
  52. * Program: Unix compatibility routines
  53. *
  54. * Author: Mark Crispin
  55. * Networks and Distributed Computing
  56. * Computing & Communications
  57. * University of Washington
  58. * Administration Building, AG-44
  59. * Seattle, WA 98195
  60. * Internet: MRC@CAC.Washington.EDU
  61. *
  62. * Date: 14 September 1996
  63. * Last Edited: 14 August 1997
  64. *
  65. * Copyright 1997 by the University of Washington
  66. *
  67. * Permission to use, copy, modify, and distribute this software and its
  68. * documentation for any purpose and without fee is hereby granted, provided
  69. * that the above copyright notice appears in all copies and that both the
  70. * above copyright notice and this permission notice appear in supporting
  71. * documentation, and that the name of the University of Washington not be
  72. * used in advertising or publicity pertaining to distribution of the software
  73. * without specific, written prior permission. This software is made available
  74. * "as is", and
  75. * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  76. * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  77. * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  78. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  79. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  80. * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  81. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  82. *
  83. */
  84. /* DEDICATION
  85. * This file is dedicated to my dog, Unix, also known as Yun-chan and
  86. * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
  87. * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
  88. * a two-month bout with cirrhosis of the liver.
  89. *
  90. * He was a dear friend, and I miss him terribly.
  91. *
  92. * Lift a leg, Yunie. Luv ya forever!!!!
  93. */
  94. {
  95. HANDLE hdl = (HANDLE) _get_osfhandle(fd);
  96. DWORD low = 0xFFFFFFFF, high = 0xFFFFFFFF;
  97. OVERLAPPED offset =
  98. {0, 0, 0, 0, NULL};
  99. DWORD err;
  100. if (INVALID_HANDLE_VALUE == hdl) {
  101. _set_errno(EBADF);
  102. return -1; /* error in file descriptor */
  103. }
  104. /* bug for bug compatible with Unix */
  105. UnlockFileEx(hdl, 0, low, high, &offset);
  106. switch (operation & ~LOCK_NB) { /* translate to LockFileEx() op */
  107. case LOCK_EX: /* exclusive */
  108. if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
  109. ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
  110. 0, low, high, &offset))
  111. return 0;
  112. break;
  113. case LOCK_SH: /* shared */
  114. if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
  115. 0, low, high, &offset))
  116. return 0;
  117. break;
  118. case LOCK_UN: /* unlock */
  119. return 0; /* always succeeds */
  120. default: /* default */
  121. break;
  122. }
  123. err = GetLastError();
  124. if (ERROR_LOCK_VIOLATION == err || ERROR_SHARING_VIOLATION == err) {
  125. _set_errno(EWOULDBLOCK);
  126. } else {
  127. _set_errno(EINVAL); /* bad call */
  128. }
  129. return -1;
  130. }
  131. /* }}} */
  132. #else
  133. #warning no proper flock support for your site
  134. {
  135. errno = 0;
  136. return 0;
  137. }
  138. #endif
  139. #ifndef PHP_WIN32
  140. #if !(HAVE_INET_ATON)
  141. /* {{{ inet_aton
  142. * Check whether "cp" is a valid ascii representation
  143. * of an Internet address and convert to a binary address.
  144. * Returns 1 if the address is valid, 0 if not.
  145. * This replaces inet_addr, the return value from which
  146. * cannot distinguish between failure and a local broadcast address.
  147. */
  148. int inet_aton(const char *cp, struct in_addr *ap)
  149. {
  150. int dots = 0;
  151. unsigned long acc = 0, addr = 0;
  152. do {
  153. char cc = *cp;
  154. switch (cc) {
  155. case '0':
  156. case '1':
  157. case '2':
  158. case '3':
  159. case '4':
  160. case '5':
  161. case '6':
  162. case '7':
  163. case '8':
  164. case '9':
  165. acc = acc * 10 + (cc - '0');
  166. break;
  167. case '.':
  168. if (++dots > 3) {
  169. return 0;
  170. }
  171. /* Fall through */
  172. case '\0':
  173. if (acc > 255) {
  174. return 0;
  175. }
  176. addr = addr << 8 | acc;
  177. acc = 0;
  178. break;
  179. default:
  180. return 0;
  181. }
  182. } while (*cp++) ;
  183. /* Normalize the address */
  184. if (dots < 3) {
  185. addr <<= 8 * (3 - dots) ;
  186. }
  187. /* Store it if requested */
  188. if (ap) {
  189. ap->s_addr = htonl(addr);
  190. }
  191. return 1;
  192. }
  193. /* }}} */
  194. #endif /* !HAVE_INET_ATON */
  195. #endif