Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83 (MC830713); site vu44.UUCP
Path: utzoo!watmath!clyde!cbosgd!ihnp4!houxm!mhuxr!ulysses!allegra!mit-eddie!godot!harvard!seismo!mcvax!vu44!jack
From: jack@vu44.UUCP (Jack Jansen)
Newsgroups: net.lang.c
Subject: Re: pipes and the sort command
Message-ID: <537@vu44.UUCP>
Date: Thu, 27-Dec-84 06:31:03 EST
Article-I.D.: vu44.537
Posted: Thu Dec 27 06:31:03 1984
Date-Received: Sat, 29-Dec-84 02:53:28 EST
References: <1650@drutx.UUCP> <6770@brl-tgr.ARPA>
Organization: The Retarded Programmers Home, VU, Amsterdam
Lines: 31

> 	I am interested in using the popen() command to form a pipe
> 	between my C program and the UNIX sort command. I would appreciate
> 	any information on the best way to do this(examples would help).
> 	I should point out that the input to the sort command would be
> 	comming from an array and the output should be stored in an array.
It is very hard to get *both* ends of a pipe into your hands.
The simplest solution is to use a tempfile, which makes your
code something like

    char command[BUFSIZ], *tnm;
    FILE *tfp;
    tnm = tempname();	/* Get temp filename */
    if((tfp = fopen(tnm,"w"))==NULL) ;
    write_array_to_file(data,tfp);
    fclose(tfp);
    sprintf(command,"sort %s",tnm);
    if( (tfp=popen(command,"r")) == NULL ) ;
    read_array_from_file(data,tfp);
    if(pclose(tfp) != 0 ) ; 

In this case, read_array_from_file() and write_array_to_file()
read/write an array to a file (in ASCII), probably with a printf or
scanf loop.

If you don't want to use a tempfile, you have to do it yourself.
This means setting up two pipes, forking, re-directing all file-
descriptors, etc etc etc.
-- 
	Jack Jansen, {seismo|philabs|decvax}!mcvax!vu44!jack
	or				       ...!vu44!htsa!jack
If *this* is my opinion, I wasn't sober at the time.