Solved

libc function?

Posted on 2002-05-25
2
308 Views
Last Modified: 2010-04-21
Hi everyone, i'm writing an application and at some point the program asks the user for a password. Now, i'd like to do this the same way that, say, GPG does: i type the password but the text doesn't show in the screen.

also i don't want (if possible) to use ncurses.

someone told me that there is a function for unechoed input in libc, any ideas?

thanks, any help will be really appreciated

uri
0
Comment
Question by:urif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
websolution earned 150 total points
ID: 7035059
I look for a "unechoed" and I couldn't find it neither in Posix nor Linux

using posix commands only:

#include <stdlib.h>
#include <stdio.h>

#include <termios.h>
#include <string.h>

static struct termios stored;

void echo_off(void)
{
    struct termios new;
    tcgetattr(0,&stored);
    memcpy(&new, &stored, sizeof(struct termios));
    new.c_lflag &= (~ECHO);
    tcsetattr(0,TCSANOW,&new);
    return;
}

void echo_on(void)
{
    tcsetattr(0,TCSANOW,&stored);
    return;
}

int main(int argc, char **argv)
{
    char passwd[256], login[256];
    printf("Enter your login: ");
    gets(login);
    printf("Enter your password: ");
    echo_off();
    gets(passwd);
    echo_on();
    printf("you entered: \n\tlogin: %s\n\tpassword: %s\n", login, passwd);
}

to compile it:  cc prog.c -o prog


using curses or ncurses
#include <curses.h>

main()
{
        char passwd[256], login[256];

        initscr();
        cbreak();
        echo();

        printw("enter your login: ");
        getstr(login);

        noecho();
        printw("enter your password: ");
        getstr(passwd);

        printw("you entered: \n\tlogin: %s\n\tpassword: %s\n", login, passwd);

        printw("Press a key to quit");
        getch();
        endwin();
}
to compile it:
cc -lcurses prog.c -o prog
or
cc -lncurses prog.c -o prog
0
 

Author Comment

by:urif
ID: 7035060
thx!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question