home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1993, 1994 Marc Parmet.
- * This file is part of the Macintosh port of GNU Emacs.
- *
- * GNU Emacs is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
- #if defined(THINK_C)
- #include <MacHeaders>
- #else
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <Files.h>
- #include <Errors.h>
- #endif
-
- #include "sys/dir.h"
-
- DIR *
- opendir_internal(char *filename)
- {
- short err;
- FSSpec fs;
- CInfoPBRec pb;
- DIR *dirp = 0L;
-
- dirp = (DIR *)NewPtr(sizeof(DIR));
- err = MemError();
- if (err) goto error;
-
- err = unixfn2FSSpec_internal(filename,&fs,0);
- if (err == nsDrvErr) {
- // Want to look at root. Need to prepare a list of all volumes.
- dirp->dd_volumes = 1;
- dirp->dd_index = 0;
- return dirp;
- }
- else if (err)
- goto error;
- else {
- pb.dirInfo.ioFDirIndex = 0;
- pb.dirInfo.ioNamePtr = fs.name;
- pb.dirInfo.ioVRefNum = fs.vRefNum;
- pb.dirInfo.ioDrDirID = fs.parID;
- err = PBGetCatInfo(&pb,0);
- if (err) goto error;
- if (!(pb.dirInfo.ioFlAttrib & 0x10)) goto error; // Not a directory
-
- dirp->dd_volumes = 0;
- dirp->dd_vRefNum = fs.vRefNum;
- dirp->dd_drDirID = pb.dirInfo.ioDrDirID;
- dirp->dd_index = -1;
- }
-
- return dirp;
-
- error:
- set_errno(err);
- if (dirp) DisposPtr((Ptr)dirp);
- return 0L;
- }
-
- int
- closedir_internal(DIR *dirp)
- {
- DisposPtr((Ptr)dirp);
- return 0;
- }
-
- struct direct *
- readdir_internal(DIR *dirp)
- {
- short err;
- CInfoPBRec pb1;
- HParamBlockRec pb2;
- static struct direct result;
-
- if (dirp->dd_volumes) {
- if (dirp->dd_index == 0) {
- strcpy(result.d_name,".");
- ++dirp->dd_index;
- }
- else {
- pb2.volumeParam.ioVolIndex = dirp->dd_index++;
- pb2.volumeParam.ioNamePtr = (unsigned char *)result.d_name;
- err = PBHGetVInfo(&pb2,0);
- if (err) { set_errno(err); return 0L; };
- PtoCstr((unsigned char *)result.d_name);
- }
- }
- else {
- if (dirp->dd_index == -1) {
- strcpy(result.d_name,"..");
- ++dirp->dd_index;
- }
- else if (dirp->dd_index == 0) {
- strcpy(result.d_name,".");
- ++dirp->dd_index;
- }
- else {
- pb1.hFileInfo.ioVRefNum = dirp->dd_vRefNum;
- pb1.hFileInfo.ioDirID = dirp->dd_drDirID;
- pb1.hFileInfo.ioFDirIndex = dirp->dd_index++;
- pb1.hFileInfo.ioNamePtr = (unsigned char *)result.d_name;
- err = PBGetCatInfo(&pb1,0);
- if (err) { set_errno(err); return 0L; };
- PtoCstr((unsigned char *)result.d_name);
- }
- }
-
- result.d_namlen = strlen(result.d_name);
- slashes_to_colons(result.d_name,result.d_namlen);
- result.d_ino = 1;
- return &result;
- }
-