diff options
-rw-r--r-- | README.amiga | 3 | ||||
-rw-r--r-- | amigaos4/amigaos.c | 86 | ||||
-rw-r--r-- | amigaos4/amigaos.h | 11 |
3 files changed, 100 insertions, 0 deletions
diff --git a/README.amiga b/README.amiga index 0e6358d225..934b037a49 100644 --- a/README.amiga +++ b/README.amiga @@ -178,6 +178,9 @@ This will build the default setup that installs under SDK:local/newlib/lib/ =item Fix issue with newlib's unlink, which could cause infinite loops. +=item Add flock() emulation using IDOS->LockRecord thanks to Tony Cook +for the suggestion. + =back =item B<27th November 2013> diff --git a/amigaos4/amigaos.c b/amigaos4/amigaos.c index 8e26064f08..f79943079f 100644 --- a/amigaos4/amigaos.c +++ b/amigaos4/amigaos.c @@ -1078,3 +1078,89 @@ BPTR amigaos_get_file(int fd) } return fh; } + +/*########################################################################*/ + +#define LOCK_START 0xFFFFFFFFFFFFFFFELL +#define LOCK_LENGTH 1LL + +// No wait forever option so lets wait for a loooong time. +#define TIMEOUT 0x7FFFFFFF + +int amigaos_flock(int fd, int oper) +{ + BPTR fh; + int32 success = -1; + + if (!(fh = amigaos_get_file(fd))) + { + errno = EBADF; + return -1; + } + + switch (oper) + { + case LOCK_SH: + { + if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH, + REC_SHARED | RECF_DOS_METHOD_ONLY, + TIMEOUT)) + { + success = 0; + } + break; + } + case LOCK_EX: + { + if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH, + REC_EXCLUSIVE | RECF_DOS_METHOD_ONLY, + TIMEOUT)) + { + success = 0; + } + break; + } + case LOCK_SH | LOCK_NB: + { + if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH, + REC_SHARED_IMMED | RECF_DOS_METHOD_ONLY, + TIMEOUT)) + { + success = 0; + } + else + { + errno = EWOULDBLOCK; + } + break; + } + case LOCK_EX | LOCK_NB: + { + if (IDOS->LockRecord(fh, LOCK_START, LOCK_LENGTH, + REC_EXCLUSIVE_IMMED | RECF_DOS_METHOD_ONLY, + TIMEOUT)) + { + success = 0; + } + else + { + errno = EWOULDBLOCK; + } + break; + } + case LOCK_UN: + { + if (IDOS->UnLockRecord(fh, LOCK_START, LOCK_LENGTH)) + { + success = 0; + } + break; + } + default: + { + errno = EINVAL; + return -1; + } + } + return success; +} diff --git a/amigaos4/amigaos.h b/amigaos4/amigaos.h index 96f521d59b..2f6d4b2d49 100644 --- a/amigaos4/amigaos.h +++ b/amigaos4/amigaos.h @@ -52,4 +52,15 @@ long amigaos_get_file(int fd); // BOOL constructed; +/* emulated flock stuff */ + +#define LOCK_SH 1 /* Shared lock. */ +#define LOCK_EX 2 /* Exclusive lock. */ +#define LOCK_UN 8 /* Unlock. */ +#define LOCK_NB 4 /* Don't block when locking. */ + +extern int flock(int fd, int operation); + +#define flock(a, b) amigaos_flock((a), (b)) + #endif |