home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 3.ddi / NOVELL / CTCLIBC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  8.4 KB  |  400 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.  *    Compact 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. #define    LPOINTER
  31.  
  32. #include "ctstdr.h"        /* standard i/o header         */
  33. #undef   EXTERN
  34. #define  EXTERN /* */
  35. #include "ctoptn.h"        /* c-tree configuration options */
  36. #include "cterrc.h"        /* c-tree error codes        */
  37. #include "ctstrc.h"        /* c-tree data structures    */
  38. #include "ctgvar.h"        /* c-tree global variables    */
  39. #include "ctcomm.h"
  40.  
  41. #define    INFINITY    3000    /* loop termination count waiting for lock */
  42.  
  43. /* DOS dependent low level function numbers */
  44.  
  45. #define OS_OPEN        0x3D00    /* MS-DOS dependent     */
  46. #define OS_CLOSE    0x3E00    /*        "        */
  47. #define OS_CREATE    0x3C00    /*        "        */
  48. #define OS_READ        0x3F00    /*        "        */
  49. #define OS_WRITE    0x4000    /*        "        */
  50. #define OS_REMOVE    0x4100    /*      "        */
  51. #define OS_LSEEK    0x4200    /*        "        */
  52. #define SH_MODE        0x0042    /*        "        */
  53. #define EX_MODE        0x0012    /*        "        */
  54. #define OS_FLUSH    0x0D00    /*        "        */
  55.  
  56. #define GT_LOCK        1
  57. #define UN_LOCK        2
  58.  
  59. COUNT        uerr();
  60. extern LONG  cts_wl,cts_rl,cts_ul;
  61. extern COUNT noverr;
  62.  
  63. /* 8086 dependent register structures based on Microsoft DOS.H model */
  64.  
  65. struct XREGS {
  66.     short ax,bx,cx,dx,si,di,cflag;
  67.     } outregs;
  68.  
  69. struct SREGS {
  70.     short es,cs,ss,ds;
  71.     } segreg;
  72.  
  73. /* parse pointer variable (used for memory models with 4 byte pointers) */
  74.  
  75. struct VAL {
  76.     int    offval;
  77.     int    segval;
  78.     };
  79. union PTR {
  80.     struct VAL v;
  81.     TEXT      *p;
  82. } ptr;
  83.  
  84. /* union to separate high and low words */
  85.  
  86. union {
  87.     LONG  li;
  88.     COUNT xi[2];    /* xi[0] = low order word */
  89. } ci;
  90.  
  91. /* --------------------------------------------------------- */
  92.  
  93. /*
  94.  * The dos() function must be changed if you are not using the Lattice
  95.  * C compiler. In particular, if your compiler uses different
  96.  * structures to represent the 8086 registers, define those structures
  97.  * local to the dos function and make the appropriate translation prior
  98.  * to calling your compiler's version of intdos(). Likewise, if your
  99.  * compiler does not return the status flag, change the dos return statement(s)
  100.  * so that it returns a non-zero value in case of error, and a zero if no
  101.  * error.
  102.  */
  103.  
  104. numdis(i)
  105. unsigned int i;
  106. {
  107. ltoa((long) i,ct_buf,10);
  108. display(ct_buf);
  109. display(" ");
  110. }
  111.  
  112. lngdis(i)
  113. long i;
  114. {
  115. ltoa(i,ct_buf,10);
  116. display(ct_buf);
  117. display(" ");
  118. }
  119.  
  120. COUNT dos(pr,nds)      /* returns a non-zero value on an OS error    */
  121. struct XREGS *pr;      /* pointer to input register values        */
  122. COUNT         nds;      /* nds != -1 implies data segment must be set    */
  123. {
  124.     int retval;
  125.  
  126. #ifdef LPOINTER          /* four byte pointers are in use        */
  127.     segread(&segreg); /* loads segreg structure with current values */
  128.     if (nds != -1)
  129.         segreg.ds = nds;
  130.  
  131. /* 
  132.  * note that intdos & intdosx return the status flag word. we "and" it
  133.  * with 0x0001 to see if the carry bit is set which signifies a dos error
  134.  */
  135.     retval = novshlx(pr,&outregs,&segreg);
  136. #else
  137.     retval = novshl(pr,&outregs);
  138. #endif
  139. #ifdef VAPDEBUG
  140.     if (retval) {
  141.         display("\n\rOS error: ");
  142.         numdis(pr->ax);
  143.         numdis(noverr);
  144.     }
  145.     return(retval);
  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.     if (!(opmode & PERMANENT) && retval >= 0)
  204.         ct_numvfil++;
  205.  
  206.     return(retval);
  207. }
  208.  
  209. /* ------------------------------------------------------------ */
  210.  
  211. RNDFILE mbcrat(ctnum)
  212. PFAST CTFILE  *ctnum;
  213. {
  214.     RNDFILE retval;
  215.     struct XREGS inregs;
  216.     COUNT        newds;
  217.  
  218.     COUNT vtclose();
  219.  
  220.     ctnum->sekpos = 0L;
  221.     if (!(ctnum->flmode & PERMANENT) && ct_numvfil >= MAXVFIL)
  222.         vtclose();
  223.  
  224.     inregs.ax = OS_CREATE;
  225.     inregs.cx = 0;
  226.     ptr.p     = ctnum->flname;
  227.     inregs.dx = ptr.v.offval;
  228.  
  229. #ifdef LPOINTER
  230.     newds     = ptr.v.segval;
  231. #endif
  232.  
  233.     if (dos(&inregs,newds)) {    /* check for carry flag set => error */
  234.         retval = -1;
  235.         if (vtclose() == YES) {
  236.             if (dos(&inregs,newds))
  237.                 retval = -1;
  238.             else
  239.                 retval = outregs.ax;
  240.         }
  241.     } else
  242.         retval = outregs.ax;
  243.  
  244.  
  245.     if (!(ctnum->flmode & PERMANENT) && retval >= 0)
  246.         ct_numvfil++;
  247.  
  248.     return(retval);
  249. }
  250.  
  251.  
  252. /* ------------------------------------------------------------ */
  253.  
  254. COUNT ctio(op_code,ctnum,recbyt,bufadr,iosize)
  255. COUNT       op_code;    /* CTREAD or CTWRITE */
  256. CTFILE          *ctnum;
  257. LONG             recbyt;
  258. TEXT                   *bufadr;
  259. unsigned int                   iosize;
  260. {
  261.     struct XREGS inregs;
  262.     COUNT        newds;
  263.     char tt_buf[12];
  264.  
  265. #ifdef VAPDEBUG
  266. if (op_code == CTREAD)
  267.     display("\n\rREAD...");
  268. else
  269.     display("\n\rWRITE..");
  270. display(ctnum->flname);
  271. display("..pos=");
  272. itoa((int) recbyt,tt_buf,10);
  273. display(tt_buf);
  274. #endif
  275.  
  276.     if (ctseek(ctnum,recbyt))
  277.         return(uerr(SEEK_ERR));
  278.     
  279.     if (!iosize)
  280.         iosize = ctnum->reclen;
  281.  
  282. #ifdef VAPDEBUG
  283. itoa(iosize,tt_buf,10);
  284. display("..io=");
  285. display(tt_buf);
  286. #endif
  287.  
  288.     inregs.bx = ctnum->fd;
  289.     inregs.cx = iosize;
  290.     ptr.p     = bufadr;
  291.     inregs.dx = ptr.v.offval;
  292. #ifdef LPOINTER
  293.     newds     = ptr.v.segval;
  294. #endif
  295.  
  296.     if (op_code == CTREAD) {
  297.         inregs.ax = OS_READ;
  298.         if (dos(&inregs,newds) || outregs.ax != iosize) {
  299.             ctnum->sekpos = -1L;
  300. #ifdef VAPDEBUG
  301.             display("\n\rERR..");
  302.             itoa(outregs.ax,tt_buf,10);
  303.             display(tt_buf);
  304. #endif
  305.             return(uerr(READ_ERR));
  306.         }
  307.     } else {
  308.         inregs.ax = OS_WRITE;
  309.         if (dos(&inregs,newds) || outregs.ax != iosize) {
  310.             ctnum->sekpos = -1L;
  311. #ifdef VAPDEBUG
  312.             display("\n\rERR..");
  313.             itoa(outregs.ax,tt_buf,10);
  314.             display(tt_buf);
  315. #endif
  316.             return(uerr(WRITE_ERR));
  317.         }
  318.     }
  319.  
  320. #ifdef VAPDEBUG
  321. display("ok");
  322. #endif
  323.  
  324.     ctnum->sekpos += iosize;
  325.     return(NO_ERROR);
  326. }
  327.  
  328.  
  329. /* ------------------------------------------------------------ */
  330.  
  331. COUNT mbclos( ctnum,clmode)
  332. PFAST CTFILE *ctnum;
  333. COUNT            clmode;    /* COMPLETE or PARTIAL */
  334. {
  335.     struct XREGS inregs;
  336.  
  337.     if (!(ctnum->flmode & PERMANENT))
  338.         ct_numvfil--;
  339.     inregs.ax = OS_CLOSE;
  340.     inregs.bx = ctnum->fd;
  341.  
  342.     if (dos(&inregs,-1))
  343.         return(-1);
  344.     else
  345.         return(0);
  346. }
  347.  
  348.  
  349. /* ------------------------------------------------------------ */
  350.  
  351. COUNT mbsave( ctnum)
  352. PFAST CTFILE *ctnum;
  353. {
  354.     COUNT   mbclos();
  355.     RNDFILE mbopen();
  356.  
  357.     if (mbclos(ctnum,ctnum->flmode))
  358.         return(uerr(FSAV_ERR));
  359.     else if ((ctnum->fd = mbopen(ctnum,ctnum->flmode)) < 0)
  360.         return(uerr(FSAV_ERR));
  361.     else
  362.         return(NO_ERROR);
  363. }
  364.  
  365. VOID flushdos(datno)
  366. COUNT          datno;
  367. {
  368. }
  369.  
  370. sexit(err)
  371. COUNT err;
  372. {
  373.     itoa(err,ct_buf,10);
  374.     display(ct_buf);
  375.     killvap();
  376. }
  377.  
  378. COUNT dltfil(filnam)
  379. TEXT        *filnam;
  380. {
  381.     struct XREGS inregs;
  382.     COUNT        newds;
  383.  
  384.     inregs.ax = OS_REMOVE;
  385.  
  386.     ptr.p     = filnam;
  387.     inregs.dx = ptr.v.offval;
  388.  
  389. #ifdef LPOINTER
  390.     newds     = ptr.v.segval;
  391. #endif
  392.  
  393.     if (dos(&inregs,newds)) /* check for carry flag set => error */
  394.         return(uerr_cod = DLTF_ERR);
  395.     else
  396.         return(NO_ERROR);
  397. }
  398.  
  399. /* end of ctclibc.nov */
  400.