home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / HSRC_100 / DOS_FILE.C < prev    next >
C/C++ Source or Header  |  1993-01-11  |  8KB  |  302 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                              Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54. #ifdef __MSDOS__
  55. #include <fcntl.h>
  56. #include <share.h>
  57. #include <io.h>
  58. #endif
  59.  
  60.  
  61. static boolean dos_sharing = false;
  62.  
  63.  
  64. void dos_sharecheck (void)    /* SHARE installed? set dos_sharing true/false */
  65. {
  66. #if __MSDOS__
  67.         union REGS regs;
  68.  
  69.         regs.x.ax = 0x1000;                             /* DOS Multiplexer   */
  70.         int86(0x2f,®s,®s);                        /* INT 2Fh sub 1000h */
  71.         dos_sharing = (regs.h.al == 0xff) ? true : false;
  72. #endif
  73. }/*dos_sharecheck()*/
  74.  
  75.  
  76. int dos_open (char *pathname, byte create)
  77. {
  78.         register int access;
  79.  
  80. #ifdef __MSDOS__
  81.         access = O_RDWR | O_BINARY;
  82.         if (create) {
  83.            access |= O_CREAT;
  84.            if (create == 2)
  85.               access |= O_TRUNC;
  86.         }
  87.  
  88.         return (open(pathname, access,
  89.                      create ? S_IREAD | S_IWRITE : 0));
  90. #endif
  91. #ifdef __TOS__
  92.         access = O_RDWR;
  93.         if (create) {
  94.            access |= O_CREAT;
  95.            if (create == 2)
  96.               access |= O_TRUNC;
  97.         }
  98.  
  99.         return (open(pathname, access, 0));
  100. #endif
  101. }/*dos_open()*/
  102.  
  103.  
  104. int dos_sopen (char *pathname, byte create)
  105. {
  106.         register int access;
  107.  
  108. #ifdef __MSDOS__
  109.         access = O_RDWR | O_BINARY;
  110.         if (create) {
  111.            access |= O_CREAT;
  112.            if (create == 2)
  113.               access |= O_TRUNC;
  114.         }
  115.  
  116.         return (sopen(pathname, access,
  117.                    dos_sharing ? SH_DENYWR : 0,
  118.                    create ? S_IREAD | S_IWRITE : 0));
  119. #endif
  120. #ifdef __TOS__
  121.         access = O_RDWR;
  122.         if (create) {
  123.            access |= O_CREAT;
  124.            if (create == 2)
  125.               access |= O_TRUNC;
  126.         }
  127.  
  128.         return (open(pathname, access, 0));
  129. #endif
  130. }/*dos_sopen()*/
  131.  
  132.  
  133. int dos_sappend (char *pathname, byte create)
  134. {
  135.         register int access;
  136.  
  137. #ifdef __MSDOS__
  138.         access = O_WRONLY | O_APPEND | O_TEXT;
  139.         if (create) {
  140.            access |= O_CREAT;
  141.            if (create == 2)
  142.               access |= O_TRUNC;
  143.         }
  144.  
  145.         return (sopen(pathname, access,
  146.                       dos_sharing ? SH_DENYWR : 0,
  147.                       create ? S_IREAD | S_IWRITE : 0));
  148. #endif
  149. #ifdef __TOS__
  150.         access = O_WRONLY | O_APPEND;
  151.         if (create) {
  152.            access |= O_CREAT;
  153.            if (create == 2)
  154.               access |= O_TRUNC;
  155.         }
  156.  
  157.         return (open(pathname, access, 0));
  158. #endif
  159. }/*dos_sappend()*/
  160.  
  161.  
  162. int dos_close (int handle)
  163. {
  164.         return (close(handle));
  165. }/*dos_close()*/
  166.  
  167.  
  168. int dos_lock (int handle, long offset, long len)
  169. {
  170. #ifdef __MSDOS__
  171.         if (dos_sharing)
  172.            while (!lock(handle,offset,len));
  173. #endif
  174.  
  175.         return (0);
  176. }/*dos_lock()*/
  177.  
  178.  
  179. int dos_unlock (int handle, long offset, long len)
  180. {
  181. #ifdef __MSDOS__
  182.         return (dos_sharing ? unlock(handle,offset,len) : 0);
  183. #endif
  184. #ifdef __TOS__
  185.         return 0;
  186. #endif
  187. }/*dos_unlock()*/
  188.  
  189.  
  190. long dos_seek (int handle, long offset, int fromwhere)
  191. {
  192.         return (lseek(handle,offset,fromwhere));
  193. }/*dos_seek()*/
  194.  
  195.  
  196. long dos_tell (int handle)
  197. {
  198. #ifdef __MSDOS__
  199.         return (tell(handle));
  200. #endif
  201. #ifdef __TOS__
  202.         return (lseek(handle,0L,SEEK_CUR));
  203. #endif
  204. }/*dos_tell()*/
  205.  
  206.  
  207. int dos_read (int handle, void *buf, word len)
  208. {
  209. #ifdef __MSDOS__
  210.         return (_read(handle,buf,len));
  211. #endif
  212. #ifdef __TOS__
  213.         return ((int) read(handle,buf,len));
  214. #endif
  215. }/*dos_read()*/
  216.  
  217.  
  218. int dos_write (int handle, void *buf, word len)
  219. {
  220. #ifdef __MSDOS__
  221.         return (_write(handle,buf,len));
  222. #endif
  223. #ifdef __TOS__
  224.         return ((int) write(handle,buf,len));
  225. #endif
  226. }/*dos_write()*/
  227.  
  228.  
  229. /* ----------------------------------------------------------------------------
  230.                         oflags                  mode
  231.         r       O_RDONLY                        don't care
  232.         w       O_CREAT | O_WRONLY | O_TRUNC    S_IWRITE
  233.         a       O_CREAT | O_WRONLY | O_APPEND   S_IWRITE
  234.         r+      O_RDWR                          don't care
  235.         w+      O_RDWR | O_CREAT | O_TRUNC      S_IWRITE | S_IREAD
  236.         a+      O_RDWR | O_CREAT | O_APPEND     S_IWRITE | S_IREAD
  237. ---------------------------------------------------------------------------- */
  238. FILE *sfopen (char *name, char *mode, int shareflag)
  239. {
  240. #ifdef __MSDOS__
  241.         int   fd, access, flags;
  242.         char  *type = mode, c;
  243.         FILE *fp;
  244.  
  245.         if ((c = *type++) == 'r') {
  246.            access = O_RDONLY;
  247.            flags = 0;
  248.         }
  249.         else if (c == 'w') {
  250.            access = O_WRONLY | O_CREAT | O_TRUNC;
  251.            flags = S_IWRITE;
  252.         }
  253.         else if (c == 'a') {
  254.            access = O_RDWR   | O_CREAT | O_APPEND;
  255.            flags = S_IWRITE;
  256.         }
  257.         else
  258.            return (NULL);
  259.  
  260.         c = *type++;
  261.  
  262.         if (c == '+' || (*type == '+' && (c == 't' || c == 'b'))) {
  263.            if (c == '+') c = *type;
  264.            access = (access & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  265.            flags = S_IREAD | S_IWRITE;
  266.         }
  267.  
  268.         if      (c == 't') access |= O_TEXT;
  269.         else if (c == 'b') access |= O_BINARY;
  270.         else               access |= (_fmode & (O_TEXT | O_BINARY));
  271.  
  272.         if (dos_sharing)
  273.            access |= shareflag;
  274.  
  275.         fd = open(name, access, flags);
  276.  
  277.         if (fd < 0) return (NULL);
  278.  
  279.         if ((fp = fdopen(fd,mode)) == NULL)
  280.            close(fd);
  281.         else if (setvbuf(fp,NULL,_IOFBF,BUFSIZ)) {
  282.            fclose(fp);
  283.            return (NULL);
  284.         }
  285.  
  286.         return (fp);
  287. #endif
  288.  
  289. #ifdef __TOS__
  290.         char *p, *q, nm[10];
  291.  
  292.         strcpy(nm,mode);
  293.         for(p=q=nm; *p; p++) if (*p!='t') *q++=*p;
  294.         *q='\0';
  295.  
  296.         return (fopen(name,nm));
  297. #endif
  298. }/*sfopen()*/
  299.  
  300.  
  301. /* end of dos_file.c */
  302.