Back to index

php5  5.3.10
flock_compat.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Sascha Schumann <sascha@schumann.cx>                         |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: flock_compat.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 #include <errno.h>
00023 #include "ext/standard/flock_compat.h"
00024 
00025 #if HAVE_STRUCT_FLOCK
00026 #include <unistd.h>
00027 #include <fcntl.h>
00028 #include <sys/file.h>
00029 #endif
00030 
00031 #ifdef PHP_WIN32
00032 #include <io.h>
00033 #include "config.w32.h"
00034 #endif
00035 
00036 #ifdef NETWARE
00037 #include <netinet/in.h>
00038 #endif
00039 
00040 #ifndef HAVE_FLOCK
00041 PHPAPI int flock(int fd, int operation)
00042 {
00043        return php_flock(fd, operation);
00044 }
00045 #endif /* !defined(HAVE_FLOCK) */
00046 
00047 PHPAPI int php_flock(int fd, int operation)
00048 #if HAVE_STRUCT_FLOCK /* {{{ */
00049 {
00050        struct flock flck;
00051        int ret;
00052 
00053        flck.l_start = flck.l_len = 0;
00054        flck.l_whence = SEEK_SET;
00055        
00056        if (operation & LOCK_SH)
00057               flck.l_type = F_RDLCK;
00058        else if (operation & LOCK_EX)
00059               flck.l_type = F_WRLCK;
00060        else if (operation & LOCK_UN)
00061               flck.l_type = F_UNLCK;
00062        else {
00063               errno = EINVAL;
00064               return -1;
00065        }
00066 
00067        ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
00068 
00069        if (operation & LOCK_NB && ret == -1 && 
00070                      (errno == EACCES || errno == EAGAIN))
00071               errno = EWOULDBLOCK;
00072 
00073        if (ret != -1) ret = 0;
00074 
00075        return ret;
00076 }
00077 /* }}} */
00078 #elif defined(PHP_WIN32) /* {{{ */
00079 /*
00080  * Program:   Unix compatibility routines
00081  *
00082  * Author:  Mark Crispin
00083  *      Networks and Distributed Computing
00084  *      Computing & Communications
00085  *      University of Washington
00086  *      Administration Building, AG-44
00087  *      Seattle, WA  98195
00088  *      Internet: MRC@CAC.Washington.EDU
00089  *
00090  * Date:    14 September 1996
00091  * Last Edited: 14 August 1997
00092  *
00093  * Copyright 1997 by the University of Washington
00094  *
00095  *  Permission to use, copy, modify, and distribute this software and its
00096  * documentation for any purpose and without fee is hereby granted, provided
00097  * that the above copyright notice appears in all copies and that both the
00098  * above copyright notice and this permission notice appear in supporting
00099  * documentation, and that the name of the University of Washington not be
00100  * used in advertising or publicity pertaining to distribution of the software
00101  * without specific, written prior permission.  This software is made available
00102  * "as is", and
00103  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
00104  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
00105  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
00106  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00107  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
00108  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
00109  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00110  *
00111  */
00112 /*              DEDICATION
00113 
00114  *  This file is dedicated to my dog, Unix, also known as Yun-chan and
00115  * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
00116  * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
00117  * a two-month bout with cirrhosis of the liver.
00118  *
00119  *  He was a dear friend, and I miss him terribly.
00120  *
00121  *  Lift a leg, Yunie.  Luv ya forever!!!!
00122  */
00123 {
00124     HANDLE hdl = (HANDLE) _get_osfhandle(fd);
00125     DWORD low = 1, high = 0;
00126     OVERLAPPED offset =
00127     {0, 0, 0, 0, NULL};
00128     if (hdl < 0)
00129         return -1;              /* error in file descriptor */
00130     /* bug for bug compatible with Unix */
00131     UnlockFileEx(hdl, 0, low, high, &offset);
00132     switch (operation & ~LOCK_NB) {    /* translate to LockFileEx() op */
00133         case LOCK_EX:           /* exclusive */
00134             if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
00135                         ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
00136                            0, low, high, &offset))
00137                 return 0;
00138             break;
00139         case LOCK_SH:           /* shared */
00140             if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
00141                            0, low, high, &offset))
00142                 return 0;
00143             break;
00144         case LOCK_UN:           /* unlock */
00145             return 0;           /* always succeeds */
00146         default:                /* default */
00147             break;
00148     }
00149        /* Under Win32 MT library, errno is not a variable but a function call,
00150         * which cannot be assigned to.
00151         */
00152 #if !defined(PHP_WIN32)
00153     errno = EINVAL;             /* bad call */
00154 #endif
00155     return -1;
00156 }
00157 /* }}} */
00158 #else
00159 #warning no proper flock support for your site
00160 {
00161        errno = 0;
00162        return 0;
00163 }
00164 #endif
00165 
00166 #ifndef PHP_WIN32
00167 #if !(HAVE_INET_ATON)
00168 /* {{{ inet_aton
00169  * Check whether "cp" is a valid ascii representation
00170  * of an Internet address and convert to a binary address.
00171  * Returns 1 if the address is valid, 0 if not.
00172  * This replaces inet_addr, the return value from which
00173  * cannot distinguish between failure and a local broadcast address.
00174  */
00175 int inet_aton(const char *cp, struct in_addr *ap)
00176 {
00177     int dots = 0;
00178     register unsigned long acc = 0, addr = 0;
00179 
00180     do {
00181         register char cc = *cp;
00182 
00183         switch (cc) {
00184         case '0':
00185         case '1':
00186         case '2':
00187         case '3':
00188         case '4':
00189         case '5':
00190         case '6':
00191         case '7':
00192         case '8':
00193         case '9':
00194             acc = acc * 10 + (cc - '0');
00195             break;
00196 
00197         case '.':
00198             if (++dots > 3) {
00199                 return 0;
00200             }
00201             /* Fall through */
00202 
00203         case '\0':
00204             if (acc > 255) {
00205                 return 0;
00206             }
00207             addr = addr << 8 | acc;
00208             acc = 0;
00209             break;
00210 
00211         default:
00212             return 0;
00213         }
00214     } while (*cp++) ;
00215 
00216     /* Normalize the address */
00217     if (dots < 3) {
00218         addr <<= 8 * (3 - dots) ;
00219     }
00220 
00221     /* Store it if requested */
00222     if (ap) {
00223         ap->s_addr = htonl(addr);
00224     }
00225 
00226     return 1;    
00227 }
00228 /* }}} */
00229 #endif /* !HAVE_INET_ATON */
00230 #endif
00231 
00232 /*
00233  * Local variables:
00234  * tab-width: 4
00235  * c-basic-offset: 4
00236  * End:
00237  * vim600: sw=4 ts=4 fdm=marker
00238  * vim<600: sw=4 ts=4
00239  */