Why is the code asking me for 4 stdin inputs when there should only be 2 stdin inputs?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAXLINE 256
#define MAX_PASSWORD 10  

#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"

int main(void) {
  char user_id[MAXLINE];
  char password[MAXLINE];

  /* The user will type in a user name on one line followed by a password 
     on the next.
     DO NOT add any prompts.  The only output of this program will be one 
         of the messages defined above.
     Please read the comments in validate carefully
   */

  if(fgets(user_id, MAXLINE, stdin) == NULL) {
      perror("fgets");
      exit(1);
  }
  if(fgets(password, MAXLINE, stdin) == NULL) {
      perror("fgets");
      exit(1);
  }

  //TODO
  if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){
          printf(NO_USER);
  }
  int fd[2];
  if(pipe(fd) == -1){
        perror("pipe");
  }

  int status;
  int n = fork();
  if(n < 0){
        perror("fork");
        exit(1);
  } else if (n > 0){
        close(fd[0]);

        if (write(fd[1], user_id, MAXLINE) == -1){
                perror("write to pipe");
        }
        if (write(fd[1], password, MAXLINE) == -1){
                perror("write to pipe");
        }
        close(fd[1]);

        if (wait(&status) == -1){
                perror("wait");
                exit(1);
        }
        if (WIFEXITED(status)){
                int child_status = WEXITSTATUS(status);
                if (child_status == 0){
                        printf(SUCCESS);
                } else if (child_status == 2){
                        printf(INVALID);
                } else {
                        printf(NO_USER);
                }
        }
  } else if (n == 0){
        close(fd[1]);
        execl("./validate", "validate", NULL);
        dup2(fd[0], STDIN_FILENO);
        dup2(fd[0], STDIN_FILENO);
        execl("echo $?", "echo", "$?", NULL);
        close(fd[0]);
        exit(0);
  }


 return 0;


}
                                                     1,1           Top
