0

I'm trying to implement << redirection in C, I'm having issues with the code below. I can implement the other three redirection <, >, and >>. I think I need a loop to handle/check the delimiter of <<, How can I handle this problem? When I run the program I get

 /usr/bin/cat: '<<': No such file or directory
 /usr/bin/cat: EOF: No such file or directory

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main(void)
{
    char *argv[] = { "/usr/bin/cat", "<<", "EOF", 0 };
    char *envp[] =
    {
        "HOME=/",
        "PATH=/bin:/usr/bin",
        "USER=julekgwa",
        0
    };
    int fd = open(0, O_RDONLY);
    dup2(fd, 0);
    close(fd);
    execve(argv[0], &argv[0], envp);
    fprintf(stderr, "Oops!\n");
    return -1;
}
3
  • 1
    What is ` <<` supposed to do? How does it differ from <? Commented Dec 11, 2016 at 3:24
  • 1
    Specifically, what are the issues? What is going on, and how is it different from what you expect? Commented Dec 11, 2016 at 3:25
  • << : A here document. It is often used to print multi-line strings. Commented Dec 11, 2016 at 3:26

1 Answer 1

1

Any kind of redirection is a shell feature. You are using execve which is executing cat directly, not giving shell a chance to do anything.

The following might work:

char *argv[] = { "/bin/bash", "-c", "/usr/bin/cat <<EOF\ntest\nEOF", 0};

But it's unlikely you really want to do that.

Sign up to request clarification or add additional context in comments.

1 Comment

I have checked every tutorial and no one has ever implemented this <<.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.