【问题标题】:fork() and exec() functions in CC 中的 fork() 和 exec() 函数
【发布时间】:2015-04-02 18:53:35
【问题描述】:

所以基本上我正在编写一个程序,它重复地从标准输入中读取输入行,并将它们拆分为一个 char **array。对于每一行,将第一个元素视为要执行的程序的完整路径。执行程序,将行中的其余项目作为参数传递。如果该行为空,则什么也不做,然后转到下一行。重复直到第一个元素是字符串“exit”。

我的问题是:

  1. 当我输入“exit”时,strcmp(l[0], "exit") 返回 10 而不是 0。为什么?
  2. 程序已编译但仅适用于奇数个参数。 例如,如果我输入“/bin/echo This is good”,它会打印“This is good” 但是如果我输入“/bin/echo This is very good”,它会打印“error”。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFSIZE 10000
int space;
char** createArray(char *line) {
    int len_line = strlen(line);
    char **wordArray = NULL;    
    if(len_line > 1){       
        char *token = strtok(line, " ");    
        space = 0;
        while (token) {
            wordArray = realloc(wordArray, sizeof(char*)*++space);
            wordArray[space-1] = token;
            token = strtok(NULL, " ");
        }
    }
    return wordArray;
}


int main(int argc, const char* argv[]) {    
    char line[BUFSIZE];
    while (1) { 
        printf("%s\n", ">");
        fgets(line, BUFSIZE, stdin);
        char** l = createArray(line);

        if (l) {
            /*why instaed of zero, when l[0] 
            /*equals quit, strcmp() returns 10?*/   
            printf("%d\n", strcmp(l[0], "exit"));       
            if(strcmp(l[0], "exit")==10) {
                exit(0);
            }
            else if(fork() == 0) {
                execv(l[0], l);
                printf("%s\n", "Error!");
            }
        }
    }
    return 1;
} 

【问题讨论】:

  • if(strcmp(l[0], "exit")==10)...或者你的意思是if(strcmp(l[0], "exit") == 0)
  • 期望 strcmp() 准确返回 10 是相当奇怪的。它返回零、正数(可能是 10)或负数。
  • 你真的应该编译所有警告和调试信息(例如gcc -Wall -Wextra -g)然后使用调试器gdb

标签: c


【解决方案1】:

你忘了fgets() 也返回一个换行符吧?

fgets()阅读后,用这一行消除它:

line[strlen(line) - 1] = '\0';

请再试一次!

【解决方案2】:

来到你面临的第一个问题

1. when I input "exit", the strcmp(l[0], "exit") returns 10 instead of 0. Why?

当你键入“exit”并按回车键时,fgets 读取的字符串是“exit\n”。将 "exit" 与 "exit\n" 进行比较得到 10。要么在比较之前从末尾删除 "\n",要么将比较方法更改为

if(strcmp(l[0], "exit\n") == 0)

请参阅下面的更正代码

int main(int argc, const char* argv[]) {
    char line[BUFSIZE];
    while (1) {
        printf("%s\n", ">");
        fgets(line, BUFSIZE, stdin);
        int len = strlen(line);
        line[len - 1] = 0;
        char** l = createArray(line);

        if (l) {
            /*why instaed of zero, when l[0]
             equals quit, strcmp() returns 10?*/
            printf("%d\n", strcmp(l[0], "exit"));
            if(strcmp(l[0], "exit")==0) {
                exit(0);
            }
            else if(fork() == 0) {
                execv(l[0], l);
                printf("%s\n", "Error!");
            }
        }
    }
    return 1;
}

关于第二个问题

2. The program got compiled but only works for odd number of arguments. For example, if I input "/bin/echo This is good", it prints "This is good" But if I input "/bin/echo This is very good", it prints "error".

尝试使用下面给出的更正代码。有用。这个问题已经不存在了。

【讨论】:

  • "exit\n" 比较是no 选项,因为他的shell 仍然无法工作。他必须删除换行符。
猜你喜欢
  • 1970-01-01
  • 2013-05-06
  • 2015-12-01
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多