cis-452-lab-1/programming-assignment.c

48 lines
1.1 KiB
C

#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#define TERMINAL_FD 0
#define MAX_CHARS 50
int main()
{
// Declaring the variables we'll be using.
struct termios old, new;
char *input;
// Grabbing terminal settings.
tcgetattr(TERMINAL_FD, &old);
new = old;
// Getting memory from the heap for the input string.
input = malloc(sizeof(char) * MAX_CHARS);
/*
* Turning off echo. I figured out how to do
* this using the man page given here:
* http://man7.org/linux/man-pages/man3/termios.3.html/
*/
printf("Disabling echo.\n");
new.c_lflag = new.c_lflag & ~ECHO;
tcsetattr(TERMINAL_FD, TCSANOW, &new);
// Entering phrase without echo!
printf("Enter secret word/phrase: ");
fgets(input, MAX_CHARS, stdin);
printf("\nYou entered: %s\n", input);
// Reverting settings.
printf("Default behavior restored.\n");
tcsetattr(TERMINAL_FD, TCSANOW, &old);
// Entering phrase with echo.
printf("Enter visible word/phrase: ");
fgets(input, MAX_CHARS, stdin);
printf("You entered: %s", input);
// Freeing memory allocated.
free(input);
return 0;
}