Making strlcpy available in linux

Viewed 5227

I'm trying to compile a source code which was originally written for FreeBSD and since strlcpy isn't included in the linux GlibC , I'm now stopped at this stage.

GCC has suggested that I can use strncpy but I don't want to actually change the source code.

Do I need to re-compile my LibC ?

Any help is appreciated.

2 Answers

The best solution to port BSD applications is libbsd; and it's already packaged for most systems.

On Debian-based systems the development package is named libbsd-dev.

You can compile unmodified BSD source code by adding the following to your CFLAGS: -DLIBBSD_OVERLAY -I/usr/include/bsd, and linking with -lbsd.

However, instead of hardcoding those values, you should use pkg-config with the libbsd-overlay package if you intend to distribute your build system.

You can download source code from https://github.com/freebsd/freebsd/blob/master/sys/libkern/strlcpy.c and add it with 2 modifications to your source code:

$ diff strlcpy.c strlcpy.c.orig 
20c20
< //__FBSDID("$FreeBSD$");
---
> __FBSDID("$FreeBSD$");
23c23
< //#include <sys/libkern.h>
---
> #include <sys/libkern.h>

I have compiled successfully this function with gcc 7.5.0 on Ubuntu 18.04.

Related