Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / stat.c
1 /*
2  * Acess2 POSIX Emulation Lib
3  * - By John Hodge (thePowersGang)
4  *
5  * sys/stat.h
6  * - stat(2)
7  */
8 #include <sys/stat.h>
9 #include <acess/sys.h>
10
11 // === CODE ===
12 int stat(const char *path, struct stat *buf)
13 {
14         int fd = _SysOpen(path, 0);
15         if( !fd ) {
16                 // errno is set by _SysOpen
17                 return -1;
18         }
19         
20         int rv = fstat(fd, buf);
21         _SysClose(fd);
22         return rv;
23 }
24
25 int lstat(const char *path, struct stat *buf)
26 {
27         int fd = _SysOpen(path, OPENFLAG_NOLINK);
28         if( !fd ) {
29                 // errno is set by _SysOpen
30                 return -1;
31         }
32         
33         int rv = fstat(fd, buf);
34         _SysClose(fd);
35         return rv;
36 }
37
38 int fstat(int fd, struct stat *buf)
39 {
40         t_sysFInfo      info;
41         
42         int rv = _SysFInfo(fd, &info, 0);
43         if( rv == -1 ) {
44                 return -1;
45         }
46
47         _SysDebug("fstat(fd=%i,buf=%p)", fd, buf);
48
49         // TODO: Populate buf
50         buf->st_dev = info.mount;
51         buf->st_ino = info.inode;
52         if( info.flags & FILEFLAG_SYMLINK )
53                 buf->st_mode = S_IFLNK;
54         else if( info.flags & FILEFLAG_DIRECTORY )
55                 buf->st_mode = S_IFDIR;
56         else
57                 buf->st_mode = S_IFREG;
58         // TODO: User modes (assume 660)
59         buf->st_mode |= 0660;
60         buf->st_nlink = 1;
61         buf->st_uid = info.uid;
62         buf->st_gid = info.gid;
63         buf->st_rdev = NULL;
64         buf->st_size = info.size;
65         buf->st_blksize = 512;
66         buf->st_blocks = (info.size+511)/512;
67         buf->st_atime = info.atime;
68         buf->st_mtime = info.mtime;
69         buf->st_ctime = info.ctime;
70         
71         return 0;
72 }
73

UCC git Repository :: git.ucc.asn.au