/* * * env /bin/python2.4 ... * or simply * env python2.4 ... * then "env" reads namespace file "/sys/lib/env/python2.4" * * env is designed to be used in the first line in the executables * #!/bin/env paython2.4 */ #include #include #include int atime = 0; static void nsop(int argc, char *argv[]); void usage(void) { fprint(2,"usage: env [-a time] path arg ...\n"); exits("usage"); } /* bind only addns * original source: /sys/src/libauth/newns.c */ #define NARG 5 int chgns(char *file) { Biobuf *spec; int argc; char *cmd, *argv[NARG]; spec = Bopen(file, OREAD); if(spec == 0) return -1; while(cmd = Brdline(spec, '\n')){ cmd[Blinelen(spec)-1] = '\0'; while(*cmd==' ' || *cmd=='\t') cmd++; if(*cmd == '#') continue; /* we need not support '#' tree */ argc = getfields(cmd, argv, NARG, 1, " \t"); if(argc) nsop(argc, argv); } Bterm(spec); return 0; } /* bind only nsop * original source: /sys/src/libauth/newns.c */ static void nsop(int argc, char *argv[]) { char *argv0; ulong flags; flags = 0; argv0 = 0; ARGBEGIN{ case 'a': flags |= MAFTER; break; case 'b': flags |= MBEFORE; break; case 'c': flags |= MCREATE; break; case 'C': // flags |= MCACHE; break; }ARGEND if(!(flags & (MAFTER|MBEFORE))) flags |= MREPL; if(strcmp(argv0, "bind") == 0 && argc == 2) bind(argv[0], argv[1], flags); else if(strcmp(argv0, "cd") == 0 && argc == 1) chdir(argv[0]); } void main(int argc, char *argv[]) { char *ns, *path, *p, *q; int n; ARGBEGIN{ case 'a': atime = atoi(EARGF(usage())); // alarm time break; default: usage(); }ARGEND path = argv[0]; if(path == nil) usage(); /* path is /bin/python2.4 */ p = strrchr(path,'/'); if(p == nil){ n = strlen(path)+10; q = malloc(n); snprint(q, n, "/bin/%s", path); p = path; path = q; } n = strlen(p)+20; ns = malloc(n); snprint(ns, n, "/sys/lib/env/%s", p); chgns(ns); if(atime) alarm(1000*atime); if(*argv) exec(path, argv); }