【问题标题】:Pass parameter to another function using C使用C将参数传递给另一个函数
【发布时间】:2016-07-14 21:30:24
【问题描述】:
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
}

以上是 validate.c 的实现,但是如何使用 pipe(),dup2 或 execl() 将用户 ID 和密码等值传递给函数

我使用了以下`

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;

}

但提示总是要求我重新输入输入。这种方法有什么问题?(注意:我“execl”“validate”因为它是一个已经创建好的可执行文件。我写的execl()只是调用了validate.c函数)

【问题讨论】:

  • 使用管道、dup2 和 execl,您试图生成一个不同的进程并将一个进程的标准输出重定向到另一个进程的标准输入。这些应该是2个不同的过程有什么原因吗?如果您只需要在程序中有不同的文件,这不是正确的方法
  • 你的管道向后看。
  • 没有2个不同的进程,一个代表父进程,另一个代表子进程。此程序中的文件不允许
  • @melpomene 我该如何解决?
  • @LeonZhang 所以你需要为 validate.c 生成一个不同的进程?

标签: c pipe dup2 execl


【解决方案1】:

正如我在 cmets 中所说,您可能不需要为此生成另一个进程,但您调用 execl 的方式有误。

这个:

execl("validate",other);

应该是:

execl(filename,list of arguments, NULL);

This is the documentation page。他们使用 (char *) 0 与使用 NULL 相同。

【讨论】:

  • 我认为“其他”有点没用,因为我从未将它们设置为我应该使用的值。另一方面,我认为 execl 不应该包含任何参数,因为 validate 中的 main() 函数不接受任何参数。
  • 然后传入 NULL 而不是其他。这可能不是代码的问题,但在 c 中,NULL 告诉函数何时到达数组的末尾,所以它很重要
猜你喜欢
  • 1970-01-01
  • 2012-09-25
  • 2020-10-03
  • 1970-01-01
  • 2020-04-20
  • 2014-01-06
  • 2019-10-09
  • 2012-04-09
相关资源
最近更新 更多