home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / mdcopy / part02 / mdseek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-02  |  1.6 KB  |  79 lines

  1. /*
  2.  * Created 1989 by greg yachuk.  Placed in the public domain.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #ifdef    MSDOS
  8. #include <io.h>
  9. #endif
  10.  
  11. #include "mdiskio.h"
  12.  
  13. /*
  14.  * mdtell -    tell the current offset from the beginning of the file.
  15.  *        Note:    this is relative to the file segment ON THE
  16.  *            CURRENT DISKETTE.
  17.  */
  18. long
  19. mdtell(mdp)
  20. MDFILE *mdp;
  21. {
  22.     if (mdp->buffer == NULL)
  23.         return (tell(mdp->fid));
  24.  
  25.     if (mdp->flags & _MDWRITE)
  26.         return (tell(mdp->fid) + mdp->bufptr - mdp->buffer);
  27.  
  28.     return (tell(mdp->fid) - mdp->bufcnt);
  29. }
  30.  
  31. /*
  32.  * mdseek -    set the current position relative to the beginning of
  33.  *        the file, the current position, or end of file.
  34.  *        return the new position relative to the beginning of file.
  35.  *
  36.  *        Note:    You cannot seek across diskette boundaries.
  37.  */
  38. long
  39. mdseek(mdp, c, orig)
  40. MDFILE *mdp;
  41. long    c;
  42. int    orig;
  43. {
  44.     long    posn = 0;
  45.  
  46.     /* just return raw position, if we haven't buffered anything */
  47.     if (mdp->buffer == NULL)
  48.         return (lseek(mdp->fid, c, orig) == -1L);
  49.  
  50.     /* find out the current (logical) location */
  51.     if (orig == SEEK_CUR)
  52.     {
  53.         /* if we stay in the buffer, use this short cut */
  54.         if (mdp->buffer <= mdp->bufptr+c && c <= mdp->bufcnt)
  55.         {
  56.             mdp->bufptr += c;
  57.             mdp->bufcnt -= c;
  58.             /* reset the EOF flag, just in case */
  59.             mdp->flags &= ~_MDEOF;
  60.             return (0);
  61.         }
  62.  
  63.         posn = mdtell(mdp);
  64.     }
  65.  
  66.     /* clear out the buffer.  write it out if necessary */
  67.     mdflush(mdp);
  68.  
  69.     /* go to the current (logical) location */
  70.     if (orig == SEEK_CUR)
  71.         lseek(mdp->fid, posn, SEEK_SET);
  72.  
  73.     /* reset the EOF flag, since we are seeking somewhere */
  74.     mdp->flags &= ~_MDEOF;
  75.  
  76.     /* seek to the new position */
  77.     return(lseek(mdp->fid, c, orig) == -1L);
  78. }
  79.