使用 dup2() 打印 execvp 命令的标准输出时遇到问题?

问题描述

我正在尝试在 C 中创建一个 shell。我想要做的一件事是让一个文件包含 stdout 的内容。例如,"echo "abcd" > filename" 将创建一个名为 filename 的文件,它将包含 "abcd"。我在实现 dup2 时遇到了问题,因此它打印 exec 的结果以便正确打印。我很感激专家的眼光来查看我的代码并告诉我我缺少什么。提前致谢。

/* ----------------------------------------------------------------- */
/* PROGRAM  shell.c                                                  */
/*    This program reads in an input line,parses the input line     */
/* into tokens,and use execvp() to execute the command.             */
/* ----------------------------------------------------------------- */

#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/* ----------------------------------------------------------------- */
/* FUNCTION  parse:                                                  */
/*    This function takes an input line and parse it into tokens.    */
/* It first replaces all white spaces with zeros until it hits a     */
/* non-white space character which indicates the beginning of an     */
/* argument.  It saves the address to argv[],and then skips all     */
/* non-white spaces which constitute the argument.                   */
/* ----------------------------------------------------------------- */

void  parse(char *line,char **argv)
{
    while (*line != '\0') {       /* if not the end of line ....... */
        while (*line == ' ' || *line == '\t' || *line == '\n')
            *line++ = '\0';     /* replace white spaces with 0    */
        *argv++ = line;          /* save the argument position     */
        while (*line != '\0' && *line != ' ' &&
                *line != '\t' && *line != '\n')
            line++;             /* skip the argument until ...    */
    }
    *argv = '\0';                 /* mark the end of argument list  */
}

/* ----------------------------------------------------------------- */
/* FUNCTION execute:                                                 */
/*    This function receives a commend line argument list with the   */
/* first one being a file name followed by its arguments.  Then,*/
/* this function forks a child process to execute the command using  */
/* system call execvp().                                             */
/* ----------------------------------------------------------------- */

void  execute(char **argv)
{
    pid_t  pid;
    int    status;

    if ((pid = fork()) < 0) {     /* fork a child process           */
        printf("*** ERROR: forking child process Failed\n");
        exit(1);
    }
    else if (pid == 0) {          /* for the child process:         */
        if (execvp(*argv,argv) < 0) {     /* execute the command  */
            printf("*** ERROR: exec Failed\n");
            exit(1);
        }
    }
    else {                                  /* for the parent:      */
        while (wait(&status) != pid)       /* wait for completion  */
            ;
    }
}

/* ----------------------------------------------------------------- */
/*                  The main program starts here                     */
/* ----------------------------------------------------------------- */
int lsh_cd(char **args)
{
    if (args[1] == NULL) {
        fprintf(stderr,"lsh: expected argument to \"cd\"\n");
    } else {
        if (chdir(args[1]) != 0) {
            perror("lsh");
        }
    }
    return 1;
}
void  main(void)
{
    char  line[1024];             /* the input line                 */
    char  *argv[64];              /* the command line argument      */

    while (1) {                   /* repeat until done ....         */
        printf("Shell -> ");     /*   display a prompt             */
        gets(line);              /*   read in the command line     */
        printf("\n");
        for(int i = 0; i < 64; i++)
        {
            argv[i] = "null";
        }
        parse(line,argv);       /*   parse the line               */
        if (strcmp(argv[0],"exit") == 0) {
            /* is it an "exit"?     */
            exit(0);
        }
         /*   makes all the values null if the arguments are null*/
        for(int i = 0; i < 64; i++)
        {
            if(argv[i] == '\0'){
                argv[i]= "null";
            }
        }
         /*  finds out the size of the arguments               */
        int sizeOfArgv = 0;
        for(int i = 0; i < 64; i++)
        {
            if (strcmp(argv[i],"null") != 0) {
                sizeOfArgv++;
            }
        }
         /*finds out if the program has a > in it     */
        bool boolContains = false;
        int x =0;
        for(int j=0;j<sizeOfArgv;j++){
            if(strcmp(argv[j],">") == 0){
                boolContains = true;
                x = j;
            }
        }
        if (strcmp(argv[0],"cd") == 0) {           /*   exit if it is                */
            lsh_cd(argv);
        }else if(boolContains){
            char  *argv2[x];
            for(int t=0;t<x;t++){
                argv2[t]=argv[t];
            }
            char str[80];
            strcpy(str,argv[x+1]);
            strcat(str,".txt");
            int f = open(str,O_WRONLY | O_CREAT | O_Trunc);
            dup2(f,1);
            execute(argv2);
        }
        else{
            execute(argv);
        }      /* otherwise,execute the command */
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)