I used to post articles related to this topic here, but at some point I thought it better to dedicate this website exclusively to its original purpose, which is to publish my literary work. I'll make an exception for this little program I wrote in C, since it's kinda related.
If you're a *nix user, you're surely used to edit plain text, using your preferred text editor. While popular modern full-featured editors like Vim or GNU Emacs come with embedded paragraph formatting functions, with the old basic ones, like BSD nvi, you have to use external tools like fmt(1) (which you can also call from the editor, using key bindings.) Anyway, as I explain in the top comment in the code itself, none of the existing tools to format plain text paragraphs fully satisfied me, which is why I wrote my own.
Those familiar with unix-like systems know well what troff is, a very versatile tag like text formatting language, which among other things has been used by many to give format to published known books. To edit my novels I used groff, the GNU version. Well, the code I'm posting here, apart from improving some features present in other fmt versions also brings an innovation to make it easier (and safer) to work with troff files.
Tested in OpenBSD and Linux. I hope you'll find it useful.
Asked me some user of the OpenBSD mailing list, and suggested me to use strlcpy(). This is what I answered him:
I know about strlcpy() and other functions that I could use, including the wide char ones to deal with non ASCII characters. Notice that I didn't even use reallocarray(). I chose the "manual" way on purpose, first because my goal was to learn C and second, among other things, because doing it that way lets you compile it also under Linux or Mac. If your concern is it could leak or corrupt memory, go ahead and test it. You can add values to MALLOC_OPTIONS variable under OpenBSD or under Linux you can pass -fsanitize=address to gcc or use tools as valgrind. You'll see if there is something wrong. ;-)
GO BACK HOME