Skip to content

Instantly share code, notes, and snippets.

@BenjiWiebe
Created August 16, 2014 04:40
Show Gist options
  • Save BenjiWiebe/034c5cef395ee7004e89 to your computer and use it in GitHub Desktop.
Save BenjiWiebe/034c5cef395ee7004e89 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
static struct termios oldt, newt;
void handlectrlc(int sig)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
exit(0);
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
fprintf(stderr, "Usage: %s <vt>\n", argv[0]);
return 1;
}
int size = strlen(argv[1] + 10);
char *filename = malloc(size);
memset(filename, 0, size);
strcpy(filename, "/dev/pts/");
strncat(filename, argv[1], size-10);
int vt = open(filename, O_RDONLY);
free(filename);
if(vt < 0)
{
fprintf(stderr, "%s\n", strerror(errno));
return 1;
}
struct sigaction act;
act.sa_handler = handlectrlc;
sigaction(SIGINT, &act, NULL);
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON|ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
int i;
char a[2] = "\0\0";
while((i = fgetc(stdin)) != EOF)
{
if(i == 4)
break;
a[0] = i;
if(ioctl(vt, TIOCSTI, a) < 0)
{
fprintf(stderr, "%s\n", strerror(errno));
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return 1;
}
}
close(vt);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment