namei(1) from util-linux on FreeBSD

After a couple of crude attempts at making my own versions of namei(1) this spring, namei.c and namei.go, I decided to take a look at the original. My goals were to see if I had made any mistakes, and if possible, have the original properly working on FreeBSD.

Yes, I have made plenty of mistakes, mostly related to the handling of mountpoints when symlinks are at play. My own versions does fullfil my immediate needs, and they are BSD two-clause licensed. It was interesting making the Go version in about three days time, several weeks after creating the initial C version. This taught me a good lesson on how to write code in the Go language.

First, you should download util-linux v2.30, e.g. util-linux-2.30.tar.xz.

Next, you must extract the following files and place them in a common directory:

  • util-linux-2.30/include/bitops.h
  • util-linux-2.30/include/c.h
  • util-linux-2.30/include/closestream.h
  • util-linux-2.30/include/idcache.h
  • util-linux-2.30/include/nls.h
  • util-linux-2.30/include/strutils.h
  • util-linux-2.30/include/widechar.h
  • util-linux-2.30/include/xalloc.h
  • util-linux-2.30/lib/idcache.c
  • util-linux-2.30/lib/strutils.c
  • util-linux-2.30/misc-utils/namei.c

Next, you must apply these three patches:

--- idcache.c.orig
+++ idcache.c
@@ -1,3 +1,4 @@
+#define HAVE_NANOSLEEP

 #include <wchar.h>
 #include <pwd.h>
--- namei.c.orig
+++ namei.c
@@ -20,6 +20,10 @@
  *     Li Zefan (2007-09-10).
  */

+#define HAVE_FSYNC
+#define HAVE_NANOSLEEP
+#define PACKAGE_STRING "util-linux"
+
 #include <stdio.h>
 #include <unistd.h>
 #include <getopt.h>
--- strutils.c.orig
+++ strutils.c
@@ -3,6 +3,8 @@
  * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
  */

+#define HAVE_NANOSLEEP
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>

Now you can compile the source files and link the object files:

clang -c idcache.c namei.c strutils.c
clang -o namei idcache.o namei.o strutils.o

Bon appétit.

Leave a Reply

Your email address will not be published. Required fields are marked *