Compare commits

...

201 Commits
0.11.0 ... main

Author SHA1 Message Date
Guillem Jover
22fddb16f8 Mark tau and sigma Chacha variables as nonstring
These contain a sequence of bytes, and are not a NUL-terminated string.
Help the compiler understand the intent of the code.
2025-07-26 12:41:23 +02:00
Guillem Jover
abe3c8c332 include: Switch from __unused to new LIBBSD_UNUSED
Because we do not define __unused, as on GNU systems Linux and glibc
providing conflicting symbols, declarations for imported functions that
use that attribute cause build failures when used.

Ref: https://bugs.debian.org/1083196
Fixes: #34
2025-07-26 12:26:01 +02:00
Guillem Jover
1d711a2ced include: Refactor «unused» attribute into new LIBBSD_UNUSED macro
This make it possible to declare function arguments in headers as
potentially unused, so that they do not emit warnings. We will use this
instead of the BSD __unused, which we cannot currently enable on GNU
systems due to Linux and glibc having conflicting symbols.
2025-07-26 12:25:40 +02:00
Alan Coopersmith
aff40cae08 doc: Update anongit & cgit URLs to use gitlab instead
Closes: !30
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2024-11-02 23:22:22 +01:00
Guillem Jover
04a24db27a Release libbsd 0.12.2 2024-03-18 03:17:06 +01:00
Sam James
b62f36f305 test: nlist: make resilient against -Wl,--gc-sections
With linker sections GC enabled, we get a test failure in `nlist.c`:
```
nlist: nlist.c:72: main: Assertion `rc == 0' failed.
```

This turns out to be because several sections used by the test can be discarded:
```
ld: removing unused section '.text.func_pub' in file 'nlist.o'
ld: removing unused section '.bss.data_pub_uninit' in file 'nlist.o'
ld: removing unused section '.data.data_pub_init' in file 'nlist.o'
```

Reproduced with `CFLAGS="-Og -fdata-sections -ffunction-sections"` and
`LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--gc-sections -Wl,-z,start-stop-gc"`.

Additionally, `LDFLAGS="... -Wl,--print-gc-sections"` can help with diagnosing
which sections get removed.

We already mark these symbols as `used`, but we need `retain` [0] for them
to survive linker GC too.

[0] https://releases.llvm.org/18.1.0/tools/lld/docs/ELF/start-stop-gc.html#annotate-c-identifier-name-sections

Closes: !29
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2024-03-14 09:33:34 +01:00
Guillem Jover
2a3d521318 build: Do not rely on internal autoconf ac_cv_sys_file_offset_bits variable
This is an internal implementation detail from AC_SYS_LARGEFILE, which
happened to change with autoconf 2.72. Instead compute our own size
for off_t.

Closes: #28
2024-03-14 02:00:36 +01:00
Guillem Jover
149aafe198 build: Use -eq instead of = as test operators for arithmetic tests 2024-03-14 01:59:19 +01:00
Guillem Jover
435825784f build: Fix typo in ABI selection description 2024-03-13 00:56:59 +01:00
Guillem Jover
31e9396bf2 build: Only install time struct conversion macros where missing
These macros are available in several systems, and we should not install
the man pages for them, otherwise we might end up shadowing the system
man pages if present.
2024-03-13 00:56:07 +01:00
Guillem Jover
3b8416acab man: Add links for errc module functions
Reported-by: Alejandro Colomar <alx@kernel.org>
2024-03-11 03:40:39 +01:00
Guillem Jover
94d0fb3c53 man: Do not install timeval(3bsd) nor timespec(3bsd)
These are system types, which we should not be documenting. Rewrite the
man pages around the TIMEVAL_TO_TIMESPEC and TIMESPEC_TO_TIMEVAL macros.
2024-03-11 03:40:26 +01:00
Duncan Overbruck
e848f5aa29 strtonum: Do not abort on EINVAL from strtoi()
With musl libc strtoimax(".", ...) returns EINVAL.

Closes: !28
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2024-03-06 02:22:11 +01:00
Duncan Overbruck
ee63bca525 closefrom: Ignore files starting with dot when scanning /proc/self/fd
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2024-03-06 02:22:11 +01:00
Guillem Jover
9cbd935d92 build: Match also gnu* for GNU/Hurd on the glibc ABI selection
The GNU/Hurd system does not specify a kernel part, so we need to match
on both *-gnu* and gnu* (to avoid overmatching with just *gnu*).
2024-03-06 02:22:04 +01:00
Guillem Jover
70f49a7028 Release libbsd 0.12.1 2024-02-28 04:50:34 +01:00
Guillem Jover
b6ccd03201 build: Add a comment to libbsd-overlay.pc about -isystem usage
Add the reasoning for why we use -isystem instead of -I, to avoid
confusing unsuspecting readers of the file.

Prompted-by: Alejandro Colomar <alx@kernel.org>
2024-02-28 04:44:02 +01:00
Guillem Jover
08188f3c13 build: Install err.h if either of the err or errc modules are built
Both modules expose their functions in err.h, so we need to install it
whenever any of them is being built.

Reported-by: Alejandro Colomar <alx@kernel.org>
2024-02-28 04:37:44 +01:00
Guillem Jover
644062da40 Release libbsd 0.12.0 2024-02-27 05:52:53 +01:00
Guillem Jover
32d18dcf47 Add explicit time32 and time64 support
Handle the three potential system scenarios:

 - system time_t is time64
 - system time_t is time32 and supports time64
 - system time_t is time32 and does not support time64

Add the explicit time32 and time64 functions when necessary and map
them accordingly for each of these cases.
2024-02-27 05:47:50 +01:00
Guillem Jover
605614d642 build: Add support for AIX 2024-02-25 04:19:24 +01:00
Guillem Jover
ec7f5ee984 Add vasprintf() and asprintf() functions missing on AIX
These functions are used by code in the library, even though these
functions started as GNU extensions, they are present in all BSDs,
so we expose them as part of our interface on AIX.
2024-02-25 04:18:51 +01:00
Guillem Jover
5392f0c1a4 build: Add support for Solaris 2024-02-25 04:18:01 +01:00
Guillem Jover
be4aced4c5 build: Make almost all exposed interfaces use the new ABI selection
Explicitly select what to include as part of the target ABI, instead of
letting autoconfiguration potentially break ABI if the system grows
functionality provided by the library.

Make almost all the library selectable per target. Do not install manual
pages for interfaces not included in the library. Control inclusion of
symbols in map file via pre-processor macros, and move the comments
describing the ABI selection to configure.ac.

For now the header files are included as is and filtered through
pre-processor conditionals. Eventually they might get switched to be
autogenerated at build time.
2024-02-25 03:50:40 +01:00
Guillem Jover
27503a1066 build: Rework ABI selection logic
Add a new LIBBSD_SELECT_ABI m4 macro, and use it to setup the ABI
interfaces to expose and the various variables and conditionals
to be used by the build system.

Switch to set the initial values to unknown and then set every one
of the selections explicitly by supported target.

Update comments for rationale for things to DROP, or for why some
selections are enabled.
2024-02-25 03:49:15 +01:00
Guillem Jover
99739877be build: Rename ABI selection variables from need_ to abi_
This should make the purpose of these variables more clear.
2024-02-25 03:47:54 +01:00
Guillem Jover
0bdbc0787e build: Revert accidental testing aid change
This was accidentally included in the commit, but should have only been
used during development.

Fixes: commit de124dcafac678351366b0572938398ea7ae93e4
Changelog: silent
2024-02-21 02:29:15 +01:00
Guillem Jover
de124dcafa build: Make digest function checks conditional on their use
The digest function checks where unconditionally requiring the functions
to exist or they would error out. But these functions are not required
on all systems, they depend on the ABI to be exposed.
2024-02-21 02:25:24 +01:00
Guillem Jover
1d28729514 build: Move ABI selection at the top of configure.ac
Merge the existing host_os block for the OS detection with the ABI
selection one, as these are related. This way we will be able to make
some of the latter checks conditional on the selected ABI.
2024-02-21 02:25:21 +01:00
Guillem Jover
a81d0b7113 build: Sort variables and their contents in automake files
This should make it easier to add new entries, and find them afterwards.
2024-02-21 02:09:48 +01:00
Guillem Jover
c8e5338a7c build: Rename LIBBSD_ABI to SOVERSION
This matches the semantics of the variable, and makes it independent of
the project, just as the package variables.
2024-02-21 02:09:48 +01:00
Guillem Jover
b6d6da4cd4 build: Refactor function checks into a new libbsd-funcs.m4 file
These are complex enough to clutter the main configure.ac. Move them
into their own file.
2024-02-21 02:09:48 +01:00
Guillem Jover
f8cb9d8b28 build: Remove space before shell redirection
This makes it more clear what the redirection applies to.
2024-02-21 02:09:17 +01:00
Guillem Jover
154624ab45 build: Add support for silent rules for the libbsd.map generation
The new rule was introduced w/o silent rule support.

Fixes: commit 19e06407eb365a2bedc9bdd29a83c1e1803e3f92
2024-02-21 02:07:38 +01:00
Guillem Jover
9fab225f26 Split errc family of functions from err ones
On most systems the err family of functions is already present, but are
missing the errc family of functions, which are also present on some
other systems. Splitting them into separate files will make it easer to
conditionally include one or the other.
2024-02-18 14:02:32 +01:00
Guillem Jover
461f10ac57 Sync strtoi()/strtou() implementations from NetBSD
These contain the fixes to the error handling logic.

In NetBSD the manual page for strtou.3 is generated from the strtoi.3
manual page applying some substitutions, the problem is that the
cross-references are then lost. We will still keep them separate.

Reported-by: Alejandro Colomar <alx@kernel.org>
2024-02-18 13:57:04 +01:00
Guillem Jover
f050160976 build: Swap symbol and alias arguments order in macros creating aliases
The current order is rather confusing, pass the real symbol first
and the alias we want to create next.
2024-02-18 13:57:03 +01:00
Guillem Jover
19e06407eb build: Generate the map file from the configured ABI
Some linkers require the map file definitions to contain only symbols
that are present on the linked object, either in the map file or in the
sym file we generate from the map file.

This is preparatory work to be able to conditionally include symbols
in the man and sym files depending on the ABI definitions.
2024-02-18 13:54:13 +01:00
Guillem Jover
10920c3084 build: Make name_from_id man pages conditional instead of id_from_name
The code is only making the name_from_id function conditional, and
assumes id_from_name are always to be included, so we need to match
the logic for the man page inclusion.
2024-02-17 05:00:58 +01:00
Guillem Jover
97b0fe84b8 man: Mention that funopen() can be made available on musl
As musl got fopencookie() implemented in 1.1.19, the funopen() function
can also be provided there. Note this in the documentation.
2024-01-09 00:00:06 +01:00
Guillem Jover
e87ae3be19 man: Fix manual page references
When referring to another manual page and their section number, we need
to use Xr instead of Fn, otherwise the section number is interpreted as
a function argument. For functions provided by libbsd itself we should
be using the 3bsd section instead of 3.
2024-01-08 23:47:26 +01:00
Guillem Jover
9275d134e5 man: Add closefrom(), strlcpy() and strlcat() as superseded functions
These were missing from the list of functions provided by some libc
implementation.
2024-01-08 23:39:19 +01:00
Guillem Jover
304a1f831c doc: Use macOS to refer to the operating system
This is the correct spelling, instead of capitalizing it.
2024-01-08 23:37:43 +01:00
Guillem Jover
edddd806df Release libbsd 0.11.8 2024-01-08 02:16:12 +01:00
Guillem Jover
dd0bdb58e0 test: Close all descriptors before initializing them for closefrom()
On macOS, closefrom() only sets the close-on-exec flag, so we cannot
check whether all file descriptors were closed, which means that if
on entry our file descriptor table was filled after the 4th file
descriptor, then we might fail the assertions for the flags for odd
file descriptors which we expect to be closed.

This can easily happen when running the test suite in parallel mode
with «make -j8 check» for example.

Closes: #23
2024-01-08 01:58:54 +01:00
Guillem Jover
0813f3786f build: Check out-of-tree builds in CI
Make sure the out-of-tree builds do not regress.
2024-01-07 20:21:08 +01:00
Guillem Jover
df116b5597 Adjust strlcpy() and strlcat() per glibc adoption
These functions were added in glibc 2.38, in anticipation of POSIX
adopting them too.

Closes: #26
2024-01-07 17:55:40 +01:00
Guillem Jover
ecb44e1655 Do not add a pointer to the NULL constant
Warned-by: cppcheck (nullPointerArithmetic)
2024-01-07 16:45:46 +01:00
Guillem Jover
459b7f7d24 Do not confuse code analyzers with out-of-bounds array access look alike
The code is only getting the address, but we might be performing an
addressing that is out-of-bounds. Avoid it and use the address form
instead.

Warned-by: cppcheck (objectIndex)
2024-01-07 16:45:42 +01:00
Guillem Jover
a44f885cd4 test: Fix short-lived memory leak
Warned-by: cppcheck
2024-01-07 16:45:39 +01:00
Guillem Jover
3f5ca0aae4 build: Add a coverage regex to the CI job
This is needed so that gitlab can know where to extract the coverage
percentage from in the output, to be able to track and report it.
2023-10-28 00:47:26 +02:00
Guillem Jover
9d3e59a01d man: Use VARIANTS instead of ALTERNATIVES in libbsd(7)
Using alternatives seems confusing in this context.
2023-10-10 01:53:32 +02:00
Guillem Jover
f02562d64a man: Markup function references with Xr instead of Fn
These references had man page sections in them, so using Fn meant that
these appeared as function arguments.
2023-10-10 01:52:37 +02:00
Guillem Jover
b7367c9ced build: Add missing dash to macro title bar 2023-09-05 03:02:44 +02:00
Guillem Jover
6777eb62b8 pwcache: Do not declare uidtb and gidtb when not used
When the system provides implementations for user_from_uid() or
group_from_gid() we are not using these variables, so better not declare
them.

Fixes: commit 21d12b02112097f0c195dceb1892c95b7b957b36
2023-09-05 03:02:20 +02:00
Guillem Jover
d4e0cdc916 fgetln: Include <stdio.h> after <sys/*>
The <sys/*> headers tend to define things that might be used by other
headers, so while they should be self-contained, it is better to simply
include them first.
2023-09-05 03:02:20 +02:00
Guillem Jover
f41d6c12aa build: Refactor GNU .init_array support check into a new m4 function 2023-09-05 03:02:12 +02:00
Guillem Jover
30b48ed9ea build: Refactor linker script detection into a new m4 function 2023-07-29 20:11:53 +02:00
Guillem Jover
d0d8d0197f build: Do not provide prototypes for arc4random() on Solaris
These functions are provided by the system.
2023-07-29 20:11:53 +02:00
Guillem Jover
cf61ebb845 build: Do not build the progname module if it is not needed
This was made conditional, but the code part was accidentally left
untouched due to having ported it locally to use __progname, which
caused build failures on the stock repo.

Fixes: commit 046621d7967e7a0f08ae988bcf7e4cd1b6cf204c
2023-07-29 20:11:53 +02:00
Guillem Jover
73b25a8f87 build: Sort entries alphabetically 2023-04-29 03:53:52 +02:00
Guillem Jover
5434ba169b build: Conditionalize wcslcpy() and wcslcat() functions on macOS
These functions are provided by the system libc.
2023-04-29 03:52:49 +02:00
Guillem Jover
dc1bd1a2cb build: Conditionalize only id-from-name functions not the entire pwcache
On macOS the name-from-id functions are present, but not the
id-from-name ones, so we want to provide those instead of suppressing
the entire file.
2023-04-29 03:52:42 +02:00
Guillem Jover
edc746ea7b build: Conditionalize getprogname()/setprogname on macOS
These functions are provided by the system libc, so there is no need for
us to provide them.
2023-04-22 22:47:33 +02:00
Guillem Jover
8f998d1d20 progname: Include <procinfo.h> if available
We need this header on AIX. Missed transplanting the code from the AIX
porting system.

Fixes: commit 9fa06763a1afe0946a3a20e5bbdba72885cbade5
2023-04-22 22:42:30 +02:00
Guillem Jover
d08163b4fe build: Check whether we need libperfstat on AIX
The getentropy() implementation makes use of this library on AIX.
2023-04-22 19:30:15 +02:00
Guillem Jover
1186cf8822 build: Annotate droppable functions for musl on next SOVERSION bump
These are already provided by the musl libc, and can thus be dropped on
the next SOVERSION bump.
2023-04-22 19:30:15 +02:00
Guillem Jover
6385ccc977 build: Conditionalize bsd_getopt() on macOS
The system library provides a getopt() with BSD semantics.
2023-04-22 19:30:05 +02:00
Guillem Jover
c12068179c Move the version script comments before the symbols
When generating the .sym export file from the .map file, we are not
stripping these comments that are part of the same line as the symbol,
which causes ld(1) implementations to error out. Moving them before
the symbols avoids the need to strip them, as we are only keeping
actual symbol lines.
2023-04-21 05:20:27 +02:00
Guillem Jover
9fa06763a1 Port getprogname() to AIX
Get the program name from the COMM field from the proc filesystem.

We could use instead the information from the psinfo binary file under
/proc, but that seems to have a shorter string limit.
2023-04-21 05:20:27 +02:00
Guillem Jover
92337b15a2 Make getprogname() porting mandatory
Although the function is documented as possibly returning NULL if it
cannot find a known source of information, we should still at least
attempt to port it to any supported system, and otherwise explicitly
mark it as not implementable for such systems if that was to be the
case.
2023-04-21 05:20:27 +02:00
Guillem Jover
90b7f3aeba test: Do not use /dev/null as compiler output file
Some ld(1) implementations, such as the one on AIX, do not support using
/dev/null as the output filename for the compiled object.

Use an actual filename that we will then clean up.
2023-04-18 01:09:05 +02:00
Guillem Jover
426bf45278 build: Add generated *.sym files to .gitignore 2023-04-18 01:09:05 +02:00
Guillem Jover
21d12b0211 build: On macOS do not build functions provided by the system
We have never built before on macOS, so we can exclude all the functions
that are currently provided in the system.

Closes: #1
Closes: !3
2023-04-17 04:12:42 +02:00
Guillem Jover
bc65806ce2 build: Select whether to include funopen() in the build system
This makes sure we include it when expected, alongside the man pages,
and the test cases, and do not accidentally break the ABI if the system
starts providing such interface.
2023-04-17 04:12:42 +02:00
Guillem Jover
8b7a4d9d3b build: Move Windows OS detection to the OS features section
This was placed here to make use of the same AS_CASE, but it does not
really fit with the section. Move it to the more appropriate place, and
detangle the AS_CASE.
2023-04-17 04:12:42 +02:00
Guillem Jover
ccbfd1c241 build: Remove __MUSL__ definition from configure
We stopped relying on this macro when we turned the funopen() cpp error
into a warning in commit e50896286cc5718898194edb73fa7262ad9a22db.
2023-04-17 04:12:42 +02:00
Guillem Jover
e0976d7e90 build: Add a new libbsd_strong_alias() macro and switch users to it
We had several cases of code needing a strong alias, so we switch those
to use the new macro. This covers systems that support the alias
attribute and others such as macOS where we need to use assembler
directives to add the alias as the attribute is not supported.
2023-04-17 04:12:42 +02:00
Guillem Jover
49c7dd1ca4 build: Only emit link warnings for ELF objects 2023-04-17 04:12:42 +02:00
Guillem Jover
8622767a8a build: Use an export symbols file if there is no version script support
We generate the symbol list from the version script to avoid repeating
ourselves and potentially getting the lists out-of-sync.
2023-04-17 04:12:42 +02:00
Guillem Jover
8f61036467 build: Add -no-undefined libtool flag
We have no need for undefined symbols, so we can let the shared
library build even on systems without support for undefined symbols.
2023-04-17 04:12:42 +02:00
Guillem Jover
ae7942ba6d build: Do not override the default DEPENDENCIES for libbsd
Extend it instead via EXTRA_*_DEPENDENCIES, to make sure that we
preserve the builtin library dependencies generated from LIBADD.
2023-04-17 04:12:42 +02:00
Guillem Jover
a5faf17090 Only use <stdio_ext.h> if present 2023-04-17 04:12:42 +02:00
Guillem Jover
06e8a1b29b Define _NSIG if it is not defined by the system
At least on macOS this macro is not defined.
2023-04-17 04:12:42 +02:00
Guillem Jover
44824aca3c Declare environ if the system does not do so
The environ variable is supposed to be defined by the code using it, but
on glibc-based systems it will get defined if we request it, by including
<unistd.h> and defining _GNU_SOURCE.
2023-04-17 04:12:42 +02:00
Guillem Jover
1fb6c3f4ce Use lockf() when flock() is not available
On Solaris flock() is not available, and we should use instead lockf()
or fcntl().
2023-04-17 04:12:42 +02:00
Guillem Jover
fe16f3863e test: Use open_memstream() only if available
On Solaris this function is not yet available.
2023-04-17 04:12:42 +02:00
Guillem Jover
7c652a94ea test: Do not hardcode root:root user and group names
On some systems the root group is named wheel, and there is no root
group.
2023-04-17 04:12:42 +02:00
Guillem Jover
ed2eb31da9 test: Fix closefrom() test on macOS
On macOS we do not close the file descriptors, and instead mark them all
as close-on-exec. So checking whether they are not valid does not work.
2023-04-17 04:12:42 +02:00
Guillem Jover
0f8bcdfd92 test: Fix closefrom() test to handle open file descriptor limits
If the system has configured a lower limit (either soft or hard) on the
number of open file descriptors, the test will fail. Make sure to check
whether we have exceeded that limit and adapt the max number of file
descriptors appropriately.
2023-04-17 04:12:42 +02:00
Guillem Jover
07192b31e3 test: Disable blank_stack_side_effects() on non-Hurd systems
This code was added to cope with Hurd specific behavior, but it is
causing flakiness on containers on some Linux systems. Only enable
it where it is currently needed to try to get stability back on CI
systems.

Closes: #14
2023-04-17 04:12:32 +02:00
Guillem Jover
ca3db5e134 build: Do not enable ASAN for musl CI pipelines
musl-libc does not currently support ASAN, so enabling it makes
configure fail to detect libmd as it cannot link to it anymore.
2023-03-29 09:35:27 +02:00
Guillem Jover
ff46386fcb man: Add HISTORY section to arc4random(3bsd) 2023-03-29 02:49:28 +02:00
Guillem Jover
4c6da577a1 man: Switch arc4random(3bsd) man page from OpenBSD to NetBSD
This gets rid of the last BSD-4-clause licensed file in the project.

The man page will probably need to be adapted to the current
implementation, but that can be done piecemeal afterwards.

Closes: #7
2023-03-29 02:48:53 +02:00
Guillem Jover
830dd88a98 doc: Remove written-by attribution
While attribution is important, it is not relevant when tracking the
copyright holders for the work. And in any case it still stays in the
relevant source file.
2023-03-29 02:48:20 +02:00
Guillem Jover
257800a03c build: Add support for sanitizer compiler flags 2023-03-29 02:48:20 +02:00
Guillem Jover
536a7d42fd test: Exempt blank_stack_side_effects() from sanitizer checks
This will mean we cannot use sanitizer support on the Hurd, for which
this function was added to fix the test. But the sanitizer suppression
function attribute is not having any effect, so this is better than
nothing.
2023-03-29 02:47:18 +02:00
Guillem Jover
7ed5de0158 test: Import explicit_bzero() sanitizer support changes from OpenBSD 2023-03-29 02:38:26 +02:00
Guillem Jover
05a802a2c1 test: Fix memory leaks in fpurge test
Warned-by: gcc -fsanitize=leak
2023-03-21 04:13:32 +01:00
Guillem Jover
5962e03c2d man: Fix BSD and glibc versions
The versions used in the BSD macros are unknown, so they emit warnings,
extend or reduce them to use the two digit form. Correct the glibc
version when closefrom(3) got introduced.
2023-03-03 01:35:31 +01:00
Guillem Jover
59a21c7fb8 man: Update STANDARDS and HISTORY sections
Include BSD versions when functions were introduced. Add mention
whether these are BSD extensions.
2023-02-20 00:29:01 +01:00
Callum Farmer
7b4ebd6521 include: Adjust closefrom() per glibc adoption
Added in glibc 2.34
https://sourceware.org/git/?p=glibc.git;a=commit;h=607449506f197cc9514408908f41f22537a47a8c

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2023-02-17 22:29:26 +01:00
Guillem Jover
0dfbe7699e build: Switch to debian:latest Docker image
This should always point to the latest Debian stable images.
2023-02-17 22:29:26 +01:00
Guillem Jover
dec783dce5 build: Fix version script linker support detection
When the linker uses --no-undefined-version either specified by the user
or as the default behavior (such as with newer clang >= 16 releases),
a missing symbol definition will cause a linker error if that symbol is
listed in the version script.
2023-02-13 00:42:02 +01:00
Guillem Jover
fe21244b05 include: Use __has_builtin to detect __builtin_offsetof support
The __has_builtin operator is more specific and is supported by GCC
and Clang, while __is_identifier() is less specific and only supported
by Clang, so we should prefer the former whenever it is available, and
only fallback to use the latter when the former is missing and the
latter.
2023-02-13 00:39:54 +01:00
Khem Raj
ec88b7bbbc funopen: Replace off64_t with off_t in funopen_seek()
AC_SYS_LARGEFILE in configure.ac is setting needed defines to make
64bit off_t on relevant platforms.

Fixes build on musl:

| src/funopen.c:68:28: error: unknown type name 'off64_t'; did you mean 'off_t'?
| funopen_seek(void *cookie, off64_t *offset, int whence)
|                            ^~~~~~~
|                            off_t

Closes: !24
Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-12-17 18:22:26 +01:00
Guilherme Janczak
23377191e8 man: Prune unneeded <sys/types.h> include in setproctitle(3)
FreeBSD first declared the function in unistd.h in 2000:
https://cgit.freebsd.org/src/commit/include/unistd.h?id=9feac5c21886
No other #include line was needed to use the function, but the man page
unnecessarily instructed users to include sys/types.h until 2022-11-29:
https://cgit.freebsd.org/src/commit/?id=5b6f0a5012e9bc37dcb32b57b41e2443a46da620

libbsd first declared the function in unistd.h in 2010 with commit
3fed78e5b08f78256e533788b4bcd6502b0949d7 and inherited the bug from
FreeBSD's man page.

No need to do anything for backwards compat.

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-12-13 03:49:29 +01:00
Florian Weimer
5dea9da38d build: Improve C99 compatibility of __progname configure check
The check uses printf, so it needs to include <stdio.h> for
compilers which do not support implicit function declarations.
(They were removed from C99.)

Closes: !23
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-12-13 03:49:29 +01:00
Guillem Jover
b9bf42ddc3 build: Enable -Wall for automake
This will make sure to warn about bogus or deprecated constructs.
2022-12-13 03:49:03 +01:00
Guillem Jover
e57c07875c build: Add missing AM_PROG_AR macro call to configure.ac
Warned-by: autoreconf
2022-12-13 03:48:51 +01:00
Guillem Jover
80f1927dcd build: Fix configure.ac indentation
Use the same style as the newly added code, which should make the code
more readable, and produce more conforming C output.
2022-12-13 03:48:31 +01:00
Guillem Jover
b7a8bc22c9 build: Require automake 1.11
This is a rather old release (from 2009), that provides AM_SILENT_RULES.
2022-12-13 03:10:28 +01:00
Guillem Jover
e50896286c build: Do not require funopen() to be ported
This function cannot be easily and (more importantly) correctly ported
without cooperation from the libc stdio layer. We already document that
users should be prepared to have the function not available on some
platforms and that they should ideally switch their code to other
more portable and better interfaces.

Instead of making the build fail, and requiring porters to add
exceptions for something that most probably cannot be ported correctly
anyway, simply print a warning and let it build. This will not be a
regression because on those systems libbsd would have never been built
before.

Prompted-by: Jens Finkhaeuser <jens@finkhaeuser.de>
2022-11-24 23:23:33 +01:00
Guillem Jover
00b538ffa3 build: Terminate lists in variables with «# EOL»
This means we can add a trailing «\» to every element, so that they
can be removed without requiring modification of other lines, and can
be easily sorted.

Replace the old usage of $(nil) which could possibly end up with junk
added if such variable is ever defined, in the environment.
2022-11-23 23:44:09 +01:00
Guillem Jover
5cfa39e5cd build: Use «yes» instead of «true» for AC_CHECK_FUNCS cache value
This autoconf macro sets the ac_cv_func_ cached variable to «yes» not
«true» so we were checking for an impossible condition.
2022-11-23 23:42:49 +01:00
Guillem Jover
084911ce87 Release libbsd 0.11.7 2022-10-06 04:07:52 +02:00
Guillem Jover
3538d38fe7 man: Discourage using the library in non-overlay mode
This makes code using it non-portable, and requires the namespeced
headers from libbsd, instead of any generic system headers. It also
requires more code changes than the overlay mode.
2022-10-06 03:59:33 +02:00
Guillem Jover
03fccd1505 include: Adjust reallocarray() per glibc adoption
On glibc 2.29 reallocarray() was moved to _DEFAULT_SOURCE.

Closes: !20
Based-on-patch-by: Callum Farmer <gmbr3@opensuse.org>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-10-05 12:31:49 +02:00
Callum Farmer
6b6e686b23 include: Adjust arc4random() per glibc adoption
Some arc4random functions were added in glibc 2.36.

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-10-05 12:31:49 +02:00
Callum Farmer
da1f45ac71 include: explicit_bzero() requires _DEFAULT_SOURCE
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-10-05 12:15:36 +02:00
Guillem Jover
2f9eddc277 include: Simplify glibc version dependent macro handling
We test once whether __GLIBC__ is not defined, so we do not need to test
whether it is on the OR branch afterwards. We decouple the glibc version
restriction check from the _*_SOURCE variable, as that contains an
implicit opposite version check.
2022-10-05 12:12:34 +02:00
Guillem Jover
28298ac037 doc: Switch references from pkg-config to pkgconf
The former used to be the reference implementation, but it has been
stagnant to the point of not showing much signs of life. Switch to
the currently active and more complete implementation for references.
2022-10-05 01:36:28 +02:00
Guillem Jover
ef981f930b doc: Add missing empty line to separate README sections 2022-10-04 04:29:37 +02:00
Guillem Jover
6928d7895e doc: Refer to the main git repository as primary 2022-10-04 04:29:37 +02:00
Guillem Jover
d5865759f8 test: Fix explicit_bzero() test on the Hurd
On the Hurd a small read(3) might end up (indirectly) copying the data
on the stack, which we will end up finding even when we have cleared
the buffer.

To avoid these side effects, we add a new function, that we force not
to be inlined, so that we can reuse the same stack space, that will
blank any possible stack side effects. This should be portable
regardless of stack growing up or down.

Diagnosis-by: Samuel Thibault <sthibault@debian.org>
2022-10-04 04:29:37 +02:00
Guillem Jover
be327c6ebe fgetwln: Add comment about lack of getwline(3) for recommendation
Ideally we'd recommend getwline(3), but unfortunately even though it
was part of the ISO/IEC TR 24731-2:2010 draft, it did not make it into
C11 and is not widely implemented.
2022-10-04 04:29:07 +02:00
Guillem Jover
a14612d968 setmode: Dot not use saveset after free
While we are only doing a pointer difference and not dereferencing it,
it's easier and more correct to do the pointer difference before passing
it to reallocarray().

Warned-by: gcc
2022-08-23 23:51:05 +02:00
Guillem Jover
f4baceb82c man: Rewrite gerprogname(3bsd) from scratch
This gets rid of one of the last man pages with a BSD-4-clause license.
2022-08-03 11:39:31 +02:00
Guillem Jover
f35c545847 man: Lowercase man page title
While using fully uppercase man page titles has been the usual
convention for a very long time, it is rather ugly and something that
some other projects are switching away from.
2022-08-03 11:39:31 +02:00
Guillem Jover
b466b14afb man: Document that some arc4random(3) functions are now in glibc 2.36 2022-08-03 11:39:31 +02:00
Guillem Jover
1f6a48b209 Sync arc4random(3) implementation from OpenBSD
Closes: #12
2022-08-03 11:39:31 +02:00
Fabrice Fontaine
873639ebb5 Fix ELF support for big endian SH
Fix the following build failure with big endian SH (e.g. sh4aeb):

  ,---
  In file included from nlist.c:44:
  nlist.c: In function '__elf_is_okay__':
  local-elf.h:223:25: error: 'ELFDATA2LMSB' undeclared (first use in this function); did you mean 'ELFDATA2LSB'?
    223 | #define ELF_TARG_DATA   ELFDATA2LMSB
        |                         ^~~~~~~~~~~~
  `---

Fixes: http://autobuild.buildroot.org/results/2980fb79c208454195d77383f1ece9afbd7f981b
Closes: !19
Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-06-08 11:51:55 +02:00
Guillem Jover
c9c78fdfd0 man: Use -compact also for alternative functions in libbsd(7)
The other function lists use the compact mode, let's be uniform about
this.
2022-06-08 11:50:47 +02:00
Guillem Jover
5f21307570 getentropy: Fix function cast for getauxval()
Fix also the Hurd implementation, missed in the original fix.

Extends: commit 72a82ee262795c9162e712885381a111fcf988bc
Warned-by: gcc
2022-06-08 11:49:16 +02:00
Guillem Jover
84acf21568 Release libbsd 0.11.6 2022-03-30 23:09:35 +02:00
WANG Xuerui
15200ec7ac Add LoongArch support to nlist()
Closes: !13
Signed-off-by: WANG Xuerui <git@xen0n.name>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-03-30 23:03:56 +02:00
Guillem Jover
f7465b93ec man: Document when functions were added to musl in libbsd(7)
The explicit_bzero(3) and reallocarray(3) functions are also present on
musl libc, mention it in the superseded section.
2022-03-30 22:58:47 +02:00
Guilherme Janczak
e5ed5fdd62 Remove arc4random() OpenBSD support
The arc4random() support for OpenBSD does not build. The intention was
to include any portability code so that the library could be easily
ported to such systems, but it makes little sense to build it on OpenBSD
where most of the functionality will be already present, or the software
would have been ported anyway.

[guillem@hadrons.org: Reword commit message to add rationale. ]

Closes: !15
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-03-30 22:55:56 +02:00
Duncan Overbruck
abb1a6b1a7 build: Add alpine to CI for tests with musl libc
Closes: !18
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-01-29 15:03:08 +01:00
Guillem Jover
c9ff83687c test: Import explicit_bzero and strtonum test cases from OpenBSD
- Remove trailing spaces.
- Declare file-scope functions and variables static.
- Declare functions with a proper prototype.
- Do not mix declarations and code for C90 conformance.
- Do not compare size_t and ssize_t variables.
2022-01-29 14:54:24 +01:00
Guillem Jover
62e67bb49e build: Sort test programs in .gitignore 2022-01-29 14:52:39 +01:00
Duncan Overbruck
87b1307040 test: Add fpurge() test case using open_memstream(3) FILE
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-01-29 14:10:55 +01:00
Duncan Overbruck
ba3c46b30f fpurge: Make it work with non fd based FILEs
Streams opened with for example open_memstream(3) will have no associated
file descriptor, and fileno(3) will fail.

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-01-26 04:42:09 +01:00
Duncan Overbruck
091097e062 test: Remove static FILE declaration from fpurge test
This does not work with libcs that do not declare the structure
in a header file, like musl. And gets in the way of supporting
non-fd based streams.

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-01-26 04:39:42 +01:00
Guillem Jover
5f9608c775 Release libbsd 0.11.5 2022-01-25 01:48:29 +01:00
Guillem Jover
eef07993c9 Revert "build: Add missing proctitle unit test program"
This reverts commit beafad2657c7a57109c28f8bad9cb028c84c7dd5.

This test was already handled later on as part of the BUILD_LIBBSD_CTOR
conditional. Adding it to the unconditional set made the build fail when
the system does not have GNU .init_array section support.

Closes: #9
Analysis-by: Duncan Overbruck <mail@duncano.de>
2022-01-25 01:41:14 +01:00
Guillem Jover
c6d589bc71 Document how to build from git and from a tarball release
Closes: #8
2022-01-23 21:09:05 +01:00
Guillem Jover
2975d809a0 build: Check for objdump explicitly
We should not assume that something will implicitly check for this tool,
as we need it ourselves, and this is an internal implementation detail
of right now libtool.

Fixes: commit f11ab6722367f1cf62704ed3c827b9b68dcb5397
2022-01-23 20:52:26 +01:00
Sam James
f11ab67223 build: Respect $(OBJDUMP)
We already search for it in `./configure` so let's respect
the result of that search.

This helps with cross-compilation and any other cases
where one might want to choose a different toolchain.

Closes: !16
Bug: https://bugs.gentoo.org/831863
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2022-01-23 18:42:01 +01:00
Guillem Jover
54796231c7 Release libbsd 0.11.4 2022-01-22 22:41:51 +01:00
Guillem Jover
7aed0edf73 man: Add missing comma in .Dd for tree(3)
Reported-by: Thorsten Glaser <tg@mirbsd.de>
2021-12-02 00:35:40 +01:00
Guillem Jover
fafcc397ac man: Update tree(3) man page from OpenBSD
This man page has a BSD-3-clause-author license instead of the old
BSD-4-clause license.
2021-12-01 04:17:11 +01:00
Guillem Jover
48107fc8c4 build: Clarify link-time warnings
Mention the involved function instead of stating an implicit "this
function". Mention libbsd when proposing using an alternative libmd
to make the context clear.
2021-12-01 04:17:11 +01:00
Guillem Jover
e7cf8c5785 Switch md5 compatibility logic back to direct linking
When using the recent dlsym() based wrapper, we are not requiring any
symbol from libmd, as we resolve those dynamically at run-time. We were
ending up linking against libmd because in another part of the code we
require (depending on the architecture) the SHA512 functions for the
getentropy() local implementation. But that function might be provided
by the system libc on some systems, which means we end up not linking
against libmd at all.

To solve this we go back to the previous simpler solution of linking
directly, which had the main drawback of then making programs fail to
link when not specifying -lmd (on platforms that need it). And then
switch the .so link point from a symlink to a linker script, so that we
can inject the -lmd library as-needed. This is similar to what glibc is
doing.

Fixes: commit 31f034e3862debda8615a449b1c11c4d6920dcc7
2021-12-01 04:14:35 +01:00
Guillem Jover
25d35625eb build: Split libmd dependency due to MD5 functions from SHA requirements
To be able to rework the md5 deprecation logic, we need to detangle when
we depend on libmd due to requiring MD5 functions, which might be
otherwise provided by libc, or when we require SHA functions for the
internal getentropy() implementation.
2021-11-28 22:40:58 +01:00
Guillem Jover
500b3080a2 build: Add new libbsd_symver_weak() macro
We will use it for the reworked md5 deprecation logic.
2021-11-28 22:34:59 +01:00
Guillem Jover
1eba406021 test: Check that strdup() does not fail
Warned-by: gcc
2021-11-27 23:42:37 +01:00
Guillem Jover
8ad7570c20 getentropy: Add missing prototype for BSD variant
Warned-by: gcc
2021-11-27 23:42:37 +01:00
Guillem Jover
43a8270317 nlist: Remove condition which is always true
The nlist() function is limited to handle ELF binaries of the same class
as size as the size_t of the architecture built.

In addition the SIZE_T_MAX macro is BSD specific, and was falling back
to the wrong constant on most 64-bit non-BSD systems.

Warned-by: gcc
2021-11-27 23:36:34 +01:00
Guillem Jover
6a71b24b63 build: Append __ after __attribute and __typeof keywords
Be consistent with other usages in the code base.
2021-11-27 22:53:32 +01:00
Guillem Jover
7389fe8d24 build: Ignore backup files 2021-11-27 06:54:49 +01:00
Victor Westerhuis
2716dfd0b7 test: Explicitly mark symbols as used
Because some of the symbols are not otherwise referenced, GCC would like
to remove them.

Closes: !14
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-11-27 06:52:17 +01:00
Victor Westerhuis
54f8745657 build: Enable .init_array support when building with LTO
Because these symbols are not otherwise referenced, GCC would like
to remove them.

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-11-27 06:51:02 +01:00
Guillem Jover
428be9e030 man: Fix .Nx macro usage
Its arguments are used as version numbers, so we need continue the
content on the next line.
2021-11-27 05:06:14 +01:00
Alexander Miller
c7a5d780ae build: Allow building with -flto on gcc-10 and newer
Global asm statements (like .symver directives) do not work reliably
in gcc with link time optimization. Use the symver attribute introduced
with gcc-10 to set symbol versions instead, if available.

[guillem@hadrons.org:
 - Simplify by using __has_attribute fallback from <sys/cdefs.h>.
 - Coding style changes. ]

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-11-27 05:06:14 +01:00
Guillem Jover
1808d64b77 test: Fix pipe_feed() to allow checking fprintf format strings
Warned-by: gcc -W
2021-11-27 05:06:14 +01:00
Guillem Jover
beafad2657 build: Add missing proctitle unit test program 2021-11-27 05:06:14 +01:00
Guillem Jover
6145b56178 test: Do not pass NULL as the first funopen() argument
Warned-by: gcc -W
2021-11-27 05:06:14 +01:00
Guillem Jover
731b0a7739 build: Detect sed at configure time
Check whether sed is available and use the implementation matching the
requirements via the SED variable.
2021-11-27 05:06:14 +01:00
Guillem Jover
50b50a4330 vis: Add prototypes for strnvis() and strnunvis() variants
Warned-by: gcc
2021-11-27 05:06:14 +01:00
Guillem Jover
25e88f6479 test: Cast literal strings to (char *) on n_name assignment
The member is declared as n_name so we cannot directly assign a literal
string constant.

Warned-by: gcc
2021-11-27 05:06:14 +01:00
Guillem Jover
04a8fb2469 Add missing prototypes to functions
Warned-by: gcc
2021-11-27 05:06:14 +01:00
Guillem Jover
4f68a88f55 build: Add compiler warnings support
Detect as many warnings as possible during configure and enable them
if the user did not supply any, so that any such problem can be spotted
and fixed.
2021-11-27 05:06:14 +01:00
Guillem Jover
8f59221c4f nlist: Remove repeated shadowing variable declaration
Warned-by: gcc
2021-02-28 05:30:37 +01:00
Guillem Jover
72a82ee262 getentropy: Fix function cast for getauxval()
Warned-by: gcc
2021-02-28 05:30:37 +01:00
Guillem Jover
3c305f2873 test: Add proper prototypes for main() function
Warned-by: gcc
2021-02-28 05:30:31 +01:00
Guillem Jover
25278891d8 Mark local functions as static
Warned-by: gcc
2021-02-28 05:30:25 +01:00
Guillem Jover
e35d9141dc Add link-time warnings to MD5 wrapper functions
Let's get the word out that these functions are deprecated and should be
switched away from.
2021-02-28 05:28:57 +01:00
Guillem Jover
4feda87049 Require a semicolon for libbsd_link_warning() macro
Remove the semicolon in the macro definition to force adding one on the
call sites, to make the code look like an actual function.
2021-02-28 05:28:57 +01:00
Guillem Jover
d563a17430 man: Call the libbsd-ctor library by its name instead of bsd-ctor 2021-02-28 05:28:57 +01:00
Guillem Jover
785cf9d1e9 man: Fix pkg-config(1) references in libbsd(7) man page 2021-02-28 05:28:57 +01:00
Guillem Jover
15bd284b29 build: Add code coverage support in the GitLab CI 2021-02-28 05:28:49 +01:00
Guillem Jover
a9fc285988 build: Use apt-get instead of apt in the GitLab CI
The former is to be used programmatically, while the latter is intended
for interactive use.
2021-02-28 05:27:35 +01:00
Guillem Jover
c0d86a6412 build: Add a Libs.private field to overlay pkg-config file
We need to list all internal libraries there so that we can statically
link.

Fixes: commit 2374f409defb380d0c5c07f28b9c166ef8bdc742
2021-02-09 08:36:01 +01:00
Guillem Jover
1fb25b7dca Release libbsd 0.11.3 2021-02-09 06:23:38 +01:00
Guillem Jover
31f034e386 Switch libmd wrapper to use dlsym()
Switch from the previous versioned symbol implementation which required
users to also link against the message digest provider explicitly, or
they would fail to find the symbols, to an implementation that loads
the symbols from the linked library providing the functions using
dlsym(), thus preserving backwards compatibility.
2021-02-09 06:23:38 +01:00
Guillem Jover
2374f409de build: Add a Libs.private field to pkg-config file
We need to list all internal libraries there so that we can statically
link.
2021-02-09 06:23:38 +01:00
Guillem Jover
a4e0db2b97 build: Use a single variable to track libraries to link against
Using various variables means we have to keep these in sync in various
places. Just use a single variable that we can use anywhere where this
is needed.
2021-02-09 06:23:38 +01:00
Guillem Jover
43d34c9d3b build: Fix message digest library checks
They were not failing when not finding the SHA-2 functions and
were hardcoding -lmd regardless of what library had been found.
2021-02-09 06:23:38 +01:00
Guillem Jover
1c3ff61699 Use uintptr_t and size_t instead of __-prefixed types in <sys/cdefs.h>
The __-prefixed types cannot be assumed to be defined. Use the standard
types instead.

Closes: #6
2021-02-09 06:23:27 +01:00
Guillem Jover
edea268ce9 Release libbsd 0.11.2 2021-02-08 04:02:46 +01:00
Guillem Jover
e832b7687e closefrom: Use close_range() on Linux when available
Closes: !11
Based-on-patch-by: cptpcrd <cptpcrd.git@gmail.com>
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-02-08 04:02:46 +01:00
cptpcrd
c4fca5bb4f closefrom: Handle lowfd < 0 properly
More important if close_range() is going to be used, since casting
negative values to 'unsigned int' might hide the errors.

[guillem@hadrons.org: Minor coding style fix. ]

Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-02-08 04:02:46 +01:00
Guillem Jover
a1f79978e8 closefrom: Import some changes from sudo
Take most of the changes done in sudo, but preserve the existing local
changes and refactoring.

In addition, refactor pstat implementation into closefrom_pstat(), so
that the code is easier to read, and requires no conditional
declarations.
2021-02-08 04:02:46 +01:00
Faidon Liambotis
4676026286 Update <sys/queue.h> from FreeBSD
This brings <sys/queue.h> to the most up-to-date version from FreeBSD,
incorporating 18 commits from the past 5 years (2015-02-24 - 2021-01-25):

  $ git log --oneline 9090a24aed70..8d55837dc133 sys/sys/queue.h share/man/man3/queue.3

Only minimal changes compared to the FreeBSD version have been applied
(queue.3 -> queue.3bsd, _LIBBSD_ prefix).

[guillem@hadrons.org: Remove reference to kernel mode in man page. ]

Closes: !12
Signed-off-by: Guillem Jover <guillem@hadrons.org>
2021-02-08 04:02:16 +01:00
Guillem Jover
25f9b30678 test: Improve code coverage for strnstr(3) unit tests 2021-02-07 23:24:51 +01:00
Guillem Jover
18ebabf223 man: Update libbsd(7) man page with updates in 0.11.0 2021-02-07 15:17:33 +01:00
Guillem Jover
4ab11c7f48 build: Install libmd-dev in the gitlab CI 2021-02-07 10:37:18 +01:00
Guillem Jover
766c883e30 build: Switch gitlab CI to use a Debian buster 2021-02-07 10:35:57 +01:00
Guillem Jover
a4de4d95a6 Release libbsd 0.11.1 2021-02-07 02:03:59 +01:00
Guillem Jover
233cab9d64 Add support for new LIBBSD_VIS_OPENBSD selection macro
This will make it possible to explicitly select the OpenBSD vis
implementation (the current default) for code of OpenBSD origins.
2021-02-07 02:03:22 +01:00
146 changed files with 4505 additions and 1397 deletions

7
.gitignore vendored
View File

@ -1,4 +1,5 @@
ChangeLog
*~
*.pc
*.la
*.lo
@ -7,6 +8,10 @@ ChangeLog
*.a
*.log
*.trs
*.gcda
*.gcno
*.sym
*.map
.dirstamp
.deps/
.libs/
@ -17,6 +22,6 @@ autom4te.cache/
build-aux/
configure
config.*
format.ld
libtool
m4/
stamp-h1

View File

@ -1,9 +1,54 @@
image: debian:stretch
.vpath-tests:
script:
- ./autogen
- mkdir -p build-tree
- cd build-tree
- ../configure
- make check
test:
before_script:
- apt update -qq
- apt install -qq -y --no-install-recommends git gcc make autoconf automake libtool
.unit-tests:
script:
- ./autogen && ./configure
- make check
.unit-tests-asan:
script:
- ./autogen && ./configure --enable-sanitize
- make check
.coverage:
script:
- ./autogen && ./configure --disable-static
- make check CFLAGS="--coverage -O0 -ggdb" LDFLAGS="--coverage -O0 -ggdb"
- gcovr -s -e test/
coverage: /^TOTAL.*\s+(\d+\%)$/
.debian:
image: debian:latest
stage: test
before_script:
- apt-get update -qq
- apt-get install -qq -y --no-install-recommends
git gcc make autoconf automake libtool libmd-dev gcovr
.alpine:
image: alpine:latest
stage: test
before_script:
- apk add --no-cache git gcc make autoconf automake libtool
bsd-compat-headers linux-headers libmd-dev musl-dev gcovr
vpath-tests:debian:
extends: [.debian, .vpath-tests]
unit-tests:debian:
extends: [.debian, .unit-tests-asan]
coverage:debian:
extends: [.debian, .coverage]
unit-tests:alpine:
extends: [.alpine, .unit-tests]
coverage:alpine:
extends: [.alpine, .coverage]

83
COPYING
View File

@ -3,74 +3,9 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Files:
*
Copyright:
Copyright © 2004-2006, 2008-2021 Guillem Jover <guillem@hadrons.org>
Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
License: BSD-3-clause
Files:
man/arc4random.3bsd
man/tree.3bsd
Copyright:
Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
All rights reserved.
License: BSD-4-clause-Niels-Provos
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Niels Provos.
4. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Files:
man/getprogname.3bsd
Copyright:
Copyright © 2001 Christopher G. Demetriou
All rights reserved.
License: BSD-4-clause-Christopher-G-Demetriou
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed for the
NetBSD Project. See http://www.netbsd.org/ for
information about NetBSD.
4. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Files:
include/bsd/err.h
include/bsd/stdlib.h
@ -236,6 +171,7 @@ License: BSD-5-clause-Peter-Wemm
Files:
include/bsd/stringlist.h
man/arc4random.3bsd
man/fmtcheck.3bsd
man/humanize_number.3bsd
man/stringlist.3bsd
@ -250,6 +186,12 @@ Copyright:
Copyright © 2013 John-Mark Gurney <jmg@FreeBSD.org>
All rights reserved.
.
Copyright © 2014 The NetBSD Foundation, Inc.
All rights reserved.
.
Some code was derived from software contributed to The NetBSD Foundation
by Taylor R. Campbell.
.
Some code was contributed to The NetBSD Foundation by Allen Briggs.
.
Some code was contributed to The NetBSD Foundation by Luke Mewburn.
@ -311,7 +253,6 @@ Copyright:
All rights reserved.
.
Copyright © 2009 Advanced Computing Technologies LLC
Written by: John H. Baldwin <jhb@FreeBSD.org>
All rights reserved.
.
Copyright © 2011 Guillem Jover <guillem@hadrons.org>
@ -348,6 +289,7 @@ License: BSD-2-clause-verbatim
Files:
include/bsd/sys/tree.h
man/fparseln.3bsd
man/tree.3bsd
src/fparseln.c
Copyright:
Copyright © 1997 Christos Zoulas.
@ -364,7 +306,6 @@ Files:
man/strtonum.3bsd
src/arc4random.c
src/arc4random_linux.h
src/arc4random_openbsd.h
src/arc4random_uniform.c
src/arc4random_unix.h
src/arc4random_win.h
@ -383,6 +324,8 @@ Files:
src/recallocarray.c
src/strlcat.c
src/strlcpy.c
test/explicit_bzero.c
test/strtonum.c
Copyright:
Copyright © 2004 Ted Unangst and Todd Miller
All rights reserved.
@ -391,6 +334,7 @@ Copyright:
Copyright © 1998, 2000-2002, 2004-2005, 2007, 2010, 2012-2015
Todd C. Miller <Todd.Miller@courtesan.com>
Copyright © 2004 Ted Unangst
Copyright © 2004 Otto Moerbeek <otto@drijf.net>
Copyright © 2008 Damien Miller <djm@openbsd.org>
Copyright © 2008, 2010-2011, 2016-2017 Otto Moerbeek <otto@drijf.net>
Copyright © 2013 Markus Friedl <markus@openbsd.org>
@ -398,8 +342,9 @@ Copyright:
Copyright © 2014 Brent Cook <bcook@openbsd.org>
Copyright © 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org>
Copyright © 2014 Theo de Raadt <deraadt@openbsd.org>
Copyright © 2014 Google Inc.
Copyright © 2015 Michael Felt <aixtools@gmail.com>
Copyright © 2015 Guillem Jover <guillem@hadrons.org>
Copyright © 2015, 2022 Guillem Jover <guillem@hadrons.org>
License: ISC
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above

View File

@ -1,13 +1,18 @@
## Process this file with automake to produce Makefile.in
SUBDIRS = include man src test
SUBDIRS = \
include \
man \
src \
test \
# EOL
ACLOCAL_AMFLAGS = -I m4
EXTRA_DIST = \
autogen \
get-version \
$(nil)
# EOL
dist-hook:
echo $(VERSION) >$(distdir)/.dist-version

41
README
View File

@ -9,6 +9,7 @@ A BSD compatible message-digest library is required, on systems where
this is not provided by its libc or libmd libraries, the canonical
implementation to use is <https://www.hadrons.org/software/libmd/>.
Releases
--------
@ -30,10 +31,44 @@ The mail address is:
Source Repository
-----------------
The master repository can be browsed at:
The primary repository can be browsed at:
<https://cgit.freedesktop.org/libbsd>
<https://gitlab.freedesktop.org/libbsd/libbsd>
and cloned from:
<https://anongit.freedesktop.org/git/libbsd>
<https://gitlab.freedesktop.org/libbsd/libbsd.git>
Building from git source
------------------------
To prepare the libbsd source tree from git before starting the build process
some required software needs to be installed:
GNU autoconf >= 2.67
GNU automake >= 1.9
GNU libtool >= 2.0
After installing the needed software, and running the following command on
the git tree:
$ ./autogen
the source should be roughly equivalent to the distributed tar source.
Building from tar source
------------------------
The minimum software required to configure and build dpkg from a tarball is:
C89 compiler
make
The following software might be required depending on the system:
libmd (whenever the libc does not provide the needed digest functions)
The build process is done by running the usual «./configure; make». To
see all available configuration options please run «./configure --help».

View File

@ -6,17 +6,22 @@ AC_CONFIG_SRCDIR([src/strlcpy.c])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([1.9 foreign nostdinc subdir-objects no-dist-gzip dist-xz])
AM_INIT_AUTOMAKE(
[1.11]
[-Wall]
[foreign]
[nostdinc]
[subdir-objects]
[no-dist-gzip dist-xz]
)
AM_SILENT_RULES([yes])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])],
[AC_SUBST([AM_DEFAULT_VERBOSITY], [1])])
SOVERSION_MAJOR=0
SOVERSION_MINOR=12
SOVERSION_PATCH=2
LIBBSD_ABI_MAJOR=0
LIBBSD_ABI_MINOR=11
LIBBSD_ABI_PATCH=0
LIBBSD_ABI="$LIBBSD_ABI_MAJOR:$LIBBSD_ABI_MINOR:$LIBBSD_ABI_PATCH"
AC_SUBST([LIBBSD_ABI])
SOVERSION="$SOVERSION_MAJOR:$SOVERSION_MINOR:$SOVERSION_PATCH"
AC_SUBST([SOVERSION])
# Check and store if we got user supplied variables
user_CFLAGS=${CFLAGS-unset}
@ -24,81 +29,480 @@ user_CFLAGS=${CFLAGS-unset}
# Checks for operating system services and capabilities.
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
LIBBSD_SYS_TIME64
AM_PROG_AR
LT_INIT
LIBBSD_LINKER_VERSION_SCRIPT
AC_CACHE_CHECK([if ld supports --version-script flag],
[libbsd_cv_version_script], [
echo "{ global: symbol; local: *; };" >conftest.map
save_LDFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -Wl,--version-script=conftest.map"
AC_LINK_IFELSE([
AC_LANG_PROGRAM([], [])
], [
libbsd_cv_version_script=yes
], [
libbsd_cv_version_script=no
])
LDFLAGS="$save_LDFLAGS"
rm -f conftest.map
]
## Select library ABI to expose.
#
# The systems have DROP comments for interfaces that could be dropped on
# the next SOVERSION bump.
is_windows=no
api_time_macros=unknown
abi_accmode=unknown
abi_arc4random=unknown
abi_arc4random_stir=unknown
abi_asprintf=unknown
abi_bsd_getopt=unknown
abi_closefrom=unknown
abi_err=unknown
abi_errc=unknown
abi_expand_number=unknown
abi_explicit_bzero=unknown
abi_fgetln=unknown
abi_flopen=unknown
abi_fmtcheck=unknown
abi_fpurge=unknown
abi_freezero=unknown
abi_funopen=unknown
abi_getbsize=unknown
abi_getpeereid=unknown
abi_humanize_number=unknown
abi_id_from_name=unknown
abi_inet_net_pton=unknown
abi_md5=unknown
abi_name_from_id=unknown
abi_nlist=unknown
abi_pidfile=unknown
abi_proctitle=unknown
abi_progname=unknown
abi_readpassphrase=unknown
abi_reallocarray=unknown
abi_reallocf=unknown
abi_recallocarray=unknown
abi_stringlist=unknown
abi_sort=unknown
abi_strl=unknown
abi_strmode=unknown
abi_strnstr=unknown
abi_strtonum=unknown
abi_strtox=unknown
abi_timeconv=unknown
abi_vis=unknown
abi_wcsl=unknown
AS_CASE([$host_os],
[*-gnu* | gnu*], [
api_time_macros=no
abi_accmode=yes
# DROP: On glibc >= 2.36.
#abi_arc4random=no
abi_arc4random=yes
abi_arc4random_stir=yes
abi_asprintf=no
abi_bsd_getopt=yes
# DROP: On glibc >= 2.34.
#abi_closefrom=no
abi_closefrom=yes
abi_err=no
abi_errc=yes
abi_expand_number=yes
# DROP: On glibc >= 2.25.
#abi_explicit_bzero=no
abi_explicit_bzero=yes
abi_fgetln=yes
abi_flopen=yes
abi_fmtcheck=yes
abi_fpurge=yes
abi_freezero=yes
abi_funopen=yes
abi_getbsize=yes
abi_getpeereid=yes
abi_humanize_number=yes
abi_id_from_name=yes
# DROP: On glibc.
#abi_inet_net_pton=no
abi_inet_net_pton=yes
# DROP: On libmd.
#abi_md5=no
abi_md5=yes
abi_name_from_id=yes
abi_nlist=yes
abi_pidfile=yes
abi_proctitle=yes
abi_progname=yes
abi_readpassphrase=yes
# DROP: On glibc >= 2.26.
#abi_reallocarray=no
abi_reallocarray=yes
abi_reallocf=yes
abi_recallocarray=yes
abi_stringlist=yes
abi_sort=yes
# DROP: On glibc >= 2.38.
#abi_strl=no
abi_strl=yes
abi_strmode=yes
abi_strnstr=yes
abi_strtonum=yes
abi_strtox=yes
abi_timeconv=yes
# DROP: On libmd.
abi_transparent_libmd=yes
abi_vis=yes
abi_wcsl=yes
],
[*-musl*], [
api_time_macros=no
abi_accmode=yes
abi_arc4random=yes
abi_arc4random_stir=yes
abi_asprintf=no
# DROP: On musl >= 0.9.7, optreset got implemented.
#abi_bsd_getopt=no
abi_bsd_getopt=yes
abi_closefrom=yes
abi_err=no
abi_errc=yes
abi_expand_number=yes
# DROP: On musl >= 1.1.20.
#abi_explicit_bzero=no
abi_explicit_bzero=yes
# DROP: On musl >= 0.9.4.
#abi_fgetln=no
abi_fgetln=yes
abi_flopen=yes
abi_fmtcheck=yes
abi_fpurge=yes
abi_freezero=yes
# On musl >= 1.1.19, fopencookie() got implemented, and because we were
# checking for its presence to decide whether to build funopen(), it got
# included in builds even when previously it had not been included, which
# is partially an ABI issue, but given that disabling it now would be
# worse, we'll ignore this as this is only a problem with downgrades. And
# enable it explicitly
#abi_funopen=no
abi_funopen=yes
abi_getbsize=yes
abi_getpeereid=yes
abi_humanize_number=yes
abi_id_from_name=yes
abi_inet_net_pton=yes
# DROP: On libmd.
#abi_md5=no
abi_md5=yes
abi_name_from_id=yes
abi_nlist=yes
abi_pidfile=yes
abi_proctitle=yes
abi_progname=yes
abi_readpassphrase=yes
# DROP: On musl >= 1.2.2.
#abi_reallocarray=no
abi_reallocarray=yes
abi_reallocf=ues
abi_recallocarray=yes
abi_stringlist=yes
abi_sort=yes
# DROP: On musl >= 0.5.0, strlcpy() and strlcat() were already implemented.
#abi_strl=no
abi_strl=yes
abi_strmode=yes
abi_strnstr=yes
abi_strtonum=yes
abi_strtox=yes
abi_timeconv=yes
# DROP: On libmd.
abi_transparent_libmd=yes
abi_vis=yes
abi_wcsl=yes
],
[darwin*], [
api_time_macros=no
abi_accmode=no
abi_arc4random=no
abi_arc4random_stir=no
abi_asprintf=no
abi_bsd_getopt=no
abi_closefrom=yes
abi_err=no
abi_errc=no
abi_expand_number=yes
abi_explicit_bzero=yes
abi_fgetln=no
abi_flopen=yes
abi_fmtcheck=no
abi_fpurge=no
abi_freezero=yes
abi_funopen=no
abi_getbsize=yes
abi_getpeereid=no
abi_humanize_number=yes
abi_id_from_name=yes
abi_inet_net_pton=no
# On libmd.
abi_md5=no
abi_name_from_id=no
abi_nlist=no
abi_pidfile=yes
abi_proctitle=yes
abi_progname=no
abi_readpassphrase=no
abi_reallocarray=yes
abi_reallocf=no
abi_recallocarray=yes
abi_stringlist=no
abi_sort=no
abi_strl=no
abi_strmode=no
abi_strnstr=no
abi_strtonum=no
abi_strtox=yes
abi_timeconv=no
# On libmd.
abi_transparent_libmd=no
abi_vis=no
abi_wcsl=no
],
[solaris2*], [
api_time_macros=yes
abi_accmode=yes
abi_arc4random=no
abi_arc4random_stir=no
abi_asprintf=no
abi_bsd_getopt=yes
abi_closefrom=no
abi_err=no
abi_errc=yes
abi_expand_number=yes
abi_explicit_bzero=yes
abi_fgetln=yes
abi_flopen=yes
abi_fmtcheck=no
abi_fpurge=yes
abi_freezero=yes
abi_funopen=no
abi_getbsize=yes
abi_getpeereid=yes
abi_humanize_number=yes
abi_id_from_name=yes
abi_inet_net_pton=yes
# On libmd.
abi_md5=no
abi_name_from_id=yes
abi_nlist=no
abi_pidfile=yes
abi_proctitle=yes
abi_progname=no
abi_readpassphrase=yes
abi_reallocarray=yes
abi_reallocf=no
abi_recallocarray=yes
abi_stringlist=yes
abi_sort=yes
abi_strl=no
abi_strmode=yes
abi_strnstr=no
abi_strtonum=no
abi_strtox=yes
abi_timeconv=yes
# On libmd.
abi_transparent_libmd=no
abi_vis=yes
abi_wcsl=yes
],
[aix*], [
api_time_macros=yes
abi_accmode=yes
abi_arc4random=yes
abi_arc4random_stir=yes
abi_asprintf=yes
abi_bsd_getopt=yes
abi_closefrom=yes
abi_err=yes
abi_errc=yes
abi_expand_number=yes
abi_explicit_bzero=yes
abi_fgetln=yes
abi_flopen=yes
abi_fmtcheck=yes
abi_fpurge=no
abi_freezero=yes
abi_funopen=no
abi_getbsize=yes
abi_getpeereid=yes
abi_humanize_number=yes
abi_id_from_name=yes
abi_inet_net_pton=no
# On libmd.
abi_md5=no
abi_name_from_id=yes
abi_nlist=no
abi_pidfile=yes
abi_proctitle=yes
abi_progname=yes
abi_readpassphrase=yes
abi_reallocarray=yes
abi_reallocf=yes
abi_recallocarray=yes
abi_stringlist=yes
abi_sort=yes
abi_strl=yes
abi_strmode=yes
abi_strnstr=yes
abi_strtonum=yes
abi_strtox=yes
abi_timeconv=yes
# On libmd.
abi_transparent_libmd=no
abi_vis=yes
abi_wcsl=yes
],
[mingw*], [
is_windows=yes
],
)
AM_CONDITIONAL([HAVE_LINKER_VERSION_SCRIPT],
[test "x$libbsd_cv_version_script" = "xyes"])
# Handle artificial ABI selections that are combinations of others.
AS_IF([test "$abi_name_from_id" = "yes" || test "$abi_id_from_name" = "yes"], [
abi_pwcache=yes
], [
abi_pwcache=no
])
AS_IF([test "$abi_err" = "yes" || test "$abi_errc" = "yes"], [
abi_err_h=yes
], [
abi_err_h=no
])
AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
# Checks for programs.
AC_CHECK_TOOL([OBJDUMP], [objdump])
AC_PROG_CC
AC_PROG_SED
AC_PROG_INSTALL
AC_PROG_LN_S
# Set default compiler variables
if test "$user_CFLAGS" = unset && test "$GCC" = yes; then
CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter"
fi
AS_IF([test "$user_CFLAGS" = unset], [
LIBBSD_CHECK_COMPILER_FLAG([-Wall])
LIBBSD_CHECK_COMPILER_FLAG([-Wextra])
LIBBSD_CHECK_COMPILER_FLAG([-Wbad-function-cast])
LIBBSD_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
LIBBSD_CHECK_COMPILER_FLAG([-Wcast-align])
LIBBSD_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
LIBBSD_CHECK_COMPILER_FLAG([-Wdocumentation])
LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-branches])
LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-cond])
LIBBSD_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
LIBBSD_CHECK_COMPILER_FLAG([-Wformat=2])
LIBBSD_CHECK_COMPILER_FLAG([-Winit-self])
LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-op])
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-declarations])
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
LIBBSD_CHECK_COMPILER_FLAG([-Wnested-externs])
LIBBSD_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
LIBBSD_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
LIBBSD_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
LIBBSD_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
LIBBSD_CHECK_COMPILER_FLAG([-Wnull-dereference])
LIBBSD_CHECK_COMPILER_FLAG([-Wold-style-definition])
LIBBSD_CHECK_COMPILER_FLAG([-Wpointer-arith])
LIBBSD_CHECK_COMPILER_FLAG([-Wregister])
LIBBSD_CHECK_COMPILER_FLAG([-Wrestrict])
LIBBSD_CHECK_COMPILER_FLAG([-Wshadow])
LIBBSD_CHECK_COMPILER_FLAG([-Wshift-negative-value])
LIBBSD_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
LIBBSD_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
LIBBSD_CHECK_COMPILER_FLAG([-Wswitch-bool])
LIBBSD_CHECK_COMPILER_FLAG([-Wvla])
LIBBSD_CHECK_COMPILER_FLAG([-Wwrite-strings])
CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
AC_ARG_ENABLE([sanitize],
[AS_HELP_STRING([--enable-sanitize], [enable compiler sanitizer support])],
[
LIBBSD_COMPILER_FLAGS=''
LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=address])
LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=leak])
LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=undefined])
CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
LDFLAGS="$LDFLAGS $LIBBSD_COMPILER_FLAGS"
])
])
# Checks for libraries.
AC_CHECK_LIB([testu01], [unif01_CreateExternGenBits],
[TESTU01_LIBS="-ltestu01"])
[TESTU01_LIBS="-ltestu01"])
AC_SUBST([TESTU01_LIBS])
AM_CONDITIONAL([HAVE_LIBTESTU01],
[test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
[test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
saved_LIBS="$LIBS"
AC_SEARCH_LIBS([MD5Update], [md], [
AC_SEARCH_LIBS([SHA512Update], [md], [
MD_LIBS="-lmd"
AS_IF([test "$abi_md5" = "yes"], [
AC_SEARCH_LIBS([MD5Update], [md], [
AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
MD5_LIBS="$MD5_LIBS $ac_cv_search_MD5Update"
abi_transparent_libmd=yes
])
], [
AC_MSG_ERROR([cannot find required MD5 functions in libc or libmd])
])
])
AS_IF([test "$abi_arc4random" = "yes"], [
AC_CHECK_FUNCS([getentropy])
AS_IF([test "$ac_cv_func_getentropy" != "yes"], [
AC_SEARCH_LIBS([SHA512Update], [md], [
AS_IF([test "x$ac_cv_search_SHA512Update" != "xnone required"], [
LIBBSD_LIBS="$SHA512_LIBS $ac_cv_search_SHA512Update"
])
], [
AC_MSG_ERROR([cannot find required SHA-2 functions in libc or libmd])
])
])
], [
AC_MSG_ERROR([cannot find required message digest functions in libc or libmd])
])
AC_SUBST([MD_LIBS])
LIBS="$saved_LIBS"
is_windows=no
AS_CASE([$host_os],
[*-gnu*], [
# In old glibc versions (< 2.17) clock_gettime() is in librt.
saved_LIBS="$LIBS"
AC_SEARCH_LIBS([clock_gettime], [rt], [
AS_IF([test "x$ac_cv_search_clock_gettime" != "xnone required"], [
CLOCK_GETTIME_LIBS="$ac_cv_search_clock_gettime"
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_clock_gettime"
])
])
AC_SUBST([CLOCK_GETTIME_LIBS])
LIBS="$saved_LIBS"
],
[*-musl*], [
# Upstream refuses to define this, we will do it ourselves then.
AC_DEFINE([__MUSL__], [1], [Define to 1 if we are building for musl])
],
[mingw*], [
is_windows=yes
[aix*], [
saved_LIBS="$LIBS"
AC_SEARCH_LIBS([perfstat_cpu_total], [perfstat], [
AS_IF([test "x$ac_cv_search_perfstat_cpu_total" != "xnone required"], [
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_perfstat_cpu_total"
])
])
LIBS="$saved_LIBS"
],
)
AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
# Checks for header files.
AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h pwd.h grp.h])
AC_CHECK_HEADERS([\
sys/ndir.h \
sys/dir.h \
ndir.h \
dirent.h \
pwd.h \
grp.h \
stdio_ext.h \
procinfo.h \
])
# Checks for typedefs, structures, and compiler characteristics.
AC_C_INLINE
@ -111,106 +515,115 @@ AC_TYPE_UID_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_CHECK_DECL([F_CLOSEM],
[AC_DEFINE([HAVE_FCNTL_CLOSEM], [1],
[Define to 1 if you have fcntl(F_CLOSEM)])],
[],
[#include <limits.h>
#include <fcntl.h>])
AC_CHECK_DECL([F_CLOSEM], [
AC_DEFINE([HAVE_FCNTL_CLOSEM], [1],
[Define to 1 if you have fcntl(F_CLOSEM)])
], [], [[
#include <limits.h>
#include <fcntl.h>
]])
AC_CACHE_CHECK(
[for GNU .init_array section support],
[libbsd_cv_gnu_init_array_support],
[AC_RUN_IFELSE(
[AC_LANG_SOURCE(
[[
static int rc = 1;
static void init(int argc) { if (argc == 1) rc = 0; }
void (*init_func)(int argc) __attribute__((__section__(".init_array"))) = init;
int main() { return rc; }
]]
)],
[libbsd_cv_gnu_init_array_support=yes],
[libbsd_cv_gnu_init_array_support=no],
[AC_PREPROC_IFELSE(
[AC_LANG_SOURCE(
[[
/* Look for a known libc that supports .init_array with the GNU extension
* to pass main() arguments to the init functions. */
#include <stdlib.h>
#if defined __GLIBC_PREREQ
# if __GLIBC_PREREQ(2, 4)
/* glibc supports GNU .init_array since 2.4. */
# else
# error glibc does not support GNU .init_array
# endif
#else
/*
* Basic SysV ABI .init_array support, init functions do not get arguments:
* - Bionic since its inception.
* - uClibc since 0.9.29.
*/
# error unknown whether libc supports GNU .init_array
#endif
]]
)],
[libbsd_cv_gnu_init_array_support=yes],
[libbsd_cv_gnu_init_array_support=no])
]
)]
)
AM_CONDITIONAL([BUILD_LIBBSD_CTOR],
[test "$libbsd_cv_gnu_init_array_support" = yes])
AC_CHECK_DECLS([environ], [], [], [[
#include <unistd.h>
]])
LIBBSD_HAS_GNU_INIT_ARRAY
# Checks for library functions.
AC_MSG_CHECKING([for program_invocation_short_name])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[#include <errno.h>]],
[[const char *p = program_invocation_short_name;]])],
[AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
[Define to 1 if you have program_invocation_short_name])
AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])])
LIBBSD_CHECK_PROGNAME
LIBBSD_CHECK_REGISTER_ATFORK
AC_MSG_CHECKING([for __progname])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[extern char *__progname;]],
[[printf("%s", __progname);]])],
[AC_DEFINE([HAVE___PROGNAME], [1], [Define to 1 if you have __progname])
AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])])
AC_CHECK_FUNCS([\
vasprintf \
asprintf \
clearenv \
dirfd \
flock \
fopencookie \
__fpurge \
funopen \
getauxval \
getentropy \
getexecname \
getline \
open_memstream \
pstat_getproc \
sysconf \
uid_from_user \
gid_from_group \
user_from_uid \
group_from_gid \
])
AC_MSG_CHECKING([for __register_atfork])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
#include <stddef.h>
extern void *__dso_handle;
extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
]], [[
__register_atfork(NULL, NULL, NULL, __dso_handle);
]])],
[AC_DEFINE([HAVE___REGISTER_ATFORK], [1],
[Define to 1 if you have __register_atfork])
AC_MSG_RESULT([yes])],
[ARC4RANDOM_ATFORK_LIBS="-pthread"
AC_SUBST([ARC4RANDOM_ATFORK_LIBS])
AC_MSG_RESULT([no])
])
AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xyes"])
AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
getauxval getentropy getexecname getline \
pstat_getproc sysconf])
AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
# API selection
LIBBSD_SELECT_API([time_macros], [time struct conversion macros])
# ABI selection
LIBBSD_SELECT_ABI([accmode], [setmode()/getmode()])
LIBBSD_SELECT_ABI([arc4random], [arc4random() API])
LIBBSD_SELECT_ABI([arc4random_stir], [arc4random_stir()/arc4random_addrandom()])
LIBBSD_SELECT_ABI([asprintf], [vasprintf()/asprintf()])
LIBBSD_SELECT_ABI([bsd_getopt], [BSD getopt()])
LIBBSD_SELECT_ABI([closefrom], [closefrom()])
LIBBSD_SELECT_ABI([err_h], [err.h header])
LIBBSD_SELECT_ABI([err], [err API])
LIBBSD_SELECT_ABI([errc], [errc API])
LIBBSD_SELECT_ABI([expand_number], [expand_number()])
LIBBSD_SELECT_ABI([explicit_bzero], [explicit_bzero()])
LIBBSD_SELECT_ABI([fgetln], [fgetln/fgetwln()])
LIBBSD_SELECT_ABI([flopen], [flopen()/flopenat()])
LIBBSD_SELECT_ABI([fmtcheck], [fmtcheck()])
LIBBSD_SELECT_ABI([fpurge], [fpurge()])
LIBBSD_SELECT_ABI([freezero], [freezero()])
LIBBSD_SELECT_ABI([funopen], [funopen()])
LIBBSD_SELECT_ABI([getbsize], [getbsize()])
LIBBSD_SELECT_ABI([getpeereid], [getpeereid()])
LIBBSD_SELECT_ABI([humanize_number], [humanize_number()/dehumanize_number()])
LIBBSD_SELECT_ABI([id_from_name], [uid_from_user()/gid_from_user()])
LIBBSD_SELECT_ABI([inet_net_pton], [inet_net_pton()])
LIBBSD_SELECT_ABI([md5], [MD5 digest functions])
LIBBSD_SELECT_ABI([name_from_id], [user_from_uid()/group_from_gid()])
LIBBSD_SELECT_ABI([nlist], [nlist()])
LIBBSD_SELECT_ABI([pidfile], [pidfile API])
LIBBSD_SELECT_ABI([proctitle], [setproctitle() API])
LIBBSD_SELECT_ABI([progname], [setprogname()/getprogname()])
LIBBSD_SELECT_ABI([pwcache], [password cache API])
LIBBSD_SELECT_ABI([readpassphrase], [readpassphrase()])
LIBBSD_SELECT_ABI([reallocarray], [reallocarray()])
LIBBSD_SELECT_ABI([reallocf], [reallocf()])
LIBBSD_SELECT_ABI([recallocarray], [recallocarray()])
LIBBSD_SELECT_ABI([stringlist], [stringlist sl_*() API])
LIBBSD_SELECT_ABI([sort], [BSD sort functions])
LIBBSD_SELECT_ABI([strl], [strlcpy()/strlcat()])
LIBBSD_SELECT_ABI([strmode], [strmode()])
LIBBSD_SELECT_ABI([strnstr], [strnstr()])
LIBBSD_SELECT_ABI([strtonum], [strtonum()])
LIBBSD_SELECT_ABI([strtox], [strtoi()/strtou()])
LIBBSD_SELECT_ABI([timeconv], [timeconv.h API])
LIBBSD_SELECT_ABI([transparent_libmd], [transparent libmd support])
LIBBSD_SELECT_ABI([vis], [vis API])
LIBBSD_SELECT_ABI([wcsl], [wcslcpy()/wcslcat()])
AS_IF([test "x$abi_funopen" = "xno" && \
test "x$ac_cv_func_funopen" != "xyes" && \
test "x$ac_cv_func_fopencookie" = "xyes"], [
AC_MSG_WARN([[can implement funopen() now based on newly added fopencooke(), report upstream]])
])
AC_SUBST([MD5_LIBS])
AC_SUBST([LIBBSD_LIBS])
AC_CONFIG_FILES([
Makefile
include/Makefile
man/Makefile
src/Makefile
src/libbsd.pc
src/libbsd-ctor.pc
src/libbsd-overlay.pc
test/Makefile
Makefile
include/Makefile
man/Makefile
src/Makefile
src/libbsd.pc
src/libbsd-ctor.pc
src/libbsd-overlay.pc
test/Makefile
])
AC_CONFIG_HEADERS([config.h])
AC_OUTPUT

View File

@ -12,21 +12,77 @@ nobase_include_HEADERS = \
bsd/netinet/ip_icmp.h \
bsd/bitstring.h \
bsd/bsd.h \
bsd/err.h \
bsd/getopt.h \
bsd/grp.h \
bsd/inttypes.h \
bsd/libutil.h \
bsd/md5.h \
bsd/nlist.h \
bsd/pwd.h \
bsd/readpassphrase.h \
bsd/stdio.h \
bsd/stdlib.h \
bsd/string.h \
bsd/stringlist.h \
bsd/timeconv.h \
bsd/unistd.h \
bsd/vis.h \
bsd/wchar.h \
$(nil)
# EOL
if ABI_ERR_H
nobase_include_HEADERS += \
bsd/err.h \
# EOL
endif
if ABI_BSD_GETOPT
nobase_include_HEADERS += \
bsd/getopt.h \
# EOL
endif
if ABI_MD5
nobase_include_HEADERS += \
bsd/md5.h \
# EOL
endif
if ABI_NLIST
nobase_include_HEADERS += \
bsd/nlist.h \
# EOL
endif
if ABI_READPASSPHRASE
nobase_include_HEADERS += \
bsd/readpassphrase.h \
# EOL
endif
if ABI_PWCACHE
nobase_include_HEADERS += \
bsd/grp.h \
bsd/pwd.h \
# EOL
endif
if ABI_STRINGLIST
nobase_include_HEADERS += \
bsd/stringlist.h \
# EOL
endif
if ABI_TIMECONV
nobase_include_HEADERS += \
bsd/timeconv.h \
# EOL
endif
if ABI_VIS
nobase_include_HEADERS += \
bsd/vis.h \
# EOL
endif
do_header_subst = $(AM_V_GEN) $(SED) \
-e 's:^//\(.define LIBBSD_SYS_TIME_BITS\).*:\1 $(LIBBSD_SYS_TIME_BITS):' \
-e 's:^//\(.define LIBBSD_SYS_HAS_TIME64\).*:\1 $(LIBBSD_SYS_HAS_TIME64):' \
# EOL
install-data-hook:
$(do_header_subst) <$(DESTDIR)$(includedir)/bsd/sys/cdefs.h \
>$(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new
mv $(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new \
$(DESTDIR)$(includedir)/bsd/sys/cdefs.h

View File

@ -44,8 +44,10 @@
__BEGIN_DECLS
int
gid_from_group(const char *, gid_t *);
#ifndef __APPLE__
const char *
group_from_gid(gid_t, int);
#endif
__END_DECLS
#endif

View File

@ -24,8 +24,10 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _AIX
#ifdef LIBBSD_OVERLAY
#include_next <md5.h>
#else
#include <md5.h>
#endif
#endif

View File

@ -44,8 +44,10 @@
__BEGIN_DECLS
int
uid_from_user(const char *, uid_t *);
#ifndef __APPLE__
const char *
user_from_uid(uid_t, int);
#endif
__END_DECLS
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2004-2005, 2009, 2011-2013 Guillem Jover <guillem@hadrons.org>
* Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -44,8 +44,18 @@
#include <bsd/sys/cdefs.h>
#endif
#include <sys/types.h>
#if defined(_AIX)
#include <stdarg.h>
#endif
__BEGIN_DECLS
#if defined(_AIX)
int asprintf(char **strp, char const *fmt, ...)
__printflike(2, 3);
int vasprintf(char **strp, char const *fmt, va_list args)
__printflike(2, 0);
#endif
const char *fmtcheck(const char *, const char *);
/* XXX: The function requires cooperation from the system libc to store the

View File

@ -51,16 +51,24 @@
#include <stdint.h>
__BEGIN_DECLS
#if !defined(__APPLE__) && !defined(__sun)
#if !defined(__GLIBC__) || \
!__GLIBC_PREREQ(2, 36) || \
!defined(_DEFAULT_SOURCE)
uint32_t arc4random(void);
void arc4random_stir(void);
void arc4random_addrandom(unsigned char *dat, int datlen);
void arc4random_buf(void *_buf, size_t n);
uint32_t arc4random_uniform(uint32_t upper_bound);
#endif
void arc4random_stir(void);
void arc4random_addrandom(unsigned char *dat, int datlen);
#endif
int dehumanize_number(const char *str, int64_t *size);
#if !defined(__APPLE__)
const char *getprogname(void);
void setprogname(const char *);
#endif
int heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
int mergesort(void *base, size_t nmemb, size_t size,
@ -72,7 +80,9 @@ int sradixsort(const unsigned char **base, int nmemb,
void *reallocf(void *ptr, size_t size);
#if !defined(__GLIBC__) || \
(defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 26) || !defined(_GNU_SOURCE)))
!__GLIBC_PREREQ(2, 26) || \
(__GLIBC_PREREQ(2, 26) && !__GLIBC_PREREQ(2, 29) && !defined(_GNU_SOURCE)) || \
(__GLIBC_PREREQ(2, 29) && !defined(_DEFAULT_SOURCE))
void *reallocarray(void *ptr, size_t nmemb, size_t size);
#endif
void *recallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size);

View File

@ -41,13 +41,19 @@
#include <sys/types.h>
__BEGIN_DECLS
#if !defined(__APPLE__) && \
(!defined(__GLIBC__) || !__GLIBC_PREREQ(2, 38) || !defined(_DEFAULT_SOURCE))
size_t strlcpy(char *dst, const char *src, size_t siz);
size_t strlcat(char *dst, const char *src, size_t siz);
#endif
char *strnstr(const char *str, const char *find, size_t str_len);
#ifndef __APPLE__
void strmode(mode_t mode, char *str);
#endif
#if !defined(__GLIBC__) || \
(defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 25) || !defined(_GNU_SOURCE)))
!__GLIBC_PREREQ(2, 25) || \
!defined(_DEFAULT_SOURCE)
void explicit_bzero(void *buf, size_t len);
#endif
__END_DECLS

View File

@ -37,6 +37,9 @@
#ifndef __is_identifier
#define __is_identifier(x) 1
#endif
#ifndef __has_builtin
#define __has_builtin(x) !__is_identifier(x)
#endif
#ifdef LIBBSD_OVERLAY
/*
@ -83,6 +86,10 @@
#define _SYS_CDEFS_H
#endif
/* Define the ABI for the current system. */
//#define LIBBSD_SYS_TIME_BITS 0
//#define LIBBSD_SYS_HAS_TIME64 0
#define LIBBSD_CONCAT(x, y) x ## y
#define LIBBSD_STRING(x) #x
@ -92,6 +99,12 @@
#define LIBBSD_GCC_VERSION 0
#endif
#if LIBBSD_GCC_VERSION >= 0x0300 || __has_attribute(__unused__)
# define LIBBSD_UNUSED __attribute__((__unused__))
#else
# define LIBBSD_UNUSED
#endif
#if LIBBSD_GCC_VERSION >= 0x0405 || __has_attribute(__deprecated__)
#define LIBBSD_DEPRECATED(x) __attribute__((__deprecated__(x)))
#elif LIBBSD_GCC_VERSION >= 0x0301
@ -143,11 +156,7 @@
* Disable for now. */
#if 0
#ifndef __unused
# if LIBBSD_GCC_VERSION >= 0x0300
# define __unused __attribute__((__unused__))
# else
# define __unused
# endif
# define __unused LIBBSD_UNUSED
#endif
#endif
@ -182,15 +191,15 @@
* require it.
*/
#ifndef __offsetof
# if LIBBSD_GCC_VERSION >= 0x0401 || !__is_identifier(__builtin_offsetof)
# if LIBBSD_GCC_VERSION >= 0x0401 || __has_builtin(__builtin_offsetof)
# define __offsetof(type, field) __builtin_offsetof(type, field)
# else
# ifndef __cplusplus
# define __offsetof(type, field) \
((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field))
# else
# define __offsetof(type, field) \
(__offsetof__ (reinterpret_cast <__size_t> \
(__offsetof__ (reinterpret_cast <size_t> \
(&reinterpret_cast <const volatile char &> \
(static_cast<type *> (0)->field))))
# endif
@ -243,15 +252,15 @@
#endif
#ifndef __DECONST
#define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
#endif
#ifndef __DEVOLATILE
#define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
#define __DEVOLATILE(type, var) ((type)(uintptr_t)(volatile void *)(var))
#endif
#ifndef __DEQUALIFY
#define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
#define __DEQUALIFY(type, var) ((type)(uintptr_t)(const volatile void *)(var))
#endif
#endif

View File

@ -1,4 +1,6 @@
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
@ -10,7 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@ -80,17 +82,25 @@
*
* For details on the use of these macros, see the queue(3) manual page.
*
* Below is a summary of implemented functions where:
* + means the macro is available
* - means the macro is not available
* s means the macro is available but is slow (runs in O(n) time)
*
* SLIST LIST STAILQ TAILQ
* _HEAD + + + +
* _CLASS_HEAD + + + +
* _HEAD_INITIALIZER + + + +
* _ENTRY + + + +
* _CLASS_ENTRY + + + +
* _INIT + + + +
* _EMPTY + + + +
* _END + + + +
* _FIRST + + + +
* _NEXT + + + +
* _PREV - + - +
* _LAST - - + +
* _LAST_FAST - - - +
* _FOREACH + + + +
* _FOREACH_FROM + + + +
* _FOREACH_SAFE + + + +
@ -103,14 +113,20 @@
* _INSERT_BEFORE - + - +
* _INSERT_AFTER + + + +
* _INSERT_TAIL - - + +
* _CONCAT - - + +
* _CONCAT s s + +
* _REMOVE_AFTER + - + -
* _REMOVE_HEAD + - + -
* _REMOVE + + + +
* _REMOVE s + s +
* _SWAP + + + +
*
*/
#ifdef QUEUE_MACRO_DEBUG
#warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
#define QUEUE_MACRO_DEBUG_TRACE
#define QUEUE_MACRO_DEBUG_TRASH
#endif
#ifdef QUEUE_MACRO_DEBUG_TRACE
/* Store the last 2 places the queue element or head was altered */
struct qm_trace {
unsigned long lastline;
@ -120,9 +136,7 @@ struct qm_trace {
};
#define TRACEBUF struct qm_trace trace;
#define TRACEBUF_INITIALIZER { __FILE__, __LINE__, NULL, 0 } ,
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
#define QMD_TRACE_HEAD(head) do { \
(head)->trace.prevline = (head)->trace.lastline; \
@ -138,14 +152,31 @@ struct qm_trace {
(elem)->trace.lastfile = __FILE__; \
} while (0)
#else
#else /* !QUEUE_MACRO_DEBUG_TRACE */
#define QMD_TRACE_ELEM(elem)
#define QMD_TRACE_HEAD(head)
#define QMD_SAVELINK(name, link)
#define TRACEBUF
#define TRACEBUF_INITIALIZER
#endif /* QUEUE_MACRO_DEBUG_TRACE */
#ifdef QUEUE_MACRO_DEBUG_TRASH
#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
#define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
#else /* !QUEUE_MACRO_DEBUG_TRASH */
#define QMD_SAVELINK(name, link)
#define TRASHIT(x)
#endif /* QUEUE_MACRO_DEBUG */
#define QMD_IS_TRASHED(x) 0
#endif /* QUEUE_MACRO_DEBUG_TRASH */
#ifdef __cplusplus
/*
* In C++ there can be structure lists and class lists:
*/
#define QUEUE_TYPEOF(type) type
#else
#define QUEUE_TYPEOF(type) struct type
#endif
/*
* Singly-linked List declarations.
@ -155,6 +186,11 @@ struct name { \
struct type *slh_first; /* first element */ \
}
#define SLIST_CLASS_HEAD(name, type) \
struct name { \
class type *slh_first; /* first element */ \
}
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
@ -163,9 +199,37 @@ struct { \
struct type *sle_next; /* next element */ \
}
#define SLIST_CLASS_ENTRY(type) \
struct { \
class type *sle_next; /* next element */ \
}
/*
* Singly-linked List functions.
*/
#if (defined(_KERNEL) && defined(INVARIANTS))
#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
if (*(prevp) != (elm)) \
panic("Bad prevptr *(%p) == %p != %p", \
(prevp), *(prevp), (elm)); \
} while (0)
#else
#define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
#endif
#define SLIST_CONCAT(head1, head2, type, field) do { \
QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
if (curelm == NULL) { \
if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
SLIST_INIT(head2); \
} else if (SLIST_FIRST(head2) != NULL) { \
while (SLIST_NEXT(curelm, field) != NULL) \
curelm = SLIST_NEXT(curelm, field); \
SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
SLIST_INIT(head2); \
} \
} while (0)
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
#define SLIST_FIRST(head) ((head)->slh_first)
@ -217,7 +281,7 @@ struct { \
SLIST_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = SLIST_FIRST((head)); \
QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
while (SLIST_NEXT(curelm, field) != (elm)) \
curelm = SLIST_NEXT(curelm, field); \
SLIST_REMOVE_AFTER(curelm, field); \
@ -234,12 +298,20 @@ struct { \
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
} while (0)
#define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
*(prevp) = SLIST_NEXT(elm, field); \
TRASHIT((elm)->field.sle_next); \
} while (0)
#define SLIST_SWAP(head1, head2, type) do { \
struct type *swap_first = SLIST_FIRST(head1); \
QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
SLIST_FIRST(head1) = SLIST_FIRST(head2); \
SLIST_FIRST(head2) = swap_first; \
} while (0)
#define SLIST_END(head) NULL
/*
* Singly-linked Tail queue declarations.
*/
@ -249,6 +321,12 @@ struct name { \
struct type **stqh_last;/* addr of last next element */ \
}
#define STAILQ_CLASS_HEAD(name, type) \
struct name { \
class type *stqh_first; /* first element */ \
class type **stqh_last; /* addr of last next element */ \
}
#define STAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).stqh_first }
@ -257,6 +335,11 @@ struct { \
struct type *stqe_next; /* next element */ \
}
#define STAILQ_CLASS_ENTRY(type) \
struct { \
class type *stqe_next; /* next element */ \
}
/*
* Singly-linked Tail queue functions.
*/
@ -315,9 +398,10 @@ struct { \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
} while (0)
#define STAILQ_LAST(head, type, field) \
(STAILQ_EMPTY((head)) ? NULL : \
__containerof((head)->stqh_last, struct type, field.stqe_next))
#define STAILQ_LAST(head, type, field) \
(STAILQ_EMPTY((head)) ? NULL : \
__containerof((head)->stqh_last, \
QUEUE_TYPEOF(type), field.stqe_next))
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
@ -327,7 +411,7 @@ struct { \
STAILQ_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = STAILQ_FIRST((head)); \
QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
while (STAILQ_NEXT(curelm, field) != (elm)) \
curelm = STAILQ_NEXT(curelm, field); \
STAILQ_REMOVE_AFTER(head, curelm, field); \
@ -348,8 +432,8 @@ struct { \
} while (0)
#define STAILQ_SWAP(head1, head2, type) do { \
struct type *swap_first = STAILQ_FIRST(head1); \
struct type **swap_last = (head1)->stqh_last; \
QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
(head1)->stqh_last = (head2)->stqh_last; \
STAILQ_FIRST(head2) = swap_first; \
@ -360,6 +444,8 @@ struct { \
(head2)->stqh_last = &STAILQ_FIRST(head2); \
} while (0)
#define STAILQ_END(head) NULL
/*
* List declarations.
@ -369,6 +455,11 @@ struct name { \
struct type *lh_first; /* first element */ \
}
#define LIST_CLASS_HEAD(name, type) \
struct name { \
class type *lh_first; /* first element */ \
}
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
@ -378,11 +469,23 @@ struct { \
struct type **le_prev; /* address of previous next element */ \
}
#define LIST_CLASS_ENTRY(type) \
struct { \
class type *le_next; /* next element */ \
class type **le_prev; /* address of previous next element */ \
}
/*
* List functions.
*/
#if (defined(_KERNEL) && defined(INVARIANTS))
/*
* QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
*
* If the list is non-empty, validates that the first element of the list
* points back at 'head.'
*/
#define QMD_LIST_CHECK_HEAD(head, field) do { \
if (LIST_FIRST((head)) != NULL && \
LIST_FIRST((head))->field.le_prev != \
@ -390,6 +493,12 @@ struct { \
panic("Bad list head %p first->prev != head", (head)); \
} while (0)
/*
* QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
*
* If an element follows 'elm' in the list, validates that the next element
* points back at 'elm.'
*/
#define QMD_LIST_CHECK_NEXT(elm, field) do { \
if (LIST_NEXT((elm), field) != NULL && \
LIST_NEXT((elm), field)->field.le_prev != \
@ -397,6 +506,11 @@ struct { \
panic("Bad link elm %p next->prev != elm", (elm)); \
} while (0)
/*
* QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
*
* Validates that the previous element (or head of the list) points to 'elm.'
*/
#define QMD_LIST_CHECK_PREV(elm, field) do { \
if (*(elm)->field.le_prev != (elm)) \
panic("Bad link elm %p prev->next != elm", (elm)); \
@ -407,6 +521,23 @@ struct { \
#define QMD_LIST_CHECK_PREV(elm, field)
#endif /* (_KERNEL && INVARIANTS) */
#define LIST_CONCAT(head1, head2, type, field) do { \
QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
if (curelm == NULL) { \
if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
LIST_FIRST(head2)->field.le_prev = \
&LIST_FIRST((head1)); \
LIST_INIT(head2); \
} \
} else if (LIST_FIRST(head2) != NULL) { \
while (LIST_NEXT(curelm, field) != NULL) \
curelm = LIST_NEXT(curelm, field); \
LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
LIST_INIT(head2); \
} \
} while (0)
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
#define LIST_FIRST(head) ((head)->lh_first)
@ -462,9 +593,10 @@ struct { \
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#define LIST_PREV(elm, head, type, field) \
((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
__containerof((elm)->field.le_prev, struct type, field.le_next))
#define LIST_PREV(elm, head, type, field) \
((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
__containerof((elm)->field.le_prev, \
QUEUE_TYPEOF(type), field.le_next))
#define LIST_REMOVE(elm, field) do { \
QMD_SAVELINK(oldnext, (elm)->field.le_next); \
@ -480,7 +612,7 @@ struct { \
} while (0)
#define LIST_SWAP(head1, head2, type, field) do { \
struct type *swap_tmp = LIST_FIRST((head1)); \
QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
LIST_FIRST((head1)) = LIST_FIRST((head2)); \
LIST_FIRST((head2)) = swap_tmp; \
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
@ -489,6 +621,8 @@ struct { \
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
} while (0)
#define LIST_END(head) NULL
/*
* Tail queue declarations.
*/
@ -499,6 +633,13 @@ struct name { \
TRACEBUF \
}
#define TAILQ_CLASS_HEAD(name, type) \
struct name { \
class type *tqh_first; /* first element */ \
class type **tqh_last; /* addr of last next element */ \
TRACEBUF \
}
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
@ -509,10 +650,23 @@ struct { \
TRACEBUF \
}
#define TAILQ_CLASS_ENTRY(type) \
struct { \
class type *tqe_next; /* next element */ \
class type **tqe_prev; /* address of previous next element */ \
TRACEBUF \
}
/*
* Tail queue functions.
*/
#if (defined(_KERNEL) && defined(INVARIANTS))
/*
* QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
*
* If the tailq is non-empty, validates that the first element of the tailq
* points back at 'head.'
*/
#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
if (!TAILQ_EMPTY(head) && \
TAILQ_FIRST((head))->field.tqe_prev != \
@ -520,11 +674,22 @@ struct { \
panic("Bad tailq head %p first->prev != head", (head)); \
} while (0)
/*
* QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
*
* Validates that the tail of the tailq is a pointer to pointer to NULL.
*/
#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
if (*(head)->tqh_last != NULL) \
panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
} while (0)
/*
* QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
*
* If an element follows 'elm' in the tailq, validates that the next element
* points back at 'elm.'
*/
#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
if (TAILQ_NEXT((elm), field) != NULL && \
TAILQ_NEXT((elm), field)->field.tqe_prev != \
@ -532,6 +697,11 @@ struct { \
panic("Bad link elm %p next->prev != elm", (elm)); \
} while (0)
/*
* QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
*
* Validates that the previous element (or head of the tailq) points to 'elm.'
*/
#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
if (*(elm)->field.tqe_prev != (elm)) \
panic("Bad link elm %p prev->next != elm", (elm)); \
@ -616,7 +786,7 @@ struct { \
TAILQ_NEXT((listelm), field) = (elm); \
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
QMD_TRACE_ELEM(&(elm)->field); \
QMD_TRACE_ELEM(&listelm->field); \
QMD_TRACE_ELEM(&(listelm)->field); \
} while (0)
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
@ -626,7 +796,7 @@ struct { \
*(listelm)->field.tqe_prev = (elm); \
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
QMD_TRACE_ELEM(&(elm)->field); \
QMD_TRACE_ELEM(&listelm->field); \
QMD_TRACE_ELEM(&(listelm)->field); \
} while (0)
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
@ -655,11 +825,25 @@ struct { \
#define TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
/*
* The FAST function is fast in that it causes no data access other
* then the access to the head. The standard LAST function above
* will cause a data access of both the element you want and
* the previous element. FAST is very useful for instances when
* you may want to prefetch the last data element.
*/
#define TAILQ_LAST_FAST(head, type, field) \
(TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#define TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#define TAILQ_PREV_FAST(elm, head, type, field) \
((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \
__containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next))
#define TAILQ_REMOVE(head, elm, field) do { \
QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
@ -679,8 +863,8 @@ struct { \
} while (0)
#define TAILQ_SWAP(head1, head2, type, field) do { \
struct type *swap_first = (head1)->tqh_first; \
struct type **swap_last = (head1)->tqh_last; \
QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
(head1)->tqh_first = (head2)->tqh_first; \
(head1)->tqh_last = (head2)->tqh_last; \
(head2)->tqh_first = swap_first; \
@ -695,4 +879,6 @@ struct { \
(head2)->tqh_last = &(head2)->tqh_first; \
} while (0)
#define TAILQ_END(head) NULL
#endif /* !LIBBSD_SYS_QUEUE_H */

View File

@ -385,7 +385,7 @@ struct { \
#define RB_PROTOTYPE(name, type, field, cmp) \
RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
RB_PROTOTYPE_INTERNAL(name, type, field, cmp, LIBBSD_UNUSED static)
#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
@ -404,7 +404,7 @@ attr struct type *name##_RB_MINMAX(struct name *, int); \
#define RB_GENERATE(name, type, field, cmp) \
RB_GENERATE_INTERNAL(name, type, field, cmp,)
#define RB_GENERATE_STATIC(name, type, field, cmp) \
RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
RB_GENERATE_INTERNAL(name, type, field, cmp, LIBBSD_UNUSED static)
#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
attr void \
name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \

View File

@ -58,4 +58,17 @@ time_t _long_to_time(long tlong);
int _time_to_int(time_t t);
time_t _int_to_time(int tint);
#if LIBBSD_SYS_TIME_BITS == 32 && LIBBSD_SYS_HAS_TIME64
#if defined _TIME_BITS && _TIME_BITS == 64
time_t LIBBSD_REDIRECT(_time32_to_time, (int32_t t32), _time32_to_time_time64);
int32_t LIBBSD_REDIRECT(_time_to_time32, (time_t t), _time_to_time32_time64);
time_t LIBBSD_REDIRECT(_time64_to_time, (int64_t t64), _time64_to_time_time64);
int64_t LIBBSD_REDIRECT(_time_to_time64, (time_t t), _time_to_time64_time64);
long LIBBSD_REDIRECT(_time_to_long, (time_t t), _time_to_long_time64);
time_t LIBBSD_REDIRECT(_long_to_time, (long tlong), _long_to_time_time64);
int LIBBSD_REDIRECT(_time_to_int, (time_t t), _time_to_int_time64);
time_t LIBBSD_REDIRECT(_int_to_time, (int tint), _int_to_time_time64);
#endif
#endif
#endif /* LIBBSD_TIMECONV_H */

View File

@ -47,6 +47,7 @@
#endif
__BEGIN_DECLS
#if !defined(__APPLE__)
extern int optreset;
#ifdef LIBBSD_OVERLAY
@ -55,11 +56,16 @@ extern int optreset;
#endif
int bsd_getopt(int argc, char * const argv[], const char *shortopts);
#endif
mode_t getmode(const void *set, mode_t mode);
void *setmode(const char *mode_str);
#if !defined(__GLIBC__) || \
!__GLIBC_PREREQ(2, 34) || \
!defined(_DEFAULT_SOURCE)
void closefrom(int lowfd);
#endif
/* Compatibility with sendmail implementations. */
#define initsetproctitle(c, a, e) setproctitle_init((c), (a), (e))

View File

@ -93,14 +93,17 @@
* existing one in OpenBSD and Freedesktop's libbsd (the former having existed
* for over ten years). Despite this incompatibility being reported during
* development (see http://gnats.netbsd.org/44977) they still shipped it.
* Even more unfortunately FreeBSD and later MacOS picked up this incompatible
* Even more unfortunately FreeBSD and later macOS picked up this incompatible
* implementation.
*
* Provide both implementations and default for now on the historical one to
* avoid breakage, we will switch to the NetBSD one in libbsd 0.10.0 or so.
* Define LIBBSD_NETBSD_VIS to switch to the NetBSD one now.
* Define LIBBSD_OPENBSD_VIS to keep using the OpenBSD one.
*/
#ifndef LIBBSD_NETBSD_VIS
#if defined(LIBBSD_OPENBSD_VIS)
#undef LIBBSD_NETBSD_VIS
#elif !defined(LIBBSD_NETBSD_VIS)
#warning "NetBSD added incompatible strnvis() and strnunvis(), please see <bsd/vis.h> for more detils."
#endif

View File

@ -50,8 +50,10 @@
__BEGIN_DECLS
wchar_t *fgetwln(FILE *stream, size_t *len);
#if !defined(__APPLE__)
size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
#endif
__END_DECLS
#endif

2
m4/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.m4
!libbsd*.m4

104
m4/libbsd-compiler.m4 Normal file
View File

@ -0,0 +1,104 @@
# Copyright © 2021 Guillem Jover <guillem@hadrons.org>
# LIBBSD_CHECK_COMPILER_FLAG
# --------------------------
AC_DEFUN([LIBBSD_CHECK_COMPILER_FLAG], [
AS_VAR_PUSHDEF([libbsd_varname_cache], [libbsd_cv_cflags_$1])
AC_CACHE_CHECK([whether $CC accepts $1], [libbsd_varname_cache], [
m4_define([libbsd_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
AS_VAR_COPY([libbsd_save_CFLAGS], [CFLAGS])
AS_VAR_SET([CFLAGS], ["-Werror libbsd_check_flag"])
AC_COMPILE_IFELSE([
AC_LANG_SOURCE([[]])
], [
AS_VAR_SET([libbsd_varname_cache], [yes])
], [
AS_VAR_SET([libbsd_varname_cache], [no])
])
AS_VAR_COPY([CFLAGS], [libbsd_save_CFLAGS])
])
AS_VAR_IF([libbsd_varname_cache], [yes], [
AS_VAR_APPEND([LIBBSD_COMPILER_FLAGS], [" $1"])
])
AS_VAR_POPDEF([libbsd_varname_cache])
])
# LIBBSD_HAS_GNU_INIT_ARRAY
# -------------------------
AC_DEFUN([LIBBSD_HAS_GNU_INIT_ARRAY], [
AC_CACHE_CHECK([for GNU .init_array section support],
[libbsd_cv_gnu_init_array_support], [
AC_RUN_IFELSE([
AC_LANG_SOURCE([[
static int rc = 1;
static void init(int argc) { if (argc == 1) rc = 0; }
void (*init_func)(int argc) __attribute__((__section__(".init_array"), __used__)) = init;
int main() { return rc; }
]])
], [
libbsd_cv_gnu_init_array_support=yes
], [
libbsd_cv_gnu_init_array_support=no
], [
AC_PREPROC_IFELSE([
AC_LANG_SOURCE([[
/* Look for a known libc that supports .init_array with the GNU extension
* to pass main() arguments to the init functions. */
#include <stdlib.h>
#if defined __GLIBC_PREREQ
# if __GLIBC_PREREQ(2, 4)
/* glibc supports GNU .init_array since 2.4. */
# else
# error glibc does not support GNU .init_array
# endif
#else
/*
* Basic SysV ABI .init_array support, init functions do not get arguments:
* - Bionic since its inception.
* - uClibc since 0.9.29.
*/
# error unknown whether libc supports GNU .init_array
#endif
]])
], [
libbsd_cv_gnu_init_array_support=yes
], [
libbsd_cv_gnu_init_array_support=no
])
])
])
AM_CONDITIONAL([BUILD_LIBBSD_CTOR],
[test "$libbsd_cv_gnu_init_array_support" = yes])
])
# LIBBSD_SELECT_API(name, desc)
# -----------------
AC_DEFUN([LIBBSD_SELECT_API], [
AS_IF([test -z "$AS_TR_SH([api_$1])"], [
AC_MSG_ERROR([missing API selection for $1])
], [test "$AS_TR_SH([api_$1])" = "unknown"], [
AC_MSG_ERROR([unknown ABI selection for $1])
], [test "$AS_TR_SH([api_$1])" = "yes"], [
AC_DEFINE(AS_TR_CPP([LIBBSD_API_$1]), [1], [Provide API for $2])
], [
AC_DEFINE(AS_TR_CPP([LIBBSD_API_$1]), [0])
])
AM_CONDITIONAL(AS_TR_CPP([API_$1]),
[test "x$AS_TR_SH([api_$1])" = "xyes"])
])
# LIBBSD_SELECT_ABI(name, desc)
# -----------------
AC_DEFUN([LIBBSD_SELECT_ABI], [
AS_IF([test -z "$AS_TR_SH([abi_$1])"], [
AC_MSG_ERROR([missing ABI selection for $1])
], [test "$AS_TR_SH([abi_$1])" = "unknown"], [
AC_MSG_ERROR([unknown ABI selection for $1])
], [test "$AS_TR_SH([abi_$1])" = "yes"], [
AC_DEFINE(AS_TR_CPP([LIBBSD_ABI_$1]), [1], [Provide ABI for $2])
], [
AC_DEFINE(AS_TR_CPP([LIBBSD_ABI_$1]), [0])
])
AM_CONDITIONAL(AS_TR_CPP([ABI_$1]),
[test "x$AS_TR_SH([abi_$1])" = "xyes"])
])

57
m4/libbsd-funcs.m4 Normal file
View File

@ -0,0 +1,57 @@
# Copyright © 2011-2024 Guillem Jover <guillem@hadrons.org>
# LIBBSD_CHECK_PROGNAME
# ---------------------
AC_DEFUN([LIBBSD_CHECK_PROGNAME], [
AC_MSG_CHECKING([for program_invocation_short_name])
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[
#include <errno.h>
]], [[
const char *p = program_invocation_short_name;
]])
], [
AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
[Define to 1 if you have program_invocation_short_name])
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
])
AC_MSG_CHECKING([for __progname])
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[
#include <stdio.h>
extern char *__progname;
]], [[
printf("%s", __progname);
]])
], [
AC_DEFINE([HAVE___PROGNAME], [1], [Define to 1 if you have __progname])
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
])
])
# LIBBSD_CHECK_REGISTER_ATFORK
# ----------------------------
AC_DEFUN([LIBBSD_CHECK_REGISTER_ATFORK], [
AC_MSG_CHECKING([for __register_atfork])
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[
#include <stddef.h>
extern void *__dso_handle;
extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
]], [[
__register_atfork(NULL, NULL, NULL, __dso_handle);
]])
], [
AC_DEFINE([HAVE___REGISTER_ATFORK], [1],
[Define to 1 if you have __register_atfork])
AC_MSG_RESULT([yes])
], [
LIBBSD_LIBS="$LIBBSD_LIBS -pthread"
AC_MSG_RESULT([no])
])
])

61
m4/libbsd-large.m4 Normal file
View File

@ -0,0 +1,61 @@
# Copyright © 2024 Guillem Jover <guillem@hadrons.org>
# LIBBSD_SYS_TIME64
# -----------------
# Check for availability of time64 support.
AC_DEFUN([LIBBSD_SYS_TIME64], [
# Check the default time_t size.
AC_CHECK_SIZEOF([time_t], [], [[
#undef _TIME_BITS
#include <time.h>
]])
AS_IF([test $ac_cv_sizeof_time_t -eq 8], [
libbsd_sys_time_bits=64
], [
libbsd_sys_time_bits=32
])
AC_DEFINE_UNQUOTED([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits],
[The number of bits for the default system time_t ABI])
AC_SUBST([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits])
AS_UNSET([ac_cv_sizeof_time_t])
AM_CONDITIONAL([LIBBSD_SYS_IS_TIME32], [test "$libbsd_sys_time_bits" -eq 32])
# Check the whether the system supports 64-bit time_t.
AC_CHECK_SIZEOF([time_t], [], [[
#define _FILE_OFFSET_BITS 64
#define _TIME_BITS 64
#include <time.h>
]])
AS_IF([test $ac_cv_sizeof_time_t -eq 8], [
libbsd_sys_has_time64=1
], [
libbsd_sys_has_time64=0
])
AC_DEFINE_UNQUOTED([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64],
[Enable if the system supports 64-bit time_t])
AC_SUBST([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64])
AM_CONDITIONAL([LIBBSD_SYS_HAS_TIME64], [test "$libbsd_sys_has_time64" -eq 1])
AS_IF([test "$libbsd_sys_time_bits" -eq 32 && \
test "$libbsd_sys_has_time64" -eq 1], [
abi_time64=yes
], [
abi_time64=no
])
LIBBSD_SELECT_ABI([time64], [explicit time64 time_t support])
AC_CHECK_SIZEOF([off_t], [], [[
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
]])
AS_IF([test $ac_cv_sizeof_off_t = 8], [
libbsd_sys_has_lfs=1
], [
libbsd_sys_has_lfs=0
])
AS_IF([test $libbsd_sys_has_lfs -eq 1 && \
test $libbsd_sys_time_bits -eq 32 && \
test $ac_cv_sizeof_time_t -eq 8], [
AC_DEFINE([_TIME_BITS], [64], [Enable 64-bit time_t support])
])
])

27
m4/libbsd-linker.m4 Normal file
View File

@ -0,0 +1,27 @@
# Copyright © 2019, 2022 Guillem Jover <guillem@hadrons.org>
# LIBBSD_LINKER_VERSION_SCRIPT
# ----------------------------
AC_DEFUN([LIBBSD_LINKER_VERSION_SCRIPT], [
AC_CACHE_CHECK([if ld supports --version-script flag],
[libbsd_cv_version_script], [
echo "{ global: symbol; local: *; };" >conftest.map
save_LDFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -Wl,--version-script=conftest.map"
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[
extern int symbol(void);
int symbol(void) { return 0; }
]], [[
]])
], [
libbsd_cv_version_script=yes
], [
libbsd_cv_version_script=no
])
LDFLAGS="$save_LDFLAGS"
rm -f conftest.map
])
AM_CONDITIONAL([HAVE_LINKER_VERSION_SCRIPT],
[test "x$libbsd_cv_version_script" = "xyes"])
])

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

1
man/LIST_CLASS_HEAD.3bsd Normal file
View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -2,18 +2,20 @@
EXTRA_DIST = \
mdX.3bsd \
$(nil)
# EOL
CLEANFILES = \
md5.3bsd \
$(nil)
# EOL
SED_MD5_SUBST = -e 's/mdX/md5/g' -e 's/mdY/md4/g' -e 's/MDX/MD5/g'
md5.3bsd: $(srcdir)/mdX.3bsd
$(AM_V_GEN) sed $(SED_MD5_SUBST) $< > $@
$(AM_V_GEN) $(SED) $(SED_MD5_SUBST) $< >$@
dist_man_MANS = \
LIST_CLASS_ENTRY.3bsd \
LIST_CLASS_HEAD.3bsd \
LIST_EMPTY.3bsd \
LIST_ENTRY.3bsd \
LIST_FIRST.3bsd \
@ -54,6 +56,8 @@ dist_man_MANS = \
RB_REMOVE.3bsd \
RB_RIGHT.3bsd \
RB_ROOT.3bsd \
SLIST_CLASS_ENTRY.3bsd \
SLIST_CLASS_HEAD.3bsd \
SLIST_EMPTY.3bsd \
SLIST_ENTRY.3bsd \
SLIST_FIRST.3bsd \
@ -70,6 +74,7 @@ dist_man_MANS = \
SLIST_REMOVE.3bsd \
SLIST_REMOVE_AFTER.3bsd \
SLIST_REMOVE_HEAD.3bsd \
SLIST_REMOVE_PREVPTR.3bsd \
SLIST_SWAP.3bsd \
SPLAY_EMPTY.3bsd \
SPLAY_ENTRY.3bsd \
@ -88,6 +93,8 @@ dist_man_MANS = \
SPLAY_REMOVE.3bsd \
SPLAY_RIGHT.3bsd \
SPLAY_ROOT.3bsd \
STAILQ_CLASS_ENTRY.3bsd \
STAILQ_CLASS_HEAD.3bsd \
STAILQ_CONCAT.3bsd \
STAILQ_EMPTY.3bsd \
STAILQ_ENTRY.3bsd \
@ -108,6 +115,8 @@ dist_man_MANS = \
STAILQ_REMOVE_AFTER.3bsd \
STAILQ_REMOVE_HEAD.3bsd \
STAILQ_SWAP.3bsd \
TAILQ_CLASS_ENTRY.3bsd \
TAILQ_CLASS_HEAD.3bsd \
TAILQ_CONCAT.3bsd \
TAILQ_EMPTY.3bsd \
TAILQ_ENTRY.3bsd \
@ -132,13 +141,6 @@ dist_man_MANS = \
TAILQ_PREV.3bsd \
TAILQ_REMOVE.3bsd \
TAILQ_SWAP.3bsd \
TIMESPEC_TO_TIMEVAL.3bsd \
TIMEVAL_TO_TIMESPEC.3bsd \
arc4random.3bsd \
arc4random_addrandom.3bsd \
arc4random_buf.3bsd \
arc4random_stir.3bsd \
arc4random_uniform.3bsd \
be16dec.3bsd \
be16enc.3bsd \
be32dec.3bsd \
@ -157,27 +159,6 @@ dist_man_MANS = \
bitstr_size.3bsd \
bitstring.3bsd \
byteorder.3bsd \
closefrom.3bsd \
dehumanize_number.3bsd \
errc.3bsd \
expand_number.3bsd \
explicit_bzero.3bsd \
fgetln.3bsd \
fgetwln.3bsd \
flopen.3bsd \
freezero.3bsd \
fmtcheck.3bsd \
fparseln.3bsd \
fpurge.3bsd \
funopen.3bsd \
getbsize.3bsd \
getmode.3bsd \
getpeereid.3bsd \
getprogname.3bsd \
gid_from_group.3bsd \
group_from_gid.3bsd \
heapsort.3bsd \
humanize_number.3bsd \
le16dec.3bsd \
le16enc.3bsd \
le32dec.3bsd \
@ -185,61 +166,270 @@ dist_man_MANS = \
le64dec.3bsd \
le64enc.3bsd \
libbsd.7 \
md5.3bsd \
mergesort.3bsd \
nlist.3bsd \
pidfile.3bsd \
pidfile_close.3bsd \
pidfile_open.3bsd \
pidfile_remove.3bsd \
pidfile_write.3bsd \
pwcache.3bsd \
queue.3bsd \
radixsort.3bsd \
readpassphrase.3bsd \
reallocarray.3bsd \
reallocf.3bsd \
recallocarray.3bsd \
setmode.3bsd \
setproctitle.3bsd \
setproctitle_init.3bsd \
setprogname.3bsd \
sl_add.3bsd \
sl_delete.3bsd \
sl_find.3bsd \
sl_free.3bsd \
sl_init.3bsd \
sradixsort.3bsd \
stringlist.3bsd \
strlcat.3bsd \
strlcpy.3bsd \
strmode.3bsd \
strnstr.3bsd \
strnunvis.3bsd \
strnvis.3bsd \
strtoi.3bsd \
strtonum.3bsd \
strtou.3bsd \
strunvis.3bsd \
strvis.3bsd \
strvisx.3bsd \
timeradd.3bsd \
timerclear.3bsd \
timercmp.3bsd \
timerisset.3bsd \
timersub.3bsd \
timespec.3bsd \
timespecadd.3bsd \
timespecclear.3bsd \
timespeccmp.3bsd \
timespecisset.3bsd \
timespecsub.3bsd \
timeval.3bsd \
tree.3bsd \
# EOL
if API_TIME_MACROS
dist_man_MANS += \
TIMESPEC_TO_TIMEVAL.3bsd \
TIMEVAL_TO_TIMESPEC.3bsd \
# EOL
endif
if ABI_ACCMODE
dist_man_MANS += \
getmode.3bsd \
setmode.3bsd \
# EOL
endif
if ABI_ARC4RANDOM
dist_man_MANS += \
arc4random.3bsd \
arc4random_addrandom.3bsd \
arc4random_buf.3bsd \
arc4random_stir.3bsd \
arc4random_uniform.3bsd \
# EOL
endif
if ABI_CLOSEFROM
dist_man_MANS += \
closefrom.3bsd \
# EOL
endif
if ABI_ERRC
dist_man_MANS += \
errc.3bsd \
verrc.3bsd \
vwarnc.3bsd \
warnc.3bsd \
# EOL
endif
if ABI_EXPAND_NUMBER
dist_man_MANS += \
expand_number.3bsd \
# EOL
endif
if ABI_EXPLICIT_BZERO
dist_man_MANS += \
explicit_bzero.3bsd \
# EOL
endif
if ABI_FGETLN
dist_man_MANS += \
fgetln.3bsd \
fgetwln.3bsd \
fparseln.3bsd \
# EOL
endif
if ABI_FLOPEN
dist_man_MANS += \
flopen.3bsd \
# EOL
endif
if ABI_FMTCHECK
dist_man_MANS += \
fmtcheck.3bsd \
# EOL
endif
if ABI_FPURGE
dist_man_MANS += \
fpurge.3bsd \
# EOL
endif
if ABI_FREEZERO
dist_man_MANS += \
freezero.3bsd \
# EOL
endif
if ABI_FUNOPEN
dist_man_MANS += \
funopen.3bsd \
# EOL
endif
if ABI_GETBSIZE
dist_man_MANS += \
getbsize.3bsd \
# EOL
endif
if ABI_GETPEEREID
dist_man_MANS += \
getpeereid.3bsd \
# EOL
endif
if ABI_HUMANIZE_NUMBER
dist_man_MANS += \
dehumanize_number.3bsd \
humanize_number.3bsd \
# EOL
endif
if ABI_MD5
dist_man_MANS += \
md5.3bsd \
# EOL
endif
if ABI_NLIST
dist_man_MANS += \
nlist.3bsd \
# EOL
endif
if ABI_PIDFILE
dist_man_MANS += \
pidfile.3bsd \
pidfile_close.3bsd \
pidfile_open.3bsd \
pidfile_remove.3bsd \
pidfile_write.3bsd \
# EOL
endif
if ABI_PROCTITLE
dist_man_MANS += \
setproctitle.3bsd \
setproctitle_init.3bsd \
# EOL
endif
if ABI_PROGNAME
dist_man_MANS += \
getprogname.3bsd \
setprogname.3bsd \
# EOL
endif
if ABI_PWCACHE
dist_man_MANS += \
pwcache.3bsd \
gid_from_group.3bsd \
uid_from_user.3bsd \
unvis.3bsd \
# EOL
if ABI_NAME_FROM_ID
dist_man_MANS += \
group_from_gid.3bsd \
user_from_uid.3bsd \
# EOL
endif
endif
if ABI_READPASSPHRASE
dist_man_MANS += \
readpassphrase.3bsd \
# EOL
endif
if ABI_REALLOCARRAY
dist_man_MANS += \
reallocarray.3bsd \
# EOL
endif
if ABI_REALLOCF
dist_man_MANS += \
reallocf.3bsd \
# EOL
endif
if ABI_RECALLOCARRAY
dist_man_MANS += \
recallocarray.3bsd \
# EOL
endif
if ABI_SORT
dist_man_MANS += \
heapsort.3bsd \
mergesort.3bsd \
radixsort.3bsd \
sradixsort.3bsd \
# EOL
endif
if ABI_STRINGLIST
dist_man_MANS += \
sl_add.3bsd \
sl_delete.3bsd \
sl_find.3bsd \
sl_free.3bsd \
sl_init.3bsd \
stringlist.3bsd \
# EOL
endif
if ABI_STRL
dist_man_MANS += \
strlcat.3bsd \
strlcpy.3bsd \
# EOL
endif
if ABI_STRMODE
dist_man_MANS += \
strmode.3bsd \
# EOL
endif
if ABI_STRNSTR
dist_man_MANS += \
strnstr.3bsd \
# EOL
endif
if ABI_STRTOX
dist_man_MANS += \
strtoi.3bsd \
strtou.3bsd \
# EOL
endif
if ABI_STRTONUM
dist_man_MANS += \
strtonum.3bsd \
# EOL
endif
if ABI_VIS
dist_man_MANS += \
strnunvis.3bsd \
strnvis.3bsd \
strunvis.3bsd \
strvis.3bsd \
strvisx.3bsd \
unvis.3bsd \
vis.3bsd \
# EOL
endif
if ABI_WCSL
dist_man_MANS += \
wcslcat.3bsd \
wcslcpy.3bsd \
$(nil)
# EOL
endif

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -0,0 +1 @@
.so man3/queue.3bsd

View File

@ -1 +1,100 @@
.so man3/timeval.3bsd
.\" $NetBSD: timeval.3,v 1.12 2011/04/12 08:39:26 jruoho Exp $
.\"
.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jukka Ruohonen.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd April 12, 2011
.Dt TIMEVAL_TO_TIMESPEC 3bsd
.Os
.Sh NAME
.Nm TIMEVAL_TO_TIMESPEC ,
.Nm TIMESPEC_TO_TIMEVAL
.Nd time structures conversion macros
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
.Lb libbsd
.Sh SYNOPSIS
.In sys/time.h
(See
.Xr libbsd 7
for include usage.)
.Ft void
.Fn TIMEVAL_TO_TIMESPEC "struct timeval *tv" "struct timespec *ts"
.Ft void
.Fn TIMESPEC_TO_TIMEVAL "struct timeval *tv" "struct timespec *ts"
.Sh DESCRIPTION
The
.Va timeval
structure represents elapsed time, in whole seconds,
and the rest of the elapsed time in microseconds.
.Pp
The
.Va timespec
structure represents elapsed time, in whole seconds,
and the rest of the elapsed time in nanoseconds.
.Pp
A microsecond is equal to one millionth of a second,
1000 nanoseconds, or 1/1000 milliseconds.
To ease the conversions, the macros
.Fn TIMEVAL_TO_TIMESPEC
and
.Fn TIMESPEC_TO_TIMEVAL
can be used to convert between
.Em struct timeval
and
.Em struct timespec .
.Sh EXAMPLES
It can be stressed that the traditional
.Tn UNIX
.Va timeval
and
.Va timespec
structures represent elapsed time, measured by the system clock.
The following sketch implements a function suitable
for use in a context where the
.Va timespec
structure is required for a conditional timeout:
.Bd -literal -offset indent
static void
example(struct timespec *spec, time_t minutes)
{
struct timeval elapsed;
(void)gettimeofday(&elapsed, NULL);
TIMEVAL_TO_TIMESPEC(&elapsed, spec);
/* Add the offset for timeout in minutes. */
spec->tv_sec = spec->tv_sec + minutes * 60;
}
.Ed
.Pp
A better alternative would use the more precise
.Xr clock_gettime 2 .
.Sh SEE ALSO
.Xr timeradd 3bsd

View File

@ -1 +1 @@
.so man3/timeval.3bsd
.so man3/TIMESPEC_TO_TIMEVAL.3bsd

View File

@ -1,8 +1,11 @@
.\" $OpenBSD: arc4random.3,v 1.34 2014/07/19 16:11:16 naddy Exp $
.\" $NetBSD: arc4random.3,v 1.21 2016/07/15 21:19:19 wiz Exp $
.\"
.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
.\" Copyright (c) 2014 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Taylor R. Campbell.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
@ -11,35 +14,29 @@
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by Niels Provos.
.\" 4. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\" Manual page, using -mandoc macros
.\"
.Dd $Mdocdate: July 19 2014 $
.Dt ARC4RANDOM 3bsd
.Dd November 16, 2014
.Dt arc4random 3bsd
.Os
.Sh NAME
.Nm arc4random ,
.Nm arc4random_buf ,
.Nm arc4random_uniform ,
.Nm arc4random_buf ,
.Nm arc4random_stir ,
.Nm arc4random_addrandom
.Nd arc4 random number generator
.Nd random number generator
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@ -51,83 +48,208 @@
for include usage.)
.Ft uint32_t
.Fn arc4random "void"
.Ft void
.Fn arc4random_buf "void *buf" "size_t nbytes"
.Ft uint32_t
.Fn arc4random_uniform "uint32_t upper_bound"
.Fn arc4random_uniform "uint32_t bound"
.Ft void
.Fn arc4random_buf "void *buf" "size_t len"
.Ft void
.Fn arc4random_stir "void"
.Ft void
.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
.Fn arc4random_addrandom "unsigned char *buf" "int len"
.Sh DESCRIPTION
This family of functions provides higher quality data than those
described in
.Xr rand 3 ,
The
.Nm
family of functions provides a cryptographic pseudorandom number
generator automatically seeded from the system entropy pool and safe to
use from multiple threads.
.Nm
is designed to prevent an adversary from guessing outputs,
unlike
.Xr rand 3
and
.Xr random 3 ,
and
.Xr rand48 3 .
and is faster and more convenient than reading from
.Pa /dev/urandom
directly.
.Pp
Use of these functions is encouraged for almost all random number
consumption because the other interfaces are deficient in either
quality, portability, standardization, or availability.
These functions can be called in almost all coding environments,
including
.Xr pthreads 3
and
.Xr chroot 2 .
.Pp
High quality 32-bit pseudo-random numbers are generated very quickly.
On each call, a cryptographic pseudo-random number generator is used
to generate a new result.
One data pool is used for all consumers in a process, so that consumption
under program flow can act as additional stirring.
The subsystem is re-seeded from the kernel random number subsystem using
.Xr getentropy 2
on a regular basis, and also upon
.Xr fork 2 .
.Pp
The
.Fn arc4random
function returns a single 32-bit value.
.Pp
The
.Fn arc4random_buf
function fills the region
.Fa buf
of length
.Fa nbytes
with random data.
returns an integer in [0, 2^32) chosen independently with uniform
distribution.
.Pp
.Fn arc4random_uniform
will return a single 32-bit value, uniformly distributed but less than
.Fa upper_bound .
This is recommended over constructions like
.Dq Li arc4random() % upper_bound
as it avoids "modulo bias" when the upper bound is not a power of two.
In the worst case, this function may consume multiple iterations
to ensure uniformity; see the source code to understand the problem
and solution.
returns an integer in [0,
.Fa bound )
chosen independently with uniform distribution.
.Pp
.Fn arc4random_buf
stores
.Fa len
bytes into the memory pointed to by
.Fa buf ,
each byte chosen independently from [0, 256) with uniform
distribution.
.Pp
.Fn arc4random_stir
draws entropy from the operating system and incorporates it into the
library's PRNG state to influence future outputs.
.Pp
.Fn arc4random_addrandom
incorporates
.Fa len
bytes, which must be nonnegative, from the buffer
.Fa buf ,
into the library's PRNG state to influence future outputs.
.Pp
It is not necessary for an application to call
.Fn arc4random_stir
or
.Fn arc4random_addrandom
before calling other
.Nm
functions.
The first call to any
.Nm
function will initialize the PRNG state unpredictably from the system
entropy pool.
.Sh SECURITY MODEL
The
.Fn arc4random_stir
function reads data from
.Xr getentropy 2
and uses it to re-seed the subsystem via
.Fn arc4random_addrandom .
.Nm
functions provide the following security properties against three
different classes of attackers, assuming enough entropy is provided by
the operating system:
.Bl -enum -offset abcd
.It
An attacker who has seen some outputs of any of the
.Nm
functions cannot predict past or future unseen outputs.
.It
An attacker who has seen the library's PRNG state in memory cannot
predict past outputs.
.It
An attacker who has seen one process's PRNG state cannot predict past
or future outputs in other processes, particularly its parent or
siblings.
.El
.Pp
One
.Sq output
means the result of any single request to an
.Nm
function, no matter how short it is.
.Pp
The second property is sometimes called
.Sq forward secrecy ,
.Sq backtracking resistance ,
or
.Sq key erasure after each output .
.Sh IMPLEMENTATION NOTES
The
.Nm
functions are currently implemented using the ChaCha20 pseudorandom
function family.
For any 32-byte string
.Fa s ,
.Pf ChaCha20_ Fa s
is a function from 16-byte strings to 64-byte strings.
It is conjectured that if
.Fa s
is chosen with uniform distribution, then the distribution on
.Pf ChaCha20_ Fa s
is indistinguishable to a computationally bounded adversary from a
uniform distribution on all functions from 16-byte strings to 64-byte
strings.
.Pp
The PRNG state is a 32-byte ChaCha20 key
.Fa s .
Each request to
an
.Nm
function
.Bl -bullet -offset abcd -compact
.It
computes the 64-byte quantity
.Fa x
=
.Pf ChaCha20_ Fa s Ns Pq 0 ,
.It
splits
.Fa x
into two 32-byte quantities
.Fa s'
and
.Fa k ,
.It
replaces
.Fa s
by
.Fa s' ,
and
.It
uses
.Fa k
as output.
.El
.Pp
There is no need to call
.Fn arc4random_stir
before using
.Fn arc4random
functions family, since
they automatically initialize themselves.
.Sh RETURN VALUES
These functions are always successful, and no return value is
reserved to indicate an error.
yields the first four bytes of
.Fa k
as output directly.
.Fn arc4random_buf
either yields up to 32 bytes of
.Fa k
as output directly, or, for longer
requests, uses
.Fa k
as a ChaCha20 key and yields the concatenation
.Pf ChaCha20_ Fa k Ns Pq 0
||
.Pf ChaCha20_ Fa k Ns Pq 1
|| ... as output.
.Fn arc4random_uniform
repeats
.Fn arc4random
until it obtains an integer in [2^32 %
.Fa bound ,
2^32), and reduces that modulo
.Fa bound .
.Pp
The PRNG state is per-thread, unless memory allocation fails inside the
library, in which case some threads may share global PRNG state with a
mutex.
The global PRNG state is zeroed on fork in the parent via
.Xr pthread_atfork 3 ,
and the per-thread PRNG state is zeroed on fork in the child via
.Xr minherit 2
with
.Dv MAP_INHERIT_ZERO ,
so that the child cannot reuse or see the parent's PRNG state.
The PRNG state is reseeded automatically from the system entropy pool
on the first use of an
.Nm
function after zeroing.
.Pp
The first use of an
.Nm
function may abort the process in the highly unlikely event that
library initialization necessary to implement the security model fails.
Additionally,
.Fn arc4random_stir
and
.Fn arc4random_addrandom
may abort the process in the highly unlikely event that the operating
system fails to provide entropy.
.Sh SEE ALSO
.Xr rand 3 ,
.Xr rand48 3 ,
.Xr random 3
.Xr random 3 ,
.Xr rnd 4 ,
.Xr cprng 9
.Rs
.%A Daniel J. Bernstein
.%T ChaCha, a variant of Salsa20
.%D 2008-01-28
.%O Document ID: 4027b5256e17b9796842e6d0f68b0b5e
.%U http://cr.yp.to/papers.html#chacha
.Re
.Sh HISTORY
These functions first appeared in
.Ox 2.1 ,
@ -135,12 +257,85 @@ These functions first appeared in
.Nx 1.6 ,
and
.Dx 1.0 .
The functions
.Fn arc4random ,
.Fn arc4random_buf
and
.Fn arc4random_uniform
appeared in glibc 2.36.
.Sh BUGS
There is no way to get deterministic, reproducible results out of
.Nm
for testing purposes.
.Pp
The original version of this random number generator used the
RC4 (also known as ARC4) algorithm.
In
.Ox 5.5
it was replaced with the ChaCha20 cipher, and it may be replaced
again in the future as cryptographic techniques advance.
A good mnemonic is
.Dq A Replacement Call for Random .
The name
.Sq arc4random
was chosen for hysterical raisins -- it was originally implemented
using the RC4 stream cipher, which has been known since shortly after
it was published in 1994 to have observable biases in the output, and
is now known to be broken badly enough to admit practical attacks in
the real world.
.\" Bob Jenkins, sci.crypt post dated 1994-09-16, message-id
.\" <359qjg$55v$1@mhadg.production.compuserve.com>,
.\" https://groups.google.com/d/msg/sci.crypt/JsO3xEATGFA/-wO4ttv7BCYJ
.\"
.\" Andrew Roos, `A Class of Weak Keys in the RC4 Stream Cipher',
.\" sci.crypt posts dated 1995-09-22, message-ids
.\" <43u1eh$1j3@hermes.is.co.za> and <44ebge$llf@hermes.is.co.za>.
.\"
.\" Paul Crowley, `Small bias in RC4 experimentally verified', March
.\" 1998, http://www.ciphergoth.org/crypto/rc4/
Unfortunately, the library found widespread adoption and the name stuck
before anyone recognized that it was silly.
.Pp
The signature of
.Fn arc4random_addrandom
is silly.
There is no reason to require casts or accept negative lengths:
it should take a
.Vt void *
buffer and a
.Vt size_t
length.
But it's too late to change that now.
.Pp
.Fn arc4random_uniform
does not help to choose integers in [0,
.Fa n )
uniformly at random when
.Fa n
> 2^32.
.Pp
The security model of
.Nm
is stronger than many applications need, and stronger than other
operating systems provide.
For example, applications encrypting messages with random, but not
secret, initialization vectors need only prevent an adversary from
guessing future outputs, since past outputs will have been published
already.
.Pp
On the one hand,
.Nm
could be marginally faster if it were not necessary to prevent an
adversary who sees the state from predicting past outputs.
On the other hand, there are applications in the wild that use
.Nm
to generate key material, such as OpenSSH, so for the sake of
.Nx
users it would be imprudent to weaken the security model.
On the third hand, relying on the security model of
.Nm
in
.Nx
may lead you to an unpleasant surprise on another operating system
whose implementation of
.Nm
has a weaker security model.
.Pp
One may be tempted to create new APIs to accommodate different
security models and performance constraints without unpleasant
surprises on different operating systems.
This should not be done lightly, though, because there are already too
many different choices, and too many opportunities for programmers to
reach for one and pick the wrong one.

View File

@ -31,7 +31,7 @@
.\" $FreeBSD$
.\"
.Dd July 19, 1993
.Dt BITSTRING 3bsd
.Dt bitstring 3bsd
.Os
.Sh NAME
.Nm bit_alloc ,

View File

@ -25,7 +25,7 @@
.\" $FreeBSD$
.\"
.Dd April 29, 2002
.Dt BYTEORDER 3bsd
.Dt byteorder 3bsd
.Os
.Sh NAME
.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec ,

View File

@ -26,7 +26,7 @@
.\" $FreeBSD$
.\"
.Dd June 12, 2009
.Dt CLOSEFROM 3bsd
.Dt closefrom 3bsd
.Os
.Sh NAME
.Nm closefrom
@ -51,8 +51,17 @@ from the per-process object reference table.
Any errors encountered while closing file descriptors are ignored.
.Sh SEE ALSO
.Xr close 2
.Sh STANDARDS
.Fn closefrom
is a
.Bx
and Solaris extension.
.Sh HISTORY
The
.Fn closefrom
function first appeared in
.Fx 8.0 .
function first appeared in Solaris 9,
.Ox 3.5 ,
.Dx 1.2 ,
.Nx 3.0 ,
.Fx 8.0
and glibc 2.34.

View File

@ -28,7 +28,7 @@
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: April 23 2014 $
.Dt ERRC 3bsd
.Dt errc 3bsd
.Os
.Sh NAME
.Nm errc ,

View File

@ -26,7 +26,7 @@
.\" $FreeBSD$
.\"
.Dd August 15, 2010
.Dt EXPAND_NUMBER 3bsd
.Dt expand_number 3bsd
.Os
.Sh NAME
.Nm expand_number

View File

@ -30,7 +30,7 @@
.\" $OpenBSD: bzero.3,v 1.10 2014/01/22 21:06:45 tedu Exp $
.\"
.Dd $Mdocdate: January 22 2014 $
.Dt EXPLICIT_BZERO 3bsd
.Dt explicit_bzero 3bsd
.Os
.Sh NAME
.Nm explicit_bzero

View File

@ -29,7 +29,7 @@
.\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/stdio/fgetln.3,v 1.8 2004/07/16 06:07:12 tjr Exp $
.\"
.Dd April 19, 1994
.Dt FGETLN 3bsd
.Dt fgetln 3bsd
.Os
.Sh NAME
.Nm fgetln

View File

@ -29,7 +29,7 @@
.\" $FreeBSD$
.\"
.Dd July 16, 2004
.Dt FGETWLN 3bsd
.Dt fgetwln 3bsd
.Os
.Sh NAME
.Nm fgetwln

View File

@ -26,7 +26,7 @@
.\" $FreeBSD$
.\"
.Dd July 28, 2017
.Dt FLOPEN 3bsd
.Dt flopen 3bsd
.Os
.Sh NAME
.Nm flopen ,

View File

@ -27,7 +27,7 @@
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 14, 2014
.Dt FMTCHECK 3bsd
.Dt fmtcheck 3bsd
.Os
.Sh NAME
.Nm fmtcheck

View File

@ -23,7 +23,7 @@
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd November 30, 2002
.Dt FPARSELN 3bsd
.Dt fparseln 3bsd
.Os
.Sh NAME
.Nm fparseln
@ -150,4 +150,6 @@ if it runs out of memory.
The
.Fn fparseln
function first appeared in
.Nx 1.4 .
.Nx 1.4
and
.Fx 4.0 .

View File

@ -32,7 +32,7 @@
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: November 4 2015 $
.Dt FPURGE 3bsd
.Dt fpurge 3bsd
.Os
.Sh NAME
.Nm fpurge

View File

@ -31,7 +31,7 @@
.\" $FreeBSD$
.\"
.Dd March 19, 2004
.Dt FUNOPEN 3bsd
.Dt funopen 3bsd
.Os
.Sh NAME
.Nm funopen ,
@ -168,9 +168,12 @@ functions first appeared in
.Sh BUGS
The
.Fn funopen
function
may not be portable to systems other than
.Bx .
function may not be portable to systems other than
.Bx ,
glibc- and musl-based ones
(as the libbsd implementation is only provided when the system has
.Fn fopencookie
available).
.Pp
On
.Fx ,

View File

@ -29,7 +29,7 @@
.\" $FreeBSD$
.\"
.Dd November 16, 2012
.Dt GETBSIZE 3bsd
.Dt getbsize 3bsd
.Os
.Sh NAME
.Nm getbsize

View File

@ -26,7 +26,7 @@
.\" $FreeBSD$
.\"
.Dd July 15, 2001
.Dt GETPEEREID 3bsd
.Dt getpeereid 3bsd
.Os
.Sh NAME
.Nm getpeereid
@ -139,4 +139,7 @@ or the kernel returned invalid data.
The
.Fn getpeereid
function appeared in
.Fx 4.6 .
.Fx 4.6 ,
.Nx 5.0
and
.Ox 3.0 .

View File

@ -1,6 +1,4 @@
.\"
.\" Copyright (c) 2001 Christopher G. Demetriou
.\" All rights reserved.
.\" Copyright (c) 2022 Guillem Jover <guillem@hadrons.org>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
@ -10,34 +8,27 @@
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed for the
.\" NetBSD Project. See http://www.netbsd.org/ for
.\" information about NetBSD.
.\" 4. The name of the author may not be used to endorse or promote products
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\" .
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd May 1, 2001
.Dt GETPROGNAME 3bsd
.Dd August 03, 2022
.Dt getprogname 3bsd
.Os
.Sh NAME
.Nm getprogname ,
.Nm setprogname
.Nd get or set the program name
.Nd get and set program name
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@ -54,46 +45,39 @@ for include usage.)
.Sh DESCRIPTION
The
.Fn getprogname
and
.Fn setprogname
functions manipulate the name of the current program.
They are used by error-reporting routines to produce
consistent output.
.Pp
The
.Fn getprogname
function returns the name of the program.
If the name has not been set yet, it will return
.Dv NULL .
returns a string with the current program name,
excluding any directory component.
The function will return
.Dv NULL
if it was unable to get the program name from any known source.
.Pp
The
.Fn setprogname
function sets the name of the program to be the last component of the
.Fa progname
argument.
Since a pointer to the given string is kept as the program name,
it should not be modified for the rest of the program's lifetime.
function sets the current program name,
stripping any directory component prefix.
The function will keep a reference to the passed string pointer,
so it must not be freed or modified while these functions might
be called.
.Pp
In
.Fx ,
the name of the program is set by the start-up code that is run before
.Fn main ;
thus,
running
.Fn setprogname
is not necessary.
Programs that desire maximum portability should still call it;
on another operating system,
these functions may be implemented in a portability library.
Calling
.Fn setprogname
allows the aforementioned library to learn the program name without
modifications to the start-up code.
The implementations on most BSDs will try to initialize the program name
at program startup time, and
.Nm libbsd
will try to infer it from various known sources depending on the
target system.
But it is not a portable assumption that the program name will be set
without calling
.Fn setprogname ,
so portable programs should always call it after starting up.
.Sh SEE ALSO
.Xr err 3 ,
.Xr setproctitle 3bsd
.Xr setproctitle 3bsd .
.Sh HISTORY
These functions first appeared in
The
.Fn setprogname
and
.Fn getprogname
functions first appeared in
.Nx 1.6 ,
and made their way into
.Fx 4.4 .
.Fx 4.4 ,
.Dx 2.1
and
.Ox 5.4 .

View File

@ -33,7 +33,7 @@
.\" $FreeBSD$
.\"
.Dd September 30, 2003
.Dt HEAPSORT 3bsd
.Dt heapsort 3bsd
.Os
.Sh NAME
.Nm heapsort , mergesort

View File

@ -28,7 +28,7 @@
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd February 9, 2008
.Dt HUMANIZE_NUMBER 3bsd
.Dt humanize_number 3bsd
.Os
.Sh NAME
.Nm dehumanize_number ,
@ -166,7 +166,9 @@ represented a number that does not fit in
.Sh HISTORY
.Fn humanize_number
first appeared in
.Nx 2.0 .
.Nx 2.0
and
.Fx 5.3 .
.Pp
.Fn dehumanize_number
first appeared in

View File

@ -1,6 +1,6 @@
.\" libbsd man page
.\"
.\" Copyright © 2017-2018 Guillem Jover <guillem@hadrons.org>
.\" Copyright © 2017-2024 Guillem Jover <guillem@hadrons.org>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
@ -24,8 +24,8 @@
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd May 21, 2018
.Dt LIBBSD 7
.Dd January 8, 2024
.Dt libbsd 7
.Os
.Sh NAME
.Nm libbsd
@ -42,7 +42,7 @@ The library can be used in an overlay mode, which is the preferred way, so
that the code is portable and requires no modification to the original BSD
code.
This can be done easily with the
.Xr pkg-config 3
.Xr pkgconf 1
library named
.Pa libbsd-overlay .
Or by adding the system-specific include directory with the
@ -59,12 +59,12 @@ The includes in this case should be the usual system ones, such as
.In unistd.h .
.Pp
The other way to use the library is to use the namespaced headers,
this is less portable as it makes using
which is a discouraged way, being less portable as it makes using
.Nm libbsd
mandatory and it will not work on BSD-based systems, and requires
modifying original BSD code.
This can be done with the
.Xr pkg-config 3
.Xr pkgconf 1
library named
.Pa libbsd .
The includes in this case should be namespaced with
@ -73,13 +73,13 @@ such as
.In bsd/unistd.h .
.Pp
The package also provides a
.Pa bsd-ctor
.Nm libbsd-ctor
static library that can be used to inject automatic constructors into a
program so that the
.Fn setproctitle_init 3
.Xr setproctitle_init 3bsd
function gets invoked automatically at startup time.
This can be done with the
.Xr pkg-config 3
.Xr pkgconf 1
library named
.Pa libbsd-ctor .
.Sh HEADERS
@ -94,11 +94,13 @@ be prefixed with
.It In bitstring.h
.It In err.h
.It In getopt.h
.It In grp.h
.It In inttypes.h
.It In libutil.h
.It In md5.h
.It In netinet/ip_icmp.h
.It In nlist.h
.It In pwd.h
.It In readpassphrase.h
.It In stdio.h
.It In stdlib.h
@ -107,6 +109,7 @@ be prefixed with
.It In sys/bitstring.h
.It In sys/cdefs.h
.It In sys/endian.h
.It In sys/param.h
.It In sys/poll.h
.It In sys/queue.h
.It In sys/time.h
@ -125,6 +128,47 @@ It only works in non-overlay mode.
.Bl -tag -width 4m -compact
.It In bsd/bsd.h
.El
.Sh VARIANTS
Some functions have different prototypes depending on the BSD where they
originated from, and these various implementations provided are selectable
at build-time.
.Pp
This is the list of functions that provide multiple implementations:
.Pp
.Bl -tag -width 4m -compact
.It Xr strnvis 3bsd
.It Xr strnunvis 3bsd
.Nx
added
.Xr strnvis 3bsd
and
.Xr strnunvis 3bsd
but unfortunately made it incompatible with the existing one in
.Ox
and Freedesktop's libbsd (the former having existed for over ten years).
Despite this incompatibility being reported during development (see
http://gnats.netbsd.org/44977) they still shipped it.
Even more unfortunately
.Fx
and later macOS picked up this incompatible implementation.
.Pp
Provide both implementations and default for now to the historical one to
avoid breakage, but we will switch to the
.Nx
one in a later release, which is internally consistent with the other
.Xr vis 3bsd
functions and is now more widespread.
Define
.Dv LIBBSD_NETBSD_VIS
to switch to the
.Nx
one now.
Define
.Dv LIBBSD_OPENBSD_VIS
to keep using the
.Ox
one.
.El
.Sh DEPRECATED
Some functions have been deprecated, they will emit warnings at compile time
and possibly while being linked at run-time.
@ -134,30 +178,30 @@ or non-buggy way; or because there are better more portable replacements now.
.Pp
This is the list of currently deprecated macros and functions:
.Bl -tag -width 4m
.It Fn fgetln 3
.It Xr 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).
Use
.Fn getline 3
.Xr getline 3
instead, which is available in many systems and required by
.St -p1003.1-2008 .
.It Fn fgetwln 3
.It Xr 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).
Use
.Fn fgetwc 3
.Xr fgetwc 3
instead, which is available in many systems and required by
.St -isoC-99
and
.St -p1003.1-2001 .
.It Fn funopen 3
.It Xr funopen 3bsd
Unportable, requires assistance from the stdio layer or some hook framework.
On GNU systems the
.Fn fopencookie 3
On glibc- and musl-based systems the
.Xr fopencookie 3
function can be used.
Otherwise the code needs to be prepared for neither of these functions being
available.
@ -171,27 +215,53 @@ are present in all major
for example.
.Pp
.Bl -tag -width 4m -compact
.It Fn MD5Init 3
.It Fn MD5Update 3
.It Fn MD5Pad 3
.It Fn MD5Final 3
.It Fn MD5Transform 3
.It Fn MD5End 3
.It Fn MD5File 3
.It Fn MD5FileChunk 3
.It Fn MD5Data 3
.It Xr MD5Init 3
.It Xr MD5Update 3
.It Xr MD5Pad 3
.It Xr MD5Final 3
.It Xr MD5Transform 3
.It Xr MD5End 3
.It Xr MD5File 3
.It Xr MD5FileChunk 3
.It Xr MD5Data 3
The set of MD5 digest functions are now proxies for the implementations
provided by the
.Nm libmd
companion library, so it is advised to switch to use that directly instead.
.It Fn explicit_bzero 3
.It Xr explicit_bzero 3bsd
This function is provided by
.Nm glibc
2.25.
.It Fn reallocarray 3
2.25, and
.Nm musl
1.1.20.
.It Xr reallocarray 3bsd
This function is provided by
.Nm glibc
2.26.
2.26, and
.Nm musl
1.2.2.
.It Xr closefrom 3bsd
This function is provided by
.Nm glibc
2.34.
.It Xr arc4random 3bsd
.It Xr arc4random_buf 3bsd
.It Xr arc4random_uniform 3bsd
These functions are provided by
.Nm glibc
2.36.
Note that it does not provide the
.Xr arc4random_stir 3bsd
and
.Xr arc4random_addrandom 3bsd
functions.
.It Xr strlcpy 3bsd
.It Xr strlcat 3bsd
These functions are provided by
.Nm glibc
2.38, and
.Nm musl
0.5.0.
.El
.Sh SEE ALSO
.Xr arc4random 3bsd ,
@ -216,6 +286,7 @@ This function is provided by
.Xr md5 3bsd ,
.Xr nlist 3bsd ,
.Xr pidfile 3bsd ,
.Xr pwcache 3bsd ,
.Xr queue 3bsd ,
.Xr radixsort 3bsd ,
.Xr readpassphrase 3bsd ,

View File

@ -29,7 +29,7 @@
.\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/nlist.3,v 1.7 2001/10/01 16:08:51 ru Exp $
.\"
.Dd April 19, 1994
.Dt NLIST 3bsd
.Dt nlist 3bsd
.Os
.Sh NAME
.Nm nlist

View File

@ -25,7 +25,7 @@
.\" $FreeBSD$
.\"
.Dd February 8, 2012
.Dt PIDFILE 3bsd
.Dt pidfile 3bsd
.Os
.Sh NAME
.Nm pidfile_open ,
@ -292,6 +292,20 @@ Probably called not from the process which used
.Xr open 2 ,
.Xr daemon 3 ,
.Xr flopen 3bsd
.Sh HISTORY
The functions
.Fn pidfile_open ,
.Fn pidfile_write ,
.Fn pidfile_close
and
.Fn pidfile_remove
first appeared in
.Fx 5.5 .
.Pp
The function
.Fn pidfile_fileno
first appeared in
.Fx 10.0 .
.Sh AUTHORS
.An -nosplit
The

View File

@ -28,7 +28,7 @@
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: September 13 2018 $
.Dt USER_FROM_UID 3
.Dt user_from_uid 3
.Os
.Sh NAME
.Nm user_from_uid ,
@ -137,7 +137,7 @@ The
.Fn uid_from_user
and
.Fn gid_from_group
functions were ported from
.Nx
and first appeared in
functions first appeared in
.Nx 1.4
and
.Ox 6.4 .

View File

@ -28,35 +28,40 @@
.\" @(#)queue.3 8.2 (Berkeley) 1/24/94
.\" $FreeBSD$
.\"
.Dd June 17, 2013
.Dt QUEUE 3bsd
.Dd September 8, 2016
.Dt queue 3bsd
.Os
.Sh NAME
.Nm SLIST_CLASS_ENTRY ,
.Nm SLIST_CLASS_HEAD ,
.Nm SLIST_CONCAT ,
.Nm SLIST_EMPTY ,
.Nm SLIST_ENTRY ,
.Nm SLIST_FIRST ,
.Nm SLIST_FOREACH ,
.Nm SLIST_FOREACH_FROM ,
.Nm SLIST_FOREACH_SAFE ,
.Nm SLIST_FOREACH_FROM_SAFE ,
.Nm SLIST_FOREACH_SAFE ,
.Nm SLIST_HEAD ,
.Nm SLIST_HEAD_INITIALIZER ,
.Nm SLIST_INIT ,
.Nm SLIST_INSERT_AFTER ,
.Nm SLIST_INSERT_HEAD ,
.Nm SLIST_NEXT ,
.Nm SLIST_REMOVE ,
.Nm SLIST_REMOVE_AFTER ,
.Nm SLIST_REMOVE_HEAD ,
.Nm SLIST_REMOVE ,
.Nm SLIST_SWAP ,
.Nm STAILQ_CLASS_ENTRY ,
.Nm STAILQ_CLASS_HEAD ,
.Nm STAILQ_CONCAT ,
.Nm STAILQ_EMPTY ,
.Nm STAILQ_ENTRY ,
.Nm STAILQ_FIRST ,
.Nm STAILQ_FOREACH ,
.Nm STAILQ_FOREACH_FROM ,
.Nm STAILQ_FOREACH_SAFE ,
.Nm STAILQ_FOREACH_FROM_SAFE ,
.Nm STAILQ_FOREACH_SAFE ,
.Nm STAILQ_HEAD ,
.Nm STAILQ_HEAD_INITIALIZER ,
.Nm STAILQ_INIT ,
@ -65,17 +70,20 @@
.Nm STAILQ_INSERT_TAIL ,
.Nm STAILQ_LAST ,
.Nm STAILQ_NEXT ,
.Nm STAILQ_REMOVE ,
.Nm STAILQ_REMOVE_AFTER ,
.Nm STAILQ_REMOVE_HEAD ,
.Nm STAILQ_REMOVE ,
.Nm STAILQ_SWAP ,
.Nm LIST_CLASS_ENTRY ,
.Nm LIST_CLASS_HEAD ,
.Nm LIST_CONCAT ,
.Nm LIST_EMPTY ,
.Nm LIST_ENTRY ,
.Nm LIST_FIRST ,
.Nm LIST_FOREACH ,
.Nm LIST_FOREACH_FROM ,
.Nm LIST_FOREACH_SAFE ,
.Nm LIST_FOREACH_FROM_SAFE ,
.Nm LIST_FOREACH_SAFE ,
.Nm LIST_HEAD ,
.Nm LIST_HEAD_INITIALIZER ,
.Nm LIST_INIT ,
@ -86,18 +94,20 @@
.Nm LIST_PREV ,
.Nm LIST_REMOVE ,
.Nm LIST_SWAP ,
.Nm TAILQ_CLASS_ENTRY ,
.Nm TAILQ_CLASS_HEAD ,
.Nm TAILQ_CONCAT ,
.Nm TAILQ_EMPTY ,
.Nm TAILQ_ENTRY ,
.Nm TAILQ_FIRST ,
.Nm TAILQ_FOREACH ,
.Nm TAILQ_FOREACH_FROM ,
.Nm TAILQ_FOREACH_SAFE ,
.Nm TAILQ_FOREACH_FROM_SAFE ,
.Nm TAILQ_FOREACH_REVERSE ,
.Nm TAILQ_FOREACH_REVERSE_FROM ,
.Nm TAILQ_FOREACH_REVERSE_SAFE ,
.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
.Nm TAILQ_FOREACH_REVERSE_SAFE ,
.Nm TAILQ_FOREACH_SAFE ,
.Nm TAILQ_HEAD ,
.Nm TAILQ_HEAD_INITIALIZER ,
.Nm TAILQ_INIT ,
@ -122,52 +132,60 @@ lists and tail queues
.Xr libbsd 7
for include usage.)
.\"
.Fn SLIST_CLASS_ENTRY "CLASSTYPE"
.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME"
.Fn SLIST_EMPTY "SLIST_HEAD *head"
.Fn SLIST_ENTRY "TYPE"
.Fn SLIST_FIRST "SLIST_HEAD *head"
.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
.Fn SLIST_HEAD "HEADNAME" "TYPE"
.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
.Fn SLIST_INIT "SLIST_HEAD *head"
.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE"
.\"
.Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
.Fn STAILQ_ENTRY "TYPE"
.Fn STAILQ_FIRST "STAILQ_HEAD *head"
.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn STAILQ_HEAD "HEADNAME" "TYPE"
.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
.Fn STAILQ_INIT "STAILQ_HEAD *head"
.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE"
.\"
.Fn LIST_CLASS_ENTRY "CLASSTYPE"
.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
.Fn LIST_EMPTY "LIST_HEAD *head"
.Fn LIST_ENTRY "TYPE"
.Fn LIST_FIRST "LIST_HEAD *head"
.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
.Fn LIST_HEAD "HEADNAME" "TYPE"
.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
.Fn LIST_INIT "LIST_HEAD *head"
@ -179,18 +197,20 @@ for include usage.)
.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
.\"
.Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
.Fn TAILQ_ENTRY "TYPE"
.Fn TAILQ_FIRST "TAILQ_HEAD *head"
.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
.Fn TAILQ_HEAD "HEADNAME" "TYPE"
.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
.Fn TAILQ_INIT "TAILQ_HEAD *head"
@ -205,8 +225,18 @@ for include usage.)
.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
.\"
.Sh DESCRIPTION
These macros define and operate on four types of data structures:
singly-linked lists, singly-linked tail queues, lists, and tail queues.
These macros define and operate on four types of data structures which
can be used in both C and C++ source code:
.Bl -enum -compact -offset indent
.It
Lists
.It
Singly-linked lists
.It
Singly-linked tail queues
.It
Tail queues
.El
All four structures support the following functionality:
.Bl -enum -compact -offset indent
.It
@ -230,6 +260,8 @@ Singly-linked lists add the following functionality:
.Bl -enum -compact -offset indent
.It
O(n) removal of any entry in the list.
.It
O(n) concatenation of two lists.
.El
.Pp
Singly-linked tail queues add the following functionality:
@ -277,6 +309,8 @@ Linked lists are the simplest of the doubly linked data structures.
They add the following functionality over the above:
.Bl -enum -compact -offset indent
.It
O(n) concatenation of two lists.
.It
They may be traversed backwards.
.El
However:
@ -308,24 +342,39 @@ than singly-linked lists.
.Pp
In the macro definitions,
.Fa TYPE
is the name of a user defined structure,
that must contain a field of type
is the name of a user defined structure.
The structure must contain a field called
.Fa NAME
which is of type
.Li SLIST_ENTRY ,
.Li STAILQ_ENTRY ,
.Li LIST_ENTRY ,
or
.Li TAILQ_ENTRY ,
named
.Fa NAME .
.Li TAILQ_ENTRY .
In the macro definitions,
.Fa CLASSTYPE
is the name of a user defined class.
The class must contain a field called
.Fa NAME
which is of type
.Li SLIST_CLASS_ENTRY ,
.Li STAILQ_CLASS_ENTRY ,
.Li LIST_CLASS_ENTRY ,
or
.Li TAILQ_CLASS_ENTRY .
The argument
.Fa HEADNAME
is the name of a user defined structure that must be declared
using the macros
.Li SLIST_HEAD ,
.Li SLIST_CLASS_HEAD ,
.Li STAILQ_HEAD ,
.Li STAILQ_CLASS_HEAD ,
.Li LIST_HEAD ,
.Li LIST_CLASS_HEAD ,
.Li TAILQ_HEAD ,
or
.Li TAILQ_HEAD .
.Li TAILQ_CLASS_HEAD .
See the examples below for further explanation of how these
macros are used.
.Sh SINGLY-LINKED LISTS
@ -367,6 +416,19 @@ evaluates to an initializer for the list
.Fa head .
.Pp
The macro
.Nm SLIST_CONCAT
concatenates the list headed by
.Fa head2
onto the end of the one headed by
.Fa head1
removing all entries from the former.
Use of this macro should be avoided as it traverses the entirety of the
.Fa head1
list.
A singly-linked tail queue should be used if this macro is needed in
high-usage code paths or to operate on long lists.
.Pp
The macro
.Nm SLIST_EMPTY
evaluates to true if there are no elements in the list.
.Pp
@ -474,6 +536,9 @@ The macro
removes the element
.Fa elm
from the list.
Use of this macro should be avoided as it traverses the entire list.
A doubly-linked list should be used if this macro is needed in
high-usage code paths or to operate on long lists.
.Pp
The macro
.Nm SLIST_SWAP
@ -690,6 +755,9 @@ The macro
removes the element
.Fa elm
from the tail queue.
Use of this macro should be avoided as it traverses the entire list.
A doubly-linked tail queue should be used if this macro is needed in
high-usage code paths or to operate on long tail queues.
.Pp
The macro
.Nm STAILQ_SWAP
@ -789,6 +857,19 @@ evaluates to an initializer for the list
.Fa head .
.Pp
The macro
.Nm LIST_CONCAT
concatenates the list headed by
.Fa head2
onto the end of the one headed by
.Fa head1
removing all entries from the former.
Use of this macro should be avoided as it traverses the entirety of the
.Fa head1
list.
A tail queue should be used if this macro is needed in
high-usage code paths or to operate on long lists.
.Pp
The macro
.Nm LIST_EMPTY
evaluates to true if there are no elements in the list.
.Pp
@ -1210,6 +1291,26 @@ while (n1 != NULL) {
}
TAILQ_INIT(&head);
.Ed
.Sh DIAGNOSTICS
When debugging
.Nm queue(3) ,
it can be useful to trace queue changes.
To enable tracing, define the macro
.Va QUEUE_MACRO_DEBUG_TRACE
at compile time.
.Pp
It can also be useful to trash pointers that have been unlinked from a queue,
to detect use after removal.
To enable pointer trashing, define the macro
.Va QUEUE_MACRO_DEBUG_TRASH
at compile time.
The macro
.Fn QMD_IS_TRASHED "void *ptr"
returns true if
.Fa ptr
has been trashed by the
.Va QUEUE_MACRO_DEBUG_TRASH
option.
.Sh SEE ALSO
.Xr tree 3bsd
.Sh HISTORY

View File

@ -30,7 +30,7 @@
.\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94
.\"
.Dd January 27, 1994
.Dt RADIXSORT 3bsd
.Dt radixsort 3bsd
.Os
.Sh NAME
.Nm radixsort ,

View File

@ -19,7 +19,7 @@
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
.Dd $Mdocdate: March 6 2014 $
.Dt READPASSPHRASE 3bsd
.Dt readpassphrase 3bsd
.Os
.Sh NAME
.Nm readpassphrase
@ -165,10 +165,12 @@ will reprint the prompt and the user may then enter a passphrase.
The
.Fn readpassphrase
function is an
.Ox
.Bx
extension and should not be used if portability is desired.
.Sh HISTORY
The
.Fn readpassphrase
function first appeared in
.Ox 2.9 .
.Ox 2.9
and
.Fx 4.6 .

View File

@ -33,7 +33,7 @@
.\" $OpenBSD: malloc.3,v 1.126 2019/09/14 13:16:50 otto Exp $
.\"
.Dd $Mdocdate: September 14 2019 $
.Dt REALLOCARRAY 3bsd
.Dt reallocarray 3bsd
.Os
.Sh NAME
.Nm reallocarray ,
@ -283,12 +283,18 @@ The
.Fn reallocarray
function appeared in
.Ox 5.6 ,
.Dx 5.5
and glibc 2.26.
.Pp
The
.Fn recallocarray
function appeared in
.Ox 6.1 .
.Ox 6.1
and
.Dx 5.5 .
The
.Fn freezero
function appeared in
.Ox 6.2 .
.Ox 6.2
and
.Dx 5.5 .

View File

@ -33,7 +33,7 @@
.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.80.2.2.2.1 2010/06/14 02:09:06 kensmith Exp $
.\"
.Dd September 26, 2009
.Dt REALLOCF 3bsd
.Dt reallocf 3bsd
.Os
.Sh NAME
.Nm reallocf

View File

@ -31,7 +31,7 @@
.\" $FreeBSD: src/lib/libc/gen/setmode.3,v 1.12 2007/01/09 00:27:55 imp Exp $
.\"
.Dd January 4, 2009
.Dt SETMODE 3bsd
.Dt setmode 3bsd
.Os
.Sh NAME
.Nm getmode ,

View File

@ -20,8 +20,8 @@
.\" $FreeBSD$
.\"
.\" The following requests are required for all man pages.
.Dd December 16, 1995
.Dt SETPROCTITLE 3bsd
.Dd December 7, 2022
.Dt setproctitle 3bsd
.Os
.Sh NAME
.Nm setproctitle
@ -31,7 +31,6 @@
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
.Lb libbsd
.Sh SYNOPSIS
.In sys/types.h
.In unistd.h
(See
.Xr libbsd 7
@ -54,7 +53,7 @@ library routine only needs to be called (before any call to
and with
.Fn main
arguments), if the automatic constructor support has not
been linked in through the libbsd-ctor pkg-config file.
been linked in through the libbsd-ctor pkgconf file.
.Pp
The title is set from the executable's name, followed by the
result of a
@ -113,6 +112,8 @@ The
.Fn setproctitle
function
first appeared in
.Nx 1.0
and
.Fx 2.2 .
Other operating systems have
similar functions.

View File

@ -27,7 +27,7 @@
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd May 6, 2010
.Dt STRINGLIST 3bsd
.Dt stringlist 3bsd
.Os
.Sh NAME
.Nm stringlist ,

View File

@ -14,8 +14,8 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: May 31 2007 $
.Dt STRLCPY 3bsd
.Dd $Mdocdate: January 7 2024 $
.Dt strlcpy 3bsd
.Os
.Sh NAME
.Nm strlcpy ,
@ -192,4 +192,7 @@ and
functions first appeared in
.Ox 2.4 ,
and made their appearance in
.Fx 3.3 .
.Nx 1.4.3 ,
.Fx 3.3
and
glibc 2.38.

View File

@ -29,7 +29,7 @@
.\" $FreeBSD: src/lib/libc/string/strmode.3,v 1.9 2003/07/01 15:28:05 maxim Exp $
.\"
.Dd July 28, 1994
.Dt STRMODE 3bsd
.Dt strmode 3bsd
.Os
.Sh NAME
.Nm strmode

View File

@ -34,7 +34,7 @@
.\" $FreeBSD$
.\"
.Dd October 11, 2001
.Dt STRSTR 3bsd
.Dt strstr 3bsd
.Os
.Sh NAME
.Nm strnstr
@ -112,3 +112,8 @@ ptr = strnstr(largestring, smallstring, 4);
.Xr strspn 3 ,
.Xr strtok 3 ,
.Xr wcsstr 3
.Sh HISTORY
The
.Fn strnstr
function originated in
.Fx .

View File

@ -1,4 +1,4 @@
.\" $NetBSD: strtoi.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
.\" $NetBSD: strtoi.3,v 1.10 2024/02/10 18:43:51 andvar Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -36,12 +36,12 @@
.\" Created by Kamil Rytarowski, based on ID:
.\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp
.\"
.Dd November 13, 2015
.Dt STRTOI 3bsd
.Dd January 20, 2024
.Dt strtoi 3bsd
.Os
.Sh NAME
.Nm strtoi
.Nd convert string value to an intmax_t integer
.Nd convert a string value to an intmax_t integer
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@ -63,8 +63,7 @@ for include usage.)
.Sh DESCRIPTION
The
.Fn strtoi
function
converts the string in
function converts the string in
.Fa nptr
to an
.Ft intmax_t
@ -77,7 +76,7 @@ and ensures that the result is always in the range [
.Fa lo ..
.Fa hi
].
In adddition it always places
In addition it always places
.Dv 0
on success or a conversion status in the
.Fa rstatus
@ -122,10 +121,11 @@ is taken as 10 (decimal) unless the next character is
.Ql 0 ,
in which case it is taken as 8 (octal).
.Pp
The remainder of the string is converted to a
The remainder of the string is converted to an
.Em intmax_t
value in the obvious manner,
stopping at the first character which is not a valid digit
stopping at the end of the string
or at the first character which is not a valid digit
in the given base.
(In bases above 10, the letter
.Ql A
@ -206,6 +206,12 @@ or the range given was invalid, i.e.
>
.Fa hi .
.El
.Pp
The range check is more important than the unconverted characters check,
and it is performed first.
If a program needs to know if there were unconverted characters when an
out of range number has been provided, it needs to supply and test
.Fa endptr.
.Sh SEE ALSO
.Xr atof 3 ,
.Xr atoi 3 ,
@ -233,7 +239,7 @@ function first appeared in
.Nx 7.0 .
.Ox
introduced the
.Fn strtonum 3bsd
.Xr strtonum 3bsd
function for the same purpose, but the interface makes it impossible to
properly differentiate illegal returns.
.Sh BUGS

View File

@ -16,7 +16,7 @@
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd January 18, 2015
.Dt STRTONUM 3bsd
.Dt strtonum 3bsd
.Os
.Sh NAME
.Nm strtonum
@ -141,8 +141,8 @@ The string did not consist solely of digit characters.
.Xr strtoull 3
.Sh STANDARDS
.Fn strtonum
is an
.Ox
is a
.Bx
extension.
.Sh HISTORY
.ds doc-operating-system-NetBSD-8.0 8.0
@ -154,9 +154,9 @@ function first appeared in
was redesigned in
.Nx 8.0
as
.Fn strtoi 3bsd
.Xr strtoi 3bsd
and
.Fn strtou 3bsd .
.Xr strtou 3bsd .
.Sh CAVEATS
The
.Fn strtonum
@ -186,6 +186,6 @@ To overcome the shortcomings of
.Fn strtonum
.Nx
provides
.Fn strtou 3bsd
.Xr strtou 3bsd
and
.Fn strtoi 3bsd .
.Xr strtoi 3bsd .

View File

@ -1,4 +1,4 @@
.\" $NetBSD: strtou.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
.\" $NetBSD: strtou.3,v 1.8 2024/01/20 16:13:39 christos Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -36,12 +36,12 @@
.\" Created by Kamil Rytarowski, based on ID:
.\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp
.\"
.Dd November 13, 2015
.Dt STRTOU 3bsd
.Dd January 20, 2024
.Dt strtou 3bsd
.Os
.Sh NAME
.Nm strtou
.Nd convert a string to an uintmax_t integer
.Nd convert a string value to an uintmax_t integer
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@ -76,7 +76,7 @@ and ensures that the result is always in the range [
.Fa lo ..
.Fa hi
].
In adddition it always places
In addition it always places
.Dv 0
on success or a conversion status in the
.Fa rstatus
@ -125,7 +125,7 @@ The remainder of the string is converted to an
.Em uintmax_t
value in the obvious manner,
stopping at the end of the string
or at the first character that does not produce a valid digit
or at the first character which is not a valid digit
in the given base.
(In bases above 10, the letter
.Ql A
@ -200,12 +200,18 @@ In this case,
.Fa endptr
points to the first unconverted character.
.It Bq Er ERANGE
The given string was out of range; the value converted has been clamped; or
the range given was invalid, i.e.
The given string was out of range; the value converted has been clamped;
or the range given was invalid, i.e.
.Fa lo
>
.Fa hi .
.El
.Pp
The range check is more important than the unconverted characters check,
and it is performed first.
If a program needs to know if there were unconverted characters when an
out of range number has been provided, it needs to supply and test
.Fa endptr.
.Sh SEE ALSO
.Xr atof 3 ,
.Xr atoi 3 ,
@ -220,20 +226,20 @@ the range given was invalid, i.e.
.Xr strtoull 3 ,
.Xr strtoumax 3
.Sh STANDARDS
.ds doc-operating-system-NetBSD-7.0 7.0
The
.Fn strtou
function is a
.Nx
extension.
.Sh HISTORY
.ds doc-operating-system-NetBSD-7.0 7.0
The
.Fn strtou
function first appeared in
.Nx 7.0 .
.Ox
introduced the
.Fn strtonum 3bsd
.Xr strtonum 3bsd
function for the same purpose, but the interface makes it impossible to
properly differentiate illegal returns.
.Sh BUGS

View File

@ -31,7 +31,7 @@
.\" $FreeBSD: src/share/man/man3/timeradd.3,v 1.3 2003/09/08 19:57:19 ru Exp $
.\"
.Dd June 7, 2010
.Dt TIMERADD 3bsd
.Dt timeradd 3bsd
.Os
.Sh NAME
.Nm timeradd ,

View File

@ -1 +0,0 @@
.so man3/timeval.3bsd

View File

@ -1,134 +0,0 @@
.\" $NetBSD: timeval.3,v 1.12 2011/04/12 08:39:26 jruoho Exp $
.\"
.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jukka Ruohonen.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd April 12, 2011
.Dt TIMEVAL 3bsd
.Os
.Sh NAME
.Nm timeval ,
.Nm timespec
.Nd time structures
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
.Lb libbsd
.Sh SYNOPSIS
.In sys/time.h
(See
.Xr libbsd 7
for include usage.)
.Ft void
.Fn TIMEVAL_TO_TIMESPEC "struct timeval *tv" "struct timespec *ts"
.Ft void
.Fn TIMESPEC_TO_TIMEVAL "struct timeval *tv" "struct timespec *ts"
.Sh DESCRIPTION
The
.In sys/time.h
header, included by
.In time.h ,
defines various structures related to time and timers.
.Bl -enum -offset 1n
.It
The following structure is used by
.Xr gettimeofday 2 ,
among others:
.Bd -literal -offset indent
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
.Ed
.Pp
The
.Va tv_sec
member represents the elapsed time, in whole seconds.
The
.Va tv_usec
member captures rest of the elapsed time,
represented as the number of microseconds.
.It
The following structure is used by
.Xr nanosleep 2 ,
among others:
.Bd -literal -offset indent
struct timespec {
time_t tv_sec;
long tv_nsec;
};
.Ed
.Pp
The
.Va tv_sec
member is again the elapsed time in whole seconds.
The
.Va tv_nsec
member represents the rest of the elapsed time in nanoseconds.
.Pp
A microsecond is equal to one millionth of a second,
1000 nanoseconds, or 1/1000 milliseconds.
To ease the conversions, the macros
.Fn TIMEVAL_TO_TIMESPEC
and
.Fn TIMESPEC_TO_TIMEVAL
can be used to convert between
.Em struct timeval
and
.Em struct timespec .
.El
.Sh EXAMPLES
It can be stressed that the traditional
.Tn UNIX
.Va timeval
and
.Va timespec
structures represent elapsed time, measured by the system clock.
The following sketch implements a function suitable
for use in a context where the
.Va timespec
structure is required for a conditional timeout:
.Bd -literal -offset indent
static void
example(struct timespec *spec, time_t minutes)
{
struct timeval elapsed;
(void)gettimeofday(&elapsed, NULL);
_DIAGASSERT(spec != NULL);
TIMEVAL_TO_TIMESPEC(&elapsed, spec);
/* Add the offset for timeout in minutes. */
spec->tv_sec = spec->tv_sec + minutes * 60;
}
.Ed
.Pp
A better alternative would use the more precise
.Xr clock_gettime 2 .
.Sh SEE ALSO
.Xr timeradd 3bsd

View File

@ -1,37 +1,30 @@
.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $
.\"
.\" Copyright 2002 Niels Provos <provos@citi.umich.edu>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by Niels Provos.
.\" 4. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd December 27, 2007
.Dt TREE 3bsd
.\" $OpenBSD: tree.3,v 1.30 2019/05/10 13:13:14 florian Exp $
.\"/*
.\" * Copyright 2002 Niels Provos <provos@citi.umich.edu>
.\" * All rights reserved.
.\" *
.\" * Redistribution and use in source and binary forms, with or without
.\" * modification, are permitted provided that the following conditions
.\" * are met:
.\" * 1. Redistributions of source code must retain the above copyright
.\" * notice, this list of conditions and the following disclaimer.
.\" * 2. Redistributions in binary form must reproduce the above copyright
.\" * notice, this list of conditions and the following disclaimer in the
.\" * documentation and/or other materials provided with the distribution.
.\" *
.\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" */
.Dd May 10, 2019
.Dt tree 3bsd
.Os
.Sh NAME
.Nm SPLAY_PROTOTYPE ,
@ -70,7 +63,9 @@
.Nm RB_RIGHT ,
.Nm RB_PARENT ,
.Nm RB_FOREACH ,
.Nm RB_FOREACH_SAFE ,
.Nm RB_FOREACH_REVERSE ,
.Nm RB_FOREACH_REVERSE_SAFE ,
.Nm RB_INIT ,
.Nm RB_INSERT ,
.Nm RB_REMOVE
@ -84,88 +79,92 @@
(See
.Xr libbsd 7
for include usage.)
.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
.Fn SPLAY_GENERATE NAME TYPE FIELD CMP
.Fn SPLAY_ENTRY TYPE
.Fn SPLAY_HEAD HEADNAME TYPE
.Pp
.Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
.Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP"
.Fn SPLAY_ENTRY "TYPE"
.Fn SPLAY_HEAD "HEADNAME" "TYPE"
.Ft "struct TYPE *"
.Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
.Fn SPLAY_ROOT "SPLAY_HEAD *head"
.Ft bool
.Ft "int"
.Fn SPLAY_EMPTY "SPLAY_HEAD *head"
.Ft "struct TYPE *"
.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
.Fn SPLAY_NEXT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
.Fn SPLAY_MIN "NAME" "SPLAY_HEAD *head"
.Ft "struct TYPE *"
.Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
.Fn SPLAY_MAX "NAME" "SPLAY_HEAD *head"
.Ft "struct TYPE *"
.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
.Fn SPLAY_FIND "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
.Ft "struct TYPE *"
.Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
.Fn SPLAY_FOREACH "VARNAME" "NAME" "SPLAY_HEAD *head"
.Ft void
.Fn SPLAY_INIT "SPLAY_HEAD *head"
.Ft "struct TYPE *"
.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
.Fn SPLAY_INSERT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
.Fn RB_PROTOTYPE NAME TYPE FIELD CMP
.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
.Fn RB_GENERATE NAME TYPE FIELD CMP
.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
.Fn RB_ENTRY TYPE
.Fn RB_HEAD HEADNAME TYPE
.Fn SPLAY_REMOVE "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
.Pp
.Fn RB_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_PROTOTYPE_STATIC "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_GENERATE "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_GENERATE_STATIC "NAME" "TYPE" "FIELD" "CMP"
.Fn RB_ENTRY "TYPE"
.Fn RB_HEAD "HEADNAME" "TYPE"
.Fn RB_INITIALIZER "RB_HEAD *head"
.Ft "struct TYPE *"
.Fn RB_ROOT "RB_HEAD *head"
.Ft "bool"
.Ft "int"
.Fn RB_EMPTY "RB_HEAD *head"
.Ft "struct TYPE *"
.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_NEXT "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_PREV "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn RB_MIN NAME "RB_HEAD *head"
.Fn RB_MIN "NAME" "RB_HEAD *head"
.Ft "struct TYPE *"
.Fn RB_MAX NAME "RB_HEAD *head"
.Fn RB_MAX "NAME" "RB_HEAD *head"
.Ft "struct TYPE *"
.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_FIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_NFIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
.Ft "struct TYPE *"
.Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
.Ft "struct TYPE *"
.Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head"
.Fn RB_FOREACH "VARNAME" "NAME" "RB_HEAD *head"
.Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
.Fn RB_FOREACH_REVERSE "VARNAME" "NAME" "RB_HEAD *head"
.Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
.Ft void
.Fn RB_INIT "RB_HEAD *head"
.Ft "struct TYPE *"
.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_INSERT "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Ft "struct TYPE *"
.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
.Fn RB_REMOVE "NAME" "RB_HEAD *head" "struct TYPE *elm"
.Sh DESCRIPTION
These macros define data structures for different types of trees:
splay trees and red-black trees.
.Pp
In the macro definitions,
.Fa TYPE
is the name tag of a user defined structure that must contain a field of type
.Vt SPLAY_ENTRY ,
is the name tag of a user defined structure that must contain a field named
.Fa FIELD ,
of type
.Li SPLAY_ENTRY
or
.Vt RB_ENTRY ,
named
.Fa ENTRYNAME .
.Li RB_ENTRY .
The argument
.Fa HEADNAME
is the name tag of a user defined structure that must be declared
using the macros
.Fn SPLAY_HEAD ,
.Fn SPLAY_HEAD
or
.Fn RB_HEAD .
The argument
@ -173,46 +172,38 @@ The argument
has to be a unique name prefix for every tree that is defined.
.Pp
The function prototypes are declared with
.Fn SPLAY_PROTOTYPE ,
.Fn RB_PROTOTYPE ,
.Li SPLAY_PROTOTYPE ,
.Li RB_PROTOTYPE ,
or
.Fn RB_PROTOTYPE_STATIC .
.Li RB_PROTOTYPE_STATIC .
The function bodies are generated with
.Fn SPLAY_GENERATE ,
.Fn RB_GENERATE ,
.Li SPLAY_GENERATE ,
.Li RB_GENERATE ,
or
.Fn RB_GENERATE_STATIC .
.Li RB_GENERATE_STATIC .
See the examples below for further explanation of how these macros are used.
.Sh SPLAY TREES
A splay tree is a self-organizing data structure.
Every operation on the tree causes a splay to happen.
The splay moves the requested
node to the root of the tree and partly rebalances it.
The splay moves the requested node to the root of the tree and partly
rebalances it.
.Pp
This has the benefit that request locality causes faster lookups as
the requested nodes move to the top of the tree.
On the other hand, every lookup causes memory writes.
.Pp
The Balance Theorem bounds the total access time for
.Ar m
operations and
.Ar n
inserts on an initially empty tree as
.Fn O "\*[lp]m + n\*[rp]lg n" .
The
amortized cost for a sequence of
.Ar m
accesses to a splay tree is
.Fn O "lg n" .
The Balance Theorem bounds the total access time for m operations
and n inserts on an initially empty tree as O((m + n)lg n).
The amortized cost for a sequence of m accesses to a splay tree is O(lg n).
.Pp
A splay tree is headed by a structure defined by the
.Fn SPLAY_HEAD
macro.
A
.Fa SPLAY_HEAD
structure is declared as follows:
.Bd -ragged -offset indent
.Fn SPLAY_HEAD HEADNAME TYPE
.Va head ;
.Bd -literal -offset indent
SPLAY_HEAD(HEADNAME, TYPE) head;
.Ed
.Pp
where
@ -251,16 +242,15 @@ macro, but should be used only once.
Finally,
the
.Fa CMP
argument is the name of a function used to compare tree nodes
argument is the name of a function used to compare trees' nodes
with each other.
The function takes two arguments of type
.Vt "struct TYPE *" .
.Fa "struct TYPE *" .
If the first argument is smaller than the second, the function returns a
value smaller than zero.
If they are equal, the function returns zero.
Otherwise, it should return a value greater than zero.
The compare
function defines the order of the tree elements.
The compare function defines the order of the tree elements.
.Pp
The
.Fn SPLAY_INIT
@ -270,11 +260,8 @@ macro initializes the tree referenced by
The splay tree can also be initialized statically by using the
.Fn SPLAY_INITIALIZER
macro like this:
.Bd -ragged -offset indent
.Fn SPLAY_HEAD HEADNAME TYPE
.Va head
=
.Fn SPLAY_INITIALIZER &head ;
.Bd -literal -offset indent
SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
.Ed
.Pp
The
@ -282,6 +269,11 @@ The
macro inserts the new element
.Fa elm
into the tree.
Upon success,
.Va NULL
is returned.
If a matching element already exists in the tree, the insertion is
aborted, and a pointer to the existing element is returned.
.Pp
The
.Fn SPLAY_REMOVE
@ -289,6 +281,11 @@ macro removes the element
.Fa elm
from the tree pointed by
.Fa head .
Upon success, a pointer to the removed element is returned.
.Va NULL
is returned if
.Fa elm
is not present in the tree.
.Pp
The
.Fn SPLAY_FIND
@ -296,7 +293,7 @@ macro can be used to find a particular element in the tree.
.Bd -literal -offset indent
struct TYPE find, *res;
find.key = 30;
res = SPLAY_FIND(NAME, head, &find);
res = SPLAY_FIND(NAME, &head, &find);
.Ed
.Pp
The
@ -313,8 +310,8 @@ for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
Or, for simplicity, one can use the
.Fn SPLAY_FOREACH
macro:
.Bd -ragged -offset indent
.Fn SPLAY_FOREACH np NAME head
.Bd -literal -offset indent
SPLAY_FOREACH(np, NAME, &head)
.Ed
.Pp
The
@ -324,29 +321,28 @@ macro should be used to check whether a splay tree is empty.
A red-black tree is a binary search tree with the node color as an
extra attribute.
It fulfills a set of conditions:
.Bl -enum -offset indent
.Pp
.Bl -enum -compact -offset indent
.It
Every search path from the root to a leaf consists of the same number of
black nodes.
every search path from the root to a leaf consists of the same number of
black nodes,
.It
Each red node (except for the root) has a black parent.
each red node (except for the root) has a black parent,
.It
Each leaf node is black.
each leaf node is black.
.El
.Pp
Every operation on a red-black tree is bounded as
.Fn O "lg n" .
The maximum height of a red-black tree is
.Fn 2lg "n + 1" .
Every operation on a red-black tree is bounded as O(lg n).
The maximum height of a red-black tree is 2lg (n+1).
.Pp
A red-black tree is headed by a structure defined by the
.Fn RB_HEAD
macro.
A
.Fa RB_HEAD
structure is declared as follows:
.Bd -ragged -offset indent
.Fn RB_HEAD HEADNAME TYPE
.Va head ;
.Bd -literal -offset indent
RB_HEAD(HEADNAME, TYPE) head;
.Ed
.Pp
where
@ -364,7 +360,7 @@ their prototypes need to be declared with the
.Fn RB_PROTOTYPE
or
.Fn RB_PROTOTYPE_STATIC
macro,
macros,
where
.Fa NAME
is a unique identifier for this particular tree.
@ -381,7 +377,7 @@ The function bodies are generated with the
.Fn RB_GENERATE
or
.Fn RB_GENERATE_STATIC
macro.
macros.
These macros take the same arguments as the
.Fn RB_PROTOTYPE
and
@ -391,16 +387,15 @@ macros, but should be used only once.
Finally,
the
.Fa CMP
argument is the name of a function used to compare tree nodes
argument is the name of a function used to compare trees' nodes
with each other.
The function takes two arguments of type
.Vt "struct TYPE *" .
.Fa "struct TYPE *" .
If the first argument is smaller than the second, the function returns a
value smaller than zero.
If they are equal, the function returns zero.
Otherwise, it should return a value greater than zero.
The compare
function defines the order of the tree elements.
The compare function defines the order of the tree elements.
.Pp
The
.Fn RB_INIT
@ -410,11 +405,8 @@ macro initializes the tree referenced by
The red-black tree can also be initialized statically by using the
.Fn RB_INITIALIZER
macro like this:
.Bd -ragged -offset indent
.Fn RB_HEAD HEADNAME TYPE
.Va head
=
.Fn RB_INITIALIZER &head ;
.Bd -literal -offset indent
RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
.Ed
.Pp
The
@ -422,6 +414,11 @@ The
macro inserts the new element
.Fa elm
into the tree.
Upon success,
.Va NULL
is returned.
If a matching element already exists in the tree, the insertion is
aborted, and a pointer to the existing element is returned.
.Pp
The
.Fn RB_REMOVE
@ -429,16 +426,24 @@ macro removes the element
.Fa elm
from the tree pointed by
.Fa head .
.Fn RB_REMOVE
returns
.Fa elm .
.Pp
The
.Fn RB_FIND
and
.Fn RB_NFIND
macros can be used to find a particular element in the tree.
.Fn RB_FIND
finds the node with the same key as
.Fa elm .
.Fn RB_NFIND
finds the first node greater than or equal to the search key.
.Bd -literal -offset indent
struct TYPE find, *res;
find.key = 30;
res = RB_FIND(NAME, head, &find);
res = RB_FIND(NAME, &head, &find);
.Ed
.Pp
The
@ -449,26 +454,122 @@ The
and
.Fn RB_PREV
macros can be used to traverse the tree:
.Pp
.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
.Bd -literal -offset indent
for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
.Ed
.Pp
Or, for simplicity, one can use the
.Fn RB_FOREACH
or
.Fn RB_FOREACH_REVERSE
macro:
.Bd -ragged -offset indent
.Fn RB_FOREACH np NAME head
macros:
.Bd -literal -offset indent
RB_FOREACH(np, NAME, &head)
.Ed
.Pp
The macros
.Fn RB_FOREACH_SAFE
and
.Fn RB_FOREACH_REVERSE_SAFE
traverse the tree referenced by head
in a forward or reverse direction respectively,
assigning each element in turn to np.
However, unlike their unsafe counterparts,
they permit both the removal of np
as well as freeing it from within the loop safely
without interfering with the traversal.
.Pp
The
.Fn RB_EMPTY
macro should be used to check whether a red-black tree is empty.
.Sh EXAMPLES
The following example demonstrates how to declare a red-black tree
holding integers.
Values are inserted into it and the contents of the tree are printed
in order.
Lastly, the internal structure of the tree is printed.
.Bd -literal -offset 3n
#include <sys/tree.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
RB_ENTRY(node) entry;
int i;
};
int intcmp(struct node *, struct node *);
void print_tree(struct node *);
int
intcmp(struct node *e1, struct node *e2)
{
return (e1->i < e2->i ? -1 : e1->i > e2->i);
}
RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
RB_PROTOTYPE(inttree, node, entry, intcmp)
RB_GENERATE(inttree, node, entry, intcmp)
int testdata[] = {
20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
7, 11, 14
};
void
print_tree(struct node *n)
{
struct node *left, *right;
if (n == NULL) {
printf("nil");
return;
}
left = RB_LEFT(n, entry);
right = RB_RIGHT(n, entry);
if (left == NULL && right == NULL)
printf("%d", n->i);
else {
printf("%d(", n->i);
print_tree(left);
printf(",");
print_tree(right);
printf(")");
}
}
int
main(void)
{
int i;
struct node *n;
for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
if ((n = malloc(sizeof(struct node))) == NULL)
err(1, NULL);
n->i = testdata[i];
RB_INSERT(inttree, &head, n);
}
RB_FOREACH(n, inttree, &head) {
printf("%d\en", n->i);
}
print_tree(RB_ROOT(&head));
printf("\en");
return (0);
}
.Ed
.Sh SEE ALSO
.Xr queue 3bsd
.Sh HISTORY
The tree macros first appeared in
.Fx 4.6 .
.Sh NOTES
Trying to free a tree in the following way is a common error:
.Bd -literal -offset indent
SPLAY_FOREACH(var, NAME, head) {
SPLAY_REMOVE(NAME, head, var);
SPLAY_FOREACH(var, NAME, &head) {
SPLAY_REMOVE(NAME, &head, var);
free(var);
}
free(head);
@ -476,36 +577,17 @@ free(head);
.Pp
Since
.Va var
is freed, the
is free'd, the
.Fn FOREACH
macro refers to a pointer that may have been reallocated already.
Proper code needs a second variable.
.Bd -literal -offset indent
for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
nxt = SPLAY_NEXT(NAME, head, var);
SPLAY_REMOVE(NAME, head, var);
for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) {
nxt = SPLAY_NEXT(NAME, &head, var);
SPLAY_REMOVE(NAME, &head, var);
free(var);
}
.Ed
.Pp
Both
.Fn RB_INSERT
and
.Fn SPLAY_INSERT
return
.Dv NULL
if the element was inserted in the tree successfully, otherwise they
return a pointer to the element with the colliding key.
.Pp
Accordingly,
.Fn RB_REMOVE
and
.Fn SPLAY_REMOVE
return the pointer to the removed element otherwise they return
.Dv NULL
to indicate an error.
.Sh SEE ALSO
.Xr queue 3bsd
.Sh AUTHORS
The author of the tree macros is
.An Niels Provos .

View File

@ -30,7 +30,7 @@
.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93
.\"
.Dd March 12, 2011
.Dt UNVIS 3bsd
.Dt unvis 3bsd
.Os
.Sh NAME
.Nm unvis ,

1
man/verrc.3bsd Normal file
View File

@ -0,0 +1 @@
.so man3/errc.3bsd

View File

@ -30,7 +30,7 @@
.\" @(#)vis.3 8.1 (Berkeley) 6/9/93
.\"
.Dd April 22, 2017
.Dt VIS 3bsd
.Dt vis 3bsd
.Os
.Sh NAME
.Nm vis ,

1
man/vwarnc.3bsd Normal file
View File

@ -0,0 +1 @@
.so man3/errc.3bsd

1
man/warnc.3bsd Normal file
View File

@ -0,0 +1 @@
.so man3/errc.3bsd

View File

@ -36,7 +36,7 @@
.\" $FreeBSD$
.\"
.Dd March 4, 2009
.Dt WCSLCPY 3bsd
.Dt wcslcpy 3bsd
.Os
.Sh NAME
.Nm wcslcat ,
@ -69,3 +69,14 @@ counterpart, such as
and
.Fn wcslcpy ,
which are BSD extensions.
.Sh HISTORY
The
.Fn wcslcpy
and
.Fn wcslcat
functions first appeared in
.Ox 3.8 ,
.Nx 1.6 ,
.Fx 5.0
and
.Dx 2.1 .

View File

@ -5,13 +5,14 @@ AM_CPPFLAGS = \
-isystem $(top_srcdir)/include/bsd/ \
-include $(top_builddir)/config.h \
-DLIBBSD_OVERLAY -DLIBBSD_DISABLE_DEPRECATED \
-D__REENTRANT
-D__REENTRANT \
# EOL
if OS_WINDOWS
AM_CPPFLAGS += \
-D_CRT_SECURE_NO_WARNINGS \
-D_CRT_NONSTDC_NO_WARNINGS \
$(nil)
# EOL
endif
libbsd_la_included_sources = \
@ -23,21 +24,22 @@ libbsd_la_included_sources = \
getentropy_osx.c \
getentropy_solaris.c \
getentropy_win.c \
$(nil)
# EOL
CLEANFILES =
EXTRA_DIST = \
libbsd.map \
libbsd.map.in \
libbsd.pc.in \
libbsd-ctor.pc.in \
libbsd-overlay.pc.in \
$(libbsd_la_included_sources) \
$(nil)
# EOL
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = \
libbsd.pc \
libbsd-overlay.pc \
$(nil)
# EOL
lib_LTLIBRARIES = libbsd.la
lib_LIBRARIES =
@ -48,100 +50,351 @@ pkgconfig_DATA += libbsd-ctor.pc
lib_LIBRARIES += libbsd-ctor.a
endif
libbsd_la_DEPENDENCIES = \
EXTRA_libbsd_la_DEPENDENCIES = \
$(libbsd_la_included_sources) \
libbsd.map
libbsd.map \
libbsd.map.in \
# EOL
libbsd_la_LIBADD = \
$(MD_LIBS) \
$(CLOCK_GETTIME_LIBS) \
$(ARC4RANDOM_ATFORK_LIBS) \
$(nil)
$(MD5_LIBS) \
$(LIBBSD_LIBS) \
# EOL
libbsd_la_LDFLAGS = \
-version-number $(LIBBSD_ABI)
-no-undefined \
-version-number $(SOVERSION) \
# EOL
if HAVE_LINKER_VERSION_SCRIPT
libbsd_la_LDFLAGS += \
-Wl,--version-script=$(srcdir)/libbsd.map
-Wl,--version-script=libbsd.map \
# EOL
else
libbsd_la_LDFLAGS += \
-export-symbols libbsd.sym \
# EOL
EXTRA_libbsd_la_DEPENDENCIES += \
libbsd.sym \
# EOL
endif
libbsd_la_SOURCES = \
arc4random.c \
arc4random.h \
arc4random_linux.h \
arc4random_openbsd.h \
arc4random_uniform.c \
arc4random_unix.h \
arc4random_win.h \
bsd_getopt.c \
chacha_private.h \
closefrom.c \
dehumanize_number.c \
err.c \
expand_number.c \
explicit_bzero.c \
fgetln.c \
freezero.c \
fgetwln.c \
flopen.c \
fmtcheck.c \
fparseln.c \
fpurge.c \
funopen.c \
getbsize.c \
getpeereid.c \
heapsort.c \
humanize_number.c \
inet_net_pton.c \
local-elf.h \
local-link.h \
md5.c \
merge.c \
nlist.c \
pidfile.c \
progname.c \
pwcache.c \
radixsort.c \
readpassphrase.c \
reallocarray.c \
reallocf.c \
recallocarray.c \
setmode.c \
setproctitle.c \
strlcat.c \
strlcpy.c \
stringlist.c \
strmode.c \
strnstr.c \
strtoi.c \
strtonum.c \
strtou.c \
timeconv.c \
unvis.c \
vis.c \
wcslcat.c \
wcslcpy.c \
$(nil)
# EOL
if ABI_ACCMODE
libbsd_la_SOURCES += \
setmode.c \
# EOL
endif
if ABI_ARC4RANDOM
if !HAVE_GETENTROPY
libbsd_la_SOURCES += \
getentropy.c \
$(nil)
# EOL
endif
libbsd_la_SOURCES += \
arc4random.c \
arc4random.h \
arc4random_linux.h \
arc4random_uniform.c \
arc4random_unix.h \
arc4random_win.h \
chacha_private.h \
# EOL
endif
if ABI_ASPRINTF
libbsd_la_SOURCES += \
asprintf.c \
vasprintf.c \
# EOL
endif
if ABI_BSD_GETOPT
libbsd_la_SOURCES += \
bsd_getopt.c \
# EOL
endif
if ABI_CLOSEFROM
libbsd_la_SOURCES += \
closefrom.c \
# EOL
endif
if ABI_ERR
libbsd_la_SOURCES += \
err.c \
# EOL
endif
if ABI_ERRC
libbsd_la_SOURCES += \
errc.c \
# EOL
endif
if ABI_EXPAND_NUMBER
libbsd_la_SOURCES += \
expand_number.c \
# EOL
endif
if ABI_EXPLICIT_BZERO
libbsd_la_SOURCES += \
explicit_bzero.c \
# EOL
endif
if ABI_FGETLN
libbsd_la_SOURCES += \
fgetln.c \
fgetwln.c \
fparseln.c \
# EOL
endif
if ABI_FLOPEN
libbsd_la_SOURCES += \
flopen.c \
# EOL
endif
if ABI_FMTCHECK
libbsd_la_SOURCES += \
fmtcheck.c \
# EOL
endif
if ABI_FPURGE
libbsd_la_SOURCES += \
fpurge.c \
# EOL
endif
if ABI_FREEZERO
libbsd_la_SOURCES += \
freezero.c \
# EOL
endif
if ABI_FUNOPEN
libbsd_la_SOURCES += \
funopen.c \
# EOL
endif
if ABI_GETBSIZE
libbsd_la_SOURCES += \
getbsize.c \
# EOL
endif
if ABI_GETPEEREID
libbsd_la_SOURCES += \
getpeereid.c \
# EOL
endif
if ABI_HUMANIZE_NUMBER
libbsd_la_SOURCES += \
dehumanize_number.c \
humanize_number.c \
# EOL
endif
if ABI_INET_NET_PTON
libbsd_la_SOURCES += \
inet_net_pton.c \
# EOL
endif
if ABI_MD5
libbsd_la_SOURCES += \
md5.c \
# EOL
endif
if ABI_NLIST
libbsd_la_SOURCES += \
nlist.c \
# EOL
endif
if ABI_PIDFILE
libbsd_la_SOURCES += \
pidfile.c \
# EOL
endif
if ABI_PROCTITLE
libbsd_la_SOURCES += \
setproctitle.c \
# EOL
endif
if ABI_PROGNAME
libbsd_la_SOURCES += \
progname.c \
# EOL
endif
if ABI_PWCACHE
libbsd_la_SOURCES += \
pwcache.c \
# EOL
endif
if ABI_READPASSPHRASE
libbsd_la_SOURCES += \
readpassphrase.c \
# EOL
endif
if ABI_REALLOCARRAY
libbsd_la_SOURCES += \
reallocarray.c \
# EOL
endif
if ABI_REALLOCF
libbsd_la_SOURCES += \
reallocf.c \
# EOL
endif
if ABI_RECALLOCARRAY
libbsd_la_SOURCES += \
recallocarray.c \
# EOL
endif
if ABI_SORT
libbsd_la_SOURCES += \
heapsort.c \
merge.c \
radixsort.c \
# EOL
endif
if ABI_STRINGLIST
libbsd_la_SOURCES += \
stringlist.c \
# EOL
endif
if ABI_STRL
libbsd_la_SOURCES += \
strlcat.c \
strlcpy.c \
# EOL
endif
if ABI_STRMODE
libbsd_la_SOURCES += \
strmode.c \
# EOL
endif
if ABI_STRNSTR
libbsd_la_SOURCES += \
strnstr.c \
# EOL
endif
if ABI_STRTONUM
libbsd_la_SOURCES += \
strtonum.c \
# EOL
endif
if ABI_STRTOX
libbsd_la_SOURCES += \
strtoi.c \
strtou.c \
# EOL
endif
if ABI_TIMECONV
libbsd_la_SOURCES += \
timeconv.c \
# EOL
endif
if ABI_VIS
libbsd_la_SOURCES += \
unvis.c \
vis.c \
# EOL
endif
if ABI_WCSL
libbsd_la_SOURCES += \
wcslcat.c \
wcslcpy.c \
# EOL
endif
if ABI_TRANSPARENT_LIBMD
CLEANFILES += \
format.ld \
# EOL
endif
DISTCLEANFILES = \
libbsd.sym \
libbsd.map \
# EOL
libbsd_ctor_a_SOURCES = \
setproctitle_ctor.c \
$(nil)
# EOL
# Generate the library map file with the pre-processor to selectively include
# symbols depending on the host system, otherwise some linkers might fail.
libbsd.map: libbsd.map.in
$(AM_V_GEN) $(CPP) $(AM_CPPFLAGS) $(CPPFLAGS) -P - <$(srcdir)/libbsd.map.in >$@
# Generate a simple libtool symbol export list to be used as a fallback if
# there is no version script support.
libbsd.sym: libbsd.map
$(AM_V_GEN) $(SED) -ne 's/^[[:space:]]\{1,\}\([A-Za-z0-9_]\{1,\}\);/\1/p' libbsd.map >$@
if ABI_TRANSPARENT_LIBMD
TRANSPARENT_LIBMD_DEPENDS = format.ld
format.ld:
$(CC) -shared -nostdlib -nostartfiles -x assembler /dev/null -o $@.so
$(OBJDUMP) -f $@.so | sed -n 's/.*file format \(.*\)/OUTPUT_FORMAT(\1)/;T;p' >$@
rm -f $@.so
endif
runtimelibdir = $(libdir)
install-exec-hook:
install-exec-hook: $(TRANSPARENT_LIBMD_DEPENDS)
if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
$(MKDIR_P) $(DESTDIR)$(runtimelibdir); \
mv $(DESTDIR)$(libdir)/libbsd*.so.* \
$(DESTDIR)$(runtimelibdir)/; \
fi
if ABI_TRANSPARENT_LIBMD
# The "GNU ld script" magic is required so that GNU ldconfig does not complain
# about an unknown format file.
soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
$(RM) $(DESTDIR)$(libdir)/libbsd.so; \
(echo '/* GNU ld script'; \
echo ' * The MD5 functions are provided by the libmd library. */'; \
cat format.ld; \
echo "GROUP($(runtimelibdir)/$$soname AS_NEEDED($(MD5_LIBS)))"; \
)>$(DESTDIR)$(libdir)/libbsd.so
else
if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
sorelprefix=`echo $(libdir) | sed -r -e 's:(^/)?[^/]+:..:g'`; \
sorelprefix=`echo $(libdir) | $(SED) -r -e 's:(^/)?[^/]+:..:g'`; \
ln -sf $$sorelprefix$(runtimelibdir)/$$soname \
$(DESTDIR)$(libdir)/libbsd.so; \
fi
endif
uninstall-hook:
rm -f $(DESTDIR)$(runtimelibdir)/libbsd*.so*

View File

@ -1,4 +1,4 @@
/* $OpenBSD: arc4random.c,v 1.53 2015/09/10 18:53:50 bcook Exp $ */
/* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
@ -50,6 +50,8 @@
#define BLOCKSZ 64
#define RSBUFSZ (16*BLOCKSZ)
#define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
static struct _rs {
size_t rs_have; /* valid bytes at end of rs_buf */
@ -74,10 +76,10 @@ _rs_init(unsigned char *buf, size_t n)
if (rs == NULL) {
if (_rs_allocate(&rs, &rsx) == -1)
abort();
_exit(1);
}
chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
}
@ -108,6 +110,7 @@ static void
_rs_stir(void)
{
unsigned char rnd[KEYSZ + IVSZ];
uint32_t rekey_fuzz = 0;
if (getentropy(rnd, sizeof rnd) == -1)
_getentropy_fail();
@ -122,7 +125,10 @@ _rs_stir(void)
rs->rs_have = 0;
memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
rs->rs_count = 1600000;
/* rekey interval should not be predictable */
chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
(uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
}
static inline void

View File

@ -32,9 +32,7 @@
int
getentropy(void *buf, size_t len);
#if defined(__OpenBSD__)
#include "arc4random_openbsd.h"
#elif defined(__linux__)
#if defined(__linux__)
#include "arc4random_linux.h"
#elif defined(_WIN32)
#include "arc4random_win.h"

View File

@ -1,61 +0,0 @@
/* $OpenBSD: arc4random.h,v 1.3 2014/07/20 20:51:13 bcook Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
* Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
* Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Stub functions for portability.
*/
#include <sys/mman.h>
#include <signal.h>
#include "thread_private.h"
static inline void
_getentropy_fail(void)
{
raise(SIGKILL);
}
static inline int
_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
{
struct {
struct _rs rs;
struct _rsx rsx;
} *p;
if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
return (-1);
if (minherit(p, sizeof(*p), MAP_INHERIT_ZERO) == -1) {
munmap(p, sizeof(*p));
return (-1);
}
*rsp = &p->rs;
*rsxp = &p->rsx;
return (0);
}
static inline void
_rs_forkdetect(void)
{
}

43
src/asprintf.c Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright © 2010-2024 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <stdarg.h>
#include <stdio.h>
int
asprintf(char **strp, char const *fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vasprintf(strp, fmt, args);
va_end(args);
return n;
}

View File

@ -4,7 +4,7 @@ D. J. Bernstein
Public domain.
*/
/* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */
/* $OpenBSD: chacha_private.h,v 1.3 2022/02/28 21:56:29 dtucker Exp $ */
typedef unsigned char u8;
typedef unsigned int u32;
@ -48,11 +48,19 @@ typedef struct
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
static const char sigma[16] = "expand 32-byte k";
static const char tau[16] = "expand 16-byte k";
#if defined(__has_attribute)
#if __has_attribute(__nonstring__)
#define ATTRIBUTE_NONSTRING __attribute__((__nonstring__))
#else
#define ATTRIBUTE_NONSTRING
#endif
#endif
static const char sigma[16] ATTRIBUTE_NONSTRING = "expand 32-byte k";
static const char tau[16] ATTRIBUTE_NONSTRING = "expand 16-byte k";
static void
chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits)
{
const char *constants;

View File

@ -1,6 +1,8 @@
/*
* Copyright (c) 2004-2005, 2007, 2010, 2012-2014
* Todd C. Miller <Todd.Miller@courtesan.com>
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -17,19 +19,17 @@
#include <config.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
#ifdef __linux__
# include <sys/syscall.h>
# if defined(__NR_close_range) && !defined(SYS_close_range)
# define SYS_close_range __NR_close_range
# endif
#endif /* STDC_HEADERS */
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_PSTAT_GETPROC
# include <sys/param.h>
# include <sys/pstat.h>
@ -56,10 +56,6 @@
# define OPEN_MAX 256
#endif
#if defined(HAVE_FCNTL_CLOSEM) && !defined(HAVE_DIRFD)
# define closefrom closefrom_fallback
#endif
static inline void
closefrom_close(int fd)
{
@ -71,56 +67,64 @@ closefrom_close(int fd)
#endif
}
#if defined(__linux__) && defined(SYS_close_range)
static inline int
sys_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags)
{
return syscall(SYS_close_range, fd, max_fd, flags);
}
#endif
/*
* Close all file descriptors greater than or equal to lowfd.
* This is the expensive (fallback) method.
*/
void
static void
closefrom_fallback(int lowfd)
{
long fd, maxfd;
/*
* Fall back on sysconf() or getdtablesize(). We avoid checking
* resource limits since it is possible to open a file descriptor
* and then drop the rlimit such that it is below the open fd.
* Fall back on sysconf(_SC_OPEN_MAX) or getdtablesize(). This is
* equivalent to checking the RLIMIT_NOFILE soft limit. It is
* possible for there to be open file descriptors past this limit
* but there is not much we can do about that since the hard limit
* may be RLIM_INFINITY (LLONG_MAX or ULLONG_MAX on modern systems).
*/
#ifdef HAVE_SYSCONF
maxfd = sysconf(_SC_OPEN_MAX);
#else
maxfd = getdtablesize();
#endif /* HAVE_SYSCONF */
if (maxfd < 0)
if (maxfd < OPEN_MAX)
maxfd = OPEN_MAX;
/* Make sure we did not get RLIM_INFINITY as the upper limit. */
if (maxfd > INT_MAX)
maxfd = INT_MAX;
for (fd = lowfd; fd < maxfd; fd++)
closefrom_close(fd);
}
/*
* Close all file descriptors greater than or equal to lowfd.
* We try the fast way first, falling back on the slow method.
*/
#if defined(HAVE_FCNTL_CLOSEM)
void
closefrom(int lowfd)
#if defined(HAVE_PSTAT_GETPROC)
static int
closefrom_pstat(int lowfd)
{
if (fcntl(lowfd, F_CLOSEM, 0) == -1)
closefrom_fallback(lowfd);
}
#elif defined(HAVE_PSTAT_GETPROC)
void
closefrom(int lowfd)
{
struct pst_status pstat;
struct pst_status pst;
int fd;
if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1) {
for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
/*
* EOVERFLOW is not a fatal error for the fields we use.
* See the "EOVERFLOW Error" section of pstat_getvminfo(3).
*/
if (pstat_getproc(&pst, sizeof(pst), 0, getpid()) != -1 ||
errno == EOVERFLOW) {
for (fd = lowfd; fd <= pst.pst_highestfd; fd++)
(void)close(fd);
} else {
closefrom_fallback(lowfd);
return 0;
}
return -1;
}
#elif defined(HAVE_DIRFD)
static int
@ -135,8 +139,8 @@ closefrom_procfs(int lowfd)
int ret = 0;
int i;
/* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
/* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */
# ifdef __APPLE__
path = "/dev/fd";
# else
path = "/proc/self/fd";
@ -149,6 +153,9 @@ closefrom_procfs(int lowfd)
const char *errstr;
int fd;
if (dent->d_name[0] == '.')
continue;
fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
if (errstr != NULL || fd == dirfd(dirp))
continue;
@ -180,13 +187,36 @@ closefrom_procfs(int lowfd)
return ret;
}
#endif
/*
* Close all file descriptors greater than or equal to lowfd.
* We try the fast way first, falling back on the slow method.
*/
void
closefrom(int lowfd)
{
if (closefrom_procfs(lowfd) == 0)
return;
if (lowfd < 0)
lowfd = 0;
/* Try the fast methods first, if possible. */
#if defined(HAVE_FCNTL_CLOSEM)
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
return;
#endif /* HAVE_FCNTL_CLOSEM */
#if defined(__linux__) && defined(SYS_close_range)
if (sys_close_range(lowfd, UINT_MAX, 0) == 0)
return;
#endif
#if defined(HAVE_PSTAT_GETPROC)
if (closefrom_pstat(lowfd) != -1)
return;
#elif defined(HAVE_DIRFD)
if (closefrom_procfs(lowfd) != -1)
return;
#endif /* HAVE_DIRFD */
/* Do things the slow way. */
closefrom_fallback(lowfd);
}
#endif /* HAVE_FCNTL_CLOSEM */

View File

@ -1,6 +1,5 @@
/*
* Copyright © 2006 Robert Millan
* Copyright © 2011, 2019 Guillem Jover <guillem@hadrons.org>
* Copyright © 2019 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -26,58 +25,12 @@
*/
#include <err.h>
#ifdef LIBBSD_NEED_ERR_H_FUNCS
#include <errno.h>
#endif
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
void
vwarnc(int code, const char *format, va_list ap)
{
fprintf(stderr, "%s: ", getprogname());
if (format) {
vfprintf(stderr, format, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(code));
}
void
warnc(int code, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vwarnc(code, format, ap);
va_end(ap);
}
void
verrc(int status, int code, const char *format, va_list ap)
{
fprintf(stderr, "%s: ", getprogname());
if (format) {
vfprintf(stderr, format, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(code));
exit(status);
}
void
errc(int status, int code, const char *format, ...)
{
va_list ap;
va_start(ap, format);
verrc(status, code, format, ap);
va_end(ap);
}
#ifdef LIBBSD_NEED_ERR_H_FUNCS
void
vwarn(const char *format, va_list ap)
{
@ -148,4 +101,3 @@ errx(int eval, const char *format, ...)
verrx(eval, format, ap);
va_end(ap);
}
#endif

75
src/errc.c Normal file
View File

@ -0,0 +1,75 @@
/*
* Copyright © 2006 Robert Millan
* Copyright © 2011, 2019 Guillem Jover <guillem@hadrons.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <err.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
void
vwarnc(int code, const char *format, va_list ap)
{
fprintf(stderr, "%s: ", getprogname());
if (format) {
vfprintf(stderr, format, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(code));
}
void
warnc(int code, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vwarnc(code, format, ap);
va_end(ap);
}
void
verrc(int status, int code, const char *format, va_list ap)
{
fprintf(stderr, "%s: ", getprogname());
if (format) {
vfprintf(stderr, format, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(code));
exit(status);
}
void
errc(int status, int code, const char *format, ...)
{
va_list ap;
va_start(ap, format);
verrc(status, code, format, ap);
va_end(ap);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
/* $OpenBSD: explicit_bzero.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
/*
* Public domain.
* Written by Matthew Dempsky.
@ -6,6 +6,9 @@
#include <string.h>
__attribute__((__weak__)) void
__explicit_bzero_hook(void *, size_t);
__attribute__((__weak__)) void
__explicit_bzero_hook(void *buf, size_t len)
{

View File

@ -25,10 +25,11 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include "local-link.h"
@ -76,8 +77,8 @@ fgetln(FILE *stream, size_t *len)
}
}
libbsd_link_warning(fgetln,
"This function cannot be safely ported, use getline(3) "
"instead, as it is supported by GNU and POSIX.1-2008.")
"The fgetln() function cannot be safely ported, use getline(3) "
"instead, as it is supported by GNU and POSIX.1-2008.");
#else
#error "Function fgetln() needs to be ported."
#endif

View File

@ -87,6 +87,9 @@ fgetwln(FILE *stream, size_t *lenp)
*lenp = wused;
return wused ? fb->wbuf : NULL;
}
/* XXX: Ideally we'd recommend getwline(3), but unfortunately even though it
* was part of the ISO/IEC TR 24731-2:2010 draft, it did not make it into C11
* and is not widely implemented. */
libbsd_link_warning(fgetwln,
"This function cannot be safely ported, use fgetwc(3) "
"instead, as it is supported by C99 and POSIX.1-2001.")
"The fgetwln() function cannot be safely ported, use fgetwc(3) "
"instead, as it is supported by C99 and POSIX.1-2001.");

View File

@ -38,6 +38,27 @@
#include <libutil.h>
static int
lock_file(int fd, int flags)
{
int operation;
#if HAVE_FLOCK
operation = LOCK_EX;
if (flags & O_NONBLOCK)
operation |= LOCK_NB;
return flock(fd, operation);
#else
if (flags & O_NONBLOCK)
operation = F_TLOCK;
else
operation = F_LOCK;
return lockf(fd, operation, 0);
#endif
}
/*
* Reliably open and lock a file.
*
@ -49,7 +70,7 @@
static int
vflopenat(int dirfd, const char *path, int flags, va_list ap)
{
int fd, operation, serrno, trunc;
int fd, serrno, trunc;
struct stat sb, fsb;
mode_t mode;
@ -62,10 +83,6 @@ vflopenat(int dirfd, const char *path, int flags, va_list ap)
mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
}
operation = LOCK_EX;
if (flags & O_NONBLOCK)
operation |= LOCK_NB;
trunc = (flags & O_TRUNC);
flags &= ~O_TRUNC;
@ -73,7 +90,7 @@ vflopenat(int dirfd, const char *path, int flags, va_list ap)
if ((fd = openat(dirfd, path, flags, mode)) == -1)
/* non-existent or no access */
return (-1);
if (flock(fd, operation) == -1) {
if (lock_file(fd, flags) == -1) {
/* unsupported or interrupted */
serrno = errno;
(void)close(fd);

View File

@ -26,13 +26,15 @@
#include <errno.h>
#include <stdio.h>
#if HAVE_STDIO_EXT_H
#include <stdio_ext.h>
#endif
#ifdef HAVE___FPURGE
int
fpurge(FILE *fp)
{
if (fp == NULL || fileno(fp) < 0) {
if (fp == NULL) {
errno = EBADF;
return EOF;
}

View File

@ -65,7 +65,7 @@ funopen_write(void *cookie, const char *buf, size_t size)
}
static int
funopen_seek(void *cookie, off64_t *offset, int whence)
funopen_seek(void *cookie, off_t *offset, int whence)
{
struct funopen_cookie *cookiewrap = cookie;
off_t soff = *offset;
@ -137,12 +137,6 @@ funopen(const void *cookie,
return fopencookie(cookiewrap, mode, funcswrap);
}
#elif defined(__MUSL__)
/*
* This is unimplementable on musl based systems, and upstream has stated
* they will not add the needed support to implement it. Just ignore this
* interface there, as it has never been provided anyway.
*/
#else
#error "Function funopen() needs to be ported or disabled."
#error "Function funopen() needs to be ported."
#endif

View File

@ -26,6 +26,8 @@
#include <errno.h>
#include <stddef.h>
int getentropy(void *buf, size_t len);
/*
* Derived from lib/libc/gen/arc4random.c from FreeBSD.
*/

Some files were not shown because too many files have changed in this diff Show More