Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Vanishing files ?



Anssi Saari wrote:
> Now that we're on the topic, I think bash does this a little better. If you h
> ave an
> invalid interpreter, it tells you, like this:
> 
> bash: ./foo: /bin/foo: bad interpreter: No such file or directory

This turns out to be easy; the function already does path look up and
even has code to search for the interpreter (to emulate #! on older
systems where it wasn't implemented natively).  It relies on the error
being ENOENT (no such file or directory), not ENOEXEC (exec format
error); this is the case on all the systems I know about (haven't check
any relevant standards).

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.98
diff -u -r1.98 exec.c
--- Src/exec.c	19 Apr 2006 16:09:07 -0000	1.98
+++ Src/exec.c	29 May 2006 15:28:29 -0000
@@ -385,7 +385,7 @@
      * then check for an errno equal to ENOEXEC.  This errno is set *
      * if the process file has the appropriate access permission,   *
      * but has an invalid magic number in its header.               */
-    if ((eno = errno) == ENOEXEC) {
+    if ((eno = errno) == ENOEXEC || eno == ENOENT) {
 	char execvebuf[POUNDBANGLIMIT + 1], *ptr, *ptr2, *argv0;
 	int fd, ct, t0;
 
@@ -405,7 +405,14 @@
 			execvebuf[POUNDBANGLIMIT] = '\0';
 			for (ptr = execvebuf + 2; *ptr && *ptr == ' '; ptr++);
 			for (ptr2 = ptr; *ptr && *ptr != ' '; ptr++);
-			if (*ptr) {
+			if (eno == ENOENT) {
+			    char *buf;
+			    if (*ptr)
+				*ptr = '\0';
+			    buf = tricat("%s: bad interpreter: ", ptr2,
+					 ": %e");
+			    zerr(buf, pth, eno);
+			} else if (*ptr) {
 			    *ptr = '\0';
 			    argv[-2] = ptr2;
 			    argv[-1] = ptr + 1;
@@ -414,11 +421,11 @@
 			    argv[-1] = ptr2;
 			    execve(ptr2, argv - 1, environ);
 			}
-		    } else {
+		    } else if (eno == ENOEXEC) {
 			argv[-1] = "sh";
 			execve("/bin/sh", argv - 1, environ);
 		    }
-		} else {
+		} else if (eno == ENOEXEC) {
 		    for (t0 = 0; t0 != ct; t0++)
 			if (!execvebuf[t0])
 			    break;

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author