home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume6
/
gb
/
part01
/
files_rw.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-07-06
|
3KB
|
130 lines
/*
* Galactic Bloodshed (Robert Chansky, smq@b)
* disk input/output routines
* most read routines lock the data they just accessed (the file is not
* closed). write routines close and thus unlock that area.
* Neither will work if the file LOCK_ALL exists, unless Ignore_lockfile
* is true, or Ignore_file_locks.
*
* Fileread(p, num, file, posn, routine); -- generic file read
* Filewrite(p, num, file, posn, routine); -- generic file write
*
*/
#include "tweakables.h"
#include <sys/file.h>
#include <signal.h>
#include <errno.h>
int sys_nerr;
char *sys_errlist[];
extern int errno;
int Ignore_lockfile=0,Ignore_filelocks=0,Ignore_readerrors=0;
Fileread(fd, p, num, file, posn)
int fd;
char *p;
int num;
char *file;
int posn;
{
int lockt = NUM_TIMES_TO_WAIT_FOR_LOCK;
int lerr,n2;
/* printf(" fread fd=%d,reading %d bytes %s posn %d \n",fd,num,file,posn);*/
if (!Ignore_lockfile) { /* we are running for the shell filestuff */
if (access(LOCK_ALL, F_OK)==0) { /* if file exists */
printf("doing update. Please come back later.\n");
exit(0);
}
/*#ifdef UNIX_VERSION4.3
lseek(fd, posn, L_SET);
if (!Ignore_filelocks) {
while ((lerr=lockf(fd, F_TLOCK, (long)num))== -1 && errno==EACCES) {
close(fd);
if (!lockt--) {
printf("too long. exit.\n");
exit();
}
printf("waiting on %s lock...\n",file);
sleep(2);
fd = open(file,O_RDWR,0777);
lseek(fd, posn, L_SET);
}
if (lerr<0 && errno!=EACCES) {
printf("weird error.\n");
perror("Fileread 2");
exit();
}
}
#else*/
if (!Ignore_filelocks) {
while ((lerr=flock(fd, LOCK_SH|LOCK_EX|LOCK_NB ))== -1 && errno==EWOULDBLOCK) {
if (!lockt--) {
printf("too long. exit.\n");
exit(-1);
}
printf("waiting on %s lock...\n",file);
sleep(2);
}
if (lerr<0 && errno!=EWOULDBLOCK) {
perror("Fileread 2");
exit(-1);
}
}
}
if (lseek(fd, posn, L_SET) < 0) {
perror("file read");
return;
}
if ((n2=read(fd,p,num))!=num && !Ignore_readerrors) {
printf("read: bad # of bytes read (%d != %d).\n",n2,num);
perror("fileread");
return;
}
}
Filewrite(fd, p, num, file, posn)
int fd;
char *p;
int num;
char *file;
int posn;
{
int mask;
int lockfd,n2;
if (!Ignore_lockfile) {
if (access(LOCK_ALL, F_OK)==0) {
printf("doing update. Please come back later.\n");
exit(-1);
}
mask = sigblock(SIGINT | SIGQUIT | SIGSTOP);
}
/*printf(" fd =%d writing %d bytes to %s posn %d.\n",fd,num,file,posn);*/
if (lseek(fd, posn, L_SET) < 0) {
perror(file);
return;
}
if ((n2=write(fd,p,num))!=num && !Ignore_readerrors) {
printf("write: bad # of bytes written. (%d!=%d)\n",n2,num);
perror(file);
return;
}
if (!Ignore_lockfile)
sigsetmask(mask);
}