🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #4989

Re: Programs which print themselves out

#4989
From: Chris Lent
Date: Sun, 08 Nov 1987 02:27
101 lines
3421 bytes
In article <9015@shemp.UCLA.EDU>, lui@CS.UCLA.EDU writes:
> What is the shortest program, in some popular language like C or Pascal,
> which prints itself out when executed?
> 
> 	Stephen Lui
> 	UCLA Department of Computer Science
Hi folks,
	Though this is probably NOT what Stephen was thinking about I
thought the net might find this amusing. Since the Bourne shell is
also a popular programming language, here are two Shell/C programs:

	selfcat.c :  Type selfcat.c and this program will cat itself
	selfhello.c: Type selfhello.c and this program will compile
			itself into an executable called a.out

These programs use the fact that the pound (or sharp) sign, '#',
means something different to the Bourne shell and the 'C' pre-
processor.  In the case of the Bourne shell '#' is a comment,
so the following lines are just comments:
	#ifdef NEVERDEF
	#undef NEVERDEF
	#endif
	#ifdef NEVERDEF
	#Bourne Shell commands follow:

BUT to the 'C' pre-processor they mean:
	if the symbol NEVERDEF is defined (by a command line -D perhaps)
	remove the definition of NEVERDEF.
	Then the following ifdef'd section will never be compiled
	into the program.

So in that ifdef'd section we can safely put a series of Bourne
shell commands. At the end of the section we put an exit to
quit the series of shell commands.

Note that the pound sign followed by a comment is ignored
by the 'C' pre-processor, but a normal Bourne Shell comment
like:	#hello
will give error messages from the pre-processor when compiled
in a 'C' program.

It's a cute trick to amaze your friends.

Enjoy,
Chris Lent
cmcl2!cooper.cooper.edu!chris
(203) 452-1522
#--------------------------CUT HERE-----------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	selfcat.c
#	selfhello.c
# This archive created: Sat Nov  7 21:12:48 1987
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'selfcat.c'
then
	echo shar: "will not over-write existing file 'selfcat.c'"
else
cat << \SHAR_EOF > 'selfcat.c'
#ifdef NEVERDEF
#undef NEVERDEF
#endif
#ifdef NEVERDEF
# /* Bourne Shell commands follow: */
cat $0
exit
# /* End of Bourne Shell commands */
#endif
main()
{
	printf("Gee, this program cat's itself!\n");
}
SHAR_EOF
chmod +x 'selfcat.c'
fi
if test -f 'selfhello.c'
then
	echo shar: "will not over-write existing file 'selfhello.c'"
else
cat << \SHAR_EOF > 'selfhello.c'
#ifdef NEVERDEF
#undef NEVERDEF
#endif
#ifdef NEVERDEF
# /* Bourne Shell commands follow: */
cc $0
exit
# /* End of Bourne Shell commands */
#endif
main()
{
	printf("Hello, world!\n");
}
SHAR_EOF
chmod +x 'selfhello.c'
fi
exit 0
#	End of shell archive
Technical Details
Message-ID: <1100@cooper.cooper.EDU>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!cmcl2!phri!cooper!chris
References: <9015@shemp.UCLA.EDU>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!cmcl2!phri!cooper!chris
From: chris@cooper.cooper.EDU (Chris Lent )
Newsgroups: comp.lang.misc
Subject: Re: Programs which print themselves out
Message-ID: <1100@cooper.cooper.EDU>
Date: Sat, 7-Nov-87 21:27:38 EST
Article-I.D.: cooper.1100
Posted: Sat Nov  7 21:27:38 1987
Date-Received: Wed, 11-Nov-87 02:47:28 EST
References: <9015@shemp.UCLA.EDU>
Organization: The Cooper Union (NY, NY)
Lines: 101
Keywords: Shell C
Summary: Shell/C dual use programs. Examples included.