Re: binfmt_script and ^M

Jan Nieuwenhuizen
2001-03-05T13:20:55Z

Pavel Machek <pavel@suse.cz> writes:

> > $ head -1 testscript
> > #!/bin/sh
> > $ ./testscript
> > bash: ./testscript: No such file or directory
>
> What kernel wants to say is "/usr/bin/perl\r: no such file". Saying ENOEXEC
> would be even more confusing.

So, why don't we make bash say that, then? As I guess that we've all
been bitten by this before.

What are the chances for something like this to be included?

Greetings,
Jan.

--- ../bash-2.04/execute_cmd.c Tue Jan 25 17:29:11 2000
+++ ./execute_cmd.c Mon Mar 5 13:50:23 2001
@@ -3035,6 +3035,42 @@
}
}

+/* Look for #!INTERPRETER in file COMMAND, and return INTERPRETER . */
+static char *
+extract_hash_bang_interpreter (char *command, char buf[80])
+{
+ int fd;
+ char *interpreter;
+
+ interpreter = "";
+ fd = open (command, O_RDONLY);
+ if (fd >= 0)
+ {
+ int len;
+
+ len = read (fd, (char *)buf, 80);
+ close (fd);
+
+ if (len > 0
+ && buf[0] == '#' && buf[1] == '!')
+ {
+ int i;
+ int start;
+
+ for (i = 2; whitespace (buf[i]) && i < len; i++)
+ ;
+
+ for (start = i;
+ !whitespace (buf[i]) && buf[i] != '\n' && i < len;
+ i++)
+ ;
+
+ interpreter = substring ((char *)buf, start, i);
+ }
+ }
+ return interpreter;
+}
+
/* Execute a simple command that is hopefully defined in a disk file
somewhere.

@@ -3155,7 +3191,12 @@

if (command == 0)
{
- internal_error ("%s: command not found", pathname);
+ char buf[80];
+ char *interpreter = extract_hash_bang_interpreter (pathname, buf);
+
+ internal_error ("%s: command not found: `%s'", pathname,
+ interpreter);
+
exit (EX_NOTFOUND); /* Posix.2 says the exit status is 127 */
}

--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond - The music typesetter
http://www.xs4all.nl/~jantien | http://www.lilypond.org