The African Chief wrote:
Pismo wrote:
[...]
If I could lock a file with GPC (like flock in Perl) then I could create my own record locking implementation. Without being able to lock a file at the OS level trying to accomplish this would be flakey at best. I want to use GPC for multi-user database apps I'm porting from BP/Delphi.
If there is a C function for that you can use that:
extern void foobar ( int foo, char *bar );
becomes
Procedure Foobar ( foo: Integer; bar: CString ); C;
but you should better ask again at the GPC mailing list, gpc@hut.fi. I have no experience with Perl, sorry.
I don't use Perl or C, but I'm sure we must have a C programmer on the list who can answer this for us.
File and record locking is a service that should be provided by the operating system, not by a compiler. I believe that any modern operating system provides this facility - but this means that you might have to access the service in a different way under each operating system that you want to support (or write a single function to access the service, with zillions of #IFDEFs to cater for different operating systems).
For Linux:
man flock says:
int flock (int fd, int operation)
So, in GPC this is
function flock (fd, operation: integer): integer; C;
Operation is one of LOCK_SH, LOCK_EX, LOCK_UN, possibly OR'ed with LOCK_NB. Unfortunately, these "constants" are macros in C, declared in <sys/file.h>, and there's not yet a way to automatically translate them into Pascal, so you have to translate them manually, hoping that they're the same on all OS versions...
fd is the file descriptor. In recent GPC betas, you can get the "FILE*" of a file (a pointer) via GetFile, but you need a way to get the file descriptor (an integer). I didn't find a C function that does this, but if there's really none, you could write one yourself. An example:
file fileno.c:
#include <stdio.h> int getfileno(FILE* foo) {return foo->_fileno;}
file x.p:
program x;
const { translated from <sys/file.h> } LOCK_SH = 1; { Shared lock. } LOCK_EX = 2; { Exclusive lock. } LOCK_UN = 8; { Unlock. }
{ Can be OR'd in to one of the above. } LOCK_NB = 4; { Don't block when locking. }
{$L fileno.c} function getfileno (f: pointer): integer; C; function flock (fd, operation: integer): integer; C;
var a,b:text; begin reset(a,'x.p'); reset(b,'x.p'); writeln(flock(getfileno(getfile(a)),lock_ex)); writeln(flock(getfileno(getfile(b)),lock_ex OR lock_nb)); close(a); close(b) end.
On my system, it outputs 0 and -1, which means that the first lock succeeded, the second didn't, as it ought to be.
BTW how would I check the system clock?
Look at the DOS and DOSH units in the BPCOMPAT package.
Not really. He was asking for Linux, and the Dos unit is not portable, AFAIK.
But there's no need to use any units, because Extended(?) Pascal has a built-in way to access the time:
program x; var time:TimeStamp; begin GetTimeStamp(time); writeln(time.day,'.',time.month,'.',time.year,' ', time.hour,':',time.minute,':',time.second) end.
Frank Heckenbach wrote: [...]
But there's no need to use any units, because Extended(?) Pascal has a built-in way to access the time:
program x; var time:TimeStamp; begin GetTimeStamp(time); writeln(time.day,'.',time.month,'.',time.year,' ', time.hour,':',time.minute,':',time.second) end.
The output from this is unintelligible. Is there a way to convert the output into something useful?
Best regards, The Chief -------- Dr. Abimbola A. Olowofoyeku (The African Chief) Email: laa12@keele.ac.uk Homepage: http://ourworld.compuserve.com/homepages/African_Chief/ Author of: Chief's Installer Pro 4.01 for Win16 and Win32: http://www.simtel.net/pub/simtelnet/win3/install/chief401.zip