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 */
21
#include <string.h> /* strlen, rindex, strncmp, strcmp */
23
volatile bool quit_now = false;
26
void termination_handler(int signum){
30
int main(int argc, char **argv){
33
struct termios t_new, t_old;
35
int status = EXIT_SUCCESS;
36
struct sigaction old_action,
37
new_action = { .sa_handler = termination_handler,
39
const char db[] = "--debug";
40
char *basename = rindex(argv[0], '/');
47
char *program_name = malloc(strlen(basename) + sizeof(db));
48
if (program_name == NULL){
53
program_name[0] = '\0';
55
for (int i = 1; i < argc; i++){
56
if (not strncmp(argv[i], db, 5)){
57
strcat(strcat(strcat(program_name, db ), "="), basename);
58
if(not strcmp(argv[i], db) or not strcmp(argv[i], program_name)){
66
fprintf(stderr, "Starting %s\n", argv[0]);
69
fprintf(stderr, "Storing current terminal attributes\n");
72
if (tcgetattr(STDIN_FILENO, &t_old) != 0){
76
sigemptyset(&new_action.sa_mask);
77
sigaddset(&new_action.sa_mask, SIGINT);
78
sigaddset(&new_action.sa_mask, SIGQUIT);
79
sigaddset(&new_action.sa_mask, SIGHUP);
80
sigaddset(&new_action.sa_mask, SIGTERM);
81
sigaction(SIGINT, NULL, &old_action);
82
if (old_action.sa_handler != SIG_IGN)
83
sigaction(SIGINT, &new_action, NULL);
84
sigaction(SIGQUIT, NULL, &old_action);
85
if (old_action.sa_handler != SIG_IGN)
86
sigaction(SIGQUIT, &new_action, NULL);
87
sigaction(SIGHUP, NULL, &old_action);
88
if (old_action.sa_handler != SIG_IGN)
89
sigaction(SIGHUP, &new_action, NULL);
90
sigaction(SIGTERM, NULL, &old_action);
91
if (old_action.sa_handler != SIG_IGN)
92
sigaction(SIGTERM, &new_action, NULL);
96
fprintf(stderr, "Removing echo flag from terminal attributes\n");
100
t_new.c_lflag &= ~ECHO;
101
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
102
perror("tcsetattr-echo");
107
fprintf(stderr, "Waiting for input from stdin \n");
111
status = EXIT_FAILURE;
114
fprintf(stderr, "Password: ");
115
ret = getline(&buffer, &n, stdin);
117
fprintf(stdout, "%s", buffer);
118
status = EXIT_SUCCESS;
121
// ret == 0 makes no other sence than to retry to read from stdin
123
if (errno != EINTR and not feof(stdin)){
125
status = EXIT_FAILURE;
133
fprintf(stderr, "Restoring terminal attributes\n");
135
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
136
perror("tcsetattr+echo");
140
fprintf(stderr, "%s is exiting\n", argv[0]);