blob: e64505086bd116841a7d3ea24d1cbd2332b3e1b9 [file] [log] [blame]
Guido van Rossume7834441994-08-26 09:09:48 +00001/* 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*/
5
6#include "stat.h"
7#include "macdefs.h"
8
9/* Bits in ioFlAttrib: */
10#define LOCKBIT (1<<0) /* File locked */
11#define DIRBIT (1<<4) /* It's a directory */
12
13int
14stat(path, buf)
15 char *path;
16 struct stat *buf;
17{
18 union {
19 DirInfo d;
20 FileParam f;
21 HFileInfo hf;
22 } pb;
23 char name[256];
24 short err;
25
26 pb.d.ioNamePtr= (unsigned char *)c2pstr(strcpy(name, 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 if (hfsrunning())
32 err= PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
33 else
34 err= PBGetFInfo((ParmBlkPtr)&pb, FALSE);
35 if (err != noErr) {
36 errno = ENOENT;
37 return -1;
38 }
39 if (pb.d.ioFlAttrib & LOCKBIT)
40 buf->st_mode= 0444;
41 else
42 buf->st_mode= 0666;
43 if (pb.d.ioFlAttrib & DIRBIT) {
44 buf->st_mode |= 0111 | S_IFDIR;
45 buf->st_size= pb.d.ioDrNmFls;
46 buf->st_rsize= 0;
47 }
48 else {
49 buf->st_mode |= S_IFREG;
50 if (pb.f.ioFlFndrInfo.fdType == 'APPL')
51 buf->st_mode |= 0111;
52 buf->st_size= pb.f.ioFlLgLen;
53 buf->st_rsize= pb.f.ioFlRLgLen;
54 }
55 buf->st_mtime= pb.f.ioFlMdDat - TIMEDIFF;
56 return 0;
57}