home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / PACKER / GTAK258 / RMT / rmt_disk.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  4KB  |  180 lines

  1. /*****************************************************************************
  2.  * $Id: rmt_disk.c,v 1.6 1994/10/17 01:05:34 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: rmt_disk.c,v $
  5.  * Revision 1.6  1994/10/17  01:05:34  ak
  6.  * DosClose was missing.
  7.  * Restrictive disk sharing modes.
  8.  * Return code checks of locking were missing.
  9.  *
  10.  * Revision 1.5  1994/10/04 21:40:53  ak
  11.  * Added the remaining functions to disk support.
  12.  *
  13.  * Revision 1.4  1994/02/16 15:29:17  edvkai
  14.  * Dummy checkin for CVS 1.3 crlf.
  15.  *
  16.  * Revision 1.3  1993/11/25 18:53:54  edvkai
  17.  * Removed DLL import by number.
  18.  * Changed return codes to avoid ambiguities.
  19.  * Changed lseek into seek, parameter changes.
  20.  *
  21.  * Revision 1.2  1993/05/26  17:06:44  AK
  22.  * 32-bit TCP/IP
  23.  *
  24.  * Revision 1.1.1.1  1993/02/08  21:31:56  AK
  25.  * TAR device interface DLLs.
  26.  *
  27.  * Revision 1.1  1993/02/08  21:31:54  AK
  28.  * Initial revision
  29.  *
  30.  *****************************************************************************/
  31.  
  32. static char *rcsid = "$Id: rmt_disk.c,v 1.6 1994/10/17 01:05:34 ak Exp $";
  33.  
  34. /*
  35.  *    disk.c
  36.  *
  37.  * Floppy disk interface for GNU tar.
  38.  *
  39.  * Thanks to Kai Use Rommel for the disk locking code.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <errno.h>
  44. #include <fcntl.h>
  45. #include <io.h>
  46. #define INCL_BASE
  47. #define INCL_DOSDEVIOCTL
  48. #ifndef __IBMC__
  49. # define _Packed
  50. #endif
  51. #include <os2.h>
  52.  
  53. #ifndef __IBMC__
  54. # define _System
  55. #endif
  56.  
  57. static int    wrflag;
  58. static unsigned    last_error;
  59.  
  60. #define ERROR_RETURN(rc) if (rc) { last_error = rc; return -2; }
  61.  
  62. int _System
  63. rmt_open(char *name, int mode, int prot)
  64. {
  65.     ULONG    rc, handle, action;
  66.  
  67.     wrflag = (mode & 3) != O_RDONLY;
  68.     rc = DosOpen((PSZ)name, &handle, &action, 0L, 0, FILE_OPEN,
  69.         ((mode & 3) ? OPEN_SHARE_DENYREADWRITE : OPEN_SHARE_DENYWRITE)
  70.         + OPEN_FLAGS_FAIL_ON_ERROR
  71.         + OPEN_FLAGS_DASD
  72.         + (mode & 3), 0);
  73.     ERROR_RETURN(rc)
  74.     if (wrflag) {
  75.             BYTE cmd = 0;
  76.         ULONG ulParmLengthInOut = sizeof(cmd), 
  77.               ulDataLengthInOut = 0;
  78.         rc = DosDevIOCtl(handle, IOCTL_DISK, DSK_LOCKDRIVE, 
  79.                 &cmd, sizeof(cmd), &ulParmLengthInOut,
  80.                 0, 0, &ulDataLengthInOut);
  81.         ERROR_RETURN(rc)
  82.     }
  83.     return handle;
  84. }
  85.  
  86. int _System
  87. rmt_read(int fd, void *p, unsigned len)
  88. {
  89.     ULONG actual, rc;
  90.     rc = DosRead(fd, p, len, &actual);
  91.     ERROR_RETURN(rc)
  92.     return actual;
  93. }
  94.  
  95. int _System
  96. rmt_write(int fd, void *p, unsigned len)
  97. {
  98.     ULONG actual, rc;
  99.     rc = DosWrite(fd, p, len, &actual);
  100.     ERROR_RETURN(rc)
  101.     return actual ? actual : -3;
  102. }
  103.  
  104. long _System
  105. rmt_seek(int fd, long block, long blocksz, int mode)
  106. {
  107.     ULONG actual, rc;
  108.  
  109.     switch (mode) {
  110.     case 4:
  111.         mode = 1;
  112.         block = 0;
  113.         break;
  114.     case 5:
  115.         block = 0;
  116.     case 3:
  117.         mode = 0;
  118.     case 2:
  119.     case 1:
  120.     case 0:
  121.         break;
  122.     default:
  123.         return -4;
  124.     }
  125.     rc = DosSetFilePtr(fd, block * blocksz, mode, &actual);
  126.     ERROR_RETURN(rc)
  127.     return actual;
  128. }
  129.  
  130. int _System
  131. rmt_close(int fd)
  132. {
  133.     ULONG rc;
  134.  
  135.     if (wrflag) {
  136.         BYTE cmd = 0;
  137.         ULONG ulParmLengthInOut = sizeof(cmd), 
  138.               ulDataLengthInOut = 0;
  139.         rc = DosDevIOCtl(fd, IOCTL_DISK, DSK_UNLOCKDRIVE, 
  140.                 &cmd, sizeof(cmd), &ulParmLengthInOut,
  141.                 0, 0, &ulDataLengthInOut);
  142.         ERROR_RETURN(rc)
  143.     }
  144.     rc = DosClose(fd);
  145.     ERROR_RETURN(rc)
  146.     return 0;
  147. }
  148.  
  149. long _System
  150. rmt_error(void)
  151. {
  152.     return last_error;
  153. }
  154.  
  155. char * _System
  156. rmt_status(long rcode)
  157. {
  158.     static char buf[128];
  159.     ULONG len;
  160.  
  161.     if (DosGetMessage(NULL, 0, buf, sizeof buf, rcode, "OSO001.MSG", &len))
  162.         sprintf(buf, "System error %u", rcode);
  163.     return buf;
  164. }
  165.  
  166. #ifdef __EMX__
  167.  
  168. unsigned long
  169. _DLL_InitTerm(unsigned long mod_handle, unsigned long flag)
  170. {
  171.     if (!flag) {
  172.         _CRT_init();
  173.     } else {
  174.         _CRT_term();
  175.     }
  176.     return 1;
  177. }
  178.  
  179. #endif
  180.