mirror of
https://github.com/troglobit/libite.git
synced 2026-10-14 10:19:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1b3b76ea3 | ||
|
|
8108b8ec14 | ||
|
|
0e2d365eb9 | ||
|
|
8b499aa9cd | ||
|
|
f4fa44fe60 | ||
|
|
be08bcd09d | ||
|
|
1495a3f858 | ||
|
|
5eb816b4c0 | ||
|
|
456c58d52d | ||
|
|
59869b876e | ||
|
|
113c3487bd | ||
|
|
de73e268d3 | ||
|
|
b57c257f09 | ||
|
|
94d42b3df7 | ||
|
|
94ad65a121 | ||
|
|
f3f6947604 | ||
|
|
613d397161 | ||
|
|
7df1b75fa0 | ||
|
|
9b1b7ca4cf | ||
|
|
1e064b8b4d | ||
|
|
18dde87f0a | ||
|
|
fcb38c9cc5 | ||
|
|
ab69ae8374 | ||
|
|
95f7a91a8a | ||
|
|
aab5a1145c | ||
|
|
fe1a3a731d | ||
|
|
cd073f5221 | ||
|
|
1a9d3bd91b | ||
|
|
8cc267f259 | ||
|
|
35d396ec27 | ||
|
|
7e91307138 | ||
|
|
2b8f54fbc7 | ||
|
|
2e58dd3203 | ||
|
|
41392f7e85 | ||
|
|
e4329d6c25 | ||
|
|
7113b3d1d8 | ||
|
|
9b6ef1f053 | ||
|
|
efc70e3a75 | ||
|
|
147df0c6da | ||
|
|
7f89000c6c | ||
|
|
7607148d46 | ||
|
|
979f76aea7 | ||
|
|
ac41fa70aa | ||
|
|
5f820b17b6 | ||
|
|
53b715c094 | ||
|
|
1b4cd7b20b | ||
|
|
8e379ca358 | ||
|
|
12241dcdfe | ||
|
|
0ce1b3bf44 | ||
|
|
38ff0af61a | ||
|
|
00737a5b74 | ||
|
|
1b8b164ed4 | ||
|
|
9f279c0fec | ||
|
|
4d7e6d4f78 | ||
|
|
0a57f2128f | ||
|
|
e254db48a8 | ||
|
|
0fe429dcbe | ||
|
|
f1a2e01775 | ||
|
|
e5eef2ae59 | ||
|
|
f9caffc9b7 | ||
|
|
604b503b84 | ||
|
|
5fc248522c | ||
|
|
c0b8bab164 | ||
|
|
c0e9c0431a | ||
|
|
efdd61648a | ||
|
|
2e8e64fd2a | ||
|
|
c4109e7e74 | ||
|
|
5d07594709 | ||
|
|
253460b4b8 | ||
|
|
acc59e0c1f | ||
|
|
90374fc237 | ||
|
|
7f375b6e9d | ||
|
|
e4236db2bd | ||
|
|
42a5a906fc | ||
|
|
10b28127f5 | ||
|
|
d265a05422 | ||
|
|
f07c05138d | ||
|
|
e1f8224666 | ||
|
|
84650d8836 | ||
|
|
b4fbd54a8c | ||
|
|
c14c6a1e62 | ||
|
|
8aecba2b7a | ||
|
|
74dcaddc55 | ||
|
|
c5a36ff7bd | ||
|
|
3371e42035 | ||
|
|
4e57a78255 | ||
|
|
ce3bd4ec8a | ||
|
|
805571a7ef | ||
|
|
36435997bf | ||
|
|
f961889724 | ||
|
|
6605fb9771 | ||
|
|
61254279b9 | ||
|
|
e42aca8856 | ||
|
|
16611f3e5f | ||
|
|
975df87d6b | ||
|
|
148027674a |
No files matched your search
+19
@@ -1,5 +1,24 @@
|
|||||||
|
*~
|
||||||
*.d
|
*.d
|
||||||
*.o
|
*.o
|
||||||
*.so
|
*.so
|
||||||
*.so.*
|
*.so.*
|
||||||
*.a
|
*.a
|
||||||
|
.deps
|
||||||
|
.libs
|
||||||
|
tmp/
|
||||||
|
compile
|
||||||
|
config.*
|
||||||
|
configure
|
||||||
|
depcomp
|
||||||
|
install-sh
|
||||||
|
libtool
|
||||||
|
ltmain.sh
|
||||||
|
missing
|
||||||
|
stamp-h1
|
||||||
|
Makefile
|
||||||
|
Makefile.in
|
||||||
|
aclocal.m4
|
||||||
|
ar-lib
|
||||||
|
autom4te.cache
|
||||||
|
libite*
|
||||||
+11
-3
@@ -4,8 +4,16 @@ language: c
|
|||||||
|
|
||||||
# Some of the autotests may use sudo, so use non-dockerized build atm.
|
# Some of the autotests may use sudo, so use non-dockerized build atm.
|
||||||
# http://stackoverflow.com/questions/26299552/travis-sudo-is-disabled
|
# http://stackoverflow.com/questions/26299552/travis-sudo-is-disabled
|
||||||
sudo: true
|
#sudo: true
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
# Test build with both GCC and Clang (LLVM)
|
||||||
|
compiler:
|
||||||
|
- gcc
|
||||||
|
- clang
|
||||||
|
|
||||||
|
# - make test
|
||||||
script:
|
script:
|
||||||
- make
|
- ./autogen.sh
|
||||||
- make test
|
- ./configure --disable-silent-rules
|
||||||
|
- make -j5 clean all
|
||||||
+204
@@ -0,0 +1,204 @@
|
|||||||
|
Change Log
|
||||||
|
==========
|
||||||
|
|
||||||
|
All notable changes to the project are documented in this file.
|
||||||
|
|
||||||
|
[v1.7.0][] - 2016-06-05
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Add support for GNU `strdupa()`, for [Inadyn][] on NetBSD.
|
||||||
|
- Add support for GNU `strndupa()` and `strnlen()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
|
||||||
|
- Fix `mkpath()` to handle creating relative directories.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.6.0][] - 2016-05-13
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Add support for NetBSD `fparaseln()` for the benefit of [Mg][]
|
||||||
|
- Add support for OpenBSD `reallocarray()` for the benefit of [Mg][]
|
||||||
|
- Constify `fisdir()` argument
|
||||||
|
|
||||||
|
|
||||||
|
[v1.5.1][] - 2016-04-17
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
Minor fix release.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Update README.md, add help when libite is used in autotools projects.
|
||||||
|
Spell checking and minor cleanup.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Activate use of `utimensat()` when `pidfile()` is called multiple
|
||||||
|
times. This reduces the overhead of calling open+write+close to
|
||||||
|
simply updating the mtime of the PID file.
|
||||||
|
- Westermo fixes for `fcopyfile()`: nothing copied on embedded PPC
|
||||||
|
system when using `tmpfile()` as source.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.5.0][] - 2016-03-20
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
This release changes the header file namespace. Apologies for any
|
||||||
|
problems this may cause!
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Change namespace for header files: `libite/` to `lite/`, e.g. using
|
||||||
|
`pkg-config` your C program must now `#include <lite/lite.h>`
|
||||||
|
- Update `pidfile()` to use `utimensat()` to update atime+mtime instead
|
||||||
|
of opening and writing PID again. Less overhead and nanonsecond res.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.4][] - 2016-02-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add `ibite.pc.in` for `pkg-config` support.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #3: `tempfile()` fails with bad `O_TMPFILE` on some embedded
|
||||||
|
targets, in particular PowerPC. On systems with older GLIBC but with
|
||||||
|
a sufficiently new kernel `O_TMPFILE` is missing and the local libite
|
||||||
|
definition was wrong, hard coded for x86. Fixed by Johan Askerin.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.3][] - 2016-02-02
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Remove symlinks to Markdown files from GIT
|
||||||
|
- Distribute and install Markdown files: README.md, etc.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.2][] - 2016-01-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #2: GCC6 `-Wmisleading-indentation` causing FTBFS in pimd,
|
||||||
|
which enables `-Werror`
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.1][] - 2016-01-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix install path for include files, regression in v1.4.0 With the
|
||||||
|
introduction of GNU autotools include file namespace was unfortunately
|
||||||
|
lost. All include files were therefore installed in $(prefix)/include
|
||||||
|
instead of $(prefix)/include/libite.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.0][] - 2016-01-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Change to GNU configure and build system
|
||||||
|
- Add Niels Provos' splay and red-black tree implementation from OpenBSD
|
||||||
|
`sys/tree.h`, r1.14
|
||||||
|
- Make `pidfile()` file name accessible as `__pidfile_name`
|
||||||
|
- Step ABI revision to 2.1, with help from http://250bpm.com/blog:41
|
||||||
|
|
||||||
|
|
||||||
|
[v1.3.0][] - 2016-01-07
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add highly useful typeless `min()`/`max()` macros to `lite.h`,
|
||||||
|
courtesy of Tobias Waldekranz
|
||||||
|
- Add BSD `queue.h` and Borland inspired `conio.h` to `install-dev`
|
||||||
|
target. Needed by [Finit][].
|
||||||
|
- Note, this release update `queue.h` to OpenBSD v1.43, which removes
|
||||||
|
support for circular queues (CIRCLEQ).
|
||||||
|
- Update `strlcpy.c` to OpenBSD v1.12, readability fixes.
|
||||||
|
- Update `strlcat.c` to OpenBSD v1.14, readability fixes.
|
||||||
|
- Update `strtonum.c` to OpenBSD v1.7, tiniest of style tweaks.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Only update mtime if `pidfile()` is called more than once, do not
|
||||||
|
install multiple `atexit()` handlers.
|
||||||
|
- Add `#ifndef pidfile`, as for other BSD functions, in case the user
|
||||||
|
already have a local copy, or a C library with one.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.2.0][] - 2015-11-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add simple parser for UNIX configuration files, e.g. `/etc/group`,
|
||||||
|
`/etc/passwd`, and `/etc/protocols`:
|
||||||
|
- `lfopen()`
|
||||||
|
- `lfclose()`
|
||||||
|
- `lftok()`
|
||||||
|
- `lfgetkey()`
|
||||||
|
- `lfgetint()`
|
||||||
|
- `fgetint()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Do not allow `VERSION` to be overloaded by build system.
|
||||||
|
- Make sure we don't inherit `LDFLAGS` from environment.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.1.1][] - 2015-09-27
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
* Silence annoying warnings from GNU ar
|
||||||
|
* Fix macro definitions for compat APIs
|
||||||
|
|
||||||
|
|
||||||
|
[v1.1.0][] - 2015-09-16
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Support for `make distdir` used by automake based projects
|
||||||
|
- `lite.h` move from `error.h` to more BSD friendly `err.h`
|
||||||
|
- Add highly useful `NELEMS()` and bitmanip macros
|
||||||
|
- Lots of autotests added
|
||||||
|
- Add support for running autotests from Travis
|
||||||
|
- Add `progress()` simple progress bar
|
||||||
|
- Add `tree()` simple command line tree replacement
|
||||||
|
- Bump `SONAME` to `libite.so.2` on behalf of [Finit][]
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `pidfile()` upgrade to OpenBSD r1.11
|
||||||
|
- Lots of README updates, fixes and additions
|
||||||
|
|
||||||
|
|
||||||
|
[v1.0.0][] - 2015-07-12
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
Initial extraction of frog DNA from [Finit][]. See [README][] for API details.
|
||||||
|
|
||||||
|
|
||||||
|
[UNRELEASED]: https://github.com/troglobit/libite/compare/v1.7.0...HEAD
|
||||||
|
[v1.7.0]: https://github.com/troglobit/libite/compare/v1.6.0...v1.7.0
|
||||||
|
[v1.6.0]: https://github.com/troglobit/libite/compare/v1.5.1...v1.6.0
|
||||||
|
[v1.5.1]: https://github.com/troglobit/libite/compare/v1.5.0...v1.5.1
|
||||||
|
[v1.5.0]: https://github.com/troglobit/libite/compare/v1.4.4...v1.5.0
|
||||||
|
[v1.4.4]: https://github.com/troglobit/libite/compare/v1.4.3...v1.4.4
|
||||||
|
[v1.4.3]: https://github.com/troglobit/libite/compare/v1.4.2...v1.4.3
|
||||||
|
[v1.4.2]: https://github.com/troglobit/libite/compare/v1.4.1...v1.4.2
|
||||||
|
[v1.4.1]: https://github.com/troglobit/libite/compare/v1.4.0...v1.4.1
|
||||||
|
[v1.4.0]: https://github.com/troglobit/libite/compare/v1.3.0...v1.4.0
|
||||||
|
[v1.3.0]: https://github.com/troglobit/libite/compare/v1.2.0...v1.3.0
|
||||||
|
[v1.2.0]: https://github.com/troglobit/libite/compare/v1.1.1...v1.2.0
|
||||||
|
[v1.1.1]: https://github.com/troglobit/libite/compare/v1.1.0...v1.1.1
|
||||||
|
[v1.1.0]: https://github.com/troglobit/libite/compare/v1.0.0...v1.1.0
|
||||||
|
[v1.0.0]: https://github.com/troglobit/libite/compare/TAIL...v1.0.0
|
||||||
|
[Finit]: https://github.com/troglobit/finit/
|
||||||
|
[Inadyn]: https://github.com/troglobit/inadyn/
|
||||||
|
[Mg]: https://github.com/troglobit/mg
|
||||||
|
[README]: https://github.com/troglobit/libite/blob/v1.0.0/README.md
|
||||||
|
|
||||||
|
<!--
|
||||||
|
-- Local Variables:
|
||||||
|
-- mode: markdown
|
||||||
|
-- End:
|
||||||
|
-->
|
||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
Maintenance and Release Checklist
|
||||||
|
=================================
|
||||||
|
|
||||||
|
Maintenance
|
||||||
|
-----------
|
||||||
|
|
||||||
|
* Leverage GitHub issues for milestone planning
|
||||||
|
* Reference issues from GitHub pull requests to alert issue subscribers
|
||||||
|
* Bump library ABI version just before release!
|
||||||
|
|
||||||
|
|
||||||
|
Release Checklist
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
* Update ChangeLog, follow http://keepachangelog.com/ loosely
|
||||||
|
- Inform users in a plain language of changes and bug fixes
|
||||||
|
- Do *not* copy-paste GIT commit logs!
|
||||||
|
- Order entrys according to importance, most relevant first
|
||||||
|
* Do we need to bump the ABI version? (Probably, see below)
|
||||||
|
* Tag
|
||||||
|
* Push last commit(s) *and* tags to GitHub
|
||||||
|
* Make release
|
||||||
|
|
||||||
|
make distclean
|
||||||
|
./autogen.sh
|
||||||
|
./configure
|
||||||
|
make release
|
||||||
|
|
||||||
|
* Create new release in GitHub releases page
|
||||||
|
* Copy and paste ChangeLog entry, check any stale links!
|
||||||
|
* Upload release tarball and MD5 files
|
||||||
|
|
||||||
|
|
||||||
|
Library Versioning
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Libite (-lite) relies on GNU Libtool for building the library. For a
|
||||||
|
user of the library it is important to maintain a clear ABI versioning
|
||||||
|
scheme. This is not the same as the library version, but rather the
|
||||||
|
library "compatibility level".
|
||||||
|
|
||||||
|
The ABI version is specified in `src/Makefile.am` and looks like this:
|
||||||
|
|
||||||
|
libite_la_LDFLAGS = -version-info 0:0:0
|
||||||
|
\ \ `-- age
|
||||||
|
\ `--- revision
|
||||||
|
`---- current
|
||||||
|
|
||||||
|
It must be updated according to the [GNU Libtool recommendations][1]:
|
||||||
|
|
||||||
|
1. Start with version information of `0:0:0` for each libtool library.
|
||||||
|
2. Update the version information only immediately before a public
|
||||||
|
release of your software. More frequent updates are unnecessary, and
|
||||||
|
only guarantee that the current interface number gets larger faster.
|
||||||
|
3. If the library *source code has changed at all* since the last update,
|
||||||
|
then increment revision (`c:r:a` becomes `c:r+1:a`).
|
||||||
|
4. If any *interfaces have been added, removed, or changed* since the
|
||||||
|
last update, increment current, and set revision to 0.
|
||||||
|
5. If any *interfaces have been added* since the last public release,
|
||||||
|
then increment age.
|
||||||
|
6. If any *interfaces have been removed or changed* since the last
|
||||||
|
public release, then set age to 0.
|
||||||
|
|
||||||
|
Usually, non-developers have no interest in running development versions
|
||||||
|
(releases are frequent enough), and developers are expected to know how
|
||||||
|
to juggle versions. In such an ideal world, it is good enough to bump
|
||||||
|
the library version just prior to a release, point 2.
|
||||||
|
|
||||||
|
However, if releases are few and far between, distributors may start to
|
||||||
|
use snapshots. When a distributor uses a snapshot, the distributor has
|
||||||
|
to handle the library version manually. Things can get ugly when the
|
||||||
|
distributor has released an intermediate version with a bumped library
|
||||||
|
version, and when the official release is bumped to that version, the
|
||||||
|
distributor will then have to bump the library version for the official
|
||||||
|
release, and it can be confusing if someone reports bugs on versions
|
||||||
|
that you didn't even know existed.
|
||||||
|
|
||||||
|
The problem with bumping the version with every change is that if your
|
||||||
|
interface is not finished, the version number might run away, and it
|
||||||
|
looks pretty bad if a library is at version 262. It kind of tells the
|
||||||
|
user that the library interface is volatile, which is not good for
|
||||||
|
business.
|
||||||
|
|
||||||
|
[1]: https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
|
||||||
@@ -4,7 +4,7 @@ the Finit code base and included pieces of software released under the ISC
|
|||||||
and BSD licenses. See each respective file for license details.
|
and BSD licenses. See each respective file for license details.
|
||||||
|
|
||||||
Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
Copyright (c) 2008-2016 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -1,127 +0,0 @@
|
|||||||
# libite -- Light-weight utility functions and C-library extensions
|
|
||||||
#
|
|
||||||
# Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in
|
|
||||||
# all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
# THE SOFTWARE.
|
|
||||||
.PHONY: install test
|
|
||||||
|
|
||||||
#VERSION = $(shell git tag -l | tail -1)
|
|
||||||
VERSION = 1.2.0
|
|
||||||
NAME = libite
|
|
||||||
PKG = $(NAME)-$(VERSION)
|
|
||||||
ARCHIVE = $(PKG).tar.xz
|
|
||||||
|
|
||||||
CFLAGS += -fPIC
|
|
||||||
CPPFLAGS += -W -Wall
|
|
||||||
LDFLAGS = $(EXTRA_LDFLAGS)
|
|
||||||
|
|
||||||
DISTFILES = README LICENSE
|
|
||||||
HEADERS = lite.h
|
|
||||||
OBJS := chomp.o copyfile.o dir.o fexist.o fisdir.o fmode.o fsendfile.o
|
|
||||||
OBJS += ifconfig.o lfile.o makepath.o pidfile.o pidfilefn.o progress.o
|
|
||||||
OBJS += rsync.o strlcpy.o strlcat.o strtonum.o tempfile.o tree.o
|
|
||||||
DEPS := $(OBJS:.o=.d)
|
|
||||||
JUNK = *~ *.bak *.map .*.d *.d DEADJOE semantic.cache *.gdb *.elf core core.*
|
|
||||||
|
|
||||||
VER = 2
|
|
||||||
LIBNAME = $(NAME)
|
|
||||||
SOLIB = $(LIBNAME).so.$(VER)
|
|
||||||
SYMLIB = $(LIBNAME).so
|
|
||||||
STATICLIB = $(LIBNAME).a
|
|
||||||
TARGET = $(STATICLIB) $(SOLIB)
|
|
||||||
|
|
||||||
# This magic trick looks like a comment, but works on BSD PMake
|
|
||||||
#include <rules.mk>
|
|
||||||
include rules.mk
|
|
||||||
|
|
||||||
test: clean
|
|
||||||
$(MAKE) -f chomp.mk
|
|
||||||
$(MAKE) -f copyfile.mk
|
|
||||||
$(MAKE) -f dir.mk
|
|
||||||
$(MAKE) -f fexist.mk
|
|
||||||
$(MAKE) -f fisdir.mk
|
|
||||||
$(MAKE) -f fsendfile.mk
|
|
||||||
$(MAKE) -f lfile.mk
|
|
||||||
$(MAKE) -f makepath.mk
|
|
||||||
$(MAKE) -f pidfilefn.mk
|
|
||||||
$(MAKE) -f progress.mk
|
|
||||||
$(MAKE) -f rsync.mk
|
|
||||||
$(MAKE) -f tempfile.mk
|
|
||||||
$(MAKE) -f tree.mk
|
|
||||||
|
|
||||||
$(OBJS): Makefile
|
|
||||||
|
|
||||||
$(SOLIB): Makefile $(OBJS)
|
|
||||||
$(PRINT) " LINK %s\n" $@
|
|
||||||
$(Q)$(CC) $(LDFLAGS) -shared -Wl,-soname,$@ -o $@ $(OBJS)
|
|
||||||
|
|
||||||
$(STATICLIB): Makefile $(OBJS)
|
|
||||||
$(Q)$(PRINT) " ARCHIVE $@\n"
|
|
||||||
$(Q)$(AR) $(ARFLAGS) $@ $(OBJS) 2>/dev/null
|
|
||||||
|
|
||||||
install-exec: all
|
|
||||||
$(PRINT) " INSTALL $(DESTDIR)$(libdir)/$(SOLIB)\n"
|
|
||||||
$(Q)install -d $(DESTDIR)$(libdir)
|
|
||||||
$(Q)$(STRIPINST) $(SOLIB) $(DESTDIR)$(libdir)/$(SOLIB)
|
|
||||||
$(Q)ln -sf $(SOLIB) $(DESTDIR)$(libdir)/$(SYMLIB)
|
|
||||||
|
|
||||||
install-dev:
|
|
||||||
$(Q)install -d $(DESTDIR)$(incdir)/$(LIBNAME)
|
|
||||||
$(Q)for file in $(HEADERS); do \
|
|
||||||
printf " INSTALL $(DESTDIR)$(incdir)/$(LIBNAME)/$$file\n"; \
|
|
||||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(incdir)/$(LIBNAME)/$$file; \
|
|
||||||
done
|
|
||||||
$(PRINT) " INSTALL $(DESTDIR)$(libdir)/$(STATICLIB)\n"
|
|
||||||
$(Q)install -d $(DESTDIR)$(libdir)
|
|
||||||
$(Q)$(INSTALL) $(STATICLIB) $(DESTDIR)$(libdir)/$(STATICLIB)
|
|
||||||
$(Q)install -d $(DESTDIR)$(datadir)
|
|
||||||
$(Q)for file in $(DISTFILES); do \
|
|
||||||
printf " INSTALL $(DESTDIR)$(datadir)/$$file\n"; \
|
|
||||||
install -m 0644 $$file $(DESTDIR)$(datadir)/$$file; \
|
|
||||||
done
|
|
||||||
|
|
||||||
install: install-exec install-dev
|
|
||||||
|
|
||||||
uninstall:
|
|
||||||
-$(Q)$(RM) -r $(DESTDIR)$(datadir)
|
|
||||||
-$(Q)$(RM) -r $(DESTDIR)$(incdir)/$(LIBNAME)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(SOLIB)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(SYMLIB)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(STATICLIB)
|
|
||||||
|
|
||||||
strip: $(TARGET)
|
|
||||||
$(Q)$(PRINT) " STRIP %s\n" $(TARGET)
|
|
||||||
$(Q)$(STRIP) --strip-unneeded $(TARGET)
|
|
||||||
$(Q)size $(TARGET)
|
|
||||||
|
|
||||||
# Runs Clang scan-build on the whole tree
|
|
||||||
check: clean
|
|
||||||
$(Q)scan-build $(MAKE) all
|
|
||||||
|
|
||||||
# When called from GNU configure and build system
|
|
||||||
distdir:
|
|
||||||
@git archive --format=tar HEAD | (cd $(distdir); tar xf -)
|
|
||||||
|
|
||||||
dist:
|
|
||||||
$(Q)echo "Building .xz tarball of $(PKG) in parent dir..."
|
|
||||||
git archive --format=tar --prefix=$(PKG)/ v$(VERSION) | xz >../$(ARCHIVE)
|
|
||||||
$(Q)(cd ..; md5sum $(ARCHIVE) | tee $(ARCHIVE).md5)
|
|
||||||
|
|
||||||
# Include automatically generated rules
|
|
||||||
-include $(DEPS)
|
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
ACLOCAL_AMFLAGS = -I m4
|
||||||
|
|
||||||
|
lib_LTLIBRARIES = libite.la
|
||||||
|
pkgincludedir = $(includedir)/lite
|
||||||
|
pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa.h strnlen.h
|
||||||
|
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \
|
||||||
|
fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \
|
||||||
|
progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \
|
||||||
|
tempfile.c tree.c
|
||||||
|
|
||||||
|
libite_la_CPPFLAGS = -D_GNU_SOURCE
|
||||||
|
libite_la_CFLAGS = -W -Wall -Wextra
|
||||||
|
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 2:3:0
|
||||||
|
|
||||||
|
pkgconfigdir = $(libdir)/pkgconfig
|
||||||
|
pkgconfig_DATA = libite.pc
|
||||||
|
doc_DATA = README.md LICENSE
|
||||||
|
EXTRA_DIST = README.md LICENSE ChangeLog.md
|
||||||
|
|
||||||
|
## Generate MD5 checksum file
|
||||||
|
MD5 = md5sum
|
||||||
|
md5-dist:
|
||||||
|
@for file in $(DIST_ARCHIVES); do \
|
||||||
|
$(MD5) $$file > $$file.md5; \
|
||||||
|
done
|
||||||
|
|
||||||
|
## Check if tagged in git
|
||||||
|
release-hook:
|
||||||
|
if [ ! `git tag | grep $(PACKAGE_VERSION)` ]; then \
|
||||||
|
echo; \
|
||||||
|
printf "\e[1m\e[41mCannot find release tag $(PACKAGE_VERSION)\e[0m\n"; \
|
||||||
|
printf "\e[1m\e[5mDo release anyway?\e[0m "; read yorn; \
|
||||||
|
if [ "$$yorn" != "y" -a "$$yorn" != "Y" ]; then \
|
||||||
|
printf "OK, aborting release.\n"; \
|
||||||
|
exit 1; \
|
||||||
|
fi; \
|
||||||
|
echo; \
|
||||||
|
else \
|
||||||
|
echo; \
|
||||||
|
printf "\e[1m\e[42mFound GIT release tag $(PACKAGE_VERSION)\e[0m\n"; \
|
||||||
|
printf "\e[1m\e[44m>>Remember to push tags!\e[0m\n"; \
|
||||||
|
echo; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
## Target to run when building a release
|
||||||
|
release: dist release-hook md5-dist
|
||||||
|
@for file in $(DIST_ARCHIVES); do \
|
||||||
|
printf "$$file \tDistribution tarball\n"; \
|
||||||
|
printf "$$file.md5\t"; cat $$file.md5 | cut -f1 -d' '; \
|
||||||
|
mv $$file* ../; \
|
||||||
|
done
|
||||||
@@ -2,16 +2,32 @@
|
|||||||
===========================
|
===========================
|
||||||
[![Travis Status][]][Travis]
|
[![Travis Status][]][Travis]
|
||||||
|
|
||||||
|
|
||||||
|
Table of Contents
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
* [Introduction](#introduction)
|
||||||
|
* [Using -lite](#using--lite)
|
||||||
|
* [Helper Macros](#helper-macros)
|
||||||
|
* [Generic Functions](#generic-functions)
|
||||||
|
* [OpenBSD Functions](#openbsd-functions)
|
||||||
|
* [Build & Install](#origin--references)
|
||||||
|
* [TODO](#todo)
|
||||||
|
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
------------
|
||||||
|
|
||||||
Libite is a lightweight library of *frog DNA*. It can be used to fill
|
Libite is a lightweight library of *frog DNA*. It can be used to fill
|
||||||
the gaps in any dinosaur project. It holds useful functions and macros
|
the gaps in any dinosaur project. It holds useful functions and macros
|
||||||
developed by both [Finit][1] and the [OpenBSD][2] project. Most notably
|
developed by both [Finit][1] and the [OpenBSD][2] project. Most notably
|
||||||
the string functions: [strlcpy(3)][3], [strlcat(3)][3] and the highly
|
the string functions: [strlcpy(3)][3], [strlcat(3)][3] and the highly
|
||||||
useful *BSD [sys/queue.h][4] API.
|
useful *BSD [sys/queue.h][4] and [sys/tree.h][7] API's.
|
||||||
|
|
||||||
Libite aims to fill in the gaps missing in GLIBC/EGLIBC. (It does not
|
Libite is the frog DNA missing in GNU libc. However, -lite does not aim
|
||||||
aimo to become another [GLIB][5] though.) One such gap in GLIBC is the
|
to become another [GLIB][5]! One noticeable gap in GLIBC is the missing
|
||||||
missing `_SAFE` macros in `sys/queue.h` — highly recommended when
|
`_SAFE` macros in the BSD `sys/queue.h` API — highly recommended
|
||||||
traversing lists to delete/free nodes.
|
when traversing lists to delete/free nodes.
|
||||||
|
|
||||||
The code is open sourced under a mix of the [MIT/X11 license][MIT], the
|
The code is open sourced under a mix of the [MIT/X11 license][MIT], the
|
||||||
[ISC license][ISC] used by OpenBSD, and [BSD licenses][BSD], all of them
|
[ISC license][ISC] used by OpenBSD, and [BSD licenses][BSD], all of them
|
||||||
@@ -22,6 +38,38 @@ For an introduction to why Libite happened, and how you can use it, see
|
|||||||
[this blog post][6].
|
[this blog post][6].
|
||||||
|
|
||||||
|
|
||||||
|
Using -lite
|
||||||
|
-----------
|
||||||
|
|
||||||
|
Libite is by default installed as a library and a set of include files.
|
||||||
|
To prevent clashing with include files of the same name `-lite` employs
|
||||||
|
an include file namespace `lite/`, which is strongly recommended to use
|
||||||
|
in your applications:
|
||||||
|
|
||||||
|
#include <lite/lite.h>
|
||||||
|
#include <lite/conio.h>
|
||||||
|
#include <lite/queue.h>
|
||||||
|
#include <lite/tree.h>
|
||||||
|
|
||||||
|
The output from the `pkg-config` tool holds no surprises in this regard:
|
||||||
|
|
||||||
|
$ pkg-config --libs --static --cflags libite
|
||||||
|
-I/usr/local/include -L/usr/local/lib -lite
|
||||||
|
|
||||||
|
The prefix path `/usr/local/` shown here is only the default. Use the
|
||||||
|
`configure` script to select a different prefix when installing libite.
|
||||||
|
|
||||||
|
For GNU autotools based projects, use the following in `configure.ac`:
|
||||||
|
|
||||||
|
# Check for required libraries
|
||||||
|
PKG_CHECK_MODULES([lite], [libite >= 1.5.0])
|
||||||
|
|
||||||
|
and in your `Makefile.am`:
|
||||||
|
|
||||||
|
proggy_CFLAGS = $(lite_CFLAGS)
|
||||||
|
proggy_LDADD = $(lite_LIBS)
|
||||||
|
|
||||||
|
|
||||||
Helper Macros
|
Helper Macros
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
@@ -40,19 +88,23 @@ Helper Macros
|
|||||||
|
|
||||||
- `erase(path)`
|
- `erase(path)`
|
||||||
|
|
||||||
Erase file/directory with `remove()`. Errors on stderr
|
Erase file/directory with `remove()`. Errors on `stderr`
|
||||||
|
|
||||||
- `makedir(path)`
|
- `makedir(path)`
|
||||||
|
|
||||||
Create directory, like `mkdir()`. Errors on stderr
|
Create directory, like `mkdir()`. Errors on `stderr`
|
||||||
|
|
||||||
- `makefifo(path)`
|
- `makefifo(path)`
|
||||||
|
|
||||||
Create a FIFO, like `mkfifo()`. Errors on stderr
|
Create a FIFO, like `mkfifo()`. Errors on `stderr`
|
||||||
|
|
||||||
|
- `min(a,b)`/`max(a,b)`
|
||||||
|
|
||||||
|
These macros take care to avoid double evaluation.
|
||||||
|
|
||||||
- `touch(path)`
|
- `touch(path)`
|
||||||
|
|
||||||
Create a file, or update mtime. Errors on stderr
|
Create a file, or update mtime. Errors on `stderr`
|
||||||
|
|
||||||
- `S_ISEXEC(mode_t m)`
|
- `S_ISEXEC(mode_t m)`
|
||||||
|
|
||||||
@@ -68,7 +120,7 @@ Helper Macros
|
|||||||
|
|
||||||
- `ISOTHER(word,bit)`
|
- `ISOTHER(word,bit)`
|
||||||
|
|
||||||
Is any other bits, except bit, in (integer) word set?
|
Are any other bits, except bit, in (integer) word set?
|
||||||
|
|
||||||
- `SETBIT(word,bit)`
|
- `SETBIT(word,bit)`
|
||||||
|
|
||||||
@@ -112,11 +164,11 @@ Generic Functions
|
|||||||
|
|
||||||
- `fexist(file)`
|
- `fexist(file)`
|
||||||
|
|
||||||
Check for the existance of a file, returns True(1) or False(0).
|
Check for the existence of a file, returns True(1) or False(0).
|
||||||
|
|
||||||
- `fisdir(path)`
|
- `fisdir(path)`
|
||||||
|
|
||||||
Check for the existance of a directory, returns True(1) or False(0).
|
Check for the existence of a directory, returns True(1) or False(0).
|
||||||
|
|
||||||
- `fmode(file)`
|
- `fmode(file)`
|
||||||
|
|
||||||
@@ -131,7 +183,7 @@ Generic Functions
|
|||||||
- `ifconfig(ifname, addr, mask, up)`
|
- `ifconfig(ifname, addr, mask, up)`
|
||||||
|
|
||||||
Basic ifconfig like operations on an interface. Only supports IPv4
|
Basic ifconfig like operations on an interface. Only supports IPv4
|
||||||
adresses. Note that mask is not CIDR notation.
|
addresses. Note that mask is not CIDR notation.
|
||||||
|
|
||||||
- `lfopen(file, sep)`, `lfclose(lf)`
|
- `lfopen(file, sep)`, `lfclose(lf)`
|
||||||
|
|
||||||
@@ -140,7 +192,7 @@ Generic Functions
|
|||||||
|
|
||||||
- `lftok(lf)`
|
- `lftok(lf)`
|
||||||
|
|
||||||
Read tokens, delimeted by `sep`, from file opened with `lfopen()`.
|
Read tokens, delimited by `sep`, from file opened with `lfopen()`.
|
||||||
|
|
||||||
- `lfgetkey(lf, key)`
|
- `lfgetkey(lf, key)`
|
||||||
|
|
||||||
@@ -172,20 +224,26 @@ Generic Functions
|
|||||||
|
|
||||||
- `pidfile(basename)`
|
- `pidfile(basename)`
|
||||||
|
|
||||||
Write a daemon pid file. Creates a pidfile in `_PATH_VARRUN` using
|
Create a daemon PID file in `_PATH_VARRUN` using either `basename` or,
|
||||||
either `basename` or, if `basename` is `NULL`, `__progname`. The
|
if `basename` is `NULL`, `__progname`. The resulting file name is
|
||||||
file name has the form `/var/run/basename.pid`.
|
available to the user as a read-only pointer:
|
||||||
|
|
||||||
Use this function to create a pid file for your daemon when it is
|
extern char *__pidfile_name;
|
||||||
|
|
||||||
|
Use this function to create a PID file for your daemon when it is
|
||||||
ready to receive signals. A client application may poll for the
|
ready to receive signals. A client application may poll for the
|
||||||
existence of this file, so make sure to have your signal handlers
|
existence of this file, so make sure to have your signal handlers
|
||||||
properly setup before calling this function.
|
properly setup before calling this function.
|
||||||
|
|
||||||
The pidfile is removed when the program exits, using an `atexit()`
|
The PID file is removed when the program exits, using an `atexit()`
|
||||||
handler. However, depending on how the program terminates the file
|
handler. However, depending on how the program terminates the file
|
||||||
may still exist even though the program is no longer running. This
|
may still exist even though the program is no longer running. This
|
||||||
is only a problem for clients.
|
is only a problem for clients.
|
||||||
|
|
||||||
|
Calling this function multiple times updates the mtime of the file.
|
||||||
|
Only one `atexit()` handler is created, regardless of the amount of
|
||||||
|
times the function is called.
|
||||||
|
|
||||||
See below for link to OpenBSD man page.
|
See below for link to OpenBSD man page.
|
||||||
|
|
||||||
- `pidfile_read(pidfile)`
|
- `pidfile_read(pidfile)`
|
||||||
@@ -224,15 +282,48 @@ Generic Functions
|
|||||||
set. The `path` argument should be a directory.
|
set. The `path` argument should be a directory.
|
||||||
|
|
||||||
|
|
||||||
OpenBSD Functions
|
GNU Functions
|
||||||
-----------------
|
-------------
|
||||||
|
|
||||||
The following are the popular OpenBSD string functions.
|
The following are useful GNU functions, that do not exist on *BSD, and
|
||||||
|
some other platforms.
|
||||||
|
|
||||||
|
- `strdupa(str)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strdupa.3.html
|
||||||
|
|
||||||
|
- `strndupa(str)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strndupa.3.html
|
||||||
|
|
||||||
|
- `strnlen(str, lim)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strnlen.3.html
|
||||||
|
|
||||||
|
|
||||||
|
OpenBSD/NetBSD/FreeBSD Functions
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
The following are popular functions and highly useful macros from the
|
||||||
|
*BSD world of UNIX that are sadly missing on Linux.
|
||||||
|
|
||||||
|
- `fparseln(fp, *len, *lineno, delim, flags)`
|
||||||
|
|
||||||
|
http://man.openbsd.org/OpenBSD-current/man3/fparseln.3
|
||||||
|
|
||||||
- `pidfile(basename)`
|
- `pidfile(basename)`
|
||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/pidfile.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/pidfile.3
|
||||||
|
|
||||||
|
**Note:** this version of `pidfile()` has been extended to handle it
|
||||||
|
being called multiple times, and also to export the path to the PID
|
||||||
|
file `__pidfile_name`, similar to `__progname`. See previous section
|
||||||
|
for details.
|
||||||
|
|
||||||
|
- `reallocarray(ptr, nmemb, size)`
|
||||||
|
|
||||||
|
http://man.openbsd.org/OpenBSD-current/man3/reallocarray.3
|
||||||
|
|
||||||
- `strlcpy(dst, src, len)`
|
- `strlcpy(dst, src, len)`
|
||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3
|
||||||
@@ -249,6 +340,29 @@ The following are the popular OpenBSD string functions.
|
|||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
||||||
|
|
||||||
|
- `sys/tree.h` API
|
||||||
|
|
||||||
|
Niels Provos' famous splay and red-black tree implementation.
|
||||||
|
|
||||||
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/SPLAY_FOREACH.3
|
||||||
|
|
||||||
|
|
||||||
|
Build & Install
|
||||||
|
---------------
|
||||||
|
|
||||||
|
This library was initially built for and developed on GNU/Linux systems
|
||||||
|
as a light weight utility library, these days NetBSD should also work.
|
||||||
|
Libite (LITE) employs the de facto standard GNU configure and build
|
||||||
|
system:
|
||||||
|
|
||||||
|
./configure
|
||||||
|
make -j5
|
||||||
|
make install-strip
|
||||||
|
|
||||||
|
**Note:** When checking code from GitHub, use <kbd>./autogen.sh</kbd> to
|
||||||
|
generate a `configure` script. It is a generated file and otherwise only
|
||||||
|
included in released tarballs.
|
||||||
|
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
----
|
----
|
||||||
@@ -262,6 +376,7 @@ TODO
|
|||||||
[4]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
[4]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
||||||
[5]: https://developer.gnome.org/glib/
|
[5]: https://developer.gnome.org/glib/
|
||||||
[6]: http://troglobit.com/blog/2015/07/02/howto-using-lite-with-a-git-based-application/
|
[6]: http://troglobit.com/blog/2015/07/02/howto-using-lite-with-a-git-based-application/
|
||||||
|
[7]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/SPLAY_FOREACH.3
|
||||||
[MIT]: https://en.wikipedia.org/wiki/MIT_License
|
[MIT]: https://en.wikipedia.org/wiki/MIT_License
|
||||||
[ISC]: https://en.wikipedia.org/wiki/ISC_license
|
[ISC]: https://en.wikipedia.org/wiki/ISC_license
|
||||||
[BSD]: https://en.wikipedia.org/wiki/BSD_licenses
|
[BSD]: https://en.wikipedia.org/wiki/BSD_licenses
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
TODO ... in no particular order
|
||||||
|
===============================
|
||||||
|
|
||||||
|
* Investigate adding FreeBSD pidfile_open() et al
|
||||||
|
* Investigate FreeBSD libutil library, could be useful for porting between unices
|
||||||
|
|
||||||
|
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
autoreconf -W portability -visfm
|
||||||
@@ -57,7 +57,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f chomp.mk"
|
* compile-command: "make V=1 -f chomp.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
AC_INIT(libite, 1.7.0, https://github.com/troglobit/libite/issues)
|
||||||
|
AM_INIT_AUTOMAKE([1.11 foreign no-dist-gzip dist-xz])
|
||||||
|
AM_SILENT_RULES([yes])
|
||||||
|
|
||||||
|
AC_CONFIG_SRCDIR(lite.h)
|
||||||
|
AM_CONFIG_HEADER(config.h)
|
||||||
|
AC_CONFIG_FILES([Makefile libite.pc])
|
||||||
|
AC_CONFIG_MACRO_DIR([m4])
|
||||||
|
|
||||||
|
AC_PROG_CC
|
||||||
|
AM_PROG_AR
|
||||||
|
LT_INIT
|
||||||
|
|
||||||
|
AC_OUTPUT
|
||||||
@@ -75,7 +75,6 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
+9
-14
@@ -237,27 +237,23 @@ int movefile(char *src, char *dst)
|
|||||||
*/
|
*/
|
||||||
int fcopyfile(FILE *src, FILE *dst)
|
int fcopyfile(FILE *src, FILE *dst)
|
||||||
{
|
{
|
||||||
int ret;
|
char *buf;
|
||||||
char *buffer;
|
|
||||||
|
|
||||||
if (!src || !dst) {
|
if (!src || !dst) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = malloc(BUFSIZ);
|
buf = malloc(BUFSIZ);
|
||||||
if (!buffer)
|
if (!buf)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ret = do_copy(fileno(src), fileno(dst), BUFSIZ, buffer, BUFSIZ);
|
while (fgets(buf, BUFSIZ, src))
|
||||||
if (ret > 0)
|
fputs(buf, dst);
|
||||||
ret = 0;
|
|
||||||
else if (errno)
|
free(buf);
|
||||||
ret = -1;
|
|
||||||
|
|
||||||
free(buffer);
|
return 0;
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITTEST
|
#ifdef UNITTEST
|
||||||
@@ -267,7 +263,7 @@ int main(void)
|
|||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char *files[] = {
|
char *files[] = {
|
||||||
"/etc/passwd", "/tmp/tok", "/tmp/tok",
|
"/etc/passwd", "/tmp/mypwd", "/tmp/mypwd",
|
||||||
"/etc/passwd", "/tmp/", "/tmp/passwd",
|
"/etc/passwd", "/tmp/", "/tmp/passwd",
|
||||||
"/etc/passwd", "/tmp", "/tmp/passwd",
|
"/etc/passwd", "/tmp", "/tmp/passwd",
|
||||||
NULL
|
NULL
|
||||||
@@ -319,7 +315,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f copyfile.mk"
|
* compile-command: "make V=1 -f copyfile.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -222,7 +222,6 @@ int main(int argc, char *argv[])
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f dir.mk"
|
* compile-command: "make V=1 -f dir.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -74,7 +74,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f fexist.mk"
|
* compile-command: "make V=1 -f fexist.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
* Returns:
|
* Returns:
|
||||||
* %TRUE(1) if the file exists and is a directory, otherwise %FALSE(0).
|
* %TRUE(1) if the file exists and is a directory, otherwise %FALSE(0).
|
||||||
*/
|
*/
|
||||||
int fisdir(char *file)
|
int fisdir(const char *file)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
||||||
@@ -73,7 +73,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f fisdir.mk"
|
* compile-command: "make V=1 -f fisdir.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ mode_t fmode(char *file)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
+202
@@ -0,0 +1,202 @@
|
|||||||
|
/* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1997 Christos Zoulas. 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
|
__RCSID("$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $");
|
||||||
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
#define _DIAGASSERT(t)
|
||||||
|
|
||||||
|
static int isescaped(const char *, const char *, int);
|
||||||
|
|
||||||
|
/* isescaped():
|
||||||
|
* Return true if the character in *p that belongs to a string
|
||||||
|
* that starts in *sp, is escaped by the escape character esc.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
isescaped(const char *sp, const char *p, int esc)
|
||||||
|
{
|
||||||
|
const char *cp;
|
||||||
|
size_t ne;
|
||||||
|
|
||||||
|
_DIAGASSERT(sp != NULL);
|
||||||
|
_DIAGASSERT(p != NULL);
|
||||||
|
|
||||||
|
/* No escape character */
|
||||||
|
if (esc == '\0')
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Count the number of escape characters that precede ours */
|
||||||
|
for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* Return true if odd number of escape characters */
|
||||||
|
return (ne & 1) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* fparseln():
|
||||||
|
* Read a line from a file parsing continuations ending in \
|
||||||
|
* and eliminating trailing newlines, or comments starting with
|
||||||
|
* the comment char.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
|
||||||
|
{
|
||||||
|
static const char dstr[3] = { '\\', '\\', '#' };
|
||||||
|
|
||||||
|
ssize_t s;
|
||||||
|
size_t len, ptrlen;
|
||||||
|
char *buf;
|
||||||
|
char *ptr, *cp;
|
||||||
|
int cnt;
|
||||||
|
char esc, con, nl, com;
|
||||||
|
|
||||||
|
_DIAGASSERT(fp != NULL);
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
buf = NULL;
|
||||||
|
ptrlen = 0;
|
||||||
|
ptr = NULL;
|
||||||
|
cnt = 1;
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
str = dstr;
|
||||||
|
|
||||||
|
esc = str[0];
|
||||||
|
con = str[1];
|
||||||
|
com = str[2];
|
||||||
|
/*
|
||||||
|
* XXX: it would be cool to be able to specify the newline character,
|
||||||
|
* getdelim(3) does let us, but supporting it would diverge from BSDs.
|
||||||
|
*/
|
||||||
|
nl = '\n';
|
||||||
|
|
||||||
|
flockfile(fp);
|
||||||
|
|
||||||
|
while (cnt) {
|
||||||
|
cnt = 0;
|
||||||
|
|
||||||
|
if (lineno)
|
||||||
|
(*lineno)++;
|
||||||
|
|
||||||
|
s = getline(&ptr, &ptrlen, fp);
|
||||||
|
if (s < 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (s && com) { /* Check and eliminate comments */
|
||||||
|
for (cp = ptr; cp < ptr + s; cp++)
|
||||||
|
if (*cp == com && !isescaped(ptr, cp, esc)) {
|
||||||
|
s = cp - ptr;
|
||||||
|
cnt = s == 0 && buf == NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s && nl) { /* Check and eliminate newlines */
|
||||||
|
cp = &ptr[s - 1];
|
||||||
|
|
||||||
|
if (*cp == nl)
|
||||||
|
s--; /* forget newline */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s && con) { /* Check and eliminate continuations */
|
||||||
|
cp = &ptr[s - 1];
|
||||||
|
|
||||||
|
if (*cp == con && !isescaped(ptr, cp, esc)) {
|
||||||
|
s--; /* forget continuation char */
|
||||||
|
cnt = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s == 0) {
|
||||||
|
/*
|
||||||
|
* nothing to add, skip realloc except in case
|
||||||
|
* we need a minimal buf to return an empty line
|
||||||
|
*/
|
||||||
|
if (cnt || buf != NULL)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((cp = realloc(buf, len + s + 1)) == NULL) {
|
||||||
|
funlockfile(fp);
|
||||||
|
free(buf);
|
||||||
|
free(ptr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
buf = cp;
|
||||||
|
|
||||||
|
(void) memcpy(buf + len, ptr, s);
|
||||||
|
len += s;
|
||||||
|
buf[len] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
funlockfile(fp);
|
||||||
|
free(ptr);
|
||||||
|
|
||||||
|
if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
|
||||||
|
strchr(buf, esc) != NULL) {
|
||||||
|
ptr = cp = buf;
|
||||||
|
while (cp[0] != '\0') {
|
||||||
|
int skipesc;
|
||||||
|
|
||||||
|
while (cp[0] != '\0' && cp[0] != esc)
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
if (cp[0] == '\0' || cp[1] == '\0')
|
||||||
|
break;
|
||||||
|
|
||||||
|
skipesc = 0;
|
||||||
|
if (cp[1] == com)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCCOMM);
|
||||||
|
if (cp[1] == con)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCCONT);
|
||||||
|
if (cp[1] == esc)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCESC);
|
||||||
|
if (cp[1] != com && cp[1] != con && cp[1] != esc)
|
||||||
|
skipesc = (flags & FPARSELN_UNESCREST);
|
||||||
|
|
||||||
|
if (skipesc)
|
||||||
|
cp++;
|
||||||
|
else
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
}
|
||||||
|
*ptr = '\0';
|
||||||
|
len = strlen(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
*size = len;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
@@ -112,7 +112,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f fsendfile.mk"
|
* compile-command: "make V=1 -f fsendfile.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -85,7 +85,6 @@ int ifconfig(char *ifname, char *addr, char *mask, int up)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -228,7 +228,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f lfile.mk"
|
* compile-command: "make V=1 -f lfile.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
prefix=@prefix@
|
||||||
|
exec_prefix=@exec_prefix@
|
||||||
|
libdir=@libdir@
|
||||||
|
includedir=@includedir@
|
||||||
|
|
||||||
|
Name: @PACKAGE@
|
||||||
|
Description: Frog DNA, basically
|
||||||
|
Version: @VERSION@
|
||||||
|
Requires:
|
||||||
|
Libs: -L${libdir} -lite
|
||||||
|
Cflags: -I${includedir}
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/* Collection of utility funcs and C library extensions for finit & its plugins
|
/* Collection of frog DNA
|
||||||
*
|
*
|
||||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
* Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2008-2016 Joachim Nilsson <troglobit@gmail.com>
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -22,24 +22,34 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef FINIT_LITE_H_
|
#ifndef LITE_H_
|
||||||
#define FINIT_LITE_H_
|
#define LITE_H_
|
||||||
|
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "strlite.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* fparseln() specific operation flags.
|
||||||
|
*/
|
||||||
|
#define FPARSELN_UNESCESC 0x01
|
||||||
|
#define FPARSELN_UNESCCONT 0x02
|
||||||
|
#define FPARSELN_UNESCCOMM 0x04
|
||||||
|
#define FPARSELN_UNESCREST 0x08
|
||||||
|
#define FPARSELN_UNESCALL 0x0f
|
||||||
|
|
||||||
typedef struct lfile lfile_t;
|
typedef struct lfile lfile_t;
|
||||||
|
|
||||||
char *chomp (char *str);
|
char *chomp (char *str);
|
||||||
|
char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
|
||||||
|
|
||||||
int fexist (char *file);
|
int fexist (char *file);
|
||||||
int fisdir (char *file);
|
int fisdir (const char *file);
|
||||||
mode_t fmode (char *file);
|
mode_t fmode (char *file);
|
||||||
|
|
||||||
FILE *tempfile (void);
|
FILE *tempfile (void);
|
||||||
@@ -68,15 +78,7 @@ int pidfile_signal(const char *pidfile, int signal);
|
|||||||
pid_t pidfile_read (const char *pidfile);
|
pid_t pidfile_read (const char *pidfile);
|
||||||
pid_t pidfile_poll (const char *pidfile);
|
pid_t pidfile_poll (const char *pidfile);
|
||||||
|
|
||||||
#ifndef strlcpy
|
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||||
size_t strlcpy (char *dst, const char *src, size_t siz);
|
|
||||||
#endif
|
|
||||||
#ifndef strlcat
|
|
||||||
size_t strlcat (char *dst, const char *src, size_t siz);
|
|
||||||
#endif
|
|
||||||
#ifndef strtonum
|
|
||||||
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int tree(char *path, int show_perms);
|
int tree(char *path, int show_perms);
|
||||||
|
|
||||||
@@ -130,55 +132,16 @@ int tree(char *path, int show_perms);
|
|||||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Does directory end with a slash? */
|
||||||
static inline int fisslashdir(char *dir)
|
static inline int fisslashdir(char *dir)
|
||||||
{
|
{
|
||||||
if (!dir) return 0;
|
if (!dir)
|
||||||
if (strlen (dir) > 0) return dir[strlen (dir) - 1] == '/';
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Convert string to natural number (0-2147483647), returns -1 on error. */
|
if (strlen(dir) > 0)
|
||||||
static inline int atonum(const char *str)
|
return dir[strlen(dir) - 1] == '/';
|
||||||
{
|
|
||||||
int val = -1;
|
|
||||||
const char *errstr;
|
|
||||||
|
|
||||||
if (str) {
|
return 0;
|
||||||
val = strtonum(str, 0, INT32_MAX, &errstr);
|
|
||||||
if (errstr)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Validate string, non NULL and not zero length */
|
|
||||||
static inline int string_valid(const char *s)
|
|
||||||
{
|
|
||||||
return s && strlen(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Relaxed comparison, e.g., sys_string_match("small", "smaller") => TRUE */
|
|
||||||
static inline int string_match(const char *a, const char *b)
|
|
||||||
{
|
|
||||||
size_t min = MIN(strlen(a), strlen(b));
|
|
||||||
|
|
||||||
return !strncasecmp(a, b, min);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Strict comparison, e.g., sys_string_match("small", "smaller") => FALSE */
|
|
||||||
static inline int string_compare (const char *a, const char *b)
|
|
||||||
{
|
|
||||||
return strlen(a) == strlen(b) && !strcmp(a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Strict comparison, like sys_string_compare(), but case insensitive,
|
|
||||||
* e.g., sys_string_match("small", "SmAlL") => TRUE
|
|
||||||
*/
|
|
||||||
static inline int string_case_compare (const char *a, const char *b)
|
|
||||||
{
|
|
||||||
return strlen (a) == strlen (b) && !strcasecmp (a, b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compat */
|
/* Compat */
|
||||||
@@ -186,11 +149,10 @@ static inline int string_case_compare (const char *a, const char *b)
|
|||||||
#define pidfile_read_pid(file) pifile_read(file)
|
#define pidfile_read_pid(file) pifile_read(file)
|
||||||
#define signal_pidfile(file,signo) pidfile_signal(file,signo)
|
#define signal_pidfile(file,signo) pidfile_signal(file,signo)
|
||||||
|
|
||||||
#endif /* FINIT_LITE_H_ */
|
#endif /* LITE_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
*.m4
|
||||||
+4
-2
@@ -25,12 +25,14 @@
|
|||||||
|
|
||||||
int mkpath(char *dir, mode_t mode)
|
int mkpath(char *dir, mode_t mode)
|
||||||
{
|
{
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(dir) == 1 && dir[0] == '/')
|
if (!stat(dir, &sb))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
mkpath(dirname(strdupa(dir)), mode);
|
mkpath(dirname(strdupa(dir)), mode);
|
||||||
@@ -96,6 +98,7 @@ int main(void)
|
|||||||
"/tmp/a/",
|
"/tmp/a/",
|
||||||
"/tmp/a/b",
|
"/tmp/a/b",
|
||||||
"/tmp/a/c/",
|
"/tmp/a/c/",
|
||||||
|
"../../sometestdir/arpa/inet",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -117,7 +120,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f makepath.mk"
|
* compile-command: "make V=1 -f makepath.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/* Updated by troglobit for libite/finit/uftpd projects 2016/03/19 */
|
||||||
/* $OpenBSD: pidfile.c,v 1.11 2015/06/03 02:24:36 millert Exp $ */
|
/* $OpenBSD: pidfile.c,v 1.11 2015/06/03 02:24:36 millert Exp $ */
|
||||||
/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
|
/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@
|
|||||||
* POSSIBILITY OF SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/stat.h> /* utimensat() */
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
@@ -37,26 +39,38 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static char *pidfile_path;
|
#ifndef pidfile
|
||||||
|
static char *pidfile_path = NULL;
|
||||||
static pid_t pidfile_pid;
|
static pid_t pidfile_pid;
|
||||||
|
|
||||||
static void pidfile_cleanup(void);
|
static void pidfile_cleanup(void);
|
||||||
|
|
||||||
|
const char *__pidfile_name = NULL;
|
||||||
extern char *__progname;
|
extern char *__progname;
|
||||||
|
|
||||||
int
|
int
|
||||||
pidfile(const char *basename)
|
pidfile(const char *basename)
|
||||||
{
|
{
|
||||||
int save_errno;
|
int save_errno;
|
||||||
|
int atexit_already;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
if (basename == NULL)
|
if (basename == NULL)
|
||||||
basename = __progname;
|
basename = __progname;
|
||||||
|
|
||||||
|
pid = getpid();
|
||||||
|
atexit_already = 0;
|
||||||
|
|
||||||
if (pidfile_path != NULL) {
|
if (pidfile_path != NULL) {
|
||||||
|
if (pid == pidfile_pid) {
|
||||||
|
utimensat(0, pidfile_path, NULL, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
free(pidfile_path);
|
free(pidfile_path);
|
||||||
pidfile_path = NULL;
|
pidfile_path = NULL;
|
||||||
|
__pidfile_name = NULL;
|
||||||
|
atexit_already = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* _PATH_VARRUN includes trailing / */
|
/* _PATH_VARRUN includes trailing / */
|
||||||
@@ -71,7 +85,6 @@ pidfile(const char *basename)
|
|||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pid = getpid();
|
|
||||||
if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
|
if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
|
||||||
save_errno = errno;
|
save_errno = errno;
|
||||||
(void) fclose(f);
|
(void) fclose(f);
|
||||||
@@ -83,6 +96,13 @@ pidfile(const char *basename)
|
|||||||
}
|
}
|
||||||
(void) fclose(f);
|
(void) fclose(f);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* LITE extension, no need to set up another atexit() handler
|
||||||
|
* if user only called us to update the mtime of the PID file
|
||||||
|
*/
|
||||||
|
if (atexit_already)
|
||||||
|
return (0);
|
||||||
|
|
||||||
pidfile_pid = pid;
|
pidfile_pid = pid;
|
||||||
if (atexit(pidfile_cleanup) < 0) {
|
if (atexit(pidfile_cleanup) < 0) {
|
||||||
save_errno = errno;
|
save_errno = errno;
|
||||||
@@ -93,6 +113,7 @@ pidfile(const char *basename)
|
|||||||
errno = save_errno;
|
errno = save_errno;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
__pidfile_name = pidfile_path;
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@@ -104,3 +125,4 @@ pidfile_cleanup(void)
|
|||||||
if (pidfile_path != NULL && pidfile_pid == getpid())
|
if (pidfile_path != NULL && pidfile_pid == getpid())
|
||||||
(void) unlink(pidfile_path);
|
(void) unlink(pidfile_path);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
+99
-20
@@ -21,6 +21,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
extern char *__pidfile_name;
|
||||||
extern char *chomp(char *str);
|
extern char *chomp(char *str);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,52 +122,130 @@ int pidfile_signal(const char *pidfile, int signal)
|
|||||||
/********************************* UNIT TESTS ************************************/
|
/********************************* UNIT TESTS ************************************/
|
||||||
#ifdef UNITTEST
|
#ifdef UNITTEST
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
|
|
||||||
extern char *__progname;
|
extern char *__progname;
|
||||||
static char PIDFILE[42];
|
static char PIDFILE[42];
|
||||||
|
static int verbose = 0;
|
||||||
|
static struct stat before, after;
|
||||||
|
|
||||||
static void sigterm_handler(int signo)
|
static int test(int result, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
printf("Exiting ...\n");
|
char buf[80];
|
||||||
|
size_t len;
|
||||||
|
va_list ap;
|
||||||
|
const char success[] = " \e[1m[ OK ]\e[0m\n";
|
||||||
|
const char failure[] = " \e[7m[FAIL]\e[0m\n";
|
||||||
|
const char dots[] = " .....................................................................";
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
write(STDERR_FILENO, buf, len);
|
||||||
|
write(STDERR_FILENO, dots, 60 - len); /* pad with dots. */
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
write(STDERR_FILENO, success, strlen(success));
|
||||||
|
else
|
||||||
|
write(STDERR_FILENO, failure, strlen(failure));
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sigalrm_handler(int signo)
|
static inline int timespec_newer(const struct timespec *a, const struct timespec *b)
|
||||||
|
{
|
||||||
|
if (a->tv_sec != b->tv_sec)
|
||||||
|
return a->tv_sec > b->tv_sec;
|
||||||
|
|
||||||
|
return a->tv_nsec > b->tv_nsec;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *timespec2str(struct timespec *ts, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
size_t ret, pos;
|
||||||
|
struct tm t;
|
||||||
|
|
||||||
|
memset(buf, 0, len);
|
||||||
|
|
||||||
|
tzset();
|
||||||
|
if (localtime_r(&(ts->tv_sec), &t) == NULL)
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
ret = strftime(buf, len, "%F %T", &t);
|
||||||
|
if (ret == 0)
|
||||||
|
return buf;
|
||||||
|
len -= ret - 1;
|
||||||
|
|
||||||
|
pos = strlen(buf);
|
||||||
|
len -= pos;
|
||||||
|
snprintf(&buf[pos], len, ".%09ld", ts->tv_nsec);
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sigterm_handler(int UNUSED(signo))
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
printf("Exiting ...\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sigalrm_handler(int UNUSED(signo))
|
||||||
{
|
{
|
||||||
printf("Testing pidfile() ...\n");
|
|
||||||
pidfile(NULL);
|
pidfile(NULL);
|
||||||
|
|
||||||
if (!fexist(PIDFILE))
|
if (!fexist(PIDFILE))
|
||||||
err(1, "pidfile() failed creating %s", PIDFILE);
|
err(1, "failed creating %s", PIDFILE);
|
||||||
|
stat(PIDFILE, &before);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
static int mtime()
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
char buf[80];
|
||||||
|
|
||||||
|
/* Must sleep a while here otherwise we execute too fast => no mtime change :-( */
|
||||||
|
usleep(10000);
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
printf("Calling pidfile() again to update mtime ...\n");
|
||||||
|
pidfile(NULL);
|
||||||
|
stat(PIDFILE, &after);
|
||||||
|
|
||||||
|
ret = timespec_newer(&after.st_mtim, &before.st_mtim);
|
||||||
|
if (verbose) {
|
||||||
|
printf("Before: %s\n", timespec2str(&before.st_mtim, buf, sizeof(buf)));
|
||||||
|
printf("After : %s\n", timespec2str(&after.st_mtim, buf, sizeof(buf)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return !ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc > 1 && !strcmp(argv[1], "-v"))
|
||||||
|
verbose = 1;
|
||||||
|
|
||||||
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", _PATH_VARRUN, __progname);
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", _PATH_VARRUN, __progname);
|
||||||
|
|
||||||
signal(SIGTERM, sigterm_handler);
|
signal(SIGTERM, sigterm_handler);
|
||||||
signal(SIGALRM, sigalrm_handler);
|
signal(SIGALRM, sigalrm_handler);
|
||||||
alarm(1);
|
alarm(1);
|
||||||
|
|
||||||
printf("Testing pidfile_poll() ...\n");
|
return test(pidfile_poll(PIDFILE) != getpid(), "Testing pidfile_poll()")
|
||||||
if (!pidfile_poll(PIDFILE))
|
|| test(strcmp(__pidfile_name, PIDFILE), "Testing __pidfile_name vs guessed PID filename")
|
||||||
printf("Timed out!\n");
|
|| test(mtime(), "Testing mtime update of followup pidfile() call")
|
||||||
else
|
|| test(pidfile_signal(PIDFILE, SIGTERM), "Testing signalling ourselves")
|
||||||
printf("We got signal!\n");
|
;
|
||||||
|
|
||||||
printf("Reading pid file, should be %d ...\n", getpid());
|
return 0;
|
||||||
printf("=> %d\n", pidfile_read(PIDFILE));
|
|
||||||
|
|
||||||
printf("Signalling ourselves ...\n");
|
|
||||||
|
|
||||||
return pidfile_signal(PIDFILE, SIGTERM);
|
|
||||||
}
|
}
|
||||||
#endif /* UTEST_PIDFN */
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f pidfilefn.mk"
|
* compile-command: "make V=1 -f pidfilefn.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
CFLAGS = -W -Wall -Wextra -Werror
|
||||||
OBJS = pidfile.o chomp.o fexist.o
|
OBJS = pidfile.o chomp.o fexist.o
|
||||||
ASROOT = 1
|
ASROOT = 1
|
||||||
TARGET = clean $(OBJS) pidfilefn.test
|
TARGET = clean $(OBJS) pidfilefn.test
|
||||||
|
|||||||
@@ -168,7 +168,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f progress.mk"
|
* compile-command: "make V=1 -f progress.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: queue.h,v 1.38 2013/07/03 15:05:21 fgsch Exp $ */
|
/* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert Exp $ */
|
||||||
/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
|
/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -36,8 +36,8 @@
|
|||||||
#define _SYS_QUEUE_H_
|
#define _SYS_QUEUE_H_
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file defines five types of data structures: singly-linked lists,
|
* This file defines five types of data structures: singly-linked lists,
|
||||||
* lists, simple queues, tail queues, and circular queues.
|
* lists, simple queues, tail queues and XOR simple queues.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* A singly-linked list is headed by a single forward pointer. The elements
|
* A singly-linked list is headed by a single forward pointer. The elements
|
||||||
@@ -57,7 +57,7 @@
|
|||||||
* or after an existing element or at the head of the list. A list
|
* or after an existing element or at the head of the list. A list
|
||||||
* may only be traversed in the forward direction.
|
* may only be traversed in the forward direction.
|
||||||
*
|
*
|
||||||
* A simple queue is headed by a pair of pointers, one the head of the
|
* A simple queue is headed by a pair of pointers, one to the head of the
|
||||||
* list and the other to the tail of the list. The elements are singly
|
* list and the other to the tail of the list. The elements are singly
|
||||||
* linked to save space, so elements can only be removed from the
|
* linked to save space, so elements can only be removed from the
|
||||||
* head of the list. New elements can be added to the list before or after
|
* head of the list. New elements can be added to the list before or after
|
||||||
@@ -71,13 +71,10 @@
|
|||||||
* after an existing element, at the head of the list, or at the end of
|
* after an existing element, at the head of the list, or at the end of
|
||||||
* the list. A tail queue may be traversed in either direction.
|
* the list. A tail queue may be traversed in either direction.
|
||||||
*
|
*
|
||||||
* A circle queue is headed by a pair of pointers, one to the head of the
|
* An XOR simple queue is used in the same way as a regular simple queue.
|
||||||
* list and the other to the tail of the list. The elements are doubly
|
* The difference is that the head structure also includes a "cookie" that
|
||||||
* linked so that an arbitrary element can be removed without a need to
|
* is XOR'd with the queue pointer (first, last or next) to generate the
|
||||||
* traverse the list. New elements can be added to the list before or after
|
* real pointer value.
|
||||||
* an existing element, at the head of the list, or at the end of the list.
|
|
||||||
* A circle queue may be traversed in either direction, but has a more
|
|
||||||
* complex end of list detection.
|
|
||||||
*
|
*
|
||||||
* For details on the use of these macros, see the queue(3) manual page.
|
* For details on the use of these macros, see the queue(3) manual page.
|
||||||
*/
|
*/
|
||||||
@@ -95,15 +92,15 @@
|
|||||||
struct name { \
|
struct name { \
|
||||||
struct type *slh_first; /* first element */ \
|
struct type *slh_first; /* first element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SLIST_HEAD_INITIALIZER(head) \
|
#define SLIST_HEAD_INITIALIZER(head) \
|
||||||
{ NULL }
|
{ NULL }
|
||||||
|
|
||||||
#define SLIST_ENTRY(type) \
|
#define SLIST_ENTRY(type) \
|
||||||
struct { \
|
struct { \
|
||||||
struct type *sle_next; /* next element */ \
|
struct type *sle_next; /* next element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Singly-linked List access methods.
|
* Singly-linked List access methods.
|
||||||
*/
|
*/
|
||||||
@@ -157,8 +154,8 @@ struct { \
|
|||||||
curelm = curelm->field.sle_next; \
|
curelm = curelm->field.sle_next; \
|
||||||
curelm->field.sle_next = \
|
curelm->field.sle_next = \
|
||||||
curelm->field.sle_next->field.sle_next; \
|
curelm->field.sle_next->field.sle_next; \
|
||||||
_Q_INVALIDATE((elm)->field.sle_next); \
|
|
||||||
} \
|
} \
|
||||||
|
_Q_INVALIDATE((elm)->field.sle_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -179,7 +176,7 @@ struct { \
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* List access methods
|
* List access methods.
|
||||||
*/
|
*/
|
||||||
#define LIST_FIRST(head) ((head)->lh_first)
|
#define LIST_FIRST(head) ((head)->lh_first)
|
||||||
#define LIST_END(head) NULL
|
#define LIST_END(head) NULL
|
||||||
@@ -316,6 +313,14 @@ struct { \
|
|||||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_CONCAT(head1, head2) do { \
|
||||||
|
if (!SIMPLEQ_EMPTY((head2))) { \
|
||||||
|
*(head1)->sqh_last = (head2)->sqh_first; \
|
||||||
|
(head1)->sqh_last = (head2)->sqh_last; \
|
||||||
|
SIMPLEQ_INIT((head2)); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* XOR Simple queue definitions.
|
* XOR Simple queue definitions.
|
||||||
*/
|
*/
|
||||||
@@ -395,7 +400,7 @@ struct { \
|
|||||||
XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Tail queue definitions.
|
* Tail queue definitions.
|
||||||
*/
|
*/
|
||||||
@@ -414,8 +419,8 @@ struct { \
|
|||||||
struct type **tqe_prev; /* address of previous next element */ \
|
struct type **tqe_prev; /* address of previous next element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* tail queue access methods
|
* Tail queue access methods.
|
||||||
*/
|
*/
|
||||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||||
#define TAILQ_END(head) NULL
|
#define TAILQ_END(head) NULL
|
||||||
@@ -516,133 +521,13 @@ struct { \
|
|||||||
_Q_INVALIDATE((elm)->field.tqe_next); \
|
_Q_INVALIDATE((elm)->field.tqe_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/*
|
#define TAILQ_CONCAT(head1, head2, field) do { \
|
||||||
* Circular queue definitions.
|
if (!TAILQ_EMPTY(head2)) { \
|
||||||
*/
|
*(head1)->tqh_last = (head2)->tqh_first; \
|
||||||
#define CIRCLEQ_HEAD(name, type) \
|
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
||||||
struct name { \
|
(head1)->tqh_last = (head2)->tqh_last; \
|
||||||
struct type *cqh_first; /* first element */ \
|
TAILQ_INIT((head2)); \
|
||||||
struct type *cqh_last; /* last element */ \
|
} \
|
||||||
}
|
|
||||||
|
|
||||||
#define CIRCLEQ_HEAD_INITIALIZER(head) \
|
|
||||||
{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
|
|
||||||
|
|
||||||
#define CIRCLEQ_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *cqe_next; /* next element */ \
|
|
||||||
struct type *cqe_prev; /* previous element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Circular queue access methods
|
|
||||||
*/
|
|
||||||
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
|
|
||||||
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
|
|
||||||
#define CIRCLEQ_END(head) ((void *)(head))
|
|
||||||
#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
|
|
||||||
#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
|
|
||||||
#define CIRCLEQ_EMPTY(head) \
|
|
||||||
(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH(var, head, field) \
|
|
||||||
for((var) = CIRCLEQ_FIRST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head); \
|
|
||||||
(var) = CIRCLEQ_NEXT(var, field))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = CIRCLEQ_FIRST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head) && \
|
|
||||||
((tvar) = CIRCLEQ_NEXT(var, field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
|
|
||||||
for((var) = CIRCLEQ_LAST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head); \
|
|
||||||
(var) = CIRCLEQ_PREV(var, field))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
|
||||||
for ((var) = CIRCLEQ_LAST(head, headname); \
|
|
||||||
(var) != CIRCLEQ_END(head) && \
|
|
||||||
((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Circular queue functions.
|
|
||||||
*/
|
|
||||||
#define CIRCLEQ_INIT(head) do { \
|
|
||||||
(head)->cqh_first = CIRCLEQ_END(head); \
|
|
||||||
(head)->cqh_last = CIRCLEQ_END(head); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
|
|
||||||
(elm)->field.cqe_prev = (listelm); \
|
|
||||||
if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
else \
|
|
||||||
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
|
|
||||||
(listelm)->field.cqe_next = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (listelm); \
|
|
||||||
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
|
|
||||||
if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
else \
|
|
||||||
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
|
|
||||||
(listelm)->field.cqe_prev = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (head)->cqh_first; \
|
|
||||||
(elm)->field.cqe_prev = CIRCLEQ_END(head); \
|
|
||||||
if ((head)->cqh_last == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
else \
|
|
||||||
(head)->cqh_first->field.cqe_prev = (elm); \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = CIRCLEQ_END(head); \
|
|
||||||
(elm)->field.cqe_prev = (head)->cqh_last; \
|
|
||||||
if ((head)->cqh_first == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
else \
|
|
||||||
(head)->cqh_last->field.cqe_next = (elm); \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_REMOVE(head, elm, field) do { \
|
|
||||||
if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm)->field.cqe_prev; \
|
|
||||||
else \
|
|
||||||
(elm)->field.cqe_next->field.cqe_prev = \
|
|
||||||
(elm)->field.cqe_prev; \
|
|
||||||
if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm)->field.cqe_next; \
|
|
||||||
else \
|
|
||||||
(elm)->field.cqe_prev->field.cqe_next = \
|
|
||||||
(elm)->field.cqe_next; \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_prev); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_next); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
|
|
||||||
if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
|
|
||||||
CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm2); \
|
|
||||||
else \
|
|
||||||
(elm2)->field.cqe_next->field.cqe_prev = (elm2); \
|
|
||||||
if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
|
|
||||||
CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm2); \
|
|
||||||
else \
|
|
||||||
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_prev); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_next); \
|
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#endif /* !_SYS_QUEUE_H_ */
|
#endif /* !_SYS_QUEUE_H_ */
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
|
||||||
|
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
|
||||||
|
*/
|
||||||
|
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
|
||||||
|
|
||||||
|
void *
|
||||||
|
reallocarray(void *optr, size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
|
||||||
|
nmemb > 0 && SIZE_MAX / nmemb < size) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return realloc(optr, size * nmemb);
|
||||||
|
}
|
||||||
@@ -306,7 +306,6 @@ int main(int argc, char *argv[])
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f rsync.mk"
|
* compile-command: "make V=1 -f rsync.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -1,30 +1,12 @@
|
|||||||
.PHONY: all clean distclean
|
CFLAGS += -g -O0
|
||||||
|
|
||||||
# Figure out root of library, unless used as submodule
|
|
||||||
ROOTDIR ?= $(shell pwd)
|
|
||||||
|
|
||||||
CC ?= $(CROSS)gcc
|
|
||||||
AR ?= $(CROSS)ar
|
|
||||||
STRIP ?= $(CROSS)strip
|
|
||||||
INSTALL := install --backup=off
|
|
||||||
STRIPINST := $(INSTALL) -s --strip-program=$(CROSS)strip -m 0755
|
|
||||||
|
|
||||||
CPPFLAGS += -D_GNU_SOURCE
|
CPPFLAGS += -D_GNU_SOURCE
|
||||||
ARFLAGS = crus
|
|
||||||
|
|
||||||
# Default to silent build, use V=1 to get verbose mode.
|
|
||||||
ifeq ($V,1)
|
ifeq ($V,1)
|
||||||
Q =
|
|
||||||
PRINT = @true
|
|
||||||
REDIRECT =
|
|
||||||
MAKEFLAGS =
|
|
||||||
VERBOSE = -v
|
VERBOSE = -v
|
||||||
|
REDIRECT =
|
||||||
else
|
else
|
||||||
Q = @
|
|
||||||
PRINT = @printf
|
|
||||||
REDIRECT = >/dev/null
|
|
||||||
MAKEFLAGS = --no-print-directory --silent
|
|
||||||
VERBOSE =
|
VERBOSE =
|
||||||
|
REDIRECT = >/dev/null
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Some tests may need to be run as root, e.g. pidfile()
|
# Some tests may need to be run as root, e.g. pidfile()
|
||||||
@@ -34,35 +16,15 @@ else
|
|||||||
SUDO =
|
SUDO =
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Default install paths
|
all: $(TARGET)
|
||||||
prefix ?= /usr/local
|
|
||||||
libdir ?= $(prefix)/lib
|
|
||||||
datadir ?= $(prefix)/share/doc/$(LIBNAME)
|
|
||||||
incdir ?= $(prefix)/include
|
|
||||||
|
|
||||||
# Pretty printing and GCC -M for auto dep files
|
|
||||||
%.o: %.c
|
|
||||||
$(PRINT) " CC $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
|
||||||
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -c -MMD -MP -o $@ $<
|
|
||||||
|
|
||||||
%: %.o
|
|
||||||
$(PRINT) " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
|
||||||
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
|
|
||||||
|
|
||||||
.PHONY: %.test
|
|
||||||
%.test: %.c
|
%.test: %.c
|
||||||
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
|
@printf " TEST $@\n"
|
||||||
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
@$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
||||||
$(Q)$(RM) $(OBJS)
|
@$(RM) $(OBJS)
|
||||||
$(Q)$(SUDO) ./$@ $(VERBOSE) $(REDIRECT)
|
@$(SUDO) ./$@ $(VERBOSE) $(REDIRECT)
|
||||||
|
|
||||||
|
|
||||||
# Default build rules for both main and unit test makefiles
|
|
||||||
all:: $(TARGET)
|
|
||||||
|
|
||||||
clean::
|
clean::
|
||||||
-$(Q)$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
-@$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
||||||
|
-@$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest *.test
|
||||||
distclean::
|
|
||||||
-$(Q)$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest *.test
|
|
||||||
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/* ==========================================================================
|
||||||
|
* strdupa.h - Re-implementation of glibc strdupa.
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Copyright (c) 2009 William Ahern
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
* ==========================================================================
|
||||||
|
*/
|
||||||
|
#ifndef GNU_STRING_STRDUPA_H
|
||||||
|
#define GNU_STRING_STRDUPA_H
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRDUPA)
|
||||||
|
#if defined(strdupa)
|
||||||
|
#define HAVE_STRDUPA 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !HAVE_STRDUPA
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
#include <string.h> /* memcpy(3) strlen(3) */
|
||||||
|
|
||||||
|
#define strdupa(src) (__extension__ ({ \
|
||||||
|
size_t len_ = strlen(src); \
|
||||||
|
char *dst_ = __builtin_alloca(len_ + 1); \
|
||||||
|
dst_[len_] = '\0'; \
|
||||||
|
(char *)memcpy(dst_, src, len_); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#else /* If not GCC, e.g. Clang */
|
||||||
|
#error strdupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
#endif /* !HAVE_STRDUPA */
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* GNU_STRING_STRDUPA_H */
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
|
/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -21,46 +21,37 @@
|
|||||||
|
|
||||||
#ifndef strlcat
|
#ifndef strlcat
|
||||||
/*
|
/*
|
||||||
* Appends src to string dst of size siz (unlike strncat, siz is the
|
* Appends src to string dst of size dsize (unlike strncat, dsize is the
|
||||||
* full size of dst, not space left). At most siz-1 characters
|
* full size of dst, not space left). At most dsize-1 characters
|
||||||
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
|
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
|
||||||
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
|
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
|
||||||
* If retval >= siz, truncation occurred.
|
* If retval >= dsize, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcat(char *dst, const char *src, size_t siz)
|
strlcat(char *dst, const char *src, size_t dsize)
|
||||||
{
|
{
|
||||||
char *d = dst;
|
const char *odst = dst;
|
||||||
const char *s = src;
|
const char *osrc = src;
|
||||||
size_t n = siz;
|
size_t n = dsize;
|
||||||
size_t dlen;
|
size_t dlen;
|
||||||
|
|
||||||
/* Find the end of dst and adjust bytes left but don't go past end */
|
/* Find the end of dst and adjust bytes left but don't go past end. */
|
||||||
while (n-- != 0 && *d != '\0')
|
while (n-- != 0 && *dst != '\0')
|
||||||
d++;
|
dst++;
|
||||||
dlen = d - dst;
|
dlen = dst - odst;
|
||||||
n = siz - dlen;
|
n = dsize - dlen;
|
||||||
|
|
||||||
if (n == 0)
|
if (n-- == 0)
|
||||||
return(dlen + strlen(s));
|
return(dlen + strlen(src));
|
||||||
while (*s != '\0') {
|
while (*src != '\0') {
|
||||||
if (n != 1) {
|
if (n != 0) {
|
||||||
*d++ = *s;
|
*dst++ = *src;
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
s++;
|
src++;
|
||||||
}
|
}
|
||||||
*d = '\0';
|
*dst = '\0';
|
||||||
|
|
||||||
return(dlen + (s - src)); /* count does not include NUL */
|
return(dlen + (src - osrc)); /* count does not include NUL */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* c-file-style: "bsd"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
|
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -21,41 +21,32 @@
|
|||||||
|
|
||||||
#ifndef strlcpy
|
#ifndef strlcpy
|
||||||
/*
|
/*
|
||||||
* Copy src to string dst of size siz. At most siz-1 characters
|
* Copy string src to buffer dst of size dsize. At most dsize-1
|
||||||
* will be copied. Always NUL terminates (unless siz == 0).
|
* chars will be copied. Always NUL terminates (unless dsize == 0).
|
||||||
* Returns strlen(src); if retval >= siz, truncation occurred.
|
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcpy(char *dst, const char *src, size_t siz)
|
strlcpy(char *dst, const char *src, size_t dsize)
|
||||||
{
|
{
|
||||||
char *d = dst;
|
const char *osrc = src;
|
||||||
const char *s = src;
|
size_t nleft = dsize;
|
||||||
size_t n = siz;
|
|
||||||
|
|
||||||
/* Copy as many bytes as will fit */
|
/* Copy as many bytes as will fit. */
|
||||||
if (n != 0) {
|
if (nleft != 0) {
|
||||||
while (--n != 0) {
|
while (--nleft != 0) {
|
||||||
if ((*d++ = *s++) == '\0')
|
if ((*dst++ = *src++) == '\0')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not enough room in dst, add NUL and traverse rest of src */
|
/* Not enough room in dst, add NUL and traverse rest of src. */
|
||||||
if (n == 0) {
|
if (nleft == 0) {
|
||||||
if (siz != 0)
|
if (dsize != 0)
|
||||||
*d = '\0'; /* NUL-terminate dst */
|
*dst = '\0'; /* NUL-terminate dst */
|
||||||
while (*s++)
|
while (*src++)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return(s - src - 1); /* count does not include NUL */
|
return(src - osrc - 1); /* count does not include NUL */
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* c-file-style: "bsd"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/* Collection of frog DNA
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008-2016 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LITE_STRING_H_
|
||||||
|
#define LITE_STRING_H_
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "strdupa.h"
|
||||||
|
#include "strndupa.h"
|
||||||
|
#include "strnlen.h"
|
||||||
|
|
||||||
|
#ifndef strlcpy
|
||||||
|
size_t strlcpy (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strlcat
|
||||||
|
size_t strlcat (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strtonum
|
||||||
|
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Convert string to natural number (0-2147483647), returns -1 on error. */
|
||||||
|
static inline int atonum(const char *str)
|
||||||
|
{
|
||||||
|
int val = -1;
|
||||||
|
const char *errstr;
|
||||||
|
|
||||||
|
if (str) {
|
||||||
|
val = strtonum(str, 0, INT32_MAX, &errstr);
|
||||||
|
if (errstr)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Validate string, non NULL and not zero length */
|
||||||
|
static inline int string_valid(const char *s)
|
||||||
|
{
|
||||||
|
return s && strlen(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Relaxed comparison, e.g., sys_string_match("small", "smaller") => TRUE */
|
||||||
|
static inline int string_match(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
size_t min = MIN(strlen(a), strlen(b));
|
||||||
|
|
||||||
|
return !strncasecmp(a, b, min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, e.g., sys_string_match("small", "smaller") => FALSE */
|
||||||
|
static inline int string_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen(a) == strlen(b) && !strcmp(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, like sys_string_compare(), but case insensitive,
|
||||||
|
* e.g., sys_string_match("small", "SmAlL") => TRUE
|
||||||
|
*/
|
||||||
|
static inline int string_case_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen (a) == strlen (b) && !strcasecmp (a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define min(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a < _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define max(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a > _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LITE_STRING_H_ */
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
/* ==========================================================================
|
||||||
|
* strndupa.h - Re-implementation of glibc strndupa.
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Copyright (c) 2009 William Ahern
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
* ==========================================================================
|
||||||
|
*/
|
||||||
|
#ifndef GNU_STRING_STRNDUPA_H
|
||||||
|
#define GNU_STRING_STRNDUPA_H
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRNDUPA)
|
||||||
|
#if defined(strndupa)
|
||||||
|
#define HAVE_STRNDUPA 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !HAVE_STRNDUPA
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
#include <string.h> /* memcpy(3) */
|
||||||
|
|
||||||
|
#include <gnu/string/strnlen.h>
|
||||||
|
|
||||||
|
#define strndupa(src, lim) (__extension__ ({ \
|
||||||
|
size_t len_ = strnlen(src, lim); \
|
||||||
|
char *dst_ = __builtin_alloca(len_ + 1); \
|
||||||
|
dst_[len_] = '\0'; \
|
||||||
|
(char *)memcpy(dst_, src, len_); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#else /* If not GCC, e.g. Clang */
|
||||||
|
+#error strndupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
#endif /* !HAVE_STRNDUPA */
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* GNU_STRING_STRNDUPA_H */
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/* strnlen.h - Re-implementation of GLIBC strnlen()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LITE_STRNLEN_H
|
||||||
|
#define LITE_STRNLEN_H
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRNLEN)
|
||||||
|
#if defined(strnlen)
|
||||||
|
#define HAVE_STRNLEN 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !HAVE_STRNLEN
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
#define strnlen(str, lim) xstrnlen(str, lim)
|
||||||
|
|
||||||
|
static inline size_t xstrnlen(const char *str, size_t lim)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (str[i] && i < lim)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* LITE_STRNLEN_H */
|
||||||
+7
-7
@@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
|
/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
||||||
@@ -22,17 +22,17 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifndef strtonum
|
#ifndef strtonum
|
||||||
#define INVALID 1
|
#define INVALID 1
|
||||||
#define TOOSMALL 2
|
#define TOOSMALL 2
|
||||||
#define TOOLARGE 3
|
#define TOOLARGE 3
|
||||||
|
|
||||||
long long
|
long long
|
||||||
strtonum(const char *numstr, long long minval, long long maxval,
|
strtonum(const char *numstr, long long minval, long long maxval,
|
||||||
const char **errstrp)
|
const char **errstrp)
|
||||||
{
|
{
|
||||||
long long ll = 0;
|
long long ll = 0;
|
||||||
char *ep;
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
char *ep;
|
||||||
struct errval {
|
struct errval {
|
||||||
const char *errstr;
|
const char *errstr;
|
||||||
int err;
|
int err;
|
||||||
@@ -45,9 +45,9 @@ strtonum(const char *numstr, long long minval, long long maxval,
|
|||||||
|
|
||||||
ev[0].err = errno;
|
ev[0].err = errno;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (minval > maxval)
|
if (minval > maxval) {
|
||||||
error = INVALID;
|
error = INVALID;
|
||||||
else {
|
} else {
|
||||||
ll = strtoll(numstr, &ep, 10);
|
ll = strtoll(numstr, &ep, 10);
|
||||||
if (numstr == ep || *ep != '\0')
|
if (numstr == ep || *ep != '\0')
|
||||||
error = INVALID;
|
error = INVALID;
|
||||||
|
|||||||
+2
-11
@@ -17,16 +17,9 @@
|
|||||||
|
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||||
#include <linux/version.h>
|
|
||||||
#include <stdlib.h> /* mkstemp() */
|
|
||||||
#include <stdio.h> /* fdopen() */
|
#include <stdio.h> /* fdopen() */
|
||||||
#include <sys/stat.h> /* umask() */
|
#include <sys/stat.h> /* umask() */
|
||||||
|
|
||||||
#ifndef O_TMPFILE /* Too old GLIBC or kernel */
|
|
||||||
#warning O_TMPFILE missing on your system, tempfile() may not work!
|
|
||||||
#define O_TMPFILE 020200000 /* Define and let it fail at runtime */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tempfile - A secure tmpfile() replacement
|
* tempfile - A secure tmpfile() replacement
|
||||||
*
|
*
|
||||||
@@ -42,7 +35,7 @@
|
|||||||
*/
|
*/
|
||||||
FILE *tempfile(void)
|
FILE *tempfile(void)
|
||||||
{
|
{
|
||||||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
|
#ifdef O_TMPFILE /* Only on Linux, with fairly recent (G)LIBC */
|
||||||
int fd;
|
int fd;
|
||||||
mode_t oldmask;
|
mode_t oldmask;
|
||||||
|
|
||||||
@@ -54,8 +47,7 @@ FILE *tempfile(void)
|
|||||||
|
|
||||||
return fdopen(fd, "rw");
|
return fdopen(fd, "rw");
|
||||||
#else
|
#else
|
||||||
#warning Too old kernel, reverting to wrap unsafe tmpfile() ...
|
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
||||||
return tmpfile();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +62,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f tempfile.mk"
|
* compile-command: "make V=1 -f tempfile.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -144,7 +144,6 @@ int main(void)
|
|||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make V=1 -f tree.mk"
|
* compile-command: "make V=1 -f tree.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
|
|||||||
@@ -0,0 +1,748 @@
|
|||||||
|
/* $OpenBSD: tree.h,v 1.14 2015/05/25 03:07:49 deraadt 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _SYS_TREE_H_
|
||||||
|
#define _SYS_TREE_H_
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file defines data structures for different types of trees:
|
||||||
|
* splay trees and red-black 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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
*
|
||||||
|
* A red-black tree is a binary search tree with the node color as an
|
||||||
|
* extra attribute. It fulfills a set of conditions:
|
||||||
|
* - every search path from the root to a leaf consists of the
|
||||||
|
* same number of black nodes,
|
||||||
|
* - each red node (except for the root) has a black parent,
|
||||||
|
* - each leaf node is black.
|
||||||
|
*
|
||||||
|
* 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).
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define SPLAY_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *sph_root; /* root of the tree */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_INITIALIZER(root) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define SPLAY_INIT(root) do { \
|
||||||
|
(root)->sph_root = NULL; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SPLAY_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *spe_left; /* left element */ \
|
||||||
|
struct type *spe_right; /* right element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
|
||||||
|
#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
|
||||||
|
#define SPLAY_ROOT(head) (head)->sph_root
|
||||||
|
#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
|
||||||
|
|
||||||
|
/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
|
||||||
|
#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
|
||||||
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
|
(head)->sph_root = tmp; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
|
||||||
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
|
(head)->sph_root = tmp; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SPLAY_LINKLEFT(head, tmp, field) do { \
|
||||||
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
|
tmp = (head)->sph_root; \
|
||||||
|
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SPLAY_LINKRIGHT(head, tmp, field) do { \
|
||||||
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
|
tmp = (head)->sph_root; \
|
||||||
|
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
|
||||||
|
SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/* Generates prototypes and inline functions */
|
||||||
|
|
||||||
|
#define SPLAY_PROTOTYPE(name, type, field, cmp) \
|
||||||
|
void name##_SPLAY(struct name *, struct type *); \
|
||||||
|
void name##_SPLAY_MINMAX(struct name *, int); \
|
||||||
|
struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
|
||||||
|
struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
|
||||||
|
\
|
||||||
|
/* Finds the node with the same key as elm */ \
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_FIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (SPLAY_EMPTY(head)) \
|
||||||
|
return(NULL); \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if ((cmp)(elm, (head)->sph_root) == 0) \
|
||||||
|
return (head->sph_root); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_NEXT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if (SPLAY_RIGHT(elm, field) != NULL) { \
|
||||||
|
elm = SPLAY_RIGHT(elm, field); \
|
||||||
|
while (SPLAY_LEFT(elm, field) != NULL) { \
|
||||||
|
elm = SPLAY_LEFT(elm, field); \
|
||||||
|
} \
|
||||||
|
} else \
|
||||||
|
elm = NULL; \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_MIN_MAX(struct name *head, int val) \
|
||||||
|
{ \
|
||||||
|
name##_SPLAY_MINMAX(head, val); \
|
||||||
|
return (SPLAY_ROOT(head)); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Main splay operation.
|
||||||
|
* Moves node close to the key of elm to top
|
||||||
|
*/
|
||||||
|
#define SPLAY_GENERATE(name, type, field, cmp) \
|
||||||
|
struct type * \
|
||||||
|
name##_SPLAY_INSERT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (SPLAY_EMPTY(head)) { \
|
||||||
|
SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
|
||||||
|
} else { \
|
||||||
|
int __comp; \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
__comp = (cmp)(elm, (head)->sph_root); \
|
||||||
|
if(__comp < 0) { \
|
||||||
|
SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
|
||||||
|
SPLAY_RIGHT(elm, field) = (head)->sph_root; \
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = NULL; \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
SPLAY_LEFT(elm, field) = (head)->sph_root; \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = NULL; \
|
||||||
|
} else \
|
||||||
|
return ((head)->sph_root); \
|
||||||
|
} \
|
||||||
|
(head)->sph_root = (elm); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
struct type * \
|
||||||
|
name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *__tmp; \
|
||||||
|
if (SPLAY_EMPTY(head)) \
|
||||||
|
return (NULL); \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if ((cmp)(elm, (head)->sph_root) == 0) { \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
|
||||||
|
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
} else { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void \
|
||||||
|
name##_SPLAY(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type __node, *__left, *__right, *__tmp; \
|
||||||
|
int __comp; \
|
||||||
|
\
|
||||||
|
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
||||||
|
__left = __right = &__node; \
|
||||||
|
\
|
||||||
|
while ((__comp = (cmp)(elm, (head)->sph_root))) { \
|
||||||
|
if (__comp < 0) { \
|
||||||
|
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if ((cmp)(elm, __tmp) < 0){ \
|
||||||
|
SPLAY_ROTATE_RIGHT(head, __tmp, field); \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKLEFT(head, __right, field); \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if ((cmp)(elm, __tmp) > 0){ \
|
||||||
|
SPLAY_ROTATE_LEFT(head, __tmp, field); \
|
||||||
|
if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKRIGHT(head, __left, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Splay with either the minimum or the maximum element \
|
||||||
|
* Used to find minimum or maximum element in tree. \
|
||||||
|
*/ \
|
||||||
|
void name##_SPLAY_MINMAX(struct name *head, int __comp) \
|
||||||
|
{ \
|
||||||
|
struct type __node, *__left, *__right, *__tmp; \
|
||||||
|
\
|
||||||
|
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
||||||
|
__left = __right = &__node; \
|
||||||
|
\
|
||||||
|
while (1) { \
|
||||||
|
if (__comp < 0) { \
|
||||||
|
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if (__comp < 0){ \
|
||||||
|
SPLAY_ROTATE_RIGHT(head, __tmp, field); \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKLEFT(head, __right, field); \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if (__comp > 0) { \
|
||||||
|
SPLAY_ROTATE_LEFT(head, __tmp, field); \
|
||||||
|
if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKRIGHT(head, __left, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_NEGINF -1
|
||||||
|
#define SPLAY_INF 1
|
||||||
|
|
||||||
|
#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
|
||||||
|
#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
|
||||||
|
#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
|
||||||
|
#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
|
||||||
|
#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
|
||||||
|
: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
|
||||||
|
#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
|
||||||
|
: name##_SPLAY_MIN_MAX(x, SPLAY_INF))
|
||||||
|
|
||||||
|
#define SPLAY_FOREACH(x, name, head) \
|
||||||
|
for ((x) = SPLAY_MIN(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = SPLAY_NEXT(name, head, x))
|
||||||
|
|
||||||
|
/* Macros that define a red-black tree */
|
||||||
|
#define RB_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *rbh_root; /* root of the tree */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_INITIALIZER(root) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define RB_INIT(root) do { \
|
||||||
|
(root)->rbh_root = NULL; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define RB_BLACK 0
|
||||||
|
#define RB_RED 1
|
||||||
|
#define RB_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *rbe_left; /* left element */ \
|
||||||
|
struct type *rbe_right; /* right element */ \
|
||||||
|
struct type *rbe_parent; /* parent element */ \
|
||||||
|
int rbe_color; /* node color */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_LEFT(elm, field) (elm)->field.rbe_left
|
||||||
|
#define RB_RIGHT(elm, field) (elm)->field.rbe_right
|
||||||
|
#define RB_PARENT(elm, field) (elm)->field.rbe_parent
|
||||||
|
#define RB_COLOR(elm, field) (elm)->field.rbe_color
|
||||||
|
#define RB_ROOT(head) (head)->rbh_root
|
||||||
|
#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
|
||||||
|
|
||||||
|
#define RB_SET(elm, parent, field) do { \
|
||||||
|
RB_PARENT(elm, field) = parent; \
|
||||||
|
RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
|
||||||
|
RB_COLOR(elm, field) = RB_RED; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define RB_SET_BLACKRED(black, red, field) do { \
|
||||||
|
RB_COLOR(black, field) = RB_BLACK; \
|
||||||
|
RB_COLOR(red, field) = RB_RED; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef RB_AUGMENT
|
||||||
|
#define RB_AUGMENT(x) do {} while (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
|
||||||
|
(tmp) = RB_RIGHT(elm, field); \
|
||||||
|
if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
|
||||||
|
RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
|
||||||
|
} \
|
||||||
|
RB_AUGMENT(elm); \
|
||||||
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
|
||||||
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
} else \
|
||||||
|
(head)->rbh_root = (tmp); \
|
||||||
|
RB_LEFT(tmp, field) = (elm); \
|
||||||
|
RB_PARENT(elm, field) = (tmp); \
|
||||||
|
RB_AUGMENT(tmp); \
|
||||||
|
if ((RB_PARENT(tmp, field))) \
|
||||||
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
|
||||||
|
(tmp) = RB_LEFT(elm, field); \
|
||||||
|
if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
|
||||||
|
RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
|
||||||
|
} \
|
||||||
|
RB_AUGMENT(elm); \
|
||||||
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
|
||||||
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
} else \
|
||||||
|
(head)->rbh_root = (tmp); \
|
||||||
|
RB_RIGHT(tmp, field) = (elm); \
|
||||||
|
RB_PARENT(elm, field) = (tmp); \
|
||||||
|
RB_AUGMENT(tmp); \
|
||||||
|
if ((RB_PARENT(tmp, field))) \
|
||||||
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/* Generates prototypes and inline functions */
|
||||||
|
#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, __attribute__((__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 *);\
|
||||||
|
attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_INSERT(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_FIND(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_NFIND(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_NEXT(struct type *); \
|
||||||
|
attr struct type *name##_RB_PREV(struct type *); \
|
||||||
|
attr struct type *name##_RB_MINMAX(struct name *, int); \
|
||||||
|
\
|
||||||
|
|
||||||
|
/* Main rb operation.
|
||||||
|
* Moves node close to the key of elm to top
|
||||||
|
*/
|
||||||
|
#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, __attribute__((__unused__)) static)
|
||||||
|
#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
|
||||||
|
attr void \
|
||||||
|
name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *parent, *gparent, *tmp; \
|
||||||
|
while ((parent = RB_PARENT(elm, field)) && \
|
||||||
|
RB_COLOR(parent, field) == RB_RED) { \
|
||||||
|
gparent = RB_PARENT(parent, field); \
|
||||||
|
if (parent == RB_LEFT(gparent, field)) { \
|
||||||
|
tmp = RB_RIGHT(gparent, field); \
|
||||||
|
if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_COLOR(tmp, field) = RB_BLACK; \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field);\
|
||||||
|
elm = gparent; \
|
||||||
|
continue; \
|
||||||
|
} \
|
||||||
|
if (RB_RIGHT(parent, field) == elm) { \
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
tmp = parent; \
|
||||||
|
parent = elm; \
|
||||||
|
elm = tmp; \
|
||||||
|
} \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field); \
|
||||||
|
RB_ROTATE_RIGHT(head, gparent, tmp, field); \
|
||||||
|
} else { \
|
||||||
|
tmp = RB_LEFT(gparent, field); \
|
||||||
|
if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_COLOR(tmp, field) = RB_BLACK; \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field);\
|
||||||
|
elm = gparent; \
|
||||||
|
continue; \
|
||||||
|
} \
|
||||||
|
if (RB_LEFT(parent, field) == elm) { \
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
tmp = parent; \
|
||||||
|
parent = elm; \
|
||||||
|
elm = tmp; \
|
||||||
|
} \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field); \
|
||||||
|
RB_ROTATE_LEFT(head, gparent, tmp, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
RB_COLOR(head->rbh_root, field) = RB_BLACK; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr void \
|
||||||
|
name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp; \
|
||||||
|
while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
|
||||||
|
elm != RB_ROOT(head)) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) { \
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
if (RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_SET_BLACKRED(tmp, parent, field); \
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
} \
|
||||||
|
if ((RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
|
||||||
|
(RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
elm = parent; \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
|
||||||
|
struct type *oleft; \
|
||||||
|
if ((oleft = RB_LEFT(tmp, field)))\
|
||||||
|
RB_COLOR(oleft, field) = RB_BLACK;\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
RB_ROTATE_RIGHT(head, tmp, oleft, field);\
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
} \
|
||||||
|
RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
|
||||||
|
RB_COLOR(parent, field) = RB_BLACK; \
|
||||||
|
if (RB_RIGHT(tmp, field)) \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
elm = RB_ROOT(head); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} else { \
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
if (RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_SET_BLACKRED(tmp, parent, field); \
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
} \
|
||||||
|
if ((RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
|
||||||
|
(RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
elm = parent; \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
|
||||||
|
struct type *oright; \
|
||||||
|
if ((oright = RB_RIGHT(tmp, field)))\
|
||||||
|
RB_COLOR(oright, field) = RB_BLACK;\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
RB_ROTATE_LEFT(head, tmp, oright, field);\
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
} \
|
||||||
|
RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
|
||||||
|
RB_COLOR(parent, field) = RB_BLACK; \
|
||||||
|
if (RB_LEFT(tmp, field)) \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
elm = RB_ROOT(head); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
if (elm) \
|
||||||
|
RB_COLOR(elm, field) = RB_BLACK; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_REMOVE(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *child, *parent, *old = elm; \
|
||||||
|
int color; \
|
||||||
|
if (RB_LEFT(elm, field) == NULL) \
|
||||||
|
child = RB_RIGHT(elm, field); \
|
||||||
|
else if (RB_RIGHT(elm, field) == NULL) \
|
||||||
|
child = RB_LEFT(elm, field); \
|
||||||
|
else { \
|
||||||
|
struct type *left; \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
while ((left = RB_LEFT(elm, field))) \
|
||||||
|
elm = left; \
|
||||||
|
child = RB_RIGHT(elm, field); \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
color = RB_COLOR(elm, field); \
|
||||||
|
if (child) \
|
||||||
|
RB_PARENT(child, field) = parent; \
|
||||||
|
if (parent) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) \
|
||||||
|
RB_LEFT(parent, field) = child; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = child; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = child; \
|
||||||
|
if (RB_PARENT(elm, field) == old) \
|
||||||
|
parent = elm; \
|
||||||
|
(elm)->field = (old)->field; \
|
||||||
|
if (RB_PARENT(old, field)) { \
|
||||||
|
if (RB_LEFT(RB_PARENT(old, field), field) == old)\
|
||||||
|
RB_LEFT(RB_PARENT(old, field), field) = elm;\
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(old, field), field) = elm;\
|
||||||
|
RB_AUGMENT(RB_PARENT(old, field)); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = elm; \
|
||||||
|
RB_PARENT(RB_LEFT(old, field), field) = elm; \
|
||||||
|
if (RB_RIGHT(old, field)) \
|
||||||
|
RB_PARENT(RB_RIGHT(old, field), field) = elm; \
|
||||||
|
if (parent) { \
|
||||||
|
left = parent; \
|
||||||
|
do { \
|
||||||
|
RB_AUGMENT(left); \
|
||||||
|
} while ((left = RB_PARENT(left, field))); \
|
||||||
|
} \
|
||||||
|
goto color; \
|
||||||
|
} \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
color = RB_COLOR(elm, field); \
|
||||||
|
if (child) \
|
||||||
|
RB_PARENT(child, field) = parent; \
|
||||||
|
if (parent) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) \
|
||||||
|
RB_LEFT(parent, field) = child; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = child; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = child; \
|
||||||
|
color: \
|
||||||
|
if (color == RB_BLACK) \
|
||||||
|
name##_RB_REMOVE_COLOR(head, parent, child); \
|
||||||
|
return (old); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Inserts a node into the RB tree */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_INSERT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp; \
|
||||||
|
struct type *parent = NULL; \
|
||||||
|
int comp = 0; \
|
||||||
|
tmp = RB_ROOT(head); \
|
||||||
|
while (tmp) { \
|
||||||
|
parent = tmp; \
|
||||||
|
comp = (cmp)(elm, parent); \
|
||||||
|
if (comp < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
RB_SET(elm, parent, field); \
|
||||||
|
if (parent != NULL) { \
|
||||||
|
if (comp < 0) \
|
||||||
|
RB_LEFT(parent, field) = elm; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = elm; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = elm; \
|
||||||
|
name##_RB_INSERT_COLOR(head, elm); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Finds the node with the same key as elm */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_FIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
int comp; \
|
||||||
|
while (tmp) { \
|
||||||
|
comp = cmp(elm, tmp); \
|
||||||
|
if (comp < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Finds the first node greater than or equal to the search key */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_NFIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
struct type *res = NULL; \
|
||||||
|
int comp; \
|
||||||
|
while (tmp) { \
|
||||||
|
comp = cmp(elm, tmp); \
|
||||||
|
if (comp < 0) { \
|
||||||
|
res = tmp; \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
} \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
return (res); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* ARGSUSED */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_NEXT(struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (RB_RIGHT(elm, field)) { \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
while (RB_LEFT(elm, field)) \
|
||||||
|
elm = RB_LEFT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_LEFT(RB_PARENT(elm, field), field))) \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
else { \
|
||||||
|
while (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* ARGSUSED */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_PREV(struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (RB_LEFT(elm, field)) { \
|
||||||
|
elm = RB_LEFT(elm, field); \
|
||||||
|
while (RB_RIGHT(elm, field)) \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
else { \
|
||||||
|
while (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_LEFT(RB_PARENT(elm, field), field)))\
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_MINMAX(struct name *head, int val) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
struct type *parent = NULL; \
|
||||||
|
while (tmp) { \
|
||||||
|
parent = tmp; \
|
||||||
|
if (val < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
} \
|
||||||
|
return (parent); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_NEGINF -1
|
||||||
|
#define RB_INF 1
|
||||||
|
|
||||||
|
#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
|
||||||
|
#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
|
||||||
|
#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
|
||||||
|
#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
|
||||||
|
#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
|
||||||
|
#define RB_PREV(name, x, y) name##_RB_PREV(y)
|
||||||
|
#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
|
||||||
|
#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
|
||||||
|
|
||||||
|
#define RB_FOREACH(x, name, head) \
|
||||||
|
for ((x) = RB_MIN(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = name##_RB_NEXT(x))
|
||||||
|
|
||||||
|
#define RB_FOREACH_SAFE(x, name, head, y) \
|
||||||
|
for ((x) = RB_MIN(name, head); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#define RB_FOREACH_REVERSE(x, name, head) \
|
||||||
|
for ((x) = RB_MAX(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = name##_RB_PREV(x))
|
||||||
|
|
||||||
|
#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
|
||||||
|
for ((x) = RB_MAX(name, head); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#endif /* _SYS_TREE_H_ */
|
||||||
Reference in new issue
Block a user