0

My program should read a user id and password, creates a new process to run the VALIDATE program (which is in file validate.c), sends to VALIDATE program the user id and password, and prints a message "Password verified" if the user id and password matched or either "Invalid password" or "No such user" depending on the return value of the validate program.

My code does almost everything but I do not know how to implement last part about printing a message "Password verified" if the user id and password matched or either "Invalid password" or "No such user" depending on the return value of the validate program…. It always prints me PASSWORD VERIFIED even if it is not….. Any help will appreciated. Sorry if it is too simple question….I am beginner in C. Any help will be appreciated….thanks….

    #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;
}
7
  • What value do you get in status ? Mar 7, 2014 at 19:02
  • status is the value which i get from validate program right?! how to check it?! i even did not check the return value of the status…. Mar 7, 2014 at 19:04
  • You use ` && WEXITSTATUS(status)` in one branch, but was it ever your intention to use ` && !WEXITSTATUS(status)` for any other? Mar 7, 2014 at 19:04
  • Why do you exec a .c file and not the executable? Why do you write the username password without their last characters, and not separated? Is an exit status of 0 a "verified" or "failed" condition? Mar 7, 2014 at 19:08
  • Why do you not close() the pipe ends you are not using in both the child and parent, and why does the parent not close() the write end of the pipe once it's done write()'ing the username and password? Mar 7, 2014 at 19:12

1 Answer 1

1

I cannot know what exactly your Validate.c has in it, therefore cannot know much about whether your conditions are all right. But I know this:

...
    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" );
            }
        }
    }
...

Here, you are telling your computer to execute the block I labelled as statements1 if both the WIFEXITED( status ) and WEXITSTATUS( status ) are something non-zero, meaning true. Otherwise, meaning that either one of those two are zero, you ask your computer to check those two again, and execute the block I labelled as statements2 if both are non-zero.

The statements2 block may never get executed, because it is to be executed under the condition that a condition is first false, and then somehow suddenly true.

Unless there are other mistakes, removing that if ( WIFEXITED( status ) && WEXITSTATUS( status ) ) right after that first else should fix your issue. You can also bring it into the following form, which would be equivalent:

...
    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" );
        }
    }
...

Well, there, another problem... The contents of the second else if may never be accessed, due to same reason. You probably have to remove that if ( WIFEXITED( status ) && WEXITSTATUS( status ) ) as well. Equivalently:

...
    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" );
    }
...

Disclaimer: These are all correct, only if both WIFEXITED and WEXITSTATUS don't have some global variables or something in themselves, causing them to return somehow different values each time they are called, despite having been called with identical arguments.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.