1 /* misc.c - utility routines for the tty server
2 * vixie 14may94 [cloned from ttyprot.c (12Sep91)]
3 */
4
5 #ifndef LINT
6 static char RCSid[] = "$Id: misc.c,v 1.7 2001/03/24 21:14:27 vixie Exp $";
7 #endif
8
9 /* Copyright (c) 1996 by Internet Software Consortium.
10 *
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
16 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
18 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
19 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
20 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
21 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 * SOFTWARE.
23 */
24
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/file.h>
29
30 #include <netinet/in.h>
31
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <termios.h>
36
37 #include "rtty.h"
38 #include "misc.h"
39 #include "ttyprot.h"
40 #ifdef NEED_BITYPES_H
41 # include "bitypes.h"
42 #endif
43
44 #include <stdlib.h>
45
46 #if DEBUG
47 extern int Debug;
48 #endif
49
50 void
cat_v(FILE * file,const u_char * buf,int nchars)51 cat_v(FILE *file, const u_char *buf, int nchars) {
52 while (nchars-- > 0) {
53 int c = *buf++;
54
55 if (isprint(c)) {
56 fputc(c, file);
57 } else if (iscntrl(c)) {
58 fputc('^', file);
59 fputc('@' + c, file); /* XXX assumes ASCII */
60 } else {
61 fprintf(file, "\\%03o", c);
62 }
63 }
64 }
65
66 int
install_ttyios(int tty,const struct termios * ios)67 install_ttyios(int tty, const struct termios *ios) {
68 #ifdef DEBUG
69 if (Debug) {
70 fprintf(stderr,
71 "install_termios(%d): C=0x%x L=0x%x I=0x%x O=0x%x m=%d t=%d\n",
72 tty, ios->c_cflag, ios->c_lflag,
73 ios->c_iflag, ios->c_oflag,
74 ios->c_cc[VMIN], ios->c_cc[VTIME]);
75 }
76 #endif
77 if (0 > tcsetattr(tty, TCSANOW, ios)) {
78 perror("tcsetattr");
79 return (-1);
80 }
81 return (0);
82 }
83
84 void
prepare_term(struct termios * ios,cc_t vmin)85 prepare_term(struct termios *ios, cc_t vmin) {
86 ios->c_cflag |= HUPCL|CLOCAL|CREAD|TAUTOFLOW|CS8;
87 ios->c_lflag |= NOFLSH;
88 ios->c_lflag &= ~(ICANON|TOSTOP|ECHO|ECHOE|ECHOK|ECHONL|IEXTEN|ISIG);
89 ios->c_iflag &= ~(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP
90 |INLCR|IGNCR|ICRNL|IXON|IXOFF);
91 ios->c_oflag &= ~OPOST;
92 ios->c_cc[VMIN] = vmin;
93 ios->c_cc[VTIME] = 0;
94 }
95
96 int
tty_nonblock(int tty,int nonblock)97 tty_nonblock(int tty, int nonblock) {
98 int old, new;
99
100 old = fcntl(tty, F_GETFL, 0);
101 if (old == -1) {
102 perror("fcntl(F_GETFL)");
103 exit(1);
104 }
105 if (nonblock)
106 new = old|O_NONBLOCK;
107 else
108 new = old & ~O_NONBLOCK;
109 if (new != old) {
110 if (fcntl(tty, F_SETFL, new) == -1) {
111 perror("fcntl(F_SETFL)");
112 exit(1);
113 }
114 }
115 return ((old & O_NONBLOCK) != 0);
116 }
117
118 void *
safe_malloc(size_t size)119 safe_malloc(size_t size) {
120 void *ret = malloc(size);
121
122 if (!ret) {
123 perror("malloc");
124 exit(1);
125 }
126 return (ret);
127 }
128
129 void *
safe_calloc(size_t n,size_t size)130 safe_calloc(size_t n, size_t size) {
131 void *ret = calloc(n, size);
132
133 if (!ret) {
134 perror("calloc");
135 exit(1);
136 }
137 return (ret);
138 }
139
140 void *
safe_realloc(void * ptr,size_t size)141 safe_realloc(void *ptr, size_t size) {
142 void *ret = realloc(ptr, size);
143
144 if (!ret) {
145 perror("realloc");
146 exit(1);
147 }
148 return (ret);
149 }
150
151 char *
safe_strdup(const char * str)152 safe_strdup(const char *str) {
153 char *new = strdup(str);
154
155 if (new == NULL) {
156 perror("strdup");
157 exit(1);
158 }
159 return (new);
160 }
161
162 #ifndef isnumber
163 /*
164 * from libvixutil.a (14may94 version)
165 */
166 int
isnumber(const char * s)167 isnumber(const char *s) {
168 char ch;
169 int n;
170
171 n = 0;
172 while (ch = *s++) {
173 n++;
174 if (!isdigit(ch))
175 return (0);
176 }
177 return (n != 0);
178 }
179 #endif
180
181 #ifdef NEED_INET_ATON
inet_aton(const char * cp,struct in_addr * addr)182 int inet_aton(const char *cp, struct in_addr *addr) {
183 u_int32_t v;
184
185 if ((v = inet_addr(cp)) > 0) {
186 addr->s_addr = v;
187 return (1);
188 }
189 return (0);
190 }
191 #endif
192