flock_compat.c 6.8 KB

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