This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gfilport.cpp

217 lines
4.5 KiB
C++
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1990-1999 Odinn Sorensen
// Copyright (C) 1999-2000 Alexander S. Aganichev
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Ports of OS-specific file/path functions.
// ------------------------------------------------------------------
#include <gfilutil.h>
#if defined(__OS2__)
#define INCL_BASE
#define INCL_ERROR_H
#include <os2.h>
#endif
#if defined(__WIN32__)
#include <windows.h>
#endif
#if defined(__CYGWIN__)
#include <io.h>
#endif
#if defined(__MINGW32__)
#include <sys/locking.h>
#endif
#if defined(__BEOS__)
#include "be_lock.h"
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
// ------------------------------------------------------------------
2001-04-15 19:24:44 +00:00
#if (defined(__BORLANDC__) && defined(__OS2__)) || defined(__UNIX__) || defined(__EMX__)
2000-02-25 10:15:17 +00:00
off_t filelength(int handle) {
2000-02-25 10:15:17 +00:00
struct stat s;
if(fstat(handle, &s) == 0)
return s.st_size;
else
return -1;
}
#endif
// ------------------------------------------------------------------
#if defined(__OS2__)
int lock(int handle, long ofs, long length)
{
int rc;
APIRET apiret;
FILELOCK urange, lrange;
lrange.lOffset = ofs;
lrange.lRange = length;
urange.lRange = urange.lOffset = 0;
if((apiret = DosSetFileLocks((HFILE)handle, &urange, &lrange, 0, 0)) != 0) {
if(apiret == ERROR_LOCK_VIOLATION) {
errno = EACCES;
rc = -1;
}
else
rc = (int)apiret;
}
else
rc = 0;
return rc;
}
#endif
// ------------------------------------------------------------------
#if defined(__OS2__)
int unlock(int handle, long ofs, long length)
{
int rc;
FILELOCK urange, lrange;
APIRET apiret;
urange.lOffset = ofs;
urange.lRange = length;
lrange.lRange = lrange.lOffset = 0;
if((apiret = DosSetFileLocks((HFILE)handle, &urange, &lrange, 0, 0)) != 0) {
if(apiret == ERROR_LOCK_VIOLATION) {
errno = EACCES;
rc = -1;
}
else
rc = apiret;
}
else
rc = 0;
return rc;
}
#endif
// ------------------------------------------------------------------
#if defined(__UNIX__) || defined(__CYGWIN__)
2000-02-25 10:15:17 +00:00
int lock(int handle, long offset, long length) {
#ifndef __BEOS__
2000-02-25 10:15:17 +00:00
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = offset;
fl.l_len = length;
return fcntl(handle, F_SETLK, &fl);
#else //__BEOS__
return be_lock(handle);
#endif
2000-02-25 10:15:17 +00:00
}
#endif
// ------------------------------------------------------------------
2001-04-15 19:24:44 +00:00
#if defined(__UNIX__) || defined(__CYGWIN__)
2000-02-25 10:15:17 +00:00
int unlock(int handle, long offset, long length) {
#ifndef __BEOS__
2000-02-25 10:15:17 +00:00
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = offset;
fl.l_len = length;
return fcntl(handle, F_SETLK, &fl);
#else //__BEOS__
return be_unlock(handle);
#endif //__BEOS__
2000-02-25 10:15:17 +00:00
}
#endif
// ------------------------------------------------------------------
2001-04-15 19:24:44 +00:00
#if defined(__MINGW32__) || defined(_MSC_VER)
2000-02-25 10:15:17 +00:00
int lock(int fh, long offs, long len) {
if(LockFile((HANDLE)_get_osfhandle(fh), offs, 0L, len, 0L)) return 0;
else return -1;
}
#endif
// ------------------------------------------------------------------
2001-04-15 19:24:44 +00:00
#if defined(__MINGW32__) || defined(_MSC_VER)
2000-02-25 10:15:17 +00:00
int unlock(int fh, long offs, long len) {
if(UnlockFile((HANDLE)_get_osfhandle(fh), offs, 0L, len, 0L)) return 0;
else return -1;
}
#endif
// ------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
// ------------------------------------------------------------------