0
int main(void){
 int n, user_length;
 char userid[30];
 char password[11];

 if ((n = read(STDIN_FILENO, userid, 10)) == -1) {
    perror("read");
    exit(1);
} else if(n == 0) {
    fprintf(stderr, "Error: could not read from stdin");
    exit(1);
}
if (userid[n-1] == '\n')
    userid[n-1] = '\0';
else
    userid[n] = '\0';

if ((n = read(STDIN_FILENO, password, 10)) == -1) {
    perror("read");
    exit(1);
} else if (n == 0) {
    fprintf(stderr, "Error: could not read from stdin");
    exit(1);
}
if (password[n-1] == '\n')
    password[n-1] = '\0';
else
    password[n] = '\0';

strcat(userid, ":");
user_length = strlen(userid);
strcat(userid, password);
FILE *fp = fopen(PASSWORD_FILE, "r");
if (!fp) {
    perror("fopen");
    exit(1);
}
char line[MAXLINE];
while(fgets(line, sizeof(line) - 1, fp)) {
    line[strlen(line) - 1] = '\0';
    if (strcmp(userid, line) == 0)
        exit(0); // found match
    else if(strncmp(userid, line, user_length) == 0)
        exit (2); // invalid password
 }
 exit(3); // no such user
}

Above is the implementation of validate.c, but how do I pass value such as userid and password to the function by using pipe(),dup2 or execl()

I used the following`

int main(void) {
    char userid[10];
    char password[10];
    int   pid;
    int p[2][4];
   char other[MAXSIZE];

/* Read a user id and password from stdin */
   printf("User id:\n");
    scanf("%s", userid);
printf("Password:\n");
scanf("%s", password);
/*Your code here*/
if (pipe(p[1]) == -1) {
    perror("pipe");
}
if (pipe(p[0]) == -1) {
    perror("pipe");
}
pid = fork();
if (pid != 0) {


  close(p[1][0]);
  close(p[0][0]);

  dup2(p[1][1],STDIN_FILENO);
  dup2(p[0][1],STDIN_FILENO);

  close(p[1][1]);
  close(p[0][1]);


  int status;
  if (wait(&status)!= -1)  {
    if (WIFEXITED(status)) {
      printf("[%d] Child exited with %d\n", getpid(),  WEXITSTATUS(status));
      switch(WEXITSTATUS(status)){
        case 0:
          printf("found match\n");
          break;
        case 2:
          printf("invalid password\n");
          break;
        case 3:
          printf("No such user\n");
          break;
        default:
          printf("error has occur\n");
          break;
      };

    } else {
      printf("[%d] Child exited abnormally\n", getpid());
    }
  }
} else if (pid == 0) {

  close(p[1][1]);
  close(p[0][1]);
  dup2(p[1][0], fileno(stdout));
  dup2(p[1][0], fileno(stdout));
  execl("validate",other);
  printf("what\n");

  close(p[1][0]);
  close(p[0][0]);

} else {
  perror("fork");
  exit(1);
}
return 0;

}

But the prompt always asks me for re-entering the input. What is wrong with this approach?( Note: I "execl" "validate" because it is an executable file that has been already created. The execl() I wrote simply calls the validate.c function )

1 Answers1

1

As I said in the comments you probably do not need to spawn another process for this but You have an error in the way you call execl.

This:

execl("validate",other);

Should be:

execl(filename,list of arguments, NULL);

This is the documentation page. They use (char *) 0 which is the same as using NULL.

Nicola Pedretti
  • 4,831
  • 3
  • 36
  • 42
  • I think "other" is kind of useless because I never set them to the value that I supposed to use. On the other hand, I don't think execl should contain any parameters since the main() function in validate takes no arguments. – Leon Zhang Jul 14 '16 at 22:20
  • Then pass in NULL instead of other. This might not be the problem with the code but in c NULL tells a function when it reached the end of an array, so it is important – Nicola Pedretti Jul 14 '16 at 22:33