c - C 中的 shell : How to execute builtin command with fork() and execve()

标签 c shell

我正在尝试用 C 语言创建一个 shell,但我被困在使内置函数正常运行的点上。

这是我的代码:

int my_fork(char *line, char **env)
{
    /*char *line = arguments entered in getline() (like 'ls' for example)*/
    /*char **env = copy of the shell environment*/
    int i = 0;
    /*Get the PID for forking*/
    pid_t pid = getpid();
    /*Declaration of one string and one array*/
    char *path = malloc(sizeof(char) * 100);
    char **tmp = malloc(sizeof(char *) * 7);
    /* my_str_to_word_array transform a string into an array of word*/
    char **arg = my_str_to_word_array(line);

    for (int j = 0; j < 30; j++)
        tmp[j] = malloc(sizeof(char) * 100);
        /*my_getpath() search and found the PATH variable in the environment*/
    path = my_getpath(env);
    /*Then, we put the different paths in an array (paths are separated by ':')*/
    tmp = my_str_to_word_array(path);
    for (int j = 0; j < 7; j++) {
            /*my_strcat() put a string after another string*/
            /*Here, i put the entered command after all the paths*/
            my_strcat(tmp[j], "/");
            my_strcat(tmp[j], arg[0]);
            /*my_putstr() print a string, here, i try to print all the modified paths*/
            my_putstr(tmp[j]);
            write (1, "\n", 1);
    }
    /*i fork and exec the command*/
    pid = fork();
    execve(tmp[i], arg, env);
    /*i wait until the child process end*/
    wait(&pid);
        return (0);
}

输出给我一个段错误信号,我不知道为什么。 Valgrind 没有帮助我,我现在不知道该怎么办......

我遇到的错误是:“访问不在映射区域 0x0 内”。此错误发生在计算字符串长度的函数“my_strlen()”中。我已经搜索了为什么会出现此错误,但我不明白,因为在我的字符串末尾,我肯定有'\0'。

我的_strcat():

char    *my_strcat(char *dest, char *src)
{
    int i = 0;
    int len = my_strlen(dest);

    for (; src[i] != '\0'; i++)
        dest[len + i] = src[i];
    i++;
    dest[len + i] = '\0';
    return (dest);
}

我的_strlen():

    int my_strlen (char *str)
    {
    int i = 0;
    while (str[i])
        i++;
    return (i);
    }

所以,总而言之,我想让内置函数发挥作用,但我在 my_strlen() 中有一个错误,这不是我遇到的唯一错误,但现在,让我们关注这个错误。

最佳答案

my_str_to_word_array():

char **my_str_to_word_array(char *str)
{
        int i = 0;
        int j = 0;
        int m = 0;
        int len = my_strlen(str);
        char **tmp = malloc(sizeof(char *) * 10);

        for (int n = 0; n < 10; n++)
                tmp[n] = malloc(sizeof(char) * 50);
        while (j < len) {
                m = 0;
                while ((str[j] == ':') || (str[j] == ' ') || (str[j] == '\n'))
                        j++;
                while ((str[j] != ':') && (str[j] != ' ') && (str[j] != '\n'))
                        tmp[i][m++] = str[j++];
                tmp[i][m + 1] = '\0';
                i++;
        }
        tmp[i] = NULL;
        return (tmp);
}

我的获取路径():

char *my_getpath(char **env)
{
    int i = 0;
    char *tmp = NULL;
    int len = 0;

    while (my_strncmp(env[i], "PATH=", 5) != 0)
        i++;
    len = my_strlen(env[i]);
    env[i][len + 1] = '\0';
    tmp = malloc(sizeof(char) * my_strlen(env[i]));
    my_printf("Path found\nLen: %d\n", my_strlen(env[i]));
    my_printf("Path: ");
    my_putstr(env[i]);
    write(1, "\n", 1);
    tmp = my_strncpy(tmp, env[i], 5);
    return (tmp);
}

my_strncpy():

    char *my_strncpy (char *src, int n)
{
        int i = 0;
        char *dest = malloc(sizeof(char));
        int len = my_strlen(src);
        for (; n <= len; i++) {
                dest[i] = src[n];
                n++;
        }
        dest[i] = '\0';
    return (dest);
}

在 getpath() 之后打印的路径给我一个段错误,当我用 valgrind 查看时,它告诉我这是因为 my_strlen()。

但是当我不打印它时,它起作用了,所以我不认为这是因为 my_strlen()。

输出:

Path found
Len: 124
Path: PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/tyrone/.local/bin:/home/tyrone/bin

Segmentation fault (core dumped)

当我看到这个时,我认为它在 my_strncpy 中,但我看不出错误在哪里。

关于c - C 中的 shell : How to execute builtin command with fork() and execve(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475309/

相关文章:

c++ - 在 windows xp 中获取所有已注册的 usb hid 设备的列表

c - 是否有测试是否在 int 中的任何位置设置非特定位的技巧?

macos - 检查两个文件是否在 bash 中的同一卷上

c++ - 如何获取桌面的窗口句柄?

linux - Unix shell 脚本 : assign value from file to variable into while loop, 并在循环外使用这个值

python - bash shell 将当前文件的路径存储为变量?

java - 同时通过不同的shell运行同一个java类

c - 如何输入一个数字并按降序显示?

c - 在 Ubuntu 上的外部终端中使用 Sublime Text 2 运行简单的 C 程序

c - 需要帮忙进行数组赋值