Related sites: | site map |

# aiju.de Home of aiju

- › about me/
- › b/
- » code/
- - › RatJS
  - › homework/
  - › k/
  - » misc/
  - - › molecule/
    - › pi
    - › sort
    - » tiny
  - › pdp11/
- › donate
- › electronics/
- › legal
- › misc/
- › plan 9/

# The Tiny Unix Tools

The goal is to find the shortest implementation of common UNIX tools. Neither errors nor success are signaled; "meteor proofness" is not required. Evil C tricks are encouraged, submissions welcome.

`rm` / `unlink` (no flags or anything):

    main(a,b)char**b;{while(*++b)unlink(*b);}

`cat`:

    main(a,b)char**b;{while(*++b){open(*b,0);while(read(3,&a,1)>0)write(1,&a,1);close(3);}}

`ed`:

    main(a){for(;;){read(0,&a,1);write(1,"?\n",2);}}

`ls`: (thanks to cls)

    #include <dirent.h>
    main(){DIR*d=opendir(".");struct dirent*e;while(e=readdir(d))puts(e->d_name);}

`kill`: (thanks to ente)

    main(a,b)char**b;{kill(atoi(b[2]),atoi(b[1]));}

`sh`: (thanks to nortti)

    main(){char a[256],*b,*c[256],**d;int p;while(1){write(1,"$",2);for(b=a;*(b-1)!='\n';b++){read(0,b,1);}*b=0;d=c;*d++=a;for(b=a;b<a+256&&*b!=0;b++){if(*b==' '||*b=='\n'){*b=0;*d=b+1;d++;}}*(d-1)=0;if(!(p=fork()))execvp(a,c);else wait(p);}}

`wc`: (thanks to TLH) char c,i;long l,w,b;main(){while(read(0,&c,1)){b++;if(c=='\n')l++;if(!isspace(c))i=1;else if(i){i=0;w++;}}if(i)w++;printf("\t%ld\t%ld\t%ld\n",l,w,b);}