I'm supposed to write a simple custom shell in C that can handle redirection with just the "<" and ">" commands.
To do this I'm parsing each command (in an array of strings), checking for the characters '<' and '>' and then opening a file name with open(fd, filename, flags)
to either read or write to.
If I issue these commands (where % signifies my shell), I expect to get the output below:
% echo hello > output.txt
% cat output.txt
hello
However, when I issue these commands, and really any commands, it seems to ignore (but not ignore?) my redirections. This is what happens when I issue the same commands:
% echo hello > output.txt
% cat output.txt
hello > output.txt
The weird part is, it does create a file, called "output.txt", and writes to it, "hello > output.txt"
This happens with both the input and output redirectors. Here is just the code for opening and executing an output command.
int fd;
open_write_file(&fd, commands[index]);
dup2(fd, 1);
execvpe(commands[0], commands, envp);
close(fd);
Note that open_write_file()
opens the file name, with flags O_WRONLY | O_TRUNC | O_CREAT, S_RUSR | S_IRGRP | S_IWGRP | S_IWUSR
and does error checking to ensure it opens correctly. How can I solve this and get it to actually execute the real command I want?