[alt.sources] is there a c program which outputs itself?

rxg3321@isc.rit.edu (R.X. Getter ) (03/20/91)

hi,
	I am looking for a c program which outputs itself. Has anyone
seen one or heard of one? (No, it cannot read its own source code.)

	thanks,
	Robert Getter

peltz@cerl.uiuc.edu (Steve Peltz) (03/22/91)

In article <1991Mar19.165839.15308@isc.rit.edu> rxg3321@isc.rit.edu (R.X. Getter ) writes:
>hi,
>	I am looking for a c program which outputs itself. Has anyone
>seen one or heard of one? (No, it cannot read its own source code.)

Well, of course there is. There's a whole string of them in this group already
of various flavors.

Here's a version which doesn't use excessively cute tricks, assume that
certain characters have fixed numeric values, or such.  In other words,
it is an HONEST self-printing program.  It is also a self-starter, in
that there are really two programs involved. The first one writes the
second one, based on a shell, which is the actual self-printing
program.

#! /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 <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile makeself.c selfstart.c
# Wrapped by peltz@uinova on Thu Mar 21 10:15:26 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(144 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
Xself:	makeself selfstart.c
X	./makeself < selfstart.c > self.c
X	cat selfstart.c >> self.c
X	cc -o self self.c
X
Xclean:
X	rm -f makeself self self.c
END_OF_FILE
if test 144 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'makeself.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'makeself.c'\"
else
echo shar: Extracting \"'makeself.c'\" \(291 characters\)
sed "s/^X//" >'makeself.c' <<'END_OF_FILE'
X#include <stdio.h>
X
Xint	main(argc, argv)
X	int	argc;
X	char	**argv;
X{
X	char	p[80];
X	char	*c;
X	puts("char	*s[] = {");
X	while (c = gets(p)) {
X		putchar('"');
X		while (*c) {
X			if ((*c == '"') || (*c == '\\'))
X				putchar('\\');
X			putchar(*c++);
X		}
X		puts("\",");
X	}
X	puts("0};");
X	puts("");
X}
END_OF_FILE
if test 291 -ne `wc -c <'makeself.c'`; then
    echo shar: \"'makeself.c'\" unpacked with wrong size!
fi
# end of 'makeself.c'
fi
if test -f 'selfstart.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'selfstart.c'\"
else
echo shar: Extracting \"'selfstart.c'\" \(359 characters\)
sed "s/^X//" >'selfstart.c' <<'END_OF_FILE'
Xint	main(argc, argv)
X	int	argc;
X	char	**argv;
X{
X	char	**p;
X	char	*c;
X	puts("char	*s[] = {");
X	p = s;
X	while (c = *p++) {
X		putchar('"');
X		while (*c) {
X			if ((*c == '"') || (*c == '\\'))
X				putchar('\\');
X			putchar(*c++);
X		}
X		puts("\",");
X	}
X	puts("0};");
X	puts("");
X	p = s;
X	while (c = *p++) {
X		while (*c) {
X			putchar(*c++);
X		}
X		putchar('\n');
X	}
X}
END_OF_FILE
if test 359 -ne `wc -c <'selfstart.c'`; then
    echo shar: \"'selfstart.c'\" unpacked with wrong size!
fi
# end of 'selfstart.c'
fi
echo shar: End of shell archive.
exit 0
--
Steve Peltz
Internet: peltz@cerl.uiuc.edu	PLATO/NovaNET: peltz/s/cerl