c2a4061997-02-06Fredrik Hübinette (Hubbe) #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
d5190b1997-02-10Fredrik Hübinette (Hubbe) import Stdio;
c2a4061997-02-06Fredrik Hübinette (Hubbe)  static private inherit File : file; varargs int exec(string file,string ... foo) { string path; if(search(file,"/")) return exece(combine_path(getcwd(),file),foo,getenv());
bf67441997-02-27Marcus Comstedt  path=getenv("PATH");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bf67441997-02-27Marcus Comstedt  foreach((path?(path/":"):({})),path)
c2a4061997-02-06Fredrik Hübinette (Hubbe)  if(file_stat(path=combine_path(path,file))) return exece(path, foo,getenv()); return 69; }
190fb01997-05-10Fredrik Hübinette (Hubbe) varargs object spawn(string s,object stdin,object stdout,object stderr)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
190fb01997-05-10Fredrik Hübinette (Hubbe)  if(object proc=fork())
c2a4061997-02-06Fredrik Hübinette (Hubbe)  {
190fb01997-05-10Fredrik Hübinette (Hubbe)  return proc;
c2a4061997-02-06Fredrik Hübinette (Hubbe)  }else{
ef293f1997-05-07Henrik Grubbström (Grubba)  if(stdin) {
c2a4061997-02-06Fredrik Hübinette (Hubbe)  stdin->dup2(File("stdin"));
ef293f1997-05-07Henrik Grubbström (Grubba)  stdin->close(); }
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
ef293f1997-05-07Henrik Grubbström (Grubba)  if(stdout) {
c2a4061997-02-06Fredrik Hübinette (Hubbe)  stdout->dup2(File("stdout"));
ef293f1997-05-07Henrik Grubbström (Grubba)  stdout->close(); }
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
ef293f1997-05-07Henrik Grubbström (Grubba)  if(stderr) {
6e61041997-04-09Niels Möller  stderr->dup2(File("stderr"));
ef293f1997-05-07Henrik Grubbström (Grubba)  stderr->close(); } ::close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  exec("/bin/sh","-c",s); exit(69); } } string popen(string s) { object p; string t; p=file::pipe(); if(!p) error("Popen failed. (couldn't create pipe)\n"); spawn(s,0,p,0);
ef293f1997-05-07Henrik Grubbström (Grubba)  p->close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  destruct(p);
190fb01997-05-10Fredrik Hübinette (Hubbe)  t=read();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  if(!t) { int e; e=errno(); close(); error("Popen failed with error "+e+".\n"); }else{ close(); } return t; }
190fb01997-05-10Fredrik Hübinette (Hubbe) int system(string s) { return spawn(s)->wait(); }
cb119a1997-04-07Fredrik Hübinette (Hubbe)  constant fork = predef::fork; constant exece = predef::exece;