summaryrefslogtreecommitdiff
path: root/appl/cmd/runas.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/cmd/runas.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/cmd/runas.b')
-rw-r--r--appl/cmd/runas.b60
1 files changed, 60 insertions, 0 deletions
diff --git a/appl/cmd/runas.b b/appl/cmd/runas.b
new file mode 100644
index 00000000..d0112396
--- /dev/null
+++ b/appl/cmd/runas.b
@@ -0,0 +1,60 @@
+implement Runas;
+
+include "sys.m";
+include "draw.m";
+include "sh.m";
+
+sys: Sys;
+sh: Sh;
+
+Context: import sh;
+
+Runas: module {
+ init: fn(nil: ref Draw->Context, argv: list of string);
+};
+
+init(drawctxt: ref Draw->Context, argv: list of string)
+{
+ sys = load Sys Sys->PATH;
+ sh = load Sh Sh->PATH;
+ if (sh == nil)
+ badmodule(Sh->PATH);
+
+ if (len argv < 3)
+ usage();
+
+ argv = tl argv;
+ user := hd argv;
+ argv = tl argv;
+
+ fd := sys->open("/dev/user", Sys->OWRITE);
+ if (fd == nil)
+ error(sys->sprint("cannot open /dev/user: %r"));
+ u := array of byte user;
+ if (sys->write(fd, u, len u) != len u)
+ error(sys->sprint("cannot set user: %r"));
+ sh->run(drawctxt, argv);
+}
+
+badmodule(p: string)
+{
+ sys->fprint(stderr(), "runas: cannot load %s: %r\n", p);
+ raise "fail:bad module";
+}
+
+stderr(): ref Sys->FD
+{
+ return sys->fildes(2);
+}
+
+usage()
+{
+ sys->fprint(stderr(), "usage: runas user cmd [args...]\n");
+ raise "fail:usage";
+}
+
+error(e: string)
+{
+ sys->fprint(stderr(), "runas: %s\n", e);
+ raise "fail:error";
+} \ No newline at end of file