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

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