-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwritevt.c
101 lines (90 loc) · 1.98 KB
/
writevt.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
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
/*
* sti.c: put text in some tty input buffer - aeb, 951009
*
* You may have to be root if the tty is not your controlling tty.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include "cline.h"
static void usage()
{
printf(_("Usage: %s tty text\n"
"Put text into the input buffer of a virtual terminal.\n"), progname);
OPTIONS_ARE();
OPT("-t --term=tty ", _("device name"));
OPT("-T --text=text ", _("text to insert"));
OPT("-h --help ", HELPDESC);
OPT("-V --version ", VERSIONDESC);
}
int main(int argc, char **argv)
{
int fd, c;
char *vterm = NULL;
char *text = NULL;
const struct option long_opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "term", 1, NULL, 't' },
{ "text", 1, NULL, 'T' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
miscsetup();
while ( (c = getopt_long (argc, argv, "t:T:Vh", long_opts, NULL)) != EOF) {
switch (c) {
case 'h':
usage();
exit(0);
case 'V':
version();
exit(0);
case 't':
vterm = optarg;
break;
case '?':
usage();
exit(1);
}
}
if (vterm == NULL) {
if (optind < argc)
vterm = argv[optind++];
else {
fprintf(stderr,_("%s: No tty specified.\n"),progname);
exit(1);
}
}
if (text == NULL) {
if (optind < argc)
text = argv[optind++];
else {
fprintf(stderr,_("%s: No text specified.\n"),progname);
exit(1);
}
}
if (argc != optind ) {
fprintf(stderr, _("%s: too many arguments\n"), progname);
exit(1);
}
fd = open(vterm, O_RDONLY);
if(fd < 0)
{
perror(vterm);
fprintf(stderr, _("%s: could not open tty\n"), progname);
exit(1);
}
while(*text)
{
if(ioctl(fd, TIOCSTI, text))
{
perror("TIOCSTI");
fprintf(stderr, _("%s: TIOCSTI ioctl failed\n"), progname);
exit(1);
}
text++;
}
return 0;
}