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

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