commit 874b4f36bd7a56da05e5628d7b600c38845e514d Author: Amber Date: Sun Feb 21 17:34:34 2021 -0500 Added files from the first CIS 452 lab. diff --git a/extra-credit.c b/extra-credit.c new file mode 100644 index 0000000..fea2f80 --- /dev/null +++ b/extra-credit.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#define TERMINAL_FD 0 +#define MAX_CHARS 50 +#define LOWERCASE_W 119 + +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); + + /* + * Setting erase key to w. I figured out how to do + * this using the man page given here: + * http://man7.org/linux/man-pages/man3/termios.3.html/ + */ + new.c_cc[VERASE] = LOWERCASE_W; + tcsetattr(TERMINAL_FD, TCSANOW, &new); + + // Pressing the w key erases input! + printf("Press the w key to erase: "); + fgets(input, MAX_CHARS, stdin); + printf("\nYou entered: %s", input); + + // Reverting settings. + tcsetattr(TERMINAL_FD, TCSANOW, &old); + + // Freeing memory allocated. + free(input); + + return 0; +} \ No newline at end of file diff --git a/programming-assignment.c b/programming-assignment.c new file mode 100644 index 0000000..cd05b29 --- /dev/null +++ b/programming-assignment.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#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; +} \ No newline at end of file