home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / mac / macdir.c < prev    next >
C/C++ Source or Header  |  1996-01-03  |  3KB  |  141 lines

  1. #include    <Errors.h>
  2. #include    <Files.h>
  3. #ifndef THINK_C
  4. #include    <Strings.h>
  5. #endif
  6.  
  7. #ifndef FSFCBLen
  8. #define FSFCBLen    (*(short *)0x3F6)
  9. #endif
  10.  
  11. #include    <errno.h>
  12. #include    <stdlib.h>
  13. #include    <string.h>
  14.  
  15. #include    "macdir.h"
  16.  
  17. int closedir(dPtr) DIR *dPtr; {
  18.     free(dPtr);
  19.  
  20.     return 0;
  21. }
  22.  
  23. DIR *opendir(dirName) char *dirName; {
  24.     int fullPath, pathLen;
  25.     char *s, pName[256];
  26.     HParamBlockRec hPB;
  27.     CInfoPBRec cPB;
  28.     DIR *dPtr;
  29.     OSErr err;
  30.  
  31.     if (dirName == NULL || *dirName == '\0' || (pathLen = strlen(dirName)) > 255) {
  32.         errno = EINVAL;
  33.         return NULL;
  34.     }
  35.  
  36.     if (FSFCBLen <= 0) {
  37.         errno = ENOTDIR;
  38.         return NULL;
  39.     }
  40.  
  41.     /* Get information about volume. */
  42.  
  43.     memset(&hPB, '\0', sizeof(hPB));
  44.  
  45.     strcpy(pName, dirName);
  46.  
  47.     if (((s = strchr(pName, ':')) == NULL) || (*pName == ':')) {
  48.         fullPath = false;
  49.     } else {
  50.         *(s + 1) = '\0';
  51.         c2pstr(pName);
  52.         hPB.volumeParam.ioVolIndex = -1;
  53.         fullPath = true;
  54.     }
  55.  
  56.     hPB.volumeParam.ioNamePtr = (StringPtr)pName;
  57.  
  58.     err = PBHGetVInfo(&hPB, 0);
  59.  
  60.     if ((err != noErr) || (hPB.volumeParam.ioVFSID != 0)) {
  61.         errno = ENOENT;
  62.         return NULL;
  63.     }
  64.  
  65.     /* Get information about file. */
  66.  
  67.     memset(&cPB, '\0', sizeof(cPB));
  68.  
  69.     strcpy(pName, dirName);
  70.     c2pstr(pName);
  71.  
  72.     if (fullPath)
  73.         cPB.hFileInfo.ioVRefNum = hPB.volumeParam.ioVRefNum;
  74.  
  75.     cPB.hFileInfo.ioNamePtr = (StringPtr)pName;
  76.  
  77.     err = PBGetCatInfo(&cPB, false);
  78.  
  79.     if (err != noErr) {
  80.         errno = (err == fnfErr) ? ENOENT : EIO;
  81.         return NULL;
  82.     }
  83.  
  84.     if (!(cPB.hFileInfo.ioFlAttrib & ioDirMask)) {
  85.         errno = ENOTDIR;
  86.         return NULL;
  87.     }
  88.  
  89.     /* Get space for, and fill in, DIR structure. */
  90.  
  91.     if ((dPtr = (DIR *)malloc(sizeof(DIR))) == NULL) {
  92.         return NULL;
  93.     }
  94.  
  95.     dPtr->ioVRefNum = cPB.dirInfo.ioVRefNum;
  96.     dPtr->ioDrDirID = cPB.dirInfo.ioDrDirID;
  97.     dPtr->ioFDirIndex = 1;
  98.     dPtr->flags = 0;
  99.  
  100.     return dPtr;
  101. }
  102.  
  103. struct dirent *readdir(dPtr) DIR *dPtr; {
  104.     struct dirent *dirPtr;
  105.     CInfoPBRec cPB;
  106.     char name[32];
  107.     OSErr err;
  108.  
  109.     if (dPtr->flags) {
  110.         return NULL;
  111.     }
  112.  
  113.     /* Get information about file. */
  114.  
  115.     memset(&cPB, '\0', sizeof(cPB));
  116.  
  117.     cPB.hFileInfo.ioNamePtr = (StringPtr)name;
  118.     cPB.hFileInfo.ioFDirIndex = dPtr->ioFDirIndex;
  119.     cPB.hFileInfo.ioVRefNum = dPtr->ioVRefNum;
  120.     cPB.hFileInfo.ioDirID = dPtr->ioDrDirID;
  121.  
  122.     err = PBGetCatInfo(&cPB, false);
  123.  
  124.     if (err != noErr) {
  125.         dPtr->flags = 0xff;
  126.         errno = (err == fnfErr) ? ENOENT : EIO;
  127.         return NULL;
  128.     }
  129.  
  130.     p2cstr((StringPtr)name);
  131.  
  132.     dirPtr = &dPtr->currEntry;
  133.  
  134.     dirPtr->d_fileno = dPtr->ioFDirIndex++;
  135.     dirPtr->d_namlen = strlen(name);
  136.     strcpy(dirPtr->d_name, name);
  137.     dirPtr->d_reclen = sizeof(struct dirent) - sizeof(dirPtr->d_name) + dirPtr->d_namlen;
  138.  
  139.     return dirPtr;
  140. }
  141.