This code is meant to read 2 lines from standard input, and send them to the child. The child is then meant to execute the validate process with the user and password sent from the parent and return an exit code. Currently, when run the code is on an infinite loop
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAXLINE 256

#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: Error checking
  int fd[2], status, r, exit_status;

  if (pipe(fd) == -1) {
     perror("pipe");
     exit(1);
  }
  
  // fork
  r = fork();
  if (r < 0) {
    perror("fork");
  }

  // Child process
  if (r == 0) {
    // Read from the pipe
    dup2(fd[0], fileno(stdin));
    char user[MAXLINE];
    char pswd[MAXLINE];

    read(fd[0], user, MAXLINE);
    read(fd[0], pswd, MAXLINE);

    // Close the read end of the pipe
    close(fd[0]);

    dup2(fd[1], fileno(stdout));
    execl("./validate", "validate", user, pswd, NULL);
    close(fd[1]);
    perror("execl");
    exit(-1);
  } else if (r > 0) {
    // Parent process
    // Write to the pipe
    dup2(fd[1], fileno(stdout));
    write(fd[1], user_id, strlen(user_id) + 1);
    write(fd[1], password, strlen(password) + 1);

    close(fd[1]);
    wait(&status);
    if (WIFEXITED(status)) {
      exit_status = WEXITSTATUS(status);
      if (exit_status == 0) {
          printf(SUCCESS);
      } else if (exit_status == 2) {
          printf(INVALID);
      } else if (exit_status == 3) {
          printf(NO_USER);
      } else {
          fprintf(stderr, "Invalid exit status %d\n", exit_status);
          exit(EXIT_FAILURE);
      }
    }
  }
  
  return 0;
}
