Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, który wyłączy echo, czyli wypisywanie znaków wprowadzonych z klawiatury na ekran. Po ponownym uruchomieniu program powinien
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <termios.h>
- #include <ctype.h>
- #include <string.h>
- #define BUFFER_SIZE 128
- void restore_settings(struct termios default_settings)
- {
- if(tcsetattr(STDIN_FILENO, TCSANOW, &default_settings) == -1) {
- perror("tcsetattr restore_settings");
- exit(EXIT_FAILURE);
- }
- }
- void set_no_echo_mode(void)
- {
- struct termios no_echo_attr;
- if(tcgetattr(STDIN_FILENO, &no_echo_attr) == -1) {
- perror("tcgetattr set_no_echo_mode");
- exit(EXIT_FAILURE);
- }
- no_echo_attr.c_lflag &= ~(ICANON | ECHO);
- no_echo_attr.c_cc[VMIN] = 1;
- no_echo_attr.c_cc[VTIME] = 0;
- if(tcsetattr(STDIN_FILENO, TCSANOW, &no_echo_attr) == -1) {
- perror("tcsetattr set_no_echo_mode");
- exit(EXIT_FAILURE);
- }
- }
- int main(void)
- {
- struct termios default_settings;
- char ch_buffer, asterisk = '*';
- char passwd_buffer[BUFFER_SIZE];
- unsigned int i = 0;
- const char *enter_password = "Podaj haslo: ";
- if(!isatty(STDIN_FILENO)) {
- perror("isatty");
- return EXIT_FAILURE;
- }
- if(tcgetattr(STDIN_FILENO, &default_settings) == -1) {
- perror("tcgetattr");
- return EXIT_FAILURE;
- }
- write(STDOUT_FILENO, enter_password, strlen(enter_password));
- set_no_echo_mode();
- while(read(STDIN_FILENO, &ch_buffer, 1) && (isalnum(ch_buffer) || (ispunct(ch_buffer))) && i < sizeof(passwd_buffer) -2) {
- passwd_buffer[i++] = ch_buffer;
- write(STDOUT_FILENO, &asterisk, 1);
- }
- passwd_buffer[i] = '\0';
- printf("\nPodane haslo: %s\n", passwd_buffer);
- restore_settings(default_settings);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment