Re: Redirection issue




souissipro wrote:
Ed Prochak wrote:
souissipro wrote:
Hi,

I have written a C program that does some of the functionalities
mentionned in my previous topic posted some days ago. This shell
should:
1- execute input commands from standard input, and also from a file
conatining the commands
2- does the redirection of the input and output from and to files.
3- retrieve the environment variables like HOME,..

I did the first question and it works fine, however when I modified the
code to implement the redirection it does not work anymore.

Could you please have a look to my code and tell me which is wrong
with?

I would also appreciate if you can tell me how to implement the 2nd
question in the function parse_line (see the code below):

Thank you very much for your help on this


Rather then us looking at your code, I have some general suggestions to
help you help yourself.

First: describe the problem.
the phrase "it does not work anymore" carries very little meaning.
"does not work" could be anything from not compiling, not executing,
crashing immediately, crashing the entire computer, and many other ways
of "not working". You have a case of berfore and after. What did it do
before that it does not do now? BE SPECIFIC. Properly defining the
problem often leads to finding the right solution.

Second: isolate the changes.
Run a diff on the current and previous versions of the program (You do
use a source control system don't you?) Determine how the changes could
have broken the code that was working. For example, look for global
variables with the same name as local variables. Watch for unterminated
block comments. Other common errors.

Three: trace your steps.
If you cannot figure it out from those ideas, try adding some debug
logging through the program OR run the program under a debugger.

Four: slash and burn.
If the problem is serious (e.g., crash and core dump) and nothing else
helps you isolate it, then start removing parts of code from the
current version until it no longer crashes, or you are back to the
previous working version. Cutting the body of functions, leaving only a
return, for example, may be useful.

You have a very good chance of finding the cause of this problem
yourself since you went from a working version to a non-working
version. You'll be a better programmer by learning how to find this
yourself. Just remember, the program is doing exactly what you told it
to do.

ed


Hi ed,

Thanks for your comments. I would add the following information in
order to make things more clear:
In my program, I first implented the three functions: parse_line,
simple_cmd and the main in order to execute input commands from
standard input, and also from input file. This works fine which means
after compilation and execution of the shell with a simple command like
"ls" or "pwd" I get the expected result. When I pass a file as input
all the command in the file are executed as well.

Now, I modified the function parse_line in order to manage the
input/output. I ceatred a new function redir_cmd that receives the name
of files for input and output. I did a modification in the main:
if argc==1 this means simple command;
if argc==2 this means command from file and output STDINOUT;
if argc==3 input from a file an dthe output directed to another file
instead of STDINOUT.
Please have a look to the shell for more details.

The new shell compiles fine but at the execution it does not execute
the commands of the input file and hence it does not make the output in
the output file.

My question is then could you please have a look to my shell and tell
me which is wrong there. I'm not expert in C and I need concrete help
on that.

Thanks in advance.

Souipro

My question is did you follow ANY of the suggestions I gave you? Your
error is very simple. I did look at your code and found the problem
right away. I really am trying to help you learn. This problem is
independent of the programming language. So please, review your own
code. before you scroll down to what I see as the root problem.

Hint, compare what you describe the program doing above with how the
program is actually coded.









DON'T PEEK!









Final suggestion before revealing the answer: Take a printout of the
program, go to a friend (even a nonprogrammer) and describe to them
what each part of the program does. If that doesn't work, then look
below














You never called the new function!

BTW, long term your approach is going about it the hard way, but you'll
learn more this way.
Ed

.