a1ad681998-01-16Fredrik Hübinette (Hubbe) #!/usr/local/bin/pike inherit Stdio.Port;
facd311998-08-05Fredrik Hübinette (Hubbe) // Bugfix for some older versions of Pike.. string combine_path(string s, string ... rest) { for(int e=0;e<sizeof(rest);e++) { if(sscanf(rest[e],"%*[a-zA-Z]:%*s")==2) { s=rest[e]; }else{ s=predef::combine_path(s,rest[e]); } } return s; } // Bugfix for some older versions of Pike.. #define BLOCK 65536 int cp(string from, string to) { if(!Stdio.cp(from,to)) { werror("Backup cp function in effect.\n"); string data; object tmp=Stdio.File(); if(!tmp->open(from,"r")) { werror(sprintf("Open %s failed.\n",from)); return 0; } function r=tmp->read; tmp=Stdio.File(); if(!tmp->open(to,"wct")) { werror(sprintf("Open %s failed.\n",to)); return 0; } function w=tmp->write; do { data=r(BLOCK); if(!data) { werror("Read failed.\n"); return 0; } if(w(data)!=strlen(data)) { werror("Write failed.\n"); return 0; } }while(strlen(data) == BLOCK); } return 1; }
d6a1a81998-01-25Fredrik Hübinette (Hubbe) void monitor(object(Stdio.File) io, object proc) { proc->wait();
80e7ad1998-07-26Fredrik Hübinette (Hubbe)  if(io) { io->close("rw"); io->close(); destruct(io); }
d6a1a81998-01-25Fredrik Hübinette (Hubbe) }
a1ad681998-01-16Fredrik Hübinette (Hubbe) void handle_incoming_connection(object(Stdio.File) io) { object p; sscanf(io->read(4),"%4c",int args); string *cmd=allocate(args); for(int e=0;e<args;e++) { sscanf(io->read(4),"%4c",int len); cmd[e]=io->read(len); } object pi=Stdio.File();
5a7ab61998-01-31Fredrik Hübinette (Hubbe) #if constant(Stdio.PROP_IPC) object p2=pi->pipe(Stdio.PROP_IPC); #else
a1ad681998-01-16Fredrik Hübinette (Hubbe)  object p2=pi->pipe();
5a7ab61998-01-31Fredrik Hübinette (Hubbe) #endif
a1ad681998-01-16Fredrik Hübinette (Hubbe)  string dir=cmd[0]; cmd=cmd[1..];
242a4c1998-01-19Fredrik Hübinette (Hubbe)  write("Doing "+cmd*" "+"\n");
47a5f41998-01-21Fredrik Hübinette (Hubbe)  switch(lower_case(cmd[0]))
a1ad681998-01-16Fredrik Hübinette (Hubbe)  {
47a5f41998-01-21Fredrik Hübinette (Hubbe)  case "mkdir": { io->write(sprintf("%4c",0)); mkdir(combine_path(combine_path(getcwd(),dir),cmd[1])); io->write(sprintf("%4c",0)); break; } case "copy": { string from=combine_path(combine_path(getcwd(),dir),cmd[1]); string to=combine_path(combine_path(getcwd(),dir),cmd[2]); if(mixed stat=file_stat(to))
5a7ab61998-01-31Fredrik Hübinette (Hubbe)  {
47a5f41998-01-21Fredrik Hübinette (Hubbe)  if(stat[1]==-2)
5a7ab61998-01-31Fredrik Hübinette (Hubbe)  { to=combine_path(to,basename(cmd[1])); } }
47a5f41998-01-21Fredrik Hübinette (Hubbe) 
5a7ab61998-01-31Fredrik Hübinette (Hubbe) 
facd311998-08-05Fredrik Hübinette (Hubbe)  int ret=cp(from,to);
5a7ab61998-01-31Fredrik Hübinette (Hubbe)  if(!ret) { string x=sprintf("Errno is %d\n" "CWD=%s\n" "from=%s\n" "to=%s\n" "dir=%s (%s)\n", errno(), getcwd(), from, to, dir, combine_path(getcwd(),dir)); io->write(sprintf("%4c%s",strlen(x),x)); }
47a5f41998-01-21Fredrik Hübinette (Hubbe)  io->write(sprintf("%4c",0));
5a7ab61998-01-31Fredrik Hübinette (Hubbe)  io->write(sprintf("%4c",!ret));
47a5f41998-01-21Fredrik Hübinette (Hubbe)  break; }
a1ad681998-01-16Fredrik Hübinette (Hubbe)  case "getenv": { string s=getenv(cmd[1])+"\n"; io->write(sprintf("%4c%s",strlen(s),s)); io->write(sprintf("%4c",0)); io->write(sprintf("%4c",0)); break; } default: mixed err=catch { p=Process.create_process(cmd, ([ "stdin":io, "stdout":p2, "stderr":p2, "cwd":dir,
d6a1a81998-01-25Fredrik Hübinette (Hubbe)  ])); };
80e7ad1998-07-26Fredrik Hübinette (Hubbe)  destruct(p2);
a1ad681998-01-16Fredrik Hübinette (Hubbe)  if(!err) {
9224441998-04-24Fredrik Hübinette (Hubbe) #if !constant(Stdio.PROP_IPC)
d6a1a81998-01-25Fredrik Hübinette (Hubbe)  thread_create(monitor,p2,p);
5a7ab61998-01-31Fredrik Hübinette (Hubbe) #endif
a1ad681998-01-16Fredrik Hübinette (Hubbe)  while(1) { string s=pi->read(1000,1); if(!s || !strlen(s)) break; io->write(sprintf("%4c%s",strlen(s),s)); } io->write(sprintf("%4c",0)); io->write(sprintf("%4c",p->wait())); }else{
d6a1a81998-01-25Fredrik Hübinette (Hubbe)  destruct(p2);
a1ad681998-01-16Fredrik Hübinette (Hubbe)  io->write(sprintf("%4c",0)); io->write(sprintf("%4c",69)); } } io->close("w"); destruct(io); } int main(int argc, string *argv) {
d95dc91998-01-19Fredrik Hübinette (Hubbe)  if(argc<2)
a1ad681998-01-16Fredrik Hübinette (Hubbe)  {
542d6d1998-02-24Fredrik Hübinette (Hubbe)  werror("Usage: sprshd <port> <hosts to accept connections from>\n");
a1ad681998-01-16Fredrik Hübinette (Hubbe)  exit(1); } if(!bind((int)argv[1])) { werror("Failed to bind port.\n"); exit(1); }
542d6d1998-02-24Fredrik Hübinette (Hubbe)  string *hosts=({}); for(int e=2;e<sizeof(argv);e++) { if(sscanf(argv[e],"%*d.%*d")==2) {
0e1ad41998-10-17Per Hedbor  hosts+=({argv[e]});
542d6d1998-02-24Fredrik Hübinette (Hubbe)  continue; } mixed tmp=gethostbyname(argv[e]); if(!tmp) { werror("Gethostbyname("+argv[e]+") failed.\n"); exit(1); } hosts+=tmp[1]; }
a1ad681998-01-16Fredrik Hübinette (Hubbe) 
facd311998-08-05Fredrik Hübinette (Hubbe)  write("Ready ("+version()+").\n");
a1ad681998-01-16Fredrik Hübinette (Hubbe)  while(1) { if(object io=accept()) { sscanf(io->query_address(),"%s ",string ip); if(search(hosts, ip)==-1) { destruct(io); continue; } thread_create(handle_incoming_connection,io); }else{ werror("Accept failed "+errno()+"\n"); } } }