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
| #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <math.h>
#include <ctype.h>
#define MAX_BUF 2048
int max(int a, int b) {
if (a>b)
return a;
else
return b;
}
int main(int argc, char **argv) {
int fda, fdb;
int ret;
int i, n;
unsigned char buffer[MAX_BUF];
struct timeval tv, now;
fd_set rfd;
tv.tv_sec = 1;
tv.tv_usec = 0;
fda = open (argv[1], O_RDWR);
fdb = open (argv[2], O_RDWR);
while (1) {
FD_ZERO (&rfd);
FD_SET (fda, &rfd);
FD_SET (fdb, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select (max(fda, fdb) + 1, &rfd, NULL, NULL, &tv);
if (ret == -1 && errno == EINTR) {
continue;
}
if (ret < 0) {
perror ("select()");
exit (1);
}
gettimeofday (&now, NULL);
if (FD_ISSET (fda, &rfd)) {
n = read (fda, buffer, MAX_BUF-1);
buffer[n]=0;
printf("%d,%06d--(%02d):", (int)now.tv_sec, (int)now.tv_usec, n);
for (i=0; i<n; i++) {
if (isprint(buffer[i]))
printf("%c ", buffer[i]);
else
printf(" ");
}
printf("\n\t\t\t");
for (i=0; i<n; i++) {
printf("%02x ", buffer[i]);
}
printf("\n");
write (fdb, buffer, n);
}
if (FD_ISSET (fdb, &rfd)) {
n = read (fdb, buffer, MAX_BUF-1);
buffer[n]=0;
printf("\t\t\t\t\t\t\t\t");
printf("%d,%06d--(%02d):", (int)now.tv_sec, (int)now.tv_usec, n);
for (i=0; i<n; i++) {
if (isprint(buffer[i]))
printf("%c ", buffer[i]);
else
printf(" ");
}
printf("\n\t\t\t\t\t\t\t\t\t\t\t");
for (i=0; i<n; i++) {
printf("%02x ", buffer[i]);
}
printf("\n");
write (fda, buffer, n);
}
}
return EXIT_SUCCESS;
} |
Partager