2015-09-04 30 views
0

今天我是在作弄我的东西,在某些时候,我需要getpass函数时,一切都还OK,以及至少直到我用--std = C11我得到:为getpass函数时更好的选择

error: implicit declaration of function ‘getpass’ [-Werror=implicit-function-declaration]|

在互联网上我发现这个功能被认为是过时:

The getpass() function is not threadsafe because it manipulates global signal state. 

The getpass() function is scheduled to be withdrawn from a future version of the X/Open CAE Specification. 

所以我不能用它,至少不是如果我编译--std = c11

我尝试的程序是这样的:

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

    int main(void){ 
     char checkPasswd[BUFSIZ] = "iajsdiajsda32121312asda:ubuntu"; 
     char *checkName = "michi"; 

     char name[25]; 
     char *passwd; 
     char *delimiter; 

     printf("Login Name:\t"); 
     scanf("%24s" ,name); 

     delimiter = strchr(checkPasswd, ':'); 
     delimiter[0] = '\0'; 
     delimiter++; 

     passwd = getpass("Give a passwd:\t"); 

     if(strcmp(name,checkName) == 0){ 
      if(strcmp(passwd,delimiter) == 0){ 
       printf("Logged In\n\n"); 
      }else{ 
       printf("\t\t\tWrong Password\n"); 
       exit(1); 
      } 
     }else{ 
      printf("\t\t\tWrong Name\n"); 
      exit(1); 
     } 

     printf("\t\t\tWelcome %s\n\n", name); 
     return 0; 
    } 

我读的东西在这里== >>Getting a password in C without using getpass (3)?并且也有一些答案,但我不知道是否有上午简单的方法之前,我尝试了我在那里发现,类似于:

passwd = getpass("Give a passwd:\t"); 

或者我要创建我的自我?

这个程序没有做什么,我需要:

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

int main(void){ 
    struct termios oflags, nflags; 
    char password[64]; 

    tcgetattr(fileno(stdin), &oflags); 
    nflags = oflags; 
    nflags.c_lflag &= ~ECHO; 
    nflags.c_lflag |= ECHONL; 

    if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { 
     perror("tcsetattr"); 
     return EXIT_FAILURE; 
    } 

    printf("password: "); 
    fgets(password, sizeof(password), stdin); 
    password[strlen(password) - 1] = 0; 
    printf("you typed '%s'\n", password); 

    if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { 
     perror("tcsetattr"); 
     return EXIT_FAILURE; 
    } 

    return 0; 
} 

所以现在的问题是,我必须把它写我自己或者有一个复用功能?

+0

你从哪里得到的#include''?这不是关于链接问题的接受答案。 – ecatmur

+1

[在不使用getpass的情况下在C中获取密码?](http://stackoverflow.com/questions/1196418/getting-a-password-in-c-without-using-getpass-3) – Olaf

+0

@olaf我刚刚在我的问题中提出了这个链接,我需要别的东西,否则我必须自己写。 – Michi

回答

2

如果您的终端支持这些转义码,这将隐藏您输入的密码。

#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[0A");//move cursor up one line 
    printf ("\033[21C");//move cursor 21 places 
    while (i) { 
     printf ("*");//overwrite password on screen. this is still concealed 
     i--; 
    } 
    printf ("\033[28m");//reveal typing 
} 

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; 
} 
+0

所以答案是,是的,我必须自己实现它。谢谢你的作品。 – Michi

0

让我给一些意见在自己的写作:

getpass()只读取8个字符,所以不要使用它。

自己的代码应该以允许检测用户是否输入比预期更多的字符的方式书写。

filedes.h不存在并且不需要。

paswd通常从/ dev/tty或从stderr中读取。

如果您不捕获重要信号,代码可能会使tty处于不可用状态。

见工作实现UNIX源代码:

http://sourceforge.net/p/schillix-on/schillix-on/ci/default/tree/usr/src/lib/libc/port/stdio/getpass.c