Subject: which echo.c would you choose?
To: None <>
From: Jonathan Cline <Cline@d2tech.com>
List: port-i386
Date: 11/29/1999 13:29:04
I've been browsing around CVSweb looking at source and I thought it would =
be interesting=20
to compare several open source *ix's-- so I picked what I thought would be =
a simple
example: echo.c.  This examination reveals a few things about the =
philosophies behind
the code, I think, though of course they are very similar..  and I do not =
believe the differences
are trivial.

Here I'll present some different echo.c's without labelling their =
ancestry.  Tell me,
which version is the best and why?


++ echo.c version A
int
main(argc, argv)
	int argc;
	char *argv[];
{
	int nflag;

	/* This utility may NOT do getopt(3) option parsing. */
	if (*++argv && !strcmp(*argv, "-n")) {
		++argv;
		nflag =3D 1;
	}
	else
		nflag =3D 0;

	while (argv[0]) {
		int len =3D strlen(argv[0]);

		if (len >=3D 2 && !argv[1] && argv[0][len - 2] =3D=3D '\\' =
&& argv[0][len - 1] =3D=3D 'c') {
			argv[0][len - 2] =3D '\0';
			nflag =3D 1;
		}
		(void)printf("%s", argv[0]);
		if (*++argv)
			putchar(' ');
	}
	if (!nflag)
		putchar('\n');
	exit(0);
}


++ echo.c version B
int
main(argc, argv)
	int argc;
	char *argv[];
{
	int nflag;

	/* This utility may NOT do getopt(3) option parsing. */
	if (*++argv && !strcmp(*argv, "-n")) {
		++argv;
		nflag =3D 1;
	}
	else
		nflag =3D 0;

	while (*argv) {
		(void)printf("%s", *argv);
		if (*++argv)
			(void)putchar(' ');
	}
	if (!nflag)
		(void)putchar('\n');
	exit(0);
	/* NOTREACHED */
}


++ echo.c version C
int
main(argc, argv)
	int argc;
	char *argv[];
{
	unsigned char nflag;

	/* This utility may NOT do getopt(3) option parsing. */
    nflag =3D 0;
	if (*++argv && (argv[0][0] =3D=3D '-' && argv[0][1] =3D=3D 'n' &&
                argv[0][2] =3D=3D '\0')) {
		++argv;
		nflag =3D 1;
	}

	while (*argv) {
		(void)fputs(*argv, stdout);
		if (*++argv)
			(void)putchar(' ');
	}
	if (!nflag)
		(void)putchar('\n');

	return(0);
}



++ echo.c version D
int
main(argc, argv)
	int argc;
	char *argv[];
{
	int nflag;

	/* This utility may NOT do getopt(3) option parsing. */
	if (*++argv && !strcmp(*argv, "-n")) {
		++argv;
		nflag =3D 1;
	}
	else
		nflag =3D 0;

	while (*argv) {
		(void)fputs(*argv, stdout);
		if (*++argv)
			putchar(' ');
	}
	if (!nflag)
		putchar('\n');

	return 0;
}