Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / sys / ttyio / ioctl.c
blob5d2b38b9412db36514a3d861040a950b7d3adf0c
1 /* $NetBSD: ioctl.c,v 1.2 2001/12/31 20:18:28 thorpej Exp $ */
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: ioctl.c,v 1.2 2001/12/31 20:18:28 thorpej Exp $");
35 #endif
37 #include <errno.h>
38 #include <stdio.h>
39 #include <termios.h>
40 #include <signal.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <err.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
48 #if defined(__NetBSD__)
49 #include <util.h>
50 #elif defined(__bsdi__)
51 int openpty(int *, int *, char *, struct termios *, struct winsize *);
52 #elif defined(__FreeBSD__)
53 #include <libutil.h>
54 #else
55 #error where openpty?
56 #endif
58 /* ARGSUSED */
59 static void
60 sigchld(int nsig)
62 if (wait(NULL) == -1)
63 err(1, "wait");
66 /* ARGSUSED */
67 int
68 main(int argc, char *argv[])
70 int m, s, rc;
71 char name[128], buf[128];
72 struct termios term;
73 struct sigaction sa;
75 /* unbuffer stdout */
76 setbuf(stdout, NULL);
78 /* get terminal settings for later use */
79 if (tcgetattr(STDIN_FILENO, &term) == -1)
80 err(1, "tcgetattr");
82 /* get a tty */
83 if (openpty(&m, &s, name, &term, NULL) == -1)
84 err(1, "openpty");
86 switch (fork()) {
87 case -1:
88 err(1, "fork");
89 /* NOTREACHED */
90 case 0:
91 /* wait for parent to get set up */
92 (void) sleep(1);
93 (void) printf("child1: exiting\n");
94 exit(0);
95 /* NOTREACHED */
96 default:
97 (void) printf("parent: spawned child1\n");
98 break;
101 switch (fork()) {
102 case -1:
103 err(1, "fork");
104 /* NOTREACHED */
105 case 0:
106 /* wait for parent to get upset */
107 (void) sleep(2);
108 /* drain the tty q */
109 if (read(m, buf, sizeof(buf)) == -1)
110 err(1, "read");
111 (void) printf("child2: exiting\n");
112 exit(0);
113 /* NOTREACHED */
114 default:
115 (void) printf("parent: spawned child2\n");
116 break;
119 /* set up a restarting signal handler */
120 (void) sigemptyset(&sa.sa_mask);
121 sa.sa_handler = sigchld;
122 sa.sa_flags = SA_RESTART;
123 if (sigaction(SIGCHLD, &sa, NULL) == -1)
124 err(1, "sigaction");
126 /* put something in the output q */
127 if (write(s, "Hello world\n", 12) == -1)
128 err(1, "write");
130 /* ask for output to drain but don't drain it */
131 rc = 0;
132 if (tcsetattr(s, TCSADRAIN, &term) == -1) {
133 (void) printf("parent: tcsetattr: %s\n", strerror(errno));
134 rc = 1;
137 /* wait for last child */
138 sa.sa_handler = SIG_DFL;
139 if (sigaction(SIGCHLD, &sa, NULL) == -1)
140 err(1, "sigaction");
141 (void) wait(NULL);
143 exit(rc);
144 /* NOTREACHED */
145 exit(rc);
146 return 0;