home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / maclibunix / opendir.c < prev    next >
Encoding:
Text File  |  1994-05-06  |  1.8 KB  |  82 lines  |  [TEXT/R*ch]

  1. BRec d;
  2.         VolumeParam v;
  3.     } pb;
  4.     char ppath[MAXPATH];
  5.     short err;
  6.     
  7.     if (opened.nextfile != 0) {
  8.         errno = EBUSY;
  9.         return NULL; /* A directory is already open. */
  10.     }
  11.     strncpy(ppath+1, path, ppath[0]= strlen(path));
  12.     pb.d.ioNamePtr= (unsigned char *)ppath;
  13.     pb.d.ioVRefNum= 0;
  14.     if (hfsrunning()) {
  15.         pb.d.ioWDProcID= 0;
  16.         pb.d.ioWDDirID= 0;
  17.         err= PBOpenWD((WDPBPtr)&pb, FALSE);
  18.     }
  19.     else {
  20.         pb.v.ioVolIndex= 0;
  21.         err= PBGetVInfo((ParmBlkPtr)&pb, FALSE);
  22.     }
  23.     if (err != noErr) {
  24.         errno = ENOENT;
  25.         return NULL;
  26.     }
  27.     opened.dirid= pb.d.ioVRefNum;
  28.     opened.nextfile= 1;
  29.     return &opened;
  30. }
  31.  
  32. /*
  33.  * Close a directory.
  34.  */
  35.  
  36. void
  37. closedir(dirp)
  38.     DIR *dirp;
  39. {
  40.     if (hfsrunning()) {
  41.         WDPBRec pb;
  42.         
  43.         pb.ioVRefNum= dirp->dirid;
  44.         (void) PBCloseWD(&pb, FALSE);
  45.     }
  46.     dirp->dirid= 0;
  47.     dirp->nextfile= 0;
  48. }
  49.  
  50. /*
  51.  * Read the next directory entry.
  52.  */
  53.  
  54. struct direct *
  55. readdir(dp)
  56.     DIR *dp;
  57. {
  58.     union {
  59.         DirInfo d;
  60.         FileParam f;
  61.         HFileInfo hf;
  62.     } pb;
  63.     short err;
  64.     static struct direct dir;
  65.     
  66.     dir.d_name[0]= 0;
  67.     pb.d.ioNamePtr= (unsigned char *)dir.d_name;
  68.     pb.d.ioVRefNum= dp->dirid;
  69.     pb.d.ioFDirIndex= dp->nextfile++;
  70.     pb.d.ioDrDirID= 0;
  71.     if (hfsrunning())
  72.         err= PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
  73.     else
  74.         err= PBGetFInfo((ParmBlkPtr)&pb, FALSE);
  75.     if (err != noErr) {
  76.         errno = EIO;
  77.         return NULL;
  78.     }
  79.     (void) p2cstr((unsigned char *)dir.d_name);
  80.     return &dir;
  81. }
  82.