home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / search.c < prev    next >
Text File  |  1990-05-10  |  1KB  |  43 lines

  1. /*
  2.  * Search and extract a directory structure.  The argument is the
  3.  * relative directory entry number (no sanity checking).  It returns a
  4.  * pointer to the directory structure at that location.  Attempts to
  5.  * optimize by trying to determine if the buffer needs to be re-read.
  6.  * A call to writedir() will scribble on the real buffer, so watch out!
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "msdos.h"
  11.                 /* dir_chain contains the list of sectors */
  12.                 /* that make up the current directory */
  13. extern int fd, dir_chain[25];
  14.  
  15. struct directory *
  16. search(num)
  17. int num;
  18. {
  19.     int skip, entry;
  20.     static int last;
  21.     static struct directory dirs[16];
  22.     void exit(), perror(), move();
  23.  
  24.                     /* first call disables optimzation */
  25.     if (num == 0)
  26.         last = 0;
  27.                     /* which sector */
  28.     skip = dir_chain[num / 16];
  29.                     /* don't read it if same sector */
  30.     if (skip != last) {
  31.         move(skip);
  32.                     /* read the sector */
  33.         if (read(fd, (char *) &dirs[0], MSECSIZ) != MSECSIZ) {
  34.             perror("mread: read");
  35.             exit(1);
  36.         }
  37.     }
  38.     last = skip;
  39.                     /* which entry in sector */
  40.     entry = num % 16;
  41.     return(&dirs[entry]);
  42. }
  43.