The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#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
   */
  int status;
  int ret;
  int pipe_fd[2];

  if(fgets(user_id, MAXLINE, stdin) == NULL) {
      perror("fgets");
      exit(1);
  }
  if(fgets(password, MAXLINE, stdin) == NULL) {
      perror("fgets");
      exit(1);
  };
  if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) {
    perror("username or password is too long\n");
    exit(1);
  }
  // TODO
  if ((pipe(pipe_fd)) == -1) {
      perror("pipe");
      exit(1);
  }
  int read_end = pipe_fd[0];
  int write_end = pipe_fd[1];

  ret = fork();
  if (ret < 0) {
      perror("fork");
      exit(1);
  }
  if (ret == 0) {
    close(write_end);
    dup2(read_end, STDIN_FILENO);
    execl("./validate", "validate", NULL);

  } else if (ret > 0) {
    close(read_end);
    int user_len = strlen(user_id);
    int passwd_len = strlen(password);
    write(write_end, &user_len, sizeof(char));
    write(write_end, &passwd_len, sizeof(char));
    close(write_end);
    wait(&status);
    if ((ret = wait(&status)) == -1) {
      perror("wait");
      exit(1);
    }
    if (WIFEXITED(status)) {
      int output = WEXITSTATUS(status);
      if (output == 0){
        printf(SUCCESS);
      } else if (output == 2){
        printf(INVALID);
      } else if (output == 3) {
        printf(NO_USER);
      }
    }

  } 
  
  return 0;
}
