-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaudrate.c
47 lines (37 loc) · 920 Bytes
/
baudrate.c
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
// baudrate.c - simulate baud rate in a pipe
// example: telnet pdp11 23 | baudrate 2400
//
// rricharz 2024
//
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
int getch(void) {
int ch;
struct termios oldt;
struct termios newt;
tcgetattr(STDIN_FILENO, &oldt); // save old settings
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // apply old settings
return ch;
}
int main(int argc, char *argv[]) {
char c;
if (argc != 2) {
printf("Usage baudrate b\n");
printf("Where b is the baud rate to simulate (e.g. 4800)\n");
exit(1);
}
int baud = atoi(argv[1]);
printf("Simulated baud rate set to %d baud\n",baud);
while ((c = getch()) != (EOF & 0xff)) {
putchar(c);
fflush(stdout);
usleep(10000000/baud);
}
return 0;
}