Back to index

php5  5.3.10
flock.c
Go to the documentation of this file.
00001 /*
00002  * Program:   Unix compatibility routines
00003  *
00004  * Author:  Mark Crispin
00005  *      Networks and Distributed Computing
00006  *      Computing & Communications
00007  *      University of Washington
00008  *      Administration Building, AG-44
00009  *      Seattle, WA  98195
00010  *      Internet: MRC@CAC.Washington.EDU
00011  *
00012  * Date:    14 September 1996
00013  * Last Edited: 14 August 1997
00014  *
00015  * Copyright 1997 by the University of Washington
00016  *
00017  *  Permission to use, copy, modify, and distribute this software and its
00018  * documentation for any purpose and without fee is hereby granted, provided
00019  * that the above copyright notice appears in all copies and that both the
00020  * above copyright notice and this permission notice appear in supporting
00021  * documentation, and that the name of the University of Washington not be
00022  * used in advertising or publicity pertaining to distribution of the software
00023  * without specific, written prior permission.  This software is made available
00024  * "as is", and
00025  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
00026  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
00028  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
00029  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00030  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
00031  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
00032  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00033  *
00034  */
00035 
00036 
00037 /*              DEDICATION
00038 
00039  *  This file is dedicated to my dog, Unix, also known as Yun-chan and
00040  * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
00041  * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
00042  * a two-month bout with cirrhosis of the liver.
00043  *
00044  *  He was a dear friend, and I miss him terribly.
00045  *
00046  *  Lift a leg, Yunie.  Luv ya forever!!!!
00047  */
00048 
00049 #include "php.h"
00050 #include <windows.h>
00051 #include <io.h>
00052 #include <errno.h>
00053 #include "flock.h"
00054 
00055 PHPAPI int flock(int fd, int op)
00056 {
00057        HANDLE hdl = (HANDLE) _get_osfhandle(fd);
00058        DWORD low = 1, high = 0;
00059        OVERLAPPED offset =
00060        {0, 0, 0, 0, NULL};
00061        if (hdl < 0)
00062               return -1;                         /* error in file descriptor */
00063        /* bug for bug compatible with Unix */
00064        UnlockFileEx(hdl, 0, low, high, &offset);
00065        switch (op & ~LOCK_NB) {    /* translate to LockFileEx() op */
00066               case LOCK_EX:               /* exclusive */
00067                      if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
00068                                           ((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
00069                                              0, low, high, &offset))
00070                             return 0;
00071                      break;
00072               case LOCK_SH:               /* shared */
00073                      if (LockFileEx(hdl, ((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
00074                                              0, low, high, &offset))
00075                             return 0;
00076                      break;
00077               case LOCK_UN:               /* unlock */
00078                      return 0;                   /* always succeeds */
00079               default:                           /* default */
00080                      break;
00081        }
00082        errno = EINVAL;                           /* bad call */
00083        return -1;
00084 }