Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Code:
if(cpid == 0){//Child
	printf("Hello I'm the child \n");
       
	while(counter<acount){
		if(*arg[counter] =='>'){
			printf("Checking here found >\n\n\n");
       
			counter++;
       
			fp=freopen(arg[counter],"w",stdout);

			counter--;// now is >
           
			while(&buffer[counter2]!=arg[counter]){
				counter2++;
			}
     
      
			while(counter2<100){
				buffer[counter2]='\0';
				counter2++;
			}
         
			counter++;
		}
		counter++;
	}
      
	int i=0;
	printf("Buffer before exec function\n\n");
      
	while(i<100){
		printf("%i ",buffer[i]);
		i++;
	}
	printf("\n");
 
	execvp(arg[0],arg);

Ok where do I start..........
acount is the number of arguments
arg is an array of argument pointers
buffer is a char array which arg points to.

This is part of a bigger programme

The rest of the programme takes a command such as

ls -l and then tokenises it
argument 1 is ls
argument 2 is -l

the code above is where I pass these arguments in to a child procees and run an exec command.

The problem is ls -l works fine but ls -l > txt.txt doesn't i get an error

ls: fts_open: No such file or directory

Even though my above code attempts to deal with the redirction bit itself

Any ideas :(
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
Redirection is always a tricky issue to deal with. The other thing to try is to give a full path name for the output file.

Things to try are have it execute a shell script with the same argument list (call it myls or something similar) and print out the arguments it passes. Make sure the local directory is in your PATH variable. You may not have write permission on the default directory or some similar issue.
 

xmlguy

macrumors newbie
Jan 7, 2003
5
0
Phoenix AZ
Code:
if(cpid == 0){//Child
:
		if(*arg[counter] =='>'){
			printf("Checking here found >\n\n\n");
       
			counter++;
       
			fp=freopen(arg[counter],"w",stdout);
:[/QUOTE]
Most likely the problem is that you are sitting on a space when you call freopen.

I would change the line: counter++;

to read: while(*arg[++counter] == ' ');
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
I don't think a blank space is the problem.

What I have done previously is initialise buffer with all blanks spaces

Then stored the string in the buffer array and made all the blank spaces NULL
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Code:
int cpid;
FILE *stream;
    cpid =fork();///Creates child process
    int counter=0;
    int counter2=0;
  
    if(cpid == 0){//Child
	printf("Hello I'm the child \n");
        
      while(counter<acount){
       
       if(*arg[counter] =='>'){
       *arg[counter]='\0';
       arg[counter]=NULL;
     

       stream=  freopen(arg[++counter],"w",stdout);
       *arg[counter]='\0';
       arg[counter]=NULL;
  
      }
      counter++;
      }
      
     execvp(arg[0],arg);
    

      fclose( stream );
    }
    
    else{//Adult
     //   wait();
	printf("hello I'm the parent \n");
     
    }

This code works a bit better, if i type "ls" it display files in terminal as it should and "ls > text.txt" outputs this information in the specified file.

But I need it to loop, if I do a redirection again it works again even if I specify a different file. But a normal ls will result in a error.

I think its because stdout is still being used, is there any way to get it back ?
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
If you need to reopen stdout, you should be able to just use this:

Code:
file_desc = open( "/dev/stdout", O_WRONLY);
close( STD_OUT );
dup( file_desc );

(lifted from a command-shell I wrote years ago with pipe and redirection support)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.