ref: 792f79ee6dd1012e4b9d27837476b0e234a78b0c
parent: 30e4700f5d0ce493c71289ce53bac2fb18c4664d
author: Ori Bernstein <[email protected]>
date: Fri Jan 10 15:28:58 EST 2014
Don't search $PATH with relative paths. execvp shouldn't search $PATH when we have a '/' in the path that we're trying to exec.
--- a/libstd/execvp.myr
+++ b/libstd/execvp.myr
@@ -1,9 +1,10 @@
-use "sys.use"
+use "alloc.use"
+use "env.use"
use "fmt.use"
use "option.use"
+use "strfind.use"
use "strsplit.use"
-use "alloc.use"
-use "env.use"
+use "sys.use"
pkg std =
const execvp : (cmd : byte[:], args : byte[:][:] -> int64)
@@ -11,40 +12,46 @@
;;
const execvp = {cmd, args
- var path, cmdlen
- var split
+ var paths, cmdlen
var buf : byte[512]
- match getenv("PATH")
- | `Some p: path = p
- | `None: path = "/usr/local/bin:/bin:/usr/bin"
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execv(cmd, args)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ cmdlen = bfmt(buf[:], "%s/%s", p, cmd)
+ execv(buf[:cmdlen], args)
+ ;;
+ slfree(paths)
;;
-
- split = strsplit(path, ":")
- for s in split
- cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
- execv(buf[:cmdlen], args)
- ;;
- slfree(split)
-> -1
}
const execvpe = {cmd, args, env
- var path, cmdlen
- var split
+ var paths, cmdlen
var buf : byte[512]
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execve(cmd, args, env)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ cmdlen = bfmt(buf[:], "%s/%s", p, cmd)
+ execve(buf[:cmdlen], args, env)
+ ;;
+ slfree(paths)
+ ;;
+ -> -1
+}
+
+const getpaths = {
+ var path
match getenv("PATH")
| `Some p: path = p
| `None: path = "/usr/local/bin:/bin:/usr/bin"
;;
-
- split = strsplit(path, ":")
- for s in split
- cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
- execve(buf[:cmdlen], args, env)
- ;;
- slfree(split)
- -> -1
+ -> strsplit(path, ":")
}
-
--- a/libstd/util.s
+++ b/libstd/util.s
@@ -19,9 +19,9 @@
subq %rcx,%rsp /* get stack */
movq %rsp,%rdi /* dest */
movq %rsp,%rax /* ret val */
+ subq $16,%rsp /* "unpop" the args */
subq $1,%rsp /* nul */
andq $(~15),%rsp /* align */
- subq $16,%rsp /* "unpop" the args */
cld
rep movsb