r6831@lvps87-230-33-50: verhaegs | 2008-02-03 14:08:57 +0100
[tangerine.git] / compiler / clib / __stat.c
blob48fe46504d5e1c16a0df380aa3412cf4007f5b05
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <dos/dos.h>
7 #include <proto/dos.h>
9 #include <string.h>
10 #include <errno.h>
12 #include "__time.h"
13 #include "__errno.h"
14 #include "__stat.h"
16 #include <sys/stat.h>
19 static mode_t __prot_a2u(ULONG protect);
20 static uid_t __id_a2u(UWORD id);
23 int __stat(BPTR lock, struct stat *sb)
25 struct FileInfoBlock *fib;
27 fib = AllocDosObject(DOS_FIB, NULL);
29 if (!fib)
31 errno = IoErr2errno(IoErr());
33 return -1;
36 if (!Examine(lock, fib))
38 errno = IoErr2errno(IoErr());
39 FreeDosObject(DOS_FIB, fib);
41 return -1;
44 sb->st_dev = (dev_t)((struct FileHandle *)lock)->fh_Device;
45 sb->st_ino = (ino_t)fib->fib_DiskKey;
46 sb->st_size = (off_t)fib->fib_Size;
47 sb->st_blocks = (long)fib->fib_NumBlocks;
48 sb->st_atime =
49 sb->st_ctime =
50 sb->st_mtime = (fib->fib_Date.ds_Days * 24*60 + fib->fib_Date.ds_Minute + __gmtoffset) * 60 +
51 fib->fib_Date.ds_Tick / TICKS_PER_SECOND + OFFSET_FROM_1970;
52 sb->st_uid = __id_a2u(fib->fib_OwnerUID);
53 sb->st_gid = __id_a2u(fib->fib_OwnerGID);
54 sb->st_mode = __prot_a2u(fib->fib_Protection);
57 struct InfoData info;
59 if (Info(lock, &info))
61 sb->st_blksize = info.id_BytesPerBlock;
63 else
65 /* The st_blksize is just a guideline anyway, so we set it
66 to 1024 in case Info() didn't succeed */
67 sb->st_blksize = 1024;
71 switch (fib->fib_DirEntryType)
73 case ST_PIPEFILE:
74 /* don't use S_IFIFO, we don't have a mkfifo() call ! */
75 sb->st_mode |= S_IFCHR;
76 break;
78 case ST_ROOT:
79 case ST_USERDIR:
80 sb->st_nlink = 2;
81 sb->st_mode |= S_IFDIR;
82 break;
84 case ST_SOFTLINK:
85 sb->st_nlink = 1;
86 sb->st_mode |= S_IFLNK;
87 break;
89 case ST_LINKDIR:
90 sb->st_nlink = 3;
91 sb->st_mode |= S_IFDIR;
92 break;
94 case ST_LINKFILE:
95 sb->st_nlink = 2;
97 case ST_FILE:
98 default:
99 sb->st_mode |= S_IFREG;
102 FreeDosObject(DOS_FIB, fib);
104 return 0;
108 static mode_t __prot_a2u(ULONG protect)
110 mode_t uprot = 0000;
112 if ((protect & FIBF_SCRIPT))
113 uprot |= 0111;
114 /* The following three flags are low-active! */
115 if (!(protect & FIBF_EXECUTE))
116 uprot |= 0100;
117 if (!(protect & FIBF_WRITE))
118 uprot |= 0200;
119 if (!(protect & FIBF_READ))
120 uprot |= 0400;
121 if ((protect & FIBF_GRP_EXECUTE))
122 uprot |= 0010;
123 if ((protect & FIBF_GRP_WRITE))
124 uprot |= 0020;
125 if ((protect & FIBF_GRP_READ))
126 uprot |= 0040;
127 if ((protect & FIBF_OTR_EXECUTE))
128 uprot |= 0001;
129 if ((protect & FIBF_OTR_WRITE))
130 uprot |= 0002;
131 if ((protect & FIBF_OTR_READ))
132 uprot |= 0004;
134 return uprot;
138 static uid_t __id_a2u(UWORD id)
140 switch(id)
142 case (UWORD)-1:
143 return 0;
145 case (UWORD)-2:
146 return (UWORD)-1;
148 case 0:
149 return (UWORD)-2;
151 default:
152 return id;