ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Upon posting on my old post this morning I realized I had moved to redirection but was still posting in my piping question. Here is my issue with redirection. I can get it to work and actually write the output of something like ls to a file that I type in. The problem is that once I call ls or whatever command my system hangs up and I have to use CTRL-D to get out. Here is what I have currently
Code:
void shellRedirection(char *buf, int index){
char command[100];
char outFile[100];
int filedes, dupped;
mode_t mode = S_IWUSR;
int output = STDOUT_FILENO;
//commandbreak is a function that splits the entire command into it's seperate command. e.g. splits ls > testfile into ls and testfile
commandBreak(buf, command, outFile, index);
printf("First Command = %s -- Second Command %s \n",command, outFile);
filedes = creat(outFile, mode); //creates the new file
dupped = dup2(filedes, output); //sets stdout to same descriptor
system(command); //calls command on host system
dup2(1, output); //should put output back at 1?
}
Like I said this command is called from a main based on the fact it contains the > character. I uses system commands instead of commands on my shell because I would figure the system commands would be more trustworthy. Can anyone see what my issue is?
Well, I can't tell you (as in don't know) what is wrong, BUT I was always under the impression that the point of these exercises is that you're supposed to write the whole thing in C (like the real shell).
Shelling out via system is 'cheating' imho, and definitely not as per the real thing.
Of course, I could be wrong.
hehe, yeah eventually the entire shell will be written but as of now the only working functions are the make file system and the ls commands. In the spirit of more thorough testing I decided to write the commands to the system with the intent of later going back and passing it as a command to my personal shell
What is output supposed to be? If I understand correctly, output is always 1 (the dup2() call never changes the value of the either argument). What you are trying to accomplish may be done by “saving” the file descriptor before duping it. Alternatively, the more commonly-seen approach is simply to fork() prior to the dup2() so the parent retains all its file descriptors, and only the child will be affected by the redirection.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.