1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2000-Apr-09 or later
(the contents of which are also included in unzip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*---------------------------------------------------------------------------
macdir.c
* This file provides dirent-style directory-reading procedures
* for V7 Unix systems that don't have such procedures.
*
*
---------------------------------------------------------------------------*/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
#include <Errors.h>
#include <Files.h>
#include <Strings.h>
#include <sound.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "macdir.h"
#include "helpers.h"
#include "pathname.h"
/*****************************************************************************/
/* Functions */
/*****************************************************************************/
int closedir(DIR *dPtr)
{
free(dPtr);
return 0;
}
DIR *opendir(char *dirName)
{
int fullPath;
unsigned pathLen;
char *s;
HParamBlockRec hPB;
CInfoPBRec cPB;
DIR *dPtr;
OSErr err;
FSSpec spec;
char CompletePath[NAME_MAX];
GetCompletePath(CompletePath, dirName, &spec, &err);
printerr("GetCompletePath", err, err, __LINE__, __FILE__, dirName);
if (dirName == NULL || *dirName == '\0' ||
(pathLen = strlen(dirName)) >= 256) {
errno = EINVAL;
return NULL;
}
/* Get information about volume. */
memset(&hPB, '\0', sizeof(hPB));
if (((s = strchr(dirName, ':')) == NULL) || (*dirName == ':')) {
fullPath = false;
} else {
*(s + 1) = '\0';
hPB.volumeParam.ioVolIndex = -1;
fullPath = true;
}
hPB.volumeParam.ioNamePtr = spec.name;
err = PBHGetVInfoSync(&hPB);
if ((err != noErr) || (hPB.volumeParam.ioVFSID != 0)) {
errno = ENOENT;
return NULL;
}
/* Get information about file. */
memset(&cPB, '\0', sizeof(cPB));
if (fullPath)
cPB.hFileInfo.ioVRefNum = hPB.volumeParam.ioVRefNum;
cPB.hFileInfo.ioNamePtr = spec.name;
err = PBGetCatInfoSync(&cPB);
if (err != noErr) {
errno = (err == fnfErr) ? ENOENT : EIO;
return NULL;
}
if (!(cPB.hFileInfo.ioFlAttrib & ioDirMask)) {
errno = ENOTDIR;
return NULL;
}
/* Get space for, and fill in, DIR structure. */
if ((dPtr = (DIR *)malloc(sizeof(DIR))) == NULL) {
return NULL;
}
dPtr->ioVRefNum = cPB.dirInfo.ioVRefNum;
dPtr->ioDrDirID = cPB.dirInfo.ioDrDirID;
dPtr->ioFDirIndex = 1;
dPtr->flags = 0;
return dPtr;
}
struct dirent *readdir(DIR *dPtr)
{
struct dirent *dirPtr;
CInfoPBRec cPB;
char name[256];
OSErr err;
if (dPtr->flags) {
return NULL;
}
/* Get information about file. */
memset(&cPB, '\0', sizeof(cPB));
cPB.hFileInfo.ioNamePtr = (StringPtr)name;
cPB.hFileInfo.ioFDirIndex = dPtr->ioFDirIndex;
cPB.hFileInfo.ioVRefNum = dPtr->ioVRefNum;
cPB.hFileInfo.ioDirID = dPtr->ioDrDirID;
err = PBGetCatInfoSync(&cPB);
if (err != noErr) {
dPtr->flags = 0xff;
errno = (err == fnfErr) ? ENOENT : EIO;
return NULL;
}
p2cstr((StringPtr)name);
dirPtr = &dPtr->currEntry;
dirPtr->d_fileno = dPtr->ioFDirIndex++;
dirPtr->d_namlen = strlen(name);
strcpy(dirPtr->d_name, name);
dirPtr->d_reclen = sizeof(struct dirent) - sizeof(dirPtr->d_name) +
dirPtr->d_namlen;
return dirPtr;
}
|