Usermode - POSIX and C conformance changes
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / unistd.c
1 /*
2  * Acess2 POSIX Emulation Layer
3  * - By John Hodge
4  * 
5  * unistd.c
6  * - POSIX->Acess VFS call translation
7  */
8 #include <unistd.h>
9 #include <acess/sys.h>
10 #include <stdarg.h>
11 #include <sys/select.h>
12
13 // === CODE ===
14 int unlink(const char *pathname)
15 {
16         return _SysUnlink(pathname);
17 }
18
19 int open(const char *path, int openmode, ...)
20 {
21         mode_t  create_mode = 0;
22         int openflags = 0;
23         
24         switch( openmode & O_RDWR )
25         {
26         case 0: // Special
27                 break;
28         case O_RDONLY:  openflags |= OPENFLAG_READ;     break;
29         case O_WRONLY:  openflags |= OPENFLAG_WRITE;    break;
30         case O_RDWR:    openflags |= OPENFLAG_READ|OPENFLAG_WRITE;      break;
31         }
32         
33         if( openmode & O_CREAT ) {
34                 openflags |= OPENFLAG_CREATE;
35                 va_list args;
36                 va_start(args, openmode);
37                 create_mode = va_arg(args, mode_t);
38                 va_end(args);
39         }
40         
41         return _SysOpen(path, openflags, create_mode);
42 }
43
44 int creat(const char *path, mode_t mode)
45 {
46         // TODO: Make native call to do this cheaper
47         int fd = _SysOpen(path, OPENFLAG_CREATE, mode);
48         if( fd == -1 )  return -1;
49         _SysClose(fd);
50         return 0;
51 }
52
53 int close(int fd)
54 {
55         _SysClose(fd);
56         return 0;
57 }
58
59 ssize_t write(int fd, const void *buf, size_t count)
60 {
61         return _SysWrite(fd, buf, count);
62 }
63
64 ssize_t read(int fd, void *buf, size_t count)
65 {
66         return _SysRead(fd, buf, count);
67 }
68
69 int seek(int fd, int whence, off_t dest)
70 {
71         return _SysSeek(fd, whence, dest);
72 }
73
74 off_t tell(int fd)
75 {
76         return _SysTell(fd);
77 }
78
79 int fork(void)
80 {
81         return _SysClone(CLONE_VM, 0);
82 }
83
84 int execv(const char *b, char *v[])
85 {
86         return _SysExecVE(b, v, NULL);
87 }
88
89 int dup(int oldfd)
90 {
91         // NOTE: Acess's CopyFD doesn't cause offset sharing
92         // TODO: Check that -1 does cause a new allocation
93         return _SysCopyFD(oldfd, -1);
94 }
95
96 int dup2(int oldfd, int newfd)
97 {
98         // NOTE: Acess's CopyFD doesn't cause offset sharing
99         return _SysCopyFD(oldfd, newfd);
100 }
101
102 pid_t getpid(void)
103 {
104         return _SysGetPID();
105 }
106
107 uid_t getuid(void)
108 {
109         return _SysGetUID();
110 }
111
112 int kill(pid_t pid, int signal)
113 {
114         // TODO: Need special handling?
115         return _SysKill(pid, signal);
116 }
117
118 int select(int nfd, fd_set *rfd, fd_set *wfd, fd_set *efd, struct timeval *timeout)
119 {
120         
121         if( timeout )
122         {
123                 long long int ltimeout = 0;
124                 ltimeout = timeout->tv_sec*1000 + timeout->tv_usec / 1000;
125                 int ret = _SysSelect(nfd, rfd, wfd, efd, &ltimeout, 0);
126                 return ret;
127         }
128         else
129         {
130                 return _SysSelect(nfd, rfd, wfd, efd, NULL, 0);
131         }
132 }
133
134 int pipe(int pipefd[2])
135 {
136         pipefd[0] = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
137         pipefd[1] = _SysCopyFD(pipefd[0], -1);
138         _SysFDFlags(pipefd[1], OPENFLAG_READ|OPENFLAG_WRITE, OPENFLAG_WRITE);
139         return 0;
140 }
141

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