Re: Redirection issue




Barry Margolin wrote:
In article <1156959022.965599.76790@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"souissipro" <souissipro@xxxxxxxx> wrote:

int
redir_cmd(char *argv[],char *in, char *out)

You don't seem to handle the case where only on direction is being
redirected. Perhaps you should allow a null point to be passed for the
ones that aren't being redirected, and check for this below before
calling open() and dup2().

This would also allow you to avoid having separate simple_cmd() and
redir_cmd() functions -- a simple command is just what you get if both
in and out are null, because it won't redirect anything.

{
int i;
int pid, status;
int fd1, fd2;
int dummy;

if (strcmp(argv[0],"exit")==0)
return do_exit(argv);
for(i=0;argv[i]!=NULL;i++)
printf("argv[%i]=\"%s\";\n",i,argv[i]);

if (strcmp(argv[0],"cd")==0)
{
return chdir(*argv);
}

if (fork() == 0) {
fd1 = open(in, O_RDONLY);
if (fd1 < 0) {
perror("can't open file : in");
exit(1);
}

if (dup2(fd1, out) != 0) {

This should be:

if (dup2(fd1, STDIN_FILENO) != 0) {

perror("catf1f2: dup2(in, 0)");
exit(1);
}
close(fd1);

fd2 = open(out, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd2 < 0) {
perror("catinout: out");
exit(2);
}
/*
if (dup2(fd2, 1) != 1) {

It's preferable to use STDOUT_FILENO instead of hard-coding 1.

perror("catf1f2: dup2(out, 1)");
exit(1);
}
*/
close(fd2);

execvp(*argv, argv);
perror(*argv);
exit(1);

}

/*
* parent executes wait.
*/
else {
wait(&dummy);
}

}

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


Hi Barry ,

Thanks for your comments.

I have changed the fd1 and fd2 types and also 0 and 1 by STDOUT_FILENO
and STDIN_FILENO. The redirection is now working. The shell compiles .
The commands of the input file (input.txt) have been executed and the
output is sent to the outout file (ouput.txt).
Now, I have another issues in the sens that the simple command and the
commands passed in a file are not executed.
In the main function I have treated 3 cases : simple command, input
file, and redirection. But the problem may be in the rediretion
(redirect_cmd) function and as you said the code is not handle the case
where only on direction is being
redirected.
Could you please clarify this by some lines of codes in this fucntion.


Thanks,

Souissipro

.



Relevant Pages

  • Re: [SLE] Cant run this script
    ... If a command is terminated by the control operator &, ... Redirection may also be used to open and close files for the ... if the file descriptor number is ... word, bash reports an error. ...
    (SuSE)
  • RE: VBA routine (101)
    ... Directory command is implementing two switches “/s” to include files in all ... the command and switches as follows: CMD, DIR, Redirects> and Pipes | ... When you use a redirection symbol to send dir output to a file or a pipe ... > Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window. ...
    (microsoft.public.excel.programming)
  • Re: VBA routine (101)
    ... DIR run the Directory command on ?C:\Ajay? ... The dos window closed upon execution, ... >probably is a slash command for the ?CMD? ... >When you use a redirection symbol to send dir output to a file or a pipe ...
    (microsoft.public.excel.programming)
  • Re: writing to a - log file
    ... MS-DOS (or command line session under Windows) you're using. ... But back to you redirection question. ... ECHO This is a test ...
    (comp.os.msdos.programmer)
  • Re: no globbing by redirection?
    ... I was quite surprized that globbing in arguments and in redirection ... "Words used for filenames in input/output redirection are not ... Note also that wildcard expansion does apply to command names. ...
    (comp.unix.shell)