Useful UNIX API:s

Had an interesting conversation with a buddy last night. It started out as a shift-reduce problem with Bison and ended up a ping-pong of useful UNIX API:s. We concluded that despite having worked professionally with UNIX for over a decade, it is still very satisfying finding gems like these.

Most people are completely unaware they exist and end up rolling their own (buggy) implementations. For instance, string manipulation and various forms of linked lists. Which is why I many years ago extracted the frog DNA from Finit to a separate library called libite, or -lite for short. It imports the OpenBSD strlcpy() family of API:s, up-to-date queue.h with the _SAFE iterators, and more. Some people like libbsd for this, but I’ve found many of the ports incomplete and unsafe and prefer to stay closer to the upstream *BSD versions.

Update: This post was originally written Nov 14, 2015. It was a Saturday and I remember being extremely inspired when I wrote it. I’ve continued adding to it over the years, and still do. So, as of Jul 2, 2017 I’m bumping the modification date each time I add something new :-)

SysV search.h

Mangage a simple queue:

Manage hash search table:

Manage a binary search tree:

Linear search and update:

BSD sys/queue.h

This header has lots of macros for handling various forms of linked lists. The version in GLIBC is a bit behind the BSD’s, because the latter also have _safe() versions of some macros to aid the user in some tricky cases, e.g. when removing entries while iterating.

Several types of lists are supported:

  • LIST: Doubly linked list
  • SLIST: Single linked list
  • STAILQ: Single linked tail queue
  • SIMPLEQ: Simple queue
  • TAILQ: Tail queue

Here’s a few of them, this example for doubly linked lists:

  • LIST_INIT()
  • LIST_EMPTY()
  • LIST_FIRST()
  • LIST_NEXT()
  • LIST_REMOVE()
  • LIST_FOREACH()
  • LIST_INSERT_AFTER()
  • LIST_INSERT_BEFORE()
  • LIST_INSERT_HEAD()

I wrote a demo of the TAILQ API a couple of years ago.

Other Noteworthy API’s

Other (standard/POSIX) functions worthy of mentioning here are:

stdio.h

stdlib.h

glob.h

fnmatch.h

ftw.h

unistd.h

wordexp.h

Note: API’s specific to GNU or BSD are not included, but there are many more useful functions on your specific OS, in case you do not need to write code that is portable across different UNIX platforms. Examples can be unshare() and clone() on Linux and pledge() on OpenBSD, all highly useful but also very specific.