comcontrol.c revision 5397
1/*- 2 * Copyright (c) 1992 Christopher G. Demetriou 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29/* comcontrol.c */ 30 31#include <sys/types.h> 32#include <sys/ioctl.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <ctype.h> 36#include <fcntl.h> 37 38 39void usage(char *progname) 40{ 41 fprintf(stderr, "usage: %s <filename> [dtrwait <n>] [drainwait <n>]\n", progname); 42 exit(1); 43} 44 45int main(int argc, char *argv[]) 46{ 47 int fd; 48 int res = 0; 49 int dtrwait = -1, drainwait = -1; 50 51 if ((argc < 2) || (argc > 5)) usage(argv[0]); 52 53 fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0); 54 if (fd < 0) { 55 perror("open"); 56 fprintf(stderr, "%s: couldn't open file %s\n", argv[0], argv[1]); 57 return 1; 58 } 59 60 if (argc == 2) { 61 if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) { 62 res = 1; 63 perror("TIOCMGDTRWAIT"); 64 } 65 if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) { 66 res = 1; 67 perror("TIOCGDRAINWAIT"); 68 } 69 printf("dtrwait %d drainwait %d\n", dtrwait, drainwait); 70 } else { 71 char *prg = argv[0]; 72 73 while (argv[2] != NULL) { 74 if (!strcmp(argv[2],"dtrwait")) { 75 if (dtrwait >= 0) 76 usage(prg); 77 if (argv[3] == NULL || !isdigit(argv[3][0])) 78 usage(prg); 79 dtrwait = atoi(argv[3]); 80 argv += 2; 81 } else if (!strcmp(argv[2],"drainwait")) { 82 if (drainwait >= 0) 83 usage(prg); 84 if (argv[3] == NULL || !isdigit(argv[3][0])) 85 usage(prg); 86 drainwait = atoi(argv[3]); 87 argv += 2; 88 } else 89 usage(prg); 90 } 91 if (dtrwait >= 0) { 92 if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) { 93 res = 1; 94 perror("TIOCMSDTRWAIT"); 95 } 96 } 97 if (drainwait >= 0) { 98 if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) { 99 res = 1; 100 perror("TIOCSDRAINWAIT"); 101 } 102 } 103 } 104 105 close(fd); 106 return res; 107} 108