Merge branch 'master' of git://localhost/acess2
[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         return _SysOpen(path, openflags, create_mode);
43 }
44
45 int creat(const char *path, mode_t mode)
46 {
47         // TODO: Make native call to do this cheaper
48         int fd = _SysOpen(path, OPENFLAG_CREATE, mode);
49         if( fd == -1 )  return -1;
50         _SysClose(fd);
51         return 0;
52 }
53
54 int close(int fd)
55 {
56         _SysClose(fd);
57         return 0;
58 }
59
60 ssize_t write(int fd, const void *buf, size_t count)
61 {
62         return _SysWrite(fd, buf, count);
63 }
64
65 ssize_t read(int fd, void *buf, size_t count)
66 {
67         return _SysRead(fd, buf, count);
68 }
69
70 int seek(int fd, int whence, off_t dest)
71 {
72         return _SysSeek(fd, whence, dest);
73 }
74
75 off_t tell(int fd)
76 {
77         return _SysTell(fd);
78 }
79
80 int fork(void)
81 {
82         return _SysClone(CLONE_VM, 0);
83 }
84
85 int execv(const char *b, char *v[])
86 {
87         return _SysExecVE(b, v, NULL);
88 }
89
90 int dup(int oldfd)
91 {
92         // NOTE: Acess's CopyFD doesn't cause offset sharing
93         // TODO: Check that -1 does cause a new allocation
94         return _SysCopyFD(oldfd, -1);
95 }
96
97 int dup2(int oldfd, int newfd)
98 {
99         // NOTE: Acess's CopyFD doesn't cause offset sharing
100         return _SysCopyFD(oldfd, newfd);
101 }
102
103
104 /*
105  * Set session ID to PID
106  */
107 pid_t setsid(void)
108 {
109         // TODO: actual syscall for this
110         return _SysGetPID();
111 }
112
113 pid_t getpid(void)
114 {
115         return _SysGetPID();
116 }
117
118 uid_t getuid(void)
119 {
120         return _SysGetUID();
121 }
122
123 uid_t geteuid(void)
124 {
125         // TODO: Impliment EUIDs in-kernel?
126         return _SysGetUID();
127 }
128
129 int kill(pid_t pid, int signal)
130 {
131         // TODO: Need special handling?
132         return _SysKill(pid, signal);
133 }
134
135 int select(int nfd, fd_set *rfd, fd_set *wfd, fd_set *efd, struct timeval *timeout)
136 {
137         
138         if( timeout )
139         {
140                 long long int ltimeout = 0;
141                 ltimeout = timeout->tv_sec*1000 + timeout->tv_usec / 1000;
142                 int ret = _SysSelect(nfd, rfd, wfd, efd, &ltimeout, 0);
143                 return ret;
144         }
145         else
146         {
147                 return _SysSelect(nfd, rfd, wfd, efd, NULL, 0);
148         }
149 }
150
151 int pipe(int pipefd[2])
152 {
153         pipefd[0] = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
154         pipefd[1] = _SysCopyFD(pipefd[0], -1);
155         _SysFDFlags(pipefd[1], OPENFLAG_READ|OPENFLAG_WRITE, OPENFLAG_WRITE);
156         return 0;
157 }
158
159 int chdir(const char *dir)
160 {
161         return _SysChdir(dir);
162 }
163
164 int mkdir(const char *pathname, mode_t mode)
165 {
166         _SysDebug("TODO: POSIX mkdir(%i, 0%o)", pathname, mode);
167         return -1;
168 }
169
170 char *getpass(void)
171 {
172         static char passbuf[PASS_MAX+1];
173         fprintf(stderr, "Password: ");
174         fgets(passbuf, PASS_MAX+1, stdin);
175         fprintf(stderr, "\n");
176         return passbuf;
177 }
178

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