簡體   English   中英

執行中的程序正在返回相同的值

[英]Executed program in process return the same value

我的程序應讀取用戶ID和密碼,創建一個新的過程以運行VALIDATE程序(位於validate.c文件中),將用戶ID和密碼發送給VALIDATE程序,如果用戶使用此命令,則打印一條消息“ Password authenticated” ID和密碼是否匹配,或者是“無效密碼”還是“沒有這樣的用戶”,具體取決於驗證程序的返回值。

我的代碼幾乎可以完成所有操作,但是如果用戶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] = '\0';
        }
        else {
            *ptr = '\0';
        }
    }


    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 stdin\n" );
            exit( 1 );
        }
        strip( userid, MAXPASSWD );
        strip( userid, MAXPASSWD );

        printf( "Password:\n" );
        if ( ( fgets( password, MAXLINE, stdin ) ) == NULL ) {
            fprintf( stderr, "Could not read from stdin\n" );
            exit( 1 );
        }
        strip( password, MAXPASSWD );
        pipe( fd );
        pid = fork( );
        if ( pid == -1 ){
            printf( "Error making process\n" );
            return ( -1 );
        }
        if (pid==0){
                close(fd[0]);
                printf("Hey I am the child with pid: %d\n",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 user\n");
    }  else if (WEXITSTATUS(status)==2){
                                printf("Invalid password");
    }else
                                 printf("Password verified\n");
}

}

}




return 0;
}

我不知道您的Validate.c到底包含了什么,因此不知道您的條件是否還可以。 但是我知道這一點:

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

如果WIFEXITED( status )WEXITSTATUS( status )都為非零值,則表示true ,這是在告訴計算機執行我標記為statements1的塊。 否則,意味着這兩個之一為零,您要求計算機再次檢查這兩個,如果兩個都不為零,則執行標記為statements2的塊。

statements2塊可能永遠不會執行,因為它是在條件首先為false,然后突然以某種方式為true的條件下執行的。

除非有其他錯誤,去除if ( WIFEXITED( status ) && WEXITSTATUS( status ) )之后,首先else應該解決您的問題。 您也可以將其轉換為以下形式,即:

...
    if ( WIFEXITED( status ) && WEXITSTATUS( status ) ){
        printf( "Password verified\n" );
    }
    else if ( WEXITSTATUS( status ) == 3 ){
        printf( " No such a user\n" );
    }
    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 verified\n" );
    }
    else if ( WEXITSTATUS( status ) == 3 ){
        printf( " No such a user\n" );
    }
    else if ( WEXITSTATUS( status ) == 2 ){
        printf( "Invalid password" );
    }
...

免責聲明:僅當WIFEXITEDWEXITSTATUS本身都沒有某些全局變量或某些東西,即使它們被調用時,盡管它們使用相同的參數,但它們每次都以某種方式返回不同的值,這一切都是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM