Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / sys_ioctl.c
1 /*
2  * Acess2 POSIX Emulation
3  * - By John Hodge (thePowersGang)
4  *
5  * sys_ioctl.c
6  * - IOCtl hacks
7  */
8 #include <sys/ioctl.h>
9 #include <acess/sys.h>
10 #include <acess/devices.h>      // DRV_TYPE_*
11 #include <acess/devices/pty.h>
12 #include <errno.h>
13 #include <termios.h>    // TIOC*
14 #include <stdarg.h>
15
16 // === CODE ===
17 int ioctl(int fd, int request, ...)
18 {
19         va_list args;
20         va_start(args, request);
21         
22         if( fd < 0 ) {
23                 errno = EBADF;
24                 return -1;
25         }
26         
27         if( request < 0 ) {
28                 errno = EINVAL;
29                 return -1;
30         }
31
32         // #1. Get device type (IOCtl 0)
33         int devtype = _SysIOCtl(fd, 0, NULL);
34
35         switch(devtype)
36         {
37         // 0: Normal file (no ioctls we care about)
38         case 0:
39                 _SysDebug("ioctl(%i, %i, ...) (File)", fd, request);
40                 return -1;
41         // 1: Has the ident set of ioctls, nothing else
42         case 1:
43                 _SysDebug("ioctl(%i, %i, ...) (Misc Dev)", fd, request);
44                 return -1;
45         
46         // TODO: Terminals
47         case DRV_TYPE_TERMINAL:
48                 switch(request)
49                 {
50                 case TIOCGWINSZ: {
51                         struct winsize *ws = va_arg(args, struct winsize*);
52                         _SysDebug("ioctl(%i, TIOCGWINSZ, %p", fd, ws);
53                         struct ptydims  dims;
54                         _SysIOCtl(fd, PTY_IOCTL_GETDIMS, &dims);
55                         ws->ws_col = dims.W;
56                         ws->ws_row = dims.H;
57                         ws->ws_xpixel = dims.PW;
58                         ws->ws_ypixel = dims.PH;
59                         return 0; }
60                 case TIOCSWINSZ: {
61                         const struct winsize *ws = va_arg(args, const struct winsize*);
62                         _SysDebug("ioctl(%i, TIOCSWINSZ, %p", fd, ws);
63                         struct ptydims  dims;
64                         dims.W = ws->ws_col;
65                         dims.H = ws->ws_row;
66                         dims.PW = ws->ws_xpixel;
67                         dims.PH = ws->ws_ypixel;
68                         _SysIOCtl(fd, PTY_IOCTL_SETDIMS, &dims);
69                         return 0; }
70                 default:
71                         _SysDebug("ioctl(%i, TIOC? %i, ...)", fd, request);
72                         break;
73                 }
74                 return -1;
75         
76         // NFI
77         default:
78                 _SysDebug("ioctl(%i, %i, ...) (DevType %i)", fd, request, devtype);
79                 return -1;
80         }
81
82         va_end(args);
83         return 0;
84 }
85
86

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