Solved

libc function?

Posted on 2002-05-25
2
296 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
script 11 138
need help to replace faulty disk using lv mirroring 18 164
usage of %{name} in rpm spec file 4 49
Shell script for mvn build 2 69
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now