/* -*- c -*- */ /* smallest */ /* the smallest running program doing nothing */ main() { return 0; } /* hello world */ /* print a simple string on stdout */ #include main() { puts("Hello World"); return 0; } /* argv */ /* access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) */ #include main(int n, char **argv) { if (n > 1) puts(argv[1]); return 0; } /* env */ /* access environment variable */ #include main() { char *s = getenv("HOME"); if (s) puts(s); return 0; } /* test file exists */ /* return exit code error (non zero) if a file does not exist */ #include main() { return access("/etc/mtab", F_OK); } /* test file readable */ /* return exit code error (non zero) if a file is not readable */ #include main() { return access("/etc/mtab", R_OK); } /* formatting */ /* print integers in a simple formatted string */ #include main() { int a=1, b=2; printf("%d + %d = %d\n", a, b, a + b); return 0; } /* system */ /* call an external program and check the return value */ #include main() { if (system("false")) fprintf(stderr, "false failed\n"); system("echo done"); return 0; } /* sed in place */ /* remove #-comments from a file (modifying the file, i.e. in place) */ #include #include main (int argc, char **argv) { FILE *f; int c=1, t=0, i=0; if (argc-c) { f=fopen(argv[c],"r+"); while (c) switch (c=getc(f)) { case '#' : if (!t) { t=1,i-=2; break; } case '\n' : if (t) { t=0; break; } default : if (t==1) { i--; break; } fseek(f,i-1,1); putc(c++,f); fseek(f,-i,1); } truncate(argv[1],ftell(f)+i); } return 0; }