pipes + structs


 
Thread Tools Search this Thread
Top Forums Programming pipes + structs
# 1  
Old 10-15-2004
pipes + structs

heya

got a small problem here. im trying to pipe eg: 'ls | more'.
i have created a command line parser which separates ls and more into two commands. i have also created a struct, each struct has a command name, number of arguments, redirect_in, redirect_out, pipe_in, etc etc.... eg:

struct com[0].com_name would be : ls
struct com[0].pipe_to = 1 ( 1 means pipe to command 1 )

struct com[1].com_name would be : more

now when i try and pipe the above ( ls | more ), only the 'ls' command is executed and more is not. the execute method is called from the main class eg :

Code:
   for (k = 0; k < lc; k++) {

      execute(cl[k], k);
   }


this is my execute method:

Code:
// ----------------------------------------------------------

/*
 * This function dumps the contents of the structure to stdout in a human
 * readable format..
 *
 * Arguments :
 *      c - the structure to be displayed.
 *      count - the array position of the structure.
 *
 * Returns :
 *      None.
 *
 */
void execute(command *a, int count) {

  int fd[2];

   int status;
   int pid;
   int command = 0; 

   // redirect out 
   if (a->redirect_out != NULL) {

      int out;
      out = open (a->redirect_out, O_TRUNC | O_CREAT | O_WRONLY, 0777);

      if (out <= 0) {

        fprintf (stderr, "Couldn't open a file\n");
        exit (errno);
      }
      
      dup2 (out, 1);
      close (out);
    }

   // redirect in 
   if (a->redirect_in != NULL) {

      int in;
      in = open (a->redirect_in, O_RDONLY); 

      if (in <= 0) {

	fprintf(stderr, " \n in = %d ", in);

        fprintf (stderr, "Couldn't open a file\n");
        exit (errno);
      }

      dup2 (in, 1);
      close (in);
    }

   // change directory
   if (strncmp (a->com_name, "cd", 2) == 0){ 
   	
      if (!a->argv[1]) 
	 a->argv[1]=(char *) 

      get_userdir();
      chdir (a->argv[1]);
      exit(0);
   }

   // exit
   if (strncmp (a->com_name, "exit" , 4) == 0) {

      fprintf (stdout, "Quit. \n");
      exit(0);
   }

   if (a->pipe_to == 1) {

      pipe(fd);
   }

   pid = fork();

   // create a process space 

   if (pid == 0) { 

      // child process 

      if (a->pipe_to == 1) {

      	close(fd[1]);
      	dup2(fd[0], STDIN_FILENO);
      	execvp(a->com_name, a->argv); // 'more' 
      	_exit(0);
      }
      else {

      	execvp(a->com_name, a->argv);
      }
   }

   else {

      // parent process

      if (a->background == 1) {
	   
         printf(" \nin background ");
         signal(SIGCHLD, SIG_IGN); // to ignore child-termination signals
	 _exit(0);
      }
     
     if (a->pipe_to == 1) {

	close(fd[0]);
	dup2(fd[1], STDOUT_FILENO);
	execvp(a->com_name, a->argv); 'ls'
	wait(NULL);
     }     

     // let's wait for the child to finish 
      else {

	 waitpid (pid, NULL, 0);
      }
   }
   return;
}

what i think needs to be done is when the 'ls' command is executed, it should go back to the main program, increase 'k++' (increase command by 1) and go back to the execute method without forking again and execute the command pathname of command which was just increased-> but it doesnt seem to be doing that.

can anyone please help

appreciated, mile 1982
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Other way aside from putting more PIPES (|)

I already manage to get the output that i want.. but wat if removing all the pipes and convert it 1 liner with less pipes. My command below can get the ouput that i want. i just want to remove the pipes or less pipes. #cat file1 us-west-2a running i-3397a421... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

2. Homework & Coursework Questions

Xlib help - Array of Structs

Hey guys! First of all english is not my main language so sorry for any english mistakes. Im from Portugal! 1. The problem statement, all variables and given/known data: Im having a problema creating and array of structs for a work i need to do. (xLib) 2. Relevant commands, code,... (1 Reply)
Discussion started by: Spiritvs
1 Replies

3. Programming

Xlib help - Array of Structs

Hey guys! First of all english is not my main language so sorry for any english mistakes. Second im a total beginner in programming, still i have a school work to do and i found a problem. Probably something easy to solve but it's driving me crazy. So i created a struct, that holds 4 ints: ... (1 Reply)
Discussion started by: Spiritvs
1 Replies

4. Programming

[solved]help passing array of structs to function in c

this is my code to try and prinnt out a deck of cards. the print function worked when used inside main without being a function but now i cant get it to work as a function probably since i dont know how to pass a struct array in c. I gave it a shot but i keep getting an assortment of errors. The... (0 Replies)
Discussion started by: bjhum33
0 Replies

5. Shell Programming and Scripting

How to combine these to pipes?

ls --color=always -laX | awk '{print $1, $3, $4, $2, $8}' |sort -k 1,1 -k 9,9r they work separately... but i don't know how to combine this to work. thx! (1 Reply)
Discussion started by: surreal7z
1 Replies

6. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

7. UNIX for Advanced & Expert Users

Consolidating Pipes

This is something I've given a lot of thought to and come up with no answer. Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
Discussion started by: Corona688
4 Replies

8. UNIX for Advanced & Expert Users

broken pipes

A broken pipe means that you are writing onto a pipe that has been closed by the other end. i have unix batch which forks some process and these processes are communicating each other via pipes. but sometimes i got too many broken pipe error. how can i search for the cause of these errors,... (1 Reply)
Discussion started by: yakari
1 Replies

9. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question