ref: ba4b0723d9e6d6ec72ca54016a932e72fbae46f7
parent: 742736e50d97306ad16853d18c977b502cf35b82
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Fri Oct 18 17:24:36 EDT 2024
npe: add popen and pclose
--- a/include/npe/stdio.h
+++ b/include/npe/stdio.h
@@ -3,6 +3,8 @@
#include <npe.h>
+FILE *popen(char *cmd, char *type);
+int pclose(FILE *s);
int rename(char *old, char *new);
#endif
--- a/libnpe/mkfile
+++ b/libnpe/mkfile
@@ -52,6 +52,8 @@
mkstemp.$O\
mktime.$O\
opendir.$O\
+ pclose.$O\
+ popen.$O\
readdir.$O\
realpath.$O\
rename.$O\
--- /dev/null
+++ b/libnpe/pclose.c
@@ -1,0 +1,7 @@
+#include <stdio.h>
+
+int
+pclose(FILE *s)
+{
+ return fclose(s);
+}
--- /dev/null
+++ b/libnpe/popen.c
@@ -1,0 +1,27 @@
+#include <stdio.h>
+
+FILE *
+popen(char *cmd, char *type)
+{
+ int p[2], r, null, pid;
+ FILE *s;
+
+ pipe(p);
+ r = type[0] == 'r' || type[1] == 'r';
+ if((pid = rfork(RFPROC|RFFDG|RFNOTEG|RFCENVG|RFNOWAIT)) == 0){
+ null = open("/dev/null", r ? OWRITE : OREAD);
+ dup(null, !r);
+ dup(p[r], r);
+ close(p[r]);
+ close(null);
+ execl("/bin/rc", "rc", "-c", cmd, nil);
+ sysfatal("popen: execl: %r");
+ }else if(pid < 0){
+ close(p[0]);
+ close(p[1]);
+ return nil;
+ }
+ s = fdopen(p[!r], r ? "r" : "w");
+ close(p[r]);
+ return s;
+}