[comp.unix.wizards] globbing in the shell and in programs

chris@mimsy.UUCP (Chris Torek) (12/07/87)

In article <1475@osiris.UUCP> mjr@osiris.UUCP (Marcus J. Ranum) writes:
>... Better yet, your version of rm(1) can check to see if CMDLINE is
>set in the invocation environment, and assume that if it is your user
>DOES want all the extra safety, otherwise rm can just unlink the
>damn file so the rest of us can get on with our work.

(Funny, this sounds like Jim's suggestion ... which was the one I
passed along.)  There are some pitfalls with this approach; since
the discussion (if that is the word for it) is continuing, I might
as well point out the obvious ones.

>so:
>	if(( cp = getenv("CMDLINE")) != NULL) {
>		/* make it look like the MSDOS delete command */
>		/* are you SURE (Y/N) ?  etc. */
>	} else {
>		/* make it look like rm(1) */
>	}

Using getenv() directly would be a mistake.  Why?  Well, consider
a program that does this:

	main(argc, argv, envp)
		int argc;
		char **argv, **envp;
	{
		...
		/* now go run some other program */
		execv(otherprog, otherargv);
		perror(otherprog);
		exit(1);
	}

If the command line is carried in the environment, it will be
passed on to otherprog.  If that happens to be rm, well....

One way to fix it might be to include some special string in
$CMDLINE, e.g., pid, or argv[0]; none of these seem quite satisfactory.
It might be best to have the C library start-up code find and remove
$CMDLINE from envp (and hence environ), storing it in another global
and/or passing it as a fourth argument to main().

Doug Gwyn asks what such a `user-friendly' shell would do with
the command

	rm `find ...`

I suspect the answer is that said shell would leave $CMDLINE out
of the environment when the command line includes backquote expansion.
(Note that $CMDLINE would be set for find, though.)  This leaves one
to wonder about

	rm * `find ...`

Possible answers include `not set', `contains magic cookie saying
use argv[M] through argv[N] here', etc.  Since (fortunately) I
am not designing such a system, I need not find any really good
solution.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris