[comp.unix.questions] Here's /bin/env.

tony@oha.UUCP (Tony Olekshy) (07/11/88)

In a recent article, Mike Wescott writes:
>
> In article <4717@killer.UUCP> jockc@killer.UUCP (Jock Cooper) writes:
> >
> > Is there a way to increase the size of the environment variable table?
> > My implementation allows only 100, and I need more.
> ...
> There is no limit in your /bin/sh for 100 environment variables. ...  It's in
> /bin/env and not the shell. ...  It'll print the garbage "after" the 100th
> variable.

So I says to him, I says:

    #include <stdio.h>
    main(c,a,e) char *a[],*e[]; { while (*e!=0) { puts(*e++); } exit(0); }

Yours, etc., Tony Olekshy (alberta!oha!tony, tony@oha.UUCP).

wescott@sauron.Columbia.NCR.COM (Mike Wescott) (07/12/88)

In article <249@oha.UUCP> tony@oha.UUCP (Tony Olekshy) writes:
> [ Source for /bin/env ]
> #include <stdio.h>
> main(c,a,e) char *a[],*e[]; { while (*e!=0) { puts(*e++); } exit(0); }

That'll work fine if all you want out of /bin/env is to print the environment
but how about the rest of the functionality of env?  It's in the code to
handle the selective addition of environment variables in which the bug occurs.
It's not that difficult of a job, but it's not a one-liner either.


-- 
	-Mike Wescott
	 wescott@ncrcae.Columbia.NCR.COM

haugj@pigs.UUCP (Joe Bob Willie) (07/14/88)

In article <1129@sauron.Columbia.NCR.COM> wescott@sauron.Columbia.NCR.COM (Mike Wescott) writes:
>In article <249@oha.UUCP> tony@oha.UUCP (Tony Olekshy) writes:
>> [ Source for /bin/env ]
>> #include <stdio.h>
>> main(c,a,e) char *a[],*e[]; { while (*e!=0) { puts(*e++); } exit(0); }
>
>That'll work fine if all you want out of /bin/env is to print the environment
>but how about the rest of the functionality of env?
>-- 
>	-Mike Wescott

well, mike, you asked for it.  here it is.  i wrote this whilst waiting
for pigs to crash.  it shouldn't have any bugs and i tested it somewhat.
try it on the cases which are known to cause the real env to bomb and
let me know how it works.  and this really is just a quick hack, at
70 someodd lines you can't expect the world.

- john.
--
/*
 * env - set environment for command execution
 *
 *	written by:	john f. haugh ii
 *	date:		13 july 88
 *	synopsis:	env [ - ] [ NAME=value ... ] [ command args ... ]
 *
 *	this is hereby placed into the public domain.  use at
 *	your own risk and all that nonsense.
 */

#include <stdio.h>
#include <string.h>

main (argc, argv, envp)
int	argc;
char	**argv;
char	**envp;
{
	char	**env;
	int	envc;
	int	lastenv;
	int	i;

	for (i = 0;envp[i] != (char *) 0;i++)
		;

	envc = i + argc;

	env = (char **) malloc ((envc + 1) * sizeof (char *));
	for (i = 0;i <= envc;i++)
		env[envc] = (char *) 0;

	argc--; argv++;			/* skip over program name */

	lastenv = 0;			/* nothing in the environment */

	if (argc > 0 && strcmp (*argv, "-") == 0) {
		argc--; argv++;
	} else {
		for (i = 0;envp[i] != (char *) 0;i++)
			env[lastenv++] = envp[i];
	}
	while (argc > 0) {
		if (strchr (*argv, '=') == (char *) 0) {
			execve (*argv, argv, env);
			perror (*argv);
			_exit (127);
		}
		for (i = 0;i < lastenv;i++)
			if (checkenv (*argv, env[i]))
				break;

		if (i < lastenv)
			env[i] = *argv;
		else
			env[lastenv++] = *argv;

		argc--; argv++;
	}
	for (i = 0;i < lastenv;i++)
		puts (env[i]);
}

checkenv (a, b)
char	*a;
char	*b;
{
	char	*aeq, *beq;
	int	len;

	if ((aeq = strchr (a, '=')) == (char *) 0 ||
			(beq = strchr (b, '=')) == (char *) 0)
		return (0);

	if (aeq - a != beq - b)
		return (0);

	return (! strncmp (a, b, aeq - a));
}
-- 
 John "Evil USENET User" F. Haugh II          HECI Exploration Co, Inc., Dallas
 UUCP: ...!killer!rpp386!jfh                            jfh@rpp386.UUCP :DOMAIN
 **** Trivia question of the day: VYARZERZIMANIMORORSEZASSEZANSERAREORSES? ****
 "You are in a twisty little maze of UUCP connections, all alike" -- fortune