12 ms·
Decoded: GNU Coreutils (2018)
- version_five 3y agoCool, I didn't know this existed. I think simple ones like `yes` can be very interesting just to see how the base code of a utility (that writes to stdout) is written. https://maizure.org/projects/decoded-gnu-coreutils/yes.html https://maizure.org/projects/decoded-gnu-coreutils/yes.html
- 38 3y agohere is a Go implementation for fun: package main import "flag" func main() { yes := flag.String("m", "y", "message") flag.Parse() for { println(*yes) } }
- sltkr 3y agoHere's a Brainfuck implementation: ++++++++++[->++++++++++++>+<<]>+[.>.<] (Unfortunately, Brainfuck doesn't support command line flags.)
- dale_glass 3y agoYeah, GNU yes could be about that simple but it's a good deal more complex to obtain the best performance possible: https://www.reddit.com/r/unix/comments/6gxduc/how_is_gnu_yes_so_fast/ https://www.reddit.com/r/unix/comments/6gxduc/how_is_gnu_yes...
- 38 3y agoum, who cares? most times people are needing to use "yes" as part of a "configure" script or similar. even if GNU yes was 10 times slower, it would NEVER be the bottleneck in any situation. so whats the point?
- the-smug-one 3y agoYou'll have to ask the author that. Now, I'd like to ask the author who cares how you implement 'yes' in Go ;-).
- 38 3y agoyes C lines of code: 112 yes Go lines of code: 9
- placesalt 3y ago16 lines of go, with comments & whitespace
- 38 3y ago134 lines of C, with comments & whitespace
- deleted 3y ago[deleted]
- not2b 3y agoThat isn't an apples-to-apples comparison. A C version could be similarly short, but the GNU yes implementer got carried away with efficiency considerations. If the Go program did the same, it would be longer.
- hunson_abadeer 3y agoIt feels like you're making a bad-faith argument here. You can implement 'yes' in a straightforward way in a couple lines of C, too. main(int argc, char** argv) { while (1) { if (argc > 1) for (int i = 1; i < argc; i++) printf("%s%c", argv[i], (i == argc - 1) ? '\n' : ' '); else puts("y"); } } The point other folks are making is that it's written differently for a reason. Maybe not a reason that's important, but at the very least, let's try to compare apples to apples.
- rcr 3y agoFyi before anyone else decides to read the idiocy that follows in this thread, this is guy is a known troll with a long history of this exact behavior. Same person: https://news.ycombinator.com/item?id=27862463 https://news.ycombinator.com/item?id=27862463 I think this person is just mentally ill unfortunately.
- 38 3y agowhen people resort to doxing, it shows how truly pathetic they are. I pity those people.
- version_five 3y agoReplying to myself because the thread got trolled: My point was in part that it's valuable for even a simple utility to be well written and optimized and that it's nice to have these minimal examples to learn about how to, e.g. write output very quickly. The program is so short that presumably the number of lines is unimportant, and if the author knows how to do it they might as well make it as fast as possible so it's never in the way, and so we can learn from it. That's why I think it's a good example.
- m1keil 3y agoalso quite interesting to compare with other implementations from the present and the past: https://github.com/openbsd/src/blob/master/usr.bin/yes/yes.c https://github.com/openbsd/src/blob/master/usr.bin/yes/yes.c https://github.com/freebsd/freebsd-src/blob/release/4.0.0/usr.bin/yes/yes.c https://github.com/freebsd/freebsd-src/blob/release/4.0.0/us...
- nilespotter 3y agoFun fact, if you install coretuils from homebrew on MacOS, since MacOS already ships with od(1), od from coretuils is installed as god(1)
- aidenn0 3y agoWell of course it is, the "G" lets you distinguish the Gnu "od" from the OG "od"
- akoboldfrying 3y ago[flagged]
- dang 3y agoRelated: Decoded: GNU Coreutils (2018) - https://news.ycombinator.com/item?id=29871037 https://news.ycombinator.com/item?id=29871037 - Jan 2022 (7 comments) Decoded: GNU coreutils (2019) - https://news.ycombinator.com/item?id=26411908 https://news.ycombinator.com/item?id=26411908 - March 2021 (38 comments) Decoded: GNU Coreutils - https://news.ycombinator.com/item?id=20328650 https://news.ycombinator.com/item?id=20328650 - July 2019 (55 comments)
- staplung 3y agoKind of a fun list of basic utilities. I've been using UNIX for a loooong time and had never heard of e.g. `shred`, `shuf`, or `factor`. Makes me want to try sudo find / -type f -exec shred {} \; to see how far it gets before killing itself (on a VM or easily re-flashed machine of course).
- Denvercoder9 3y agoOn Linux, it'd likely go until completion. You can't write to executables and libraries that are currently running (ETXTBUSY), so shred can't trash either itself or find.
- staplung 3y agoTrue, but I wonder if it would first get stuck on trying to shred e.g. /dev/stderr or if after shredding the files in /etc some daemon woke up and tried to read a file there and choked.
- yjftsjthsd-h 3y agoI'd be curious what it did to /sys and /proc
- rascul 3y agoThat doesn't seem right. I can delete nano's executable while it's running, and rm can remove itself. What am I missing?
- shric 3y agorm doesn't actually modify the file. It does an unlink which removes the link from the filename to the file. Essentially, a file can have 0 or more names linked to it. As long as a file has at least one name or at least one process with it open, it will persevere. By contrast, shred actually writes to the underlying file.
- 3y ago
- 1vuio0pswjnm7 3y ago"Many of these utilities are approaching 30 years old and include revisions by many people over the years." "They are not designed for long life or to scale beyond their role." Would love to see some examples from the author of programs he believes are "designed for long life" that have been around 30 years. Or even ones he thinks will be around for 30 years.
- radlad 3y agoI think perhaps the author meant "long development life." As in, they are basically write-once utilities. Part of the benefit of doing "one thing well."
- remexre 3y agoI think that was meant to be understood in context as "meant to be actively developed" (as opposed to maintained) for a long time. The practices the author lists are ones that in larger programs would typically be criticized for damaging maintainability as the program grows.
- matheusmoreira 3y agoWell, what exactly is their role? Is there a limit to what we can do with these things? To test a little programming language I made, I created a testing framework with bash and coreutils. I felt guilty about not using a "proper" language at first but it works so well. In parallel too. I found that the the only thing I couldn't test was the argv[0] of the program. No matter how much I twisted the programs, I couldn't get them to do exactly what I wanted. So I sent a feature request and a patch to coreutils to give env this feature: https://lists.gnu.org/archive/html/coreutils/2023-08/msg00060.html https://lists.gnu.org/archive/html/coreutils/2023-08/msg0006... Looks like it's gonna make it in. A new feature for this old program.
- MenhirMike 3y agoI do like that /bin/true can actually fail and return false, which technically makes a "Not /bin/false" invocation more resilient: https://github.com/coreutils/coreutils/blob/master/src/true.c#L67 https://github.com/coreutils/coreutils/blob/master/src/true.... (and yes, I know it's the most unlikely thing, I just found it funny)
- yjftsjthsd-h 3y agoOnly if run with options, right? It looks like just running `true` is unaffected unless the comment is misleading me.
- patal 3y agoYeah, I don't see it either. If being run without --help or --version, true can only ever return EXIT_SUCCESS. However, I find it interesting that true and false use the very same implementation.
- somat 3y agoThe other interesting thing about the true command is how much more complicated it got then it needed to be. first an exercise touch mytrue chmod u+x mytrue ./mytrue echo "error code for mytrue is $?" This is literally how true started life. yes it is very zen. The first offense was legal. All code had to have a copyright disclaimer. even an empty file? Yes. so now it was a file with a copyright disclaimer and nothing else. And the koan-like question comes to mind is "Can you copyright nothing?" well AT&T sure tried. Then somebody said our programs should be well defined and not depend on a fluke of unix, which at this point was probably a good idea. So true finally had code. It was "exit 0" Then somebody said we should write our system utilities in C instead of shell so it runs faster. openbsd still has a good example of how this would look. http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/usr.bin/true/true.c?rev=1.1&content-type=text/plain http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/usr.... At some point gnu bureaucracy got involved and said all programs must support the '-h' flag. so that got added, then they said all programs must support locale so that got added. now days gnu true is an astonishing 80 lines long. https://github.com/coreutils/coreutils/blob/master/src/true.c https://github.com/coreutils/coreutils/blob/master/src/true.... Which is fine I guess, but that is a lot of code for a program that by definition "Does nothing, successfully" http://trillian.mit.edu/~jc/humor/ATT_Copyright_true.html http://trillian.mit.edu/~jc/humor/ATT_Copyright_true.html
- asicsp 3y agoSee also: * How the GNU coreutils are tested: https://www.pixelbeat.org/docs/coreutils-testing.html https://www.pixelbeat.org/docs/coreutils-testing.html * Exploration of each of the coreutils commands: https://ratfactor.com/slackware/pkgblog/coreutils https://ratfactor.com/slackware/pkgblog/coreutils * Command line text processing with GNU Coreutils: https://learnbyexample.github.io/cli_text_processing_coreutils/ https://learnbyexample.github.io/cli_text_processing_coreuti... (my ebook that covers 20+ text processing tools)
- jmholla 3y agoI noticed at least one error, if the author is here. The short description on the shred page[0] is actually the description for csplit[1]. It should be something along the lines of "overwrite a file to hide its contents, and optionally delete it". [0]: https://maizure.org/projects/decoded-gnu-coreutils/shred.html https://maizure.org/projects/decoded-gnu-coreutils/shred.htm... [1]: https://maizure.org/projects/decoded-gnu-coreutils/csplit.html https://maizure.org/projects/decoded-gnu-coreutils/csplit.ht...
- boppo1 3y agoAs a novice programmer trying to sharpen my grasp of how to fruitfully apply DS&A, are there any of these I should look at in particular?
- nrabulinski 3y agoMaybe I’m misunderstanding but what do data structures and algorithms have to do with CLI tools?
- matthews2 3y agoThe cleverness inside coreutils is mostly around choosing effective ways to interface with the kernel, e.g. using copy_file_range() instead of read()/write() to avoid having to copy the data into userspace. It's more a software engineering endeavour instead of computer science.
- sylware 3y agoTry to avoid gnu options and niche commands. Stick to busybox commands as much as you can.
- msm_ 3y agoHonest question: why? I'm not really interested in portable scripts, usually I just want to get a job done as fast as possible and move on (for serious automation I prefer Python, shell is for more interactive use for me). Is there any upside to sticking to busybox commands in this case?
- sylware 3y agoWow, when I read that, I realize how much I have to teach, but there, it is worth restarting from scratch, it seems the basics are not even here.
- saagarjha 3y agoSounds like there’s still a little left for you to learn about not being pretentious.
- sylware 3y agoThat guy is starting from very far away, HN not the place as it is not enough to fill in that amount of blanks. And from the first thing you learn in real life, is humility, but sometimes, some people makes you feel you know everything, like here... but I very well know I am average/normal, which worsen even more his/her case. Yeah, a bit like John Snow. This is an horrible feeling. Not to mention, this post is border-line passive-aggressive... so...
- saagarjha 3y agoWhy do you feel that way? I see nothing that indicates that this person is clueless.
- Roark66 3y agoI might be missing a point of this site, but don't we have man (or info) pages for each of these?
- samtheprogram 3y agoRather than usage information, this site details how the programs work internally.