summaryrefslogtreecommitdiff
path: root/lib/std/execvp.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/execvp.myr')
-rw-r--r--lib/std/execvp.myr57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/std/execvp.myr b/lib/std/execvp.myr
new file mode 100644
index 0000000..38d9fc5
--- /dev/null
+++ b/lib/std/execvp.myr
@@ -0,0 +1,57 @@
+use "alloc.use"
+use "env.use"
+use "fmt.use"
+use "option.use"
+use "strfind.use"
+use "strsplit.use"
+use "syswrap.use"
+
+pkg std =
+ const execvp : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execvpe : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+;;
+
+const execvp = {cmd, args
+ var paths, binpath
+ var buf : byte[512]
+
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execv(cmd, args)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
+ execv(binpath, args)
+ ;;
+ slfree(paths)
+ ;;
+ -> -1
+}
+
+const execvpe = {cmd, args, env
+ var paths, binpath
+ var buf : byte[512]
+
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execve(cmd, args, env)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
+ execve(binpath, args, env)
+ ;;
+ slfree(paths)
+ ;;
+ -> -1
+}
+
+const getpaths = {
+ var path
+ match getenv("PATH")
+ | `Some p: path = p
+ | `None: path = "/usr/local/bin:/bin:/usr/bin"
+ ;;
+ -> strsplit(path, ":")
+}