Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gerd Stolpmann <Gerd.Stolpmann@darmstadt.netsurf.de>
To: Markus Mottl <mottl@miss.wu-wien.ac.at>, luther@dpt-info.u-strasbg.fr
Cc: caml-list@inria.fr (OCAML)
Subject: Re: Sys.argv with interpreter and compiler
Date: Fri, 9 Jul 1999 01:23:34 +0200	[thread overview]
Message-ID: <99070901440006.28040@schneemann> (raw)
In-Reply-To: <199907050937.LAA30606@miss.wu-wien.ac.at>

On Mon, 05 Jul 1999, Markus Mottl wrote:
>> I don't get the same, why :
>> 
>> sh-2.02$ cat essay
>> #!/usr/local/bin/ocaml
>> print_string "Hello world!"; print_newline();;
>> exit 0;;
>> ^D
>> sh-2.02$ ./essai
>> ./essai: line 2: syntax error near unexpected token `;'
>> ./essai: line 2: `print_string "Hello world!"; print_newline();;'
>
>Your toplevel "/usr/local/bin/ocaml" is obviously compiled to byte code.
>Take a look at it with "less" and you will see that the first line of
>this executable is actually "#!/home/mottl/mysys/bin/ocamlrun".

There is another way to get around this:

#!/bin/sh
# (* 
exec ocaml "$0" "$@"
*) directory ".";;
print_string "Hello world!"; print_newline();;
exit(0);;

It does not matter whether "ocaml" is itself a script or not, and you 
do not need to specify the interpreter with an absolute file name;
/bin/sh always exists. The solution is a bit tricky: The first lines
are interpreted by both /bin/sh and ocaml.

	# (* 

The shell sees a comment line because of #. Ocaml sees the beginning of
a directive with a comment between # and the name of the directive
which is allowed.

	exec ocaml "$0" "$@"

This line is only seen by the shell, as ocaml thinnks it is a comment.
It causes that the process switches over to run ocaml with the given
parameters. The shell stops here and does not read further.

	*) directory ".";;

The end of the comment and the directive. #directory "." has no effect,
as "." is already in the search path.


>As far as I know it is not allowed on any unix system to run scripts
>whose interpreter is a script itself. This would make it much easier to
>replace this interpreter with something evil - e.g. some kind of wrapper
>that executes unfriendly commands under you UID and continues with the
>"true" interpreter.

That's what they tell you. I believe that kernel programmers do not like
recursions at all. (There are actually some serious problems with recursions
in kernel mode, for example recursive mutexes.)

Gerd
--
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   Gerd.Stolpmann@darmstadt.netsurf.de (privat)
Germany                     
----------------------------------------------------------------------------




  reply	other threads:[~1999-07-09  1:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-06-27 12:02 Markus Mottl
1999-07-01 17:32 ` Xavier Leroy
1999-07-01 23:35   ` Markus Mottl
1999-07-02  0:39     ` Pierre Weis
1999-07-02  0:53       ` Fabrice Le Fessant
1999-07-05  8:09       ` Sven LUTHER
1999-07-05 10:37         ` Markus Mottl
1999-07-08 23:23           ` Gerd Stolpmann [this message]
1999-07-02  1:30     ` Jacques GARRIGUE
1999-07-02  8:56       ` Markus Mottl
1999-06-29 17:01 Damien Doligez
1999-07-08 11:39 Damien Doligez
1999-07-09  2:25 ` Jacques GARRIGUE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=99070901440006.28040@schneemann \
    --to=gerd.stolpmann@darmstadt.netsurf.de \
    --cc=caml-list@inria.fr \
    --cc=luther@dpt-info.u-strasbg.fr \
    --cc=mottl@miss.wu-wien.ac.at \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox