Unix Tools
Seven tools you use every day, rebuilt from nothing. Each chapter is one complete program and one technique: streaming reads, a state machine, searching, owning the lines you sorted, splitting on a delimiter, turning a number into text, and deciding what needs rebuilding. The tools are small enough to hold in your head and old enough that their design is the lesson.
catis a loop around a fixed buffer. The buffer size is a decision about memory, not about correctness, which is why it can copy a file larger than memory.wccounts words by counting transitions into a word, not words. That is a state machine, and it is why leading spaces and double spaces cost nothing.- Sorting lines means owning them. The moment the sorted output has to outlive the buffer it was read into, you have to answer who allocates.
printfbuilds its digits backwards, because the only way to get the last digit of a number is to divide, and division hands them to you in reverse.makeis a topological sort over a graph of file timestamps. Everything else about it is syntax.
7 chapters.
- catA loop around one fixed buffer, which is why it can copy a file larger than memory.
- wcCounting words means counting the moments you enter one.
- grepSearch is a loop over start positions, and the exit code is the answer.
- sortSorting lines is easy. Owning them is the part that costs you something.
- cutSplitting on a delimiter, and the edge cases that decide whether it is correct.
- printfTurning a number into text, digit by digit, backwards.
- makeA dependency graph, a topological order, and a comparison of timestamps.