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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uC-libc/] [misc/] [popen.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
 
2
#include <stdio.h>
3
#include <unistd.h>
4
 
5
 
6
FILE * popen(command, rw)
7
char * command;
8
char * rw;
9
{
10
   int pipe_fd[2];
11
   int pid, reading;
12
 
13
   if( pipe(pipe_fd) < 0 ) return NULL;
14
   reading = (rw[0] == 'r');
15
 
16
   pid = vfork();
17
   if( pid < 0 ) { close(pipe_fd[0]); close(pipe_fd[1]); return NULL; }
18
   if( pid == 0 )
19
   {
20
      close(pipe_fd[!reading]);
21
      close(reading);
22
      if( pipe_fd[reading] != reading )
23
      {
24
         dup2(pipe_fd[reading], reading);
25
         close(pipe_fd[reading]);
26
      }
27
 
28
      execl("/bin/sh", "sh", "-c", command, (char*)0);
29
      _exit(255);
30
   }
31
 
32
   close(pipe_fd[reading]);
33
   return fdopen(pipe_fd[!reading], rw);
34
}
35
 
36
int pclose(fd)
37
FILE *fd;
38
{
39
   int waitstat;
40
   if( fclose(fd) != 0 ) return EOF;
41
   wait(&waitstat);
42
}
43
 

powered by: WebSVN 2.1.0

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