* * * * *

    99 ways to program a hex, Part 29: K&R, system calls, full buffering,
                                  obfuscated

I suspect that many entries in the IOCCC (International Obfuscated C Code
Contest) [1] start out as normal [2], are converted to K&R [3] as a first
step, then rename all variables and functions to one or two character names
and unneeded spaces removed.

Much like today's version.

> /*************************************************************************
> *
> * Copyright 2012 by Sean Conner.  All Rights Reserved.
> *
> * This program is free software; you can redistribute it and/or
> * modify it under the terms of the GNU General Public License
> * as published by the Free Software Foundation; either version 2
> * of the License, or (at your option) any later version.
> *
> * This program is distributed in the hope that it will be useful,
> * but WITHOUT ANY WARRANTY; without even the implied warranty of
> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> * GNU General Public License for more details.
> *
> * You should have received a copy of the GNU General Public License
> * along with this program; if not, write to the Free Software
> * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
> *
> * Comments, questions and criticisms can be sent to: [email protected]
> *
> *************************************************************************/
>
> /* Style: K&R, system calls, full buffering, obfuscated */
>
> #include <errno.h>
> #include <fcntl.h>
>
> main(a,b)char**b;{int i,f;if(a==1)fa(0,1);else{for(i=1;i<a;i++){f=open
> (b[i],O_RDONLY);if(f==-1){fd(b[i]);continue;}ff(1,"-----",5);ff(1,b[i],
> strlen(b[i]));ff(1,"-----\n",6);fa(f,1);if(close(f)<0)fd(b[i]);}}return 0;}
>
> char a[4096],b[75*109];
> fa(c,d){int e,f,g,h;char*i,*p;memset(b,' ',sizeof(b));e=g=0;i=b;
> while((f=fe(c,(char *)a,sizeof(a)))>0){p=a;for(p=a;f>0;){h=fb(&i,p,f,e);
> p+=h;f-=h;e+=h;g++;if(g==109){ff(d,b,i-b);memset(b,' ',sizeof(b));g=0;
> i=b;}}}if (i-b>0)ff(d,b,i-b);}
>
> fb(a,p,c,d)char**a,*p;{char*e,*f,*g;int h;e=*a;fc(e,d,8,':');if(c>16)
> {c=16;}p+=c;f=&e[10+c*3];g=&e[58+c];for(h=0;h<c;h++){p--;g--;f-=3;
> if((*p>=' ')&&(*p<='~'))*g=*p;else*g = '.';fc(f,*p,2,' ');}e[58+h]='\n';
> *a=&e[59+h];return h;}
>
> fc(a,b,c,d)char*a;{a[c]=d;while(c--){a[c]=
> (b&0x0F)+'0';if(a[c]>'9')a[c]+=7;b>>=4;}}
>
> fd(a)char*a;{extern char**sys_errlist;extern int sys_nerr;int b=errno;
> ff(2,a,strlen(a));ff(2,": ",2);if(b>sys_nerr){ff(2,"(unknown)",9);}else
> {ff(2,sys_errlist[b],strlen(sys_errlist[b]));}ff(2,"\n",1);}
>
> fe(a,b,c)char*b;{int d=0,e;while(c>0){e=read(a,b,c);if(e<0){fd("read()");
> exit(1);}if(e==0){break;}d+=e;c-=e;b+=e;}return d;}
>
> ff(a,b,c)char*b;{if(write(a,b,c)<c){if(a!=2){fd("output");}exit(1);}}
>

The sad thing—I've seen production code like this (and no, The Protocol Stack
From Hell™ [4] isn't this bad, thankfully).

* Part 28: K&R, system calls, full buffering [5]
* Part 30: K&R, really obfuscated [6]

[1] http://www.ioccc.org/
[2] gopher://gopher.conman.org/0Phlog:2012/01/30.1
[3] gopher://gopher.conman.org/0Phlog:2012/02/05.1
[4] gopher://gopher.conman.org/0Phlog:2012/01/30.2
[5] gopher://gopher.conman.org/0Phlog:2012/02/05.1
[6] gopher://gopher.conman.org/0Phlog:2012/02/07.1

Email author at [email protected]