Sponsored Content
Full Discussion: pipes + structs
Top Forums Programming pipes + structs Post 56955 by mile1982 on Friday 15th of October 2004 12:11:49 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
vodovod(6)							       Games								vodovod(6)

NAME
vodovod - lead the water from the house to the storage tank SYNOPSIS
vodovod DESCRIPTION
Vodovod is a free game, released under GNU GPL license. Graphics and programming are done by Milan Babuskov. This is an over-weekend project for now, so don't expect miracles. However, game is completely playable. The goal of the game is to reach the highest possible score. You get a limited number of pipes on each level and need to combine them to lead the water from the house at the top of the screen to the storage tank at the bottom. For each pipe water goes through, you get 20 points and if you fill the cross-pipe both ways, you get 60 points. At end of each level, you are awarded depending on the skill level: * Beginner: 100 points * Toolman: 100 points + number of pipes remaining * Master plumber: 100 points + 2 x number of pipes remaining Some of the levels have obstacles where you cannot place pipes, checkpoints through which the water must go and holes in the ground that slow the flow. Select HELP from the main menu to get more info. The game is playable with joystick/joypad: just move it and press buttons when you go to Options -> Configure controls USAGE
The goal of the game is to use pipes to connect the house at the top of the screen with the tank at the bottom. You have time to construct the initial pipeline before the man enters the house. For each level you get a limited number of pipes, so use them wisely. If you make a mistake, you can place another pipe over it. There are certain special blocks in the map: * Blocked Area - You cannot place pipes on these fields. * Checkpoint - Your pipeline must go through this field. * Hole in the ground - Slows down the flow. SEE ALSO
You can find more information at http://home.gna.org/vodovod/ May 2007 vodovod(6)
All times are GMT -4. The time now is 03:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy