home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 4.ddi / NETBIOS / CTRFIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  3.8 KB  |  231 lines

  1. /*
  2.  *    c-tree server for netbios
  3.  *     open, sopen, read, write, lseek, close for netbios servers
  4.  *
  5.  *  !!DOES NOT SUPPORT fstat()!!
  6.  *
  7.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  8.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  9.  *    transfer of this program is strictly prohibited.
  10.  *
  11.  *      Copyright (c) 1987, 1988, 1989 FairCom Corporation
  12.  *    (Subject to limited distribution and
  13.  *     restricted disclosure only.)
  14.  *    *** ALL RIGHTS RESERVED ***
  15.  *
  16.  *    4006 West Broadway
  17.  *    Columbia, MO 65203
  18.  *
  19.  *
  20.  *    c-tree(R)    Version 4.3
  21.  *            Release C 
  22.  *            February 7, 1989 17:30
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <sys\types.h>
  30. #include <sys\stat.h>
  31. #include <share.h>
  32. #include <dos.h>    /* cpu regs structure */
  33.  
  34. extern int errno;
  35.  
  36. /*** variables for passing CPU regs to/from intdos ***/
  37. static union REGS r;
  38. static struct SREGS sr;
  39.  
  40.  
  41. /*
  42.  * sopen
  43.  */
  44.  
  45. /* binary mode is assumed for sopen and open */
  46. /* pmode ignored */
  47.  
  48. int sopen(path, oflag, shflag, pmode)
  49. char     *path;
  50. int        oflag, shflag, pmode;
  51. {
  52.     long lseek();
  53.     int cmd, access, handle;
  54.     char far *lp;
  55.     lp = (char far *) path;
  56.     if (oflag & O_RDONLY)
  57.         access = 0;
  58.     else if (oflag & O_WRONLY)
  59.         access = 1;
  60.     else
  61.         access = 2;
  62.     switch (shflag)
  63.     {
  64.         case SH_DENYRW:    access |= 0x10; break;
  65.         case SH_DENYWR: access |= 0x20; break;
  66.         case SH_DENYRD: access |= 0x30; break;
  67.         case SH_DENYNO: access |= 0x40; break;
  68.         default: access |= 0;
  69.     }
  70.     if (oflag & O_CREAT)
  71.     {
  72.         if (oflag & O_EXCL)
  73.             cmd = 0x5b00;        /* create new */
  74.         else
  75.             cmd = 0x3c00;
  76.     }
  77.     else /* not create */
  78.     {
  79.             cmd = 0x3d00 | access;
  80.     }
  81.     r.x.ax = cmd;
  82.     r.x.cx = 0;
  83.     r.x.dx = FP_OFF(lp);
  84.     sr.ds = FP_SEG(lp);
  85.     intdosx(&r, &r, &sr);
  86.     if (r.x.cflag)
  87.     {
  88.         if (r.x.ax == 0x50)
  89.             errno = EEXIST;
  90.         else if (r.x.ax == 4)
  91.             errno = EMFILE;
  92.         else if (r.x.ax == 0x20)
  93.             errno = EACCES;
  94.         else
  95.             errno = ENOENT;
  96.         return -1;
  97.     }
  98.     else
  99.     {
  100.         handle = r.x.ax;
  101.         if (0x3d00 == (cmd & 0xff00))
  102.         {
  103.             if (oflag & O_TRUNC)
  104.                 write(handle, (char *)&cmd, 0);        /* truncates file */
  105.             else if (oflag & O_APPEND)
  106.                 lseek(handle, 0L, 2);
  107.         }
  108.         return handle;
  109.     }
  110. }
  111.  
  112. /*
  113.  * open
  114.  */
  115.  
  116. int open(path, oflag, pmode)
  117. char     *path;
  118. int        oflag, pmode;
  119. {
  120.     return sopen(path, oflag, 0, pmode);
  121. }
  122.  
  123. /*
  124.  * read
  125.  */
  126.  
  127. int read(handle, buffer, count)
  128. int handle;
  129. char *buffer;
  130. int count;
  131. {
  132.     char far *lp;
  133.     lp = (char far *) buffer;
  134.     r.x.ax = 0x3f00;
  135.     r.x.bx = handle;
  136.     r.x.cx = count;
  137.     r.x.dx = FP_OFF(lp);
  138.     sr.ds = FP_SEG(lp);
  139.     intdosx(&r, &r, &sr);
  140.     if (r.x.cflag)
  141.     {
  142.         errno = EBADF;
  143.         return -1;
  144.     }
  145.     else
  146.         return r.x.ax;            /* num bytes actually read */
  147. }
  148.  
  149. /*
  150.  * write
  151.  */
  152.  
  153. int write(handle, buffer, count)
  154. int handle;
  155. char *buffer;
  156. int count;
  157. {
  158.     char far *lp;
  159.     lp = (char far *) buffer;
  160.     r.x.ax = 0x4000;
  161.     r.x.bx = handle;
  162.     r.x.cx = count;
  163.     r.x.dx = FP_OFF(lp);
  164.     sr.ds = FP_SEG(lp);
  165.     intdosx(&r, &r, &sr);
  166.     if (r.x.cflag)
  167.     {
  168.         errno = EBADF;
  169.         return -1;
  170.     }
  171.     else
  172.     {
  173.         if (r.x.ax != count)
  174.         {
  175.             errno = ENOSPC;
  176.             return -1;
  177.         }
  178.         else
  179.             return r.x.ax;            /* num bytes actually read */
  180.     }
  181. }
  182.  
  183. /*
  184.  * lseek
  185.  */
  186.  
  187. long lseek(handle, offset, origin)
  188. int handle;
  189. long offset;
  190. int origin;
  191. {
  192.     r.x.ax = 0x4200 | origin;
  193.     r.x.bx = handle;
  194. /*
  195.     r.x.dx = (int) origin;
  196.     r.x.cx = (int) (origin >> 16);
  197. */
  198.     r.x.dx = (int) offset;
  199.     r.x.cx = (int) (offset >> 16);
  200.     intdos(&r, &r);
  201.     if (r.x.cflag)
  202.     {
  203.         errno = (r.x.ax == 6) ? EBADF : EINVAL;
  204.         return -1l;
  205.     }
  206.     else
  207.         return (((long) r.x.dx) << 16) + ((long) (unsigned) r.x.ax);
  208. }
  209.  
  210.  
  211. /*
  212.  * close
  213.  */
  214.  
  215. int close(handle)
  216. int handle;
  217. {
  218.     r.x.ax = 0x3e00;
  219.     r.x.bx = handle;
  220.     intdos(&r, &r);
  221.     if (r.x.cflag)
  222.     {
  223.         errno = EBADF;
  224.         return -1;
  225.     }
  226.     else
  227.         return 0;
  228. }
  229.  
  230. /* end of ctrfio.c */
  231.