home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 12 / MA_Cover_12.iso / devs / hyperio-install-idc / goodies / hydra.lha / hydracom-source.lha / dos_file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-02  |  9.0 KB  |  360 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. #ifndef AMIGA
  61. static boolean dos_sharing = false;
  62. #endif    /* AMIGA */
  63.  
  64.  
  65. void dos_sharecheck (void)    /* SHARE installed? set dos_sharing true/false */
  66. {
  67. #if __MSDOS__
  68.         union REGS regs;
  69.  
  70.         regs.x.ax = 0x1000;                             /* DOS Multiplexer   */
  71.         int86(0x2f,®s,®s);                        /* INT 2Fh sub 1000h */
  72.         dos_sharing = (regs.h.al == 0xff) ? true : false;
  73. #endif
  74. }/*dos_sharecheck()*/
  75.  
  76.  
  77. int dos_open (char *pathname, byte create)
  78. {
  79.         register int access;
  80.  
  81. #ifdef __MSDOS__
  82.         access = O_RDWR | O_BINARY;
  83.         if (create) {
  84.            access |= O_CREAT;
  85.            if (create == 2)
  86.               access |= O_TRUNC;
  87.         }
  88.  
  89.         return (open(pathname, access,
  90.                      create ? S_IREAD | S_IWRITE : 0));
  91. #endif
  92. #ifdef __TOS__
  93.         access = O_RDWR;
  94.         if (create) {
  95.            access |= O_CREAT;
  96.            if (create == 2)
  97.               access |= O_TRUNC;
  98.         }
  99.  
  100.         return (open(pathname, access, 0));
  101. #endif
  102.  
  103. #ifdef AMIGA
  104.         access = O_RDWR;
  105.         if (create) {
  106.            access |= O_CREAT;
  107.            if (create == 2)
  108.               access |= O_TRUNC;
  109.         }
  110.  
  111.         return (open(pathname, access, 0));
  112. #endif    /* AMIGA */
  113. }/*dos_open()*/
  114.  
  115.  
  116. int dos_sopen (char *pathname, byte create)
  117. {
  118.         register int access;
  119.  
  120. #ifdef __MSDOS__
  121.         access = O_RDWR | O_BINARY;
  122.         if (create) {
  123.            access |= O_CREAT;
  124.            if (create == 2)
  125.               access |= O_TRUNC;
  126.         }
  127.  
  128.         return (sopen(pathname, access,
  129.                    dos_sharing ? SH_DENYWR : 0,
  130.                    create ? S_IREAD | S_IWRITE : 0));
  131. #endif
  132. #ifdef __TOS__
  133.         access = O_RDWR;
  134.         if (create) {
  135.            access |= O_CREAT;
  136.            if (create == 2)
  137.               access |= O_TRUNC;
  138.         }
  139.  
  140.         return (open(pathname, access, 0));
  141. #endif
  142. #ifdef AMIGA
  143.         access = O_RDWR;
  144.         if (create) {
  145.            access |= O_CREAT;
  146.            if (create == 2)
  147.               access |= O_TRUNC;
  148.         }
  149.  
  150.         return (open(pathname, access, 0));
  151. #endif
  152. }/*dos_sopen()*/
  153.  
  154.  
  155. int dos_sappend (char *pathname, byte create)
  156. {
  157.         register int access;
  158.  
  159. #ifdef __MSDOS__
  160.         access = O_WRONLY | O_APPEND | O_TEXT;
  161.         if (create) {
  162.            access |= O_CREAT;
  163.            if (create == 2)
  164.               access |= O_TRUNC;
  165.         }
  166.  
  167.         return (sopen(pathname, access,
  168.                       dos_sharing ? SH_DENYWR : 0,
  169.                       create ? S_IREAD | S_IWRITE : 0));
  170. #endif
  171. #ifdef __TOS__
  172.         access = O_WRONLY | O_APPEND;
  173.         if (create) {
  174.            access |= O_CREAT;
  175.            if (create == 2)
  176.               access |= O_TRUNC;
  177.         }
  178.  
  179.         return (open(pathname, access, 0));
  180. #endif
  181. #ifdef AMIGA
  182.         access = O_WRONLY | O_APPEND;
  183.         if (create) {
  184.            access |= O_CREAT;
  185.            if (create == 2)
  186.               access |= O_TRUNC;
  187.         }
  188.  
  189.         return (open(pathname, access, 0));
  190. #endif
  191. }/*dos_sappend()*/
  192.  
  193.  
  194. int dos_close (int handle)
  195. {
  196.         return (close(handle));
  197. }/*dos_close()*/
  198.  
  199.  
  200. int dos_lock (int handle, long offset, long len)
  201. {
  202. #ifdef __MSDOS__
  203.         if (dos_sharing)
  204.            while (!lock(handle,offset,len));
  205. #endif
  206.  
  207.         return (0);
  208. }/*dos_lock()*/
  209.  
  210.  
  211. int dos_unlock (int handle, long offset, long len)
  212. {
  213. #ifdef __MSDOS__
  214.         return (dos_sharing ? unlock(handle,offset,len) : 0);
  215. #endif
  216. #ifdef __TOS__
  217.         return 0;
  218. #endif
  219. #ifdef AMIGA
  220.         return 0;
  221. #endif
  222. }/*dos_unlock()*/
  223.  
  224.  
  225. long dos_seek (int handle, long offset, int fromwhere)
  226. {
  227.         return (lseek(handle,offset,fromwhere));
  228. }/*dos_seek()*/
  229.  
  230.  
  231. long dos_tell (int handle)
  232. {
  233. #ifdef __MSDOS__
  234.         return (tell(handle));
  235. #endif
  236. #ifdef __TOS__
  237.         return (lseek(handle,0L,SEEK_CUR));
  238. #endif
  239. #ifdef AMIGA
  240.         return (lseek(handle,0L,SEEK_CUR));
  241. #endif
  242. }/*dos_tell()*/
  243.  
  244.  
  245. #ifdef AMIGA
  246. int dos_read (int handle, void *buf, long len)
  247. {
  248.         return ((int) read(handle,buf,len));
  249. }/*dos_read()*/
  250. #else
  251. int dos_read (int handle, void *buf, word len)
  252. {
  253. #ifdef __MSDOS__
  254.         return (_read(handle,buf,len));
  255. #endif
  256. #ifdef __TOS__
  257.         return ((int) read(handle,buf,len));
  258. #endif
  259. #ifdef AMIGA
  260.         return ((int) read(handle,buf,len));
  261. #endif
  262. }/*dos_read()*/
  263. #endif    /* AMIGA */
  264.  
  265. #ifdef AMIGA
  266. int dos_write (int handle, void *buf, long len)
  267. {
  268.         return ((int) write(handle,buf,len));
  269. }/*dos_write()*/
  270. #else
  271. int dos_write (int handle, void *buf, word len)
  272. {
  273. #ifdef __MSDOS__
  274.         return (_write(handle,buf,len));
  275. #endif
  276. #ifdef __TOS__
  277.         return ((int) write(handle,buf,len));
  278. #endif
  279. }/*dos_write()*/
  280. #endif    /* AMIGA */
  281.  
  282.  
  283. /* ----------------------------------------------------------------------------
  284.                         oflags                  mode
  285.         r       O_RDONLY                        don't care
  286.         w       O_CREAT | O_WRONLY | O_TRUNC    S_IWRITE
  287.         a       O_CREAT | O_WRONLY | O_APPEND   S_IWRITE
  288.         r+      O_RDWR                          don't care
  289.         w+      O_RDWR | O_CREAT | O_TRUNC      S_IWRITE | S_IREAD
  290.         a+      O_RDWR | O_CREAT | O_APPEND     S_IWRITE | S_IREAD
  291. ---------------------------------------------------------------------------- */
  292. FILE *sfopen (char *name, char *mode, int shareflag)
  293. {
  294. #ifdef __MSDOS__
  295.         int   fd, access, flags;
  296.         char  *type = mode, c;
  297.         FILE *fp;
  298.  
  299.         if ((c = *type++) == 'r') {
  300.            access = O_RDONLY;
  301.            flags = 0;
  302.         }
  303.         else if (c == 'w') {
  304.            access = O_WRONLY | O_CREAT | O_TRUNC;
  305.            flags = S_IWRITE;
  306.         }
  307.         else if (c == 'a') {
  308.            access = O_RDWR   | O_CREAT | O_APPEND;
  309.            flags = S_IWRITE;
  310.         }
  311.         else
  312.            return (NULL);
  313.  
  314.         c = *type++;
  315.  
  316.         if (c == '+' || (*type == '+' && (c == 't' || c == 'b'))) {
  317.            if (c == '+') c = *type;
  318.            access = (access & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  319.            flags = S_IREAD | S_IWRITE;
  320.         }
  321.  
  322.         if      (c == 't') access |= O_TEXT;
  323.         else if (c == 'b') access |= O_BINARY;
  324.         else               access |= (_fmode & (O_TEXT | O_BINARY));
  325.  
  326.         if (dos_sharing)
  327.            access |= shareflag;
  328.  
  329.         fd = open(name, access, flags);
  330.  
  331.         if (fd < 0) return (NULL);
  332.  
  333.         if ((fp = fdopen(fd,mode)) == NULL)
  334.            close(fd);
  335.         else if (setvbuf(fp,NULL,_IOFBF,BUFSIZ)) {
  336.            fclose(fp);
  337.            return (NULL);
  338.         }
  339.  
  340.         return (fp);
  341. #endif
  342.  
  343. #ifdef __TOS__
  344.         char *p, *q, nm[10];
  345.  
  346.         strcpy(nm,mode);
  347.         for(p=q=nm; *p; p++) if (*p!='t') *q++=*p;
  348.         *q='\0';
  349.  
  350.         return (fopen(name,nm));
  351. #endif
  352.  
  353. #ifdef AMIGA
  354.     return(fopen(name,mode));
  355. #endif    /* AMIGA */
  356. }/*sfopen()*/
  357.  
  358.  
  359. /* end of dos_file.c */
  360.