0

In this function, if a user wants to use the function then it will ask for the password i.e crackneedit. And when the user enters the password it will get printed on the screen as it is

So I want that whenever someone enters a password it is printed in * (asterisk). Which trick should I use?

int pvote()
{
    int check;
    char *str = "crackneedit";
    char pass[11];
    printf("Enter your password: ");
    scanf("%s",pass);

    check = strcmp(pass,str);
    if (check == 0)
    {
        printf("\nVote recorded according to parties are:\n");
        printf("PARTY               VOTE\n\n");
        printf("BJP                 %d\n",b);
        printf("CONGRESS            %d\n",c);
        printf("AAP                 %d\n",a);
        printf("OTHER               %d\n",o);
        getch();
        return(0);
    }
    printf("\nACCESS DENIED\n");
    return(0);
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    Have you tried something or did some research? – Rizier123 Mar 07 '15 at 13:04
  • 1
    see: http://stackoverflow.com/a/6856689/57135 but do not use the 'getpass' suggestion. – Joe Mar 07 '15 at 13:08
  • Use a bit of indentation. There are various libraries for using the terminal - termcap being own – Ed Heal Mar 07 '15 at 13:27
  • 1
    Why do you use parentheses for `return`? it's not a function. Also you can easily make your program misbehave just input `abcdefghijkl` and there you go, you should also use the standard functions correctly and `scanf()` returns a value, don't ignore it. – Iharob Al Asimi Mar 07 '15 at 13:32
  • 1
    @iharob: Some coding standards mandate this. In one that I read, the reason given was for visual consistency with `while`, `for` etc which always require parentheses. That may or may not be convincing, but at least it does no real harm :) – psmears Mar 07 '15 at 13:35
  • It doesn't make sense, but it's ok if some coding standards mandate this, if I was instructed to do this in a job, I would quit. – Iharob Al Asimi Mar 07 '15 at 13:36

2 Answers2

1

The trick you should use is to tell your operating system to stop echoing keyboard input, and, instead, echo each character yourself.

It is your operating system that typically handles keyboard entry, until an entire line of text has been entered, echoing these characters on the screen; then sending the entire line of entered input to your application. You need to make the appropriate system calls to turn off processed keyboard input, and have your operating system send each typed character to your process (including the cursor keys, the enter key, etc).

The details for doing so depend entirely on your operating system. Since you neglected to mention which operating system you're using, no further suggestions can be made.

I should also mention is that you will then need to do a lot more your in your application. You can't use scanf(), for example. You will have to write the code to process each typed character, one at a time, echoing each one manually; handle backspace yourself, building the character buffer, etc...

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

You could use termcaps for that if I'm not wrong.

Everytime you detect (with a read for example) an input, store it in a char * in your code, and print a * on the term instead.

Here's a final version :

#include <unistd.h>
#include <termios.h>
#include <stdio.h>

int                     getch()
{
  struct termios        oldtc, newtc;
  int                   ch;

  tcgetattr(STDIN_FILENO, &oldtc);
  newtc = oldtc;
  newtc.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newtc);
  ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldtc);
  return ch;
}

int             main(int argc, char **argv)
{
  int           c;
  char          ch[20];
  int           i = 0;

  printf("What's your password ?(20 char max.)\n");
  while ((c = getch()) != '\n')
    {
      printf("*");
      ch[i++] = c;
    }
  ch[i] = '\0';
  printf("\nYour password is : [%s]\n", ch);
  return 0;
}

Example :

#include <unistd.h>
#include <termios.h>
#include <stdio.h>

int                     getch()
{
  struct termios        oldtc, newtc;
  int                   ch;

  tcgetattr(STDIN_FILENO, &oldtc);
  newtc = oldtc;
  newtc.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newtc);
  ch=getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldtc);
  return ch;
}

int             main(int argc, char **argv)
{
    int           ch;

  printf("What's your password ?\n");
  while ((ch = getch()) != '\n') // Read 'till you'll type a newline     
      {
      printf("*"); // Print a * instead of what you'll type              
      // Need to store the ch into a char * or whatever.                 
    }
  printf("\n");
  return 0;
}
nookonee
  • 871
  • 2
  • 8
  • 19
  • I'm sorry i don't have much time but I did it for school last year like this : First : do a loop with a `read` for 1 character and store it By usings termcaps and ioctl you can "catch" what the user is typing, so catch it and avoid the printing of the characters and instead, every time `read` read print a `*` Then : When the `read` read a `\n` exit the loop -> So you have all the user typed. – nookonee Mar 07 '15 at 13:26
  • @SiddhantSingh I just added an example without the store part, you can try it. – nookonee Mar 07 '15 at 13:38
  • @SiddhantSingh I added the final version which works. Please validate it. ;) – nookonee Mar 07 '15 at 13:47
  • 1
    It's very commendable of you to write someone else's code for them. It would be quite hillarious if he was actually on Windows, where this is not likely to do anything useful – Sam Varshavchik Mar 07 '15 at 13:51