home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / WWIVSOR.ZIP / SHARE.C < prev    next >
Text File  |  1995-04-25  |  7KB  |  286 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1995 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <conio.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <process.h>
  22. #include <sys\stat.h>
  23. #include <dos.h>
  24. #include <dir.h>
  25. #include <alloc.h>
  26. #include <share.h>
  27. #include <errno.h>
  28.  
  29. #include "share.h"
  30.  
  31.  
  32. #ifdef __OS2__
  33. #define INCL_DOSPROCESS
  34. #include <os2.h>
  35. #define delay(ms) DosSleep((ULONG) ms)
  36. #endif /* __OS2__ */
  37.  
  38.  
  39.  
  40. #define SHARE_LEVEL 10
  41. #define WAIT_TIME 10
  42. #define TRIES 100
  43.  
  44. void sysoplog(char *s);                          /* warn */
  45. void giveup_timeslice(void);                     /* warn */
  46. extern int incom;
  47.  
  48. /* debug levels:
  49.  
  50.    0 turns all debug operations off
  51.  
  52.    1 or greater shows file information if the file must be waited upon.
  53.  
  54.    2 or greater shows file information when an attempt is made to open a file.
  55.  
  56.    3 or greater shows file information BEFORE any attempt is made to open
  57.      a file.
  58.  
  59.    4 or greater waits for key from console with each file open.
  60.  
  61. */
  62.  
  63. int sh_open(char *path, int file_access, unsigned mode)
  64. {
  65.   int handle, count, share;
  66.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  67.  
  68.   if (debuglevel>2)
  69.     printf("\rsh_open %s, access=%u.\r\n",
  70.       path,file_access);
  71.   if ((file_access & O_RDWR) || (file_access & O_WRONLY)  || (mode & S_IWRITE)) {
  72.     share=SH_DENYRW;
  73.   } else {
  74.     share=SH_DENYWR;
  75.   }
  76.   handle=open(path,file_access|share,mode);
  77.   if (handle < 0) {
  78.     count=1;
  79.     fnsplit(path,drive,dir,file,ext);
  80.     if (access(path,0)!=-1) {
  81.       delay(WAIT_TIME);
  82.       handle=open(path,file_access|share,mode);
  83.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  84.         if (count%2)
  85.           delay(WAIT_TIME);
  86.         else
  87.           giveup_timeslice();
  88.         if (debuglevel)
  89.           printf("\rWaiting to access %s%s %d.  \r",
  90.             file,ext,TRIES-count);
  91.         count++;
  92.         handle=open(path,file_access|share,mode);
  93.       }
  94.       if ((handle < 0) && (debuglevel))
  95.         printf("\rThe file %s%s is busy.  Try again later.\r\n",file,ext);
  96.     }
  97.   }
  98.   if (debuglevel>1)
  99.     printf("\rsh_open %s, access=%u, handle=%d.\r\n",
  100.       path,file_access,handle);
  101.   if ((debuglevel > 3) && (!incom))
  102.     getche();
  103.   return(handle);
  104. }
  105.  
  106.  
  107. int sh_open1(char *path, int access)
  108. {
  109.   unsigned mode;
  110.  
  111.   mode=0;
  112.   if ((access & O_RDWR) || (access & O_WRONLY))
  113.     mode |=S_IWRITE;
  114.   if ((access & O_RDWR) || (access & O_RDONLY))
  115.     mode |=S_IREAD;
  116.   return(sh_open(path, access, mode));
  117. }
  118.  
  119.  
  120. FILE *fsh_open(char *path, char *mode)
  121. {
  122.   FILE *f;
  123.   int count,share, md, fd;
  124.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  125.  
  126.   if (debuglevel>2)
  127.     printf("\rfsh_open %s, access=%s.\r\n",
  128.       path,mode);
  129.   share=SH_DENYWR;
  130.   md=0;
  131.   if (strchr(mode,'w')!=NULL) {
  132.     share=SH_DENYRD;
  133.     md= O_RDWR|O_CREAT|O_TRUNC;
  134.   } else if (strchr(mode,'a')!=NULL) {
  135.     share=SH_DENYRD;
  136.     md= O_RDWR|O_CREAT;
  137.   } else {
  138.     md= O_RDONLY;
  139.   }
  140.   if (strchr(mode,'b')!=NULL) {
  141.     md |= O_BINARY;
  142.   }
  143.   if (strchr(mode,'+')!=NULL) {
  144.     md&= ~O_RDONLY;
  145.     md|= O_RDWR;
  146.     share=SH_DENYRD;
  147.   }
  148.   fd=open(path, md|share, S_IREAD|S_IWRITE);
  149.   if (fd<0) {
  150.     count=1;
  151.     fnsplit(path,drive,dir,file,ext);
  152.     if (access(path,0)!=-1) {
  153.       delay(WAIT_TIME);
  154.       fd=open(path, md|share, S_IREAD|S_IWRITE);
  155.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  156.         delay(WAIT_TIME);
  157.         if (debuglevel)
  158.           printf("\rWaiting to access %s%s %d.  \r",
  159.             file,ext,TRIES-count);
  160.           count++;
  161.         fd=open(path, md|share, S_IREAD|S_IWRITE);
  162.       }
  163.       if ((f == NULL) && (debuglevel))
  164.         printf("\rThe file %s%s is busy.  Try again later.\r\n",file,ext);
  165.     }
  166.   }
  167.   if (fd>0) {
  168.     if (strchr(mode,'a'))
  169.       lseek(fd, 0L, SEEK_END);
  170.  
  171.     f=fdopen(fd,mode);
  172.     if (!f) {
  173.       close(fd);
  174.     }
  175.   } else
  176.     f=0;
  177.   if (debuglevel>1)
  178.     printf("\rfsh_open %s, access=%s.\r\n",
  179.       path,mode);
  180.   if ((debuglevel > 3) && (!incom))
  181.     getche();
  182.   return(f);
  183. }
  184.  
  185.  
  186. int sh_close(int f)
  187. {
  188.   if (f!=-1)
  189.     close(f);
  190.   return(-1);
  191. }
  192.  
  193.  
  194. void fsh_close(FILE *f)
  195. {
  196.   fclose(f);
  197. }
  198.  
  199.  
  200. void share_installed(void)
  201. /* detects if SHARE is installed - only in DOS versions 4 or later */
  202. /* returns FALSE if DOS version less than 4 or not installed */
  203. {
  204. #ifndef __OS2__
  205.   if (_osmajor >= 3) {
  206.     _AX=0x1000;
  207.     geninterrupt(0x2f);
  208.   } else {
  209.     printf("\r\nIncorrect DOS version.\r\n");
  210.     exit(SHARE_LEVEL);
  211.   }
  212.   if (_AL == 0xFF) {
  213.     if (debuglevel)
  214.       printf("\rShare is loaded.\r");
  215.     if (debuglevel > 3)
  216.       getche();
  217.     return;
  218.   }
  219.   if (_AL == 0x01) {
  220.     printf("\r\nShare can not be loaded.\r\n");
  221.     exit(SHARE_LEVEL);
  222.   }
  223.   if (_AL == 0x00) {
  224.     printf("\r\nShare should be INSTALLED in your config.sys.\r\n");
  225.     exit(SHARE_LEVEL);
  226.   }
  227.   printf("\r\nUnexpected result from SHARE TEST %d.\r\n",_AL);
  228.   exit(SHARE_LEVEL);
  229. #endif
  230. }
  231.  
  232. long sh_lseek(int handle, long offset, int fromwhere)
  233. {
  234.  
  235.   if (handle == -1) {
  236.     sysoplog("\r\n6Attempted to seek in closed file.\r\n");
  237.     return(-1L);
  238.   }
  239.   return(lseek(handle, offset, fromwhere));
  240. }
  241.  
  242.  
  243. int sh_read(int handle, void *buf, unsigned len)
  244. {
  245.  
  246.   if (handle == -1) {
  247.     sysoplog("\r\n6Attempted to read from closed file.\r\n");
  248.     return(-1);
  249.   }
  250.   return(read(handle, buf, len));
  251. }
  252.  
  253.  
  254. int sh_write(int handle, void *buf, unsigned len)
  255. {
  256.  
  257.   if (handle == -1) {
  258.     sysoplog("\r\n6Attempted to write to closed file.\r\n");
  259.     return(-1);
  260.   }
  261.   return(write(handle, buf, len));
  262. }
  263.  
  264.  
  265. size_t fsh_read(void *ptr, size_t size, size_t n, FILE *stream)
  266. {
  267.  
  268.   if (stream == NULL) {
  269.     sysoplog("\r\n6Attempted to fread from closed file.\r\n");
  270.     return(0);
  271.   }
  272.   return(fread(ptr, size, n, stream));
  273. }
  274.  
  275.  
  276. size_t fsh_write(void *ptr, size_t size, size_t n, FILE *stream)
  277. {
  278.  
  279.   if (stream == NULL) {
  280.     sysoplog("\r\n6Attempted to fwrite to closed file.\r\n");
  281.     return(0);
  282.   }
  283.   return(fwrite(ptr, size, n, stream));
  284. }
  285.  
  286.