flock_compat.c 6.9 KB

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