Added files from the first CIS 452 lab.

This commit is contained in:
Amber McCloughan 2021-02-21 17:34:34 -05:00
commit 874b4f36bd
2 changed files with 90 additions and 0 deletions

42
extra-credit.c Normal file
View File

@ -0,0 +1,42 @@
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}

48
programming-assignment.c Normal file
View File

@ -0,0 +1,48 @@
#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;
}