1
 
#define _GNU_SOURCE             /* getline() */
 
2
 
#define _FORTIFY_SOURCE 2
 
3
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
4
 
                                   TCSAFLUSH, tcgetattr(), ECHO */
 
5
 
#include <unistd.h>             /* struct termios, tcsetattr(),
 
6
 
                                   STDIN_FILENO, TCSAFLUSH,
 
8
 
#include <signal.h>             /* sig_atomic_t, raise(), struct
 
9
 
                                   sigaction, sigemptyset(),
 
10
 
                                   sigaction(), sigaddset(), SIGINT,
 
11
 
                                   SIGQUIT, SIGHUP, SIGTERM */
 
12
 
#include <stddef.h>             /* NULL, size_t */
 
13
 
#include <sys/types.h>          /* ssize_t */
 
14
 
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE */
 
15
 
#include <stdio.h>              /* fprintf(), stderr, getline(),
 
16
 
                                   stdin, feof(), perror(), fputc(),
 
18
 
#include <errno.h>              /* errno, EINVAL */
 
19
 
#include <iso646.h>             /* or, not */
 
20
 
#include <stdbool.h>            /* bool, false, true */
 
22
 
volatile bool quit_now = false;
 
24
 
void termination_handler(int signum){
 
28
 
int main(int argc, char **argv){
 
31
 
  struct termios t_new, t_old;
 
33
 
  int status = EXIT_SUCCESS;
 
34
 
  struct sigaction old_action,
 
35
 
    new_action = { .sa_handler = termination_handler,
 
38
 
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
 
42
 
  sigemptyset(&new_action.sa_mask);
 
43
 
  sigaddset(&new_action.sa_mask, SIGINT);
 
44
 
  sigaddset(&new_action.sa_mask, SIGQUIT);
 
45
 
  sigaddset(&new_action.sa_mask, SIGHUP);
 
46
 
  sigaddset(&new_action.sa_mask, SIGTERM);
 
47
 
  sigaction(SIGINT, NULL, &old_action);
 
48
 
  if (old_action.sa_handler != SIG_IGN)
 
49
 
    sigaction(SIGINT, &new_action, NULL);
 
50
 
  sigaction(SIGQUIT, NULL, &old_action);
 
51
 
  if (old_action.sa_handler != SIG_IGN)
 
52
 
    sigaction(SIGQUIT, &new_action, NULL);
 
53
 
  sigaction(SIGHUP, NULL, &old_action);
 
54
 
  if (old_action.sa_handler != SIG_IGN)
 
55
 
    sigaction(SIGHUP, &new_action, NULL);
 
56
 
  sigaction(SIGTERM, NULL, &old_action);
 
57
 
  if (old_action.sa_handler != SIG_IGN)
 
58
 
    sigaction(SIGTERM, &new_action, NULL);
 
61
 
  t_new.c_lflag &= ~ECHO;
 
62
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
63
 
    perror("tcsetattr-echo");
 
69
 
      status = EXIT_FAILURE;
 
72
 
    fprintf(stderr, "Password: ");
 
73
 
    ret = getline(&buffer, &n, stdin);
 
75
 
      fprintf(stdout, "%s", buffer);
 
76
 
      status = EXIT_SUCCESS;
 
79
 
    // ret == 0 makes no other sence than to retry to read from stdin
 
81
 
      if (errno != EINTR and not feof(stdin)){
 
83
 
        status = EXIT_FAILURE;
 
90
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
91
 
    perror("tcsetattr+echo");