4 ms·
cat.c is also interesting. The GNU version is an appalling unreadable mess. V6's assembly implementation in easier to understand. V6: http://www.bsdlover.cn/st
by p9idf 15y ago
cat.c is also interesting. The GNU version is an appalling unreadable mess. V6's assembly implementation in easier to understand.
V6: http://www.bsdlover.cn/study/UnixTree/V6/usr/source/s1/cat.s.html http://www.bsdlover.cn/study/UnixTree/V6/usr/source/s1/cat.s...
V7: http://www.bsdlover.cn/study/UnixTree/V7/usr/src/cmd/cat.c.html http://www.bsdlover.cn/study/UnixTree/V7/usr/src/cmd/cat.c.h...
Plan 9: http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/cat.c http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/cat.c
BSD: http://www.koders.com/c/fidF501905968D8BE7BBDD355C3C8DB628048A0DEDE.aspx?s=netbsd#L1 http://www.koders.com/c/fidF501905968D8BE7BBDD355C3C8DB62804...
GNU: http://git.savannah.gnu.org/cgit/coreutils.git/plain/src/cat.c http://git.savannah.gnu.org/cgit/coreutils.git/plain/src/cat...
- bdonlan 15y agoV6's assembly version also does less. The reason GNU's is so complex is because it has a line-numbering feature (cat -n) not supported in V6 or V7, and also tries to read and write large chunks, to avoid overhead from calling stdio functions in a loop. It also tries to take advantage of non-portable extensions where possible, but fall back to portable code when not supported. Yeah, it looks a bit complex at first, but it's not really that bad if you actually take the time to read it.
- p9idf 15y agoThose features simply do not belong in a program whose purpose is to concatenate its input. If you want to number a file's lines, 'echo ,n | ed file | sed 1d' or 'awk ''{ print NR " " $0 }''' will do just fine. You could even wrap your ed or awk script into a shell script with a descriptive name like "lineno" rather than something silly like "cat -n". The reason GNU's is so complex is because it does many things and does them poorly. The V6 implementation does exactly what is says on the tin, does it well, and does nothing more: it catenates files.
- ez77 15y agoI liked your use of ed. You can avoid one pipe with the -s option: `echo ,n | ed -s file`. Also, another POSIX one-process option besides awk: `pr -tn file` (with different padding).
- pyre 15y agoBut now you've spawned many processes when one could have been used, and you'll incur the wrath of people that think that 'grep foo file' is 1000x more efficient than 'cat file | grep foo'.
- ianb 15y agoI am confused by your definition of "poorly". Are you asserting that GNU cat is slow, or unportable, or uses too much memory, or some other actual noticeable problem?
- p9idf 15y agoYes. Gnu cat is slow. http://hnwriteup.blogspot.com/2011/07/gnu-cat-vs-plan-9-cat.html http://hnwriteup.blogspot.com/2011/07/gnu-cat-vs-plan-9-cat....
- cpeterso 15y agoGNU's Hello World (version 2.7) example is 586 KB gzipped. https://www.gnu.org/software/hello/ https://www.gnu.org/software/hello/
- rat 15y ago"The GNU Hello program produces a familiar, friendly greeting. Yes, this is another implementation of the classic program that prints “Hello, world!” when you run it. However, unlike the minimal version often seen, GNU Hello processes its argument list to modify its behavior, supports greetings in many languages, and so on. The primary purpose of GNU Hello is to demonstrate how to write other programs that do these things; it serves as a model for GNU coding standards and GNU maintainer practices."