Last active
August 1, 2022 21:49
-
-
Save lionello/4860a0bbfe9ba948db7ef145610a4aad to your computer and use it in GitHub Desktop.
Forked writevt from console-tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Mostly ripped off of console-tools' writevt.c | |
*/ | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <termios.h> | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdlib.h> | |
char *progname; | |
static int usage() { | |
printf("Usage: %s ttydev text\n", progname); | |
return 2; | |
} | |
int main(int argc, char **argv) { | |
int fd, argi; | |
char *term = NULL; | |
char *text = NULL; | |
size_t len = 0; | |
progname = argv[0]; | |
argi = 1; | |
if (argi < argc) | |
term = argv[argi++]; | |
else { | |
fprintf(stderr, "%s: no tty specified\n", progname); | |
return usage(); | |
} | |
if (argi < argc) | |
text = argv[argi++]; | |
else { | |
fprintf(stderr, "%s: no text specified\n", progname); | |
return usage(); | |
} | |
len = strlen(text); | |
if (len == 1 && text[0] == '-') { | |
text = alloca(1024); | |
len = read(0, text, 1024); | |
} | |
if (argi != argc) { | |
fprintf(stderr, "%s: too many arguments\n", progname); | |
return usage(); | |
} | |
fd = open(term, O_RDONLY); | |
if (fd < 0) { | |
perror(term); | |
fprintf(stderr, "%s: could not open tty\n", progname); | |
return 1; | |
} | |
while (len) { | |
if (ioctl(fd, TIOCSTI, text)) { | |
perror("ioctl"); | |
return 1; | |
} | |
text++; | |
len--; | |
} | |
return ioctl(fd, TIOCFLUSH, NULL); | |
} |
This version adds -
as an input filename, which causes (per POSIX convention) the text to come from stdin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copied from here: https://github.com/grawity/code/blob/master/thirdparty/writevt.c
Original
writevt
can be found inconsole-tools
here: https://sourceforge.net/projects/lct/files/