home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 3.ddi / NOVELL / CTCLIBS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  8.5 KB  |  401 lines

  1. /*
  2.  *    Novell network example of the CTSLOK.C module based on low level
  3.  *    MS-DOS calls made through the c runtime library. This sample is
  4.  *    setup for the Novell 2.1 environment and the MICROSOFT C
  5.  *    compiler. This code assumes that the low level MS-DOS calls made
  6.  *    through the c runtime library return the status flags so that the
  7.  *    carry bit can be tested for a DOS error condition.
  8.  *
  9.  *    Small memory model server.
  10.  *
  11.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  12.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  13.  *    transfer of this program is strictly prohibited.
  14.  *
  15.  *      Copyright (c) 1985, 1986, 1987, 1988, 1989 FairCom Corporation
  16.  *    (Subject to limited distribution and
  17.  *     restricted disclosure only.)
  18.  *    *** ALL RIGHTS RESERVED ***
  19.  *
  20.  *    4006 West Broadway
  21.  *    Columbia, MO 65203
  22.  *
  23.  *
  24.  *    c-tree(R)    Version 4.3
  25.  *            Release C
  26.  *            February 7, 1989 17:30
  27.  *
  28.  */
  29.  
  30. #include "ctstdr.h"        /* standard i/o header         */
  31. #undef   EXTERN
  32. #define  EXTERN /* */
  33. #include "ctoptn.h"        /* c-tree configuration options */
  34. #include "cterrc.h"        /* c-tree error codes        */
  35. #include "ctstrc.h"        /* c-tree data structures    */
  36. #include "ctgvar.h"        /* c-tree global variables    */
  37. #include "ctcomm.h"
  38.  
  39. #define    INFINITY    3000    /* loop termination count waiting for lock */
  40.  
  41. /* DOS dependent low level function numbers */
  42.  
  43. #define OS_OPEN        0x3D00    /* MS-DOS dependent     */
  44. #define OS_CLOSE    0x3E00    /*        "        */
  45. #define OS_CREATE    0x3C00    /*        "        */
  46. #define OS_READ        0x3F00    /*        "        */
  47. #define OS_WRITE    0x4000    /*        "        */
  48. #define OS_REMOVE    0x4100    /*      "        */
  49. #define OS_LSEEK    0x4200    /*        "        */
  50. #define SH_MODE        0x0042    /*        "        */
  51. #define EX_MODE        0x0012    /*        "        */
  52. #define OS_FLUSH    0x0D00    /*        "        */
  53.  
  54. #define GT_LOCK        1
  55. #define UN_LOCK        2
  56.  
  57. COUNT        uerr();
  58. extern LONG  cts_wl,cts_rl,cts_ul;
  59. extern COUNT noverr;
  60.  
  61. /* 8086 dependent register structures based on Microsoft DOS.H model */
  62.  
  63. struct XREGS {
  64.     short ax,bx,cx,dx,si,di,cflag;
  65.     } outregs;
  66.  
  67. struct SREGS {
  68.     short es,cs,ss,ds;
  69.     } segreg;
  70.  
  71. /* parse pointer variable (used for memory models with 4 byte pointers) */
  72.  
  73. struct VAL {
  74.     int    offval;
  75.     int    segval;
  76.     };
  77. union PTR {
  78.     struct VAL v;
  79.     TEXT      *p;
  80. } ptr;
  81.  
  82. /* union to separate high and low words */
  83.  
  84. union {
  85.     LONG  li;
  86.     COUNT xi[2];    /* xi[0] = low order word */
  87. } ci;
  88.  
  89. /* --------------------------------------------------------- */
  90.  
  91. /*
  92.  * The dos() function must be changed if you are not using the Lattice
  93.  * C compiler. In particular, if your compiler uses different
  94.  * structures to represent the 8086 registers, define those structures
  95.  * local to the dos function and make the appropriate translation prior
  96.  * to calling your compiler's version of intdos(). Likewise, if your
  97.  * compiler does not return the status flag, change the dos return statement(s)
  98.  * so that it returns a non-zero value in case of error, and a zero if no
  99.  * error.
  100.  */
  101.  
  102. numdis(i)
  103. unsigned int i;
  104. {
  105. ltoa((long) i,ct_buf,10);
  106. display(ct_buf);
  107. display(" ");
  108. }
  109.  
  110. lngdis(i)
  111. long i;
  112. {
  113. ltoa(i,ct_buf,16);
  114. display(ct_buf);
  115. display("x ");
  116. }
  117.  
  118. COUNT dos(pr,nds)      /* returns a non-zero value on an OS error    */
  119. struct XREGS *pr;      /* pointer to input register values        */
  120. COUNT         nds;      /* nds != -1 implies data segment must be set    */
  121. {
  122.     int retval;
  123.  
  124. #ifdef LPOINTER          /* four byte pointers are in use        */
  125.     segread(&segreg); /* loads segreg structure with current values */
  126.     if (nds != -1)
  127.         segreg.ds = nds;
  128.  
  129. /* 
  130.  * note that intdos & intdosx return the status flag word. we "and" it
  131.  * with 0x0001 to see if the carry bit is set which signifies a dos error
  132.  */
  133.     return(intdosx(pr,&outregs,&segreg) & 0x0001);
  134. #else
  135.     retval = novshl(pr,&outregs);
  136. #ifdef VAPDEBUG
  137.     if (retval) {
  138.         display("\n\rOS error: ");
  139.         numdis(pr->ax);
  140.         numdis(noverr);
  141.     }
  142.     return(retval);
  143. #endif
  144.     /*???? delete  intdos(pr,&outregs);
  145.     return(outregs.cflag);*/
  146. #endif
  147. }
  148.  
  149. LOCAL COUNT ctseek(ctnum,recbyt)
  150. PFAST CTFILE      *ctnum;
  151. POINTER             recbyt;
  152. {
  153.     struct XREGS inregs;
  154.  
  155.     if (ctnum->sekpos == recbyt)
  156.         return(NO_ERROR);
  157.  
  158.     inregs.ax = OS_LSEEK;
  159.     ci.li     = ctnum->sekpos = recbyt;
  160.     inregs.dx = ci.xi[0];
  161.     inregs.cx = ci.xi[1];
  162.     inregs.bx = ctnum->fd;
  163.     if (dos(&inregs,-1))
  164.         return(uerr(SEEK_ERR));
  165.     else
  166.         return(NO_ERROR);
  167. }
  168.  
  169. RNDFILE mbopen(ctnum,opmode)
  170. PFAST CTFILE  *ctnum;
  171. COUNT             opmode;    /* EXCLUSIVE or SHARED */
  172. {
  173.     RNDFILE retval;
  174.     struct XREGS inregs;
  175.     COUNT        newds;
  176.  
  177.     COUNT vtclose();
  178.  
  179.     ctnum->sekpos = 0L;
  180.     inregs.ax = (OS_OPEN | EX_MODE);
  181.  
  182.     if (!(opmode & PERMANENT) && ct_numvfil >= MAXVFIL)
  183.         vtclose();
  184.  
  185.     ptr.p     = ctnum->flname;
  186.     inregs.dx = ptr.v.offval;
  187.  
  188. #ifdef LPOINTER
  189.     newds     = ptr.v.segval;
  190. #endif
  191.  
  192.     if (dos(&inregs,newds)) {  /* check for carry flag set => error */
  193.         retval = -1;
  194.         if (vtclose() == YES) {
  195.             if (dos(&inregs,newds))
  196.                 retval = -1;
  197.             else
  198.                 retval = outregs.ax;
  199.         }
  200.     } else
  201.         retval = outregs.ax;
  202.             
  203.  
  204.     if (!(opmode & PERMANENT) && retval >= 0)
  205.         ct_numvfil++;
  206.  
  207.     return(retval);
  208. }
  209.  
  210. /* ------------------------------------------------------------ */
  211.  
  212. RNDFILE mbcrat(ctnum)
  213. PFAST CTFILE  *ctnum;
  214. {
  215.     RNDFILE retval;
  216.     struct XREGS inregs;
  217.     COUNT        newds;
  218.  
  219.     COUNT vtclose();
  220.  
  221.     ctnum->sekpos = 0L;
  222.     if (!(ctnum->flmode & PERMANENT) && ct_numvfil >= MAXVFIL)
  223.         vtclose();
  224.  
  225.     inregs.ax = OS_CREATE;
  226.     inregs.cx = 0;
  227.     ptr.p     = ctnum->flname;
  228.     inregs.dx = ptr.v.offval;
  229.  
  230. #ifdef LPOINTER
  231.     newds     = ptr.v.segval;
  232. #endif
  233.  
  234.     if (dos(&inregs,newds)) {    /* check for carry flag set => error */
  235.         retval = -1;
  236.         if (vtclose() == YES) {
  237.             if (dos(&inregs,newds))
  238.                 retval = -1;
  239.             else
  240.                 retval = outregs.ax;
  241.         }
  242.     } else
  243.         retval = outregs.ax;
  244.  
  245.  
  246.     if (!(ctnum->flmode & PERMANENT) && retval >= 0)
  247.         ct_numvfil++;
  248.  
  249.     return(retval);
  250. }
  251.  
  252.  
  253. /* ------------------------------------------------------------ */
  254.  
  255. COUNT ctio(op_code,ctnum,recbyt,bufadr,iosize)
  256. COUNT       op_code;    /* CTREAD or CTWRITE */
  257. CTFILE          *ctnum;
  258. LONG             recbyt;
  259. TEXT                   *bufadr;
  260. unsigned int                   iosize;
  261. {
  262.     struct XREGS inregs;
  263.     COUNT        newds;
  264.     char tt_buf[12];
  265.  
  266. #ifdef VAPDEBUG
  267. if (op_code == CTREAD)
  268.     display("\n\rREAD...");
  269. else
  270.     display("\n\rWRITE..");
  271. display(ctnum->flname);
  272. display("..pos=");
  273. itoa((int) recbyt,tt_buf,10);
  274. display(tt_buf);
  275. #endif
  276.  
  277.     if (ctseek(ctnum,recbyt))
  278.         return(uerr(SEEK_ERR));
  279.     
  280.     if (!iosize)
  281.         iosize = ctnum->reclen;
  282.  
  283. #ifdef VAPDEBUG
  284. itoa(iosize,tt_buf,10);
  285. display("..io=");
  286. display(tt_buf);
  287. #endif
  288.  
  289.     inregs.bx = ctnum->fd;
  290.     inregs.cx = iosize;
  291.     ptr.p     = bufadr;
  292.     inregs.dx = ptr.v.offval;
  293. #ifdef LPOINTER
  294.     newds     = ptr.v.segval;
  295. #endif
  296.  
  297.     if (op_code == CTREAD) {
  298.         inregs.ax = OS_READ;
  299.         if (dos(&inregs,newds) || outregs.ax != iosize) {
  300.             ctnum->sekpos = -1L;
  301. #ifdef VAPDEBUG
  302.             display("\n\rERR..");
  303.             itoa(outregs.ax,tt_buf,10);
  304.             display(tt_buf);
  305. #endif
  306.             return(uerr(READ_ERR));
  307.         }
  308.     } else {
  309.         inregs.ax = OS_WRITE;
  310.         if (dos(&inregs,newds) || outregs.ax != iosize) {
  311.             ctnum->sekpos = -1L;
  312. #ifdef VAPDEBUG
  313.             display("\n\rERR..");
  314.             itoa(outregs.ax,tt_buf,10);
  315.             display(tt_buf);
  316. #endif
  317.             return(uerr(WRITE_ERR));
  318.         }
  319.     }
  320.  
  321. #ifdef VAPDEBUG
  322. display("ok");
  323. #endif
  324.  
  325.     ctnum->sekpos += iosize;
  326.     return(NO_ERROR);
  327. }
  328.  
  329.  
  330. /* ------------------------------------------------------------ */
  331.  
  332. COUNT mbclos( ctnum,clmode)
  333. PFAST CTFILE *ctnum;
  334. COUNT            clmode;    /* COMPLETE or PARTIAL */
  335. {
  336.     struct XREGS inregs;
  337.  
  338.     if (!(ctnum->flmode & PERMANENT))
  339.         ct_numvfil--;
  340.     inregs.ax = OS_CLOSE;
  341.     inregs.bx = ctnum->fd;
  342.  
  343.     if (dos(&inregs,-1))
  344.         return(-1);
  345.     else
  346.         return(0);
  347. }
  348.  
  349.  
  350. /* ------------------------------------------------------------ */
  351.  
  352. COUNT mbsave( ctnum)
  353. PFAST CTFILE *ctnum;
  354. {
  355.     COUNT   mbclos();
  356.     RNDFILE mbopen();
  357.  
  358.     if (mbclos(ctnum,ctnum->flmode))
  359.         return(uerr(FSAV_ERR));
  360.     else if ((ctnum->fd = mbopen(ctnum,ctnum->flmode)) < 0)
  361.         return(uerr(FSAV_ERR));
  362.     else
  363.         return(NO_ERROR);
  364. }
  365.  
  366. VOID flushdos(datno)
  367. COUNT          datno;
  368. {
  369. }
  370.  
  371. sexit(err)
  372. COUNT err;
  373. {
  374.     itoa(err,ct_buf,10);
  375.     display(ct_buf);
  376.     killvap();
  377. }
  378.  
  379. COUNT dltfil(filnam)
  380. TEXT        *filnam;
  381. {
  382.     struct XREGS inregs;
  383.     COUNT        newds;
  384.  
  385.     inregs.ax = OS_REMOVE;
  386.  
  387.     ptr.p     = filnam;
  388.     inregs.dx = ptr.v.offval;
  389.  
  390. #ifdef LPOINTER
  391.     newds     = ptr.v.segval;
  392. #endif
  393.  
  394.     if (dos(&inregs,newds)) /* check for carry flag set => error */
  395.         return(uerr_cod = DLTF_ERR);
  396.     else
  397.         return(NO_ERROR);
  398. }
  399.  
  400. /* end of ctclib.nov */
  401.