Step 6 of 6
Challenge: a real command-line tool
Command-line tools follow conventions that scripts and other programs rely on:
- Normal output goes to stdout; error messages go to stderr (
fprintf(stderr, ...)), so they don't pollute output that's piped somewhere else. - The exit code reports success:
0means everything worked; nonzero means something failed. Scripts check it (if ./tool; then ...).<stdlib.h>names themEXIT_SUCCESSandEXIT_FAILURE. - Keep going after one bad input when that's useful, but still report failure at the end.
Your turn: write lines FILE..., a tool that prints COUNT NAME for each file and a final COUNT total line. A file that can't be opened prints lines: cannot open NAME to stderr, is skipped, and makes the program exit with code 1 at the end (after the total). With no arguments at all, print usage: lines FILE... to stderr and exit with code 2.