mirror of
https://github.com/troglobit/libite.git
synced 2026-10-14 10:19:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
12ed342d55 | ||
|
|
23badc990d | ||
|
|
ca53e097c3 | ||
|
|
a7b5c240c1 | ||
|
|
c4eb5cb124 | ||
|
|
db08b20f23 | ||
|
|
8779f54125 | ||
|
|
bd9a618ea3 | ||
|
|
3f89fafe24 | ||
|
|
ebec48d671 | ||
|
|
4d6c32f81a | ||
|
|
25c725c240 | ||
|
|
039a92b292 | ||
|
|
5ae5bfb767 | ||
|
|
a00e765789 | ||
|
|
aff5812c46 | ||
|
|
3809b109c6 | ||
|
|
1909dd475c | ||
|
|
a157fe4162 | ||
|
|
42b6a03317 | ||
|
|
1f1e88a640 | ||
|
|
bb67fb749d | ||
|
|
3e337c7078 | ||
|
|
407e954f02 | ||
|
|
8978cb9bff | ||
|
|
9c90ec56a6 | ||
|
|
44e787c966 | ||
|
|
e8ff38ad6a | ||
|
|
1138822d19 | ||
|
|
de8f2b087c | ||
|
|
87d6fe822c | ||
|
|
57c49bfe0c | ||
|
|
b59249714b | ||
|
|
36da3aa35e | ||
|
|
ab2b0f00fa | ||
|
|
bd69aad13a | ||
|
|
ebe2ad4a4c | ||
|
|
2ea2357b62 | ||
|
|
b7a8042c87 | ||
|
|
7d1423b5b1 | ||
|
|
117ec58a90 | ||
|
|
a4cbc7a10e | ||
|
|
3eb1fbb31d | ||
|
|
f5355ecf7b | ||
|
|
61b1b5b457 | ||
|
|
83f83466b5 | ||
|
|
6d2717726f | ||
|
|
2675426845 | ||
|
|
51c73030e0 | ||
|
|
a9d11d0498 | ||
|
|
1147e547dc | ||
|
|
41c9caff17 | ||
|
|
4005dd5e2c | ||
|
|
614c8cbe47 | ||
|
|
8db314623a | ||
|
|
e731d83881 | ||
|
|
c2d0ed0d12 | ||
|
|
5a50a6add1 | ||
|
|
9ed00baa94 | ||
|
|
1561e7567e | ||
|
|
c5366a33da | ||
|
|
af39ae5fa9 | ||
|
|
1423edc044 | ||
|
|
c48ce14739 | ||
|
|
6f19d1407a | ||
|
|
436a07ee06 | ||
|
|
e37aad9530 | ||
|
|
32827cdea9 | ||
|
|
e4c5a0e77e | ||
|
|
35b43c5c56 | ||
|
|
c5434a4bc0 | ||
|
|
84c3667eba | ||
|
|
262f8b8936 | ||
|
|
c045bc7264 | ||
|
|
82ca189a52 | ||
|
|
1b4dd15614 | ||
|
|
121e8ee888 | ||
|
|
745256909e | ||
|
|
552f917b9b | ||
|
|
08ce3d503e | ||
|
|
22a79ee947 | ||
|
|
1758f9a847 | ||
|
|
9f0979daea | ||
|
|
95922a9536 | ||
|
|
f35f64f06f | ||
|
|
8b7eb36d2a | ||
|
|
5e31d05110 | ||
|
|
5d184794b4 | ||
|
|
0701b688fd | ||
|
|
e0c84e25a7 | ||
|
|
e540ea8735 | ||
|
|
949d0a020f | ||
|
|
36d24ce366 | ||
|
|
80c2a132cd | ||
|
|
5eb4f640b1 | ||
|
|
6b64189d14 | ||
|
|
bf9d1ec012 | ||
|
|
499f4d45d3 | ||
|
|
cd63f8a73d | ||
|
|
ddf5ecc907 | ||
|
|
77971a31b2 | ||
|
|
7627b5848a |
No files matched your search
+24
@@ -0,0 +1,24 @@
|
|||||||
|
*~
|
||||||
|
*.d
|
||||||
|
*.o
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.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*
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
# Travis CI integration -- https://travis-ci.org/troglobit/libuev
|
||||||
|
# Defaults to GNU GCC and autotools: ./configure && make && make test
|
||||||
|
language: c
|
||||||
|
|
||||||
|
# Some of the autotests may use sudo, so use non-dockerized build atm.
|
||||||
|
# http://stackoverflow.com/questions/26299552/travis-sudo-is-disabled
|
||||||
|
sudo: true
|
||||||
|
|
||||||
|
# - make test
|
||||||
|
script:
|
||||||
|
- ./autogen.sh
|
||||||
|
- ./configure --disable-silent-rules
|
||||||
|
- make -j5 clean all
|
||||||
+131
@@ -0,0 +1,131 @@
|
|||||||
|
Change Log
|
||||||
|
==========
|
||||||
|
|
||||||
|
All notable changes to the project are documented in this file.
|
||||||
|
|
||||||
|
[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.4.3...HEAD
|
||||||
|
[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/
|
||||||
|
[README]: https://github.com/troglobit/libite/blob/v1.0.0/README.md
|
||||||
|
|
||||||
|
<!--
|
||||||
|
-- Local Variables:
|
||||||
|
-- mode: markdown
|
||||||
|
-- End:
|
||||||
|
-->
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
Much of the code in libite (-lite) is written by Claudio Matsuoka for Finit
|
||||||
|
and released under the MIT/X11 license. Joachim Nilsson later improved on
|
||||||
|
the Finit code base and included pieces of software released under the ISC
|
||||||
|
and BSD licenses. See each respective file for license details.
|
||||||
|
|
||||||
|
Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
|
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.
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
# Finit libite -- Light-weight utility functions and C-library extensions
|
|
||||||
#
|
|
||||||
# Copyright (c) 2008-2012 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.
|
|
||||||
|
|
||||||
.EXPORT_ALL_VARIABLES:
|
|
||||||
.PHONY: all install clean
|
|
||||||
|
|
||||||
include ../common.mk
|
|
||||||
|
|
||||||
CFLAGS += -fPIC
|
|
||||||
HEADERS = lite.h
|
|
||||||
OBJS := copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o
|
|
||||||
DEPS := $(addprefix ., $(OBJS:.o=.d))
|
|
||||||
|
|
||||||
VER = 0
|
|
||||||
LIBNAME = libite
|
|
||||||
TARGET = $(LIBNAME).so
|
|
||||||
VERLIB = $(TARGET).$(VER)
|
|
||||||
STATICLIB = $(LIBNAME).a
|
|
||||||
|
|
||||||
CHECK_FLAGS = $(CFLAGS) $(CPPFLAGS)
|
|
||||||
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
$(OBJS): Makefile
|
|
||||||
|
|
||||||
$(TARGET): $(OBJS)
|
|
||||||
@printf " LINK %s\n" $@
|
|
||||||
# @$(AR) $(ARFLAGS) $(STATICLIB) $(OBJS)
|
|
||||||
@$(CC) $(LDFLAGS) -shared $(OBJS) -lrt -lcrypt -Wl,-soname,$@ -o $@
|
|
||||||
@ln -sf $@ $(VERLIB)
|
|
||||||
|
|
||||||
install-exec: all
|
|
||||||
@install -d $(DESTDIR)$(libdir)
|
|
||||||
@echo " INSTALL $(DESTDIR)$(libdir)/$(TARGET)"
|
|
||||||
@$(STRIPINST) $(TARGET) $(DESTDIR)$(libdir)/$(TARGET)
|
|
||||||
@ln -sf $(TARGET) $(DESTDIR)$(libdir)/$(VERLIB)
|
|
||||||
|
|
||||||
install-dev:
|
|
||||||
@for file in $(HEADERS); do \
|
|
||||||
printf " INSTALL $(DESTDIR)$(incdir)/$$file\n"; \
|
|
||||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(incdir)/$$file; \
|
|
||||||
done
|
|
||||||
|
|
||||||
install: install-dev install-exec
|
|
||||||
|
|
||||||
uninstall:
|
|
||||||
-@$(RM) $(DESTDIR)$(libdir)/$(VERLIB)
|
|
||||||
-@$(RM) $(DESTDIR)$(libdir)/$(TARGET)
|
|
||||||
|
|
||||||
clean: uninstall
|
|
||||||
-@$(RM) $(OBJS) $(DEPS) $(TARGET) $(VERLIB) $(STATICLIB)
|
|
||||||
|
|
||||||
distclean: clean
|
|
||||||
-@$(RM) $(JUNK) unittest *.o .*.d *.unittest
|
|
||||||
|
|
||||||
ifneq ($(MAKECMDGOALS),clean)
|
|
||||||
ifneq ($(MAKECMDGOALS),distclean)
|
|
||||||
-include $(DEPS)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
ACLOCAL_AMFLAGS = -I m4
|
||||||
|
|
||||||
|
lib_LTLIBRARIES = libite.la
|
||||||
|
pkginclude_HEADERS = lite.h queue.h conio.h tree.h
|
||||||
|
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fsendfile.c \
|
||||||
|
ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c progress.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:1:0
|
||||||
|
|
||||||
|
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
|
||||||
@@ -0,0 +1,309 @@
|
|||||||
|
-lite | Frog DNA, basically
|
||||||
|
===========================
|
||||||
|
[![Travis Status][]][Travis]
|
||||||
|
|
||||||
|
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
|
||||||
|
developed by both [Finit][1] and the [OpenBSD][2] project. Most notably
|
||||||
|
the string functions: [strlcpy(3)][3], [strlcat(3)][3] and the highly
|
||||||
|
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
|
||||||
|
aimo to become another [GLIB][5] though.) One such gap in GLIBC is the
|
||||||
|
missing `_SAFE` macros in `sys/queue.h` — highly recommended when
|
||||||
|
traversing lists to delete/free nodes.
|
||||||
|
|
||||||
|
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
|
||||||
|
are extremely liberal and can be used freely in proprietary software if
|
||||||
|
needed.
|
||||||
|
|
||||||
|
For an introduction to why Libite happened, and how you can use it, see
|
||||||
|
[this blog post][6].
|
||||||
|
|
||||||
|
|
||||||
|
Helper Macros
|
||||||
|
-------------
|
||||||
|
|
||||||
|
- `atonum(str)`
|
||||||
|
|
||||||
|
Convert string to natural number, works for 32-bit non-negative
|
||||||
|
integers. Returns -1 on error. (Uses `strtonum()` internally.)
|
||||||
|
|
||||||
|
- `blkdev(dev)`
|
||||||
|
|
||||||
|
Create block device
|
||||||
|
|
||||||
|
- `chardev(dev)`
|
||||||
|
|
||||||
|
Create character device
|
||||||
|
|
||||||
|
- `erase(path)`
|
||||||
|
|
||||||
|
Erase file/directory with `remove()`. Errors on stderr
|
||||||
|
|
||||||
|
- `makedir(path)`
|
||||||
|
|
||||||
|
Create directory, like `mkdir()`. Errors on stderr
|
||||||
|
|
||||||
|
- `makefifo(path)`
|
||||||
|
|
||||||
|
Create a FIFO, like `mkfifo()`. Errors on stderr
|
||||||
|
|
||||||
|
- `min(a,b)`/`max(a,b)`
|
||||||
|
|
||||||
|
These macros take care to avoid double evaluation.
|
||||||
|
|
||||||
|
- `touch(path)`
|
||||||
|
|
||||||
|
Create a file, or update mtime. Errors on stderr
|
||||||
|
|
||||||
|
- `S_ISEXEC(mode_t m)`
|
||||||
|
|
||||||
|
Mysteriously missing from GLIBC
|
||||||
|
|
||||||
|
- `ISCLR(word,bit)`
|
||||||
|
|
||||||
|
Is bit in (integer) word cleared?
|
||||||
|
|
||||||
|
- `ISSET(word,bit)`
|
||||||
|
|
||||||
|
Is bit in (integer) word set?
|
||||||
|
|
||||||
|
- `ISOTHER(word,bit)`
|
||||||
|
|
||||||
|
Is any other bits, except bit, in (integer) word set?
|
||||||
|
|
||||||
|
- `SETBIT(word,bit)`
|
||||||
|
|
||||||
|
Set bit in (integer) word.
|
||||||
|
|
||||||
|
- `CLRBIT(word,bit)`
|
||||||
|
|
||||||
|
Clear bit in (integer) word.
|
||||||
|
|
||||||
|
- `NELEMS(array)`
|
||||||
|
|
||||||
|
Returns the number of elements in an array. From the great book, The
|
||||||
|
Practice of Programming, by Kernighan and Pike.
|
||||||
|
|
||||||
|
- `UNUSED(var)`
|
||||||
|
|
||||||
|
Shorter and more readable version of `var __attribute__ ((unused))`
|
||||||
|
|
||||||
|
|
||||||
|
Generic Functions
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
- `chomp(str)`
|
||||||
|
|
||||||
|
Perl like chomp function, chop off last char if newline.
|
||||||
|
|
||||||
|
- `copyfile(src, dst, len, symlink)`
|
||||||
|
|
||||||
|
Like the shell `cp(1)` and `dd(1),` can also create symlinks.
|
||||||
|
|
||||||
|
- `dir(dir, ext, filter, list, strip)`
|
||||||
|
|
||||||
|
Wrapper for `scandir()` with optional filter. Returns a list of
|
||||||
|
names: files and directories that must be freed after use. See
|
||||||
|
the unit test at the bottom for an example.
|
||||||
|
|
||||||
|
- `fcopyfile(src, dst)`
|
||||||
|
|
||||||
|
Like `copyfile()` but uses already open `FILE *` pointers. Copies
|
||||||
|
from current file positions to current file positions until EOF.
|
||||||
|
|
||||||
|
- `fexist(file)`
|
||||||
|
|
||||||
|
Check for the existance of a file, returns True(1) or False(0).
|
||||||
|
|
||||||
|
- `fisdir(path)`
|
||||||
|
|
||||||
|
Check for the existance of a directory, returns True(1) or False(0).
|
||||||
|
|
||||||
|
- `fmode(file)`
|
||||||
|
|
||||||
|
Returns the `mode_t` bits of a file or directory.
|
||||||
|
|
||||||
|
- `fsendfile(src, dst, len)`
|
||||||
|
|
||||||
|
Copy data between file streams, very similar to `fcopyfile()`, but
|
||||||
|
`dst` is allowed to be `NULL` to be able to read and discard `len`
|
||||||
|
bytes from `src`.
|
||||||
|
|
||||||
|
- `ifconfig(ifname, addr, mask, up)`
|
||||||
|
|
||||||
|
Basic ifconfig like operations on an interface. Only supports IPv4
|
||||||
|
adresses. Note that mask is not CIDR notation.
|
||||||
|
|
||||||
|
- `lfopen(file, sep)`, `lfclose(lf)`
|
||||||
|
|
||||||
|
API for parsing UNIX style configuration files like `/etc/protocols`
|
||||||
|
and `/etc/services`.
|
||||||
|
|
||||||
|
- `lftok(lf)`
|
||||||
|
|
||||||
|
Read tokens, delimeted by `sep`, from file opened with `lfopen()`.
|
||||||
|
|
||||||
|
- `lfgetkey(lf, key)`
|
||||||
|
|
||||||
|
Find `key` in file opened with `lfopen()`, return value/argument.
|
||||||
|
|
||||||
|
- `lfgetint(lf, key)`
|
||||||
|
|
||||||
|
Wrapper for `lfgetkey()`, returns positive integer value to `key`,
|
||||||
|
or `-1` if `key` is not found.
|
||||||
|
|
||||||
|
- `fgetint(file, sep, key)`
|
||||||
|
|
||||||
|
Wrapper for `lfopen()`, `lfgetint()`, and `lfclose()`. Useful for
|
||||||
|
when only reading a single `key` from a file.
|
||||||
|
|
||||||
|
- `makepath(dir)`
|
||||||
|
|
||||||
|
Create all components of the specified directory.
|
||||||
|
|
||||||
|
- `mkpath(dir, mode)`
|
||||||
|
|
||||||
|
Like `makepath()`, but also takes a `mode_t` permission mode argument.
|
||||||
|
|
||||||
|
- `movefile(src, dst)`
|
||||||
|
|
||||||
|
Like `copyfile()`, but renames `src` to `dst`, or recreates symlink
|
||||||
|
with the `dst` name. On successful operation the source is removed
|
||||||
|
and the function returns POSIX OK (0).
|
||||||
|
|
||||||
|
- `pidfile(basename)`
|
||||||
|
|
||||||
|
Create a daemon PID file in `_PATH_VARRUN` using either `basename` or,
|
||||||
|
if `basename` is `NULL`, `__progname`. The resulting file name is
|
||||||
|
available to the user as a read-only pointer:
|
||||||
|
|
||||||
|
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
|
||||||
|
existence of this file, so make sure to have your signal handlers
|
||||||
|
properly setup before calling this function.
|
||||||
|
|
||||||
|
The pidfile is removed when the program exits, using an `atexit()`
|
||||||
|
handler. However, depending on how the program terminates the file
|
||||||
|
may still exist even though the program is no longer running. This
|
||||||
|
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.
|
||||||
|
|
||||||
|
- `pidfile_read(pidfile)`
|
||||||
|
|
||||||
|
Read PID from pid file created by `pidfile()`.
|
||||||
|
|
||||||
|
- `pidfile_signal(pidfile, signal)`
|
||||||
|
|
||||||
|
Send signal to PID found in pid file created by `pidfile()`.
|
||||||
|
|
||||||
|
- `progress(percent, max_width)`
|
||||||
|
|
||||||
|
Simple ASCII progress bar with a spinner. Start it with `percent=0`
|
||||||
|
and set the `max_width=chars` to indicate width of the progress bar.
|
||||||
|
Called multiple times with the same percentage value cause spinner to
|
||||||
|
spin.
|
||||||
|
|
||||||
|
- `rsync(src, dst, delete, *filter())`
|
||||||
|
|
||||||
|
Very simple `rsync()` to copy files files and directories
|
||||||
|
recursively.
|
||||||
|
|
||||||
|
- `tempfile()`
|
||||||
|
|
||||||
|
Secure replacement for `tmpfile()`. Creates an invisible temporary
|
||||||
|
file in `/tmp` that is removed when the returned `FILE` pointer is
|
||||||
|
closed.
|
||||||
|
|
||||||
|
**Note:** Requires Linux v3.11, or later, will fall back to the old
|
||||||
|
and unsafe `tmpfile()` on older systems.
|
||||||
|
|
||||||
|
- `tree(path, show_perms)`
|
||||||
|
|
||||||
|
Very simple `/bin/tree` replacement. Draw ASCII tree of `path`, with
|
||||||
|
optional listing of file and directory permissions if `show_perms` is
|
||||||
|
set. The `path` argument should be a directory.
|
||||||
|
|
||||||
|
|
||||||
|
OpenBSD Functions
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
The following are popular OpenBSD functions and highly useful macros.
|
||||||
|
|
||||||
|
- `pidfile(basename)`
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
- `strlcpy(dst, src, len)`
|
||||||
|
|
||||||
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3
|
||||||
|
|
||||||
|
- `strlcat(dst, src, len)`
|
||||||
|
|
||||||
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcat.3
|
||||||
|
|
||||||
|
- `strtonum()`
|
||||||
|
|
||||||
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strtonum.3
|
||||||
|
|
||||||
|
- `sys/queue.h` API
|
||||||
|
|
||||||
|
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
|
||||||
|
---------------
|
||||||
|
|
||||||
|
The library is built for and developed on GNU/Linux systems as a light
|
||||||
|
weight utility library. Libite (LITE) employs the de facto standard GNU
|
||||||
|
configure and build system:
|
||||||
|
|
||||||
|
./configure
|
||||||
|
make all
|
||||||
|
make install
|
||||||
|
|
||||||
|
|
||||||
|
TODO
|
||||||
|
----
|
||||||
|
|
||||||
|
- Improve documentation, possibly use kdoc or gdoc2 to generate docs from API.
|
||||||
|
- Continuously, update OpenBSD functions/macros.
|
||||||
|
|
||||||
|
[1]: https://github.com/troglobit/finit
|
||||||
|
[2]: http://www.openbsd.org/
|
||||||
|
[3]: http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
|
||||||
|
[4]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
||||||
|
[5]: https://developer.gnome.org/glib/
|
||||||
|
[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
|
||||||
|
[ISC]: https://en.wikipedia.org/wiki/ISC_license
|
||||||
|
[BSD]: https://en.wikipedia.org/wiki/BSD_licenses
|
||||||
|
[Travis]: https://travis-ci.org/troglobit/libite
|
||||||
|
[Travis Status]: https://travis-ci.org/troglobit/libite.png?branch=master
|
||||||
|
|
||||||
|
<!--
|
||||||
|
-- Local Variables:
|
||||||
|
-- mode: markdown
|
||||||
|
-- End:
|
||||||
|
-->
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
autoreconf -W portability -visfm
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/* Perl inspired chomp() implementation.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014-2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* chomp - Perl like chomp function, chop off last char(s) if newline.
|
||||||
|
* @str: String to chomp
|
||||||
|
*
|
||||||
|
* This function is like Perl chomp, but it's set to chop of all
|
||||||
|
* trailing newlines.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* If @str is a valid pointer this function returns @str, otherwise
|
||||||
|
* @errno is set to %EINVAL and this function returns %NULL.
|
||||||
|
*/
|
||||||
|
char *chomp(char *str)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
if (!str || strlen(str) < 1) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = str + strlen(str) - 1;
|
||||||
|
while (*p == '\n')
|
||||||
|
*p-- = 0;
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <stdio.h>
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i; char t[][16] = { "hej\ndej", "Slime\n\n\\n", "Tripple\n\n\n", "" };
|
||||||
|
for (i = 0; t[i][0]; i++) printf("[%02d]: '%s'\n", i, chomp(t[i])); return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f chomp.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# Use v2.61 for backwards compat with Ubuntu 12.04 LTS
|
||||||
|
AC_PREREQ([2.61])
|
||||||
|
AC_INIT(libite, 1.4.3, 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])
|
||||||
|
AC_CONFIG_MACRO_DIR([m4])
|
||||||
|
|
||||||
|
AC_PROG_CC
|
||||||
|
AM_PROG_AR
|
||||||
|
LT_INIT
|
||||||
|
|
||||||
|
AC_HEADER_STDC
|
||||||
|
AC_C_CONST
|
||||||
|
|
||||||
|
AC_OUTPUT
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
/* A conio.h like implementation for VTANSI displays.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2013 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 CONIO_H_
|
||||||
|
#define CONIO_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Attributes */
|
||||||
|
#define RESETATTR 0
|
||||||
|
#define BRIGHT 1
|
||||||
|
#define DIM 2
|
||||||
|
#define UNDERSCORE 4
|
||||||
|
#define BLINK 5 /* May not work on all displays. */
|
||||||
|
#define REVERSE 7
|
||||||
|
#define HIDDEN 8
|
||||||
|
|
||||||
|
/* Colors for text and background */
|
||||||
|
#define BLACK 0x0
|
||||||
|
#define RED 0x1
|
||||||
|
#define GREEN 0x2
|
||||||
|
#define BROWN 0x3
|
||||||
|
#define BLUE 0x4
|
||||||
|
#define MAGENTA 0x5
|
||||||
|
#define CYAN 0x6
|
||||||
|
#define LIGHTGREY 0x7
|
||||||
|
|
||||||
|
#define DARKGREY 0x10
|
||||||
|
#define LIGHTRED 0x11
|
||||||
|
#define LIGHTGREEN 0x12
|
||||||
|
#define YELLOW 0x13
|
||||||
|
#define LIGHTBLUE 0x14
|
||||||
|
#define LIGHTMAGENTA 0x15
|
||||||
|
#define LIGHTCYAN 0x16
|
||||||
|
#define WHITE 0x17
|
||||||
|
|
||||||
|
/* Esc[2JEsc[1;1H - Clear screen and move cursor to 1,1 (upper left) pos. */
|
||||||
|
#define clrscr() fputs ("\e[2J\e[1;1H", stdout)
|
||||||
|
/* Esc[K - Erases from the current cursor position to the end of the current line. */
|
||||||
|
#define clreol() fputs ("\e[K", stdout)
|
||||||
|
/* Esc[2K - Erases the entire current line. */
|
||||||
|
#define delline() fputs ("\e[2K", stdout)
|
||||||
|
/* Esc[Line;ColumnH - Moves the cursor to the specified position (coordinates) */
|
||||||
|
#define gotoxy(x,y) printf("\e[%d;%dH", y, x)
|
||||||
|
/* Esc[?25l (lower case L) - Hide Cursor */
|
||||||
|
#define hidecursor() fputs ("\e[?25l", stdout)
|
||||||
|
/* Esc[?25h (lower case H) - Show Cursor */
|
||||||
|
#define showcursor() fputs ("\e[?25h", stdout)
|
||||||
|
|
||||||
|
/* Esc[Value;...;Valuem - Set Graphics Mode */
|
||||||
|
#define __set_gm(attr,color,val) \
|
||||||
|
if (!color) \
|
||||||
|
printf("\e[%dm", attr); \
|
||||||
|
else \
|
||||||
|
printf("\e[%d;%dm", color & 0x10 ? 1 : 0, (color & 0xF) + val)
|
||||||
|
#define textattr(attr) __set_gm(attr, 0, 0)
|
||||||
|
#define textcolor(color) __set_gm(RESETATTR, color, 30)
|
||||||
|
#define textbackground(color) __set_gm(RESETATTR, color, 40)
|
||||||
|
|
||||||
|
#endif /* CONIO_H_ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+153
-144
@@ -23,89 +23,157 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
|
|
||||||
static int __isdir_and_append_filename(char *src, char **dst);
|
|
||||||
|
/* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */
|
||||||
|
static int adjust_target(char *src, char **dst)
|
||||||
|
{
|
||||||
|
int isdir = 0;
|
||||||
|
|
||||||
|
if (fisdir(*dst)) {
|
||||||
|
int slash = 0;
|
||||||
|
char *tmp, *ptr = strrchr(src, '/');
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
|
ptr = src;
|
||||||
|
else
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
tmp = malloc(strlen(*dst) + strlen(ptr) + 2);
|
||||||
|
if (!tmp) {
|
||||||
|
errno = EISDIR;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
isdir = 1; /* Free dst before exit! */
|
||||||
|
slash = fisslashdir(*dst);
|
||||||
|
|
||||||
|
sprintf(tmp, "%s%s%s", *dst, slash ? "" : "/", ptr);
|
||||||
|
*dst = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isdir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Actual copy loop, used by both copyfile() and fcopyfile()
|
||||||
|
* breaks loop on error and EOF */
|
||||||
|
static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t ret = 0, size = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
size_t count = num > len ? len : num;
|
||||||
|
|
||||||
|
ret = read(in, buffer, count);
|
||||||
|
if (ret <= 0) {
|
||||||
|
if (ret == -1 && EINTR == errno)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret > 0)
|
||||||
|
size += write(out, buffer, ret);
|
||||||
|
num -= count;
|
||||||
|
} while (num > 0);
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* copyfile - Copy a file to another.
|
* copyfile - Copy a file to another.
|
||||||
* @src: Source file.
|
* @src: Full path name to source file.
|
||||||
* @dst: Target file.
|
* @dst: Full path name to target file.
|
||||||
* @len: Number of bytes to copy, zero (0) for entire file.
|
* @len: Number of bytes to copy, zero (0) for entire file.
|
||||||
|
* @sym: Should symlinks be recreated (1) or followed (0)
|
||||||
*
|
*
|
||||||
* This is a C implementation of the command line cp(1) utility. It is one
|
* This is a C implementation of the command line cp(1) utility. It is one
|
||||||
* of the classic missing links in the UNIX C library. This version is from
|
* of the classic missing links in the UNIX C library. This version is from
|
||||||
* the finit project, http://helllabs.org/finit/, which is a reimplementation
|
* the finit project, http://helllabs.org/finit/, which is a reimplementation
|
||||||
* of fastinit for the Asus EeePC.
|
* of fastinit for the Asus EeePC.
|
||||||
*
|
*
|
||||||
* Only some small changes to save stack space have been introduced.
|
|
||||||
*
|
|
||||||
* Returns:
|
* Returns:
|
||||||
* The number of bytes copied, zero may be error (check errno!), but it
|
* The number of bytes copied, zero may be error (check errno!), but it
|
||||||
* may also indicate that @src was empty. If @src is a directory errno
|
* may also indicate that @src was empty. If @src is a directory @errno
|
||||||
* will be set to %EISDIR since copyfile() is not recursive.
|
* will be set to %EISDIR since copyfile() is not recursive.
|
||||||
*/
|
*/
|
||||||
ssize_t copyfile(char *src, char *dst, int len)
|
ssize_t copyfile(char *src, char *dst, int len, int sym)
|
||||||
{
|
{
|
||||||
char *buffer;
|
char *buffer;
|
||||||
int s, d, n, isdir = 0, saved_errno = 0;
|
int in, out, isdir = 0, saved_errno = 0;
|
||||||
|
size_t num;
|
||||||
ssize_t size = 0;
|
ssize_t size = 0;
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
errno = 0; /* Reset before leaving this function. */
|
errno = 0;
|
||||||
// fprintf (stderr, "%s() src: %s, dst: %s, len: %d\n", __func__, src, dst, len);
|
|
||||||
|
|
||||||
buffer = malloc(BUFSIZ);
|
buffer = malloc(BUFSIZ);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fisdir(src)) {
|
if (fisdir(src)) {
|
||||||
errno = EISDIR; /* Error: source is a directory */
|
saved_errno = EISDIR; /* Error: source is a directory */
|
||||||
return 0;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if target is a directory, then append the src base filename. */
|
/* Check if target is a directory, then append src filename. */
|
||||||
isdir = __isdir_and_append_filename(src, &dst);
|
isdir = adjust_target(src, &dst);
|
||||||
|
|
||||||
/* Len == 0 means copy entire file */
|
/* Check if the source file is a symlink ... */
|
||||||
if (len == 0) {
|
if (stat(src, &st)) {
|
||||||
/* XXX: This feels a bit unsafe, but 2047 MiB may be sufficient for us.
|
size = -1;
|
||||||
* --Jocke 2008-08-11 */
|
goto exit;
|
||||||
len = INT_MAX;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((s = open(src, O_RDONLY)) >= 0) {
|
/* ... only try readlink() if symlink and @sym is set. */
|
||||||
if ((d =
|
if (sym && (st.st_mode & S_IFMT) == S_IFLNK) {
|
||||||
open(dst, O_WRONLY | O_CREAT | O_TRUNC,
|
size = readlink(src, buffer, BUFSIZ);
|
||||||
fmode(src))) >= 0) {
|
if (size > 0) {
|
||||||
do {
|
if (size >= (ssize_t)BUFSIZ) {
|
||||||
int clen = len > BUFSIZ ? BUFSIZ : len;
|
saved_errno = ENOBUFS;
|
||||||
|
size = -1;
|
||||||
if ((n = read(s, buffer, clen)) > 0) {
|
} else {
|
||||||
size += write(d, buffer, n);
|
buffer[size] = 0;
|
||||||
}
|
size = !symlink(buffer, dst);
|
||||||
len -= clen;
|
|
||||||
}
|
}
|
||||||
while (len > 0 && n == BUFSIZ);
|
|
||||||
|
|
||||||
close(d);
|
|
||||||
} else {
|
|
||||||
saved_errno = errno;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(s);
|
/* Don't fall back to copy, that is a potentially
|
||||||
} else {
|
* dangerous race condition, see CWE-367. */
|
||||||
saved_errno = errno;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 0: copy entire file */
|
||||||
|
if (len == 0)
|
||||||
|
num = st.st_size;
|
||||||
|
else
|
||||||
|
num = (size_t)len;
|
||||||
|
|
||||||
|
in = open(src, O_RDONLY);
|
||||||
|
if (in < 0) {
|
||||||
|
saved_errno = errno;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, fmode(src));
|
||||||
|
if (out < 0) {
|
||||||
|
close(in);
|
||||||
|
saved_errno = errno;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = do_copy(in, out, num, buffer, BUFSIZ);
|
||||||
|
if (!size && errno)
|
||||||
|
saved_errno = errno;
|
||||||
|
|
||||||
|
close(out);
|
||||||
|
close(in);
|
||||||
|
|
||||||
|
exit:
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if (isdir)
|
if (isdir)
|
||||||
free(dst);
|
free(dst);
|
||||||
@@ -135,12 +203,12 @@ int movefile(char *src, char *dst)
|
|||||||
int isdir, result = 0;
|
int isdir, result = 0;
|
||||||
|
|
||||||
/* Check if target is a directory, then append the src base filename. */
|
/* Check if target is a directory, then append the src base filename. */
|
||||||
isdir = __isdir_and_append_filename(src, &dst);
|
isdir = adjust_target(src, &dst);
|
||||||
|
|
||||||
if (rename(src, dst)) {
|
if (rename(src, dst)) {
|
||||||
if (errno == EXDEV) {
|
if (errno == EXDEV) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
copyfile(src, dst, 0);
|
copyfile(src, dst, 0, 1);
|
||||||
if (errno)
|
if (errno)
|
||||||
result = 1;
|
result = 1;
|
||||||
else
|
else
|
||||||
@@ -157,105 +225,44 @@ int movefile(char *src, char *dst)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* copy_filep - Copy between FILE *fp.
|
* fcopyfile - Copy between FILE *fp.
|
||||||
* @src: Source FILE.
|
* @src: Source FILE.
|
||||||
* @dst: Destination FILE.
|
* @dst: Destination FILE.
|
||||||
*
|
*
|
||||||
* Function takes signals into account and will restart the syscalls as long
|
* Function takes signals into account and will restart the syscalls as
|
||||||
* as error is %EINTR.
|
* long as error is %EINTR.
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* POSIX OK(0), or non-zero with errno set on error.
|
* POSIX OK(0), or non-zero with errno set on error.
|
||||||
*/
|
*/
|
||||||
int copy_filep(FILE *src, FILE *dst)
|
int fcopyfile(FILE *src, FILE *dst)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int ret;
|
||||||
char *buf = (char *)malloc(BUFSIZ);
|
char *buffer;
|
||||||
|
|
||||||
if (!src || !dst)
|
if (!src || !dst) {
|
||||||
return EINVAL;
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
if (!buf)
|
|
||||||
return errno;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
if (!fgets(buf, BUFSIZ, src)) {
|
|
||||||
if (errno == EINTR) {
|
|
||||||
clearerr(src);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (feof(src))
|
|
||||||
break;
|
|
||||||
|
|
||||||
result = errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
while (EOF == fputs(buf, dst)) {
|
|
||||||
if (errno == EINTR) {
|
|
||||||
clearerr(dst);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
buffer = malloc(BUFSIZ);
|
||||||
free(buf);
|
if (!buffer)
|
||||||
return errno = result;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
/* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */
|
ret = do_copy(fileno(src), fileno(dst), BUFSIZ, buffer, BUFSIZ);
|
||||||
static int __isdir_and_append_filename(char *src, char **dst)
|
if (ret > 0)
|
||||||
{
|
ret = 0;
|
||||||
int isdir = 0;
|
else if (errno)
|
||||||
|
ret = -1;
|
||||||
|
|
||||||
if (fisdir(*dst)) {
|
free(buffer);
|
||||||
int slash = 0;
|
|
||||||
char *tmp, *ptr = strrchr(src, '/');
|
|
||||||
|
|
||||||
if (!ptr)
|
return ret;
|
||||||
ptr = src;
|
|
||||||
else
|
|
||||||
ptr++;
|
|
||||||
|
|
||||||
// fprintf (stderr, "Yes %s is a dir --> add %s\n", *dst, ptr);
|
|
||||||
tmp = malloc(strlen(*dst) + strlen(ptr) + 2);
|
|
||||||
if (!tmp) {
|
|
||||||
errno = EISDIR;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
isdir = 1; /* Free dst before exit! */
|
|
||||||
slash = fisslashdir(*dst);
|
|
||||||
|
|
||||||
sprintf(tmp, "%s%s%s", *dst, slash ? "" : "/", ptr);
|
|
||||||
*dst = tmp;
|
|
||||||
// fprintf (stderr, "NEW DST:%s\n", *dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
return isdir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITTEST
|
#ifdef UNITTEST
|
||||||
#include <stdio.h>
|
#include <err.h>
|
||||||
/* XXX: Make a real set of unit tests ... */
|
|
||||||
#if 0
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
if (argc < 3) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"Usage: %s <src> <dst>\nCopies the <src> file to the <dst> file.\n",
|
|
||||||
argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Copying file %s to %s\n", argv[1], argv[2]);
|
|
||||||
|
|
||||||
return copyfile(argv[1], argv[2], 0);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -267,14 +274,15 @@ int main(void)
|
|||||||
};
|
};
|
||||||
FILE *src, *dst;
|
FILE *src, *dst;
|
||||||
|
|
||||||
fprintf(stderr, "\n=>Start testing copy_filep()\n");
|
printf("=>Start testing fcopyfile()\n");
|
||||||
while (files[i]) {
|
while (files[i]) {
|
||||||
fprintf(stderr, "copy_filep(%s, %s)\t", files[i],
|
printf("fcopyfile(%s, %s)\t", files[i], files[i + 1]);
|
||||||
files[i + 1]);
|
|
||||||
src = fopen(files[i], "r");
|
src = fopen(files[i], "r");
|
||||||
dst = fopen(files[i + 1], "w");
|
dst = fopen(files[i + 1], "w");
|
||||||
if (copy_filep(src, dst))
|
if (fcopyfile(src, dst)) {
|
||||||
perror("Failed");
|
if (!fisdir(files[i + 1]))
|
||||||
|
err(1, "Failed fcopyfile(%s, %s)", files[i], files[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
if (src)
|
if (src)
|
||||||
fclose(src);
|
fclose(src);
|
||||||
@@ -282,36 +290,37 @@ int main(void)
|
|||||||
fclose(dst);
|
fclose(dst);
|
||||||
|
|
||||||
if (fexist(files[i + 2]))
|
if (fexist(files[i + 2]))
|
||||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
printf("OK => %s", files[i + 2]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
remove(files[i + 2]);
|
erase(files[i + 2]);
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n\n=>Start testing copyfile()\n");
|
printf("\n=>Start testing copyfile()\n");
|
||||||
i = 0;
|
i = 0;
|
||||||
while (files[i]) {
|
while (files[i]) {
|
||||||
fprintf(stderr, "copyfile(%s, %s)\t", files[i], files[i + 1]);
|
printf("copyfile(%s, %s)\t", files[i], files[i + 1]);
|
||||||
if (!copyfile(files[i], files[i + 1], 0))
|
if (!copyfile(files[i], files[i + 1], 0, 0))
|
||||||
perror("Failed");
|
err(1, "Failed copyfile(%s, %s)", files[i], files[i + 1]);
|
||||||
|
|
||||||
if (fexist(files[i + 2]))
|
if (fexist(files[i + 2]))
|
||||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
printf("OK => %s", files[i + 2]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
remove(files[i + 2]);
|
erase(files[i + 2]);
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif /* UNITTEST */
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -I../include -o unittest -DUNITTEST copyfile.c && ./unittest"
|
* compile-command: "make V=1 -f copyfile.mk"
|
||||||
* version-control: t
|
* version-control: t
|
||||||
* kept-new-versions: 2
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "ellemtel"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
OBJS = fisdir.o fexist.o fmode.o
|
||||||
|
TARGET = $(OBJS) copyfile.test clean
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/* Functions for operating on files in directories.
|
/* Functions for operating on files in directories.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2008-2012 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2008-2014 Joachim Nilsson <troglobit@gmail.com>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
* Permission to use, copy, modify, and/or 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
|
||||||
@@ -94,7 +94,7 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
n = scandir(dir, &namelist, matcher, alphasort);
|
n = scandir(dir, &namelist, matcher, alphasort);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
perror("scandir");
|
perror("scandir");
|
||||||
} else {
|
} else if (n > 0) {
|
||||||
files = (char **)malloc(n * sizeof(char *));
|
files = (char **)malloc(n * sizeof(char *));
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
if (files) {
|
if (files) {
|
||||||
@@ -109,11 +109,12 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
}
|
}
|
||||||
free(namelist[i]);
|
free(namelist[i]);
|
||||||
}
|
}
|
||||||
free(namelist);
|
if (num)
|
||||||
|
*list = files;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num)
|
if (namelist)
|
||||||
*list = files;
|
free(namelist);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
@@ -122,7 +123,7 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include "otn/c.h"
|
#include "lite.h"
|
||||||
|
|
||||||
#define DIR_TYPE_IMAGE ".img"
|
#define DIR_TYPE_IMAGE ".img"
|
||||||
#define DIR_TYPE_SYSLOG ""
|
#define DIR_TYPE_SYSLOG ""
|
||||||
@@ -137,17 +138,17 @@ int simulate_files(int creat)
|
|||||||
"rr109.img", "rx100.img", "rx107.img", "rm957.img", "messages"
|
"rr109.img", "rx100.img", "rx107.img", "rm957.img", "messages"
|
||||||
};
|
};
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_ELEMENTS(files); i++) {
|
for (i = 0; i < NELEMS(files); i++) {
|
||||||
if (creat)
|
if (creat)
|
||||||
touch(files[i]);
|
touch(files[i]);
|
||||||
else
|
else
|
||||||
remove(files[i]);
|
erase(files[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (creat)
|
if (creat)
|
||||||
symlink("config2.cfg", STARTUP_CONFIG);
|
symlink("config2.cfg", STARTUP_CONFIG);
|
||||||
else
|
else
|
||||||
remove(STARTUP_CONFIG);
|
erase(STARTUP_CONFIG);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -216,11 +217,11 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* UNITTEST */
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -DUNITTEST -o unittest dir.c"
|
* compile-command: "make V=1 -f dir.mk"
|
||||||
* version-control: t
|
* version-control: t
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
|
|||||||
@@ -44,8 +44,36 @@ int fexist(char *file)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
struct { char *file; int exist; } arr[] = {
|
||||||
|
{ "/etc/passwd", 1 },
|
||||||
|
{ "/etc/kalle", 0 },
|
||||||
|
{ "/sbin/init", 1 },
|
||||||
|
{ "/dev/null", 1 },
|
||||||
|
{ NULL, 0 },
|
||||||
|
};
|
||||||
|
FILE *src, *dst;
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(arr); i++) {
|
||||||
|
if (fexist(arr[i].file) != arr[i].exist)
|
||||||
|
err(1, "Failed fexist(%s)", arr[i].file ?: "NULL");
|
||||||
|
else
|
||||||
|
printf("File %-11s %-14s => OK!\n", arr[i].file ?: "NULL",
|
||||||
|
arr[i].exist ? "does exist" : "does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f fexist.mk"
|
||||||
* version-control: t
|
* version-control: t
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
|
|||||||
@@ -42,8 +42,37 @@ int fisdir(char *file)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
struct { char *file; int exist; } arr[] = {
|
||||||
|
{ "/etc/passwd", 0 },
|
||||||
|
{ "/etc/", 1 },
|
||||||
|
{ "/sbin/init", 0 },
|
||||||
|
{ "/dev/null", 0 },
|
||||||
|
{ "/dev/", 1 },
|
||||||
|
{ NULL, 0 },
|
||||||
|
};
|
||||||
|
FILE *src, *dst;
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(arr); i++) {
|
||||||
|
if (fisdir(arr[i].file) != arr[i].exist)
|
||||||
|
err(1, "Failed fisdir(%s)", arr[i].file ?: "NULL");
|
||||||
|
else
|
||||||
|
printf("fisdir() %-11s %-18s => OK!\n", arr[i].file ?: "NULL",
|
||||||
|
arr[i].exist ? "is a directory" : "is NOT a directory");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f fisdir.mk"
|
||||||
* version-control: t
|
* version-control: t
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ mode_t fmode(char *file)
|
|||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return 0; /* Doesn't exist ... */
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stat(file, &sb))
|
if (!stat(file, &sb))
|
||||||
|
|||||||
+119
@@ -0,0 +1,119 @@
|
|||||||
|
/* Copy data between file streams
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 Tobias Waldekranz <tobias@waldekranz.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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fsendfile - copy data between file streams
|
||||||
|
* @src: Source stream
|
||||||
|
* @dst: Destination stream
|
||||||
|
* @len: Number of bytes to copy
|
||||||
|
*
|
||||||
|
* @dst may be %NULL, in which case @len bytes are read and discarded
|
||||||
|
* from @src. This can be useful for streams where seeking is not
|
||||||
|
* permitted. Additionally, @len may be the special value zero (0), in
|
||||||
|
* which case fsendfile() will copy until %EOF is seen on @src.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* The number of bytes copied. If an error is detected -1 is returned
|
||||||
|
* and @errno will be set accordingly.
|
||||||
|
*/
|
||||||
|
size_t fsendfile(FILE *src, FILE *dst, size_t len)
|
||||||
|
{
|
||||||
|
char *buf;
|
||||||
|
size_t blk = BUFSIZ, num = 0, tot = 0;
|
||||||
|
|
||||||
|
if (!src) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = (char *)malloc(BUFSIZ);
|
||||||
|
if (!buf)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
while (!len || tot < len) {
|
||||||
|
if (len && ((len - tot) < BUFSIZ))
|
||||||
|
blk = len - tot;
|
||||||
|
|
||||||
|
num = fread(buf, 1, blk, src);
|
||||||
|
if (num == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (dst && (fwrite(buf, num, 1, dst) != 1)) {
|
||||||
|
num = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tot += num;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
return (num == (size_t)-1) ? (size_t)-1 : tot;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <err.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
char *files[] = {
|
||||||
|
"/etc/passwd", "/tmp/tok",
|
||||||
|
"/etc/passwd", "/tmp/passwd",
|
||||||
|
"/etc/passwd", "/tmp/passwd",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
FILE *src, *dst;
|
||||||
|
|
||||||
|
while (files[i]) {
|
||||||
|
src = fopen(files[i], "r");
|
||||||
|
dst = fopen(files[i + 1], "w");
|
||||||
|
printf("fsendfile(%s, %s, 512)\t", files[i], files[i + 1]);
|
||||||
|
if (-1 == fsendfile(src, dst, 512))
|
||||||
|
err(1, "Failed fsendfile(%s, %s)", files[i], files[i + 1]);
|
||||||
|
|
||||||
|
if (!access(files[i + 1], F_OK))
|
||||||
|
printf("OK => %s\n", files[i + 1]);
|
||||||
|
|
||||||
|
remove(files[i + 1]);
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f fsendfile.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
OBJS =
|
||||||
|
TARGET = fsendfile.test clean
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
/* Fastinit (finit) ifconfig() implementation.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||||
|
* Copyright (C) 2009-2014 Joachim Nilsson <joachim.nilsson@westermo.se>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
extern size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ifconfig - Basic ifconfig like operations on an interface
|
||||||
|
* @ifname: Name of interface to operate on
|
||||||
|
* @addr: If @up then set this optional IPv4 address
|
||||||
|
* @mask: If @up and @addr, and @addr is not INADDR_ANY, then set netmask
|
||||||
|
* @up: Control %IFF_UP flag on interface
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, or non-zero on error.
|
||||||
|
*/
|
||||||
|
int ifconfig(char *ifname, char *addr, char *mask, int up)
|
||||||
|
{
|
||||||
|
int sd, ret = -1;
|
||||||
|
struct ifreq ifr;
|
||||||
|
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||||
|
|
||||||
|
if ((sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memset(&ifr, 0, sizeof (ifr));
|
||||||
|
strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
||||||
|
ifr.ifr_addr.sa_family = AF_INET;
|
||||||
|
|
||||||
|
if (up) {
|
||||||
|
if (addr) {
|
||||||
|
if (inet_pton(AF_INET, addr, &sin->sin_addr) == 1)
|
||||||
|
ret = ioctl(sd, SIOCSIFADDR, &ifr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Non-zero IP address */
|
||||||
|
if (mask && addr && strcmp(addr, "0.0.0.0")) {
|
||||||
|
if (inet_pton(AF_INET, mask, &sin->sin_addr) == -1)
|
||||||
|
ret = ioctl(sd, SIOCSIFNETMASK, &ifr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ioctl(sd, SIOCGIFFLAGS, &ifr)) {
|
||||||
|
if (up)
|
||||||
|
ifr.ifr_flags |= IFF_UP;
|
||||||
|
else
|
||||||
|
ifr.ifr_flags &= ~IFF_UP;
|
||||||
|
|
||||||
|
ret = ioctl(sd, SIOCSIFFLAGS, &ifr);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sd);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,235 @@
|
|||||||
|
/* Parse UNIX /etc configuration files like /etc/protocols and /etc/services
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h> /* FILE */
|
||||||
|
#include <stdlib.h> /* atoi() */
|
||||||
|
#include <string.h> /* strlen(), strncmp(), strtok_r() */
|
||||||
|
#include <sys/param.h> /* MAX() */
|
||||||
|
|
||||||
|
typedef struct lfile {
|
||||||
|
FILE *fp;
|
||||||
|
char buf[256];
|
||||||
|
char *sep, *save;
|
||||||
|
} lfile_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lfopen - Open file and return parsing context
|
||||||
|
* @file: File to parse
|
||||||
|
* @sep: Separator(s) to use in lftok()
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* Pointer to a &lftile_t parser context, or %NULL on error.
|
||||||
|
*/
|
||||||
|
lfile_t *lfopen(char *file, char *sep)
|
||||||
|
{
|
||||||
|
lfile_t *lf;
|
||||||
|
|
||||||
|
if (!file || !sep) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Use calloc for clearing buf on behalf of lftok() */
|
||||||
|
lf = calloc(sizeof(*lf), sizeof(char));
|
||||||
|
if (lf) {
|
||||||
|
lf->fp = fopen(file, "r");
|
||||||
|
lf->sep = sep;
|
||||||
|
lf->save = lf->buf;
|
||||||
|
|
||||||
|
if (!lf->fp) {
|
||||||
|
free(lf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lfclose - Close a parser context
|
||||||
|
* @lf: Pointer to &lfile_t context from lfopen()
|
||||||
|
*/
|
||||||
|
void lfclose(lfile_t *lf)
|
||||||
|
{
|
||||||
|
if (!lf)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (lf->fp)
|
||||||
|
fclose(lf->fp);
|
||||||
|
free(lf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lftok - Get next token in file
|
||||||
|
* @lf: Pointer to &lfile_t context from lfopen()
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* Next token, read from file previously opened with lfopen(),
|
||||||
|
* or %NULL if EOF.
|
||||||
|
*/
|
||||||
|
char *lftok(lfile_t *lf)
|
||||||
|
{
|
||||||
|
char *token;
|
||||||
|
|
||||||
|
if (!lf || !lf->fp || !lf->sep) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
token = strtok_r(NULL, lf->sep, &lf->save);
|
||||||
|
if (!token) {
|
||||||
|
while (fgets(lf->buf, sizeof(lf->buf), lf->fp)) {
|
||||||
|
if (lf->buf[0] == '#')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
token = strtok_r(lf->buf, lf->sep, &lf->save);
|
||||||
|
if (!token)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lfgetkey - Find key in file
|
||||||
|
* @lf: Pointer to &lfile_t context from lfopen()
|
||||||
|
* @key: Key to look for
|
||||||
|
*
|
||||||
|
* Locate @key from the current position in the file parser context
|
||||||
|
* returned from lfopen(). Please note, the search for @key does not
|
||||||
|
* start from the beginning of the file, it searches from the current
|
||||||
|
* position. To restart search from the beginning use rewind() on the
|
||||||
|
* lf->fp.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* The value to @key, or %NULL if not found.
|
||||||
|
*/
|
||||||
|
char *lfgetkey(lfile_t *lf, char *key)
|
||||||
|
{
|
||||||
|
char *token;
|
||||||
|
|
||||||
|
while ((token = lftok(lf))) {
|
||||||
|
if (token[0] == '#')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!strncmp(token, key, MAX(strlen(token), strlen(key))))
|
||||||
|
return lftok(lf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lfgetint - Same as lfgetkey() but returns an integer
|
||||||
|
* @lf: Pointer to &lfile_t context from lfopen()
|
||||||
|
* @key: Key to look for
|
||||||
|
*
|
||||||
|
* This function is the same as lfgetkey() but returns the positive
|
||||||
|
* integer value for the matching @key, if found.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* The positive integer value for @key, or -1 if not found.
|
||||||
|
*/
|
||||||
|
int lfgetint(lfile_t *lf, char *key)
|
||||||
|
{
|
||||||
|
char *token = lfgetkey(lf, key);
|
||||||
|
|
||||||
|
if (token)
|
||||||
|
return atoi(token);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fgetint - Find the integer value for key in a file
|
||||||
|
* @file: File to search for @key
|
||||||
|
* @sep: Separator for tokens in @file
|
||||||
|
* @key: Key to look for in @file
|
||||||
|
*
|
||||||
|
* This is a convenience wrapper for lfopen(), lfgetint(), and
|
||||||
|
* lfclose().
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* The positive integer value for @key, or -1 if not found.
|
||||||
|
*/
|
||||||
|
int fgetint(char *file, char *sep, char *key)
|
||||||
|
{
|
||||||
|
int val = -1;
|
||||||
|
lfile_t *lf;
|
||||||
|
|
||||||
|
lf = lfopen(file, sep);
|
||||||
|
if (lf) {
|
||||||
|
val = lfgetint(lf, key);
|
||||||
|
lfclose(lf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
lfile_t lf;
|
||||||
|
|
||||||
|
val = fgetint("/etc/protocols", " \n\t", "udp");
|
||||||
|
if (val == -1) {
|
||||||
|
perror("Failed locating 'udp' protocol");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("udp has proto %d\n", val);
|
||||||
|
|
||||||
|
val = fgetint("/etc/services", " /\n\t", "ftp");
|
||||||
|
if (val == -1) {
|
||||||
|
perror("Failed locating 'ftp' service");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("ftp is inet port %d\n", val);
|
||||||
|
|
||||||
|
val = fgetint("/etc/group", "x:\n", "utmp");
|
||||||
|
if (val == -1) {
|
||||||
|
perror("Failed locating group 'utmp'");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("utmp is gid %d\n", val);
|
||||||
|
|
||||||
|
val = fgetint("/etc/passwd", "x:\n", "nobody");
|
||||||
|
if (val == -1) {
|
||||||
|
perror("Failed locating user 'nobody'");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("nobody is uid %d\n", val);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f lfile.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -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-2012 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,34 +22,189 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef FINIT_LITE_H_
|
#ifndef LITE_H_
|
||||||
#define FINIT_LITE_H_
|
#define LITE_H_
|
||||||
|
|
||||||
|
#include <err.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
||||||
#include <string.h>
|
#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 <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
typedef struct lfile lfile_t;
|
||||||
|
|
||||||
|
char *chomp (char *str);
|
||||||
|
|
||||||
int fexist (char *file);
|
int fexist (char *file);
|
||||||
int fisdir (char *file);
|
int fisdir (char *file);
|
||||||
mode_t fmode (char *file);
|
mode_t fmode (char *file);
|
||||||
|
|
||||||
ssize_t copyfile (char *src, char *dst, int len);
|
FILE *tempfile (void);
|
||||||
|
ssize_t copyfile (char *src, char *dst, int len, int sym);
|
||||||
int movefile (char *src, char *dst);
|
int movefile (char *src, char *dst);
|
||||||
int copy_filep (FILE *src, FILE *dst);
|
int fcopyfile (FILE *src, FILE *dst);
|
||||||
|
size_t fsendfile (FILE *src, FILE *dst, size_t len);
|
||||||
|
|
||||||
|
int ifconfig (char *ifname, char *addr, char *mask, int up);
|
||||||
|
|
||||||
|
lfile_t*lfopen (char *file, char *sep);
|
||||||
|
void lfclose (lfile_t *lf);
|
||||||
|
char *lftok (lfile_t *lf);
|
||||||
|
char *lfgetkey (lfile_t *lf, char *key);
|
||||||
|
int lfgetint (lfile_t *lf, char *key);
|
||||||
|
int fgetint (char *file, char *sep, char *key);
|
||||||
|
|
||||||
|
int mkpath (char *dir, mode_t mode);
|
||||||
|
int makepath (char *dir);
|
||||||
|
|
||||||
int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
|
int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
|
||||||
int rsync (char *src, char *dst, int delete, int (*filter) (const char *file));
|
int rsync (char *src, char *dst, int delete, int (*filter) (const char *file));
|
||||||
|
|
||||||
|
int pidfile (const char *basename);
|
||||||
|
int pidfile_signal(const char *pidfile, int signal);
|
||||||
|
pid_t pidfile_read (const char *pidfile);
|
||||||
|
pid_t pidfile_poll (const char *pidfile);
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
int tree(char *path, int show_perms);
|
||||||
|
|
||||||
|
#ifndef touch
|
||||||
|
# define touch(x) do { if (mknod((x), S_IFREG|0644, 0) && errno != EEXIST) warn("Failed creating %s", x); } while (0)
|
||||||
|
#endif
|
||||||
|
#ifndef makedir
|
||||||
|
# define makedir(x, p) do { if (mkdir(x, p) && errno != EEXIST) warn("Failed creating directory %s", x); } while (0)
|
||||||
|
#endif
|
||||||
|
#ifndef makefifo
|
||||||
|
# define makefifo(x, p) do { if (mkfifo(x, p) && errno != EEXIST) warn("Failed creating FIFO %s", x); } while (0)
|
||||||
|
#endif
|
||||||
|
#ifndef erase
|
||||||
|
# define erase(x) do { if (remove(x) && errno != ENOENT) warn("Failed removing %s", x); } while (0)
|
||||||
|
#endif
|
||||||
|
#ifndef chardev
|
||||||
|
# define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
|
||||||
|
#endif
|
||||||
|
#ifndef blkdev
|
||||||
|
# define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S_ISEXEC
|
||||||
|
# define S_ISEXEC(m) (((m) & S_IXUSR) == S_IXUSR)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Unline isset(), setbit() et al, these work with integers/shorts/longwords/etc. */
|
||||||
|
#ifndef ISCLR
|
||||||
|
#define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1))
|
||||||
|
#endif
|
||||||
|
#ifndef ISSET
|
||||||
|
#define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0))
|
||||||
|
#endif
|
||||||
|
#ifndef ISOTHER
|
||||||
|
#define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) /* Is any other bit set? */
|
||||||
|
#endif
|
||||||
|
#ifndef SETBIT
|
||||||
|
#define SETBIT(word,bit) (word |= (1 << (bit)))
|
||||||
|
#endif
|
||||||
|
#ifndef CLRBIT
|
||||||
|
#define CLRBIT(word,bit) (word &= ~(1 << (bit)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* From The Practice of Programming, by Kernighan and Pike */
|
||||||
|
#ifndef NELEMS
|
||||||
|
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Mark a function variable as unused, useful for generic callbacks */
|
||||||
|
#ifndef UNUSED
|
||||||
|
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||||
|
#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;
|
|
||||||
|
if (strlen(dir) > 0)
|
||||||
|
return dir[strlen(dir) - 1] == '/';
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* FINIT_LITE_H_ */
|
/* 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; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/* Compat */
|
||||||
|
#define copy_filep(src,dst) fcopyfile(src,dst)
|
||||||
|
#define pidfile_read_pid(file) pifile_read(file)
|
||||||
|
#define signal_pidfile(file,signo) pidfile_signal(file,signo)
|
||||||
|
|
||||||
|
#endif /* LITE_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
*.m4
|
||||||
+124
@@ -0,0 +1,124 @@
|
|||||||
|
/* mkpath() -- Create all components leading up to a given directory
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int mkpath(char *dir, mode_t mode)
|
||||||
|
{
|
||||||
|
if (!dir) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen(dir) == 1 && dir[0] == '/')
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mkpath(dirname(strdupa(dir)), mode);
|
||||||
|
|
||||||
|
return mkdir(dir, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* makepath - Create all components of the specified directory.
|
||||||
|
* @dir: Directory to create.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK (0) on success, otherwise -1 and errno set appropriately.
|
||||||
|
* This function returns EINVAL on bad argument, or ENOMEM when it
|
||||||
|
* fails allocating temporary memory. For other error codes see the
|
||||||
|
* mkdir() syscall description.
|
||||||
|
*/
|
||||||
|
int makepath(char *dir)
|
||||||
|
{
|
||||||
|
return mkpath(dir, 0777);
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************* UNIT TESTS ************************************/
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
int checkpath(char *dir)
|
||||||
|
{
|
||||||
|
char tmp[256];
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
|
snprintf(tmp, sizeof(tmp), "ls -ld %s", dir);
|
||||||
|
if (system(tmp))
|
||||||
|
perror("system");
|
||||||
|
|
||||||
|
if (!stat(dir, &sb) && S_ISDIR(sb.st_mode))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
errno = ENOTDIR;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_makepath(char *dir)
|
||||||
|
{
|
||||||
|
int ret = makepath(dir);
|
||||||
|
|
||||||
|
if (!ret)
|
||||||
|
ret = checkpath(dir);
|
||||||
|
if (ret)
|
||||||
|
perror("Failed");
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i, ret = 0;
|
||||||
|
char *list[] = {
|
||||||
|
"/tmp/tok/",
|
||||||
|
"/tmp/tok2",
|
||||||
|
"/tmp/ab",
|
||||||
|
"/tmp/b",
|
||||||
|
"/tmp/a/",
|
||||||
|
"/tmp/a/b",
|
||||||
|
"/tmp/a/c/",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; list[i]; i++)
|
||||||
|
rmdir(list[i]);
|
||||||
|
|
||||||
|
printf("Testing makepath() ...\n");
|
||||||
|
for (i = 0; list[i] && !ret; i++)
|
||||||
|
ret = test_makepath(list[i]);
|
||||||
|
|
||||||
|
printf("\nCleaning up ...\n");
|
||||||
|
for (i = 0; list[i]; i++)
|
||||||
|
rmdir(list[i]);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f makepath.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
OBJS =
|
||||||
|
TARGET = makepath.test clean
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
/* $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 $ */
|
||||||
|
|
||||||
|
/*-
|
||||||
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This code is derived from software contributed to The NetBSD Foundation
|
||||||
|
* by Jason R. Thorpe.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||||
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||||
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifndef pidfile
|
||||||
|
static char *pidfile_path = NULL;
|
||||||
|
static pid_t pidfile_pid;
|
||||||
|
|
||||||
|
static void pidfile_cleanup(void);
|
||||||
|
|
||||||
|
const char *__pidfile_name = NULL;
|
||||||
|
extern char *__progname;
|
||||||
|
|
||||||
|
int
|
||||||
|
pidfile(const char *basename)
|
||||||
|
{
|
||||||
|
int save_errno;
|
||||||
|
pid_t pid;
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
if (basename == NULL)
|
||||||
|
basename = __progname;
|
||||||
|
|
||||||
|
if (pidfile_path != NULL) {
|
||||||
|
free(pidfile_path);
|
||||||
|
pidfile_path = NULL;
|
||||||
|
__pidfile_name = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* _PATH_VARRUN includes trailing / */
|
||||||
|
if (asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename) == -1)
|
||||||
|
return (-1);
|
||||||
|
|
||||||
|
if ((f = fopen(pidfile_path, "w")) == NULL) {
|
||||||
|
save_errno = errno;
|
||||||
|
free(pidfile_path);
|
||||||
|
pidfile_path = NULL;
|
||||||
|
errno = save_errno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = getpid();
|
||||||
|
if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
|
||||||
|
save_errno = errno;
|
||||||
|
(void) fclose(f);
|
||||||
|
(void) unlink(pidfile_path);
|
||||||
|
free(pidfile_path);
|
||||||
|
pidfile_path = NULL;
|
||||||
|
errno = save_errno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
(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 (pidfile_pid == pid)
|
||||||
|
return (0);
|
||||||
|
|
||||||
|
pidfile_pid = pid;
|
||||||
|
if (atexit(pidfile_cleanup) < 0) {
|
||||||
|
save_errno = errno;
|
||||||
|
(void) unlink(pidfile_path);
|
||||||
|
free(pidfile_path);
|
||||||
|
pidfile_path = NULL;
|
||||||
|
pidfile_pid = 0;
|
||||||
|
errno = save_errno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
__pidfile_name = pidfile_path;
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pidfile_cleanup(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (pidfile_path != NULL && pidfile_pid == getpid())
|
||||||
|
(void) unlink(pidfile_path);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+187
@@ -0,0 +1,187 @@
|
|||||||
|
/* Functions for dealing with PID files (client side)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
extern char *__pidfile_name;
|
||||||
|
extern char *chomp(char *str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pidfile_read - Reads a PID value from a pidfile.
|
||||||
|
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||||
|
*
|
||||||
|
* This function takes a @pidfile and returns the PID found therein.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* On invalid @pidfile -1 and errno set to %EINVAL, when @pidfile does not exist -1
|
||||||
|
* and errno set to %ENOENT. When the pidfile is empty or when its contents cannot
|
||||||
|
* be translated this function returns zero (0), on success this function returns
|
||||||
|
* a PID value greater than one. PID 1 is reserved for the system init process.
|
||||||
|
*/
|
||||||
|
pid_t pidfile_read(const char *pidfile)
|
||||||
|
{
|
||||||
|
int pid = 0;
|
||||||
|
char buf[16];
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
if (!pidfile) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fp = fopen(pidfile, "r");
|
||||||
|
if (!fp)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (fgets(buf, sizeof(buf), fp)) {
|
||||||
|
char *ptr = chomp(buf);
|
||||||
|
|
||||||
|
if (ptr) {
|
||||||
|
errno = 0;
|
||||||
|
pid = strtoul(ptr, NULL, 0);
|
||||||
|
if (errno)
|
||||||
|
pid = 0; /* Failed conversion. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pidfile_poll - Poll for the existence of a pidfile and return PID
|
||||||
|
* @pidfile: Path to pidfile to poll for
|
||||||
|
*
|
||||||
|
* This function polls for the pidfile at @pidfile for at most 5 seconds
|
||||||
|
* before timing out. If the file is created within that time span the
|
||||||
|
* file is read and its PID contents returned.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* The PID read from @pidfile, or zero on timeout.
|
||||||
|
*/
|
||||||
|
pid_t pidfile_poll(const char *pidfile)
|
||||||
|
{
|
||||||
|
pid_t pid = 0;
|
||||||
|
int tries = 0;
|
||||||
|
|
||||||
|
/* Timeout = 100 * 50ms = 5s */
|
||||||
|
while ((pid = pidfile_read(pidfile)) <= 0 && tries++ < 100)
|
||||||
|
usleep(50000); /* Wait 50ms between retries */
|
||||||
|
|
||||||
|
if (pid < 0)
|
||||||
|
pid = 0;
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pidfile_signal - Send signal to a PID and cleanup pidfile afterwards
|
||||||
|
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||||
|
* @signal: Signal to send to PID found in @pidfile.
|
||||||
|
*
|
||||||
|
* If @signal is any of %SIGTERM, %SIGKILL, or if kill() returns -1 the
|
||||||
|
* @pidfile is removed.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, non-zero otherwise.
|
||||||
|
*/
|
||||||
|
int pidfile_signal(const char *pidfile, int signal)
|
||||||
|
{
|
||||||
|
int pid = -1, ret = -1;
|
||||||
|
|
||||||
|
pid = pidfile_read(pidfile);
|
||||||
|
if (pid <= 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
ret = kill(pid, signal);
|
||||||
|
if ((ret == -1) || (signal == SIGTERM) || (signal == SIGKILL))
|
||||||
|
(void)remove(pidfile);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************* UNIT TESTS ************************************/
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <paths.h>
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
extern char *__progname;
|
||||||
|
static char PIDFILE[42];
|
||||||
|
|
||||||
|
static void sigterm_handler(int UNUSED(signo))
|
||||||
|
{
|
||||||
|
printf("Exiting ...\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sigalrm_handler(int UNUSED(signo))
|
||||||
|
{
|
||||||
|
printf("Testing pidfile() ...\n");
|
||||||
|
pidfile(NULL);
|
||||||
|
|
||||||
|
if (!fexist(PIDFILE))
|
||||||
|
err(1, "pidfile() failed creating %s", PIDFILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char cmd[80];
|
||||||
|
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", _PATH_VARRUN, __progname);
|
||||||
|
|
||||||
|
signal(SIGTERM, sigterm_handler);
|
||||||
|
signal(SIGALRM, sigalrm_handler);
|
||||||
|
alarm(1);
|
||||||
|
|
||||||
|
printf("Testing pidfile_poll() ...\n");
|
||||||
|
if (!pidfile_poll(PIDFILE))
|
||||||
|
printf("Timed out!\n");
|
||||||
|
else
|
||||||
|
printf("We got signal!\n");
|
||||||
|
|
||||||
|
printf("Reading pid file, should be %d ...\n", getpid());
|
||||||
|
printf("=> %d\n", pidfile_read(PIDFILE));
|
||||||
|
|
||||||
|
printf("\nComparing __pidfile_name with our guessed PID filename ...\n");
|
||||||
|
printf("strcmp(\"%s\",\n \"%s\") => %s\n\n", __pidfile_name, PIDFILE,
|
||||||
|
!strcmp(__pidfile_name, PIDFILE) ? "OK" : "FAIL");
|
||||||
|
|
||||||
|
/* Occular verification that calling pidfile() again updates mtime */
|
||||||
|
snprintf(cmd, sizeof(cmd), "ls -l --full-time %s", PIDFILE);
|
||||||
|
system(cmd);
|
||||||
|
printf("Before ^^ pidfile() vv after ...\n");
|
||||||
|
pidfile(NULL);
|
||||||
|
system(cmd);
|
||||||
|
|
||||||
|
printf("Signalling ourselves ...\n");
|
||||||
|
|
||||||
|
return pidfile_signal(PIDFILE, SIGTERM);
|
||||||
|
}
|
||||||
|
#endif /* UTEST_PIDFN */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f pidfilefn.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
CFLAGS = -W -Wall -Wextra -Werror
|
||||||
|
OBJS = pidfile.o chomp.o fexist.o
|
||||||
|
ASROOT = 1
|
||||||
|
TARGET = clean $(OBJS) pidfilefn.test
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
+175
@@ -0,0 +1,175 @@
|
|||||||
|
/* Simple termios based progress bar
|
||||||
|
*
|
||||||
|
* Copyright (c) 2012-2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <limits.h> /* INT_MAX */
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "conio.h"
|
||||||
|
|
||||||
|
#define SPINNER_THROB ".oOo"
|
||||||
|
#define SPINNER_PULSAR ".oO°Oo."
|
||||||
|
#define SPINNER_ARROW "v<^>"
|
||||||
|
#define SPINNER_STAR ".oO@*"
|
||||||
|
#define SPINNER_DEFAULT "|/-\\"
|
||||||
|
|
||||||
|
static char spinner(char *style)
|
||||||
|
{
|
||||||
|
size_t num;
|
||||||
|
static unsigned int i = 0;
|
||||||
|
|
||||||
|
if (!style)
|
||||||
|
style = SPINNER_DEFAULT;
|
||||||
|
num = strlen(style);
|
||||||
|
|
||||||
|
return style[i++ % num]; /* % Number of states in style */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* progress - Advanced ASCII progress bar with spinner
|
||||||
|
* @percent: Start first call with this set to 0, end with 100
|
||||||
|
* @max_width: Max width of progress bar, in total characters.
|
||||||
|
*
|
||||||
|
* This function draws an advanced ASCII progressbar at the current
|
||||||
|
* line. It always start from the first column.
|
||||||
|
*
|
||||||
|
* The progress bar will hide the cursor if started with @percent 0 and
|
||||||
|
* show it again at the end, when called with @percent 100.
|
||||||
|
*
|
||||||
|
* While being called with the same percentage the spinner will spin,
|
||||||
|
* to show the user the process hasn't frozen.
|
||||||
|
*
|
||||||
|
* If the output TTY cannot interpret control characters, like \r, it is
|
||||||
|
* advised to instead used the progress_simple() function.
|
||||||
|
*/
|
||||||
|
void progress(int percent, int max_width)
|
||||||
|
{
|
||||||
|
int i, bar;
|
||||||
|
|
||||||
|
/* Adjust for progress bar overhead */
|
||||||
|
max_width -= 10;
|
||||||
|
|
||||||
|
if (0 == percent)
|
||||||
|
hidecursor();
|
||||||
|
|
||||||
|
fprintf(stderr, "\r%3d%% %c [", percent, spinner(NULL));
|
||||||
|
|
||||||
|
bar = percent * max_width / 100;
|
||||||
|
for (i = 0; i < max_width; i++) {
|
||||||
|
if (i > bar)
|
||||||
|
fputc(' ', stderr);
|
||||||
|
else if (i == bar)
|
||||||
|
fputc('>', stderr);
|
||||||
|
else
|
||||||
|
fputc('=', stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "]");
|
||||||
|
if (100 == percent) {
|
||||||
|
showcursor();
|
||||||
|
fputc('\n', stderr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void progress_simple(int percent)
|
||||||
|
{
|
||||||
|
static int last = 1;
|
||||||
|
int ratio, numout;
|
||||||
|
|
||||||
|
if (!percent && last) {
|
||||||
|
last = 0;
|
||||||
|
fputs("0% 25% 50% 75% 100%\n"
|
||||||
|
"|---------+---------+---------+---------|\n"
|
||||||
|
"|", stderr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ratio = 40 * percent / 100;
|
||||||
|
numout = ratio - last;
|
||||||
|
|
||||||
|
if (ratio <= last)
|
||||||
|
return;
|
||||||
|
|
||||||
|
last = ratio;
|
||||||
|
|
||||||
|
while (numout--) {
|
||||||
|
if (ratio != 40 || numout)
|
||||||
|
putc('=', stderr);
|
||||||
|
else
|
||||||
|
putc('|', stderr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include <stdlib.h> /* atexit() */
|
||||||
|
#include <unistd.h> /* usleep() */
|
||||||
|
|
||||||
|
#define MAX_WIDTH 80
|
||||||
|
#define msleep(msec) usleep(msec * 1000)
|
||||||
|
|
||||||
|
static void bye(void)
|
||||||
|
{
|
||||||
|
showcursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testit(int fn, int percent)
|
||||||
|
{
|
||||||
|
if (fn)
|
||||||
|
progress(percent, MAX_WIDTH);
|
||||||
|
else
|
||||||
|
progress_simple(percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test(int fn)
|
||||||
|
{
|
||||||
|
int i, percent, block = 0, num = 85;
|
||||||
|
|
||||||
|
while (block < num) {
|
||||||
|
percent = block * 100 / num;
|
||||||
|
for (i = 0; i < 10; i++) {
|
||||||
|
testit(fn, percent);
|
||||||
|
msleep(5);
|
||||||
|
}
|
||||||
|
block++;
|
||||||
|
msleep(10);
|
||||||
|
}
|
||||||
|
testit(fn, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
atexit(bye);
|
||||||
|
hidecursor();
|
||||||
|
|
||||||
|
printf("\nAdvanced:\n");
|
||||||
|
test(1);
|
||||||
|
printf("\nSimple:\n");
|
||||||
|
test(0);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif /* UNITTEST */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f progress.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
OBJS =
|
||||||
|
TARGET = progress.test clean
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
@@ -0,0 +1,533 @@
|
|||||||
|
/* $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 $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1991, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _SYS_QUEUE_H_
|
||||||
|
#define _SYS_QUEUE_H_
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file defines five types of data structures: singly-linked lists,
|
||||||
|
* lists, simple queues, tail queues and XOR simple queues.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* A singly-linked list is headed by a single forward pointer. The elements
|
||||||
|
* are singly linked for minimum space and pointer manipulation overhead at
|
||||||
|
* the expense of O(n) removal for arbitrary elements. New elements can be
|
||||||
|
* added to the list after an existing element or at the head of the list.
|
||||||
|
* Elements being removed from the head of the list should use the explicit
|
||||||
|
* macro for this purpose for optimum efficiency. A singly-linked list may
|
||||||
|
* only be traversed in the forward direction. Singly-linked lists are ideal
|
||||||
|
* for applications with large datasets and few or no removals or for
|
||||||
|
* implementing a LIFO queue.
|
||||||
|
*
|
||||||
|
* A list is headed by a single forward pointer (or an array of forward
|
||||||
|
* pointers for a hash table header). The elements are doubly linked
|
||||||
|
* so that an arbitrary element can be removed without a need to
|
||||||
|
* traverse the list. New elements can be added to the list before
|
||||||
|
* or after an existing element or at the head of the list. A list
|
||||||
|
* may only be traversed in the forward direction.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* 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
|
||||||
|
* an existing element, at the head of the list, or at the end of the
|
||||||
|
* list. A simple queue may only be traversed in the forward direction.
|
||||||
|
*
|
||||||
|
* A tail 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 doubly
|
||||||
|
* linked so that an arbitrary element can be removed without a need to
|
||||||
|
* traverse the list. New elements can be added to the list before or
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* An XOR simple queue is used in the same way as a regular simple queue.
|
||||||
|
* The difference is that the head structure also includes a "cookie" that
|
||||||
|
* is XOR'd with the queue pointer (first, last or next) to generate the
|
||||||
|
* real pointer value.
|
||||||
|
*
|
||||||
|
* For details on the use of these macros, see the queue(3) manual page.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
|
||||||
|
#define _Q_INVALIDATE(a) (a) = ((void *)-1)
|
||||||
|
#else
|
||||||
|
#define _Q_INVALIDATE(a)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked List definitions.
|
||||||
|
*/
|
||||||
|
#define SLIST_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *slh_first; /* first element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SLIST_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define SLIST_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *sle_next; /* next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked List access methods.
|
||||||
|
*/
|
||||||
|
#define SLIST_FIRST(head) ((head)->slh_first)
|
||||||
|
#define SLIST_END(head) NULL
|
||||||
|
#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
|
||||||
|
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
||||||
|
|
||||||
|
#define SLIST_FOREACH(var, head, field) \
|
||||||
|
for((var) = SLIST_FIRST(head); \
|
||||||
|
(var) != SLIST_END(head); \
|
||||||
|
(var) = SLIST_NEXT(var, field))
|
||||||
|
|
||||||
|
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = SLIST_FIRST(head); \
|
||||||
|
(var) && ((tvar) = SLIST_NEXT(var, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked List functions.
|
||||||
|
*/
|
||||||
|
#define SLIST_INIT(head) { \
|
||||||
|
SLIST_FIRST(head) = SLIST_END(head); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
||||||
|
(elm)->field.sle_next = (slistelm)->field.sle_next; \
|
||||||
|
(slistelm)->field.sle_next = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
(elm)->field.sle_next = (head)->slh_first; \
|
||||||
|
(head)->slh_first = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE_AFTER(elm, field) do { \
|
||||||
|
(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE_HEAD(head, field) do { \
|
||||||
|
(head)->slh_first = (head)->slh_first->field.sle_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE(head, elm, type, field) do { \
|
||||||
|
if ((head)->slh_first == (elm)) { \
|
||||||
|
SLIST_REMOVE_HEAD((head), field); \
|
||||||
|
} else { \
|
||||||
|
struct type *curelm = (head)->slh_first; \
|
||||||
|
\
|
||||||
|
while (curelm->field.sle_next != (elm)) \
|
||||||
|
curelm = curelm->field.sle_next; \
|
||||||
|
curelm->field.sle_next = \
|
||||||
|
curelm->field.sle_next->field.sle_next; \
|
||||||
|
} \
|
||||||
|
_Q_INVALIDATE((elm)->field.sle_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* List definitions.
|
||||||
|
*/
|
||||||
|
#define LIST_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *lh_first; /* first element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define LIST_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *le_next; /* next element */ \
|
||||||
|
struct type **le_prev; /* address of previous next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* List access methods.
|
||||||
|
*/
|
||||||
|
#define LIST_FIRST(head) ((head)->lh_first)
|
||||||
|
#define LIST_END(head) NULL
|
||||||
|
#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
|
||||||
|
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
|
||||||
|
|
||||||
|
#define LIST_FOREACH(var, head, field) \
|
||||||
|
for((var) = LIST_FIRST(head); \
|
||||||
|
(var)!= LIST_END(head); \
|
||||||
|
(var) = LIST_NEXT(var, field))
|
||||||
|
|
||||||
|
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = LIST_FIRST(head); \
|
||||||
|
(var) && ((tvar) = LIST_NEXT(var, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* List functions.
|
||||||
|
*/
|
||||||
|
#define LIST_INIT(head) do { \
|
||||||
|
LIST_FIRST(head) = LIST_END(head); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
|
||||||
|
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
|
||||||
|
(listelm)->field.le_next->field.le_prev = \
|
||||||
|
&(elm)->field.le_next; \
|
||||||
|
(listelm)->field.le_next = (elm); \
|
||||||
|
(elm)->field.le_prev = &(listelm)->field.le_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
|
||||||
|
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
||||||
|
(elm)->field.le_next = (listelm); \
|
||||||
|
*(listelm)->field.le_prev = (elm); \
|
||||||
|
(listelm)->field.le_prev = &(elm)->field.le_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
|
||||||
|
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
|
||||||
|
(head)->lh_first = (elm); \
|
||||||
|
(elm)->field.le_prev = &(head)->lh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_REMOVE(elm, field) do { \
|
||||||
|
if ((elm)->field.le_next != NULL) \
|
||||||
|
(elm)->field.le_next->field.le_prev = \
|
||||||
|
(elm)->field.le_prev; \
|
||||||
|
*(elm)->field.le_prev = (elm)->field.le_next; \
|
||||||
|
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||||
|
_Q_INVALIDATE((elm)->field.le_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_REPLACE(elm, elm2, field) do { \
|
||||||
|
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
|
||||||
|
(elm2)->field.le_next->field.le_prev = \
|
||||||
|
&(elm2)->field.le_next; \
|
||||||
|
(elm2)->field.le_prev = (elm)->field.le_prev; \
|
||||||
|
*(elm2)->field.le_prev = (elm2); \
|
||||||
|
_Q_INVALIDATE((elm)->field.le_prev); \
|
||||||
|
_Q_INVALIDATE((elm)->field.le_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Simple queue definitions.
|
||||||
|
*/
|
||||||
|
#define SIMPLEQ_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *sqh_first; /* first element */ \
|
||||||
|
struct type **sqh_last; /* addr of last next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SIMPLEQ_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL, &(head).sqh_first }
|
||||||
|
|
||||||
|
#define SIMPLEQ_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *sqe_next; /* next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Simple queue access methods.
|
||||||
|
*/
|
||||||
|
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
|
||||||
|
#define SIMPLEQ_END(head) NULL
|
||||||
|
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
|
||||||
|
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
|
||||||
|
|
||||||
|
#define SIMPLEQ_FOREACH(var, head, field) \
|
||||||
|
for((var) = SIMPLEQ_FIRST(head); \
|
||||||
|
(var) != SIMPLEQ_END(head); \
|
||||||
|
(var) = SIMPLEQ_NEXT(var, field))
|
||||||
|
|
||||||
|
#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = SIMPLEQ_FIRST(head); \
|
||||||
|
(var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Simple queue functions.
|
||||||
|
*/
|
||||||
|
#define SIMPLEQ_INIT(head) do { \
|
||||||
|
(head)->sqh_first = NULL; \
|
||||||
|
(head)->sqh_last = &(head)->sqh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
|
||||||
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
|
(head)->sqh_first = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
|
||||||
|
(elm)->field.sqe_next = NULL; \
|
||||||
|
*(head)->sqh_last = (elm); \
|
||||||
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||||
|
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
|
||||||
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
|
(listelm)->field.sqe_next = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_REMOVE_HEAD(head, field) do { \
|
||||||
|
if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
|
||||||
|
(head)->sqh_last = &(head)->sqh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
|
||||||
|
if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
|
||||||
|
== NULL) \
|
||||||
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
|
} 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.
|
||||||
|
*/
|
||||||
|
#define XSIMPLEQ_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *sqx_first; /* first element */ \
|
||||||
|
struct type **sqx_last; /* addr of last next element */ \
|
||||||
|
unsigned long sqx_cookie; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define XSIMPLEQ_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *sqx_next; /* next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XOR Simple queue access methods.
|
||||||
|
*/
|
||||||
|
#define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
|
||||||
|
(unsigned long)(ptr)))
|
||||||
|
#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
|
||||||
|
#define XSIMPLEQ_END(head) NULL
|
||||||
|
#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
|
||||||
|
#define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
|
||||||
|
|
||||||
|
|
||||||
|
#define XSIMPLEQ_FOREACH(var, head, field) \
|
||||||
|
for ((var) = XSIMPLEQ_FIRST(head); \
|
||||||
|
(var) != XSIMPLEQ_END(head); \
|
||||||
|
(var) = XSIMPLEQ_NEXT(head, var, field))
|
||||||
|
|
||||||
|
#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = XSIMPLEQ_FIRST(head); \
|
||||||
|
(var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XOR Simple queue functions.
|
||||||
|
*/
|
||||||
|
#define XSIMPLEQ_INIT(head) do { \
|
||||||
|
arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
|
||||||
|
(head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
|
||||||
|
(head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
if (((elm)->field.sqx_next = (head)->sqx_first) == \
|
||||||
|
XSIMPLEQ_XOR(head, NULL)) \
|
||||||
|
(head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
|
(head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
|
||||||
|
(elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
|
||||||
|
*(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
|
||||||
|
(head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||||
|
if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
|
||||||
|
XSIMPLEQ_XOR(head, NULL)) \
|
||||||
|
(head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
|
(listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
|
||||||
|
if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
|
||||||
|
(head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
|
||||||
|
(head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
|
||||||
|
if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
|
||||||
|
(elm)->field.sqx_next)->field.sqx_next) \
|
||||||
|
== XSIMPLEQ_XOR(head, NULL)) \
|
||||||
|
(head)->sqx_last = \
|
||||||
|
XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tail queue definitions.
|
||||||
|
*/
|
||||||
|
#define TAILQ_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *tqh_first; /* first element */ \
|
||||||
|
struct type **tqh_last; /* addr of last next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TAILQ_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL, &(head).tqh_first }
|
||||||
|
|
||||||
|
#define TAILQ_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *tqe_next; /* next element */ \
|
||||||
|
struct type **tqe_prev; /* address of previous next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tail queue access methods.
|
||||||
|
*/
|
||||||
|
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||||
|
#define TAILQ_END(head) NULL
|
||||||
|
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||||
|
#define TAILQ_LAST(head, headname) \
|
||||||
|
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||||
|
/* XXX */
|
||||||
|
#define TAILQ_PREV(elm, headname, field) \
|
||||||
|
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||||
|
#define TAILQ_EMPTY(head) \
|
||||||
|
(TAILQ_FIRST(head) == TAILQ_END(head))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH(var, head, field) \
|
||||||
|
for((var) = TAILQ_FIRST(head); \
|
||||||
|
(var) != TAILQ_END(head); \
|
||||||
|
(var) = TAILQ_NEXT(var, field))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = TAILQ_FIRST(head); \
|
||||||
|
(var) != TAILQ_END(head) && \
|
||||||
|
((tvar) = TAILQ_NEXT(var, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||||
|
for((var) = TAILQ_LAST(head, headname); \
|
||||||
|
(var) != TAILQ_END(head); \
|
||||||
|
(var) = TAILQ_PREV(var, headname, field))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
||||||
|
for ((var) = TAILQ_LAST(head, headname); \
|
||||||
|
(var) != TAILQ_END(head) && \
|
||||||
|
((tvar) = TAILQ_PREV(var, headname, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tail queue functions.
|
||||||
|
*/
|
||||||
|
#define TAILQ_INIT(head) do { \
|
||||||
|
(head)->tqh_first = NULL; \
|
||||||
|
(head)->tqh_last = &(head)->tqh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
|
||||||
|
(head)->tqh_first->field.tqe_prev = \
|
||||||
|
&(elm)->field.tqe_next; \
|
||||||
|
else \
|
||||||
|
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||||
|
(head)->tqh_first = (elm); \
|
||||||
|
(elm)->field.tqe_prev = &(head)->tqh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||||
|
(elm)->field.tqe_next = NULL; \
|
||||||
|
(elm)->field.tqe_prev = (head)->tqh_last; \
|
||||||
|
*(head)->tqh_last = (elm); \
|
||||||
|
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||||
|
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
|
||||||
|
(elm)->field.tqe_next->field.tqe_prev = \
|
||||||
|
&(elm)->field.tqe_next; \
|
||||||
|
else \
|
||||||
|
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||||
|
(listelm)->field.tqe_next = (elm); \
|
||||||
|
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||||
|
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||||
|
(elm)->field.tqe_next = (listelm); \
|
||||||
|
*(listelm)->field.tqe_prev = (elm); \
|
||||||
|
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_REMOVE(head, elm, field) do { \
|
||||||
|
if (((elm)->field.tqe_next) != NULL) \
|
||||||
|
(elm)->field.tqe_next->field.tqe_prev = \
|
||||||
|
(elm)->field.tqe_prev; \
|
||||||
|
else \
|
||||||
|
(head)->tqh_last = (elm)->field.tqe_prev; \
|
||||||
|
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
|
||||||
|
_Q_INVALIDATE((elm)->field.tqe_prev); \
|
||||||
|
_Q_INVALIDATE((elm)->field.tqe_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_REPLACE(head, elm, elm2, field) do { \
|
||||||
|
if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
|
||||||
|
(elm2)->field.tqe_next->field.tqe_prev = \
|
||||||
|
&(elm2)->field.tqe_next; \
|
||||||
|
else \
|
||||||
|
(head)->tqh_last = &(elm2)->field.tqe_next; \
|
||||||
|
(elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
|
||||||
|
*(elm2)->field.tqe_prev = (elm2); \
|
||||||
|
_Q_INVALIDATE((elm)->field.tqe_prev); \
|
||||||
|
_Q_INVALIDATE((elm)->field.tqe_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_CONCAT(head1, head2, field) do { \
|
||||||
|
if (!TAILQ_EMPTY(head2)) { \
|
||||||
|
*(head1)->tqh_last = (head2)->tqh_first; \
|
||||||
|
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
||||||
|
(head1)->tqh_last = (head2)->tqh_last; \
|
||||||
|
TAILQ_INIT((head2)); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#endif /* !_SYS_QUEUE_H_ */
|
||||||
@@ -25,9 +25,9 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
#include "../helpers.h"
|
|
||||||
|
|
||||||
static int makedir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode);
|
static int copy(char *src, char *dst);
|
||||||
|
static int mdir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode);
|
||||||
static int prune(char *dst, char **new_files, int new_num);
|
static int prune(char *dst, char **new_files, int new_num);
|
||||||
|
|
||||||
|
|
||||||
@@ -58,21 +58,16 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
int i = 0, num = 0, result = 0;
|
int i = 0, num = 0, result = 0;
|
||||||
char **files; /* Array of file names. */
|
char **files; /* Array of file names. */
|
||||||
|
|
||||||
void copy(char *src, char *dst)
|
|
||||||
{
|
|
||||||
errno = 0;
|
|
||||||
copyfile(src, dst, 0);
|
|
||||||
if (errno) result++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fisdir(dst))
|
if (!fisdir(dst))
|
||||||
mkdir(dst, 0755);
|
makedir(dst, 0755);
|
||||||
|
|
||||||
if (!fisdir(src)) {
|
if (!fisdir(src)) {
|
||||||
if (!fexist(src))
|
if (!fexist(src))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
copy(src, dst);
|
if (copy(src, dst))
|
||||||
|
result++;
|
||||||
|
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +80,7 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
else
|
else
|
||||||
ptr++;
|
ptr++;
|
||||||
|
|
||||||
if (makedir(dest, sizeof(dest), dst, ptr, fmode(src)))
|
if (mdir(dest, sizeof(dest), dst, ptr, fmode(src)))
|
||||||
return 1;
|
return 1;
|
||||||
dst = dest;
|
dst = dest;
|
||||||
}
|
}
|
||||||
@@ -98,7 +93,7 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
char dst2[256];
|
char dst2[256];
|
||||||
|
|
||||||
strcat(source, "/");
|
strcat(source, "/");
|
||||||
if (makedir (dst2, sizeof(dst2), dst, files[i], fmode(source))) {
|
if (mdir (dst2, sizeof(dst2), dst, files[i], fmode(source))) {
|
||||||
result++;
|
result++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -106,7 +101,9 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
rsync(source, dst2, delete, filter);
|
rsync(source, dst2, delete, filter);
|
||||||
continue; /* Next file/dir in @src to copy... */
|
continue; /* Next file/dir in @src to copy... */
|
||||||
}
|
}
|
||||||
copy(source, dst);
|
|
||||||
|
if (copy(source, dst))
|
||||||
|
result++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We ignore any errors from the pruning, that phase albeit useful is only
|
/* We ignore any errors from the pruning, that phase albeit useful is only
|
||||||
@@ -123,36 +120,60 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Creates dir/name @mode ... skipping / if dir already ends so. */
|
static int copy(char *src, char *dst)
|
||||||
static int makedir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode)
|
|
||||||
{
|
{
|
||||||
snprintf(buf, buf_len, "%s%s%s/", dir, fisslashdir(dir) ? "" : "/", name);
|
errno = 0;
|
||||||
if (mkdir(buf, mode))
|
|
||||||
return 1;
|
copyfile(src, dst, 0, 1);
|
||||||
|
if (errno) {
|
||||||
|
if (errno != EEXIST)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Creates dir/name @mode ... skipping / if dir already ends so. */
|
||||||
|
static int mdir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode)
|
||||||
|
{
|
||||||
|
snprintf(buf, buf_len, "%s%s%s/", dir, fisslashdir(dir) ? "" : "/", name);
|
||||||
|
if (mkdir(buf, mode)) {
|
||||||
|
if (EEXIST != errno)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int find(char *file, char **files, int num)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
for (n = 0; n < num; n++)
|
||||||
|
if (!strncmp (files[n], file, MAX(strlen(files[n]), strlen(file))))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Prune old files, no longer existing on source, from destination directory. */
|
/* Prune old files, no longer existing on source, from destination directory. */
|
||||||
static int prune(char *dst, char **new_files, int new_num)
|
static int prune(char *dst, char **new_files, int new_num)
|
||||||
{
|
{
|
||||||
int i, num, result = 0;
|
int num, result = 0;
|
||||||
char **files;
|
char **files;
|
||||||
|
|
||||||
int find(char *file) {
|
|
||||||
int n;
|
|
||||||
|
|
||||||
for (n = 0; n < new_num; n++)
|
|
||||||
if (!strncmp (new_files[n], file, MAX(strlen(new_files[n]), strlen(file))))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
num = dir(dst, "", NULL, &files, 0);
|
num = dir(dst, "", NULL, &files, 0);
|
||||||
if (num) {
|
if (num) {
|
||||||
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < num; i++) {
|
for (i = 0; i < num; i++) {
|
||||||
if (!find(files[i])) {
|
if (!find(files[i], new_files, new_num)) {
|
||||||
char *name;
|
char *name;
|
||||||
size_t len = strlen(files[i]) + 2 + strlen(dst);
|
size_t len = strlen(files[i]) + 2 + strlen(dst);
|
||||||
|
|
||||||
@@ -177,7 +198,8 @@ static int prune(char *dst, char **new_files, int new_num)
|
|||||||
#define SRC BASE "src/"
|
#define SRC BASE "src/"
|
||||||
#define DST BASE "dst/"
|
#define DST BASE "dst/"
|
||||||
|
|
||||||
char *files[] = {
|
static int verbose = 0;
|
||||||
|
static char *files[] = {
|
||||||
SRC "sub1/1.tst",
|
SRC "sub1/1.tst",
|
||||||
SRC "sub1/2.tst",
|
SRC "sub1/2.tst",
|
||||||
SRC "sub1/3.tst",
|
SRC "sub1/3.tst",
|
||||||
@@ -219,51 +241,62 @@ void setup_test(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_test()
|
static void check_tree(char *heading, char *dir)
|
||||||
{
|
{
|
||||||
|
if (verbose) {
|
||||||
|
char cmd[128];
|
||||||
|
|
||||||
|
if (heading)
|
||||||
|
puts(heading);
|
||||||
|
|
||||||
|
tree(dir, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_test(void)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
setup_test();
|
setup_test();
|
||||||
puts("Before:");
|
check_tree("Before:", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
rsync(SRC, DST, 0, NULL);
|
result += rsync(SRC, DST, 0, NULL);
|
||||||
puts("After:");
|
check_tree("After:", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
cleanup_test();
|
cleanup_test();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
setup_test();
|
setup_test();
|
||||||
rsync(BASE "src", DST, 0, NULL);
|
result += rsync(BASE "src", DST, 0, NULL);
|
||||||
puts("Only partial rsync of src <-- No slash!");
|
check_tree("Only partial rsync of src <-- No slash!", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
#if 0
|
#if 0
|
||||||
cleanup_test();
|
cleanup_test();
|
||||||
setup_test();
|
setup_test();
|
||||||
rsync(BASE "src/sub1", BASE "dst", 0, NULL);
|
result += rsync(BASE "src/sub1", BASE "dst", 0, NULL);
|
||||||
puts("Only partial rsync of src/sub1 <-- No slashes!!");
|
check_tree("Only partial rsync of src/sub1 <-- No slashes!!", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
cleanup_test();
|
cleanup_test();
|
||||||
setup_test();
|
setup_test();
|
||||||
rsync(BASE "src/sub1/", DST, 0, NULL);
|
result += rsync(BASE "src/sub1/", DST, 0, NULL);
|
||||||
puts("Only partial rsync of src/sub1/");
|
check_tree("Only partial rsync of src/sub1/", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
cleanup_test();
|
cleanup_test();
|
||||||
setup_test();
|
setup_test();
|
||||||
rsync(BASE "src/sub1", DST, 0, NULL);
|
result += rsync(BASE "src/sub1", DST, 0, NULL);
|
||||||
puts("Only partial rsync of src/sub1 <-- No slash!");
|
check_tree("Only partial rsync of src/sub1 <-- No slash!", BASE);
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
rsync("/etc", "/var/tmp", 0, NULL);
|
result += rsync("/etc", "/var/tmp", 0, NULL);
|
||||||
puts("Real life test:");
|
check_tree("Real life test:", "/var/tmp");
|
||||||
system("tree -np /var/tmp");
|
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
if (argc > 1)
|
||||||
|
verbose = !strncmp("-v", argv[1], 2);
|
||||||
|
|
||||||
atexit(cleanup_test);
|
atexit(cleanup_test);
|
||||||
|
|
||||||
return run_test();
|
return run_test();
|
||||||
@@ -272,7 +305,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make -f rsync.mk"
|
* compile-command: "make V=1 -f rsync.mk"
|
||||||
* version-control: t
|
* version-control: t
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
|
|||||||
@@ -1,22 +1,4 @@
|
|||||||
# Unittest makefile for rsync.c
|
OBJS = dir.o copyfile.o fexist.o fisdir.o fmode.o tree.o
|
||||||
.PHONY: clean
|
TARGET = clean $(OBJS) rsync.test
|
||||||
|
|
||||||
SRC = rsync.c
|
|
||||||
OBJS = dir.o copyfile.o fexist.o fisdir.o fmode.o
|
|
||||||
UNIT = rsync.unittest
|
|
||||||
#CPPFLAGS = -DDEBUG -I../../.. -I../../include
|
|
||||||
|
|
||||||
all: build run
|
|
||||||
|
|
||||||
clean:
|
|
||||||
@rm -f $(OBJS) $(UNIT)
|
|
||||||
|
|
||||||
# This rule also forces a cleanup of built objects to not leave any object files
|
|
||||||
# lingering in the tree to interfere with cross builds.
|
|
||||||
build: clean $(OBJS)
|
|
||||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $(UNIT) -DUNITTEST $(SRC) $(OBJS)
|
|
||||||
@rm -f $(OBJS)
|
|
||||||
|
|
||||||
run:
|
|
||||||
@./$(UNIT)
|
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
.PHONY: all clean distclean
|
||||||
|
|
||||||
|
# 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
|
||||||
|
ARFLAGS = crus
|
||||||
|
|
||||||
|
# Default to silent build, use V=1 to get verbose mode.
|
||||||
|
ifeq ($V,1)
|
||||||
|
Q =
|
||||||
|
PRINT = @true
|
||||||
|
REDIRECT =
|
||||||
|
MAKEFLAGS =
|
||||||
|
VERBOSE = -v
|
||||||
|
else
|
||||||
|
Q = @
|
||||||
|
PRINT = @printf
|
||||||
|
REDIRECT = >/dev/null
|
||||||
|
MAKEFLAGS = --no-print-directory --silent
|
||||||
|
VERBOSE =
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Some tests may need to be run as root, e.g. pidfile()
|
||||||
|
ifeq ($(ASROOT), 1)
|
||||||
|
SUDO = sudo -n
|
||||||
|
else
|
||||||
|
SUDO =
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Default install paths
|
||||||
|
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
|
||||||
|
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
|
||||||
|
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
||||||
|
$(Q)$(RM) $(OBJS)
|
||||||
|
$(Q)$(SUDO) ./$@ $(VERBOSE) $(REDIRECT)
|
||||||
|
|
||||||
|
|
||||||
|
# Default build rules for both main and unit test makefiles
|
||||||
|
all:: $(TARGET)
|
||||||
|
|
||||||
|
clean::
|
||||||
|
-$(Q)$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
||||||
|
|
||||||
|
distclean::
|
||||||
|
-$(Q)$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest *.test
|
||||||
|
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
*
|
||||||
|
* 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 <string.h>
|
||||||
|
|
||||||
|
#ifndef strlcat
|
||||||
|
/*
|
||||||
|
* Appends src to string dst of size dsize (unlike strncat, dsize is the
|
||||||
|
* full size of dst, not space left). At most dsize-1 characters
|
||||||
|
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
|
||||||
|
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
|
||||||
|
* If retval >= dsize, truncation occurred.
|
||||||
|
*/
|
||||||
|
size_t
|
||||||
|
strlcat(char *dst, const char *src, size_t dsize)
|
||||||
|
{
|
||||||
|
const char *odst = dst;
|
||||||
|
const char *osrc = src;
|
||||||
|
size_t n = dsize;
|
||||||
|
size_t dlen;
|
||||||
|
|
||||||
|
/* Find the end of dst and adjust bytes left but don't go past end. */
|
||||||
|
while (n-- != 0 && *dst != '\0')
|
||||||
|
dst++;
|
||||||
|
dlen = dst - odst;
|
||||||
|
n = dsize - dlen;
|
||||||
|
|
||||||
|
if (n-- == 0)
|
||||||
|
return(dlen + strlen(src));
|
||||||
|
while (*src != '\0') {
|
||||||
|
if (n != 0) {
|
||||||
|
*dst++ = *src;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
src++;
|
||||||
|
}
|
||||||
|
*dst = '\0';
|
||||||
|
|
||||||
|
return(dlen + (src - osrc)); /* count does not include NUL */
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
*
|
||||||
|
* 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 <string.h>
|
||||||
|
|
||||||
|
#ifndef strlcpy
|
||||||
|
/*
|
||||||
|
* Copy string src to buffer dst of size dsize. At most dsize-1
|
||||||
|
* chars will be copied. Always NUL terminates (unless dsize == 0).
|
||||||
|
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
||||||
|
*/
|
||||||
|
size_t
|
||||||
|
strlcpy(char *dst, const char *src, size_t dsize)
|
||||||
|
{
|
||||||
|
const char *osrc = src;
|
||||||
|
size_t nleft = dsize;
|
||||||
|
|
||||||
|
/* Copy as many bytes as will fit. */
|
||||||
|
if (nleft != 0) {
|
||||||
|
while (--nleft != 0) {
|
||||||
|
if ((*dst++ = *src++) == '\0')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Not enough room in dst, add NUL and traverse rest of src. */
|
||||||
|
if (nleft == 0) {
|
||||||
|
if (dsize != 0)
|
||||||
|
*dst = '\0'; /* NUL-terminate dst */
|
||||||
|
while (*src++)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
return(src - osrc - 1); /* count does not include NUL */
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* 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 <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifndef strtonum
|
||||||
|
#define INVALID 1
|
||||||
|
#define TOOSMALL 2
|
||||||
|
#define TOOLARGE 3
|
||||||
|
|
||||||
|
long long
|
||||||
|
strtonum(const char *numstr, long long minval, long long maxval,
|
||||||
|
const char **errstrp)
|
||||||
|
{
|
||||||
|
long long ll = 0;
|
||||||
|
int error = 0;
|
||||||
|
char *ep;
|
||||||
|
struct errval {
|
||||||
|
const char *errstr;
|
||||||
|
int err;
|
||||||
|
} ev[4] = {
|
||||||
|
{ NULL, 0 },
|
||||||
|
{ "invalid", EINVAL },
|
||||||
|
{ "too small", ERANGE },
|
||||||
|
{ "too large", ERANGE },
|
||||||
|
};
|
||||||
|
|
||||||
|
ev[0].err = errno;
|
||||||
|
errno = 0;
|
||||||
|
if (minval > maxval) {
|
||||||
|
error = INVALID;
|
||||||
|
} else {
|
||||||
|
ll = strtoll(numstr, &ep, 10);
|
||||||
|
if (numstr == ep || *ep != '\0')
|
||||||
|
error = INVALID;
|
||||||
|
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
|
||||||
|
error = TOOSMALL;
|
||||||
|
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
|
||||||
|
error = TOOLARGE;
|
||||||
|
}
|
||||||
|
if (errstrp != NULL)
|
||||||
|
*errstrp = ev[error].errstr;
|
||||||
|
errno = ev[error].err;
|
||||||
|
if (error)
|
||||||
|
ll = 0;
|
||||||
|
|
||||||
|
return (ll);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
/* A secure tmpfile() replacement.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <paths.h>
|
||||||
|
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||||
|
#include <linux/version.h>
|
||||||
|
#include <stdlib.h> /* mkstemp() */
|
||||||
|
#include <stdio.h> /* fdopen() */
|
||||||
|
#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
|
||||||
|
*
|
||||||
|
* This is the secure replacement for tmpfile() that does not exist in
|
||||||
|
* GLIBC. The function uses the Linux specific %O_TMPFILE and %O_EXCL
|
||||||
|
* for security. When the %FILE is fclose()'ed the file contents is
|
||||||
|
* lost. The file is hidden in the %_PATH_TMP directory on the system.
|
||||||
|
*
|
||||||
|
* This function requires Linux 3.11, or later, due to %O_TMPFILE.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* An open %FILE pointer, or %NULL on error.
|
||||||
|
*/
|
||||||
|
FILE *tempfile(void)
|
||||||
|
{
|
||||||
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
|
||||||
|
int fd;
|
||||||
|
mode_t oldmask;
|
||||||
|
|
||||||
|
oldmask = umask(0077);
|
||||||
|
fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
||||||
|
umask(oldmask);
|
||||||
|
if (-1 == fd)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return fdopen(fd, "rw");
|
||||||
|
#else
|
||||||
|
#warning Too old kernel, reverting to wrap unsafe tmpfile() ...
|
||||||
|
return tmpfile();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE *fp = tempfile(); system("ls -lrt "
|
||||||
|
_PATH_TMP " | tail -10"); return fclose(fp);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f tempfile.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
OBJS =
|
||||||
|
TARGET = tempfile.test clean
|
||||||
|
|
||||||
|
include rules.mk
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
/* Simple /bin/tree replacement
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static int all = 0;
|
||||||
|
|
||||||
|
static int filter(const struct dirent *entry)
|
||||||
|
{
|
||||||
|
/* Skip current dir ".", and prev dir "..", from list of files */
|
||||||
|
if ((1 == strlen(entry->d_name) && entry->d_name[0] == '.') ||
|
||||||
|
(2 == strlen(entry->d_name) && !strcmp(entry->d_name, "..")))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (!all && entry->d_name[0] == '.')
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_perms(struct stat *st, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
mode_t m = st->st_mode;
|
||||||
|
|
||||||
|
snprintf(buf, len, "[%c%c%c%c%c%c%c%c%c%c] ",
|
||||||
|
S_ISCHR(m) ? 'c' : S_ISBLK(m) ? 'b' : S_ISFIFO(m) ? 'p' : S_ISLNK(m) ? 'l' : S_ISSOCK(m) ? 's' : '-',
|
||||||
|
(m & S_IRUSR) ? 'r' : '-',
|
||||||
|
(m & S_IWUSR) ? 'w' : '-',
|
||||||
|
(m & S_ISUID ) ? 's' : (m & S_IXUSR) ? 'x' : '-',
|
||||||
|
(m & S_IRGRP) ? 'r' : '-',
|
||||||
|
(m & S_IWGRP) ? 'w' : '-',
|
||||||
|
(m & S_ISGID) ? 's' : (m & S_IXGRP) ? 'x' : '-',
|
||||||
|
(m & S_IROTH) ? 'r' : '-',
|
||||||
|
(m & S_IWOTH) ? 'w' : '-',
|
||||||
|
(m & S_IXOTH) ? 'x' : '-'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int descend(char *path, int show_perms, char *pfx)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if (-1 == lstat(path, &st))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if ((st.st_mode & S_IFMT) == S_IFDIR) {
|
||||||
|
int i, n;
|
||||||
|
struct dirent **namelist = NULL;
|
||||||
|
|
||||||
|
n = scandir(path, &namelist, filter, alphasort);
|
||||||
|
if (n) {
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
char t = ' ', p[14] = "", s[256] = "";
|
||||||
|
char buf[256];
|
||||||
|
char dir[80];
|
||||||
|
|
||||||
|
if (i + 1 == n) {
|
||||||
|
printf("%s `- ", pfx);
|
||||||
|
snprintf(dir, sizeof(dir), "%s ", pfx);
|
||||||
|
} else {
|
||||||
|
printf("%s|-- ", pfx);
|
||||||
|
snprintf(dir, sizeof(dir), "%s| ", pfx);
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name);
|
||||||
|
if (!lstat(buf, &st)) {
|
||||||
|
if (show_perms)
|
||||||
|
get_perms(&st, p, sizeof(p));
|
||||||
|
if ((st.st_mode & S_IFMT) == S_IFDIR)
|
||||||
|
t = '/';
|
||||||
|
if (S_ISLNK(st.st_mode)) {
|
||||||
|
snprintf(s, sizeof(s), "-> ");
|
||||||
|
if (-1 == readlink(buf, &s[3], sizeof(s) - 3))
|
||||||
|
s[0] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s);
|
||||||
|
if (t == '/')
|
||||||
|
result += descend(buf, show_perms, dir);
|
||||||
|
|
||||||
|
free(namelist[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(namelist);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
errno = ENOTDIR;
|
||||||
|
result = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tree(char *path, int show_perms)
|
||||||
|
{
|
||||||
|
printf("[%s]\n", path);
|
||||||
|
return descend(path, show_perms, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNITTEST
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i, result = 0;
|
||||||
|
struct { char *path; int fail; } arr[] = {
|
||||||
|
{ "/etc/passwd", -1 },
|
||||||
|
{ "/dev", 0 },
|
||||||
|
{ "/sbin", 0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(arr); i++) {
|
||||||
|
if (tree(arr[i].path, 1) != arr[i].fail)
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* compile-command: "make V=1 -f tree.mk"
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* 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