Skip to content

Instantly share code, notes, and snippets.

@amatus
Created March 24, 2016 16:23
Show Gist options
  • Save amatus/90892d192123814ad6dd to your computer and use it in GitHub Desktop.
Save amatus/90892d192123814ad6dd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
/*
* 0x00 - strange char
* 0x01 ^a - seems to do nothing, except after ^r
* 0x02 - 0x0E ^b - ^n - strange chars
* 0x0F ^o - clear the screen
* 0x10 ^p + <char> - seems to move the cursor
* 0x11 ^q - same as space
* 0x12 ^r - kills output then ^a fixes it
* 0x13 - 0x1A ^s - ^z - same as space
* 0x1B - 0x1F - same as space
*
* 0x10 0x00 - col 0 row 0 home cursor
* 0x01 - col 1 row 0
* ...
* 0x17 - col 23 row 0
* 0x18 - off the screen (sometimes?)
* 0x18 - col 0 row 1
* 0x19 - col 1 row 1
* ...
* 0x2F - col 23 row 0
* 0x30 - off the screen (for real this time)
*/
void reset_screen(void)
{
putchar(0x0F);
}
void move_cursor(int row, int col)
{
if(row < 0 || row > 1)
return;
if(col < 0 || col > 23)
return;
putchar(0x10);
putchar(row * 24 + col);
}
char get(void)
{
char ch;
read(0, &ch, 1);
if(ch == 'q')
execl("/sbin/init", "/sbin/init", (char *)NULL);
return ch;
}
void wait_for_key(void)
{
get();
}
int main(void)
{
char code[5] = "31337";
char input[5];
int i;
struct termios t;
/*
if(fcntl(0, F_SETFL, O_DIRECT) == -1)
{
perror("fcntl failed");
return -1;
}
if(setvbuf(stdin, NULL, _IONBF, 0) != 0)
{
perror("setbuf for stdin failed");
return -1;
}
*/
if(setvbuf(stdout, NULL, _IONBF, 0) != 0)
{
perror("setbuf on stdout failed");
return -1;
}
if(tcgetattr(0, &t) == -1)
{
perror("tcgetattr failed");
return -1;
}
cfmakeraw(&t);
if(tcsetattr(0, TCSAFLUSH, &t) == -1)
{
perror("tcsetattr failed");
return -1;
}
while(1)
{
reset_screen();
/* Wait for a keystroke to display message */
/* We don't want to burn the screen do we? */
wait_for_key();
printf("You are at a keypad:");
move_cursor(1, 0);
/* Give them a hint at how many keys to press */
for(i = 0; i < sizeof(code); i++)
putchar('_');
move_cursor(1, 0);
/* Read in keys */
for(i = 0; i < sizeof(input); i++)
{
input[i] = get();
putchar('*');
}
if(memcmp(input, code, sizeof(code)) == 0)
{
printf("good!");
wait_for_key();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment