Complex(ish) C / unix question

Discussion in 'Mac Programming' started by MacDonaldsd, Mar 27, 2007.

  1. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Messages:
    1,009
    Location:
    London , UK
    #1
    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 :(
     
  2. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Messages:
    2,234
    Location:
    Long Island
    #2
    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.
     
  3. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Messages:
    725
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #3
  4. macrumors newbie

    Joined:
    Jan 7, 2003
    Messages:
    5
    Location:
    Phoenix AZ
    #4
     
  5. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Messages:
    1,009
    Location:
    London , UK
    #5
    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
     
  6. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Messages:
    1,009
    Location:
    London , UK
    #6
    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 ?
     
  7. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Messages:
    1,009
    Location:
    London , UK
    #7
    if I use

    dup(1,3);

    it compiles but I can't use it in freopen so how would go about doing that ?
     
  8. macrumors newbie

    Joined:
    Jan 7, 2003
    Messages:
    5
    Location:
    Phoenix AZ
    #8
    To get STDOUT back, can't you use dup to "back up" the old handle, then use dup again to put the old setting back over STDOUT again?
     
  9. macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Messages:
    498
    Location:
    Wilmington, DE
    #9
    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)
     

Share This Page