| 1 | /* $Id: stat.c,v 1.1 1999-12-10 17:11:28 ansari Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Minimal 'stat' emulation: tells directories from files and | 
|---|
| 5 | * gives length and mtime. | 
|---|
| 6 | * | 
|---|
| 7 | * Largely based on code written by Guido van Rossum, CWI, Amsterdam | 
|---|
| 8 | * and placed by him in the public domain -- | 
|---|
| 9 | * retrieved by anonymous FTP from ftp.cwi.nl | 
|---|
| 10 | */ | 
|---|
| 11 |  | 
|---|
| 12 | #include <string.h> | 
|---|
| 13 | #include <errno.h> | 
|---|
| 14 | #include <sys/stat.h> | 
|---|
| 15 | #include "unixmac.h" | 
|---|
| 16 |  | 
|---|
| 17 | int sys_nerr = 0; | 
|---|
| 18 | char *sys_errlist[] = {""}; | 
|---|
| 19 | char *myenviron[] = {NULL}; | 
|---|
| 20 | char **environ = myenviron; | 
|---|
| 21 | extern int __uid, __gid; | 
|---|
| 22 | int Stat(char*, long, struct stat*); | 
|---|
| 23 |  | 
|---|
| 24 | int __uid = 0; | 
|---|
| 25 | int __gid = 0; | 
|---|
| 26 |  | 
|---|
| 27 | /* Bits in ioFlAttrib: */ | 
|---|
| 28 | #define LOCKBIT (1<<0)          /* File locked */ | 
|---|
| 29 | #define DIRBIT  (1<<4)          /* It's a directory */ | 
|---|
| 30 |  | 
|---|
| 31 | /* | 
|---|
| 32 | * Mac-ky "stat" in which filename is given relative to a directory, | 
|---|
| 33 | * specified by long DirID. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | int | 
|---|
| 37 | Stat(name, DirID, buf) | 
|---|
| 38 | char *name; | 
|---|
| 39 | long DirID; | 
|---|
| 40 | struct stat *buf; | 
|---|
| 41 | { | 
|---|
| 42 | CInfoPBRec cipbr; | 
|---|
| 43 | HFileInfo *fpb = (HFileInfo*)&cipbr; | 
|---|
| 44 | DirInfo *dpb = (DirInfo*)&cipbr; | 
|---|
| 45 | unsigned char pname[256]; | 
|---|
| 46 | short err; | 
|---|
| 47 |  | 
|---|
| 48 | strcpy((char*)pname, name); | 
|---|
| 49 | c2pstr((char*)pname); | 
|---|
| 50 |  | 
|---|
| 51 | dpb->ioDrDirID = DirID; | 
|---|
| 52 | fpb->ioNamePtr = pname; | 
|---|
| 53 | fpb->ioVRefNum = 0; | 
|---|
| 54 | fpb->ioFDirIndex = 0; | 
|---|
| 55 | fpb->ioFVersNum = 0; | 
|---|
| 56 | err = PBGetCatInfo(&cipbr, FALSE); | 
|---|
| 57 | if (err != noErr) { | 
|---|
| 58 | errno = ENOENT; | 
|---|
| 59 | return -1; | 
|---|
| 60 | } | 
|---|
| 61 | if (fpb->ioFlAttrib & LOCKBIT) | 
|---|
| 62 | buf->st_mode= 0444; | 
|---|
| 63 | else | 
|---|
| 64 | buf->st_mode= 0666; | 
|---|
| 65 | if (fpb->ioFlAttrib & DIRBIT) { | 
|---|
| 66 | buf->st_mode |= 0111 | S_IFDIR; | 
|---|
| 67 | buf->st_size= dpb->ioDrNmFls; | 
|---|
| 68 | buf->st_rsize= 0; | 
|---|
| 69 | } | 
|---|
| 70 | else { | 
|---|
| 71 | buf->st_mode |= S_IFREG; | 
|---|
| 72 | if (fpb->ioFlFndrInfo.fdType == 'APPL') | 
|---|
| 73 | buf->st_mode |= 0111; | 
|---|
| 74 | buf->st_size= fpb->ioFlLgLen; | 
|---|
| 75 | buf->st_rsize= fpb->ioFlRLgLen; | 
|---|
| 76 | } | 
|---|
| 77 | buf->st_mtime= fpb->ioFlMdDat; | 
|---|
| 78 | buf->st_ctime= fpb->ioFlCrDat; | 
|---|
| 79 | buf->st_ino= (unsigned short)fpb->ioDirID; | 
|---|
| 80 | buf->st_uid= __uid; | 
|---|
| 81 | buf->st_gid= __gid; | 
|---|
| 82 | return 0; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | int | 
|---|
| 86 | stat(path, buf) | 
|---|
| 87 | const char *path; | 
|---|
| 88 | struct stat *buf; | 
|---|
| 89 | { | 
|---|
| 90 | return Stat(path, 0L, buf); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | #if 0 | 
|---|
| 95 | int | 
|---|
| 96 | fstat(fd, buf) | 
|---|
| 97 | int fd; | 
|---|
| 98 | struct stat *buf; | 
|---|
| 99 | { | 
|---|
| 100 | FCBPBRec fcb; | 
|---|
| 101 | unsigned char pname[256]; | 
|---|
| 102 | long DirID; | 
|---|
| 103 | short err; | 
|---|
| 104 |  | 
|---|
| 105 | /* | 
|---|
| 106 | * fdopen() gives FILE entry with name of file, | 
|---|
| 107 | * as well as RefNum of containing directory | 
|---|
| 108 | */ | 
|---|
| 109 |  | 
|---|
| 110 | FILE *fp = fdopen(fd, ""); | 
|---|
| 111 |  | 
|---|
| 112 | /* | 
|---|
| 113 | * PBGetFCBInfo() converts short RefNum to long DirID | 
|---|
| 114 | */ | 
|---|
| 115 |  | 
|---|
| 116 | fcb.ioRefNum= fp->refnum; | 
|---|
| 117 | fcb.ioVRefNum= 0; | 
|---|
| 118 | fcb.ioFCBIndx= 0; | 
|---|
| 119 | fcb.ioNamePtr= pname; | 
|---|
| 120 | err= PBGetFCBInfo(&fcb, FALSE); | 
|---|
| 121 | if (err != noErr) { | 
|---|
| 122 | errno = ENOENT; | 
|---|
| 123 | return -1; | 
|---|
| 124 | } | 
|---|
| 125 | DirID = fcb.ioFCBParID; | 
|---|
| 126 |  | 
|---|
| 127 | p2cstr(pname); | 
|---|
| 128 | return Stat((char*)pname, DirID, buf); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | #endif | 
|---|