Article 4471 of alt.sources: Path: intelhf!ichips!inews!olivea!samsung!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: alt.sources Subject: argv0, saner version of execing program with an explicit argv[0] Message-ID: <4057.Oct1303.54.1691@kramden.acf.nyu.edu> Date: 13 Oct 91 03:54:16 GMT References: Organization: IR Lines: 105 In article pcg@aber.ac.uk (Piercarlo Grandi) writes: > Here is the source and manual page for `dowithargv', a code snippet that > invokes another program with an arbitrary argv[0]. And here's a better version. It's shorter, the man page is easier to understand, it doesn't recopy arguments, it doesn't bother with strange defaults, and there are already people around the world who use it. Finally, argv0 is easier to type than dowithargv. ---Dan #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh 'argv0.1' <<'END_OF_FILE' X.TH argv0 1 X.SH NAME Xargv0 \- run a program with a specified zeroth argument X.SH SYNOPSIS X.B argv0 X.I realname X.I program X[ X.I arg ... X] X.SH DESCRIPTION X.B argv0 Xruns Xthe program stored as X.I realname Xon disk, Xwith the given Xarguments. XIt sets the zeroth argument of Xthe program to X.I program. X.SH EXAMPLES X.EX Xargv0 /bin/csh -bin/csh X.EE X XRun the shell with a hyphen as its first name. XThis is how X.B login(1) Xruns shells; X.B csh Xwill think it is a login shell Xand behave accordingly. X X.EX Xargv0 sendmail mailq X.EE X XRun X.B sendmail Xwith a zeroth argument of X.B mailq. X.SH "SEE ALSO" Xexecve(2), Xexecvp(3) END_OF_FILE if test 605 -ne `wc -c <'argv0.1'`; then echo shar: \"'argv0.1'\" unpacked with wrong size! fi # end of 'argv0.1' fi if test -f 'argv0.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'argv0.c'\" else echo shar: Extracting \"'argv0.c'\" \(241 characters\) sed "s/^X//" >'argv0.c' <<'END_OF_FILE' X#include X Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X if (argc < 3) X { X fputs("Usage: argv0 realname program [ arg ... ]\n",stderr); X exit(1); X } X execvp(argv[1],argv + 2); X perror("argv0: fatal: cannot execute"); X exit(4); X} END_OF_FILE if test 241 -ne `wc -c <'argv0.c'`; then echo shar: \"'argv0.c'\" unpacked with wrong size! fi # end of 'argv0.c' fi echo shar: End of shell archive. exit 0 Article 4473 of alt.sources: Path: intelhf!ichips!iWarp.intel.com|uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!princeton!phoenix.Princeton.EDU!subbarao From: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Newsgroups: alt.sources Subject: Re: argv0, saner version of execing program with an explicit argv[0] Message-ID: Date: 13 Oct 91 23:24:02 GMT References: <4057.Oct1303.54.1691@kramden.acf.nyu.edu> Sender: news@princeton.Princeton.EDU Reply-To: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Organization: putchar('I'); for (i = 0; i < 2; i++) putchar('E'); Lines: 43 In article <4057.Oct1303.54.1691@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >In article pcg@aber.ac.uk (Piercarlo Grandi) writes: >> Here is the source and manual page for `dowithargv', a code snippet that >> invokes another program with an arbitrary argv[0]. > >And here's a better version. It's shorter, the man page is easier to >understand, it doesn't recopy arguments, it doesn't bother with strange >defaults, and there are already people around the world who use it. >Finally, argv0 is easier to type than dowithargv. > >---Dan > And here's a perlier version... #! /usr/local/bin/perl $prog = $ARGV[0]; $fake = $ARGV[1]; exec $full $fake, @ARGV[2..$#ARGV]; It's shorter, it doesn't even need a man page, and all the cooooler people on the block use it :-) Finally, you can call it whatever you want .. You get the idea. ------------------------------------------------------------------------------- I'm now going to steal Paul's thunder and explain how this can be done in one line in zsh: zsh% ARGV0=innocent sneakyfoobar [arguments] executes ``sneakyfoobar'' and its arguments with argv[0] = "innocent". This takes only one line, and is one of a plethora of other features available in zsh :-) -Kartik -- internet# adb -w -k /vmunix /dev/mem < /dev/zero subbarao@phoenix.Princeton.EDU -| Internet kartik@silvertone.Princeton.EDU (NeXT mail) SUBBARAO@PUCC.BITNET - Bitnet