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