Receiving error code during output redirection usi

2019-09-10 09:31发布

I am trying to redirect the output from ls to a file, in a shell I created in C. I type in:

ls > junk 

and what I get out is:

ls: cannot access >: No such file or directory

Then if I use CTRL-D to exit the shell it prints the results of the ls command to the screen before exiting. I tried to use print statements to figure out where it is happening and no print statements get printed after:

dup2(f, STDOUT_FILENO); Also tried  dup2(f, 1);

Code:

            pid = fork();

            if(pid == 0)
            {
              // Get the arguments for execvp into a null terminated array  
                    for(i = 0; i <= count; i++)
                    {   if(i == count)
                        {
                            args[i] = (char *)malloc(2 * sizeof(char));
                            args[i] = '\0';
                        }
                        else
                        {
                            str = strlen(string[i]);
                            args[i] = malloc(str);
                            strcpy(args[i], string[i]);                     
                        }
                    }                       

                if(count == 1)
                {

                }
                else if(strcmp(string[(numargs + 1)], ">") == 0) //numargs is the number of arguments typed in by the user
                {
// printed out string[numargs+2] previously, and it says junk
                    int f = open(string[(numargs + 2)], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

                    if(f < 0)
                    {
                        printf("Unable to open output file\n");
                        status = 1;
                    }
                    else
                    {
                        fflush(stdout);
                        dup2(f, STDOUT_FILENO);

                        close(f);

                    }
                }  

                j = execvp(string[0], args); // The first element of the string array is the first thing the user enters which is the command ls in this case

The file called junk gets created, but all that gets placed in it is junk. I have been struggling with this for a while so any help figuring out why the redirection won't work would be greatly appreciated. Thanks.

2条回答
疯言疯语
2楼-- · 2019-09-10 10:00

You cannot use execvp to parse shell commands.

The redirection (>) character is understood by the shell (e.g., bash, sh, ksh) and execvp executes the command you pass it directly. It does not try and interpret the arguments and create file redirections, etc.

If you want to do that you need to use the system call. See System(3)

Similarly, any other special shell characters (pipe, *, ?, &, etc) won't work.

查看更多
对你真心纯属浪费
3楼-- · 2019-09-10 10:00
            j = execvp(string[0], args); // The first element of the string array is the first thing the user enters which is the command ls in this case

This will pass the > to execvp, which is obviously incorrect. You need to remove it from the arguments.

查看更多
登录 后发表回答