Chapter Contents

Previous

Next
popen

popen



Open Pipe I/O to a Shell Command

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

FILE *popen(const char *command, const char *mode);


DESCRIPTION

popen creates a pipe between the calling program and a command to be executed by the USS shell. A stream opened by popen should be closed by pclose .

The arguments are pointers to null-terminated strings. command is a null-terminated shell command. mode is the I/O mode, which can be set to these values:
r indicates read mode. You read from the standard output of the command by reading from the FILE pointer returned by popen .
w indicates write mode. You write to the standard input of the command by writing to the FILE pointer returned by popen .

Because open files are shared, you can use a mode of "r" as an input filter and a mode of "w" as an output filter.

You must define an appropriate feature test macro ( _SASC_POSIX_SOURCE or _POSIX_C_SOURCE ) to make the declaration of popen in <stdio.h> visible.

Note:    A stream opened by popen must be closed by pclose .  [cautionend]


RETURN VALUE

popen returns a FILE pointer if successful. popen returns a NULL pointer if a file or process cannot be created.


PORTABILITY

popen is defined in accordance with POSIX 1003.2


EXAMPLE

This example sorts the lines of an HFS file and writes out the first line of the sorted file. The popen function is used to invoke the shell sort command to do all the work.

/* This program must be compiled with the posix compiler option */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXLINE 500

main(int argc, char *argv[])
{
   char linebuf[MAXLINE];
   char *cmdbuf;
   int cmdlen;
   FILE *sort_output;

   if (argc < 2) cmdbuf = "sort";
   else {
      if (argc > 2) fputs("Extraneous arguments ignored\n", stderr);
      cmdlen = 5+strlen(argv[1]);

         /* Allocate space for sort command.                     */
      cmdbuf = malloc(cmdlen);
      if (!cmdbuf) exit(EXIT_FAILURE);
      sprintf(cmdbuf, "sort %s", argv[1]); /* Build sort command.*/
   }
   sort_output = popen(cmdbuf, "r");  /* Read the output of sort.*/
   if (!sort_output) {
      perror("popen failure");
      exit(EXIT_FAILURE);
   }

      /* Read first sorted line.                                 */
   fgets(linebuf, sizeof(linebuf), sort_output);

   if (feof(sort_output) || ferror(sort_output)) {
      fputs("Input error.\n");
      pclose(sort_output); /* Close sort process before quitting.*/
      exit(EXIT_FAILURE);
   }
   puts(linebuf);           /* Write line to stdout.             */

      /* Close the sort process. It will probably terminate      */
      /*  with SIGPIPE.                                          */
   pclose(sort_output);
   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

pclose , pipe , system


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.