hi guys..
i have this problem with using dub2()
i whant to semuliate the unix shell like "prog1 > file.out"
this code is for that but it dose not work
i used linked list functions and i am sure that it is correct.
the parsing is working good ethier.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "linklist.h"
int read_cmd();
ptr list;
int main()
{
read_cmd();
return 0;
}
int read_cmd()
{
char com[100];
int i = 0, j = 0, len;
datatype buffer;
pid_t pid, status;
ptr f, o, s;
static char *argv[] = { "ps", "f", NULL};
while(1)
{
printf("vshell>");
fflush(stdin);
gets(com);
len = strlen(com);
if(!strcmp(com, "exit")) // if exit
{
delete_all(&list); // empty the list
return 0;
}
i = 0;
while(i < len)
{
j = 0;
while((com[i] != ' ') && (com[i] != '\0'))
{
buffer.s[j] = com[i];
i++;
j++;
}
buffer.s[j] = '\0';
insertlast(&list, buffer);
i++;
}
pid = fork();
if( pid == 0 ) // child
{
printf("\nforked\n");
f = list;
o = list->next;
s = o->next;
if(strcmp(o->el.s, "<") == 0)
{
close(0);
dup2( open ( s->el.s,O_WRONLY) , 0 ) ;
}
else if(strcmp(o->el.s, ">") == 0)
{
printf("\n1\n");
// close(4);
dup2(open(s->el.s, O_WRONLY), stdin);
printf("\n2\n");
execve(f->el.s, argv, NULL);
}
printf("**3**");
exit(0);
}
else // parent
{
wait(&status);
if(status != 0)
printf("Error return code = %d\n", status);
}
delete_all(&list); // empty the list
}
delete_all(&list); // empty the list
return 0;
}
thanx ...