c语言 - 进程中执行的程序返回相同的值



我的程序应该读取用户标识和密码,创建一个新进程来运行验证程序(在文件validate.c中),向验证程序发送用户标识和密码,如果用户标识和密码匹配,则打印消息"密码已验证",或者"密码无效"或"没有这样的用户",具体取决于验证程序的返回值。

我的代码几乎可以完成所有操作,但我不知道如何实现有关打印消息"密码已验证"的最后一部分,如果用户 ID 和密码匹配,或者"密码无效"或"没有这样的用户"取决于验证程序的返回值......它总是打印我密码验证,即使它不是.....任何帮助将不胜感激。对不起,如果这个问题太简单了....我是C语言的初学者。任何帮助将不胜感激....谢谢....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAXPASSWD 10
void strip( char *str, int capacity ) {
char *ptr;
if ( ( ptr = strchr( str, 'n' ) ) == NULL ) {
str[capacity - 1] = '';
}
else {
*ptr = '';
}
}

int main( void ) {
char userid[MAXLINE];
char password[MAXLINE];
pid_t pid;
int fd[2];
/* Read a user id and password from stdin */
printf( "User id:n" );
if ( ( fgets( userid, MAXLINE, stdin ) ) == NULL ) {
fprintf( stderr, "Could not read from stdinn" );
exit( 1 );
}
strip( userid, MAXPASSWD );
strip( userid, MAXPASSWD );
printf( "Password:n" );
if ( ( fgets( password, MAXLINE, stdin ) ) == NULL ) {
fprintf( stderr, "Could not read from stdinn" );
exit( 1 );
}
strip( password, MAXPASSWD );
pipe( fd );
pid = fork( );
if ( pid == -1 ){
printf( "Error making processn" );
return ( -1 );
}
if (pid==0){
close(fd[0]);
printf("Hey I am the child with pid: %dn",getpid());
execl("/h/u15/c2/00/c2rsaldi/csc209labs/ex5/validate.c","validate.c", NULL);           
}    
/*Your code here*/
int status;   
if (pid>0){
close(fd[1]);
write(fd[1],password,(strlen(password)-1));
write(fd[1],userid,(strlen(userid)-1));
if (waitpid(pid,&status,0)==0){
if (WIFEXITED(status) && !WEXITSTATUS(status)){
if (WEXITSTATUS(status)==3){
printf(" No such a usern");
}  else if (WEXITSTATUS(status)==2){
printf("Invalid password");
}else
printf("Password verifiedn");
}
}
}


return 0;
}

我无法知道您的Validate.c到底包含什么,因此无法知道您的条件是否正常。但我知道这一点:

...
if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
//statements1
printf( "Password verifiedn" );
}
else if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
//statements2
if ( WEXITSTATUS( status ) == 3 ){
printf( " No such a usern" );
}
else if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
if ( WEXITSTATUS( status ) == 2 ){
printf( "Invalid password" );
}
}
}
...

在这里,你告诉您的计算机执行我标记为statements1的块,如果WIFEXITED( status )WEXITSTATUS( status )都是非零的,这意味着true。否则,这意味着这两个中的任何一个零,你要求您的计算机再次检查这两个,如果两者都不为零,则执行我标记为statements2的块。

statements2块可能永远不会被执行,因为它是在条件首先为假,然后以某种方式突然为真的条件下执行的。

除非有其他错误,否则在第一个else之后立即删除该if ( WIFEXITED( status ) && WEXITSTATUS( status ) )应该可以解决您的问题。您也可以将其转换为以下形式,这将是等效的:

...
if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
printf( "Password verifiedn" );
}
else if ( WEXITSTATUS( status ) == 3 ){
printf( " No such a usern" );
}
else if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
if ( WEXITSTATUS( status ) == 2 ){
printf( "Invalid password" );
}
}
...

嗯,还有另一个问题...出于同样的原因,可能永远无法访问第二个else if的内容。您可能还必须删除该if ( WIFEXITED( status ) && WEXITSTATUS( status ) )。等效地:

...
if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
printf( "Password verifiedn" );
}
else if ( WEXITSTATUS( status ) == 3 ){
printf( " No such a usern" );
}
else if ( WEXITSTATUS( status ) == 2 ){
printf( "Invalid password" );
}
...

免责声明:这些都是正确的,只有当WIFEXITEDWEXITSTATUS本身都没有一些全局变量或其他东西时,导致它们每次被调用时都会返回不同的值,尽管它们被调用了相同的参数。

最新更新