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) {
bcfe4f1997-06-09John W. Pierce  if (sizeof(file)) { string path;
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bcfe4f1997-06-09John W. Pierce  if(search(file,"/") >= 0) return exece(combine_path(getcwd(),file),foo,getenv());
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bcfe4f1997-06-09John W. Pierce  path=getenv("PATH")||({});
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bcfe4f1997-06-09John W. Pierce  foreach(path, path) if(file_stat(path=combine_path(path,file))) return exece(path, foo,getenv()); }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return 69; } varargs int spawn(string s,object stdin,object stdout,object stderr) { int pid; pid=fork(); if(pid==-1) error("No more processes.\n"); if(pid) { return pid; }else{
f95b701997-05-21Fredrik Hübinette (Hubbe)  if(stdin ) stdin ->dup2(File("stdin")); if(stdout) stdout->dup2(File("stdout")); if(stderr) stderr->dup2(File("stderr")); if(stdin ) destruct(stdin); if(stdout) destruct(stdout); if(stderr) destruct(stderr);
ef293f1997-05-07Henrik Grubbström (Grubba)  ::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); t=read(0x7fffffff); if(!t) { int e; e=errno(); close(); error("Popen failed with error "+e+".\n"); }else{ close(); } return t; } void system(string s) { object p; int pid; string t; p=file::pipe(); if(!p) error("System() failed.\n"); p->set_close_on_exec(0); if(pid=fork()) { destruct(p); /* Nothing will ever be written here, we are just waiting for it * to close */ file::read(1); }else{ exec("/bin/sh","-c",s); exit(69); } }
cb119a1997-04-07Fredrik Hübinette (Hubbe)  constant fork = predef::fork; constant exece = predef::exece;