libbsd(7)
π NAME π
libbsd β utility functions from BSD systems. That's the whole thing, that's the vibe. It lives in section 7 of the Miscellaneous Information Manual, so libbsd(7), iykyk. π
β¨ DESCRIPTION β¨
okay so bestie, here's the tea: the libbsd library gives you a whole set of compatibility macros and functions that you'd normally only find on BSD-based systems. Its purpose? Make those available on non-BSD based systems to ease portability. That's it. That's the mission. π
Now, there are two ways to use this library, and they are NOT equal, I fear.
π overlay mode (the preferred way, obviously)
This is the one. Your code stays portable and requires no modification to the original BSD code. You can do this easily with the pkgconf(1) library named libbsd-overlay. Or, if you're feeling crafty, by adding the system-specific include directory with the bsd/ suffix to the list of system include paths. With gcc this could be:
-isystem ${includedir}/bsd
In addition, the LIBBSD_OVERLAY pre-processor variable needs to be defined. And the includes in this case should be the usual system ones, such as <unistd.h>. slay. β¨
π namespaced headers (the discouraged way)
this one is lowkey a red flag, bestie.
It's the other way to use the library, and it's discouraged β it's less portable because it makes using libbsd mandatory, and it will not work on BSD-based systems. It also requires modifying original BSD code. π You can do it with the pkgconf(1) library named libbsd, and the includes in this case should be namespaced with bsd/, such as <bsd/unistd.h>.
π§ the constructor situation
The package also provides a libbsd-ctor static library that can be used to inject automatic constructors into a program so that the setproctitle_init(3bsd) function gets invoked automatically at startup time. Not me finding that iconic. π You get it with the pkgconf(1) library named libbsd-ctor.
πΈ HEADERS πΈ
The following are the headers provided by libbsd, that extend the standard system headers. They can work in normal or overlay modes β for the former (normal mode) they need to be prefixed with bsd/.
<bitstring.h><err.h><getopt.h><grp.h><inttypes.h><libutil.h><md5.h><netinet/ip_icmp.h><nlist.h><pwd.h><readpassphrase.h><stdio.h><stdlib.h><string.h><stringlist.h><sys/bitstring.h><sys/cdefs.h><sys/endian.h><sys/param.h><sys/poll.h><sys/queue.h><sys/time.h><sys/tree.h><timeconv.h><unistd.h><vis.h><wchar.h>
And then there's this one, a libbsd specific convenience header that includes some of the extended headers. Heads up though: it only works in non-overlay mode. π«Ά
<bsd/bsd.h>
π¦ VARIANTS π¦
Some functions have different prototypes depending on the BSD where they originated from, and these various implementations provided are selectable at build-time. Here's the list of functions that provide multiple implementations:
strnvis(3bsd),strnunvis(3bsd)NetBSD addedstrnvis(3bsd)andstrnunvis(3bsd)but unfortunately made it incompatible with the existing one in OpenBSD and Freedesktop's libbsd (the former having existed for over ten years). π Despite this incompatibility being reported during development (seehttp://gnats.netbsd.org/44977) they still shipped it. Even more unfortunately, FreeBSD and later macOS picked up this incompatible implementation.
So: both implementations are provided, and for now it defaults to the historical one to avoid breakage β but we will switch to the NetBSD one in a later release, which is internally consistent with the other vis(3bsd) functions and is now more widespread. Define LIBBSD_NETBSD_VIS to switch to the NetBSD one now. Define LIBBSD_OPENBSD_VIS to keep using the OpenBSD one. π
π DEPRECATED π
Some functions have been deprecated β they will emit warnings at compile time and possibly while being linked at run-time. Why? Could be because the functions are not portable at all to other systems (which makes the package not buildable there), or not portable in a correct or non-buggy way, or because there are better, more portable replacements now. Periodt.
This is the list of currently deprecated macros and functions:
fgetln(3bsd)Unportable, requires assistance from the stdio layer. An implementation has to choose between leaking buffers or being reentrant for a limited amount of streams (this implementation chose the latter with a limit of 32). Usegetline(3)instead, which is available in many systems and required by IEEE Std 1003.1-2008 (βPOSIX.1β).fgetwln(3bsd)Unportable, requires assistance from the stdio layer. An implementation has to choose between leaking buffers or being reentrant for a limited amount of streams (this implementation chose the latter with a limit of 32). Usefgetwc(3)instead, which is available in many systems and required by ISO/IEC 9899:1999 (βISO C99β) and IEEE Std 1003.1-2001 (βPOSIX.1β).funopen(3bsd)Unportable, requires assistance from the stdio layer or some hook framework. On glibc- and musl-based systems thefopencookie(3)function can be used. Otherwise the code needs to be prepared for neither of these functions being available. π¬
π SUPERSEDED π
Some functions have been superseded by implementations in other system libraries, and might disappear on the next SONAME bump β assuming those other implementations have widespread deployment, or the implementations are present in all major libc for example. So likeβ¦ enjoy them while they're here. π
MD5Init(3),MD5Update(3),MD5Pad(3),MD5Final(3),MD5Transform(3),MD5End(3),MD5File(3),MD5FileChunk(3),MD5Data(3)The set of MD5 digest functions are now proxies for the implementations provided by thelibmdcompanion library, so it is advised to switch to use that directly instead.explicit_bzero(3bsd)This function is provided by glibc 2.25, and musl 1.1.20.reallocarray(3bsd)This function is provided by glibc 2.26, and musl 1.2.2.closefrom(3bsd)This function is provided by glibc 2.34.arc4random(3bsd),arc4random_buf(3bsd),arc4random_uniform(3bsd)These functions are provided by glibc 2.36. Note that it does not provide thearc4random_stir(3bsd)andarc4random_addrandom(3bsd)functions. πstrlcpy(3bsd),strlcat(3bsd)These functions are provided by glibc 2.38, and musl 0.5.0.
π SEE ALSO π
arc4random(3bsd), bitstring(3bsd), byteorder(3bsd), closefrom(3bsd), errc(3bsd), expand_number(3bsd), explicit_bzero(3bsd), fgetln(3bsd), fgetwln(3bsd), flopen(3bsd), fmtcheck(3bsd), fparseln(3bsd), fpurge(3bsd), funopen(3bsd), getbsize(3bsd), getpeereid(3bsd), getprogname(3bsd), heapsort(3bsd), humanize_number(3bsd), md5(3bsd), nlist(3bsd), pidfile(3bsd), pwcache(3bsd), queue(3bsd), radixsort(3bsd), readpassphrase(3bsd), reallocarray(3bsd), reallocf(3bsd), setmode(3bsd), setproctitle(3bsd), stringlist(3bsd), strlcpy(3bsd), strmode(3bsd), strnstr(3bsd), strtoi(3bsd), strtonum(3bsd), strtou(3bsd), timeradd(3bsd), timeval(3bsd), tree(3bsd), unvis(3bsd), vis(3bsd), wcslcpy(3bsd).
π HISTORY π
The libbsd project started in the Debian GNU/kFreeBSD port as a way to ease porting code from FreeBSD to the GNU-based system. Pretty early on it was generalized and a project created on FreeDesktop.org for other distributions and projects to use. π
It is now distributed as part of most non-BSD distributions. Main character energy, honestly. π
and that's the whole page bestie π«Ά this one's from the Debian crew, dated January 8, 2024, and it's libbsd(7) β go forth and port things, you absolute icon πβ¨
π¦ where this page comes from
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- nixpkgs unstable: libbsd 0.12.2
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright Β© 2017-2024 Guillem Jover <guillem@hadrons.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms