LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2006, 10:58 PM   #1
banan
LQ Newbie
 
Registered: Mar 2005
Posts: 9

Rep: Reputation: 0
Question Piping output between processes. C.


Hello everyone. I have a problem.
I have to write a peace of code in C that will simulate a simple shell and in that shell only commands that are listed in specification file are allowed. However, that is not a problem, because I got it working already for single command entries. What I'm struggling with is geting output piped from one command to another like:
# ls -l | grep stuff
given that both ls and grep are permitted.
Any idea how to approach it?
I use fork() and execvp() to execute commands.
Many thanks in advance.
 
Old 11-12-2006, 11:25 PM   #2
banan
LQ Newbie
 
Registered: Mar 2005
Posts: 9

Original Poster
Rep: Reputation: 0
I am so sorry.
I should have searched this forum before posting my question.
This problem can easily be solved by using popen() like it was suggested in thread below.
Sort of:
Code:
/*****************************************************************************
 Excerpt from "Linux Programmer's Guide - Chapter 6"
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: popen2.c
 *****************************************************************************/

#include <stdio.h>

int main(void)
{
        FILE *pipein_fp, *pipeout_fp;
        char readbuf[80];

        /* Create one way pipe line with call to popen() */
        if (( pipein_fp = popen("ls", "r")) == NULL)
        {
                perror("popen");
                exit(1);
        }

        /* Create one way pipe line with call to popen() */
        if (( pipeout_fp = popen("sort", "w")) == NULL)
        {
                perror("popen");
                exit(1);
        }

        /* Processing loop */
        while(fgets(readbuf, 80, pipein_fp))
                fputs(readbuf, pipeout_fp);

        /* Close the pipes */
        pclose(pipein_fp);
        pclose(pipeout_fp);

        return(0);
}
Sorry ones again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting output of background processes after they've been started (BASH) DaneM Programming 18 08-08-2011 09:03 PM
Piping keyboard input to two processes (or threads) caffeinebot Programming 3 05-17-2005 04:51 PM
Piping output to text files corbis_demon Linux - General 3 10-12-2004 03:33 AM
Piping Processes (a little different) mackie_lin Programming 4 12-27-2002 08:17 AM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration