OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [misc/] [popen.c] - Rev 1765

Compare with Previous | Blame | View Log

 
#include <stdio.h>
#include <unistd.h>
 
 
FILE * popen(command, rw)
char * command;
char * rw;
{
   int pipe_fd[2];
   int pid, reading;
 
   if( pipe(pipe_fd) < 0 ) return NULL;
   reading = (rw[0] == 'r');
 
   pid = vfork();
   if( pid < 0 ) { close(pipe_fd[0]); close(pipe_fd[1]); return NULL; }
   if( pid == 0 )
   {
      close(pipe_fd[!reading]);
      close(reading);
      if( pipe_fd[reading] != reading )
      {
	 dup2(pipe_fd[reading], reading);
         close(pipe_fd[reading]);
      }
 
      execl("/bin/sh", "sh", "-c", command, (char*)0);
      _exit(255);
   }
 
   close(pipe_fd[reading]);
   return fdopen(pipe_fd[!reading], rw);
}
 
int pclose(fd)
FILE *fd;
{
   int waitstat;
   if( fclose(fd) != 0 ) return EOF;
   wait(&waitstat);
}
 
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.