update dev300-m58
[ooovba.git] / dmake / mac / directry.c
blob537b1e3170e6a47e259697e1b49d3f55930704af
1 /* RCS $Id: directry.c,v 1.1.1.1 2000-09-22 15:33:27 hr Exp $
2 --
3 -- SYNOPSIS
4 -- Fake directory and file functions for the Mac
5 --
6 -- DESCRIPTION
7 -- This file contains implementations for some ANSI standard routines dmake
8 -- uses which are not otherwise available for the mac.
9 --
10 -- Assume we are using at least 128K ROMS.
12 -- AUTHOR
13 -- Dennis Vadura, dvadura@dmake.wticorp.com
16 -- WWW
17 -- http://dmake.wticorp.com/
19 -- COPYRIGHT
20 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
21 --
22 -- This program is NOT free software; you can redistribute it and/or
23 -- modify it under the terms of the Software License Agreement Provided
24 -- in the file <distribution-root>/readme/license.txt.
26 -- LOG
27 -- Use cvs log to obtain detailed change logs.
30 #include <Errors.h>
31 #include <Files.h>
32 #include <OSUtils.h>
33 #include <StdLib.h>
34 #include <Strings.h>
36 * We now include LowMem.h instead of SysEqu.h as LowMem.h is what Apple recommends
37 * we use.
39 #include <LowMem.h>
40 #include "extern.h"
45 * Implementation of stat function for dmake on the mac.
47 * Many fields aren't filled in, and the times are seconds from 1/1//1904,
48 * but it should be enough for dmake (I think we only need st_mtime and
49 * st_mode's S_IFDIR set correctly).
51 PUBLIC int
52 stat(pPath, pStat)
53 char *pPath;
54 struct stat *pStat;
56 CInfoPBRec infoPB;
57 OSErr err;
58 int retVal;
60 infoPB.hFileInfo.ioCompletion = NULL;
61 infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
62 infoPB.hFileInfo.ioVRefNum = 0;
63 infoPB.hFileInfo.ioFDirIndex = 0;
64 infoPB.hFileInfo.ioDirID = 0;
65 err = PBGetCatInfo(&infoPB, FALSE);
66 p2cstr ((StringPtr) pPath);
68 if (err == noErr) {
69 pStat->st_mtime = (time_t) infoPB.hFileInfo.ioFlMdDat;
70 pStat->st_ctime = (time_t) infoPB.hFileInfo.ioFlCrDat;
71 pStat->st_mode = S_IREAD | S_IEXEC;
73 /* If it is a directory ... */
74 if (infoPB.hFileInfo.ioFlAttrib & 0x10) {
75 pStat->st_size = infoPB.dirInfo.ioDrNmFls;
76 pStat->st_mode |= S_IFDIR;
77 } else {
78 pStat->st_size = infoPB.hFileInfo.ioFlLgLen;
79 pStat->st_mode |= S_IFREG;
80 } /* if ... else */
82 /* If it is writeable */
83 if ((infoPB.hFileInfo.ioFlAttrib & 0x1) == 0) {
84 pStat->st_mode |= S_IWRITE;
85 } /* if */
87 retVal = 0;
89 } else {
90 retVal = -1;
91 } /* if ... else */
93 return (retVal);
94 } /* PUBLIC int stat () */
99 * Return the current working directory, or NULL if there is an error.
101 PUBLIC char *
102 getcwd(char *pPath, size_t pathSize)
104 DirInfo dirInfo;
105 OSErr err;
106 Str255 dirName;
107 char *pBeginName;
108 char *pC;
109 size_t len;
110 size_t spaceForColon;
112 /* Set up the info for the PBGetCatInfo() calls */
113 dirInfo.ioCompletion = NULL;
114 dirInfo.ioNamePtr = dirName;
115 dirInfo.ioVRefNum = 0;
116 dirInfo.ioFDirIndex = -1;
117 dirInfo.ioDrDirID = 0;
118 pBeginName = pPath + pathSize - 1;
119 spaceForColon = 0; /* Make sure we don't have an end colon on the name */
122 * Keep going up the directory path until the end is reached or an error
123 * occurs. Ideally, we would check for errors at every level and stop
124 * when we received an fnfErr (File Not Found), but it appears that there
125 * are some problems with network volumes. (During testing, I received
126 * a paramErr (No Default Volume) beyond the top level.) Thus, to keep it
127 * simple, I assume any error past the first directory indicates we have
128 * seen all directories.
130 while (TRUE) {
131 err = PBGetCatInfo ((CInfoPBPtr) &dirInfo, FALSE);
132 len = ((size_t)(unsigned char) dirName[0]);
133 if ((err == noErr) && (len < pBeginName - pPath)) {
134 p2cstr (dirName);
135 pBeginName -= len + spaceForColon;
136 strcpy (pBeginName, (char *)dirName);
137 /* Note that strcpy() adds the '\0' at the end of
138 the first directory for us */
139 if (spaceForColon == 1) {
140 pBeginName[len] = ':';
141 } else {
142 /* The end of the string shouldn't have a ':' */
143 spaceForColon = 1;
144 } /* if */
146 /* Set up for the next call to PBGetCatInfo() with
147 the parent's directory ID */
148 dirInfo.ioDrDirID = dirInfo.ioDrParID;
150 } else if (spaceForColon == 1) {
151 /* We got past the top-level directory */
152 break;
154 } else {
155 /* We either have an error when looking at the first directory
156 or have run out of room. */
157 return (NULL);
158 } /* if ... elses */
159 } /* while */
161 /* Now copy the directory string to the beginning of the path string.
162 (It's possible the directory already starts at the beginning of the
163 string, but this is unlikely and doesn't hurt anything if it does,
164 so we don't bother to check for it.) */
165 pC = pPath;
166 while ((*(pC++) = *(pBeginName++)) != '\0')
169 return (pPath);
170 } /* PUBLIC char *getcwd () */
175 * Change the directory to a new default directory.
177 * Return 0 if successful, or -1 if there is an error.
179 PUBLIC int
180 chdir(char *pPath)
182 WDPBRec WDPB;
183 VolumeParam vParam;
184 OSErr err;
185 int result;
186 char *pC;
187 char c;
189 /* Set up the directory */
190 c2pstr (pPath);
191 WDPB.ioCompletion = NULL;
192 WDPB.ioNamePtr = (unsigned char *)pPath;
193 WDPB.ioVRefNum = 0;
194 WDPB.ioWDProcID = 0;
195 WDPB.ioWDDirID = 0;
196 err = PBOpenWD (&WDPB, FALSE);
197 /* Restore path to a C-type string in case the caller wants
198 to use it after this call. */
199 p2cstr ((unsigned char *)pPath);
200 if (err != noErr) {
201 return (-1);
202 } /* if */
204 /* Set up the volume if necessary */
205 if (*pPath != ':') {
206 for (pC = pPath + 1; (*pC != ':') && (*pC != '\0'); ++pC)
208 c = *pC;
209 *pC = '\0';
210 vParam.ioCompletion = NULL;
211 vParam.ioNamePtr = c2pstr (pPath);
212 vParam.ioVRefNum = WDPB.ioVRefNum;
213 err = PBSetVol ((ParmBlkPtr) &vParam, FALSE);
214 p2cstr ((unsigned char *)pPath);
215 *pC = c;
216 result = ((err == noErr) ? 0 : -1);
218 } else {
219 result = 0;
220 } /* if ... else */
222 return (result);
223 } /* PUBLIC int chdir () */
228 * Change the modification time for the file to the current time.
230 * The normal version of utime can set the modification time to any
231 * time, this function aborts the function if this is tried.
233 * We return 0 if the modification time was updated and -1 if there
234 * was an error.
236 PUBLIC int
237 utime(char *pPath, time_t *pTimes)
239 CInfoPBRec infoPB;
240 OSErr err;
242 if (pTimes != NULL) {
243 Fatal ("SUBROUTINE SHORTCOMING: utime cannot take a utimbuf struct");
244 } /* if */
246 /* Get the old info */
247 infoPB.hFileInfo.ioCompletion = NULL;
248 infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
249 infoPB.hFileInfo.ioVRefNum = 0;
250 infoPB.hFileInfo.ioFDirIndex = 0;
251 infoPB.hFileInfo.ioDirID = 0;
252 err = PBGetCatInfo (&infoPB, FALSE);
253 if (err != noErr) {
254 p2cstr ((StringPtr) pPath);
255 return (-1);
256 } /* if */
258 /* Change the modification time and set the new info */
259 GetDateTime (&(infoPB.hFileInfo.ioFlMdDat));
260 infoPB.hFileInfo.ioDirID = 0;
261 err = PBSetCatInfo (&infoPB, FALSE);
262 p2cstr ((StringPtr) pPath);
263 return ((err == noErr) ? 0 : -1);
264 } /* PUBLIC int utime () */