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

Re: what is hackzero?



>Does anyone know what the global variable hackzero
>is for?  Offhand I can't figure out what bin_fg is
>doing with it.

% grep hackzero *
builtin.c:	    strcpy(hackzero, *argv);
globals.h:EXTERN char *hackzero;
init.c:    hackzero = argzero = *argv;

Looks like the shell can write into its argv[0].  Here's the code:

/**/
int
bin_fg(char *name, char **argv, char *ops, int func)
{
    int job, lng, firstjob = -1, retval = 0;

    if (ops['Z']) {
	if (*argv)
	    strcpy(hackzero, *argv);
	return 0;
    }

Okay, "jobs -Z foo" can set the shell's argv[0].  This works (try it),
and is undocumented.  It's a bit dangerous, as exceeding the length of
the actual argv[0] is easy and causes undefined behaviour.  It does in
fact cause trouble on some Unices if the length differs at all.

Note that argzero can't be used in place of hackzero, because it's only
a copy of argv[0].  And as argzero is what $0 refers to, $0 is
unaffected by the jobs -Z thing.

I think it's actually a potentially useful feature, as it allows a
long-running zsh script to change its name, in the manner of some
daemons.  (Actually, it should be just about possible to implement
init(8) in zsh... frightening, isn't it?)

Does someone here know enough about modifying argv to advise how this
could be modified to work better?

-zefram




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