ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to know how I can write a programm that just sits there and waits for the user to input a password. I want to make it so that the user can not exit the program by Ctrl-C. I also would like to have the password show up as "*" when the user types the password.
This little program ignores the signals SIGINT (CTRL-C) and SIGTSTP (CTRL-Z) by adding a signal handler that does nothing (the predefined handler SIGIGN)
Code:
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
struct sigaction sa;
struct sigaction oldint;
struct sigaction oldtstp;
/* Ignore signal completely */
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
/* Set up the signal handler for SIGINT (CTRL-C),
remember old value, so we can restore it later */
sigaction(SIGINT, &sa, &oldint);
/* Same for SIGTSTP (CTRL-Z)*/
sigaction(SIGTSTP, &sa, &oldtstp);
/* Sleep for five seconds, to terminate the program */
sleep(5);
/* Restore the behavior of SIGINT and SIGTSTP, not really
neccessary here. Just to show that it's possible*/
sigaction(SIGINT, &oldint, NULL);
sigaction(SIGTSTP, &oldtstp, NULL);
return 0;
};
It's generally not trivial to read character by character from a terminal since there is a buffer that gets flushed when the user presses return. But if you borrow some code from that page, you get a working program. Like this:
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
static struct termios termattr, save_termattr;
static int ttysavefd = -1;
static enum
{
RESET, RAW, CBREAK
} ttystate = RESET;
int
set_tty_raw(void)
{
int i;
i = tcgetattr (STDIN_FILENO, &termattr);
if (i < 0)
{
printf("tcgetattr() returned %d for fildes=%d\n",i,STDIN_FILENO);
perror ("");
return -1;
}
save_termattr = termattr;
termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
termattr.c_cflag &= ~(CSIZE | PARENB);
termattr.c_cflag |= CS8;
termattr.c_oflag &= ~(OPOST);
termattr.c_cc[VMIN] = 1;
termattr.c_cc[VTIME] = 0;
i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
if (i < 0)
{
printf("tcsetattr() returned %d for fildes=%d\n",i,STDIN_FILENO);
perror("");
return -1;
}
ttystate = RAW;
ttysavefd = STDIN_FILENO;
return 0;
}
int
set_tty_cooked()
{
int i;
if (ttystate != CBREAK && ttystate != RAW)
{
return 0;
}
i = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr);
if (i < 0)
{
return -1;
}
ttystate = RESET;
return 0;
}
unsigned char
kb_getc_w(void)
{
unsigned char ch;
size_t size;
while (1)
{
usleep(20000); /* 1/50th second: thanks, Floyd! */
size = read (STDIN_FILENO, &ch, 1);
if (size > 0)
{
break;
}
}
return ch;
}
int main(void)
{
unsigned char a;
set_tty_raw();
a = kb_getc_w();
do
{
printf("*");
fflush(stdout);
a = kb_getc_w();
}
while(a != '\r');
set_tty_cooked();
printf("\n");
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.