这是我的代码:
void get_pass(char *p);
int main(){
char *host, *user, *pass;
host = malloc(64); /* spazio per max 64 caratteri */
if(!host) abort(); /* se malloc ritorna NULL allora termino l'esecuzione */
host[63] = '\0'; /* evitare un tipo di buffer overflow impostando l'ultimo byte come NUL byte */
user = malloc(64);
if(!user) abort();
user[63] = '\0';
pass = malloc(64);
if(!pass) abort();
pass[63] = '\0';
/* Immissione di hostname, username e password; controllo inoltre i 'return code' dei vari fscanf e, se non sono 0, esco */
fprintf(stdout,"--> Inserisci <hostname>: ");
if(fscanf(stdin, "%63s", host) == EOF){
fprintf(stdout, "\nErrore, impossibile leggere i dati\n");
exit(EXIT_FAILURE);
}
fprintf(stdout,"\n--> Inserisci <username>: ");
if(fscanf(stdin, "%63s", user) == EOF){
fprintf(stdout, "\nErrore, impossibile leggere i dati\n");
exit(EXIT_FAILURE);
};
fprintf(stdout, "\n--> Inserisci <password>: ");
get_pass(pass);
/* Stampo a video le informazioni immesse */
fprintf(stdout, "\n\nHost: %s\nUser: %s\nPass: %s\n\n", host,user,pass);
/* Azzero il buffer della password e libero la memoria occupata */
memset(pass,0,(strlen(pass)+1));
free(host);
free(user);
free(pass);
return EXIT_SUCCESS;
}
void get_pass(char *p){
/* Grazie a termios.h posso disabilitare l'echoing del terminale (password nascosta) */
struct termios term, term_orig;
tcgetattr(STDIN_FILENO, &term);
term_orig = term;
term.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
/* Leggo la password e controllo il 'return code' di fscanf */
if(fscanf(stdin, "%63s", p) == EOF){
fprintf(stdout, "\nErrore, impossibile leggere i dati\n");
tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
exit(EXIT_FAILURE);
};
/* Reimposto il terminale allo stato originale */
tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
}
我想知道函数get_pass是否没有return
代码是否正确?
在这个函数中,我用fscanf
读取密码,然后我认为我必须将它返回到主程序......但是:
return p;
我收到了警告)return p;
所以我觉得一切都好......但我不太确定...... 我不明白如何使用函数返回工作。
答案 0 :(得分:2)
void
返回类型的函数不应返回任何内容。return;
语句随时从函数返回给调用者。return;
语句,并且该功能到达其结尾,则控件将返回其调用者。答案 1 :(得分:2)
int main()
{
char * password = NULL;
get_pass(&password); //that is how you want to pass pointer to pointer
}
void get_pass(char **password)
{
//1. get password using scanf from stdin or whatever way you want.
//2. assign it to *password
//3. no need to return anything
}
请记住,您需要处理密码字符串的内存分配。假设您要将密码大小修改为MAX_PASS_SIZE,然后在main()或get_pass中分配那么多内存,这样就不会损坏堆栈内存。我写的上面的代码片段只显示了如何将值填充到密码中,这可能是您的主要问题,即将指针传递给指针。
答案 2 :(得分:1)
get_pass被定义为返回void,这没什么。在这种情况下,返回值通过参数p。
传递答案 3 :(得分:1)
如果您希望能够在密码中允许空格,可以使用以下内容。关闭行缓冲并一次处理一个字符,而不是使用fscanf
。
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void get_pass(char *p);
void flushStdin( void );
int main(){
// what you have now in your code.
}
void flushStdin( void )
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
return;
}
void get_pass(char *p){
int i = 0;
int c;
/* Grazie a termios.h posso disabilitare l'echoing del terminale (password nascosta) */
struct termios term, term_orig;
tcgetattr(STDIN_FILENO, &term);
term_orig = term;
term.c_lflag &= ~ECHO;
term.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
/* Leggo la password e controllo il 'return code' di fscanf */
flushStdin();
while( (( c = getchar() ) != '\n') && (i < 63) )
{
if( c != 127 ) // did user hit the backspace key?
{
p[i++] = (char)c;
}
else
{
// null last character in password and backup to one space in string
// should make sure i doesn't go negative... oops.
if( i > 0 )
{
p[--i] = 0x00;
}
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
return;
}