Main Page | File List | Globals

src/flock.c

Go to the documentation of this file.
00001 /*
00002      This file is part of PlibC.
00003      (C) 2005 Nils Durner (and other contributing authors)
00004 
00005            This library is free software; you can redistribute it and/or
00006            modify it under the terms of the GNU Lesser General Public
00007            License as published by the Free Software Foundation; either
00008            version 2.1 of the License, or (at your option) any later version.
00009         
00010            This library is distributed in the hope that it will be useful,
00011            but WITHOUT ANY WARRANTY; without even the implied warranty of
00012            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013            Lesser General Public License for more details.
00014         
00015            You should have received a copy of the GNU Lesser General Public
00016            License along with this library; if not, write to the Free Software
00017            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00025 #include "plibc_private.h"
00026 
00027 extern const char *errlist;
00028 
00032 int flock(int fd, int operation)
00033 {
00034   DWORD dwFlags;
00035   HANDLE hFile;
00036   OVERLAPPED theOvInfo;
00037   BOOL bRet;
00038 
00039   errno = 0;
00040 
00041   hFile = (HANDLE) _get_osfhandle(fd);
00042   memset(&theOvInfo, 0, sizeof(OVERLAPPED));
00043 
00044   /* Don't deadlock ourselves */
00045   if (IsWinNT())
00046     bRet = UnlockFileEx(hFile, 0, 1, 0, &theOvInfo);
00047   else
00048     bRet = UnlockFile(hFile, 0, 0, 1, 0);
00049 
00050   if (operation & LOCK_UN)
00051   {
00052     if (!bRet && ((dwFlags = GetLastError()) != ERROR_NOT_LOCKED))
00053     {
00054       SetErrnoFromWinError(dwFlags);
00055       return -1;
00056     }
00057     else
00058       return 0;
00059   }
00060 
00061   if (operation & LOCK_EX)
00062   {
00063     dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
00064   }
00065   else if (operation & LOCK_SH)
00066   {
00067     dwFlags = 0;
00068   }
00069   else
00070   {
00071     errno = EINVAL;
00072     return -1;
00073   }
00074 
00075   if (operation & LOCK_NB)
00076     dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
00077 
00078   if (IsWinNT())
00079     bRet = LockFileEx(hFile, dwFlags, 0, 1, 0, &theOvInfo);
00080   else
00081     bRet = LockFile(hFile, 0, 0, 1, 0);
00082 
00083   if (! bRet)
00084   {
00085     SetErrnoFromWinError(GetLastError());
00086     return -1;
00087   }
00088   else
00089     return 0;
00090 }
00091 
00092 /* end of flock.c */

Generated on Sun Sep 4 11:16:46 2005 for PlibC by  doxygen 1.4.2