plptools
Loading...
Searching...
No Matches
ttytap.c
Go to the documentation of this file.
1/* plpfuse/Makefile.am
2 *
3 * This file is part of plptools.
4 *
5 * Copyright (C) 2001 Fritz Elfert <felfert@to.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <sys/fcntl.h>
24#include <sys/time.h>
25#include <sys/ioctl.h>
26#include <unistd.h>
27#include <errno.h>
28#include <stdio.h>
29#include <ctype.h>
30#include <linux/ttytap.h>
31#include <linux/kdev_t.h>
32
33static void usage(void) {
34 fprintf(stderr, "usage: ttytap -q|<ttyDevice>\n");
35 exit(1);
36}
37
38int main(int argc, char **argv) {
39 int fd;
40 tapheader th;
41 int i;
42 int r;
43 int major;
44 int minor;
45 int lastdir = -1;
46 struct stat stbuf;
47 char tbuf[256];
48 unsigned char buf[4096];
49
50
51 if (argc != 2)
52 usage();
53 if (strcmp(argv[1], "-q")) {
54 if (stat(argv[1], &stbuf)) {
55 perror(argv[1]);
56 exit(-1);
57 }
58 if (!S_ISCHR(stbuf.st_mode)) {
59 fprintf(stderr, "%s is not a char device\n", argv[1]);
60 exit(1);
61 }
62 major = MAJOR(stbuf.st_rdev);
63 minor = MINOR(stbuf.st_rdev);
64 } else {
65 minor = 0;
66 major = 0;
67 }
68 fd = open("/proc/ttytap", O_RDONLY);
69 if (fd == -1) {
70 perror("/proc/ttytap");
71 exit(-1);
72 }
73 if (ioctl(fd, TTYTAP_SETDEV, (major << 8) | minor)) {
74 perror("ioctl /proc/ttytap");
75 exit(-1);
76 }
77 if ((minor == 0) && (major == 0)) {
78 printf("Serial tapping switched off\n");
79 exit(0);
80 }
81 if (ioctl(fd, TTYTAP_GETQLEN, &i)) {
82 perror("ioctl /proc/ttytap");
83 exit(-1);
84 }
85 fprintf(stderr, "Queue length is %d\n", i);
86 while (1) {
87 r = read(fd, &th, sizeof(th));
88 if (r != sizeof(th)) {
89 perror("read /proc/ttytap");
90 exit(-1);
91 }
92 strftime(tbuf, 255, "%H:%M:%S", localtime(&th.stamp.tv_sec));
93 printf("%s.%06ld %c (%04d) ", tbuf,
94 (unsigned long)th.stamp.tv_usec,
95 (th.io) ? '<' : '>', th.len);
96 r = read(fd, buf, th.len);
97 if (r != th.len) {
98 perror("read /proc/ttytap");
99 exit(-1);
100 }
101 for (i = 0; i < th.len; i++)
102 printf("%02x ", buf[i] & 255);
103 for (i = 0; i < th.len; i++)
104 printf("%c", isprint(buf[i]) ? buf[i] : '.');
105 printf("\n"); fflush(stdout);
106 }
107 return 0;
108}
static rpcs * r
Definition: main.cc:56
int main(int argc, char **argv)
Definition: ttytap.c:38
static void usage(void)
Definition: ttytap.c:33