What's Next?

Congratulations

You have reimplemented 13 coreutils in C: echo, cat, rev, wc -c/l/w, head, tail, grep, toupper, tr, uniq, and tac.

That is real systems programming. These are the same algorithms that run on every Linux server in the world.

What to Explore Next

You have built the core. Here are ways to go deeper:

  • Add the missing flags -- head -n N with atoi to parse the argument; grep -i for case-insensitive matching; uniq -c to count occurrences.
  • Read from stdin -- Real coreutils read from stdin (or files given as arguments). Add fgets or getchar loops to handle real input.
  • Handle multiple files -- cat file1 file2 opens and concatenates multiple files using fopen, fread, fclose.
  • Add error handling -- What happens when a file does not exist? fprintf(stderr, ...) and non-zero exit codes.
  • Implement sort -- Sorting lines requires either dynamic memory (malloc) or a fixed buffer. A classic exercise.

Build Something Real

The ultimate exercise: write a complete coreutil that works on your system.

// mycat.c
#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc < 2) {
        // read from stdin
        int c;
        while ((c = getchar()) != EOF) putchar(c);
        return 0;
    }
    for (int i = 1; i < argc; i++) {
        FILE *f = fopen(argv[i], "r");
        if (!f) { fprintf(stderr, "mycat: %s: No such file\n", argv[i]); return 1; }
        int c;
        while ((c = fgetc(f)) != EOF) putchar(c);
        fclose(f);
    }
    return 0;
}

Compile with gcc mycat.c -o mycat and test it on real files. This is software you actually built, running on real hardware.

References

  • GNU coreutils source code -- Read the real implementations. src/cat.c is ~700 lines. src/wc.c handles UTF-8. See how far your implementations got.
  • The C Programming Language by Kernighan & Ritchie -- The original book. Chapter 7 on I/O is directly relevant.
  • Beej's Guide to C -- Free, comprehensive, practical.
  • man 1 coreutils -- On any Linux system, man cat, man grep, etc. document every flag.
← Previous