Bump version to 0.9.1.
[python/dscho.git] / Mac / Compat / macstat.c
blobee2c099dd7bbd5d67ba321991d53e25f5b67ef5d
1 /* Minimal 'stat' emulation: tells directories from files and
2 gives length and mtime.
3 Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
4 Updated to give more info, August 1994.
5 */
7 #include "macstat.h"
8 #include "macdefs.h"
10 /* Bits in ioFlAttrib: */
11 #define LOCKBIT (1<<0) /* File locked */
12 #define DIRBIT (1<<4) /* It's a directory */
14 int
15 macstat(path, buf)
16 char *path;
17 struct macstat *buf;
19 union {
20 DirInfo d;
21 FileParam f;
22 HFileInfo hf;
23 } pb;
24 short err;
26 pb.d.ioNamePtr = (unsigned char *)Pstring(path);
27 pb.d.ioVRefNum = 0;
28 pb.d.ioFDirIndex = 0;
29 pb.d.ioDrDirID = 0;
30 pb.f.ioFVersNum = 0; /* Fix found by Timo! See Tech Note 102 */
31 err = PBGetCatInfoSync((CInfoPBPtr)&pb);
32 if (err != noErr) {
33 errno = ENOENT;
34 return -1;
36 if (pb.d.ioFlAttrib & LOCKBIT)
37 buf->st_mode = 0444;
38 else
39 buf->st_mode = 0666;
40 if (pb.d.ioFlAttrib & DIRBIT) {
41 buf->st_mode |= 0111 | S_IFDIR;
42 buf->st_size = pb.d.ioDrNmFls;
43 buf->st_rsize = 0;
45 else {
46 buf->st_mode |= S_IFREG;
47 if (pb.f.ioFlFndrInfo.fdType == 'APPL')
48 buf->st_mode |= 0111;
50 buf->st_ino = pb.hf.ioDirID;
51 buf->st_nlink = 1;
52 buf->st_uid = 1;
53 buf->st_gid = 1;
54 buf->st_size = (buf->st_mode & S_IFDIR) ? 0 : pb.f.ioFlLgLen;
55 buf->st_mtime = buf->st_atime = pb.f.ioFlMdDat;
56 buf->st_ctime = pb.f.ioFlCrDat;
57 buf->st_rsize = (buf->st_mode & S_IFDIR) ? 0 : pb.f.ioFlRLgLen;
58 *(unsigned long *)buf->st_type =
59 (buf->st_mode & S_IFDIR) ? 0 : pb.f.ioFlFndrInfo.fdType;
60 *(unsigned long *)buf->st_creator =
61 (buf->st_mode & S_IFDIR) ? 0 : pb.f.ioFlFndrInfo.fdCreator;
62 return 0;