Pristine Ack-5.5
[Ack-5.5.git] / util / int / m_ioctl.c
1 /*
2         Dedicated to the ioctl system call, MON 54.
3 */
4
5 /* $Id: m_ioctl.c,v 2.3 1994/06/24 10:47:58 ceriel Exp $ */
6
7 #include        "sysidf.h"
8 #include        "v7ioctl.h"
9 #include        "global.h"
10 #include        "mem.h"
11 #include        "warn.h"
12
13 #include        <sgtty.h>
14
15 #ifdef  V7IOCTL                         /* define the proper V7 requests */
16
17 #define V7IOGETP        (('t'<<8)|8)
18 #define V7IOSETP        (('t'<<8)|9)
19 #define V7IOSETN        (('t'<<8)|10)
20 #define V7IOEXCL        (('t'<<8)|13)
21 #define V7IONXCL        (('t'<<8)|14)
22 #define V7IOHPCL        (('t'<<8)|2)
23 #define V7IOFLUSH       (('t'<<8)|16)
24
25 #define V7IOSETC        (('t'<<8)|17)
26 #define V7IOGETC        (('t'<<8)|18)
27
28 #endif  /* V7IOCTL */
29
30
31 /************************************************************************
32  * do_ioctl handles all ioctl system calls. It is called by the         *
33  * moncall() routine, case 54.  It was too big to leave it there.       *
34  * The ioctl system call is divided into 5 parts.                       *
35  * Ioctl's dealing with respectively:                                   *
36  * sgttyb, tchars, local mode word, ltchars, and miscellaneous ioctl's. *
37  * Some of the sgttyb-calls are only possible under the new tty-driver. *
38  * All of these are to be found in the miscellaneous section.           *
39  * do_ioctl() simply returns the value ioctl() would return itself.     *
40  * (0 for success, -1 for failure)                                      *
41  ***********************************************************************/
42
43 int do_ioctl(fd, req, addr)
44         int fd, req;
45         ptr addr;
46 {
47         register long e;
48         struct sgttyb sg_buf;
49
50 #ifdef  BSD_X                           /* from system.h */
51 #ifndef V7IOCTL
52         char c;
53         int mask;       /* will get ALIGNMENT problems with this one */
54         long count;     /* might get ALIGNMENT problems with this one */
55         int ldisc;      /* might get ALIGNMENT problems with this one */
56         int pgrp;       /* might get ALIGNMENT problems with this one */
57 #endif  /* V7IOCTL */
58
59         struct tchars tc_buf;
60 #ifndef V7IOCTL
61         struct ltchars ltc_buf;
62 #endif  /* V7IOCTL */
63 #endif  /* BSD_X */
64
65
66 #ifdef  V7IOCTL
67         switch (req) {                  /* translate the V7 requests */
68                                         /* and reject the non-V7 ones */
69         case V7IOGETP:
70                 req = TIOCGETP;
71                 break;
72         case V7IOSETP:
73                 req = TIOCSETP;
74                 break;
75         case V7IOEXCL:
76                 req = TIOCEXCL;
77                 break;
78         case V7IONXCL:
79                 req = TIOCNXCL;
80                 break;
81         case V7IOHPCL:
82                 req = TIOCHPCL;
83                 break;
84 #ifdef  BSD_X                           /* from system.h */
85         case V7IOSETN:
86                 req = TIOCSETN;
87                 break;
88
89         case V7IOSETC:
90                 req = TIOCSETC;
91                 break;
92         case V7IOGETC:
93                 req = TIOCGETC;
94                 break;
95 #endif  /* BSD_X */
96
97         default:
98                 einval(WBADIOCTL);
99                 return (-1);                    /* Fake return value */
100         }
101
102 #endif  /* V7IOCTL */
103
104
105         switch (req) {
106
107                 /*************************************/
108                 /****** Struct sgttyb ioctl's ********/
109                 /*************************************/
110
111         case TIOCGETP:
112                 /* Get fd's current param's and store at dsp2 */
113                 if (    (e = ioctl(fd, req, (char *) &sg_buf)) == -1
114                 ||      !sgttyb2mem(addr, &sg_buf)
115                 ) {
116                         e = -1;         /* errno already set */
117                 }
118                 break;
119
120         case TIOCSETP:
121 #ifdef  BSD4_1                          /* from system.h */
122         case TIOCSETN:
123 #endif  /* BSD4_1 */
124                 /* set fd's parameters according to sgtty buffer        */
125                 /* pointed to (addr), so first fill sg_buf properly.    */
126                 if (    !mem2sgtty(addr, &sg_buf)
127                 ||      (e = ioctl(fd, req, (char *) &sg_buf)) == -1
128                 ) {
129                         e = -1;         /* errno already set */
130                 }
131                 break;
132
133         case TIOCEXCL:
134         case TIOCNXCL:
135         case TIOCHPCL:
136                 /* These have no third argument. */
137                 e = ioctl(fd, req, (char *) 0);
138                 break;
139
140 #ifdef  BSD_X                           /* from system.h */
141                 /*************************************/
142                 /****** Struct tchars ioctl's ********/
143                 /*************************************/
144
145         case TIOCGETC:
146                 /* get special char's; store at addr */
147                 if (    (e = ioctl(fd, req, (char *) &tc_buf)) == -1
148                 ||      !tchars2mem(addr, &tc_buf)
149                 ) {
150                         e = -1;         /* errno already set */
151                 }
152                 break;
153
154         case TIOCSETC:
155                 /* set special char's; load from addr */
156                 if (    !mem2tchars(addr, &tc_buf)
157                 ||      (e = ioctl(fd, req, (char *) &tc_buf)) == -1
158                 ) {
159                         e = -1;
160                 }
161                 break;
162
163 #ifndef V7IOCTL
164                 /***************************************/
165                 /****** Local mode word ioctl's ********/
166                 /***************************************/
167
168         case TIOCLBIS:  /* addr points to mask which is or-ed with lmw */
169         case TIOCLBIC:  /* addr points to mask, ~mask & lmw is done */
170         case TIOCLSET:  /* addr points to mask, lmw is replaced by it */
171                 if (memfault(addr, wsize)) {
172                         e = -1;
173                 }
174                 else {
175                         mask = mem_ldu(addr, wsize);
176                         e = ioctl(fd, req, (char *) &mask);
177                 }
178                 break;
179
180         case TIOCLGET:  /* addr points to space, store lmw there */
181                 if (    memfault(addr, wsize)
182                 ||      (e = ioctl(fd, req, (char *) &mask)) == -1
183                 ) {
184                         e = -1;
185                 }
186                 else {
187                         mem_stn(addr, (long) mask, wsize);
188                 }
189                 break;
190
191                 /**************************************/
192                 /****** Struct ltchars ioctl's ********/
193                 /**************************************/
194
195         case TIOCGLTC:
196                 /* get current ltc's; store at addr */
197                 if (    (e = ioctl(fd, req, (char *) &ltc_buf)) == -1
198                 ||      !ltchars2mem(addr, &ltc_buf)
199                 ) {
200                         e = -1;         /* errno already set */
201                 }
202                 break;
203
204         case TIOCSLTC:
205                 /* set ltc_buf; load from addr */
206                 if (    !mem2ltchars(addr, &ltc_buf)
207                 ||      (e = ioctl(fd, req, (char *) &ltc_buf)) == -1
208                 ) {
209                         e = -1;
210                 }
211                 break;
212
213                 /*************************************/
214                 /****** Miscellaneous ioctl's ********/
215                 /*************************************/
216
217         case TIOCGETD:
218                 /* Get line discipline, store at addr */
219                 if (    memfault(addr, wsize)
220                 ||      (e = ioctl(fd, req, (char *) &ldisc)) == -1
221                 ) {
222                         e = -1;
223                 }
224                 else {
225                         mem_stn(addr, (long) ldisc, wsize);
226                 }
227                 break;
228
229         case TIOCSETD:
230                 /* Set line discipline, load from addr */
231                 if (memfault(addr, wsize)) {
232                         e = -1;
233                 }
234                 else {
235                         ldisc = (int) mem_ldu(addr, wsize);
236                         e = ioctl(fd, req, (char *) &ldisc);
237                 }
238                 break;
239
240         /* The following are not standard vanilla 7 UNIX */
241         case TIOCSBRK:  /* These have no argument */
242         case TIOCCBRK:  /* They work on parts of struct sgttyb */
243         case TIOCSDTR:
244         case TIOCCDTR:
245                 e = ioctl(fd, req, (char *) 0);
246                 break;
247
248         /* The following are used to set the line discipline */
249         case OTTYDISC:
250         case NETLDISC:
251         case NTTYDISC:
252                 e = ioctl(fd, req, (char *) 0);
253                 break;
254
255         case TIOCSTI:   /* addr = address of character */
256                 if (memfault(addr, 1L)) {
257                         e = -1;
258                 }
259                 else {
260                         c = (char) mem_ldu(addr, 1L);
261                         e = ioctl(fd, req, (char *) &c);
262                 }
263                 break;
264
265         case TIOCGPGRP:
266                 /* store proc grp number of control term in addr */
267                 if (    memfault(addr, wsize)
268                 ||      (e = ioctl(fd, req, (char *) &pgrp)) == -1
269                 ) {
270                         e = -1;
271                 }
272                 else {
273                         mem_stn(addr, (long) pgrp, wsize);
274                 }
275                 break;
276
277         case TIOCSPGRP: /* addr is NO POINTER !! */
278                 e = ioctl(fd, req, (char *) addr);
279                 break;
280
281         case FIONREAD:  /* do the ioctl, addr is long-int ptr now */
282                 if (    memfault(addr, wsize)
283                 ||      (e = ioctl(fd, req, (char *) &count)) == -1
284                 ) {
285                         e = -1;
286                 }
287                 else {
288                         mem_stn(addr, count, wsize);
289                 }
290                 break;
291
292 #endif  /* V7IOCTL */
293 #endif  /* BSD_X */
294
295         default:
296                 einval(WBADIOCTL);
297                 e = -1;                 /* Fake return value */
298                 break;
299         }
300         return (e);
301 }