prism2.device: fix strict aliasing issue detected with gcc 4.8.3
[AROS.git] / compiler / posixc / ioctl.c
blobea8883458b54eb57b22daf3fa7972b8f7a7661e5
1 /*
2 Copyright © 2004-2016, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
8 #include <unistd.h>
10 #include <devices/conunit.h>
11 #include <dos/dosextens.h>
12 #include <sys/ioctl.h>
13 #include <proto/dos.h>
15 #include <errno.h>
17 #include "__fdesc.h"
19 static int send_action_packet(struct MsgPort *port, BPTR arg)
21 struct MsgPort *replyport;
22 struct StandardPacket *packet;
23 SIPTR ret;
25 replyport = CreatePort(NULL,0L);
26 if(!replyport)
28 return 0;
30 packet = (struct StandardPacket *) AllocMem(sizeof(struct StandardPacket),
31 MEMF_PUBLIC|MEMF_CLEAR);
32 if(!packet)
34 DeletePort(replyport);
35 return 0;
38 packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
39 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
40 packet->sp_Pkt.dp_Port = replyport;
41 packet->sp_Pkt.dp_Type = ACTION_DISK_INFO;
42 packet->sp_Pkt.dp_Arg1 = (SIPTR) arg;
44 PutMsg(port, (struct Message *) packet);
45 WaitPort(replyport);
46 GetMsg(replyport);
48 ret = packet->sp_Pkt.dp_Res1;
50 FreeMem(packet, sizeof(struct StandardPacket));
51 DeletePort(replyport);
53 return ret;
56 static int fill_consize(APTR fd, struct winsize *ws)
58 struct ConUnit *console_unit = NULL;
59 struct InfoData *info_data;
60 BPTR action_disk_info_arg;
61 void *console = ((struct FileHandle *) BADDR(fd))->fh_Type;
63 if(!console)
65 return 0;
68 info_data = AllocMem(sizeof(*info_data), MEMF_PUBLIC);
69 if(info_data == NULL)
71 /* no memory, ouch */
72 return 0;
75 action_disk_info_arg = MKBADDR(info_data);
76 if(send_action_packet((struct MsgPort *) console, action_disk_info_arg))
78 console_unit = (void *) ((struct IOStdReq *) info_data->id_InUse)->io_Unit;
81 /* info_data not required anymore */
82 FreeMem(info_data, sizeof(*info_data));
84 if (console_unit == NULL)
86 return 0;
89 ws->ws_row = (unsigned short) console_unit->cu_YMax+1;
90 ws->ws_col = (unsigned short) console_unit->cu_XMax+1;
91 if(console_unit->cu_Window)
93 /* does a console always have a window? might be iconified? */
94 ws->ws_xpixel = (unsigned short) console_unit->cu_Window->Width;
95 ws->ws_ypixel = (unsigned short) console_unit->cu_Window->Height;
98 return 1;
101 /*****************************************************************************
103 NAME */
105 #include <sys/ioctl.h>
107 int ioctl(
109 /* SYNOPSIS */
110 int fd,
111 int request,
112 ...)
114 /* FUNCTION
115 Control device. Function to manipulate and fetch special device
116 parameters.
118 INPUTS
119 fd - file descriptor
120 request - ioctl request id, containing request type, input or output
121 type and argument size in bytes. Use macros and defines
122 from <sys/ioctl.h>:
124 TIOCGWINSZ - fill in rows, columns, width and height of
125 console window
127 ... - Other arguments for the specified request
129 RESULT
130 EBADF - fd is not valid
131 EFAULT - no valid argument
132 ENOTTY - fd is not of required type
134 NOTES
135 Width and height are the width and height of the intuition window.
137 EXAMPLE
138 #include <stdio.h>
139 #include <unistd.h>
140 #include <sys/ioctl.h>
143 int ret;
144 struct winsize w;
145 ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
146 if(ret)
148 printf("ERROR: %d\n", ret);
150 else
152 printf ("columns: %4d\n", w.ws_col);
153 printf ("lines: %4d\n", w.ws_row);
154 printf ("width: %4d\n", w.ws_xpixel);
155 printf ("height: %4d\n", w.ws_ypixel);
159 BUGS
160 Only the requests listed above are implented.
162 SEE ALSO
164 INTERNALS
166 ******************************************************************************/
168 va_list args;
169 struct winsize *ws;
170 fdesc *desc;
172 if(request != TIOCGWINSZ)
174 /* FIXME: Implement missing ioctl() parameters */
175 AROS_FUNCTION_NOT_IMPLEMENTED("posixc");
176 errno = ENOSYS;
177 return EFAULT;
180 switch(fd)
182 case STDIN_FILENO: desc=BADDR(Input());
183 break;
184 case STDOUT_FILENO: desc=BADDR(Output());
185 break;
186 default:
187 desc=__getfdesc(fd); /* FIXME: is this correct? why does it not work for STDOUT/STDIN? */
190 if(!desc || !IsInteractive(desc))
192 return EBADF;
195 va_start(args, request);
196 ws=(struct winsize *) va_arg(args, struct winsize *);
197 va_end(args);
199 if(!ws || !fill_consize(desc, ws))
201 return EFAULT;
204 return 0;