perl 5.0 alpha 2
[p5sagit/p5-mst-13.2.git] / do / ctl
1 int
2 do_ctl(optype,stab,func,argstr)
3 int optype;
4 STAB *stab;
5 int func;
6 STR *argstr;
7 {
8     register STIO *stio;
9     register char *s;
10     int retval;
11
12     if (!stab || !argstr || !(stio = stab_io(stab)) || !stio->ifp) {
13         errno = EBADF;  /* well, sort of... */
14         return -1;
15     }
16
17     if (argstr->str_pok || !argstr->str_nok) {
18         if (!argstr->str_pok)
19             s = str_get(argstr);
20
21 #ifdef IOCPARM_MASK
22 #ifndef IOCPARM_LEN
23 #define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
24 #endif
25 #endif
26 #ifdef IOCPARM_LEN
27         retval = IOCPARM_LEN(func);     /* on BSDish systes we're safe */
28 #else
29         retval = 256;                   /* otherwise guess at what's safe */
30 #endif
31         if (argstr->str_cur < retval) {
32             Str_Grow(argstr,retval+1);
33             argstr->str_cur = retval;
34         }
35
36         s = argstr->str_ptr;
37         s[argstr->str_cur] = 17;        /* a little sanity check here */
38     }
39     else {
40         retval = (int)str_gnum(argstr);
41 #ifdef DOSISH
42         s = (char*)(long)retval;                /* ouch */
43 #else
44         s = (char*)retval;              /* ouch */
45 #endif
46     }
47
48 #ifndef lint
49     if (optype == O_IOCTL)
50         retval = ioctl(fileno(stio->ifp), func, s);
51     else
52 #ifdef DOSISH
53         fatal("fcntl is not implemented");
54 #else
55 #ifdef HAS_FCNTL
56         retval = fcntl(fileno(stio->ifp), func, s);
57 #else
58         fatal("fcntl is not implemented");
59 #endif
60 #endif
61 #else /* lint */
62     retval = 0;
63 #endif /* lint */
64
65     if (argstr->str_pok) {
66         if (s[argstr->str_cur] != 17)
67             fatal("Return value overflowed string");
68         s[argstr->str_cur] = 0;         /* put our null back */
69     }
70     return retval;
71 }
72