[alt.sources.wanted] Backwards cat [trick recursive main

jaw@eos.UUCP (James A. Woods) (12/19/89)

# Furthermore, we are concerned with authenticity.  Bald-headedness
  among females is a real phenomenon.  Hence we are unalterably
  opposed to simulation, air-brushing, etc.
	-- The Razor's Edge, vol. 1, no. 1, 1977

... then there's always (modulo stack death), the classic:

	#include <stdio.h>

	main() {
    	    char line[BUFSIZ];
 	
    	    if (gets(line) == NULL)
		return;
    	    main();
    	    puts(line);
	}

for some reason, it always reminds me of joost swarte's trickster back cover
page of the comic "dutch treat" (a translation of "tante leny presenteert").
it's really two pages in one -- you read it in one direction, turn it
upside down, and continue with the story -- the panel illustrations serving
a dual role.  

ames!jaw

trost@reed.bitnet (Bill Trost) (12/20/89)

>... then there's always (modulo stack death), the classic:
>
>	#include <stdio.h>
>
>	main() {
>    	    char line[BUFSIZ];
> 	
>    	    if (gets(line) == NULL)
>		return;
>    	    main();
>    	    puts(line);
>	}

But you forgot to handle arguments!

#include <stdio.h>

main(argc, argv)
char** argv;
{
    switch (argc) {
    default:
	main(argc - 1, argv + 1);
	/* FALLS THROUGH */
    case 2:
	if (argv[1][0] != '-' || argv[1][1] != '\0') {
	    FILE*	fp;

	    if (fp = fopen(argv[1])) {
		main(-1, (char **) fp);
		fclose(fp);
	    }
	    break;
	}
	/* FALLS THROUGH */
    case 1:
	main(-1, stdin);
	break;
    case -1:
	{
	    char line[BUFSIZ];

	    if (!fgets(line, BUFSIZ, (FILE *) argv))
		return;
	    main(argc, argv);
	    fputs(line, stdout);
	}
    }
}