-1

I need to make this work Linux, I know that conio.h is not for Linux and the main problem is getch() function. I tried using another lib like curses.h but still I got a lot of errors. It takes users input of password and converts it to **** for safety reasons.

Old code:

#include<stdio.h>
#include<conio.h>

void main()
{
    char password[25],ch;
    int i;

    clrscr();
    puts("Enter password: ");

    while(1)
    {
        if(i<0)
            i=0;
        ch=getch();

        if(ch==13)
            break;

        if(ch==8)
        {
            putch('b');
            putch(NULL);
            putch('b');
            i--;
            continue;
        }

        password[i++]=ch;
        ch='*';
        putch(ch);
    }

    password[i]='';
    printf("\nPassword enterd : %s",password);
    getch();
}

Updated code based on @SouravGhosh's answer:

#include<stdio.h>

int main(void)
{
    char password[25],ch;
    int i;

    //system("clear");
    puts("Enter password: ");

    while(1)
    {
        if(i<0)
            i=0;
        ch=getchar();

        if(ch==13)
            break;

        if(ch==8)
        {
            putchar('b');
            putchar('b');
            i--;
            continue;
        }

        password[i++]=ch;
        ch='*';
        putchar(ch);
    }

    password[i]=' ';
    printf("\nPassword enterd : %s",password);
    getchar();
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • duplicate of: http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed and http://stackoverflow.com/questions/558009/ansi-c-no-echo-keyboard-input – Ali Jul 17 '15 at 09:35
  • Try adding [this code](http://stackoverflow.com/a/912796/3049655) just after `#include `. Then, change all `getchar()`s to `getch()`s and use `int i=0;` instead of `int i;` – Spikatrix Jul 17 '15 at 09:54
  • See also: [stackoverflow.com/q/1196418/1679849](http://stackoverflow.com/q/1196418/1679849) – r3mainer Jul 17 '15 at 10:13
  • 1
    Don't update your question with answers, it confuses the original question. Instead, mark an answer as "correct" or if none are exactly what you need, you can write an answer yourself. – crashmstr Jul 17 '15 at 12:24

3 Answers3

3

Some pointers to start with

  1. Remove conio.h
  2. Replace getch() with getchar() NOTE
  3. void main() to int main(void).
  4. Remove clrscr(). Thanks to Mr. Paul R.

Also note,

  1. getchar() returns an int value. You're trying to collect that into a char. At times, (example, EOF) the return value may not fit into a char. Change ch to int type.
  2. You have an unbound increment of the nindex inside while() loop for input. An overly long input can cause buffer overflow for password. Always limit the index.
  3. After finished taking the input character-by-charcater, null-terminate the array, to use it as a string.

Note: getchar() will echo back the chacrater entered. It will not replace it with *. TO hide the input (i.e., not to echo back), you can do

  1. Use ncurses library. echo() amd noecho() along with initscr() can help you achieving that. This is the preferred way of achieveing what you want.

  2. [Obsolete way] Using getpass() from unistd.h.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • "_Replace `getch()` with `getchar()`_" -- The OP probably wants the program to display `*`s when the user types each character(just like `getch()` works). But using `getchar` will not work like that. – Spikatrix Jul 17 '15 at 09:33
  • i get this ../src/sup.c: In function ‘main’: ../src/sup.c:22:4: warning: implicit declaration of function ‘putch’ [-Wimplicit-function-declaration] putch('b'); ^ ../src/sup.c:34:14: error: empty character constant password[i]=''; ^ ../src/sup.c:37:1: warning: control reaches end of non-void function [-Wreturn-type] –  Jul 17 '15 at 09:33
  • @AmirCausevic Replace `putch` with `putchar` and add `return 0;` at the end of `main`.. – Spikatrix Jul 17 '15 at 09:33
  • getchar() will echo the character. but the OP want's to echo * instead of the input characters. – Ali Jul 17 '15 at 09:34
  • Looking at the next level up, none of those pointers will work until you start fiddling with the termios echo flag. Otherwise the entered password will be echoed and the whole function will be useless. – paxdiablo Jul 17 '15 at 09:36
  • now i get this ../src/sup.c:23:4: warning: passing argument 1 of ‘putchar’ makes integer from pointer without a cast [enabled by default] putchar(NULL); ^ In file included from ../src/sup.c:1:0: /usr/include/stdio.h:580:12: note: expected ‘int’ but argument is of type ‘void *’ extern int putchar (int __c); ^ ../src/sup.c:34:14: error: empty character constant password[i]=''; –  Jul 17 '15 at 09:36
  • @AmirCausevic Remove `putchar(NULL)` and use `password[i]=' ';` instead of `password[i]='';` – Spikatrix Jul 17 '15 at 09:37
  • Well there are no compile errors but it shows the password while im typing and than turns it into ******. Instead of hiding pass with *** and showing the pass ant the end. I have updated the code. –  Jul 17 '15 at 09:42
0

If your terminal supports these escape codes, this will conceal typing as the password is entered.

#include <stdio.h>

void UserPW ( char *pw, size_t pwsize) {
    int i = 0;
    int ch = 0;

    printf ( "\033[8m");//conceal typing
    while ( 1) {
        ch = getchar();
        if ( ch == '\r' || ch == '\n' || ch == EOF) {//get characters until CR or NL
            break;
        }
        if ( i < pwsize - 1) {//do not save pw longer than space in pw
            pw[i] = ch;       //longer pw can be entered but excess is ignored
            pw[i + 1] = '\0';
        }
        i++;
    }
    printf ( "\033[28m");//reveal typing
    printf ( "\033[1;1H\033[2J");//clear screen
}

int main ( ) {
    char password[20];

    printf ( "Enter your password: ");
    fflush ( stdout);//prompt does not have '\n' so make sure it prints
    UserPW ( password, sizeof ( password));//password array and size
    printf ( "\nentered [%s]\n", password);//instead of printing you would verify the entered password
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
0

invoke the compiler with -cpp and save the outputs, this will show you every header file referenced, implicit and explicit. many times this will you to find alternative headers on different platforms.

jobeard
  • 129
  • 6