更改不相关的代码会导致段错误。它为什么要这样做?

标签 c shell segmentation-fault

我正在创建自己的 Shell,并通过使用 is_background 函数查找 & 成功地让进程在后台运行。它工作正常,直到我尝试实现标准输出的重定向。 chk_if_output 函数以及 process 函数中的 if 语句 if(out[0] == 1) 都是其中的一部分。不知何故,实现重定向搞砸了我实现后台进程的方式。如果我注释掉重定向代码,它会再次起作用。每次我尝试使用程序中的重定向代码运行后台进程时,我都会遇到段错误,但我一生都无法弄清楚原因。我没有更改任何后台进程代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* The maximum length command */

int is_background(char *args[], int size){
    int background = 0;
    if (strcmp(args[size-1 ], "&") == 0){
        background = 1;
        args[size-1] = NULL;
    }
    return background;
}

int * chk_if_output(char *args[], int size){
    int * out = malloc(2);
    out[0] = 0; out[1] = 0;
    for (int i = 0; i < size; i++){
        if (strcmp(args[i],">") == 0){
            out[0] = 1;
            out[1] = i;
            break;
        }
    }   
    return out;
}
void process(char *command, char *params[], int size){
    pid_t pid;
    int background = is_background(params, size);
    int *out = chk_if_output(params, size);
    int fd;
    int fd2;
    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "Fork Failed\n");
    }else if (pid == 0) {
        if(out[0] == 1){
            for (int i = out[1]; i < size; i++){
                params[i] = params[i+1];
            }
            fd = open(params[out[1]-1],O_RDONLY,0);
            dup2(fd,STDIN_FILENO);
            close(fd);
            fd2 = creat(params[out[1]],0644);
            dup2(fd2,STDOUT_FILENO);
            close(fd2);
            out[0] = 0;
            out[1] = 0;     
        }
        execvp(command, params);
    }else {
        if(background == 1){
            waitpid(pid, NULL, 0);
        }
        background = 0;
    }
}
int main(void) { 
    char *args[MAX_LINE/2 + 1]; /* command line arguments */ 
    int should_run = 1; /* flag to determine when to exit program */
    while (should_run) {
        char *line;
        char *endline; 
        printf("Leyden_osh>");

        fgets(line, MAX_LINE*sizeof line, stdin);

        if((endline = strchr(line, '\n')) != NULL){
            *endline = '\0';
        }

        if (strcmp((const char *)line,"exit") == 0){
            should_run = 0;
        }

        int i = 0;
        args[i] = strtok(line, " ");
        do{
            args[++i] = strtok(NULL, " ");
        }while(args[i] != NULL);

        process(args[0], args, i);

        fflush(stdout);

        return 0;
    }

最佳答案

在 chk_if_output() 函数中,循环中数组的最后一个元素为 NULL。

通过循环到大小 -1 来修复它。

关于更改不相关的代码会导致段错误。它为什么要这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42545699/

相关文章:

c++ - C++ 中的 Trie 实现

c - 网络编程,从客户端发送文件到服务器

c - 这个堆栈实现的问题

python - 为什么以下循环不会中断 - Python?

c - 尝试读取文件时出现段错误?

c++ - Linux - 有时只会出现段错误 - 如何调试

C90 不允许在 printf 中使用 %lf,为什么?

c - 尝试创建一个程序,在用户提供的数组中的每个整数之间插入一个 0

linux - 在 Shell 脚本中分析文件

linux - 如何使用shell脚本访问linux中的特定路径