Solved

A question about interprocess communication

Posted on 2009-04-13
8
256 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:errang
  • 4
  • 2
  • 2
8 Comments
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24135504
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
 

Author Comment

by:errang
ID: 24135528
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24135532
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24135566
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
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24135577
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
 

Author Comment

by:errang
ID: 24155237
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24155703
>> 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
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24155762
If you are using visual studio 2005++, you should use _dup and _dup2 instead.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now