Add preview of 1984-2019
The 2020 winners are hinted it, but the source code is not there,
nor are the hints. Someone could look into the HTML and
see what was commented out, but that is not a problem because
WHO won is known and the name of their AWARD is known already.
Moreover a number of the winners have talked about what their
entirs do, so the commented out spoiler text is not a bit problem.
One will find HTML commented out with:
<!-- XXX remove when 2020 is ready
2020 links here
XXX remove when 2020 is ready -->
The "XXX remove when 2020 is ready" string is found at the
start and end comment blocks. Again, soneone could read
what was commented out, but that is OK since most of that
information is known or can be known. The following files
have such HTML comment blocks in them.
./winners.html
./years-spoiler.html
./years.html
In addition, ./all/summary.txt was modified to not
include the 2020 entries for the momemt.
The 2020 winner code AND write-ups are NOT part of this set.
p.s. The all/all.tar.bz2 does not contain the 2020 source code (yet?).
This commit is contained in:
1 parent
28efc67f5d
commit
c0663537cb
1683 files changed
+220064
No files matched your search
@@ -0,0 +1,286 @@
|
||||
#!/usr/bin/env make
|
||||
#
|
||||
# 2018 makefile
|
||||
#
|
||||
# This work by Landon Curt Noll, Simon Cooper, and Leonid A. Broukhis
|
||||
# is licensed under:
|
||||
#
|
||||
# Creative Commons Attribution-ShareAlike 3.0 Unported License.
|
||||
#
|
||||
# See: http://creativecommons.org/licenses/by-sa/3.0/
|
||||
|
||||
|
||||
################
|
||||
# tool locations
|
||||
################
|
||||
#
|
||||
SHELL= /bin/bash
|
||||
CP= cp
|
||||
CPP= cpp
|
||||
GUNZIP= gunzip
|
||||
BUNZIP2= bunzip2
|
||||
GZIP_PROG= gzip
|
||||
BZIP2= bzip2
|
||||
CURL= curl
|
||||
DD= dd
|
||||
LD= ld
|
||||
MAKE= make
|
||||
RM= rm
|
||||
SED= sed
|
||||
TAR= tar
|
||||
TRUE= true
|
||||
MKDIR= mkdir
|
||||
MV= mv
|
||||
TR= tr
|
||||
GREP= grep
|
||||
SORT= sort
|
||||
UNIQ= uniq
|
||||
|
||||
# Set X11_LIBDIR to the directory where the X11 library resides
|
||||
#
|
||||
#X11_LIBDIR= /usr/X11R6/lib
|
||||
#X11_LIBDIR= /usr/X11/lib
|
||||
X11_LIBDIR= /opt/X11/lib
|
||||
|
||||
# Set X11_INCLUDEDIR to the directory where the X11 include files reside
|
||||
#
|
||||
#X11_INCDIR= /usr/X11R6/include
|
||||
#X11_INCDIR= /usr/X11/include
|
||||
X11_INCDIR= /opt/X11/include
|
||||
|
||||
# Compiler warnings
|
||||
#
|
||||
#CWARN=
|
||||
#CWARN= -Wall
|
||||
#CWARN= -Wall -Wextra
|
||||
CWARN= -Wall -Wextra -pedantic ${CSILENCE}
|
||||
#CWARN= -Wall -Wextra -Weverything
|
||||
#CWARN= -Wall -Wextra -Weverything -pedantic
|
||||
#CWARN= -Wall -Wextra -Weverything -pedantic ${CSILENCE}
|
||||
|
||||
# Silence warnings that ${CWARN} would normally complain about
|
||||
#
|
||||
CSILENCE=
|
||||
#CSILENCE= -Wno-implicit-int
|
||||
|
||||
# compiler standard
|
||||
#
|
||||
#CSTD=
|
||||
#CSTD= -ansi
|
||||
CSTD= -std=c11
|
||||
|
||||
# compiler bit architecture
|
||||
#
|
||||
# Some entries require 32-bitness:
|
||||
# ARCH= -m32
|
||||
#
|
||||
# Some entries require 64-bitness:
|
||||
# ARCH= -m64
|
||||
#
|
||||
# By default we assume nothing:
|
||||
#
|
||||
ARCH=
|
||||
|
||||
# defines that are needed to compile
|
||||
#
|
||||
CDEFINE=
|
||||
#CDEFINE= -DIOCCC=25
|
||||
|
||||
# include files that are needed to compile
|
||||
#
|
||||
#CINCLUDE=
|
||||
#CINCLUDE= -include stdlib.h
|
||||
CINCLUDE= -include stdio.h
|
||||
#CINCLUDE= -include stdlib.h -include stdio.h
|
||||
#CINCLUDE= -I ${X11_INCDIR}
|
||||
|
||||
# optimization
|
||||
#
|
||||
# Most compiles will safely use -O2. Some can use only -O1 or -O.
|
||||
# A few compilers have broken optimizers or this entry make break
|
||||
# under those buggy optimizers and thus you may not want anything.
|
||||
# Reasonable compilers will allow for -O3.
|
||||
#
|
||||
#OPT=
|
||||
#OPT= -O
|
||||
#OPT= -O1
|
||||
#OPT= -O2
|
||||
OPT= -O3
|
||||
|
||||
# default flags for ANSI C compilation
|
||||
#
|
||||
CFLAGS= ${CSTD} ${CWARN} ${ARCH} ${CDEFINE} ${CINCLUDE} ${OPT}
|
||||
|
||||
# Libraries needed to build
|
||||
#
|
||||
LIBS=
|
||||
#LIBS= -L ${X11_LIBDIR}
|
||||
|
||||
# ANSI compiler
|
||||
#
|
||||
# Set CC to the name of your ANSI compiler.
|
||||
#
|
||||
# Some entries seem to need gcc. If you have gcc, set
|
||||
# both CC and MAY_NEED_GCC to gcc.
|
||||
#
|
||||
# If you do not have gcc, set CC to the name of your ANSI compiler, and
|
||||
# set MAY_NEED_GCC to either ${CC} (and hope for the best) or to just :
|
||||
# to disable such programs.
|
||||
#
|
||||
CC= cc
|
||||
#CC=clang
|
||||
MAY_NEED_GCC= gcc
|
||||
|
||||
|
||||
##############################
|
||||
# Special flags for this entry
|
||||
##############################
|
||||
#
|
||||
OBJ= prog.o
|
||||
DATA=
|
||||
TARGET= prog prog.nowarn
|
||||
#
|
||||
ALT_OBJ= prog.nowarn.o
|
||||
ALT_TARGET= prog.nowarn
|
||||
|
||||
|
||||
#################
|
||||
# build the entry
|
||||
#################
|
||||
#
|
||||
all: ${TARGET} ${DATA}
|
||||
@${TRUE}
|
||||
|
||||
prog: prog.c
|
||||
${CC} ${CFLAGS} $< -o $@ ${LIBS}
|
||||
|
||||
# alternative executable
|
||||
#
|
||||
alt: ${ALT_TARGET}
|
||||
@${TRUE}
|
||||
|
||||
prog.nowarn: prog.nowarn.c
|
||||
${CC} ${CFLAGS} $< -o $@ ${LIBS}
|
||||
|
||||
# data files
|
||||
#
|
||||
data: ${DATA}
|
||||
@${TRUE}
|
||||
|
||||
|
||||
###############
|
||||
# utility rules
|
||||
###############
|
||||
#
|
||||
everything: all alt
|
||||
|
||||
clean:
|
||||
${RM} -f ${OBJ} ${ALT_OBJ}
|
||||
|
||||
clobber: clean
|
||||
${RM} -f ${TARGET} ${ALT_TARGET}
|
||||
@-if [ -e sandwich ]; then \
|
||||
${RM} -f sandwich; \
|
||||
echo 'ate sandwich'; \
|
||||
fi
|
||||
|
||||
nuke: clobber
|
||||
@${TRUE}
|
||||
|
||||
dist_clean: nuke
|
||||
@${TRUE}
|
||||
|
||||
install:
|
||||
@echo "Surely you are performing, Dr. May!"
|
||||
@${TRUE}
|
||||
|
||||
# backwards compatibility
|
||||
#
|
||||
build: all
|
||||
@${TRUE}
|
||||
|
||||
|
||||
##################
|
||||
# 133t hacker rulz
|
||||
##################
|
||||
#
|
||||
love:
|
||||
@echo 'not war?'
|
||||
@${TRUE}
|
||||
|
||||
haste:
|
||||
$(MAKE) waste
|
||||
@${TRUE}
|
||||
|
||||
waste:
|
||||
@echo 'haste'
|
||||
@${TRUE}
|
||||
|
||||
make:
|
||||
@echo 'Attend a maker faire'
|
||||
@${TRUE}
|
||||
|
||||
easter_egg:
|
||||
@echo you expected to often mis-understand this $${RANDOM} magic
|
||||
@echo chongo '<was here>' "/\\oo/\\"
|
||||
@echo Eggy
|
||||
|
||||
fabricate fashion form frame manufacture produce: make
|
||||
@${TRUE}
|
||||
|
||||
sandwich:
|
||||
@if [ `id -u` -eq 0 ]; then \
|
||||
echo 'Okay.'; \
|
||||
echo $${RANDOM}`date +%s`$${RANDOM} > $@; \
|
||||
else \
|
||||
echo 'What? Make it yourself.'; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
# Understand the history of the Homebrew Computer Club
|
||||
# as well as the West Coast Computer Faire and
|
||||
# you might be confused different. :-)
|
||||
#
|
||||
supernova: nuke
|
||||
@-if [ -r .code_anal ]; then \
|
||||
${RM} -f .code_anal_v6; \
|
||||
else \
|
||||
echo "planet deniers, like some members of the IAU, are so cute when they try to defend their logic"; \
|
||||
fi
|
||||
@echo A $@ helps ${MAKE} the elements that help form planets
|
||||
@${TRUE}
|
||||
|
||||
deep_magic:
|
||||
@-if [ -r .code_anal ]; then \
|
||||
ccode_analysis --deep_magic 3a3fabb90892eeb2f610ae0f74a35a970060665a063ab702bd686c17c4ae07f6 --FNV1a_hash_256_bit "prog"; \
|
||||
else \
|
||||
echo "Wrong! Do it again!"; \
|
||||
sleep 2; \
|
||||
echo "Wrong! Do it again!"; \
|
||||
sleep 2; \
|
||||
echo "Wrong! Do it again!"; \
|
||||
fi
|
||||
@${TRUE}
|
||||
|
||||
magic: deep_magic
|
||||
@-if [ -r .code_anal ]; then \
|
||||
ccode_analysis --mode 216091 --level 216193 --FNV1a_hash_256_bit "prog"; \
|
||||
else \
|
||||
echo "If you don't eat yer meat, you can't have any pudding!"; \
|
||||
echo "How can you have any pudding if you don't eat yer meat?!"; \
|
||||
fi
|
||||
@${TRUE}
|
||||
|
||||
# The IOCCC resident astronomer states
|
||||
#
|
||||
charon: supernova
|
||||
@echo $@ is a dwarf planet
|
||||
@echo dwarf is a type of planet
|
||||
@echo $@ is a planet
|
||||
|
||||
pluto: supernova
|
||||
${MAKE} charon
|
||||
@echo $@ is a dwarf planet
|
||||
@echo dwarf is a type of planet
|
||||
@echo $@ is a planet
|
||||
@echo get used to having lots of planets because a $< can ${MAKE} a lot of them
|
||||
@@ -0,0 +1,254 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta name="GENERATOR" content="mkd2html 2.1.5a DL=DISCOUNT">
|
||||
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
|
||||
<link rel="stylesheet"
|
||||
type="text/css"
|
||||
href="../hint.css">
|
||||
<title>Best one-liner</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Best one-liner</h1>
|
||||
|
||||
<p>Dave Burton<br>
|
||||
<a href="http://ioccc.snox.net">http://ioccc.snox.net</a></p>
|
||||
|
||||
<h2>Judges' comments:</h2>
|
||||
|
||||
<h3>To use:</h3>
|
||||
|
||||
<pre><code>make
|
||||
./prog < any-file
|
||||
</code></pre>
|
||||
|
||||
<h3>Try:</h3>
|
||||
|
||||
<pre><code>./prog < prog
|
||||
./prog < prog.c
|
||||
</code></pre>
|
||||
|
||||
<h3>Selected Judges Remarks:</h3>
|
||||
|
||||
<p>One line, one array, one loop and one statement but it prints many bytes. It won.</p>
|
||||
|
||||
<h2>Author’s comments:</h2>
|
||||
|
||||
<p>Feed this anything on stdin.</p>
|
||||
|
||||
<p>The included script may be used as a driver program for this entry.
|
||||
It adds several nice-to-have features, and allows salt-to-taste customizability.</p>
|
||||
|
||||
<p>This code has been compiled and tested on:</p>
|
||||
|
||||
<ul>
|
||||
<li>OSX-10.11, gcc 4.2.1 and clang 3.0, -O0 and -O3</li>
|
||||
<li>X86-Linux, gcc 4.1.2 and 4.8.4, -O0 and -O3</li>
|
||||
<li>ARM-QEMU, gcc 4.8.4, -O0 and -O3</li>
|
||||
<li>X86-FreeBSD 10.3, clang 3.4.1, -O0 and -O3</li>
|
||||
<li>TCC 0.9.26 (x86-64) (used as a proxy for <a href="http://ioccc.org/years.html#2001_bellard">2001/bellard</a>)</li>
|
||||
<li>UNIX V7 pcc (Johnson’s C compiler) on a <a href="http://simh.trailing-edge.com/">simulated PDP-11/45</a>
|
||||
(see spoiler for further detail)</li>
|
||||
</ul>
|
||||
|
||||
|
||||
<div style="margin-bottom:60em;margin-top:4em"><strong>Spoiler below; scroll down.</strong></div>
|
||||
|
||||
|
||||
<h2>SPOILER</h2>
|
||||
|
||||
<p>This started life as a personal challenge: write the shortest hex dump utility in C.</p>
|
||||
|
||||
<p>The self-imposed ground rules in the hunt for shortest:
|
||||
the code has to output correctly,
|
||||
no extraneous newlines, the code must be portable,
|
||||
and without bypassing the solution using execve(2),
|
||||
or functions built upon it (system(3), popen(3), etc). E.g:</p>
|
||||
|
||||
<pre><code>main(){system("hd");} // it's turtles all the way down!
|
||||
</code></pre>
|
||||
|
||||
<p>is not acceptable.
|
||||
And, “output correctly” is the output format of this entry. QED.</p>
|
||||
|
||||
<p>Early attempts ended up at a reasonably short 157 characters.
|
||||
This was too long for a one-liner, so it had to be formatted as something more interesting.
|
||||
This solution is pleasing, but a true one-liner was the hoped-for goal.
|
||||
Here is the 157 character version in it’s entirety:</p>
|
||||
|
||||
<pre><code>char O,o[ 20];L(O){
|
||||
putchar ( O+ 48+39 *(9<
|
||||
O));} I(O) {L(~ 15);
|
||||
O|| puts(o);} main (l){
|
||||
for(;~(l=getchar()) ;I(O
|
||||
&=15 ))o[ O++ ]=4<
|
||||
(4^l >>5) ?l: 46,L
|
||||
(l>> 4),L (l&15);O&&
|
||||
I(o[ O]=0 );}//157c
|
||||
</code></pre>
|
||||
|
||||
<p>While thinking about this in the shower (you are not the only one that does this!)
|
||||
came the idea which reduced the code to just 119 chars.
|
||||
This dramatic reduction started to feel close to minimal, and the hunt resumed.
|
||||
The progression to the present size is interesting,
|
||||
and it is interesting that the same algorithm can be expressed so differently
|
||||
by slight rearrangement of its parts.</p>
|
||||
|
||||
<pre><code>// 119
|
||||
char O,o[20];main(l){for(;~(l=getchar());O||puts(o))o[O++]=isprint(l)?l:46,O&=15,printf("%02x ",l);O[o]=0;O&&puts(o);}
|
||||
// 116
|
||||
char O,o[20];main(l){for(;O++[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;(O&=15)||puts(o))printf("%02x ",l);O>1&&puts(o);}
|
||||
// 115
|
||||
char O,o[20];main(l){for(;O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;(O=-~O&15)||puts(o))printf("%02x ",l);O&&puts(o);}
|
||||
// 113
|
||||
char O,o[20];main(l){for(;~l;(O=~l?printf("%02x ",l),-~O&15:!O)||puts(o))O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;}
|
||||
// 112
|
||||
char O,o[20];main(l){for(;~l;(O=~l?-~O&printf("%02x ",l)*5:!O)||puts(o))O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;}
|
||||
// 111
|
||||
char O,o[20];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
// 110
|
||||
char O,o[0];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
// 109
|
||||
char O,o[];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
</code></pre>
|
||||
|
||||
<p>There are solutions without the ASCII sidebar that are significantly shorter.
|
||||
These do not compete in the same category, since their output is materially different.
|
||||
They are derived from the solutions above, but are still fascinating as concise utilities,
|
||||
so here is their progression, for the curious:</p>
|
||||
|
||||
<pre><code>// 102
|
||||
I;O(O){putchar(O+48+39*(9<O));}main(l){for(;~(l=getchar());O(++I&15?~15:~37))O(l>>4),O(l&15);O(-38);}
|
||||
// 80
|
||||
O;main(l){while(~(l=getchar()))printf("%02x%c",l,++O&15?32:10);O&15&&puts("");}
|
||||
// 78
|
||||
O;main(l){for(;~l;(O=~(l=getchar())?printf("%02x ",l),-~O&15:!O)||puts(""));}
|
||||
// 77
|
||||
O;main(l){for(;~l;(O=~(l=getchar())?-~O&printf("%02x ",l)*5:!O)||puts(""));}
|
||||
// 76
|
||||
O;main(l){for(;~l;O||puts(&O))O=~(l=getchar())?-~O&printf("%02x ",l)*5:!O;}
|
||||
</code></pre>
|
||||
|
||||
<p>It is possible to reduce the size of each of these by one by eliding the trailing newline.
|
||||
This was not reported above since the newline kept getting appended by processing software,
|
||||
and it is more interesting to remove characters by changing code than by filtering:</p>
|
||||
|
||||
<pre><code>$ tr -d '\012' < 109.c | wc -c
|
||||
108
|
||||
$ tr -d '\012' < 76.c | wc -c
|
||||
75
|
||||
$ tr -d '\012' < 109.c | cc -o 108 -xc -
|
||||
$ verify 108
|
||||
$
|
||||
</code></pre>
|
||||
|
||||
<p>An obfuscated hex dump has been done before:
|
||||
<a href="http://ioccc.org/1986/bright/bright.c">1986/bright/bright.c</a> is similar in function,
|
||||
but it uses more characters in just setting up its obfuscation than this entire solution!</p>
|
||||
|
||||
<h2>BIGGER SPOILER</h2>
|
||||
|
||||
<p>If you are unhappy with the way DEL (character 127) is handled,
|
||||
replace the cryptic expression <code>4<(4^l>>5)?l:46</code> with <code>32>l|l>126?46:l</code>.
|
||||
Both take the same number of characters, but the former is a bit more interesting.
|
||||
Bonus question: how does the first expression work?</p>
|
||||
|
||||
<h2>BIGGEST SPOILER (and some history)</h2>
|
||||
|
||||
<p>Technically, <code>char o[0];</code> is illegal (SS 6.7.6.2) according to ISO (N1570).
|
||||
And <code>char o[];</code> is a conditionally supported feature (SS 6.7.6.2, 6.10.8.3).
|
||||
However, it works in modern C implementations as an extension,
|
||||
including GCC, Clang and TCC, for X86 and ARM,
|
||||
on multiple operating systems (Linux, OSX, FreeBSD), so it is <em>functionally</em> correct.
|
||||
MicroSoft Visual C++ does not allow <code>char o[]</code> or <code>char o[0]</code>,
|
||||
and implements its close relative <code>char o[1]</code>
|
||||
such that writing more than a single char will cause a fault, which is <em>technically</em> correct.</p>
|
||||
|
||||
<p>But how does this work at all, even as an extension?
|
||||
<code>o[]</code> allocates one(!) byte in a zero-filled data area (formerly BSS).
|
||||
The UNIX-derived systems this code was run upon all place the
|
||||
storage of <code>O</code> and <code>o</code> into a zero-filled VM page with nothing following:
|
||||
thus, there are plenty of available and unused zero bytes following <code>O</code>, addressed via <code>o</code>,
|
||||
and this code only needs 17.
|
||||
This is <em>undefined</em> behavior, but as shown, it is widely available.</p>
|
||||
|
||||
<p>The <a href="http://minnie.tuhs.org/cgi-bin/utree.pl">1979 Seventh Edition UNIX</a> (v7) system pre-dates the IOCCC by several years,
|
||||
and the source code to <code>sh</code>(1) was a <a href="http://ioccc.org/all/README">significant inspiration for this contest</a>.
|
||||
There are two C compilers present: <code>cc</code> written by Dennis Ritchie, and <code>pcc</code> written by Steven Johnson.
|
||||
The nested conditional expressions in 116 and smaller are too complex for <code>cc</code> (“Illegal conditional”)
|
||||
but <code>pcc</code> handles them correctly, and neither compiler accepts <code>char o[]</code> or <code>char o[0]</code> nor puts the bytes in
|
||||
a zero-filled page (as a paged VM was not yet written for UNIX).</p>
|
||||
|
||||
<p>More significant is that v7 <code>printf</code>(3) does not report the number of characters written,
|
||||
and therefore 111 compiles but does not work correctly;
|
||||
113 is the shortest code that compiles and runs correctly in Ancient UNIX.</p>
|
||||
|
||||
<blockquote><p>Interesting digression: v7 <code>printf</code> (<code>_doprnt</code>) was written in PDP-11 assembler.
|
||||
It pre-dates <code>varargs.h</code>, and assembly was the only way to gain access to the variable argument types.
|
||||
The first edition <em>The C Programming Language</em>, 1978, page 71, remarks:</p>
|
||||
|
||||
<blockquote><p>By the way, there is no entirely satisfactory way to write a portable function
|
||||
that accepts a variable number of arguments, because there is no portable way
|
||||
for the called function to determine how many arguments where actually passed to it in a given call.
|
||||
…
|
||||
<code>printf</code>, the most common C function with a variable number of arguments,
|
||||
… is also non-portable and must be modified for different environments.</p></blockquote>
|
||||
|
||||
<p>It is fascinating to explore the roots of C through the history of the operating system
|
||||
it was birthed within. C is often criticized as a difficult, inscrutable language,
|
||||
but as this contest demonstrates, this is in parody.
|
||||
<em>Shell-gol</em> is one of those instances where someone <a href="http://www.computerworld.com.au/article/279011/a-z_programming_languages_bourne_shell_sh/?pp=4">preferred a different language</a>,
|
||||
and (ab)used the preprocessor to re-mold C into something they found comfortable.
|
||||
Most of the rest of the system, especially in the kernel,
|
||||
are models of clarity and efficient expression.</p></blockquote>
|
||||
|
||||
<h2>TL;DR</h2>
|
||||
|
||||
<p>So which version is the shortest, portable hex dump?</p>
|
||||
|
||||
<ul>
|
||||
<li>109 works with (most?) modern Unix-like systems and compilers, see example list above.</li>
|
||||
<li>111 works adds Microsoft, is standards-approved, and does not exploit undefined behavior.</li>
|
||||
<li>113 works on Ancient UNIX and the portable C compiler.</li>
|
||||
<li>119 works on all platforms, including the original Ritchie PDP-11 C compiler.</li>
|
||||
</ul>
|
||||
|
||||
|
||||
<h2>Coda</h2>
|
||||
|
||||
<p>Clearly, the smallest possible program violates modern best practice.
|
||||
It is not even possible to compile cleanly, but it will compile correctly.</p>
|
||||
|
||||
<p>Included is <code>prog.nowarn.c</code>, a modern C implementation that is – perhaps – the smallest,
|
||||
clean-compiling version using <code>clang</code> or <code>gcc</code> with <code>-Wall -pedantic</code>.
|
||||
Note that while this is larger than all but the original attempt,
|
||||
it will <em>not</em> work with the original Ritchie PDP-11 C compiler,
|
||||
since the expression within the ternary remains too complex.</p>
|
||||
|
||||
<p>Try</p>
|
||||
|
||||
<pre><code>cat prog.c prog.nowarn.c
|
||||
</code></pre>
|
||||
|
||||
<p>to compare the differences (<code>diff</code> is not helpful on a one-liner).</p>
|
||||
|
||||
<hr />
|
||||
|
||||
<!--
|
||||
(c) Copyright 1984-2018, [Leo Broukhis, Simon Cooper, Landon Curt Noll][judges] - All rights reserved
|
||||
This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License][cc].
|
||||
|
||||
[judges]: http://www.ioccc.org/judges.html
|
||||
[cc]: http://creativecommons.org/licenses/by-sa/3.0/
|
||||
-->
|
||||
|
||||
<TABLE><TR>
|
||||
<TD><a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a></TD>
|
||||
<TD><P>© Copyright 1984-2018,
|
||||
<A HREF="/judges.html">Leo Broukhis, Simon Cooper, Landon Curt Noll</A>
|
||||
- All rights reserved<BR>
|
||||
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.</P></TD>
|
||||
<TD> <!--<a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>--></TD>
|
||||
</TR></TABLE>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,224 @@
|
||||
# Best one-liner
|
||||
|
||||
Dave Burton
|
||||
<http://ioccc.snox.net>
|
||||
|
||||
|
||||
## Judges' comments:
|
||||
### To use:
|
||||
|
||||
make
|
||||
./prog < any-file
|
||||
|
||||
### Try:
|
||||
|
||||
./prog < prog
|
||||
./prog < prog.c
|
||||
|
||||
### Selected Judges Remarks:
|
||||
|
||||
One line, one array, one loop and one statement but it prints many bytes. It won.
|
||||
|
||||
## Author's comments:
|
||||
Feed this anything on stdin.
|
||||
|
||||
The included script may be used as a driver program for this entry.
|
||||
It adds several nice-to-have features, and allows salt-to-taste customizability.
|
||||
|
||||
This code has been compiled and tested on:
|
||||
|
||||
* OSX-10.11, gcc 4.2.1 and clang 3.0, -O0 and -O3
|
||||
* X86-Linux, gcc 4.1.2 and 4.8.4, -O0 and -O3
|
||||
* ARM-QEMU, gcc 4.8.4, -O0 and -O3
|
||||
* X86-FreeBSD 10.3, clang 3.4.1, -O0 and -O3
|
||||
* TCC 0.9.26 (x86-64) (used as a proxy for [2001/bellard](http://ioccc.org/years.html#2001_bellard))
|
||||
* UNIX V7 pcc (Johnson's C compiler) on a [simulated PDP-11/45](http://simh.trailing-edge.com/)
|
||||
(see spoiler for further detail)
|
||||
|
||||
<div style="margin-bottom:60em;margin-top:4em"><strong>Spoiler below; scroll down.</strong></div>
|
||||
|
||||
SPOILER
|
||||
-------
|
||||
|
||||
This started life as a personal challenge: write the shortest hex dump utility in C.
|
||||
|
||||
The self-imposed ground rules in the hunt for shortest:
|
||||
the code has to output correctly,
|
||||
no extraneous newlines, the code must be portable,
|
||||
and without bypassing the solution using execve(2),
|
||||
or functions built upon it (system(3), popen(3), etc). E.g:
|
||||
|
||||
main(){system("hd");} // it's turtles all the way down!
|
||||
|
||||
is not acceptable.
|
||||
And, "output correctly" is the output format of this entry. QED.
|
||||
|
||||
Early attempts ended up at a reasonably short 157 characters.
|
||||
This was too long for a one-liner, so it had to be formatted as something more interesting.
|
||||
This solution is pleasing, but a true one-liner was the hoped-for goal.
|
||||
Here is the 157 character version in it's entirety:
|
||||
|
||||
char O,o[ 20];L(O){
|
||||
putchar ( O+ 48+39 *(9<
|
||||
O));} I(O) {L(~ 15);
|
||||
O|| puts(o);} main (l){
|
||||
for(;~(l=getchar()) ;I(O
|
||||
&=15 ))o[ O++ ]=4<
|
||||
(4^l >>5) ?l: 46,L
|
||||
(l>> 4),L (l&15);O&&
|
||||
I(o[ O]=0 );}//157c
|
||||
|
||||
While thinking about this in the shower (you are not the only one that does this!)
|
||||
came the idea which reduced the code to just 119 chars.
|
||||
This dramatic reduction started to feel close to minimal, and the hunt resumed.
|
||||
The progression to the present size is interesting,
|
||||
and it is interesting that the same algorithm can be expressed so differently
|
||||
by slight rearrangement of its parts.
|
||||
|
||||
// 119
|
||||
char O,o[20];main(l){for(;~(l=getchar());O||puts(o))o[O++]=isprint(l)?l:46,O&=15,printf("%02x ",l);O[o]=0;O&&puts(o);}
|
||||
// 116
|
||||
char O,o[20];main(l){for(;O++[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;(O&=15)||puts(o))printf("%02x ",l);O>1&&puts(o);}
|
||||
// 115
|
||||
char O,o[20];main(l){for(;O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;(O=-~O&15)||puts(o))printf("%02x ",l);O&&puts(o);}
|
||||
// 113
|
||||
char O,o[20];main(l){for(;~l;(O=~l?printf("%02x ",l),-~O&15:!O)||puts(o))O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;}
|
||||
// 112
|
||||
char O,o[20];main(l){for(;~l;(O=~l?-~O&printf("%02x ",l)*5:!O)||puts(o))O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0;}
|
||||
// 111
|
||||
char O,o[20];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
// 110
|
||||
char O,o[0];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
// 109
|
||||
char O,o[];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
|
||||
There are solutions without the ASCII sidebar that are significantly shorter.
|
||||
These do not compete in the same category, since their output is materially different.
|
||||
They are derived from the solutions above, but are still fascinating as concise utilities,
|
||||
so here is their progression, for the curious:
|
||||
|
||||
// 102
|
||||
I;O(O){putchar(O+48+39*(9<O));}main(l){for(;~(l=getchar());O(++I&15?~15:~37))O(l>>4),O(l&15);O(-38);}
|
||||
// 80
|
||||
O;main(l){while(~(l=getchar()))printf("%02x%c",l,++O&15?32:10);O&15&&puts("");}
|
||||
// 78
|
||||
O;main(l){for(;~l;(O=~(l=getchar())?printf("%02x ",l),-~O&15:!O)||puts(""));}
|
||||
// 77
|
||||
O;main(l){for(;~l;(O=~(l=getchar())?-~O&printf("%02x ",l)*5:!O)||puts(""));}
|
||||
// 76
|
||||
O;main(l){for(;~l;O||puts(&O))O=~(l=getchar())?-~O&printf("%02x ",l)*5:!O;}
|
||||
|
||||
It is possible to reduce the size of each of these by one by eliding the trailing newline.
|
||||
This was not reported above since the newline kept getting appended by processing software,
|
||||
and it is more interesting to remove characters by changing code than by filtering:
|
||||
|
||||
$ tr -d '\012' < 109.c | wc -c
|
||||
108
|
||||
$ tr -d '\012' < 76.c | wc -c
|
||||
75
|
||||
$ tr -d '\012' < 109.c | cc -o 108 -xc -
|
||||
$ verify 108
|
||||
$
|
||||
|
||||
An obfuscated hex dump has been done before:
|
||||
[1986/bright/bright.c](http://ioccc.org/1986/bright/bright.c) is similar in function,
|
||||
but it uses more characters in just setting up its obfuscation than this entire solution!
|
||||
|
||||
BIGGER SPOILER
|
||||
--------------
|
||||
|
||||
If you are unhappy with the way DEL (character 127) is handled,
|
||||
replace the cryptic expression `4<(4^l>>5)?l:46` with `32>l|l>126?46:l`.
|
||||
Both take the same number of characters, but the former is a bit more interesting.
|
||||
Bonus question: how does the first expression work?
|
||||
|
||||
BIGGEST SPOILER (and some history)
|
||||
----------------------------------
|
||||
|
||||
Technically, `char o[0];` is illegal (SS 6.7.6.2) according to ISO (N1570).
|
||||
And `char o[];` is a conditionally supported feature (SS 6.7.6.2, 6.10.8.3).
|
||||
However, it works in modern C implementations as an extension,
|
||||
including GCC, Clang and TCC, for X86 and ARM,
|
||||
on multiple operating systems (Linux, OSX, FreeBSD), so it is _functionally_ correct.
|
||||
MicroSoft Visual C++ does not allow `char o[]` or `char o[0]`,
|
||||
and implements its close relative `char o[1]`
|
||||
such that writing more than a single char will cause a fault, which is _technically_ correct.
|
||||
|
||||
But how does this work at all, even as an extension?
|
||||
`o[]` allocates one(!) byte in a zero-filled data area (formerly BSS).
|
||||
The UNIX-derived systems this code was run upon all place the
|
||||
storage of `O` and `o` into a zero-filled VM page with nothing following:
|
||||
thus, there are plenty of available and unused zero bytes following `O`, addressed via `o`,
|
||||
and this code only needs 17.
|
||||
This is _undefined_ behavior, but as shown, it is widely available.
|
||||
|
||||
The [1979 Seventh Edition UNIX][1] (v7) system pre-dates the IOCCC by several years,
|
||||
and the source code to `sh`(1) was a [significant inspiration for this contest][2].
|
||||
There are two C compilers present: `cc` written by Dennis Ritchie, and `pcc` written by Steven Johnson.
|
||||
The nested conditional expressions in 116 and smaller are too complex for `cc` ("Illegal conditional")
|
||||
but `pcc` handles them correctly, and neither compiler accepts `char o[]` or `char o[0]` nor puts the bytes in
|
||||
a zero-filled page (as a paged VM was not yet written for UNIX).
|
||||
|
||||
[1]: http://minnie.tuhs.org/cgi-bin/utree.pl
|
||||
[2]: http://ioccc.org/all/README
|
||||
[3]: http://www.computerworld.com.au/article/279011/a-z_programming_languages_bourne_shell_sh/?pp=4
|
||||
|
||||
More significant is that v7 `printf`(3) does not report the number of characters written,
|
||||
and therefore 111 compiles but does not work correctly;
|
||||
113 is the shortest code that compiles and runs correctly in Ancient UNIX.
|
||||
|
||||
> Interesting digression: v7 `printf` (`_doprnt`) was written in PDP-11 assembler.
|
||||
> It pre-dates `varargs.h`, and assembly was the only way to gain access to the variable argument types.
|
||||
> The first edition _The C Programming Language_, 1978, page 71, remarks:
|
||||
|
||||
>> By the way, there is no entirely satisfactory way to write a portable function
|
||||
>> that accepts a variable number of arguments, because there is no portable way
|
||||
>> for the called function to determine how many arguments where actually passed to it in a given call.
|
||||
>> ...
|
||||
>> `printf`, the most common C function with a variable number of arguments,
|
||||
>> ... is also non-portable and must be modified for different environments.
|
||||
|
||||
> It is fascinating to explore the roots of C through the history of the operating system
|
||||
> it was birthed within. C is often criticized as a difficult, inscrutable language,
|
||||
> but as this contest demonstrates, this is in parody.
|
||||
> _Shell-gol_ is one of those instances where someone [preferred a different language][3],
|
||||
> and (ab)used the preprocessor to re-mold C into something they found comfortable.
|
||||
> Most of the rest of the system, especially in the kernel,
|
||||
> are models of clarity and efficient expression.
|
||||
|
||||
TL;DR
|
||||
-----
|
||||
|
||||
So which version is the shortest, portable hex dump?
|
||||
|
||||
* 109 works with (most?) modern Unix-like systems and compilers, see example list above.
|
||||
* 111 works adds Microsoft, is standards-approved, and does not exploit undefined behavior.
|
||||
* 113 works on Ancient UNIX and the portable C compiler.
|
||||
* 119 works on all platforms, including the original Ritchie PDP-11 C compiler.
|
||||
|
||||
Coda
|
||||
----
|
||||
|
||||
Clearly, the smallest possible program violates modern best practice.
|
||||
It is not even possible to compile cleanly, but it will compile correctly.
|
||||
|
||||
Included is `prog.nowarn.c`, a modern C implementation that is -- perhaps -- the smallest,
|
||||
clean-compiling version using `clang` or `gcc` with `-Wall -pedantic`.
|
||||
Note that while this is larger than all but the original attempt,
|
||||
it will *not* work with the original Ritchie PDP-11 C compiler,
|
||||
since the expression within the ternary remains too complex.
|
||||
|
||||
Try
|
||||
|
||||
cat prog.c prog.nowarn.c
|
||||
|
||||
to compare the differences (`diff` is not helpful on a one-liner).
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
<!--
|
||||
(c) Copyright 1984-2018, [Leo Broukhis, Simon Cooper, Landon Curt Noll][judges] - All rights reserved
|
||||
This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License][cc].
|
||||
|
||||
[judges]: http://www.ioccc.org/judges.html
|
||||
[cc]: http://creativecommons.org/licenses/by-sa/3.0/
|
||||
-->
|
||||
@@ -0,0 +1 @@
|
||||
char O,o[];main(l){for(;~l;O||puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;}
|
||||
@@ -0,0 +1 @@
|
||||
int O;char o[17];int main(int l,char**v){for(;~l;O?O:puts(o))O=(O[o]=~(l=getchar())?4<(4^l>>5)?l:46:0)?-~O&printf("%02x ",l)*5:!O;return!v;}
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
|
||||
hd=${HD:=prog}
|
||||
|
||||
cat $* |
|
||||
$hd |
|
||||
awk '
|
||||
BEGIN { nodup=1; group=1 }
|
||||
nodup {
|
||||
# suppress duplicate lines
|
||||
if ($0 == prev) {
|
||||
if (dots == 0)
|
||||
printf "%04x: . . .\n", off
|
||||
dots = off += 16
|
||||
next
|
||||
}
|
||||
prev = $0
|
||||
dots = 0
|
||||
}
|
||||
{
|
||||
# determine the number of bytes, rest is ascii
|
||||
nb = 16
|
||||
if (length($0) < 48 || length($16) != 2) {
|
||||
for (nb=1; nb<NF; ++nb)
|
||||
if (length($nb) != 2 || $nb !~ /[0-9a-f][0-9a-f]/)
|
||||
break
|
||||
--nb
|
||||
}
|
||||
}
|
||||
group {
|
||||
sz = 35
|
||||
hex = $1 $2 $3 $4 " " $5 $6 $7 $8 " " $9 $10 $11 $12 " " $13 $14 $15 $16
|
||||
if (nb < 16) hex = substr(hex, 1, nb*2+nb/4)
|
||||
}
|
||||
!group {
|
||||
sz = 47
|
||||
hex = substr($0, 0, nb*3-1)
|
||||
}
|
||||
{
|
||||
asc = substr($0, nb*3+1)
|
||||
printf "%04x: %-*.*s |%-16s|\n", off, sz, sz, hex, asc
|
||||
off += nb
|
||||
}'
|
||||
Reference in new issue
Block a user