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

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