home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / FILEACC.C < prev    next >
Text File  |  1996-08-27  |  15KB  |  223 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   fileacc.c  (name changed 7/25/89 from exefile.c)                        */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*   File handling functions for debug info file.                            */
  7. /*                                                                           */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   02/08/91 Creation of 32-bit SD86, from 16-bit version.                  */
  12. /*                                                                           */
  13. /*...16->32 port.                                                            */
  14. /*...                                                                        */
  15. /*... 05/14/91  105   Christina port to 32 bit.                              */
  16. /*...                                                                        */
  17. /*...Release 1.00 (Pre-release 108 12/05/91)                                 */
  18. /*...                                                                        */
  19. /*... 02/21/92  521   Srinivas  Port to C-Set/2.                             */
  20. /**Includes*******************************************************************/
  21.  
  22. #include "all.h"                        /* SD86 include files                */
  23. /*****************************************************************************/
  24. /* opendos()                                                                 */
  25. /*                                                                           */
  26. /* Description:  The fopen() function is mimicked by using the DosOpen       */
  27. /*    function. Fopen() is not used because it sets a limit on the number of */
  28. /*    files that can be open at one time. The number of files can be set by  */
  29. /*    the DosSetMaxFH function, and this is done in SD86.C. The only         */
  30. /*    conversion that needs to be done is interpreting the attr string (which*/
  31. /*    is the open mode that is usually passed to fopen, ex. "r+").           */
  32. /*                                                                           */
  33. /* Parameters:                                                               */
  34. /*    filename      name of the file to open                                 */
  35. /*    attr          open mode for file                                       */
  36. /*    FileHandle    handle that will be used to operate on this file         */
  37. /*                                                                           */
  38. /* Return:                                                                   */
  39. /*    zero if no error, non-zero if error                                    */
  40. /*                                                                           */
  41. /*****************************************************************************/
  42. /* translate a function that simulates stream i/o into a dos function */
  43. int opendos(char *filename,char *attr,HFILE *FileHandle)                /*105*/
  44. {                                                                       /*105*/
  45.                                         /*                                   */
  46.    ULONG ActionTaken;                   /* action taken on file           105*/
  47.    ULONG FileSize = 800L;               /* size of file to open; ignored  105*/
  48.                                         /* if the file already exists        */
  49.    ULONG FileAttr = FILE_ARCHIVED;      /* attributes chosen for the file 105*/
  50.    ULONG OpenFlag;                      /* to handle new/existing files   105*/
  51.    ULONG OpenMode;                      /* opening for read,write, append 105*/
  52.    LONG Distance = 0L;                  /* distance from EOF to write     105*/
  53.    ULONG NewPtr;                        /* new -> location after seek     105*/
  54.    int rc;                              /* error code                     105*/
  55.  
  56.    OpenMode = OPEN_FLAGS_NOINHERIT;                                     /*105*/
  57.     /* the flag constants are defined in <stdio.h>, the others in <bsedos.h> */
  58.    if (*attr == 'r')                                                    /*105*/
  59.        {
  60.        OpenFlag = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS; /*105*/
  61.        }
  62.    else if (*attr == 'w')                                               /*105*/
  63.        {
  64.        OpenFlag = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
  65.        }
  66.    else if (*attr == 'a')                                               /*105*/
  67.        {
  68.        OpenFlag = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  69.        }
  70.    else /* this is an error - first char must be r,w, or a */           /*105*/
  71.        return 87;                                                       /*105*/
  72.  
  73.    if ((*(attr+1) == '+') || (*(attr+2) == '+'))                        /*105*/
  74.        {
  75.        OpenMode = OpenMode | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE;
  76.        }
  77.    else
  78.        if (*attr == 'r')                                                /*105*/
  79.            OpenMode = OpenMode | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY;
  80.        else                             /* write or append operation      105*/
  81.            OpenMode = OpenMode | OPEN_SHARE_DENYREAD | OPEN_ACCESS_WRITEONLY;
  82.  
  83.    rc = DosOpen(filename,FileHandle,&ActionTaken,FileSize,FileAttr,
  84.                 OpenFlag,OpenMode,NULL);                                /*105*/
  85.  
  86.    if (rc != NO_ERROR)                                                  /*105*/
  87.        return rc;                                                       /*105*/
  88.  
  89.    /* if this was an append to an existing file, move the file ptr           */
  90.    if ((*attr == 'a') && (ActionTaken == FILE_EXISTED))                 /*105*/
  91.        rc = DosChgFilePtr(*FileHandle,Distance,SEEK_END,&NewPtr);       /*105*/
  92.  
  93.    return rc;                                                           /*105*/
  94. }
  95.  
  96. /*****************************************************************************/
  97. /* closedos()                                                                */
  98. /*                                                                           */
  99. /* Description:                                                              */
  100. /*   mimic fclose() with DosClose, using a file handle. See description of   */
  101. /*   opendos() for why.                                                      */
  102. /*                                                                           */
  103. /* Parameters:                                                               */
  104. /*    FileHandle     file handle for file to close                           */
  105. /*                                                                           */
  106. /* Return: zero if no error, non-zero if error                               */
  107. /*                                                                           */
  108. /*****************************************************************************/
  109. int closedos(HFILE FileHandle)                                          /*105*/
  110. {
  111.     return(DosClose(FileHandle));                                       /*105*/
  112. }
  113.  
  114. /*****************************************************************************/
  115. /* readdos()                                                                 */
  116. /*                                                                           */
  117. /* Description:                                                              */
  118. /*    mimic the fread() function using DosRead with file handles. See        */
  119. /*    description of OpenDos as to why.                                      */
  120. /*                                                                           */
  121. /* Parameters:                                                               */
  122. /*    buffer     the buffer to put the bytes read into                       */
  123. /*    size       what size of chunks to get                                  */
  124. /*    count      how many chunks to get                                      */
  125. /*    FileHandle file handle for this file                                   */
  126. /*                                                                           */
  127. /* Return:                                                                   */
  128. /*   zero if no error, non-zero if an error. Does NOT return the number of   */
  129. /*   bytes read to the caller, but this is in BytesRead if you want it.      */
  130. /*                                                                           */
  131. /*****************************************************************************/
  132. int readdos(void *buffer,int size,int count,HFILE FileHandle)           /*105*/
  133. {
  134.     ULONG BytesRead;                    /* no of bytes read, should be    105*/
  135.                                         /* same as size * count           105*/
  136.     return(DosRead(FileHandle,buffer,size*count,&BytesRead));           /*105*/
  137. }
  138.  
  139. /*****************************************************************************/
  140. /* seekdos()                                                                 */
  141. /*                                                                           */
  142. /* Description:                                                              */
  143. /*   mimics fseek() function using DosChgFilePtr with file handles. See      */
  144. /*   description of OpenDos above to see why.                                */
  145. /*                                                                           */
  146. /* Parameters:                                                               */
  147. /*   FileHandle   file handle for file to seek in                            */
  148. /*   offset       offset to seek to from 'origin'                            */
  149. /*   origin       start at this point (current, end, or beginning) and       */
  150. /*                add offset                                                 */
  151. /*                                                                           */
  152. /* Return: zero if no error, non-zero if error.                              */
  153. /*                                                                           */
  154. /*****************************************************************************/
  155. int seekdos(HFILE FileHandle,long offset,int origin)                    /*105*/
  156. {
  157.     ULONG Local;                                                        /*105*/
  158.  
  159.     return(DosChgFilePtr(FileHandle,offset,origin,&Local));             /*105*/
  160. }
  161.  
  162. /*****************************************************************************/
  163. /* seekf()                                                                   */
  164. /*                                                                           */
  165. /* Description:                                                              */
  166. /*  seek file location.                                                      */
  167. /*                                                                           */
  168. /* Parameters:                                                               */
  169. /*  pdf         debug file pointer.                                          */
  170. /*  offset      file offset we're seeking.                                   */
  171. /*                                                                           */
  172. /* Return:                                                                   */
  173. /*   void                                                                    */
  174. /*                                                                           */
  175. /*****************************************************************************/
  176.   void
  177. seekf(DEBFILE *pdf,ulong offset)
  178. {
  179.  HFILE  FileHandle;                     /* file struct for this debug file105*/
  180.  int    rc = 0;                         /* return code. assume success.      */
  181.  ULONG    Local;
  182.  FileHandle = pdf->DebFilePtr->fh;      /* -> file structure for this pdf 105*/
  183.  rc = DosChgFilePtr(FileHandle,         /* pointer to the debug info file 105*/
  184.              offset,                    /* where to seek                     */
  185.              SEEK_SET,                  /* offset from file beginning        */
  186.              &Local);                   /* returns addr of new -> location105*/
  187.  if ( rc )
  188.    Error(ERR_FILE_READ,TRUE,1,pdf->DebFilePtr->fn);
  189.  return;                                /* return.                           */
  190. }                                       /* end of seekf()                    */
  191.  
  192. /*****************************************************************************/
  193. /* readf()                                                                   */
  194. /*                                                                           */
  195. /* Description:                                                              */
  196. /*  read a buffer from file.                                                 */
  197. /*                                                                           */
  198. /* Parameters:                                                               */
  199. /*  bp          buffer pointer where caller wants the bytes stuffed.         */
  200. /*  num         number of bytes the caller wants to read.                    */
  201. /*  fp          file pointer.                                                */
  202. /*                                                                           */
  203. /* Return:                                                                   */
  204. /*   void                                                                    */
  205. /*                                                                           */
  206. /*****************************************************************************/
  207.   uint
  208. readf(uchar *bp,uint num,DEBFILE *pdf)
  209. {
  210.  HFILE  FileHandle;                     /* file struct for this debug file105*/
  211.  uint   bytes = 0;                      /* bytes returned.                105*/
  212.  uint   rc=0;                           /* return code.                      */
  213.  
  214.  FileHandle = pdf->DebFilePtr->fh;      /* -> file structure for this pdf 105*/
  215.  rc = DosRead( FileHandle,              /* file handle                    105*/
  216.                  bp,                    /* buffer                         105*/
  217.                 num,                    /* size                           105*/
  218.                 (ulong *)&bytes );      /* number of bytes read           521*/
  219.  if ( (rc != 0) || (bytes != num) )
  220.    Error(ERR_FILE_READ,TRUE,1,pdf->DebFilePtr->fn);
  221.  return(rc);
  222. }                                       /* end of readf()                    */
  223.