[cs631apue] setting the password

lbustama lbustama at stevens.edu
Mon Dec 9 02:35:06 EST 2013


Not sure if you still need this, but you could also read directly from 
/dev/tty

ttyfd = open("/dev/tty", O_RDWR)

then turn off echoing for file descriptor ttyfd

or you could use readpassphrase(3). I downloaded the source code and it 
does the same as above plus more.

Luis

On 12/08/2013 9:56 PM, Nick Smith wrote:
> Hey PA,
> When you run with <test.txt this is redirecting the file's contents as
> your stdin. So being as this is not a TTY (it's a pipe), I bet that's
> why tcsetattr is failing. Check isatty(3) first.
> -Nick
> 
> On Sun 08 Dec 2013 09:55:33 PM EST, Paul-Anthony Dudzinski wrote:
>> Hello all,
>> 
>> I am writing the function to take the password from the user using the
>> termios (3). So far I have
>> 
>> int get_pass(unsigned char *pwd)
>> {
>>     struct termios oflags, nflags;
>>     char password[MAX_PASS_LEN];
>>     char pass2[MAX_PASS_LEN];
>> 
>>     /* disabling echo */
>>     tcgetattr(fileno(stdin), &oflags);
>>     nflags = oflags;
>>     nflags.c_lflag &= ~ECHO;
>>     nflags.c_lflag |= ECHONL;
>> 
>>     if (tcsetattr(fileno(stdin),  TCSANOW, &nflags) != 0) {
>>     perror("tcsetattr");
>>     }
>> 
>>     printf("Password: ");
>>     fgets(password, sizeof(password), stdin);
>>     password[strlen(password) - 1] = 0;
>> 
>>     while( strcmp(password,pass2) != 0){
>>         printf("Again : ");
>>         fgets(pass2, sizeof(pass2), stdin);
>>         pass2[strlen(pass2) - 1] = 0;
>>     }
>> 
>>     memcpy(pwd,password,strlen(password));
>> 
>>     /* restore terminal */
>>     if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
>>         perror("tcsetattr");
>>         return EXIT_FAILURE;
>>     }
>> 
>>     return 0;
>> }
>> 
>> 
>> This works fine when I run this by itself in a separate main function.
>> However when I put this in with my other code I get some errors that I
>> do not know how to handle and makes my program unstable.
>> 
>> 
>> pdudzins at rainman:~/cs631/hw5$ ./a.out -e < test.txt > out.txt
>> tcsetattr: Inappropriate ioctl for device
>> 
>> I am not sure how to appropriately handle the terminal using
>> tcsetattr. Has anyone run into this problem?
>> 
>> 
>> _______________________________________________
>> cs631apue mailing list
>> cs631apue at lists.stevens.edu
>> https://lists.stevens.edu/mailman/listinfo/cs631apue
> _______________________________________________
> cs631apue mailing list
> cs631apue at lists.stevens.edu
> https://lists.stevens.edu/mailman/listinfo/cs631apue


More information about the cs631apue mailing list