Rebuilding Unix Tools from Scratch in C
Every episode takes a standard Unix tool: cat, head, tail, grep, ls, and rebuilds it from a blank file. The only tools allowed are the C language and POSIX system calls.
No stdio.h. No printf. No frameworks. Just read(), write(), open(), lseek(), and the operating system. Every decision gets explained. Nothing is glossed over.
// the work
/* ** cat.c: concatenate and print files ** episode 01: file I/O, read(), write(), POSIX loops */ #include <unistd.h> #include <fcntl.h> #define BUFFER_SIZE 4096 int main(int argc, char **argv) { char buf[BUFFER_SIZE]; int fd; ssize_t n; int i; i = 1; if (argc < 2) { while ((n = read(0, buf, BUFFER_SIZE)) > 0) write(1, buf, n); return (0); } while (i < argc) { fd = open(argv[i], O_RDONLY); while ((n = read(fd, buf, BUFFER_SIZE)) > 0) write(1, buf, n); close(fd); i++; } return (0); }
// published
// also published