]> git.ozlabs.org Git - ppp.git/blob - NeXT/libposix/libposix.c
Update from Steve Perkins
[ppp.git] / NeXT / libposix / libposix.c
1 /*  Yes, that's right:  of all the platforms supported by ppp, 
2     only Mach OpenStep 4.x doesn't support POSIX.  Sheesh.
3
4     Stranger still, the POSIX declatations are still in the 4.x header files,
5     and the gcc -posix still defines _POSIX_SOURCE.  So... 
6     we emulate (sometimes badly) the missing POSIX functions.  This
7     is by no means a complete or general POSIX emulation.  Just enough 
8     to get us by for ppp, so we don't have to pollute the rest of the 
9     sources of every other (non-braindead) platform.  Much of the
10     code was snarfed from NeXT's 4.0 ppp port, the rest inspired by
11     "POSIX Programmers Guide" by Donald Lewine.
12     
13     Maybe if we complain NeXT will upgrade to BSD4.4 libs like the rest of
14     the free world (and maybe pink elephants will fly out of my...  -KC)
15  */
16
17 #include <signal.h>
18 #include <termios.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21 #include <sys/time.h>
22 #include <errno.h>
23
24 int sigemptyset(sigset_t *set)
25 {
26         *set = 0;
27         return 0;
28 }
29
30 int sigaddset(sigset_t *set, int signo)
31 {
32         *set |= 1<<signo;
33         return 0;
34 }
35
36 int sigprocmask(int how, const sigset_t *set, sigset_t *oset)
37 {
38         switch(how) {
39         case SIG_BLOCK:
40                 *oset = sigblock(*set);
41                 break;
42         case SIG_UNBLOCK:
43                 /* XXX How does one emulate this with ancient BSD? (KC) */
44                 break;
45         case SIG_SETMASK:
46                 *oset = sigsetmask(*set);
47                 break;
48         }
49         return 0;
50 }
51
52 int sigsuspend(const sigset_t *sigmask)
53 {
54         sigpause(*sigmask);
55 }
56
57 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
58 {
59         struct sigvec vec, ovec;
60         int st;
61
62         vec.sv_handler = act->sa_handler;
63         vec.sv_mask = act->sa_mask;
64         vec.sv_flags = act->sa_flags;
65         
66         st = sigvec(sig, &vec, &ovec);
67
68         if (oact) {
69                 oact->sa_handler = ovec.sv_handler;
70                 oact->sa_mask = ovec.sv_mask;
71                 oact->sa_flags = ovec.sv_flags;
72         }
73
74         return st;
75 }
76
77 int tcgetattr(int fildes, struct termios *tp)
78 {
79         return ioctl(fildes, TIOCGETA, tp);
80 }
81
82 int tcsetattr(int fd, int opt, const struct termios *t)
83 {
84         int st;
85
86         switch(opt) {
87         case TCSANOW:
88                 st = ioctl(fd, TIOCSETA, t);
89                 break;
90         case TCSADRAIN:
91                 st = ioctl(fd, TIOCSETAW, t);
92                 break;
93         case TCSAFLUSH:
94                 st = ioctl(fd, TIOCSETAF, t);
95                 break;
96         default:
97                 st = -1;
98                 errno = EINVAL;
99                 break;
100         }
101         return st;
102 }
103
104 /*  XXX we ignore duration (which is 0 in chat.c anyway).
105  */
106 int tcsendbreak(int fildes, int duration)
107 {
108         struct timeval sleepytime;
109
110         sleepytime.tv_sec = 0;
111         sleepytime.tv_usec = 400000;
112         if (ioctl(fildes, TIOCSBRK, 0) != -1)
113         {
114             select(0, 0, 0, 0, &sleepytime);
115             (void) ioctl(fildes, TIOCCBRK, 0);
116         }
117 }
118
119 /*  XXX This is the implementation of cfgetospeed from NeXT's ppp-5
120     pppd/sys-NeXT.c.  I don't know whether returning c_ispeed instead
121     of c_ospeed is deliberate or a type-o.
122  */
123 speed_t cfgetospeed(const struct termios *t)
124 {
125         return t->c_ispeed;
126 }
127
128 int cfsetospeed(struct termios *t, int speed)
129
130         t->c_ospeed = speed; 
131         return 0; 
132 }
133
134 speed_t cfgetispeed(const struct termios *t)
135 {
136         return t->c_ispeed;
137 }
138
139 int cfsetispeed(struct termios *t, int speed)
140
141         t->c_ispeed = speed; 
142         return 0; 
143 }
144
145 int setsid(void)
146 {
147     int fd;
148
149     setpgrp(0, getpid());
150     
151     if ( (fd = open("/dev/tty", O_RDWR | O_NDELAY)) < 0)
152             return -1;
153     ioctl(fd, TIOCNOTTY, NULL);
154     close(fd);
155
156     return 0;
157 }
158
159 int waitpid(pid_t pid, int *stat_loc, int options)
160 {
161     if (pid == -1) 
162         pid = 0;
163     return wait4(pid, (union wait *) stat_loc, options, NULL);
164 }
165