Skip to content

Instantly share code, notes, and snippets.

@joerick
Last active February 16, 2022 12:49
Show Gist options
  • Save joerick/9e2d244f456c2431619e7063eda62e1d to your computer and use it in GitHub Desktop.
Save joerick/9e2d244f456c2431619e7063eda62e1d to your computer and use it in GitHub Desktop.
C program which will fix the terminal when an SDL app has crashed or been SIGKILL'd before cleaning up after itself
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <linux/vt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
int main(int argc, char** argv) {
if (argc != 2) {
printf("usage: termfix /dev/ttyX\n");
return 2;
}
int fd = open(argv[1], O_RDWR, 0);
int res = ioctl(fd, VT_UNLOCKSWITCH, 1);
if (res != 0) {
perror("ioctl VT_UNLOCKSWITCH failed");
return 3;
}
ioctl(fd, KDSETMODE, KD_TEXT);
if (res != 0) {
perror("ioctl KDSETMODE failed");
return 3;
}
printf("Success\n");
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment