Usermode/libc,libpsocket - Various fixes to psockets and errno
[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 #include <stdio.h>
13
14 // === CODE ===
15 int unlink(const char *pathname)
16 {
17         return _SysUnlink(pathname);
18 }
19
20 int open(const char *path, int openmode, ...)
21 {
22         mode_t  create_mode = 0;
23         int openflags = 0;
24         
25         switch( openmode & O_RDWR )
26         {
27         case 0: // Special
28                 break;
29         case O_RDONLY:  openflags |= OPENFLAG_READ;     break;
30         case O_WRONLY:  openflags |= OPENFLAG_WRITE;    break;
31         case O_RDWR:    openflags |= OPENFLAG_READ|OPENFLAG_WRITE;      break;
32         }
33         
34         if( openmode & O_CREAT ) {
35                 openflags |= OPENFLAG_CREATE;
36                 va_list args;
37                 va_start(args, openmode);
38                 create_mode = va_arg(args, mode_t);
39                 va_end(args);
40         }
41         
42         if( openmode & O_NONBLOCK )
43                 openflags |= OPENFLAG_NONBLOCK;
44         
45         int ret = _SysOpen(path, openflags, create_mode);
46         _SysDebug("open('%s', 0%o, 0%o) = %i", path, openmode, create_mode, ret);
47         return ret;
48 }
49
50 int creat(const char *path, mode_t mode)
51 {
52         // TODO: Make native call to do this cheaper
53         int fd = _SysOpen(path, OPENFLAG_CREATE, mode);
54         if( fd == -1 )  return -1;
55         _SysClose(fd);
56         return 0;
57 }
58
59 int close(int fd)
60 {
61         _SysClose(fd);
62         return 0;
63 }
64
65 ssize_t write(int fd, const void *buf, size_t count)
66 {
67         return _SysWrite(fd, buf, count);
68 }
69
70 ssize_t read(int fd, void *buf, size_t count)
71 {
72         return _SysRead(fd, buf, count);
73 }
74
75 int seek(int fd, int whence, off_t dest)
76 {
77         return _SysSeek(fd, whence, dest);
78 }
79
80 off_t tell(int fd)
81 {
82         return _SysTell(fd);
83 }
84
85 int fork(void)
86 {
87         return _SysClone(CLONE_VM, 0);
88 }
89
90 int execv(const char *b, char *v[])
91 {
92         return _SysExecVE(b, v, NULL);
93 }
94
95 int dup(int oldfd)
96 {
97         _SysDebug("libposix: dup() does not share offsets/flags");
98         // NOTE: Acess's CopyFD doesn't cause offset sharing
99         int ret = _SysCopyFD(oldfd, -1);
100         _SysDebug("dup(%i) = %i", oldfd, ret);
101         return ret;
102 }
103
104 int dup2(int oldfd, int newfd)
105 {
106         _SysDebug("libposix: dup2() does not share offsets/flags");
107         // NOTE: Acess's CopyFD doesn't cause offset sharing
108         _SysDebug("dup2(%i,%i)", oldfd, newfd);
109         return _SysCopyFD(oldfd, newfd);
110 }
111
112
113 /*
114  * Set session ID to PID
115  */
116 pid_t setsid(void)
117 {
118         // TODO: actual syscall for this
119         return _SysGetPID();
120 }
121
122 pid_t getpid(void)
123 {
124         return _SysGetPID();
125 }
126
127 uid_t getuid(void)
128 {
129         return _SysGetUID();
130 }
131
132 uid_t geteuid(void)
133 {
134         // TODO: Impliment EUIDs in-kernel?
135         return _SysGetUID();
136 }
137
138 int kill(pid_t pid, int signal)
139 {
140         // TODO: Need special handling?
141         return _SysKill(pid, signal);
142 }
143
144 int select(int nfd, fd_set *rfd, fd_set *wfd, fd_set *efd, struct timeval *timeout)
145 {
146         long long int   ltimeout = 0, *ltimeoutp = NULL;
147         if( timeout )
148         {
149                 ltimeout = timeout->tv_sec*1000 + timeout->tv_usec / 1000;
150                 ltimeoutp = &ltimeout;
151         }
152         _SysDebug("select(%i,{0x%x},{0x%x},{0x%x},%lli)",
153                 nfd, (rfd?rfd->flags[0]:0), (wfd?wfd->flags[0]:0), (efd?efd->flags[0]:0),
154                 (ltimeoutp ? *ltimeoutp : -1)
155                 );
156         return _SysSelect(nfd, rfd, wfd, efd, ltimeoutp, 0);
157 }
158
159 int pipe(int pipefd[2])
160 {
161         pipefd[0] = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
162         pipefd[1] = _SysCopyFD(pipefd[0], -1);
163         _SysFDFlags(pipefd[1], OPENFLAG_READ|OPENFLAG_WRITE, OPENFLAG_WRITE);
164         _SysDebug("pipe({%i,%i})", pipefd[0], pipefd[1]);
165         return 0;
166 }
167
168 int chdir(const char *dir)
169 {
170         return _SysChdir(dir);
171 }
172
173 int mkdir(const char *pathname, mode_t mode)
174 {
175         _SysDebug("TODO: POSIX mkdir(%i, 0%o)", pathname, mode);
176         return -1;
177 }
178
179 char *getpass(const char *prompt)
180 {
181         static char passbuf[PASS_MAX+1];
182         fprintf(stderr, "%s", prompt);
183         fgets(passbuf, PASS_MAX+1, stdin);
184         fprintf(stderr, "\n");
185         return passbuf;
186 }
187

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