home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / ZAPCTLZ.CAS < prev   
Encoding:
Text File  |  1992-06-10  |  5.8 KB  |  187 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - zapctrlz.cas
  3.  *
  4.  * function(s)
  5.  *        dosSeekFinalChar - seek to the final character in file
  6.  *        dosReadOne       - reads one byte from a file
  7.  *        __TrimCtlZ       - zaps a terminating control Z
  8.  *        __AppendCtlZ     - appends a control Z character to a file
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1987, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <io.h>
  23. #include <_io.h>
  24.  
  25. #define _ctlZ  26
  26.  
  27. /*-----------------------------------------------------------------------*
  28.  
  29.    The ctrl-Z file terminator is a legacy of operating systems used
  30.    on the PDP-8, which were strongly influential on the CPM OS, which
  31.    in turn influenced the first version of DOS.  Some old programs
  32.    use it, but it is obsolete practice in DOS since DOS keeps track
  33.    of exact byte lengths of files (CPM kept count only of disk blocks,
  34.    hence the importance of ^Z).  ^Z can also conflict with other uses
  35.    of the ASCII character set since the standard meaning of 0x1A is
  36.    "SUB" (subscript).
  37.  
  38.    With the advent of DOS networking the ^Z convention has become
  39.    impossible to implement safely.  The reason is the "deny read"
  40.    mode of file access, which can be used to enforce write-only access
  41.    to a shared file.  If the file cannot be read, the TC RTL cannot
  42.    decide whether or not a file is ^Z terminated.
  43.  
  44.    If you need ^Z conventions, the simplest way is to place ^Z logic
  45.    in your application.  However, the TC RTL includes code for ^Z
  46.    which automatically appends and strips a single ^Z terminator for
  47.    text files.  To enable this code, you compile "open.cas" and "close.c"
  48.    with the symbol "CPM_ctlZ" defined,
  49.  
  50.         tcc -DCPM_ctlz  ..etc.. open.cas close.c
  51.  
  52.    The TC library as shipped does not have this code enabled.
  53.  
  54.    Note that these algorithms DO NOT work safely if files are opened
  55.    for write-only access, which includes the fopen "a" append mode.
  56.    Let the user beware !
  57.  
  58. *------------------------------------------------------------------------*/
  59.  
  60. /*-----------------------------------------------------------------------*
  61.  
  62. Name            dosSeekFinalChar - seek to the final character in file
  63.  
  64. Usage           static  void pascal near    dosSeekFinalChar (int fildes)
  65.  
  66. Description     seeks to the end of file.
  67.  
  68. Return value    Nothing.  DX:AX is left with the MSW:LSW of the
  69.                 end-of-file position.
  70.  
  71. *------------------------------------------------------------------------*/
  72. static void pascal near dosSeekFinalChar(int fildes)
  73. {
  74. asm     mov     bx, fildes
  75. asm     mov     cx, -1
  76. asm     mov     dx, cx
  77. asm     mov     ax, 4200h + SEEK_END
  78. asm     int     21h             /* DX:AX = lseek (BX, CX:DX, AL)        */
  79.  
  80.         return;                     /* no error checking    */
  81. }
  82.  
  83.  
  84. /*-----------------------------------------------------------------------*
  85.  
  86. Name            dosReadOne - reads one byte from a file
  87.  
  88. Usage           static  char pascal near    dosReadOne (int fildes)
  89.  
  90. Description     reads one byte from a file
  91.  
  92. Return value    the character read
  93.  
  94. *------------------------------------------------------------------------*/
  95. static char pascal near dosReadOne(int fildes)
  96. {
  97.         char    c = 0;
  98.  
  99.         pushDS_
  100. asm     mov     bx, fildes
  101. asm     mov     cx, 1
  102. #if LDATA
  103. asm     push    SS
  104. asm     pop     DS
  105. #endif
  106. asm     lea     dx, c
  107. asm     mov     ah, 3Fh
  108. asm     int     21h             /* dosRead (fildes, DS:DX, CX)  */
  109.         popDS_
  110.  
  111.         return c;                   /* no error checking    */
  112. }
  113.  
  114.  
  115. /*-----------------------------------------------------------------------*
  116.  
  117. Name            __TrimCtlZ - zaps a terminating control Z
  118.  
  119. Usage           void    pascal  __TrimCtlZ  (int fildes)
  120.  
  121. Prototype in    _io.h
  122.  
  123. Description     removes a terminating control Z from a file if it is
  124.                 present.
  125.  
  126. Return value    Nothing
  127.  
  128. *-----------------------------------------------------------------------*/
  129. void pascal near __TrimCtlZ(int fildes)
  130. {
  131.         dosSeekFinalChar (fildes);
  132. asm     push    DX
  133. asm     push    AX              /* remember original position   */
  134.  
  135.         if (dosReadOne (fildes) == _ctlZ)
  136.         {
  137.                 dosSeekFinalChar (fildes);
  138.  
  139.         asm     mov     bx, fildes
  140.         asm     sub     cx, cx  /* zero length causes truncation        */
  141.         asm     sub     dx, dx
  142.         asm     mov     ah, 40h
  143.         asm     int     21h     /* dosWrite (fildes, NULL, 0)           */
  144.         }
  145.  
  146. asm     mov     bx, fildes
  147. asm     pop     dx
  148. asm     pop     cx              /* original position    */
  149. asm     mov     ax, 4200h + SEEK_SET
  150. asm     int     21h             /* DX:AX = lseek (BX, CX:DX, AL)        */
  151.  
  152.         lseek (fildes, 0L, SEEK_SET);
  153. }
  154.  
  155.  
  156. /*-----------------------------------------------------------------------*
  157.  
  158. Name            __AppendCtlZ - appends a control Z character to a file
  159.  
  160. Usage           void    pascal  __AppendCtlZ  (int fildes)
  161.  
  162. Prototype in    _io.h
  163.  
  164. Description     appends a control Z character to a file
  165.  
  166. *------------------------------------------------------------------------*/
  167. void pascal near __AppendCtlZ(int fildes)
  168. {
  169.         char    c = _ctlZ;
  170.  
  171.         dosSeekFinalChar (fildes);
  172.         if (dosReadOne (fildes) != _ctlZ)
  173.         {
  174.                 pushDS_
  175.         asm     mov     bx, fildes
  176.         asm     mov     cx, 1
  177. #if LDATA
  178.         asm     push    SS
  179.         asm     pop     DS
  180. #endif
  181.         asm     lea     dx, c
  182.         asm     mov     ah, 40h
  183.         asm     int     21h     /* dosWrite (fildes, DS:DX, CX) */
  184.                 popDS_
  185.         }
  186. }
  187.