• C

A question about interprocess communication

Hey, I need to have a few wildcards that do some file redirection, they are:

1) > - redirect standard output of command on left to file on right (i.e. command > file)
2) >& - redirect standard output and standard error of command on left to file on right (i.e. command >& file).
3) >> - redirect standard output of command on left to file on right (i.e. command >> file), appending to the file.
4) >>& - redirect standard output and standard error of command on left to file on right (i.e. command >>& file), appending to the file.
5) < - redirect standard input of command on left to come from file on the right (i.e. command < file).

I'm kind of lost as to what this is about, is it asking us to redirect output from a.out? How would we go about trying to get this to work?
errangAsked:
Who is Participating?
 
Infinity08Commented:
>> would something like this do the file redirection in the shell I'm going to write?

Something like this should work better :
int ret = 0;
int fout = 0;
 
/* save the old standard output stream : */
int oldstdout = dup(fileno(stdout));
if (oldstdout < 0) {
  perror("dup failed");
  exit(1);
}
 
/* open the output file : */
fout = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0666);
if (fout < 0) {
  perror("open failed");
  exit(1);
}
 
/* redirect the standard output to the given file : */
ret = dup2(fout, fileno(stdout));
if (ret < 0) {
  perror("dup2 failed");
  exit(1);
}
close(fout);
 
/* ... */
fprintf(stdout, "test1\n");               /* <--- will write to the file */
/* ... */
 
/* restore the original situation : */
fflush(stdout);                           /* <--- just to make sure */
ret = dup2(oldstdout, fileno(stdout));
if (ret < 0) {
  perror("dup2 failed");
  exit(1);
}
close(oldstdout);
 
/* ... */
fprintf(stdout, "test2\n");               /* <--- will write to standard output (the screen usually) */
/* ... */

Open in new window

0
 
HalfAsleepCommented:
Well, those command line functions you mention above, are provided by your command shell (either dos, or a linux bash shell or similar).

If your a.out application writes anything to the standard output, then point 1 to 4 is a simple "a.out >> output.txt", and the output will end up in a file, instead of/in addition to the console.

If your a.out application reads anything from the standard input, then point 5 will make it read from the file instead of/in addition to the console.

If for example your a.out reads a number from the console, then it will read a number contained in the file instead.
0
 
errangAuthor Commented:
yeah, they maybe provided in a regular shell, but the thing is I need to write a shell that has that, and I have no clue how to go about doing it.
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
Infinity08Commented:
If you use an actual shell, some of these (like >, >>, <) are commonly available. So, try them out to see what the result is.

For example, if you have a program that generates some output (like a "Hello World" program to keep things simple), and you do (the # is the shell prompt here) :

        # ./hello
        Hello World !
        # ./hello > out.txt
        # cat out.txt
        Hello World !
        # ./hello >> out.txt
        # cat out.txt
        Hello World !
        Hello World !
        #

So, the first command just puts the standard output on the screen. The second command creates an empty text file called out.txt, and puts the standard output in there. The third command appends the standard output to the already existing file out.txt

Similarly, you can get standard input from a file instead of from the keyboard using <.

The idea is to support this in your shell too.
0
 
HalfAsleepCommented:
Your shell has to be able to read a command and "see" all the arguments and tokens when the user presses the enter key.  You have to be able to "discover" the redirect symbols and treat them accordingly.  When you see those symbols, after you have found out their meaning, you then have to decide how to "run" the command the user requested.

If for example the user wants to append to a file (>>), you have to do a couple of things...

You have to run the command and hook into the standard output while running that command.  You have to open the file to append to.  Then you have to make sure that any data going to standard out, ends up in the file instead.  I am not quite up to speed on how to hook into the standard streams.
0
 
HalfAsleepCommented:
This may help just a tad, when it comes to hooking into the standard out of a process.

http://stackoverflow.com/questions/646868/how-can-i-capture-another-processs-output-using-c
0
 
errangAuthor Commented:
would something like this do the file redirection in the shell I'm going to write?

fid = open(filename,O_WRONLY|O_CREAT|O_TRUNC);
close(1);
dup(fid);
close(fid);
0
 
HalfAsleepCommented:
If you are using visual studio 2005++, you should use _dup and _dup2 instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.