1 /* RCS $Id: directry.c,v 1.1.1.1 2000-09-22 15:33:27 hr Exp $
4 -- Fake directory and file functions for the Mac
7 -- This file contains implementations for some ANSI standard routines dmake
8 -- uses which are not otherwise available for the mac.
10 -- Assume we are using at least 128K ROMS.
13 -- Dennis Vadura, dvadura@dmake.wticorp.com
17 -- http://dmake.wticorp.com/
20 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
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.
27 -- Use cvs log to obtain detailed change logs.
36 * We now include LowMem.h instead of SysEqu.h as LowMem.h is what Apple recommends
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).
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
);
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
;
78 pStat
->st_size
= infoPB
.hFileInfo
.ioFlLgLen
;
79 pStat
->st_mode
|= S_IFREG
;
82 /* If it is writeable */
83 if ((infoPB
.hFileInfo
.ioFlAttrib
& 0x1) == 0) {
84 pStat
->st_mode
|= S_IWRITE
;
94 } /* PUBLIC int stat () */
99 * Return the current working directory, or NULL if there is an error.
102 getcwd(char *pPath
, size_t pathSize
)
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.
131 err
= PBGetCatInfo ((CInfoPBPtr
) &dirInfo
, FALSE
);
132 len
= ((size_t)(unsigned char) dirName
[0]);
133 if ((err
== noErr
) && (len
< pBeginName
- pPath
)) {
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
] = ':';
142 /* The end of the string shouldn't have a ':' */
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 */
155 /* We either have an error when looking at the first directory
156 or have run out of room. */
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.) */
166 while ((*(pC
++) = *(pBeginName
++)) != '\0')
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.
189 /* Set up the directory */
191 WDPB
.ioCompletion
= NULL
;
192 WDPB
.ioNamePtr
= (unsigned char *)pPath
;
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
);
204 /* Set up the volume if necessary */
206 for (pC
= pPath
+ 1; (*pC
!= ':') && (*pC
!= '\0'); ++pC
)
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
);
216 result
= ((err
== noErr
) ? 0 : -1);
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
237 utime(char *pPath
, time_t *pTimes
)
242 if (pTimes
!= NULL
) {
243 Fatal ("SUBROUTINE SHORTCOMING: utime cannot take a utimbuf struct");
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
);
254 p2cstr ((StringPtr
) pPath
);
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 () */