home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / gb / part01 / files_rw.c < prev    next >
C/C++ Source or Header  |  1989-07-06  |  3KB  |  130 lines

  1. /*
  2.  * Galactic Bloodshed (Robert Chansky, smq@b)
  3.  *  disk input/output routines
  4.  *    most read routines lock the data they just accessed (the file is not
  5.  *    closed).  write routines close and thus unlock that area.
  6.  *    Neither will work if the file LOCK_ALL exists, unless Ignore_lockfile
  7.  *    is true, or Ignore_file_locks.
  8.  *
  9.  *  Fileread(p, num, file, posn, routine); -- generic file read
  10.  *  Filewrite(p, num, file, posn, routine); -- generic file write
  11.  *
  12.  */
  13.  
  14. #include "tweakables.h"
  15. #include <sys/file.h>
  16. #include <signal.h>
  17. #include <errno.h>
  18. int sys_nerr;
  19. char *sys_errlist[];
  20. extern int errno;
  21.  
  22.  
  23. int Ignore_lockfile=0,Ignore_filelocks=0,Ignore_readerrors=0;
  24.  
  25. Fileread(fd, p, num, file, posn)
  26. int fd;
  27. char *p;
  28. int num;
  29. char *file;
  30. int posn;
  31. {
  32.  int lockt = NUM_TIMES_TO_WAIT_FOR_LOCK;
  33.  int lerr,n2;
  34.  
  35. /* printf(" fread fd=%d,reading %d bytes %s posn %d \n",fd,num,file,posn);*/
  36. if (!Ignore_lockfile) {        /* we are running for the shell filestuff */
  37.  
  38.  if (access(LOCK_ALL, F_OK)==0) {    /* if file exists */
  39.     printf("doing update. Please come back later.\n");
  40.     exit(0);
  41.  }
  42.  
  43.  
  44.  
  45. /*#ifdef UNIX_VERSION4.3
  46.  lseek(fd, posn, L_SET);
  47.  if (!Ignore_filelocks) {
  48.      while ((lerr=lockf(fd, F_TLOCK, (long)num))== -1 && errno==EACCES) {
  49.         close(fd);
  50.         if (!lockt--) {
  51.             printf("too long. exit.\n");
  52.             exit();
  53.         }
  54.         printf("waiting on %s lock...\n",file);
  55.         sleep(2);
  56.         fd = open(file,O_RDWR,0777);
  57.          lseek(fd, posn, L_SET);
  58.      }
  59.      if (lerr<0 && errno!=EACCES) {
  60.         printf("weird error.\n");
  61.         perror("Fileread 2");
  62.         exit();
  63.      }
  64.  }
  65. #else*/
  66.  if (!Ignore_filelocks) {
  67.      while ((lerr=flock(fd, LOCK_SH|LOCK_EX|LOCK_NB ))== -1 && errno==EWOULDBLOCK) {
  68.         if (!lockt--) {
  69.             printf("too long. exit.\n");
  70.             exit(-1);
  71.         }
  72.         printf("waiting on %s lock...\n",file);
  73.         sleep(2);
  74.      }
  75.      if (lerr<0 && errno!=EWOULDBLOCK) {
  76.         perror("Fileread 2");
  77.         exit(-1);
  78.      }
  79.   }
  80.  
  81.  }
  82.  
  83.  if (lseek(fd, posn, L_SET) < 0) {
  84.     perror("file read");
  85.     return;
  86.  }
  87.  if ((n2=read(fd,p,num))!=num && !Ignore_readerrors) {
  88.     printf("read: bad # of bytes read (%d != %d).\n",n2,num);
  89.     perror("fileread");
  90.     return;
  91.  }
  92.  
  93. }
  94.  
  95.  
  96. Filewrite(fd, p, num, file, posn)
  97. int fd;
  98. char *p;
  99. int num;
  100. char *file;
  101. int posn;
  102. {
  103. int mask;
  104. int lockfd,n2;
  105.  
  106.   if (!Ignore_lockfile) {
  107.    if (access(LOCK_ALL, F_OK)==0) {
  108.     printf("doing update. Please come back later.\n");
  109.     exit(-1);
  110.    }
  111.    mask = sigblock(SIGINT | SIGQUIT | SIGSTOP);
  112.   }
  113.  
  114.  /*printf(" fd =%d writing %d bytes to %s posn %d.\n",fd,num,file,posn);*/
  115.  
  116.  if (lseek(fd, posn, L_SET) < 0) {
  117.     perror(file);
  118.     return;
  119.  }
  120.  
  121.  if ((n2=write(fd,p,num))!=num && !Ignore_readerrors) {
  122.     printf("write: bad # of bytes written. (%d!=%d)\n",n2,num);
  123.     perror(file);
  124.     return;
  125.  }
  126.  
  127.  if (!Ignore_lockfile)
  128.    sigsetmask(mask);
  129. }
  130.