mirror of
https://github.com/troglobit/libite.git
synced 2026-10-14 10:19:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20d8d40285 | ||
|
|
4dc24f37e3 | ||
|
|
11f8846f63 | ||
|
|
41778fb3ce | ||
|
|
59d1f0d60c | ||
|
|
afc4289326 | ||
|
|
67267659f7 | ||
|
|
188509d811 | ||
|
|
c10cd67266 | ||
|
|
82db75cac6 | ||
|
|
51970ff86e | ||
|
|
d6fa3cde96 | ||
|
|
9d50de3ec7 | ||
|
|
2aed5a6b3f | ||
|
|
f873635cfe | ||
|
|
c3d5588298 | ||
|
|
a65b2dfd71 | ||
|
|
6e8d8fc32c | ||
|
|
966aee09cd | ||
|
|
f93e298875 | ||
|
|
c8ec83f20c | ||
|
|
835e09a9fe | ||
|
|
13ffe87d10 | ||
|
|
72d1cb6501 | ||
|
|
83ace4f12d | ||
|
|
9bdc5e94b5 | ||
|
|
83047d4413 | ||
|
|
b2c2d032e8 | ||
|
|
7f053f7624 | ||
|
|
b15a14e920 | ||
|
|
7e8534e36b | ||
|
|
3989d91d36 | ||
|
|
fe3673a5e1 | ||
|
|
1bdcb1bfe4 | ||
|
|
75d3f1978e | ||
|
|
c07bfbda58 | ||
|
|
0ef509c393 | ||
|
|
5620faf7bf | ||
|
|
b6a9ed3c6d | ||
|
|
9d63302337 | ||
|
|
02d9d9f8c1 | ||
|
|
9f92a9d572 | ||
|
|
f0e3ed3342 | ||
|
|
34f23a77f8 | ||
|
|
261cf3a01b | ||
|
|
2c9c13bd07 | ||
|
|
59ce0c0dc4 | ||
|
|
a603208da9 | ||
|
|
eaac01871b | ||
|
|
8cefa12675 | ||
|
|
5893010074 | ||
|
|
d84bcb3847 | ||
|
|
a9a3a2dda1 | ||
|
|
20af39300c | ||
|
|
7b612cda83 | ||
|
|
60406c33f6 | ||
|
|
1d21b11802 | ||
|
|
a3badc85f8 | ||
|
|
ac936448f5 | ||
|
|
3a26ab28b2 | ||
|
|
123108fbf9 | ||
|
|
3545d63d2e | ||
|
|
3e921d72e5 | ||
|
|
1c9c038c06 | ||
|
|
e5b79443b8 | ||
|
|
589465f48b | ||
|
|
1f745ff9a0 | ||
|
|
71a0c53029 | ||
|
|
6608d2e8c9 | ||
|
|
62334c80fd | ||
|
|
626d5456f7 | ||
|
|
e3dff8e5be | ||
|
|
bbb951dc23 | ||
|
|
94800e7d5c | ||
|
|
3442d8e12b | ||
|
|
75f57283bd | ||
|
|
daf41d326b | ||
|
|
3179be4348 | ||
|
|
56530a8c78 | ||
|
|
abd6e3abd6 | ||
|
|
b078a8676f | ||
|
|
c2b92a38ef | ||
|
|
98b2e9d426 | ||
|
|
96d3e5dd8b | ||
|
|
ff626acb73 | ||
|
|
ba3f7c89fb | ||
|
|
714ea88cc9 | ||
|
|
add4d5d325 | ||
|
|
be6f276557 | ||
|
|
6daebf4ea4 | ||
|
|
4d6e3e060e | ||
|
|
86eef8e38d | ||
|
|
d5d3dafc78 | ||
|
|
2534c398ea | ||
|
|
3691044007 | ||
|
|
6770dad0cc | ||
|
|
ef0db5e766 | ||
|
|
485463c70e | ||
|
|
4b77eb1423 | ||
|
|
0b54dc6015 | ||
|
|
9d6df87d0d | ||
|
|
5b2eee6aff | ||
|
|
6904f6e0fe | ||
|
|
877ba02d13 | ||
|
|
25dfded835 | ||
|
|
1323a88e94 | ||
|
|
018e607290 | ||
|
|
e48c9c5589 | ||
|
|
2c875afe06 | ||
|
|
10e4a1d057 | ||
|
|
3dde2922c3 | ||
|
|
4dd9459f65 | ||
|
|
7172f487d5 | ||
|
|
4c0967405d | ||
|
|
fd65af31db | ||
|
|
e55976d137 | ||
|
|
1b1ffb33f1 | ||
|
|
d018a5215f | ||
|
|
0d70c116d7 | ||
|
|
32c774deef | ||
|
|
441d5f58d7 | ||
|
|
2a59289d53 | ||
|
|
f89c3c88de | ||
|
|
705b402344 | ||
|
|
fe82ab953c | ||
|
|
6ec2a31042 | ||
|
|
9f5e852084 | ||
|
|
628c5597b9 | ||
|
|
b7b635a3f4 | ||
|
|
dd53c3ae9e | ||
|
|
16a30a293c | ||
|
|
325fcbe149 | ||
|
|
cbb1604ea0 | ||
|
|
8587971fb9 | ||
|
|
6545ac8fd4 | ||
|
|
9105e8d3d8 | ||
|
|
3555af6a6a | ||
|
|
42715eab8a | ||
|
|
497d58dbf0 | ||
|
|
9ab70ebbcd | ||
|
|
bad363883d | ||
|
|
7f3215b3e6 | ||
|
|
729cc5307f | ||
|
|
66290d132f | ||
|
|
59f7a78441 | ||
|
|
b716776863 | ||
|
|
018ecaca15 | ||
|
|
a293c37777 | ||
|
|
a4937a93e2 | ||
|
|
94d547c9ec | ||
|
|
68cf8b6576 | ||
|
|
b6a55fcc46 | ||
|
|
5f29ef6413 | ||
|
|
627c47caff | ||
|
|
7c2ed556b9 | ||
|
|
6dec4e3858 | ||
|
|
dff92356b3 | ||
|
|
883c90112d | ||
|
|
a2c8dddaad | ||
|
|
14f547b638 | ||
|
|
6f2a03bba7 | ||
|
|
cdfffa312b | ||
|
|
50cb72018f | ||
|
|
e8a3117158 | ||
|
|
7cf2a3786c | ||
|
|
1c1d5c94e3 | ||
|
|
23703c75da | ||
|
|
efae15416c | ||
|
|
661b4b4d0b | ||
|
|
682b662bd5 | ||
|
|
75d4edb424 | ||
|
|
141d4f6936 | ||
|
|
63aa6c5d07 | ||
|
|
1d698418c7 | ||
|
|
92d81280a9 | ||
|
|
267465f3d2 | ||
|
|
c22a6e64d9 | ||
|
|
16ae343df2 | ||
|
|
6e558e98f0 | ||
|
|
69291d3e74 | ||
|
|
6fdffeb355 | ||
|
|
b2f6721813 | ||
|
|
3c350b8862 | ||
|
|
5b786ce221 | ||
|
|
86b883e2af | ||
|
|
2d6e8850a2 | ||
|
|
d8cb477861 | ||
|
|
27644677dd | ||
|
|
3588c9b01f | ||
|
|
ca3ea5ae92 | ||
|
|
188d968f89 | ||
|
|
8e2c58501c | ||
|
|
1da317ce1e | ||
|
|
fe39284d50 | ||
|
|
c3c23b9aef | ||
|
|
d45f2bad5b | ||
|
|
61fb7aeca2 | ||
|
|
dedcaf92d5 | ||
|
|
c009c49a92 | ||
|
|
12f619a320 | ||
|
|
c1b3b76ea3 | ||
|
|
8108b8ec14 | ||
|
|
0e2d365eb9 | ||
|
|
8b499aa9cd | ||
|
|
f4fa44fe60 | ||
|
|
be08bcd09d | ||
|
|
1495a3f858 | ||
|
|
5eb816b4c0 | ||
|
|
456c58d52d | ||
|
|
59869b876e | ||
|
|
113c3487bd | ||
|
|
de73e268d3 | ||
|
|
b57c257f09 | ||
|
|
94d42b3df7 | ||
|
|
94ad65a121 | ||
|
|
f3f6947604 | ||
|
|
613d397161 | ||
|
|
7df1b75fa0 | ||
|
|
9b1b7ca4cf | ||
|
|
1e064b8b4d | ||
|
|
18dde87f0a | ||
|
|
fcb38c9cc5 | ||
|
|
ab69ae8374 | ||
|
|
95f7a91a8a | ||
|
|
aab5a1145c | ||
|
|
fe1a3a731d | ||
|
|
cd073f5221 | ||
|
|
1a9d3bd91b | ||
|
|
8cc267f259 | ||
|
|
35d396ec27 | ||
|
|
7e91307138 | ||
|
|
2b8f54fbc7 | ||
|
|
2e58dd3203 | ||
|
|
41392f7e85 | ||
|
|
e4329d6c25 | ||
|
|
7113b3d1d8 | ||
|
|
9b6ef1f053 | ||
|
|
efc70e3a75 | ||
|
|
147df0c6da | ||
|
|
7f89000c6c | ||
|
|
7607148d46 | ||
|
|
979f76aea7 | ||
|
|
ac41fa70aa | ||
|
|
5f820b17b6 | ||
|
|
53b715c094 | ||
|
|
1b4cd7b20b | ||
|
|
8e379ca358 | ||
|
|
12241dcdfe | ||
|
|
0ce1b3bf44 | ||
|
|
38ff0af61a | ||
|
|
00737a5b74 | ||
|
|
1b8b164ed4 | ||
|
|
9f279c0fec | ||
|
|
4d7e6d4f78 | ||
|
|
0a57f2128f | ||
|
|
e254db48a8 | ||
|
|
0fe429dcbe | ||
|
|
f1a2e01775 | ||
|
|
e5eef2ae59 | ||
|
|
f9caffc9b7 | ||
|
|
604b503b84 | ||
|
|
5fc248522c | ||
|
|
c0b8bab164 | ||
|
|
c0e9c0431a | ||
|
|
efdd61648a | ||
|
|
2e8e64fd2a | ||
|
|
c4109e7e74 | ||
|
|
5d07594709 | ||
|
|
253460b4b8 | ||
|
|
acc59e0c1f | ||
|
|
90374fc237 | ||
|
|
7f375b6e9d | ||
|
|
e4236db2bd | ||
|
|
42a5a906fc | ||
|
|
10b28127f5 | ||
|
|
d265a05422 | ||
|
|
f07c05138d | ||
|
|
e1f8224666 | ||
|
|
84650d8836 | ||
|
|
b4fbd54a8c | ||
|
|
c14c6a1e62 | ||
|
|
8aecba2b7a | ||
|
|
74dcaddc55 | ||
|
|
c5a36ff7bd | ||
|
|
3371e42035 | ||
|
|
4e57a78255 | ||
|
|
ce3bd4ec8a | ||
|
|
805571a7ef | ||
|
|
36435997bf | ||
|
|
f961889724 | ||
|
|
6605fb9771 | ||
|
|
61254279b9 | ||
|
|
e42aca8856 | ||
|
|
16611f3e5f | ||
|
|
975df87d6b | ||
|
|
148027674a | ||
|
|
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 |
No files matched your search
+21
-5
@@ -1,5 +1,21 @@
|
|||||||
*.d
|
*~
|
||||||
*.o
|
ID
|
||||||
*.so
|
GPATH
|
||||||
*.so.*
|
GRTAGS
|
||||||
*.a
|
GTAGS
|
||||||
|
compile
|
||||||
|
config.*
|
||||||
|
configure
|
||||||
|
depcomp
|
||||||
|
install-sh
|
||||||
|
libtool
|
||||||
|
ltmain.sh
|
||||||
|
missing
|
||||||
|
stamp-h1
|
||||||
|
Makefile
|
||||||
|
Makefile.in
|
||||||
|
aclocal.m4
|
||||||
|
ar-lib
|
||||||
|
autom4te.cache
|
||||||
|
test-driver
|
||||||
|
libite.pc
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
# Travis CI integration -- https://travis-ci.org/troglobit/libite
|
||||||
|
# Defaults to GNU GCC and autotools: ./configure && make && make test
|
||||||
|
language: c
|
||||||
|
|
||||||
|
# Use docker for quicker builds, it now allows https://docs.travis-ci.com/user/apt/
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
# Test build with both GCC and Clang (LLVM)
|
||||||
|
compiler:
|
||||||
|
- gcc
|
||||||
|
- clang
|
||||||
|
|
||||||
|
script:
|
||||||
|
- ./autogen.sh
|
||||||
|
- ./configure --prefix=/tmp --disable-silent-rules
|
||||||
|
- make clean
|
||||||
|
- make -j5
|
||||||
|
- make install-strip
|
||||||
|
- ls -lR /tmp
|
||||||
|
- make check
|
||||||
+392
@@ -0,0 +1,392 @@
|
|||||||
|
Change Log
|
||||||
|
==========
|
||||||
|
|
||||||
|
All notable changes to the project are documented in this file.
|
||||||
|
|
||||||
|
[v2.1.2][] - 2020-02-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix Debian package upgrade from libite -> libite5
|
||||||
|
|
||||||
|
|
||||||
|
[v2.1.1][] - 2020-02-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add test/example for `printhdr()`
|
||||||
|
- Update copyright years
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- chomp: Check for beginning of string while removing \n's
|
||||||
|
|
||||||
|
|
||||||
|
[v2.1.0][] - 2019-08-26
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add support for `strmatch()` and `strnmatch()`
|
||||||
|
- Add support for `yorn()`, safe yes-or-no prompt
|
||||||
|
- Add support for telnet expect/session APIs
|
||||||
|
- Add modified `printhdr()` which takes ANSI attribute
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix `pidfile_signal()`: always return result of `kill()`, or the
|
||||||
|
result of `remove()` if we successfully sent `SIGKILL`
|
||||||
|
- Fix `strtonum()`: POSIX `strtoll()` returns `EINVAL`, check that
|
||||||
|
- Fix #13, support C++ `extern "C" {}` for header files
|
||||||
|
|
||||||
|
|
||||||
|
[v2.0.2][] - 2018-05-10
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #12, missing includes in `strlite.h` for standalone use
|
||||||
|
- FTBFS `strtonum.c` on older systems w/o `LLONG_MAX` and `LLONG_MIN`
|
||||||
|
|
||||||
|
|
||||||
|
[v2.0.1][] - 2017-12-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add missing `initscr()` declaration, regression in [v2.0.0][]
|
||||||
|
- Add `#ifdef` guards around `min()` and `max()` macros
|
||||||
|
- Add `const` to `char *` args in multiple API:s:
|
||||||
|
- `fexist()`
|
||||||
|
- `fgetint()`
|
||||||
|
- `fisslashdir()`
|
||||||
|
- `copyfile()`
|
||||||
|
- `movefile()`
|
||||||
|
- `ifconfig()`
|
||||||
|
- `lfopen()`
|
||||||
|
- `lfgetkey()`
|
||||||
|
- `lfgetint()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix #9, uncommon location of `which` command in unit test
|
||||||
|
- Fix #10, supply unit test with a default `$PATH`
|
||||||
|
- Fix #11, `touch()` command cannot handle relative paths when updating
|
||||||
|
the mtime of an existing file
|
||||||
|
|
||||||
|
|
||||||
|
[v2.0.0][] - 2017-11-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Remove `tree()`, moved to toolbox.git instead
|
||||||
|
- Remove `fmode()`, please use `stat()` instead, it's safer
|
||||||
|
- Add support for keeping mtime in `copy()` and `rsync()`
|
||||||
|
- Move source files to an `src/` sub-directory
|
||||||
|
- Move `initscr()` from `conio.h` to `conio.c`
|
||||||
|
- Update docs, recommend `mkpath()` over `makepath()`
|
||||||
|
- Bump ABI version, major changes to library
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix #7, inconsistent return value in `mkpath()` and `makepath()`
|
||||||
|
- Fix #8, constify `mkpath()` and `makepath()`
|
||||||
|
|
||||||
|
|
||||||
|
[v1.9.3][] - 2017-10-24
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- `initscr()`, use stdout, not stderr, like the rest of the
|
||||||
|
`conio.h` macros. Also, stderr may be redirected by user
|
||||||
|
- `tree()`, update unit test, use standard directries
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `initscr()`, verify that both stdin and stdout are real TTYs
|
||||||
|
before messing up output or blocking on input for the user
|
||||||
|
- `tree()`, increase internal buffer slightly, output may be
|
||||||
|
truncated otherwise. Found by GCC 7.2
|
||||||
|
|
||||||
|
|
||||||
|
[v1.9.2][] - 2017-07-09
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add `initscr()` function to query screen size
|
||||||
|
- Convert `printheader()` to a static inline function
|
||||||
|
|
||||||
|
|
||||||
|
[v1.9.1][] - 2017-07-03
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add missing `whichp()` predicate function to accompany `which()`
|
||||||
|
- Allow `which("ps aux")`, which skips any arguments to command
|
||||||
|
|
||||||
|
|
||||||
|
[v1.9.0][] - 2017-07-02
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add `which()`, a C implementation of UNIX which(1)
|
||||||
|
- Add `printheader()`, prints a top-like table heading for client tools
|
||||||
|
- Remove `UNUSED()` macro, recommended to disable GCC warning instead
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #6: `touch()` does not update mtime if file exists
|
||||||
|
- Fix non-writable `tempfile()`, now with proper unit test
|
||||||
|
|
||||||
|
|
||||||
|
[v1.8.3][] - 2017-01-08
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Support absolute path in `pidfile()`. Needed by, e.g. Inadyn
|
||||||
|
- Add initial `debian/` packaging
|
||||||
|
- Update README with note on remembering to set `umask()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- In use-cases when `pidfile()` is called consecutively, e.g. on
|
||||||
|
`SIGHUP`, recreate PID file if it no longer exists
|
||||||
|
|
||||||
|
|
||||||
|
[v1.8.2][] - 2016-09-05
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Updated build instructions in README.
|
||||||
|
- Converted `touch()`, `makedir()`, `makefifo()`, and `erase()` from
|
||||||
|
macros to inline functions. Also removed implicit logging to stderr
|
||||||
|
on error.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix small memory leak in `pidfile()` ... at exit.
|
||||||
|
- Fixes for building on musl libc
|
||||||
|
|
||||||
|
|
||||||
|
[v1.8.1][] - 2016-06-25
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
|
||||||
|
- `fparseln()`, portability fix for NetBSD
|
||||||
|
- `pidfile()`, portability fix for NetBSD and OpenBSD
|
||||||
|
|
||||||
|
|
||||||
|
[v1.8.0][] - 2016-06-06
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Change return type of `fsendfile()` from `size_t` to `ssize_t`
|
||||||
|
- Change `dir()` semantics: do not assert and always return error
|
||||||
|
without calling `perror()` or similar.
|
||||||
|
- The OpenBSD `pidfile()` function's prefix path `_PATH_VARRUN`
|
||||||
|
can now be overridden by setting the hidden `__pidfile_path`
|
||||||
|
variable. E.g., to `_PATH_TMP`. Make sure read the docs!
|
||||||
|
- Add new unit test framework `make check`.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix bad path in new `strndupa()` header file.
|
||||||
|
- Add missing progress bar API's to `lite.h`
|
||||||
|
|
||||||
|
|
||||||
|
[v1.7.0][] - 2016-06-05
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Add support for GNU `strdupa()`, for [Inadyn][] on NetBSD.
|
||||||
|
- Add support for GNU `strndupa()` and `strnlen()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
|
||||||
|
- Fix `mkpath()` to handle creating relative directories.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.6.0][] - 2016-05-13
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Add support for NetBSD `fparaseln()` for the benefit of [Mg][]
|
||||||
|
- Add support for OpenBSD `reallocarray()` for the benefit of [Mg][]
|
||||||
|
- Constify `fisdir()` argument
|
||||||
|
|
||||||
|
|
||||||
|
[v1.5.1][] - 2016-04-17
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
Minor fix release.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Update README.md, add help when libite is used in autotools projects.
|
||||||
|
Spell checking and minor cleanup.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Activate use of `utimensat()` when `pidfile()` is called multiple
|
||||||
|
times. This reduces the overhead of calling open+write+close to
|
||||||
|
simply updating the mtime of the PID file.
|
||||||
|
- Westermo fixes for `fcopyfile()`: nothing copied on embedded PPC
|
||||||
|
system when using `tmpfile()` as source.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.5.0][] - 2016-03-20
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
This release changes the header file namespace. Apologies for any
|
||||||
|
problems this may cause!
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Change namespace for header files: `libite/` to `lite/`, e.g. using
|
||||||
|
`pkg-config` your C program must now `#include <lite/lite.h>`
|
||||||
|
- Update `pidfile()` to use `utimensat()` to update atime+mtime instead
|
||||||
|
of opening and writing PID again. Less overhead and nanonsecond res.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.4][] - 2016-02-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add `ibite.pc.in` for `pkg-config` support.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #3: `tempfile()` fails with bad `O_TMPFILE` on some embedded
|
||||||
|
targets, in particular PowerPC. On systems with older GLIBC but with
|
||||||
|
a sufficiently new kernel `O_TMPFILE` is missing and the local libite
|
||||||
|
definition was wrong, hard coded for x86. Fixed by Johan Askerin.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.3][] - 2016-02-02
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Remove symlinks to Markdown files from GIT
|
||||||
|
- Distribute and install Markdown files: README.md, etc.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.2][] - 2016-01-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix issue #2: GCC6 `-Wmisleading-indentation` causing FTBFS in pimd,
|
||||||
|
which enables `-Werror`
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.1][] - 2016-01-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Fix install path for include files, regression in v1.4.0 With the
|
||||||
|
introduction of GNU autotools include file namespace was unfortunately
|
||||||
|
lost. All include files were therefore installed in $(prefix)/include
|
||||||
|
instead of $(prefix)/include/libite.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.4.0][] - 2016-01-22
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Change to GNU configure and build system
|
||||||
|
- Add Niels Provos' splay and red-black tree implementation from OpenBSD
|
||||||
|
`sys/tree.h`, r1.14
|
||||||
|
- Make `pidfile()` file name accessible as `__pidfile_name`
|
||||||
|
- Step ABI revision to 2.1, with help from http://250bpm.com/blog:41
|
||||||
|
|
||||||
|
|
||||||
|
[v1.3.0][] - 2016-01-07
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add highly useful typeless `min()`/`max()` macros to `lite.h`,
|
||||||
|
courtesy of Tobias Waldekranz
|
||||||
|
- Add BSD `queue.h` and Borland inspired `conio.h` to `install-dev`
|
||||||
|
target. Needed by [Finit][].
|
||||||
|
- Note, this release update `queue.h` to OpenBSD v1.43, which removes
|
||||||
|
support for circular queues (CIRCLEQ).
|
||||||
|
- Update `strlcpy.c` to OpenBSD v1.12, readability fixes.
|
||||||
|
- Update `strlcat.c` to OpenBSD v1.14, readability fixes.
|
||||||
|
- Update `strtonum.c` to OpenBSD v1.7, tiniest of style tweaks.
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Only update mtime if `pidfile()` is called more than once, do not
|
||||||
|
install multiple `atexit()` handlers.
|
||||||
|
- Add `#ifndef pidfile`, as for other BSD functions, in case the user
|
||||||
|
already have a local copy, or a C library with one.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.2.0][] - 2015-11-23
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Add simple parser for UNIX configuration files, e.g. `/etc/group`,
|
||||||
|
`/etc/passwd`, and `/etc/protocols`:
|
||||||
|
- `lfopen()`
|
||||||
|
- `lfclose()`
|
||||||
|
- `lftok()`
|
||||||
|
- `lfgetkey()`
|
||||||
|
- `lfgetint()`
|
||||||
|
- `fgetint()`
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- Do not allow `VERSION` to be overloaded by build system.
|
||||||
|
- Make sure we don't inherit `LDFLAGS` from environment.
|
||||||
|
|
||||||
|
|
||||||
|
[v1.1.1][] - 2015-09-27
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
* Silence annoying warnings from GNU ar
|
||||||
|
* Fix macro definitions for compat APIs
|
||||||
|
|
||||||
|
|
||||||
|
[v1.1.0][] - 2015-09-16
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
- Support for `make distdir` used by automake based projects
|
||||||
|
- `lite.h` move from `error.h` to more BSD friendly `err.h`
|
||||||
|
- Add highly useful `NELEMS()` and bitmanip macros
|
||||||
|
- Lots of autotests added
|
||||||
|
- Add support for running autotests from Travis
|
||||||
|
- Add `progress()` simple progress bar
|
||||||
|
- Add `tree()` simple command line tree replacement
|
||||||
|
- Bump `SONAME` to `libite.so.2` on behalf of [Finit][]
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
- `pidfile()` upgrade to OpenBSD r1.11
|
||||||
|
- Lots of README updates, fixes and additions
|
||||||
|
|
||||||
|
|
||||||
|
[v1.0.0][] - 2015-07-12
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
Initial extraction of frog DNA from [Finit][]. See [README][] for API details.
|
||||||
|
|
||||||
|
|
||||||
|
[UNRELEASED]: https://github.com/troglobit/libite/compare/v2.1.2...HEAD
|
||||||
|
[v2.1.2]: https://github.com/troglobit/libite/compare/v2.1.1...v2.1.2
|
||||||
|
[v2.1.1]: https://github.com/troglobit/libite/compare/v2.1.0...v2.1.1
|
||||||
|
[v2.1.0]: https://github.com/troglobit/libite/compare/v2.0.2...v2.1.0
|
||||||
|
[v2.0.2]: https://github.com/troglobit/libite/compare/v2.0.1...v2.0.2
|
||||||
|
[v2.0.1]: https://github.com/troglobit/libite/compare/v2.0.0...v2.0.1
|
||||||
|
[v2.0.0]: https://github.com/troglobit/libite/compare/v1.9.3...v2.0.0
|
||||||
|
[v1.9.3]: https://github.com/troglobit/libite/compare/v1.9.2...v1.9.3
|
||||||
|
[v1.9.2]: https://github.com/troglobit/libite/compare/v1.9.1...v1.9.2
|
||||||
|
[v1.9.1]: https://github.com/troglobit/libite/compare/v1.9.0...v1.9.1
|
||||||
|
[v1.9.0]: https://github.com/troglobit/libite/compare/v1.8.3...v1.9.0
|
||||||
|
[v1.8.3]: https://github.com/troglobit/libite/compare/v1.8.2...v1.8.3
|
||||||
|
[v1.8.2]: https://github.com/troglobit/libite/compare/v1.8.1...v1.8.2
|
||||||
|
[v1.8.1]: https://github.com/troglobit/libite/compare/v1.8.0...v1.8.1
|
||||||
|
[v1.8.0]: https://github.com/troglobit/libite/compare/v1.7.0...v1.8.0
|
||||||
|
[v1.7.0]: https://github.com/troglobit/libite/compare/v1.6.0...v1.7.0
|
||||||
|
[v1.6.0]: https://github.com/troglobit/libite/compare/v1.5.1...v1.6.0
|
||||||
|
[v1.5.1]: https://github.com/troglobit/libite/compare/v1.5.0...v1.5.1
|
||||||
|
[v1.5.0]: https://github.com/troglobit/libite/compare/v1.4.4...v1.5.0
|
||||||
|
[v1.4.4]: https://github.com/troglobit/libite/compare/v1.4.3...v1.4.4
|
||||||
|
[v1.4.3]: https://github.com/troglobit/libite/compare/v1.4.2...v1.4.3
|
||||||
|
[v1.4.2]: https://github.com/troglobit/libite/compare/v1.4.1...v1.4.2
|
||||||
|
[v1.4.1]: https://github.com/troglobit/libite/compare/v1.4.0...v1.4.1
|
||||||
|
[v1.4.0]: https://github.com/troglobit/libite/compare/v1.3.0...v1.4.0
|
||||||
|
[v1.3.0]: https://github.com/troglobit/libite/compare/v1.2.0...v1.3.0
|
||||||
|
[v1.2.0]: https://github.com/troglobit/libite/compare/v1.1.1...v1.2.0
|
||||||
|
[v1.1.1]: https://github.com/troglobit/libite/compare/v1.1.0...v1.1.1
|
||||||
|
[v1.1.0]: https://github.com/troglobit/libite/compare/v1.0.0...v1.1.0
|
||||||
|
[v1.0.0]: https://github.com/troglobit/libite/compare/TAIL...v1.0.0
|
||||||
|
[Finit]: https://github.com/troglobit/finit/
|
||||||
|
[Inadyn]: https://github.com/troglobit/inadyn/
|
||||||
|
[Mg]: https://github.com/troglobit/mg
|
||||||
|
[README]: https://github.com/troglobit/libite/blob/v1.0.0/README.md
|
||||||
+104
@@ -0,0 +1,104 @@
|
|||||||
|
Maintenance and Release Checklist
|
||||||
|
=================================
|
||||||
|
|
||||||
|
Maintenance
|
||||||
|
-----------
|
||||||
|
|
||||||
|
* Encourage contributors to write tests, in particular for new features
|
||||||
|
* Run tests regularly, use Travis-CI to do this automatically
|
||||||
|
* Leverage GitHub issues for milestone planning
|
||||||
|
* Reference issues from GitHub pull requests to alert issue subscribers
|
||||||
|
* Bump library ABI version just before release!
|
||||||
|
|
||||||
|
|
||||||
|
Release Checklist
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
* Update ChangeLog, follow http://keepachangelog.com/ loosely
|
||||||
|
- Inform users in a plain language of changes and bug fixes
|
||||||
|
- Do *not* copy-paste GIT commit logs!
|
||||||
|
- Order entrys according to importance, most relevant first
|
||||||
|
* Run unit tests: `make check`
|
||||||
|
* Do we need to bump the ABI version? (Probably, see below)
|
||||||
|
* Tag
|
||||||
|
* Push last commit(s) *and* tags to GitHub
|
||||||
|
* Make release
|
||||||
|
|
||||||
|
make distclean
|
||||||
|
./autogen.sh
|
||||||
|
./configure
|
||||||
|
make release
|
||||||
|
|
||||||
|
* Create new release in GitHub releases page
|
||||||
|
* Copy and paste ChangeLog entry, check any stale links!
|
||||||
|
* Upload release tarball and MD5 files
|
||||||
|
|
||||||
|
|
||||||
|
Library Versioning
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Libite (-lite) relies on GNU Libtool for building the library. For a
|
||||||
|
user of the library it is important to maintain a clear ABI versioning
|
||||||
|
scheme. This is not the same as the library version, but rather the
|
||||||
|
library "compatibility level".
|
||||||
|
|
||||||
|
The ABI version is specified in `src/Makefile.am` and looks like this:
|
||||||
|
|
||||||
|
libite_la_LDFLAGS = -version-info 0:0:0
|
||||||
|
\ \ `-- age
|
||||||
|
\ `--- revision
|
||||||
|
`---- current
|
||||||
|
|
||||||
|
It must be updated according to the [GNU Libtool recommendations][1]:
|
||||||
|
|
||||||
|
1. Start with version information of `0:0:0` for each libtool library.
|
||||||
|
2. Update the version information only immediately before a public
|
||||||
|
release of your software. More frequent updates are unnecessary, and
|
||||||
|
only guarantee that the current interface number gets larger faster.
|
||||||
|
3. If the library *source code has changed at all* since the last update,
|
||||||
|
then increment revision (`c:r:a` becomes `c:r+1:a`).
|
||||||
|
4. If any *interfaces have been added, removed, or changed* since the
|
||||||
|
last update, increment current, and set revision to 0.
|
||||||
|
5. If any *interfaces have been added* since the last public release,
|
||||||
|
then increment age.
|
||||||
|
6. If any *interfaces have been removed or changed* since the last
|
||||||
|
public release, then set age to 0.
|
||||||
|
|
||||||
|
The libtool ABI versioning logic is very confusing but works if you just
|
||||||
|
disable your brain and follow the rules, one by one.
|
||||||
|
|
||||||
|
**Example #1:** a new function has been added, none of the existing ones
|
||||||
|
have changed. The initial version is 1:0:0, we follow the rules above to
|
||||||
|
the letter: increase revision, increase current and set revision to zero,
|
||||||
|
and finally increase age. This, rather confusingly, gives us 2:0:1 which
|
||||||
|
libtool then translates to `libconfuse.so.1.1.0`.
|
||||||
|
|
||||||
|
**Example #2:** some existing functions are changed, they now return an
|
||||||
|
`int` instead of `void`. The initial version is 0:0:0, and we follow the
|
||||||
|
rules again: increment revision, increment current and set revision to
|
||||||
|
zero, set age to zero. This gives us 1:0:0, which is then translated to
|
||||||
|
`libconfuse.so.1.0.0`.
|
||||||
|
|
||||||
|
### Note
|
||||||
|
|
||||||
|
Usually, non-developers have no interest in running development versions
|
||||||
|
(releases are frequent enough), and developers are expected to know how
|
||||||
|
to juggle versions. In such an ideal world, it is good enough to bump
|
||||||
|
the library version just prior to a release, point 2.
|
||||||
|
|
||||||
|
However, if releases are few and far between, distributors may start to
|
||||||
|
use snapshots. When a distributor uses a snapshot, the distributor has
|
||||||
|
to handle the library version manually. Things can get ugly when the
|
||||||
|
distributor has released an intermediate version with a bumped library
|
||||||
|
version, and when the official release is bumped to that version, the
|
||||||
|
distributor will then have to bump the library version for the official
|
||||||
|
release, and it can be confusing if someone reports bugs on versions
|
||||||
|
that you didn't even know existed.
|
||||||
|
|
||||||
|
The problem with bumping the version with every change is that if your
|
||||||
|
interface is not finished, the version number might run away, and it
|
||||||
|
looks pretty bad if a library is at version 262. It kind of tells the
|
||||||
|
user that the library interface is volatile, which is not good for
|
||||||
|
business.
|
||||||
|
|
||||||
|
[1]: https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
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-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
Copyright (c) 2008-2020 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
|
||||||
@@ -23,3 +18,9 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
Note: 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.
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
# libite -- Light-weight utility functions and C-library extensions
|
|
||||||
#
|
|
||||||
# Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in
|
|
||||||
# all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
# THE SOFTWARE.
|
|
||||||
.PHONY: all install clean
|
|
||||||
|
|
||||||
# Figure out root of library, unless used as submodule
|
|
||||||
ROOTDIR ?= $(shell pwd)
|
|
||||||
|
|
||||||
#VERSION = $(shell git tag -l | tail -1)
|
|
||||||
VERSION ?= 1.0.0
|
|
||||||
NAME = libite
|
|
||||||
PKG = $(NAME)-$(VERSION)
|
|
||||||
ARCHIVE = $(PKG).tar.xz
|
|
||||||
|
|
||||||
CC ?= $(CROSS)gcc
|
|
||||||
AR ?= $(CROSS)ar
|
|
||||||
STRIP ?= $(CROSS)strip
|
|
||||||
INSTALL := install --backup=off
|
|
||||||
STRIPINST := $(INSTALL) -s --strip-program=$(CROSS)strip -m 0755
|
|
||||||
|
|
||||||
CFLAGS += -fPIC
|
|
||||||
CFLAGS += -Os
|
|
||||||
CPPFLAGS += -D_GNU_SOURCE
|
|
||||||
CPPFLAGS += -W -Wall
|
|
||||||
ARFLAGS = crus
|
|
||||||
|
|
||||||
# Default to silent build, use V=1 to get verbose mode.
|
|
||||||
ifeq ($V,1)
|
|
||||||
Q =
|
|
||||||
PRINT = @true
|
|
||||||
MAKEFLAGS =
|
|
||||||
else
|
|
||||||
Q = @
|
|
||||||
PRINT = @printf
|
|
||||||
MAKEFLAGS = --no-print-directory --silent
|
|
||||||
endif
|
|
||||||
|
|
||||||
DISTFILES = README LICENSE
|
|
||||||
HEADERS = lite.h
|
|
||||||
OBJS := chomp.o copyfile.o dir.o fexist.o fisdir.o fmode.o fsendfile.o
|
|
||||||
OBJS += ifconfig.o makepath.o pidfile.o pidfilefn.o rsync.o
|
|
||||||
OBJS += strlcpy.o strlcat.o strtonum.o tempfile.o
|
|
||||||
DEPS := $(OBJS:.o=.d)
|
|
||||||
JUNK = *~ *.bak *.map .*.d *.d DEADJOE semantic.cache *.gdb *.elf core core.*
|
|
||||||
|
|
||||||
VER = 1
|
|
||||||
LIBNAME = $(NAME)
|
|
||||||
SOLIB = $(LIBNAME).so.$(VER)
|
|
||||||
SYMLIB = $(LIBNAME).so
|
|
||||||
STATICLIB = $(LIBNAME).a
|
|
||||||
TARGET = $(STATICLIB) $(SOLIB)
|
|
||||||
|
|
||||||
# Default install paths
|
|
||||||
prefix ?= /usr/local
|
|
||||||
libdir ?= $(prefix)/lib
|
|
||||||
datadir ?= $(prefix)/share/doc/$(LIBNAME)
|
|
||||||
incdir ?= $(prefix)/include
|
|
||||||
|
|
||||||
# This magic trick looks like a comment, but works on BSD PMake
|
|
||||||
#include <rules.mk>
|
|
||||||
include rules.mk
|
|
||||||
|
|
||||||
# Build rules
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
$(OBJS): Makefile
|
|
||||||
|
|
||||||
$(SOLIB): Makefile $(OBJS)
|
|
||||||
$(PRINT) " LINK %s\n" $@
|
|
||||||
$(Q)$(CC) $(LDFLAGS) -shared -Wl,-soname,$@ -o $@ $(OBJS)
|
|
||||||
|
|
||||||
$(STATICLIB): Makefile $(OBJS)
|
|
||||||
$(Q)$(PRINT) " ARCHIVE $@\n"
|
|
||||||
$(Q)$(AR) $(ARFLAGS) $@ $(OBJS)
|
|
||||||
|
|
||||||
install-exec: all
|
|
||||||
$(PRINT) " INSTALL $(DESTDIR)$(libdir)/$(SOLIB)\n"
|
|
||||||
$(Q)install -d $(DESTDIR)$(libdir)
|
|
||||||
$(Q)$(STRIPINST) $(SOLIB) $(DESTDIR)$(libdir)/$(SOLIB)
|
|
||||||
$(Q)ln -sf $(SOLIB) $(DESTDIR)$(libdir)/$(SYMLIB)
|
|
||||||
|
|
||||||
install-dev:
|
|
||||||
$(Q)install -d $(DESTDIR)$(incdir)/$(LIBNAME)
|
|
||||||
$(Q)for file in $(HEADERS); do \
|
|
||||||
printf " INSTALL $(DESTDIR)$(incdir)/$(LIBNAME)/$$file\n"; \
|
|
||||||
$(INSTALL) -m 0644 $$file $(DESTDIR)$(incdir)/$(LIBNAME)/$$file; \
|
|
||||||
done
|
|
||||||
$(PRINT) " INSTALL $(DESTDIR)$(libdir)/$(STATICLIB)\n"
|
|
||||||
$(Q)install -d $(DESTDIR)$(libdir)
|
|
||||||
$(Q)$(INSTALL) $(STATICLIB) $(DESTDIR)$(libdir)/$(STATICLIB)
|
|
||||||
$(Q)install -d $(DESTDIR)$(datadir)
|
|
||||||
$(Q)for file in $(DISTFILES); do \
|
|
||||||
printf " INSTALL $(DESTDIR)$(datadir)/$$file\n"; \
|
|
||||||
install -m 0644 $$file $(DESTDIR)$(datadir)/$$file; \
|
|
||||||
done
|
|
||||||
|
|
||||||
install: install-exec install-dev
|
|
||||||
|
|
||||||
uninstall:
|
|
||||||
-$(Q)$(RM) -r $(DESTDIR)$(datadir)
|
|
||||||
-$(Q)$(RM) -r $(DESTDIR)$(incdir)/$(LIBNAME)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(SOLIB)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(SYMLIB)
|
|
||||||
-$(Q)$(RM) $(DESTDIR)$(libdir)/$(STATICLIB)
|
|
||||||
|
|
||||||
strip: $(TARGET)
|
|
||||||
$(Q)$(PRINT) " STRIP %s\n" $(TARGET)
|
|
||||||
$(Q)$(STRIP) --strip-unneeded $(TARGET)
|
|
||||||
$(Q)size $(TARGET)
|
|
||||||
|
|
||||||
# Runs Clang scan-build on the whole tree
|
|
||||||
check: clean
|
|
||||||
$(Q)scan-build $(MAKE) all
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-$(Q)$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
|
||||||
|
|
||||||
distclean:
|
|
||||||
-$(Q)$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest
|
|
||||||
|
|
||||||
dist:
|
|
||||||
$(Q)echo "Building .xz tarball of $(PKG) in parent dir..."
|
|
||||||
git archive --format=tar --prefix=$(PKG)/ v$(VERSION) | xz >../$(ARCHIVE)
|
|
||||||
$(Q)(cd ..; md5sum $(ARCHIVE) | tee $(ARCHIVE).md5)
|
|
||||||
|
|
||||||
# Include automatically generated rules
|
|
||||||
-include $(DEPS)
|
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
ACLOCAL_AMFLAGS = -I m4
|
||||||
|
|
||||||
|
doc_DATA = README.md LICENSE ChangeLog.md
|
||||||
|
EXTRA_DIST = README.md LICENSE ChangeLog.md
|
||||||
|
pkgconfigdir = $(libdir)/pkgconfig
|
||||||
|
pkgconfig_DATA = libite.pc
|
||||||
|
SUBDIRS = src tests
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
# lintian --profile debian -i -I --show-overrides ../$PKG.changes
|
||||||
|
package build-deb:
|
||||||
|
@dpkg-buildpackage -uc -us -B
|
||||||
|
|
||||||
|
## Target to run when building a release
|
||||||
|
release: release-hook distcheck package md5-dist
|
||||||
|
@mv $(DIST_ARCHIVES) ../
|
||||||
|
@echo
|
||||||
|
@echo "Resulting release files:"
|
||||||
|
@echo "================================================================="
|
||||||
|
@for file in $(DIST_ARCHIVES); do \
|
||||||
|
printf "%-32s Distribution tarball\n" $$file; \
|
||||||
|
printf "%-32s " $$file.md5; cat ../$$file.md5 | cut -f1 -d' '; \
|
||||||
|
done
|
||||||
|
@for file in `cd ..; ls $(PACKAGE)*_$(VERSION)*`; do \
|
||||||
|
printf "%-32s Debian/Ubuntu package\n" $$file; \
|
||||||
|
done
|
||||||
@@ -1,24 +1,78 @@
|
|||||||
-lite | Frog DNA, basically
|
-lite | Frog DNA, basically
|
||||||
===========================
|
===========================
|
||||||
|
[![Travis Status][]][Travis]
|
||||||
|
|
||||||
Libite is a lightweight library of *frog DNA*. It can be used to fill
|
|
||||||
|
Table of Contents
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
* [Introduction](#introduction)
|
||||||
|
* [Using -lite](#using--lite)
|
||||||
|
* [Helper Macros](#helper-macros)
|
||||||
|
* [Generic Functions](#generic-functions)
|
||||||
|
* [OpenBSD Functions](#openbsd-functions)
|
||||||
|
* [Build & Install](#origin--references)
|
||||||
|
* [TODO](#todo)
|
||||||
|
|
||||||
|
|
||||||
|
> **NOTE:** Incompatible changes in v2.0 compared to v1.x!
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
------------
|
||||||
|
|
||||||
|
Libite is a lightweight library of *frog DNA* that can be used to fill
|
||||||
the gaps in any dinosaur project. It holds useful functions and macros
|
the gaps in any dinosaur project. It holds useful functions and macros
|
||||||
developed by both [Finit][1] and the [OpenBSD][2] project. Most notably
|
developed by both [Finit][1] and the [OpenBSD][2] project. Most notably
|
||||||
the string functions: [strlcpy(3)][3], [strlcat(3)][3] and the highly
|
the string functions: [strlcpy(3)][3], [strlcat(3)][3] and the highly
|
||||||
useful *BSD [sys/queue.h][4] API.
|
useful *BSD [sys/queue.h][4] and [sys/tree.h][7] API's.
|
||||||
|
|
||||||
Libite aims to fill in the gaps missing in GLIBC/EGLIBC. (It does not
|
Libite holds many of the missing pieces in GNU libc, although -lite does
|
||||||
aimo to become another [GLIB][5] though.) One such gap in GLIBC is the
|
not aim to become another [GLIB][5]. One noticeable gap in GLIBC is the
|
||||||
missing `_SAFE` macros in `sys/queue.h` — highly recommended when
|
`_SAFE` macros available in the BSD `sys/queue.h` API — highly
|
||||||
traversing lists to delete/free nodes.
|
recommended when traversing lists to delete/free nodes.
|
||||||
|
|
||||||
The code is open sourced under a mix of the [MIT/X11 license][MIT], the
|
The code is open sourced under a mix of permissive Open Source licenses:
|
||||||
[ISC license][ISC] used by OpenBSD, and [BSD licenses][BSD], all of them
|
[MIT/X11 license][MIT], [ISC license][ISC], and [BSD licenses][BSD].
|
||||||
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].
|
Using -lite
|
||||||
|
-----------
|
||||||
|
|
||||||
|
Libite is by default installed as a library and a set of include files.
|
||||||
|
To prevent clashing with include files of the same name `-lite` employs
|
||||||
|
an include file namespace `lite/`, which is strongly recommended to use
|
||||||
|
in your applications:
|
||||||
|
|
||||||
|
#include <lite/lite.h>
|
||||||
|
#include <lite/conio.h>
|
||||||
|
#include <lite/queue.h>
|
||||||
|
#include <lite/tree.h>
|
||||||
|
|
||||||
|
The output from the `pkg-config` tool holds no surprises in this regard:
|
||||||
|
|
||||||
|
$ pkg-config --libs --static --cflags libite
|
||||||
|
-I/usr/local/include -L/usr/local/lib -lite
|
||||||
|
|
||||||
|
The prefix path `/usr/local/` shown here is only the default. Use the
|
||||||
|
`configure` script to select a different prefix when installing libite.
|
||||||
|
|
||||||
|
For GNU autotools based projects, use the following in `configure.ac`:
|
||||||
|
|
||||||
|
# Check for required libraries
|
||||||
|
PKG_CHECK_MODULES([lite], [libite >= 1.5.0])
|
||||||
|
|
||||||
|
and in your `Makefile.am`:
|
||||||
|
|
||||||
|
proggy_CFLAGS = $(lite_CFLAGS)
|
||||||
|
proggy_LDADD = $(lite_LIBS)
|
||||||
|
|
||||||
|
|
||||||
|
Important Note
|
||||||
|
--------------
|
||||||
|
|
||||||
|
When using functions like `mkpath()`, `makepath()`, and `makefifo()`,
|
||||||
|
make sure your `umask()` is correct so you do not accidentally create
|
||||||
|
files and directories accessible to other users than intended.
|
||||||
|
|
||||||
|
|
||||||
Helper Macros
|
Helper Macros
|
||||||
@@ -31,35 +85,60 @@ Helper Macros
|
|||||||
|
|
||||||
- `blkdev(dev)`
|
- `blkdev(dev)`
|
||||||
|
|
||||||
Create block device
|
Create block device
|
||||||
|
|
||||||
- `chardev(dev)`
|
- `chardev(dev)`
|
||||||
|
|
||||||
Create character device
|
Create character device
|
||||||
|
|
||||||
- `erase(path)`
|
- `erase(path)`
|
||||||
|
|
||||||
Erase file/directory with `remove()`. Errors on stderr
|
Erase file/directory with `remove()`. Errors on `stderr`
|
||||||
|
|
||||||
- `makedir(path)`
|
- `makedir(path)`
|
||||||
|
|
||||||
Create directory, like `mkdir()`. Errors on stderr
|
Create directory, like `mkdir()`. Errors on `stderr`
|
||||||
|
|
||||||
- `makefifo(path)`
|
- `makefifo(path)`
|
||||||
|
|
||||||
Create a FIFO, like `mkfifo()`. Errors on stderr
|
Create a FIFO, like `mkfifo()`. Errors on `stderr`
|
||||||
|
|
||||||
|
- `min(a,b)`/`max(a,b)`
|
||||||
|
|
||||||
|
These macros take care to avoid double evaluation.
|
||||||
|
|
||||||
- `touch(path)`
|
- `touch(path)`
|
||||||
|
|
||||||
Create a file, or update mtime. Errors on stderr
|
Create a file, or update mtime. Errors on `stderr`
|
||||||
|
|
||||||
- `S_ISEXEC(mode_t m)`
|
- `S_ISEXEC(mode_t m)`
|
||||||
|
|
||||||
Mysteriously missing from GLIBC
|
Mysteriously missing from GLIBC
|
||||||
|
|
||||||
- `UNUSED(var)`
|
- `ISCLR(word,bit)`
|
||||||
|
|
||||||
Shorter and more readable version of `var __attribute__ ((unused))`
|
Is bit in (integer) word cleared?
|
||||||
|
|
||||||
|
- `ISSET(word,bit)`
|
||||||
|
|
||||||
|
Is bit in (integer) word set?
|
||||||
|
|
||||||
|
- `ISOTHER(word,bit)`
|
||||||
|
|
||||||
|
Are 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.
|
||||||
|
|
||||||
|
|
||||||
Generic Functions
|
Generic Functions
|
||||||
@@ -69,15 +148,25 @@ Generic Functions
|
|||||||
|
|
||||||
Perl like chomp function, chop off last char if newline.
|
Perl like chomp function, chop off last char if newline.
|
||||||
|
|
||||||
- `copyfile(src, dst, len, symlink)`
|
- `copyfile(src, dst, len, opt)`
|
||||||
|
|
||||||
Like the shell `cp(1)` and `dd(1),` can also create symlinks.
|
Like the shell `cp(1)` and `dd(1),`, can also copy symlinks and
|
||||||
|
preserve the mtime of the source file. The opt argument can be
|
||||||
|
a mask of:
|
||||||
|
|
||||||
|
- `LITE_FOPT_COPYFILE_SYM (0x01)`
|
||||||
|
- `LITE_FOPT_KEEP_MTIME (0x02)`
|
||||||
|
|
||||||
|
In releases prior to v2.0 the opt argument was called `symlink`.
|
||||||
|
The APIs are 100% compatible if the value `1` was used to enable
|
||||||
|
the symlink option.
|
||||||
|
|
||||||
- `dir(dir, ext, filter, list, strip)`
|
- `dir(dir, ext, filter, list, strip)`
|
||||||
|
|
||||||
Wrapper for `scandir()` with optional filter. Returns a list of
|
Wrapper for `scandir()` with optional filter. Returns a list of
|
||||||
names: files and directories that must be freed after use. See
|
names: files and directories that must be freed after use. See
|
||||||
the unit test at the bottom for an example.
|
the unit test for an example, or take a look at `glob(3)`, it's
|
||||||
|
probably what you want anyway.
|
||||||
|
|
||||||
- `fcopyfile(src, dst)`
|
- `fcopyfile(src, dst)`
|
||||||
|
|
||||||
@@ -86,15 +175,11 @@ Generic Functions
|
|||||||
|
|
||||||
- `fexist(file)`
|
- `fexist(file)`
|
||||||
|
|
||||||
Check for the existance of a file, returns True(1) or False(0).
|
Check for the existence of a file, returns True(1) or False(0).
|
||||||
|
|
||||||
- `fisdir(path)`
|
- `fisdir(path)`
|
||||||
|
|
||||||
Check for the existance of a directory, returns True(1) or False(0).
|
Check for the existence of a directory, returns True(1) or False(0).
|
||||||
|
|
||||||
- `fmode(file)`
|
|
||||||
|
|
||||||
Returns the `mode_t` bits of a file or directory.
|
|
||||||
|
|
||||||
- `fsendfile(src, dst, len)`
|
- `fsendfile(src, dst, len)`
|
||||||
|
|
||||||
@@ -105,7 +190,30 @@ Generic Functions
|
|||||||
- `ifconfig(ifname, addr, mask, up)`
|
- `ifconfig(ifname, addr, mask, up)`
|
||||||
|
|
||||||
Basic ifconfig like operations on an interface. Only supports IPv4
|
Basic ifconfig like operations on an interface. Only supports IPv4
|
||||||
adresses. Note that mask is not CIDR notation.
|
addresses. Note that mask is not CIDR notation.
|
||||||
|
|
||||||
|
- `lfopen(file, sep)`, `lfclose(lf)`
|
||||||
|
|
||||||
|
LITE file API for parsing UNIX style configuration files like
|
||||||
|
`/etc/protocols` and `/etc/services`.
|
||||||
|
|
||||||
|
- `lftok(lf)`
|
||||||
|
|
||||||
|
Read tokens, delimited 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)`
|
- `makepath(dir)`
|
||||||
|
|
||||||
@@ -121,23 +229,31 @@ Generic Functions
|
|||||||
with the `dst` name. On successful operation the source is removed
|
with the `dst` name. On successful operation the source is removed
|
||||||
and the function returns POSIX OK (0).
|
and the function returns POSIX OK (0).
|
||||||
|
|
||||||
- `pidfile(basename)`
|
- `pidfile(name)`
|
||||||
|
|
||||||
Write a daemon pid file. Creates a pidfile in `_PATH_VARRUN` using
|
Create a daemon PID file using either the `name` as a full path, if
|
||||||
either `basename` or, if `basename` is `NULL`, `__progname`. The
|
`name` starts with `/`, or in `_PATH_VARRUN` using `name` as the
|
||||||
file name has the form `/var/run/basename.pid`.
|
basename of the application. If `name` is `NULL`, then `__progname`
|
||||||
|
is used as the basename. The resulting file name is available to the
|
||||||
|
user as a read-only pointer:
|
||||||
|
|
||||||
Use this function to create a pid file for your daemon when it is
|
extern char *__pidfile_name;
|
||||||
|
|
||||||
|
Use this function to create a PID file for your daemon when it is
|
||||||
ready to receive signals. A client application may poll for the
|
ready to receive signals. A client application may poll for the
|
||||||
existence of this file, so make sure to have your signal handlers
|
existence of this file, so make sure to have your signal handlers
|
||||||
properly setup before calling this function.
|
properly setup before calling this function.
|
||||||
|
|
||||||
The pidfile is removed when the program exits, using an `atexit()`
|
|
||||||
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.
|
|
||||||
|
|
||||||
See below for link to OpenBSD man page.
|
The PID file 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.
|
||||||
|
|
||||||
|
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. If the file is removed, subsequent
|
||||||
|
calls to this function will recreate the file.
|
||||||
|
|
||||||
|
See below for a link to OpenBSD man page.
|
||||||
|
|
||||||
- `pidfile_read(pidfile)`
|
- `pidfile_read(pidfile)`
|
||||||
|
|
||||||
@@ -147,27 +263,112 @@ Generic Functions
|
|||||||
|
|
||||||
Send signal to PID found in pid file created by `pidfile()`.
|
Send signal to PID found in pid file created by `pidfile()`.
|
||||||
|
|
||||||
- `rsync(src, dst, delete, *filter())`
|
- `progress(percent, max_width)`
|
||||||
|
|
||||||
Very simple `rsync()` to copy files files and directories
|
Simple ASCII progress bar with a spinner. Start it with `percent=0`
|
||||||
recursively.
|
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, opt, *filter())`
|
||||||
|
|
||||||
|
Very simple `rsync()` to copy files and directories recursively. It
|
||||||
|
supports pruning files from the destination tree that do not exist in
|
||||||
|
the source tree and preserving the mtime of the source files. The opt
|
||||||
|
argument can be a mask of:
|
||||||
|
|
||||||
|
- `LITE_FOPT_RSYNC_DELETE (0x01)`
|
||||||
|
- `LITE_FOPT_KEEP_MTIME (0x02)`
|
||||||
|
|
||||||
|
In releases prior to v2.0 the argument controlling pruning was called
|
||||||
|
`delete`, it is now called `opt`. The APIs are 100% compatible if the
|
||||||
|
value `1` was used.
|
||||||
|
|
||||||
|
- `strmatch()`, `strnmatch()`
|
||||||
|
|
||||||
|
Find matching string in an array of strings. Returns index in array
|
||||||
|
on match, or `-1` on error or not found.
|
||||||
|
|
||||||
|
- `telnet_open(), telnet_close(), telnet_expect()`
|
||||||
|
|
||||||
|
Poor mans telnet expect in C. Opens connection to a Telnet service;
|
||||||
|
FTP, Telnet, similar, and run an expect-like script.
|
||||||
|
|
||||||
|
- `telnet_session()`
|
||||||
|
|
||||||
|
Wrapper for the above three in one API.
|
||||||
|
|
||||||
- `tempfile()`
|
- `tempfile()`
|
||||||
|
|
||||||
Secure replacement for `tmpfile()`. Creates an invisible temporary
|
Secure replacement for `tmpfile()`. Creates an invisible temporary
|
||||||
file in `/tmp` that is removed when the returned `FILE` pointer is
|
file in `/tmp` that is removed when the returned `FILE` pointer is
|
||||||
closed. **Note:** Requires Linux v3.11, or later.
|
closed.
|
||||||
|
|
||||||
|
**Note:** Requires Linux v3.11, or later, will fall back to the old
|
||||||
|
and unsafe `tmpfile()` on older systems.
|
||||||
|
|
||||||
|
- `which(cmd)`, `whichp(cmd)`
|
||||||
|
|
||||||
|
C implementation of UNIX which(1). Returns a malloc'ed string with the
|
||||||
|
full path to `cmd` on success, otherwise `NULL`.
|
||||||
|
|
||||||
|
`whichp()` is a predicate function, returns `TRUE` or `FALSE`.
|
||||||
|
|
||||||
|
**Note:** `which("/bin/ps aux")` will return `/bin/ps`, or `TRUE`,
|
||||||
|
provided of course `/bin/ps` exists.
|
||||||
|
|
||||||
|
- `yorn(fmt, ...)`
|
||||||
|
|
||||||
|
Pose a question to user, appended with `(y/N)?`, returns `TRUE` for
|
||||||
|
yes (both `y` and `Y` are handled) and `FALSE` for everything else.
|
||||||
|
|
||||||
|
|
||||||
OpenBSD Functions
|
GNU Functions
|
||||||
-----------------
|
-------------
|
||||||
|
|
||||||
The following are the popular OpenBSD string functions.
|
The following are useful GNU functions, that do not exist on *BSD, and
|
||||||
|
some other platforms.
|
||||||
|
|
||||||
|
- `strdupa(str)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strdupa.3.html
|
||||||
|
|
||||||
|
- `strndupa(str)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strndupa.3.html
|
||||||
|
|
||||||
|
- `strnlen(str, lim)`
|
||||||
|
|
||||||
|
http://man7.org/linux/man-pages/man3/strnlen.3.html
|
||||||
|
|
||||||
|
|
||||||
|
OpenBSD/NetBSD/FreeBSD Functions
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
The following are popular functions and highly useful macros from the
|
||||||
|
*BSD world of UNIX that are sadly missing on Linux.
|
||||||
|
|
||||||
|
- `fparseln(fp, *len, *lineno, delim, flags)`
|
||||||
|
|
||||||
|
http://man.openbsd.org/OpenBSD-current/man3/fparseln.3
|
||||||
|
|
||||||
- `pidfile(basename)`
|
- `pidfile(basename)`
|
||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/pidfile.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/pidfile.3
|
||||||
|
|
||||||
|
**Note:** the version of `pidfile()` in -lite has been extended to
|
||||||
|
handle it being called multiple times, i.e. to not create multiple
|
||||||
|
`atexit()` handlers, and also to export a few internal paths:
|
||||||
|
|
||||||
|
- `__pidfile_path`: prefix path, default `_PATH_VARRUN`. Notice the
|
||||||
|
trailing slash requirement if you want to override it!
|
||||||
|
- `__pidfile_name`: full path to PID file , similar to `__progname`.
|
||||||
|
See previous section for details.
|
||||||
|
|
||||||
|
- `reallocarray(ptr, nmemb, size)`
|
||||||
|
|
||||||
|
http://man.openbsd.org/OpenBSD-current/man3/reallocarray.3
|
||||||
|
|
||||||
- `strlcpy(dst, src, len)`
|
- `strlcpy(dst, src, len)`
|
||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3
|
||||||
@@ -184,26 +385,37 @@ The following are the popular OpenBSD string functions.
|
|||||||
|
|
||||||
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
||||||
|
|
||||||
|
- `sys/tree.h` API
|
||||||
|
|
||||||
TODO
|
Niels Provos' famous splay and red-black tree implementation.
|
||||||
----
|
|
||||||
|
http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/SPLAY_FOREACH.3
|
||||||
|
|
||||||
|
|
||||||
|
Build & Install
|
||||||
|
---------------
|
||||||
|
|
||||||
|
This library was initially built for and developed on GNU/Linux systems
|
||||||
|
as a light weight utility library, these days NetBSD should also work.
|
||||||
|
|
||||||
|
./configure
|
||||||
|
make -j5
|
||||||
|
sudo make install-strip
|
||||||
|
sudo ldconfig
|
||||||
|
|
||||||
|
**Note:** When checking out code from GIT, use <kbd>./autogen.sh</kbd>
|
||||||
|
to generate a `configure` script. It is a generated file and otherwise
|
||||||
|
only included in released tarballs.
|
||||||
|
|
||||||
- Improve documentation, possibly use kdoc or gdoc2 to generate docs from API.
|
|
||||||
- Continuously, update OpenBSD functions/macros.
|
|
||||||
|
|
||||||
[1]: https://github.com/troglobit/finit
|
[1]: https://github.com/troglobit/finit
|
||||||
[2]: http://www.openbsd.org/
|
[2]: http://www.openbsd.org/
|
||||||
[3]: http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy
|
[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
|
[4]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/LIST_EMPTY.3
|
||||||
[5]: https://developer.gnome.org/glib/
|
[5]: https://developer.gnome.org/glib/
|
||||||
[6]: http://troglobit.com/blog/2015/07/02/howto-using-lite-with-a-git-based-application/
|
[7]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/SPLAY_FOREACH.3
|
||||||
[MIT]: https://en.wikipedia.org/wiki/MIT_License
|
[MIT]: https://en.wikipedia.org/wiki/MIT_License
|
||||||
[ISC]: https://en.wikipedia.org/wiki/ISC_license
|
[ISC]: https://en.wikipedia.org/wiki/ISC_license
|
||||||
[BSD]: https://en.wikipedia.org/wiki/BSD_licenses
|
[BSD]: https://en.wikipedia.org/wiki/BSD_licenses
|
||||||
|
[Travis]: https://travis-ci.org/troglobit/libite
|
||||||
|
[Travis Status]: https://travis-ci.org/troglobit/libite.png?branch=master
|
||||||
<!--
|
|
||||||
-- Local Variables:
|
|
||||||
-- mode: markdown
|
|
||||||
-- End:
|
|
||||||
-->
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
TODO ... in no particular order
|
||||||
|
===============================
|
||||||
|
|
||||||
|
* Improve documentation, possibly use kdoc or gdoc2 to generate docs from API
|
||||||
|
* Continuously, update OpenBSD functions/macros
|
||||||
|
* Investigate adding FreeBSD pidfile_open() et al
|
||||||
|
* Investigate FreeBSD libutil library, could be useful for porting between unices
|
||||||
|
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
autoreconf -W portability -visfm
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
AC_INIT(libite, 2.1.2, https://github.com/troglobit/libite/issues)
|
||||||
|
AC_CONFIG_AUX_DIR(aux)
|
||||||
|
AM_INIT_AUTOMAKE([1.11 foreign no-dist-gzip dist-xz])
|
||||||
|
AM_SILENT_RULES([yes])
|
||||||
|
|
||||||
|
AC_CONFIG_SRCDIR(src/lite.h)
|
||||||
|
AM_CONFIG_HEADER(config.h)
|
||||||
|
AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile libite.pc])
|
||||||
|
AC_CONFIG_MACRO_DIR([m4])
|
||||||
|
|
||||||
|
AC_PROG_CC
|
||||||
|
AC_PROG_INSTALL
|
||||||
|
AM_PROG_AR
|
||||||
|
LT_INIT
|
||||||
|
|
||||||
|
AC_OUTPUT
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
*.log
|
||||||
|
*.substvars
|
||||||
|
.debhelper/*
|
||||||
|
tmp/*
|
||||||
|
libite5/*
|
||||||
|
libite-dev/*
|
||||||
|
autoreconf*
|
||||||
|
debhelper*
|
||||||
|
files
|
||||||
|
!*.dirs
|
||||||
|
!*.install
|
||||||
Vendored
+88
@@ -0,0 +1,88 @@
|
|||||||
|
libite (2.1.2) stable; urgency=high
|
||||||
|
|
||||||
|
* Fix upgrade of base package, new library name to match SONAME
|
||||||
|
breaks upgrade from libite_2.0.x.deb to libite5_2.1.x.deb
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sat, 22 Feb 2020 15:04:27 +0100
|
||||||
|
|
||||||
|
libite (2.1.1) stable; urgency=medium
|
||||||
|
|
||||||
|
* Fix to chomp(), ensure stopping before beginning of string
|
||||||
|
* Added example of printhdr()
|
||||||
|
* Update copyright years
|
||||||
|
* Add missing libite5.symbols file, found by lintian
|
||||||
|
* Fix package short description, found by linitian
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sat, 22 Feb 2020 13:52:24 +0100
|
||||||
|
|
||||||
|
libite (2.1.0) unstable; urgency=medium
|
||||||
|
|
||||||
|
* New functions; strmatch(), strnmatch(), yorn(), telnet expect APIs
|
||||||
|
* Fixes to pidfile_signal(), strtonum() and C++ linking
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Mon, 26 Aug 2019 20:21:46 +0200
|
||||||
|
|
||||||
|
libite (2.0.2) unstable; urgency=medium
|
||||||
|
|
||||||
|
* Fix issues with using `strlite.h` standalone
|
||||||
|
* Fix build issue with `strtonum.c` older GLIBC
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Thu, 10 May 2018 19:12:57 +0200
|
||||||
|
|
||||||
|
libite (2.0.1-1) unstable; urgency=medium
|
||||||
|
|
||||||
|
* New upstream release, v2.0.1, fixes to:
|
||||||
|
- touch(), cannot handle relative paths when updating mtime
|
||||||
|
- unit test for which()
|
||||||
|
- add missing initscr() declaration in conio.h
|
||||||
|
- ifdef guards around min()/max() macros
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Fri, 22 Dec 2017 12:11:41 +0100
|
||||||
|
|
||||||
|
libite (2.0.0-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream release, v2.0.0, fixes to:
|
||||||
|
- makepath(), mkpath()
|
||||||
|
* Changes to
|
||||||
|
- rsync(), API changes
|
||||||
|
- copyfile(), API changes
|
||||||
|
- tree(), removed from library
|
||||||
|
- fmode(), removed from library
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sun, 19 Nov 2017 22:42:51 +0100
|
||||||
|
|
||||||
|
libite (1.9.3-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream release, v1.9.3, fixes to conio.h, initscr() and tree().
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Tue, 24 Oct 2017 08:49:54 +0200
|
||||||
|
|
||||||
|
libite (1.9.2-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream reelase, v1.9.2, see upstream ChangeLog for details.
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Tue, 15 Aug 2017 08:15:14 +0200
|
||||||
|
|
||||||
|
libite (1.9.1-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream reelase, v1.9.1.
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sun, 02 Jul 2017 19:23:00 +0100
|
||||||
|
|
||||||
|
libite (1.9.0-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream reelase.
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sun, 02 Jul 2017 19:23:00 +0100
|
||||||
|
|
||||||
|
libite (1.8.3-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* New upstream reelase.
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Sun, 08 Jan 2017 15:22:26 +0100
|
||||||
|
|
||||||
|
libite (1.8.2-1) unstable; urgency=low
|
||||||
|
|
||||||
|
* Initial release.
|
||||||
|
|
||||||
|
-- Joachim Nilsson <troglobit@gmail.com> Tue, 29 Nov 2016 08:23:35 +0100
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
config.log
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
10
|
||||||
Vendored
+55
@@ -0,0 +1,55 @@
|
|||||||
|
Source: libite
|
||||||
|
Priority: optional
|
||||||
|
Maintainer: Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
Build-Depends: debhelper (>= 10)
|
||||||
|
Standards-Version: 4.3.0
|
||||||
|
Section: libs
|
||||||
|
Homepage: https://github.com/troglobit/libite/
|
||||||
|
Vcs-Git: https://github.com/troglobit/libite.git
|
||||||
|
Vcs-Browser: https://github.com/troglobit/libite/
|
||||||
|
|
||||||
|
Package: libite-dev
|
||||||
|
Priority: optional
|
||||||
|
Section: libdevel
|
||||||
|
Architecture: any
|
||||||
|
Depends: ${misc:Depends}, libite5 (= ${binary:Version})
|
||||||
|
Description: static library, header files, and docs for libite
|
||||||
|
Static library, header files, and documentation for libite.
|
||||||
|
.
|
||||||
|
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 and the OpenBSD project. Most notably the
|
||||||
|
string functions: strlcpy(3), strlcat(3) and the highly useful *BSD
|
||||||
|
sys/queue.h and sys/tree.h API's.
|
||||||
|
.
|
||||||
|
Libite is the frog DNA missing in GNU libc. However, -lite does not
|
||||||
|
aim to become another GLIB! One noticeable gap in GLIBC is the missing
|
||||||
|
_SAFE macros in the BSD sys/queue.h API — highly recommended when
|
||||||
|
traversing lists to delete/free nodes.
|
||||||
|
.
|
||||||
|
The code is open sourced under a mix of the MIT/X11 license, the ISC
|
||||||
|
license used by OpenBSD, and BSD licenses, all of them are extremely
|
||||||
|
liberal and can be used freely in proprietary software if needed.
|
||||||
|
|
||||||
|
Package: libite5
|
||||||
|
Replaces: libite
|
||||||
|
Conflicts: libite
|
||||||
|
Provides: libite
|
||||||
|
Architecture: any
|
||||||
|
Depends: ${misc:Depends}, ${shlibs:Depends}
|
||||||
|
Description: That missing frog DNA you've been looking for
|
||||||
|
Libite, or -lite, 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 and the OpenBSD project. Most
|
||||||
|
notably the string functions: strlcpy(3), strlcat(3) and the highly
|
||||||
|
useful *BSD sys/queue.h and sys/tree.h API's.
|
||||||
|
.
|
||||||
|
Libite is the frog DNA missing in GNU libc. However, -lite does not
|
||||||
|
aim to become another GLIB! One noticeable gap in GLIBC is the missing
|
||||||
|
_SAFE macros in the BSD sys/queue.h API — highly recommended when
|
||||||
|
traversing lists to delete/free nodes.
|
||||||
|
.
|
||||||
|
The code is open source software under a mix of the MIT/X11 license,
|
||||||
|
the ISC license used by OpenBSD, and BSD licenses, all of them are
|
||||||
|
extremely liberal and can be used freely in proprietary software if
|
||||||
|
needed.
|
||||||
Vendored
+96
@@ -0,0 +1,96 @@
|
|||||||
|
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
|
||||||
|
Source: https.//github.com/troglobit/libite/
|
||||||
|
|
||||||
|
Files: *
|
||||||
|
Copyright:
|
||||||
|
2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
|
2008-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: MIT
|
||||||
|
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.
|
||||||
|
|
||||||
|
Files: chomp.c
|
||||||
|
Copyright: 2014-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: conio.h
|
||||||
|
Copyright: 2009-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: dir.c
|
||||||
|
Copyright: 2008-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: lfile.c
|
||||||
|
Copyright: 2015-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: pidfile.c
|
||||||
|
Copyright: 1999 The NetBSD Foundation, Inc.
|
||||||
|
License: BSD-2-clause
|
||||||
|
|
||||||
|
Files: queue.h
|
||||||
|
Copyright: 1991, 1993 The Regents of the University of California.
|
||||||
|
License: BSD-3-clause
|
||||||
|
|
||||||
|
Files: reallocarray.c
|
||||||
|
Copyright: 2008 Otto Moerbeek <otto@drijf.net>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: rsync.c
|
||||||
|
Copyright: 2011-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: strdupa.h strndupa.h
|
||||||
|
Copyright: 2009 William Ahern
|
||||||
|
License: MIT
|
||||||
|
|
||||||
|
Files: strlcat.c strlcpy.c
|
||||||
|
Copyright: 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
License: MIT
|
||||||
|
|
||||||
|
Files: strmatch.c
|
||||||
|
Copyright: 2009-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: strnlen.c
|
||||||
|
Copyright: 2016-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: strtonum.c
|
||||||
|
Copyright: 2004 Ted Unangst and Todd Miller
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: telnet.c
|
||||||
|
Copyright: 2010-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: tempfile.c
|
||||||
|
Copyright: 2015-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: tree.h
|
||||||
|
Copyright: 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
|
License: BSD-2-clause
|
||||||
|
|
||||||
|
Files: yorn.c
|
||||||
|
Copyright: 2009-2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
License: ISC
|
||||||
|
|
||||||
|
Files: tests/*.c
|
||||||
|
License: public-domain
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
README.md
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
usr/lib
|
||||||
|
usr/include
|
||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
usr/include/lite/*.h
|
||||||
|
usr/lib/*/lib*.a
|
||||||
|
usr/lib/*/lib*.so
|
||||||
|
usr/lib/*/pkgconfig/lib*.pc
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
usr/lib
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
usr/lib/*/lib*.so.*
|
||||||
Vendored
+45
@@ -0,0 +1,45 @@
|
|||||||
|
libite.so.5 libite5 #MINVER#
|
||||||
|
* Build-Depends-Package: libite-dev
|
||||||
|
__pidfile_name@Base 2.0.0
|
||||||
|
__pidfile_path@Base 2.0.0
|
||||||
|
chomp@Base 2.0.0
|
||||||
|
copyfile@Base 2.0.0
|
||||||
|
dir@Base 2.0.0
|
||||||
|
fcopyfile@Base 2.0.0
|
||||||
|
fexist@Base 2.0.0
|
||||||
|
fgetint@Base 2.0.0
|
||||||
|
fisdir@Base 2.0.0
|
||||||
|
fparseln@Base 2.0.0
|
||||||
|
fsendfile@Base 2.0.0
|
||||||
|
ifconfig@Base 2.0.0
|
||||||
|
initscr@Base 2.0.0
|
||||||
|
lfclose@Base 2.0.0
|
||||||
|
lfgetint@Base 2.0.0
|
||||||
|
lfgetkey@Base 2.0.0
|
||||||
|
lfopen@Base 2.0.0
|
||||||
|
lftok@Base 2.0.0
|
||||||
|
makepath@Base 2.0.0
|
||||||
|
mkpath@Base 2.0.0
|
||||||
|
movefile@Base 2.0.0
|
||||||
|
pidfile@Base 2.0.0
|
||||||
|
pidfile_poll@Base 2.0.0
|
||||||
|
pidfile_read@Base 2.0.0
|
||||||
|
pidfile_signal@Base 2.1.0
|
||||||
|
progress@Base 2.0.0
|
||||||
|
progress_simple@Base 2.0.0
|
||||||
|
reallocarray@Base 2.0.0
|
||||||
|
rsync@Base 2.0.0
|
||||||
|
sdbuf@Base 2.0.0
|
||||||
|
strlcat@Base 2.0.0
|
||||||
|
strlcpy@Base 2.0.0
|
||||||
|
strmatch@Base 2.1.0
|
||||||
|
strnmatch@Base 2.1.0
|
||||||
|
strtonum@Base 2.1.0
|
||||||
|
telnet_close@Base 2.1.0
|
||||||
|
telnet_expect@Base 2.1.0
|
||||||
|
telnet_open@Base 2.1.0
|
||||||
|
telnet_session@Base 2.1.0
|
||||||
|
tempfile@Base 2.0.0
|
||||||
|
which@Base 2.0.0
|
||||||
|
whichp@Base 2.0.0
|
||||||
|
yorn@Base 2.1.0
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/make -f
|
||||||
|
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
|
||||||
|
|
||||||
|
# Uncomment this to turn on verbose mode.
|
||||||
|
#export DH_VERBOSE=1
|
||||||
|
|
||||||
|
%:
|
||||||
|
dh $@ --with autoreconf
|
||||||
|
|
||||||
|
override_dh_auto_install:
|
||||||
|
dh_auto_install
|
||||||
|
-rm -f debian/libite/usr/share/doc/libite/LICENSE
|
||||||
|
|
||||||
|
override_dh_installchangelogs:
|
||||||
|
dh_installchangelogs ChangeLog.md
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
3.0 (native)
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
compression=xz
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
/* File mode bit operations
|
|
||||||
*
|
|
||||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
|
||||||
*
|
|
||||||
* 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 <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fmode - Return a file's mode bits.
|
|
||||||
* @file: File to look for, with full path.
|
|
||||||
*
|
|
||||||
* Returns:
|
|
||||||
* The mode bits or zero when @file does not exist and errno set to %ENOENT.
|
|
||||||
*/
|
|
||||||
mode_t fmode(char *file)
|
|
||||||
{
|
|
||||||
struct stat sb;
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stat(file, &sb))
|
|
||||||
return sb.st_mode;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
|
||||||
* c-file-style: "linux"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
prefix=@prefix@
|
||||||
|
exec_prefix=@exec_prefix@
|
||||||
|
libdir=@libdir@
|
||||||
|
includedir=@includedir@
|
||||||
|
|
||||||
|
Name: @PACKAGE@
|
||||||
|
Description: Frog DNA, basically
|
||||||
|
Version: @VERSION@
|
||||||
|
Requires:
|
||||||
|
Libs: -L${libdir} -lite
|
||||||
|
Cflags: -I${includedir}
|
||||||
|
|
||||||
@@ -1,162 +0,0 @@
|
|||||||
/* Collection of utility funcs and C library extensions for finit & its plugins
|
|
||||||
*
|
|
||||||
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
|
||||||
* Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef FINIT_LITE_H_
|
|
||||||
#define FINIT_LITE_H_
|
|
||||||
|
|
||||||
#include <error.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
char *chomp (char *str);
|
|
||||||
|
|
||||||
int fexist (char *file);
|
|
||||||
int fisdir (char *file);
|
|
||||||
mode_t fmode (char *file);
|
|
||||||
|
|
||||||
FILE *tempfile (void);
|
|
||||||
ssize_t copyfile (char *src, char *dst, int len, int sym);
|
|
||||||
int movefile (char *src, char *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);
|
|
||||||
|
|
||||||
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 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
|
|
||||||
|
|
||||||
#ifndef touch
|
|
||||||
# define touch(x) do { if (mknod((x), S_IFREG|0644, 0) && errno != EEXIST) error(0, errno, "Failed creating %s", x); } while (0)
|
|
||||||
#endif
|
|
||||||
#ifndef makedir
|
|
||||||
# define makedir(x, p) do { if (mkdir(x, p) && errno != EEXIST) error(0, errno, "Failed creating directory %s", x); } while (0)
|
|
||||||
#endif
|
|
||||||
#ifndef makefifo
|
|
||||||
# define makefifo(x, p) do { if (mkfifo(x, p) && errno != EEXIST) error(0, errno, "Failed creating FIFO %s", x); } while (0)
|
|
||||||
#endif
|
|
||||||
#ifndef erase
|
|
||||||
# define erase(x) do { if (remove(x) && errno != ENOENT) error(0, errno, "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
|
|
||||||
#ifndef UNUSED
|
|
||||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static inline int fisslashdir(char *dir)
|
|
||||||
{
|
|
||||||
if (!dir) return 0;
|
|
||||||
if (strlen (dir) > 0) return dir[strlen (dir) - 1] == '/';
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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 /* FINIT_LITE_H_ */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
|
||||||
* c-file-style: "linux"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
*.m4
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# Unittest makefile for rsync.c
|
|
||||||
.PHONY: clean
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# 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-$(@)))
|
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
*.la
|
||||||
|
.libs/*
|
||||||
|
libite_la*
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
lib_LTLIBRARIES = libite.la
|
||||||
|
|
||||||
|
libite_la_CPPFLAGS = -D_GNU_SOURCE
|
||||||
|
libite_la_CFLAGS = -W -Wall -Wextra
|
||||||
|
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 5:1:0
|
||||||
|
libite_la_SOURCES = chomp.c copyfile.c \
|
||||||
|
conio.c conio.h \
|
||||||
|
dir.c \
|
||||||
|
fexist.c fisdir.c \
|
||||||
|
fparseln.c fsendfile.c \
|
||||||
|
ifconfig.c lfile.c \
|
||||||
|
makepath.c progress.c \
|
||||||
|
pidfile.c pidfilefn.c \
|
||||||
|
reallocarray.c rsync.c \
|
||||||
|
strlcpy.c strlcat.c strtonum.c \
|
||||||
|
strdupa.h strndupa.h strnlen.h \
|
||||||
|
strmatch.c telnet.c tempfile.c \
|
||||||
|
yorn.c which.c \
|
||||||
|
lite.h strlite.h \
|
||||||
|
queue.h tree.h
|
||||||
|
|
||||||
|
|
||||||
|
## Distribute these header files in the <lite/...> name space
|
||||||
|
pkgincludedir = $(includedir)/lite
|
||||||
|
pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h \
|
||||||
|
strndupa.h strnlen.h
|
||||||
+3
-14
@@ -1,6 +1,6 @@
|
|||||||
/* Perl inspired chomp() implementation.
|
/* Perl inspired chomp() implementation.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2014-2015 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2014-2020 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
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
* @str: String to chomp
|
* @str: String to chomp
|
||||||
*
|
*
|
||||||
* This function is like Perl chomp, but it's set to chop of all
|
* This function is like Perl chomp, but it's set to chop of all
|
||||||
* trailing newlines.
|
* trailing newlines. Useful in combination with fgets().
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* If @str is a valid pointer this function returns @str, otherwise
|
* If @str is a valid pointer this function returns @str, otherwise
|
||||||
@@ -39,25 +39,14 @@ char *chomp(char *str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
p = str + strlen(str) - 1;
|
p = str + strlen(str) - 1;
|
||||||
while (*p == '\n')
|
while (p >= str && *p == '\n')
|
||||||
*p-- = 0;
|
*p-- = 0;
|
||||||
|
|
||||||
return str;
|
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("'%s'\n", chomp(t[i])); return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -DUNITTEST -o chompy chomp.c && ./chompy"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+83
@@ -0,0 +1,83 @@
|
|||||||
|
/* A conio.h like implementation for VTANSI displays.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2020 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 <poll.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void initscr(int *row, int *col)
|
||||||
|
{
|
||||||
|
if (!row || !col)
|
||||||
|
return;
|
||||||
|
|
||||||
|
#if defined(TCSANOW) && defined(POLLIN)
|
||||||
|
struct termios tc, saved;
|
||||||
|
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
|
||||||
|
|
||||||
|
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disable echo to terminal while probing */
|
||||||
|
tcgetattr(STDOUT_FILENO, &tc);
|
||||||
|
saved = tc;
|
||||||
|
tc.c_cflag |= (CLOCAL | CREAD);
|
||||||
|
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||||
|
tcsetattr(STDOUT_FILENO, TCSANOW, &tc);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Save cursor pos+attr
|
||||||
|
* Diable top+bottom margins
|
||||||
|
* Set cursor at the far bottom,right pos
|
||||||
|
* Query term for resulting pos
|
||||||
|
*/
|
||||||
|
fprintf(stdout, "\e7" "\e[r" "\e[999;999H" "\e[6n");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wait here for terminal to echo back \e[row,lineR ...
|
||||||
|
*/
|
||||||
|
if (poll(&fd, 1, 300) <= 0 || scanf("\e[%d;%dR", row, col) != 2) {
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Restore above saved cursor pos+attr
|
||||||
|
*/
|
||||||
|
fprintf(stdout, "\e8");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
/* Restore terminal */
|
||||||
|
tcsetattr(STDOUT_FILENO, TCSANOW, &saved);
|
||||||
|
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+118
@@ -0,0 +1,118 @@
|
|||||||
|
/* A conio.h like implementation for VTANSI displays.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#ifndef SCREEN_WIDTH
|
||||||
|
#define SCREEN_WIDTH 80 /* Override using, e.g. #define SCREEN_WIDTH tty_width() */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Esc[2JEsc[1;1H - Clear screen and move cursor to 1,1 (upper left) pos. */
|
||||||
|
#define clrscr() fputs("\033[2J\033[1;1H", stdout)
|
||||||
|
/* Esc[K - Erases from the current cursor position to the end of the current line. */
|
||||||
|
#define clreol() fputs("\033[K", stdout)
|
||||||
|
/* Esc[2K - Erases the entire current line. */
|
||||||
|
#define delline() fputs("\033[2K", stdout)
|
||||||
|
/* Esc[Line;ColumnH - Moves the cursor to the specified position (coordinates) */
|
||||||
|
#define gotoxy(x,y) fprintf(stdout, "\033[%d;%dH", y, x)
|
||||||
|
/* Esc[?25l (lower case L) - Hide Cursor */
|
||||||
|
#define hidecursor() fputs("\033[?25l", stdout)
|
||||||
|
/* Esc[?25h (lower case H) - Show Cursor */
|
||||||
|
#define showcursor() fputs("\033[?25h", stdout)
|
||||||
|
|
||||||
|
/* Esc[Value;...;Valuem - Set Graphics Mode (attr, color, val) */
|
||||||
|
#define __set_gm(a,c,v) \
|
||||||
|
if (!c) \
|
||||||
|
fprintf(stdout, "\033[%dm", a); \
|
||||||
|
else \
|
||||||
|
fprintf(stdout, "\033[%d;%dm", c & 0x10 ? 1 : 0, (c & 0xF) + v)
|
||||||
|
#define textattr(attr) __set_gm(attr, 0, 0)
|
||||||
|
#define textcolor(color) __set_gm(RESETATTR, color, 30)
|
||||||
|
#define textbackground(color) __set_gm(RESETATTR, color, 40)
|
||||||
|
|
||||||
|
void initscr(int *row, int *col);
|
||||||
|
|
||||||
|
/* Print table heading @line to @fp, with optional leading newline
|
||||||
|
* Example:
|
||||||
|
* _________________ <-- Empty line with UNDERSCORE to frame first heading
|
||||||
|
* First heading <-- In normal/RESETATTR
|
||||||
|
* SUBHEADING <-- In REVERSE and with capital letters like 'top'
|
||||||
|
*/
|
||||||
|
static inline void printhdr(FILE *fp, const char *line, int nl, int attr)
|
||||||
|
{
|
||||||
|
if (attr < 0 || attr > 8)
|
||||||
|
attr = REVERSE;
|
||||||
|
|
||||||
|
fprintf(fp ?: stdout,
|
||||||
|
"%s\033[%dm%s%*s\033[0m\n",
|
||||||
|
nl ? "\n" : "", attr, line,
|
||||||
|
SCREEN_WIDTH - (int)strlen(line), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void printheader(FILE *fp, const char *line, int nl)
|
||||||
|
{
|
||||||
|
printhdr(fp, line, nl, REVERSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONIO_H_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+44
-82
@@ -23,15 +23,11 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
|
|
||||||
|
|
||||||
/* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */
|
/* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */
|
||||||
static int adjust_target(char *src, char **dst)
|
static int adjust_target(const char *src, char **dst)
|
||||||
{
|
{
|
||||||
int isdir = 0;
|
int isdir = 0;
|
||||||
|
|
||||||
@@ -40,7 +36,7 @@ static int adjust_target(char *src, char **dst)
|
|||||||
char *tmp, *ptr = strrchr(src, '/');
|
char *tmp, *ptr = strrchr(src, '/');
|
||||||
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
ptr = src;
|
ptr = (char *)src;
|
||||||
else
|
else
|
||||||
ptr++;
|
ptr++;
|
||||||
|
|
||||||
@@ -84,26 +80,46 @@ static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len)
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_mtime(int in, int out)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
struct timespec tv[2];
|
||||||
|
|
||||||
|
fstat(in, &st);
|
||||||
|
tv[0] = st.st_atim;
|
||||||
|
tv[1] = st.st_mtim;
|
||||||
|
futimens(out, tv);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* copyfile - Copy a file to another.
|
* copyfile - Copy a file to another.
|
||||||
* @src: Full path name to source file.
|
* @src: Full path name to source file.
|
||||||
* @dst: Full path name to 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)
|
* @opt: An option mask of %LITE_FOPT_COPYFILE_SYM, %LITE_FOPT_KEEP_MTIME
|
||||||
*
|
*
|
||||||
* 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.
|
||||||
*
|
*
|
||||||
|
* The @opt field replaces the @sym argument in previous releases and
|
||||||
|
* works as follows. To maintain backwards compatibility with @sym
|
||||||
|
* the %LITE_FOPT_COPYFILE_SYM maintains a value of 1:
|
||||||
|
*
|
||||||
|
* %LITE_FOPT_COPYFILE_SYM: Recreate symlink or follow to copy target
|
||||||
|
* %LITE_FOPT_KEEP_MTIME: Preserve modification time
|
||||||
|
*
|
||||||
* 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, int sym)
|
ssize_t copyfile(const char *src, const char *dst, int len, int opt)
|
||||||
{
|
{
|
||||||
char *buffer;
|
char *buffer, *dest = (char *)dst;
|
||||||
|
int sym = (opt & LITE_FOPT_COPYFILE_SYM) != 0;
|
||||||
|
int keep_mtim = (opt & LITE_FOPT_KEEP_MTIME) != 0;
|
||||||
int in, out, isdir = 0, saved_errno = 0;
|
int in, out, isdir = 0, saved_errno = 0;
|
||||||
size_t num;
|
size_t num;
|
||||||
ssize_t size = 0;
|
ssize_t size = 0;
|
||||||
@@ -121,7 +137,7 @@ ssize_t copyfile(char *src, char *dst, int len, int sym)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check if target is a directory, then append src filename. */
|
/* Check if target is a directory, then append src filename. */
|
||||||
isdir = adjust_target(src, &dst);
|
isdir = adjust_target(src, &dest);
|
||||||
|
|
||||||
/* Check if the source file is a symlink ... */
|
/* Check if the source file is a symlink ... */
|
||||||
if (stat(src, &st)) {
|
if (stat(src, &st)) {
|
||||||
@@ -138,7 +154,7 @@ ssize_t copyfile(char *src, char *dst, int len, int sym)
|
|||||||
size = -1;
|
size = -1;
|
||||||
} else {
|
} else {
|
||||||
buffer[size] = 0;
|
buffer[size] = 0;
|
||||||
size = !symlink(buffer, dst);
|
size = !symlink(buffer, dest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +175,7 @@ ssize_t copyfile(char *src, char *dst, int len, int sym)
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, fmode(src));
|
out = open(dest, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode);
|
||||||
if (out < 0) {
|
if (out < 0) {
|
||||||
close(in);
|
close(in);
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
@@ -169,6 +185,8 @@ ssize_t copyfile(char *src, char *dst, int len, int sym)
|
|||||||
size = do_copy(in, out, num, buffer, BUFSIZ);
|
size = do_copy(in, out, num, buffer, BUFSIZ);
|
||||||
if (!size && errno)
|
if (!size && errno)
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
|
else if (keep_mtim)
|
||||||
|
set_mtime(in, out);
|
||||||
|
|
||||||
close(out);
|
close(out);
|
||||||
close(in);
|
close(in);
|
||||||
@@ -176,7 +194,7 @@ ssize_t copyfile(char *src, char *dst, int len, int sym)
|
|||||||
exit:
|
exit:
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if (isdir)
|
if (isdir)
|
||||||
free(dst);
|
free(dest);
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
@@ -198,17 +216,18 @@ exit:
|
|||||||
* Returns:
|
* Returns:
|
||||||
* POSIX OK(0), or non-zero with errno set.
|
* POSIX OK(0), or non-zero with errno set.
|
||||||
*/
|
*/
|
||||||
int movefile(char *src, char *dst)
|
int movefile(const char *src, const char *dst)
|
||||||
{
|
{
|
||||||
|
char *dest = (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 = adjust_target(src, &dst);
|
isdir = adjust_target(src, &dest);
|
||||||
|
|
||||||
if (rename(src, dst)) {
|
if (rename(src, dest)) {
|
||||||
if (errno == EXDEV) {
|
if (errno == EXDEV) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
copyfile(src, dst, 0, 1);
|
copyfile(src, dest, 0, 1);
|
||||||
if (errno)
|
if (errno)
|
||||||
result = 1;
|
result = 1;
|
||||||
else
|
else
|
||||||
@@ -219,7 +238,7 @@ int movefile(char *src, char *dst)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isdir)
|
if (isdir)
|
||||||
free(dst);
|
free(dest);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -237,84 +256,27 @@ int movefile(char *src, char *dst)
|
|||||||
*/
|
*/
|
||||||
int fcopyfile(FILE *src, FILE *dst)
|
int fcopyfile(FILE *src, FILE *dst)
|
||||||
{
|
{
|
||||||
int ret;
|
char *buf;
|
||||||
char *buffer;
|
|
||||||
|
|
||||||
if (!src || !dst) {
|
if (!src || !dst) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = malloc(BUFSIZ);
|
buf = malloc(BUFSIZ);
|
||||||
if (!buffer)
|
if (!buf)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ret = do_copy(fileno(src), fileno(dst), BUFSIZ, buffer, BUFSIZ);
|
while (fgets(buf, BUFSIZ, src))
|
||||||
if (ret > 0)
|
fputs(buf, dst);
|
||||||
ret = 0;
|
|
||||||
else if (errno)
|
free(buf);
|
||||||
ret = -1;
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef UNITTEST
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
char *files[] = {
|
|
||||||
"/etc/passwd", "/tmp/tok", "/tmp/tok",
|
|
||||||
"/etc/passwd", "/tmp/", "/tmp/passwd",
|
|
||||||
"/etc/passwd", "/tmp", "/tmp/passwd",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
FILE *src, *dst;
|
|
||||||
|
|
||||||
fprintf(stderr, "\n=>Start testing copy_filep()\n");
|
|
||||||
while (files[i]) {
|
|
||||||
fprintf(stderr, "fcopyfile(%s, %s)\t", files[i],
|
|
||||||
files[i + 1]);
|
|
||||||
src = fopen(files[i], "r");
|
|
||||||
dst = fopen(files[i + 1], "w");
|
|
||||||
if (fcopyfile(src, dst))
|
|
||||||
perror("Failed");
|
|
||||||
|
|
||||||
if (src)
|
|
||||||
fclose(src);
|
|
||||||
if (dst)
|
|
||||||
fclose(dst);
|
|
||||||
|
|
||||||
if (fexist(files[i + 2]))
|
|
||||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
|
||||||
|
|
||||||
erase(files[i + 2]);
|
|
||||||
i += 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n\n=>Start testing copyfile()\n");
|
|
||||||
i = 0;
|
|
||||||
while (files[i]) {
|
|
||||||
fprintf(stderr, "copyfile(%s, %s)\t", files[i], files[i + 1]);
|
|
||||||
if (!copyfile(files[i], files[i + 1], 0, 0))
|
|
||||||
perror("Failed");
|
|
||||||
|
|
||||||
if (fexist(files[i + 2]))
|
|
||||||
fprintf(stderr, "OK => %s\n", files[i + 2]);
|
|
||||||
|
|
||||||
erase(files[i + 2]);
|
|
||||||
i += 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -o coppy -DUNITTEST copyfile.c fisdir.c fexist.c fmode.c && ./coppy"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+13
-110
@@ -1,6 +1,6 @@
|
|||||||
/* Functions for operating on files in directories.
|
/* Functions for operating on files in directories.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2008-2014 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2008-2020 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
|
||||||
@@ -15,14 +15,14 @@
|
|||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <assert.h>
|
#include <errno.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static const char *matcher_type = NULL;
|
static const char *matcher_type = NULL;
|
||||||
static int (*matcher_filter) (const char *file) = NULL;
|
static int (*matcher_filter) (const char *file) = NULL;
|
||||||
|
|
||||||
static int matcher(const struct dirent *entry)
|
static int matcher(const struct dirent *entry)
|
||||||
{
|
{
|
||||||
char *pos = strrchr(entry->d_name, '.');
|
char *pos = strrchr(entry->d_name, '.');
|
||||||
@@ -72,7 +72,8 @@ static int matcher(const struct dirent *entry)
|
|||||||
* would be returned as "config0".
|
* would be returned as "config0".
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* Number of files in @list, zero if no matching files of @type.
|
* Number of files in @list, zero if no matching files of @type, or
|
||||||
|
* non-zero on error with @errno set.
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -80,7 +81,10 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
char **files;
|
char **files;
|
||||||
struct dirent **namelist;
|
struct dirent **namelist;
|
||||||
|
|
||||||
assert(list);
|
if (!list) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!dir)
|
if (!dir)
|
||||||
/* Assuming current directory */
|
/* Assuming current directory */
|
||||||
@@ -92,9 +96,10 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
matcher_type = type;
|
matcher_type = type;
|
||||||
matcher_filter = filter;
|
matcher_filter = filter;
|
||||||
n = scandir(dir, &namelist, matcher, alphasort);
|
n = scandir(dir, &namelist, matcher, alphasort);
|
||||||
if (n < 0) {
|
if (n < 0)
|
||||||
perror("scandir");
|
return -1;
|
||||||
} else if (n > 0) {
|
|
||||||
|
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) {
|
||||||
@@ -119,110 +124,8 @@ int dir(const char *dir, const char *type, int (*filter) (const char *file), cha
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITTEST
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include "otn/c.h"
|
|
||||||
|
|
||||||
#define DIR_TYPE_IMAGE ".img"
|
|
||||||
#define DIR_TYPE_SYSLOG ""
|
|
||||||
#define DIR_TYPE_CONFIG ".cfg"
|
|
||||||
#define STARTUP_CONFIG "startup-config.cfg"
|
|
||||||
|
|
||||||
int simulate_files(int creat)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *files[] =
|
|
||||||
{ "config0.cfg", "config1.cfg", "config2.cfg", "config3.cfg",
|
|
||||||
"rr109.img", "rx100.img", "rx107.img", "rm957.img", "messages"
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_ELEMENTS(files); i++) {
|
|
||||||
if (creat)
|
|
||||||
touch(files[i]);
|
|
||||||
else
|
|
||||||
erase(files[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (creat)
|
|
||||||
symlink("config2.cfg", STARTUP_CONFIG);
|
|
||||||
else
|
|
||||||
erase(STARTUP_CONFIG);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int cfg_dir_filter(const char *file)
|
|
||||||
{
|
|
||||||
/* Skip the STARTUP_CONFIG file, it is a symbolic link to the
|
|
||||||
* current startup configuration. */
|
|
||||||
return ! !strcmp(file, STARTUP_CONFIG);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int i, num;
|
|
||||||
char *type = DIR_TYPE_CONFIG;
|
|
||||||
char **files;
|
|
||||||
int once = 1;
|
|
||||||
|
|
||||||
int is_startup_config(const char *entry) {
|
|
||||||
static char file[80];
|
|
||||||
|
|
||||||
if (once) {
|
|
||||||
int len = readlink(STARTUP_CONFIG, file, sizeof(file));
|
|
||||||
|
|
||||||
if (len == -1)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
file[len] = 0;
|
|
||||||
once = 0; /* Only once per call to dir() */
|
|
||||||
}
|
|
||||||
//printf ("Comparing link %s with entry %s\n", file, entry);
|
|
||||||
return !strcmp(file, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
simulate_files(1);
|
|
||||||
|
|
||||||
if (argc >= 2) {
|
|
||||||
if (!strcasecmp("CONFIG", argv[1])) {
|
|
||||||
type = DIR_TYPE_CONFIG;
|
|
||||||
system("ls -l *" DIR_TYPE_CONFIG);
|
|
||||||
}
|
|
||||||
if (!strcasecmp("IMAGE", argv[1])) {
|
|
||||||
type = DIR_TYPE_IMAGE;
|
|
||||||
system("ls -l *" DIR_TYPE_IMAGE);
|
|
||||||
}
|
|
||||||
if (!strcasecmp("SYSLOG", argv[1])) {
|
|
||||||
type = DIR_TYPE_SYSLOG;
|
|
||||||
system("ls -l *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
num = dir(NULL, type, cfg_dir_filter, &files, 0);
|
|
||||||
if (num) {
|
|
||||||
for (i = 0; i < num; i++) {
|
|
||||||
printf("%s", files[i]);
|
|
||||||
if (is_startup_config(files[i]))
|
|
||||||
printf(" --> startup-config");
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
free(files[i]);
|
|
||||||
}
|
|
||||||
free(files);
|
|
||||||
}
|
|
||||||
|
|
||||||
simulate_files(0);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -DUNITTEST -o unittest dir.c"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
* Returns:
|
* Returns:
|
||||||
* %TRUE(1) if the file exists, otherwise %FALSE(0).
|
* %TRUE(1) if the file exists, otherwise %FALSE(0).
|
||||||
*/
|
*/
|
||||||
int fexist(char *file)
|
int fexist(const char *file)
|
||||||
{
|
{
|
||||||
if (!file) {
|
if (!file) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
@@ -46,7 +46,6 @@ int fexist(char *file)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
@@ -21,22 +21,21 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fexist - Check if a file exists and is a directory.
|
* fisdir - Check if a path exists and is a directory.
|
||||||
* @file: File to look for, with full path.
|
* @path: Path to file or directory
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* %TRUE(1) if the file exists and is a directory, otherwise %FALSE(0).
|
* %TRUE(1) if @path exists and is a directory, otherwise %FALSE(0).
|
||||||
*/
|
*/
|
||||||
int fisdir(char *file)
|
int fisdir(const char *path)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
||||||
if (!stat(file, &sb) && S_ISDIR(sb.st_mode))
|
if (!stat(path, &sb) && S_ISDIR(sb.st_mode))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -44,7 +43,6 @@ int fisdir(char *file)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+199
@@ -0,0 +1,199 @@
|
|||||||
|
/* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
#ifndef _DIAGASSERT
|
||||||
|
#define _DIAGASSERT(t)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int isescaped(const char *, const char *, int);
|
||||||
|
|
||||||
|
/* isescaped():
|
||||||
|
* Return true if the character in *p that belongs to a string
|
||||||
|
* that starts in *sp, is escaped by the escape character esc.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
isescaped(const char *sp, const char *p, int esc)
|
||||||
|
{
|
||||||
|
const char *cp;
|
||||||
|
size_t ne;
|
||||||
|
|
||||||
|
_DIAGASSERT(sp != NULL);
|
||||||
|
_DIAGASSERT(p != NULL);
|
||||||
|
|
||||||
|
/* No escape character */
|
||||||
|
if (esc == '\0')
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Count the number of escape characters that precede ours */
|
||||||
|
for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* Return true if odd number of escape characters */
|
||||||
|
return (ne & 1) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* fparseln():
|
||||||
|
* Read a line from a file parsing continuations ending in \
|
||||||
|
* and eliminating trailing newlines, or comments starting with
|
||||||
|
* the comment char.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
|
||||||
|
{
|
||||||
|
static const char dstr[3] = { '\\', '\\', '#' };
|
||||||
|
|
||||||
|
ssize_t s;
|
||||||
|
size_t len, ptrlen;
|
||||||
|
char *buf;
|
||||||
|
char *ptr, *cp;
|
||||||
|
int cnt;
|
||||||
|
char esc, con, nl, com;
|
||||||
|
|
||||||
|
_DIAGASSERT(fp != NULL);
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
buf = NULL;
|
||||||
|
ptrlen = 0;
|
||||||
|
ptr = NULL;
|
||||||
|
cnt = 1;
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
str = dstr;
|
||||||
|
|
||||||
|
esc = str[0];
|
||||||
|
con = str[1];
|
||||||
|
com = str[2];
|
||||||
|
/*
|
||||||
|
* XXX: it would be cool to be able to specify the newline character,
|
||||||
|
* getdelim(3) does let us, but supporting it would diverge from BSDs.
|
||||||
|
*/
|
||||||
|
nl = '\n';
|
||||||
|
|
||||||
|
flockfile(fp);
|
||||||
|
|
||||||
|
while (cnt) {
|
||||||
|
cnt = 0;
|
||||||
|
|
||||||
|
if (lineno)
|
||||||
|
(*lineno)++;
|
||||||
|
|
||||||
|
s = getline(&ptr, &ptrlen, fp);
|
||||||
|
if (s < 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (s && com) { /* Check and eliminate comments */
|
||||||
|
for (cp = ptr; cp < ptr + s; cp++)
|
||||||
|
if (*cp == com && !isescaped(ptr, cp, esc)) {
|
||||||
|
s = cp - ptr;
|
||||||
|
cnt = s == 0 && buf == NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s && nl) { /* Check and eliminate newlines */
|
||||||
|
cp = &ptr[s - 1];
|
||||||
|
|
||||||
|
if (*cp == nl)
|
||||||
|
s--; /* forget newline */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s && con) { /* Check and eliminate continuations */
|
||||||
|
cp = &ptr[s - 1];
|
||||||
|
|
||||||
|
if (*cp == con && !isescaped(ptr, cp, esc)) {
|
||||||
|
s--; /* forget continuation char */
|
||||||
|
cnt = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s == 0) {
|
||||||
|
/*
|
||||||
|
* nothing to add, skip realloc except in case
|
||||||
|
* we need a minimal buf to return an empty line
|
||||||
|
*/
|
||||||
|
if (cnt || buf != NULL)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((cp = realloc(buf, len + s + 1)) == NULL) {
|
||||||
|
funlockfile(fp);
|
||||||
|
free(buf);
|
||||||
|
free(ptr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
buf = cp;
|
||||||
|
|
||||||
|
(void) memcpy(buf + len, ptr, s);
|
||||||
|
len += s;
|
||||||
|
buf[len] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
funlockfile(fp);
|
||||||
|
free(ptr);
|
||||||
|
|
||||||
|
if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
|
||||||
|
strchr(buf, esc) != NULL) {
|
||||||
|
ptr = cp = buf;
|
||||||
|
while (cp[0] != '\0') {
|
||||||
|
int skipesc;
|
||||||
|
|
||||||
|
while (cp[0] != '\0' && cp[0] != esc)
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
if (cp[0] == '\0' || cp[1] == '\0')
|
||||||
|
break;
|
||||||
|
|
||||||
|
skipesc = 0;
|
||||||
|
if (cp[1] == com)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCCOMM);
|
||||||
|
if (cp[1] == con)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCCONT);
|
||||||
|
if (cp[1] == esc)
|
||||||
|
skipesc += (flags & FPARSELN_UNESCESC);
|
||||||
|
if (cp[1] != com && cp[1] != con && cp[1] != esc)
|
||||||
|
skipesc = (flags & FPARSELN_UNESCREST);
|
||||||
|
|
||||||
|
if (skipesc)
|
||||||
|
cp++;
|
||||||
|
else
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
*ptr++ = *cp++;
|
||||||
|
}
|
||||||
|
*ptr = '\0';
|
||||||
|
len = strlen(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
*size = len;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
@@ -41,17 +41,18 @@
|
|||||||
* The number of bytes copied. If an error is detected -1 is returned
|
* The number of bytes copied. If an error is detected -1 is returned
|
||||||
* and @errno will be set accordingly.
|
* and @errno will be set accordingly.
|
||||||
*/
|
*/
|
||||||
size_t fsendfile(FILE *src, FILE *dst, size_t len)
|
ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t blk = BUFSIZ, num = 0, tot = 0;
|
size_t tot = 0;
|
||||||
|
ssize_t blk = BUFSIZ, num = 0;
|
||||||
|
|
||||||
if (!src) {
|
if (!src) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = (char *)malloc(BUFSIZ);
|
buf = malloc(BUFSIZ);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -73,45 +74,11 @@ size_t fsendfile(FILE *src, FILE *dst, size_t len)
|
|||||||
|
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
return (num == (size_t)-1) ? (size_t)-1 : tot;
|
return (num == -1) ? -1 : (ssize_t)tot;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITTEST
|
|
||||||
#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");
|
|
||||||
fprintf(stderr, "fsendfile(%s, %s, 512)\t", files[i], files[i + 1]);
|
|
||||||
if (-1 == fsendfile(src, dst, 512))
|
|
||||||
perror("Failed");
|
|
||||||
|
|
||||||
if (!access(files[i + 1], F_OK))
|
|
||||||
fprintf(stderr, "OK => %s\n", files[i + 1]);
|
|
||||||
|
|
||||||
remove(files[i + 1]);
|
|
||||||
i += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -o sendy -DUNITTEST fsendfile.c && ./sendy"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
/* Fastinit (finit) ifconfig() implementation.
|
/* Fastinit (finit) ifconfig() implementation.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
|
* Copyright (c) 2008-2010 Claudio Matsuoka <http://helllabs.org/finit/>
|
||||||
* Copyright (C) 2009-2014 Joachim Nilsson <joachim.nilsson@westermo.se>
|
* Copyright (C) 2009-2020 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
|
||||||
@@ -43,7 +43,7 @@ extern size_t strlcpy(char *dst, const char *src, size_t siz);
|
|||||||
* Returns:
|
* Returns:
|
||||||
* POSIX OK(0) on success, or non-zero on error.
|
* POSIX OK(0) on success, or non-zero on error.
|
||||||
*/
|
*/
|
||||||
int ifconfig(char *ifname, char *addr, char *mask, int up)
|
int ifconfig(const char *ifname, const char *addr, const char *mask, int up)
|
||||||
{
|
{
|
||||||
int sd, ret = -1;
|
int sd, ret = -1;
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
@@ -85,7 +85,6 @@ int ifconfig(char *ifname, char *addr, char *mask, int up)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+193
@@ -0,0 +1,193 @@
|
|||||||
|
/* Parse UNIX /etc configuration files like /etc/protocols and /etc/services
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2020 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 *save;
|
||||||
|
const char *sep;
|
||||||
|
} 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(const char *file, const 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)
|
||||||
|
return 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)
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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, const 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, const 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(const char *file, const char *sep, const char *key)
|
||||||
|
{
|
||||||
|
int val = -1;
|
||||||
|
lfile_t *lf;
|
||||||
|
|
||||||
|
lf = lfopen(file, sep);
|
||||||
|
if (lf) {
|
||||||
|
val = lfgetint(lf, key);
|
||||||
|
lfclose(lf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+209
@@ -0,0 +1,209 @@
|
|||||||
|
/* Collection of frog DNA
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
|
||||||
|
* Copyright (c) 2008-2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LITE_H_
|
||||||
|
#define LITE_H_
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h> /* Definition of AT_* constants for utimensat() */
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "strlite.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* fparseln() specific operation flags.
|
||||||
|
*/
|
||||||
|
#define FPARSELN_UNESCESC 0x01
|
||||||
|
#define FPARSELN_UNESCCONT 0x02
|
||||||
|
#define FPARSELN_UNESCCOMM 0x04
|
||||||
|
#define FPARSELN_UNESCREST 0x08
|
||||||
|
#define FPARSELN_UNESCALL 0x0f
|
||||||
|
|
||||||
|
/*
|
||||||
|
* copyfile() and rsync() option flags
|
||||||
|
*/
|
||||||
|
#define LITE_FOPT_RSYNC_DELETE 0x01
|
||||||
|
#define LITE_FOPT_COPYFILE_SYM 0x01
|
||||||
|
#define LITE_FOPT_KEEP_MTIME 0x02
|
||||||
|
|
||||||
|
typedef struct lfile lfile_t;
|
||||||
|
typedef struct sdbuf sdbuf_t;
|
||||||
|
|
||||||
|
char *chomp (char *str);
|
||||||
|
char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
|
||||||
|
|
||||||
|
int fexist (const char *file);
|
||||||
|
int fisdir (const char *path);
|
||||||
|
|
||||||
|
FILE *tempfile (void);
|
||||||
|
ssize_t copyfile (const char *src, const char *dst, int len, int opt);
|
||||||
|
int movefile (const char *src, const char *dst);
|
||||||
|
int fcopyfile (FILE *src, FILE *dst);
|
||||||
|
ssize_t fsendfile (FILE *src, FILE *dst, size_t len);
|
||||||
|
|
||||||
|
int ifconfig (const char *ifname, const char *addr, const char *mask, int up);
|
||||||
|
|
||||||
|
lfile_t*lfopen (const char *file, const char *sep);
|
||||||
|
void lfclose (lfile_t *lf);
|
||||||
|
char *lftok (lfile_t *lf);
|
||||||
|
char *lfgetkey (lfile_t *lf, const char *key);
|
||||||
|
int lfgetint (lfile_t *lf, const char *key);
|
||||||
|
int fgetint (const char *file, const char *sep, const char *key);
|
||||||
|
|
||||||
|
int mkpath (const char *dir, mode_t mode);
|
||||||
|
int makepath (const char *dir);
|
||||||
|
|
||||||
|
int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip);
|
||||||
|
int rsync (char *src, char *dst, int opt, 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);
|
||||||
|
|
||||||
|
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||||
|
|
||||||
|
void progress (int percent, int max_width);
|
||||||
|
void progress_simple(int percent);
|
||||||
|
|
||||||
|
sdbuf_t*telnet_open (int addr, short port);
|
||||||
|
int telnet_close (sdbuf_t *ctx);
|
||||||
|
int telnet_expect (sdbuf_t *ctx, char *script[], FILE *output);
|
||||||
|
int telnet_session (int addr, short port, char *script[], FILE *output);
|
||||||
|
|
||||||
|
int yorn (const char *fmt, ...);
|
||||||
|
|
||||||
|
char *which (const char *cmd);
|
||||||
|
int whichp (const char *cmd);
|
||||||
|
|
||||||
|
#ifndef touch
|
||||||
|
#include <sys/stat.h> /* utimensat() */
|
||||||
|
#include <sys/time.h> /* utimensat() on *BSD */
|
||||||
|
static inline int touch(const char *path)
|
||||||
|
{
|
||||||
|
if (mknod((path), S_IFREG|0644, 0)) {
|
||||||
|
if (errno == EEXIST)
|
||||||
|
return utimensat(AT_FDCWD, path, NULL, 0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifndef makedir
|
||||||
|
static inline int makedir(const char *path, mode_t mode)
|
||||||
|
{
|
||||||
|
if (mkdir(path, mode) && errno != EEXIST)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifndef makefifo
|
||||||
|
static inline int makefifo(const char *path, mode_t mode)
|
||||||
|
{
|
||||||
|
if (mkfifo(path, mode) && errno != EEXIST)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifndef erase
|
||||||
|
static inline int erase(const char *path)
|
||||||
|
{
|
||||||
|
if (remove(path) && errno != ENOENT)
|
||||||
|
return -1;
|
||||||
|
return 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
|
||||||
|
|
||||||
|
/* Does directory end with a slash? */
|
||||||
|
static inline int fisslashdir(const char *dir)
|
||||||
|
{
|
||||||
|
if (!dir)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (strlen(dir) > 0)
|
||||||
|
return dir[strlen(dir) - 1] == '/';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
|
|
||||||
+21
-67
@@ -1,6 +1,6 @@
|
|||||||
/* mkpath() -- Create all components leading up to a given directory
|
/* mkpath() -- Create all components leading up to a given directory
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2013-2016 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
|
||||||
@@ -17,20 +17,28 @@
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
int mkpath(char *dir, mode_t mode)
|
/**
|
||||||
|
* mkpath - Like makepath() but takes a mode_t argument
|
||||||
|
* @dir: Directory to created, relative or absolute
|
||||||
|
* @mode: A &mode_t mode to create @dir with
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) on success, otherwise -1 with @errno set.
|
||||||
|
*/
|
||||||
|
int mkpath(const char *dir, mode_t mode)
|
||||||
{
|
{
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return 1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(dir) == 1 && dir[0] == '/')
|
if (!stat(dir, &sb))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
mkpath(dirname(strdupa(dir)), mode);
|
mkpath(dirname(strdupa(dir)), mode);
|
||||||
@@ -42,78 +50,24 @@ int mkpath(char *dir, mode_t mode)
|
|||||||
* makepath - Create all components of the specified directory.
|
* makepath - Create all components of the specified directory.
|
||||||
* @dir: Directory to create.
|
* @dir: Directory to create.
|
||||||
*
|
*
|
||||||
|
* Comment:
|
||||||
|
* It is recommended to use mkpath() over this function since it has
|
||||||
|
* the @mode argument while this function instead default ot 0777,
|
||||||
|
* which in most cases is insecure.
|
||||||
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* POSIX OK (0) on success, otherwise -1 and errno set appropriately.
|
* POSIX OK (0) on success, otherwise -1 and errno set appropriately.
|
||||||
* This function returns EINVAL on bad argument, or ENOMEM when it
|
* This function returns EINVAL on bad argument, or ENOMEM when it
|
||||||
* fails allocating temporary memory. For other error codes see the
|
* fails allocating temporary memory. For other error codes see the
|
||||||
* mkdir() syscall description.
|
* mkdir() syscall description.
|
||||||
*/
|
*/
|
||||||
int makepath(char *dir)
|
int makepath(const char *dir)
|
||||||
{
|
{
|
||||||
return mkpath(dir, 0777);
|
return mkpath(dir, 0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************************* UNIT TESTS ************************************/
|
|
||||||
#ifdef UNITTEST
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int checkpath(char *dir)
|
|
||||||
{
|
|
||||||
char tmp[256];
|
|
||||||
struct stat sb;
|
|
||||||
|
|
||||||
snprintf(tmp, sizeof(tmp), "ls -ld %s", dir);
|
|
||||||
system(tmp);
|
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
fprintf(stderr, "Testing makepath() ...\n");
|
|
||||||
for (i = 0; list[i] && !ret; i++)
|
|
||||||
ret = test_makepath(list[i]);
|
|
||||||
|
|
||||||
fprintf(stderr, "\nCleaning up ...\n");
|
|
||||||
for (i = 0; list[i]; i++)
|
|
||||||
rmdir(list[i]);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -o unittest -D_GNU_SOURCE -DUNITTEST makepath.c && ./unittest"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+40
-11
@@ -1,4 +1,5 @@
|
|||||||
/* $OpenBSD: pidfile.c,v 1.10 2014/06/30 00:26:22 deraadt Exp $ */
|
/* Updated by troglobit for libite/finit/uftpd projects 2016/07/04 */
|
||||||
|
/* $OpenBSD: pidfile.c,v 1.11 2015/06/03 02:24:36 millert Exp $ */
|
||||||
/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
|
/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
@@ -30,6 +31,8 @@
|
|||||||
* POSSIBILITY OF SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/stat.h> /* utimensat() */
|
||||||
|
#include <sys/time.h> /* utimensat() on *BSD */
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
@@ -37,32 +40,48 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static char *pidfile_path;
|
#ifndef pidfile
|
||||||
static pid_t pidfile_pid;
|
static char *pidfile_path = NULL;
|
||||||
|
static pid_t pidfile_pid = 0;
|
||||||
|
|
||||||
static void pidfile_cleanup(void);
|
static void pidfile_cleanup(void);
|
||||||
|
|
||||||
|
const char *__pidfile_path = _PATH_VARRUN; /* Note: includes trailing slash '/' */
|
||||||
|
const char *__pidfile_name = NULL;
|
||||||
extern char *__progname;
|
extern char *__progname;
|
||||||
|
|
||||||
int
|
int
|
||||||
pidfile(const char *basename)
|
pidfile(const char *basename)
|
||||||
{
|
{
|
||||||
int save_errno, result;
|
int save_errno;
|
||||||
|
int atexit_already;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
if (basename == NULL)
|
if (basename == NULL)
|
||||||
basename = __progname;
|
basename = __progname;
|
||||||
|
|
||||||
|
pid = getpid();
|
||||||
|
atexit_already = 0;
|
||||||
|
|
||||||
if (pidfile_path != NULL) {
|
if (pidfile_path != NULL) {
|
||||||
|
if (!access(pidfile_path, R_OK) && pid == pidfile_pid) {
|
||||||
|
utimensat(0, pidfile_path, NULL, 0);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
free(pidfile_path);
|
free(pidfile_path);
|
||||||
pidfile_path = NULL;
|
pidfile_path = NULL;
|
||||||
|
__pidfile_name = NULL;
|
||||||
|
atexit_already = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* _PATH_VARRUN includes trailing / */
|
if (basename[0] != '/') {
|
||||||
result = asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
|
if (asprintf(&pidfile_path, "%s%s.pid", __pidfile_path, basename) == -1)
|
||||||
if (result == -1 || pidfile_path == NULL)
|
return (-1);
|
||||||
return (-1);
|
} else {
|
||||||
|
if (asprintf(&pidfile_path, "%s", basename) == -1)
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
if ((f = fopen(pidfile_path, "w")) == NULL) {
|
if ((f = fopen(pidfile_path, "w")) == NULL) {
|
||||||
save_errno = errno;
|
save_errno = errno;
|
||||||
@@ -72,7 +91,6 @@ pidfile(const char *basename)
|
|||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pid = getpid();
|
|
||||||
if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
|
if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
|
||||||
save_errno = errno;
|
save_errno = errno;
|
||||||
(void) fclose(f);
|
(void) fclose(f);
|
||||||
@@ -83,6 +101,14 @@ pidfile(const char *basename)
|
|||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
(void) fclose(f);
|
(void) fclose(f);
|
||||||
|
__pidfile_name = pidfile_path;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* LITE extension, no need to set up another atexit() handler
|
||||||
|
* if user only called us to update the mtime of the PID file
|
||||||
|
*/
|
||||||
|
if (atexit_already)
|
||||||
|
return (0);
|
||||||
|
|
||||||
pidfile_pid = pid;
|
pidfile_pid = pid;
|
||||||
if (atexit(pidfile_cleanup) < 0) {
|
if (atexit(pidfile_cleanup) < 0) {
|
||||||
@@ -101,7 +127,10 @@ pidfile(const char *basename)
|
|||||||
static void
|
static void
|
||||||
pidfile_cleanup(void)
|
pidfile_cleanup(void)
|
||||||
{
|
{
|
||||||
|
if (pidfile_path != NULL && pidfile_pid == getpid()) {
|
||||||
if (pidfile_path != NULL && pidfile_pid == getpid())
|
|
||||||
(void) unlink(pidfile_path);
|
(void) unlink(pidfile_path);
|
||||||
|
free(pidfile_path);
|
||||||
|
pidfile_path = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/* Functions for dealing with PID files (client side)
|
/* Functions for dealing with PID files (client side)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2009-2015 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2009-2020 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
|
||||||
@@ -111,55 +111,15 @@ int pidfile_signal(const char *pidfile, int signal)
|
|||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
ret = kill(pid, signal);
|
ret = kill(pid, signal);
|
||||||
if ((ret == -1) || (signal == SIGTERM) || (signal == SIGKILL))
|
if (!ret && signal == SIGKILL)
|
||||||
(void)remove(pidfile);
|
ret = remove(pidfile);
|
||||||
|
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/********************************* UNIT TESTS ************************************/
|
|
||||||
#ifdef UTEST_PIDFN
|
|
||||||
#define PIDFILE "/var/run/unittest.pid"
|
|
||||||
|
|
||||||
static void sigterm_handler(int signo)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Exiting ...\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sigalrm_handler(int signo)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Testing pidfile() ...\n");
|
|
||||||
pidfile(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
signal(SIGTERM, sigterm_handler);
|
|
||||||
signal(SIGALRM, sigalrm_handler);
|
|
||||||
alarm(3);
|
|
||||||
|
|
||||||
fprintf(stderr, "Testing pidfile_poll() ...\n");
|
|
||||||
if (!pidfile_poll(PIDFILE))
|
|
||||||
fprintf(stderr, "Timed out!\n");
|
|
||||||
else
|
|
||||||
fprintf(stderr, "We got signal!\n");
|
|
||||||
|
|
||||||
system("ls -ld " PIDFILE);
|
|
||||||
|
|
||||||
fprintf(stderr, "Reading pid file, should be %d ...\n", getpid());
|
|
||||||
fprintf(stderr, "=> %d\n", pidfile_read(PIDFILE));
|
|
||||||
|
|
||||||
fprintf(stderr, "Signalling ourselves ...\n");
|
|
||||||
|
|
||||||
return pidfile_signal(PIDFILE, SIGTERM);
|
|
||||||
}
|
|
||||||
#endif /* UTEST_PIDFN */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -g -o unittest -DUTEST_PIDFN pidfile.c chomp.c pidfilefn.c && ./unittest"
|
* compile-command: "make V=1 -f pidfilefn.mk"
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+122
@@ -0,0 +1,122 @@
|
|||||||
|
/* Simple termios based progress bar
|
||||||
|
*
|
||||||
|
* Copyright (c) 2012-2020 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+40
-146
@@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: queue.h,v 1.38 2013/07/03 15:05:21 fgsch Exp $ */
|
/* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert Exp $ */
|
||||||
/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
|
/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -32,12 +32,17 @@
|
|||||||
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef _SYS_QUEUE_H_
|
#ifndef _SYS_QUEUE_H_
|
||||||
#define _SYS_QUEUE_H_
|
#define _SYS_QUEUE_H_
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file defines five types of data structures: singly-linked lists,
|
* This file defines five types of data structures: singly-linked lists,
|
||||||
* lists, simple queues, tail queues, and circular queues.
|
* lists, simple queues, tail queues and XOR simple queues.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* A singly-linked list is headed by a single forward pointer. The elements
|
* A singly-linked list is headed by a single forward pointer. The elements
|
||||||
@@ -57,7 +62,7 @@
|
|||||||
* or after an existing element or at the head of the list. A list
|
* or after an existing element or at the head of the list. A list
|
||||||
* may only be traversed in the forward direction.
|
* may only be traversed in the forward direction.
|
||||||
*
|
*
|
||||||
* A simple queue is headed by a pair of pointers, one the head of the
|
* A simple queue is headed by a pair of pointers, one to the head of the
|
||||||
* list and the other to the tail of the list. The elements are singly
|
* list and the other to the tail of the list. The elements are singly
|
||||||
* linked to save space, so elements can only be removed from the
|
* linked to save space, so elements can only be removed from the
|
||||||
* head of the list. New elements can be added to the list before or after
|
* head of the list. New elements can be added to the list before or after
|
||||||
@@ -71,13 +76,10 @@
|
|||||||
* after an existing element, at the head of the list, or at the end of
|
* after an existing element, at the head of the list, or at the end of
|
||||||
* the list. A tail queue may be traversed in either direction.
|
* the list. A tail queue may be traversed in either direction.
|
||||||
*
|
*
|
||||||
* A circle queue is headed by a pair of pointers, one to the head of the
|
* An XOR simple queue is used in the same way as a regular simple queue.
|
||||||
* list and the other to the tail of the list. The elements are doubly
|
* The difference is that the head structure also includes a "cookie" that
|
||||||
* linked so that an arbitrary element can be removed without a need to
|
* is XOR'd with the queue pointer (first, last or next) to generate the
|
||||||
* traverse the list. New elements can be added to the list before or after
|
* real pointer value.
|
||||||
* an existing element, at the head of the list, or at the end of the list.
|
|
||||||
* A circle queue may be traversed in either direction, but has a more
|
|
||||||
* complex end of list detection.
|
|
||||||
*
|
*
|
||||||
* For details on the use of these macros, see the queue(3) manual page.
|
* For details on the use of these macros, see the queue(3) manual page.
|
||||||
*/
|
*/
|
||||||
@@ -95,15 +97,15 @@
|
|||||||
struct name { \
|
struct name { \
|
||||||
struct type *slh_first; /* first element */ \
|
struct type *slh_first; /* first element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SLIST_HEAD_INITIALIZER(head) \
|
#define SLIST_HEAD_INITIALIZER(head) \
|
||||||
{ NULL }
|
{ NULL }
|
||||||
|
|
||||||
#define SLIST_ENTRY(type) \
|
#define SLIST_ENTRY(type) \
|
||||||
struct { \
|
struct { \
|
||||||
struct type *sle_next; /* next element */ \
|
struct type *sle_next; /* next element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Singly-linked List access methods.
|
* Singly-linked List access methods.
|
||||||
*/
|
*/
|
||||||
@@ -157,8 +159,8 @@ struct { \
|
|||||||
curelm = curelm->field.sle_next; \
|
curelm = curelm->field.sle_next; \
|
||||||
curelm->field.sle_next = \
|
curelm->field.sle_next = \
|
||||||
curelm->field.sle_next->field.sle_next; \
|
curelm->field.sle_next->field.sle_next; \
|
||||||
_Q_INVALIDATE((elm)->field.sle_next); \
|
|
||||||
} \
|
} \
|
||||||
|
_Q_INVALIDATE((elm)->field.sle_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -179,7 +181,7 @@ struct { \
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* List access methods
|
* List access methods.
|
||||||
*/
|
*/
|
||||||
#define LIST_FIRST(head) ((head)->lh_first)
|
#define LIST_FIRST(head) ((head)->lh_first)
|
||||||
#define LIST_END(head) NULL
|
#define LIST_END(head) NULL
|
||||||
@@ -316,6 +318,14 @@ struct { \
|
|||||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define SIMPLEQ_CONCAT(head1, head2) do { \
|
||||||
|
if (!SIMPLEQ_EMPTY((head2))) { \
|
||||||
|
*(head1)->sqh_last = (head2)->sqh_first; \
|
||||||
|
(head1)->sqh_last = (head2)->sqh_last; \
|
||||||
|
SIMPLEQ_INIT((head2)); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* XOR Simple queue definitions.
|
* XOR Simple queue definitions.
|
||||||
*/
|
*/
|
||||||
@@ -395,7 +405,7 @@ struct { \
|
|||||||
XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Tail queue definitions.
|
* Tail queue definitions.
|
||||||
*/
|
*/
|
||||||
@@ -414,8 +424,8 @@ struct { \
|
|||||||
struct type **tqe_prev; /* address of previous next element */ \
|
struct type **tqe_prev; /* address of previous next element */ \
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* tail queue access methods
|
* Tail queue access methods.
|
||||||
*/
|
*/
|
||||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||||
#define TAILQ_END(head) NULL
|
#define TAILQ_END(head) NULL
|
||||||
@@ -516,133 +526,17 @@ struct { \
|
|||||||
_Q_INVALIDATE((elm)->field.tqe_next); \
|
_Q_INVALIDATE((elm)->field.tqe_next); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/*
|
#define TAILQ_CONCAT(head1, head2, field) do { \
|
||||||
* Circular queue definitions.
|
if (!TAILQ_EMPTY(head2)) { \
|
||||||
*/
|
*(head1)->tqh_last = (head2)->tqh_first; \
|
||||||
#define CIRCLEQ_HEAD(name, type) \
|
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
||||||
struct name { \
|
(head1)->tqh_last = (head2)->tqh_last; \
|
||||||
struct type *cqh_first; /* first element */ \
|
TAILQ_INIT((head2)); \
|
||||||
struct type *cqh_last; /* last element */ \
|
} \
|
||||||
}
|
|
||||||
|
|
||||||
#define CIRCLEQ_HEAD_INITIALIZER(head) \
|
|
||||||
{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
|
|
||||||
|
|
||||||
#define CIRCLEQ_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *cqe_next; /* next element */ \
|
|
||||||
struct type *cqe_prev; /* previous element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Circular queue access methods
|
|
||||||
*/
|
|
||||||
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
|
|
||||||
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
|
|
||||||
#define CIRCLEQ_END(head) ((void *)(head))
|
|
||||||
#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
|
|
||||||
#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
|
|
||||||
#define CIRCLEQ_EMPTY(head) \
|
|
||||||
(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH(var, head, field) \
|
|
||||||
for((var) = CIRCLEQ_FIRST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head); \
|
|
||||||
(var) = CIRCLEQ_NEXT(var, field))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = CIRCLEQ_FIRST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head) && \
|
|
||||||
((tvar) = CIRCLEQ_NEXT(var, field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
|
|
||||||
for((var) = CIRCLEQ_LAST(head); \
|
|
||||||
(var) != CIRCLEQ_END(head); \
|
|
||||||
(var) = CIRCLEQ_PREV(var, field))
|
|
||||||
|
|
||||||
#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
|
||||||
for ((var) = CIRCLEQ_LAST(head, headname); \
|
|
||||||
(var) != CIRCLEQ_END(head) && \
|
|
||||||
((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Circular queue functions.
|
|
||||||
*/
|
|
||||||
#define CIRCLEQ_INIT(head) do { \
|
|
||||||
(head)->cqh_first = CIRCLEQ_END(head); \
|
|
||||||
(head)->cqh_last = CIRCLEQ_END(head); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
|
|
||||||
(elm)->field.cqe_prev = (listelm); \
|
|
||||||
if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
else \
|
|
||||||
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
|
|
||||||
(listelm)->field.cqe_next = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (listelm); \
|
|
||||||
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
|
|
||||||
if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
else \
|
|
||||||
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
|
|
||||||
(listelm)->field.cqe_prev = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = (head)->cqh_first; \
|
|
||||||
(elm)->field.cqe_prev = CIRCLEQ_END(head); \
|
|
||||||
if ((head)->cqh_last == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
else \
|
|
||||||
(head)->cqh_first->field.cqe_prev = (elm); \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
|
|
||||||
(elm)->field.cqe_next = CIRCLEQ_END(head); \
|
|
||||||
(elm)->field.cqe_prev = (head)->cqh_last; \
|
|
||||||
if ((head)->cqh_first == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm); \
|
|
||||||
else \
|
|
||||||
(head)->cqh_last->field.cqe_next = (elm); \
|
|
||||||
(head)->cqh_last = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_REMOVE(head, elm, field) do { \
|
|
||||||
if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm)->field.cqe_prev; \
|
|
||||||
else \
|
|
||||||
(elm)->field.cqe_next->field.cqe_prev = \
|
|
||||||
(elm)->field.cqe_prev; \
|
|
||||||
if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm)->field.cqe_next; \
|
|
||||||
else \
|
|
||||||
(elm)->field.cqe_prev->field.cqe_next = \
|
|
||||||
(elm)->field.cqe_next; \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_prev); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_next); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
|
|
||||||
if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
|
|
||||||
CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_last = (elm2); \
|
|
||||||
else \
|
|
||||||
(elm2)->field.cqe_next->field.cqe_prev = (elm2); \
|
|
||||||
if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
|
|
||||||
CIRCLEQ_END(head)) \
|
|
||||||
(head)->cqh_first = (elm2); \
|
|
||||||
else \
|
|
||||||
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_prev); \
|
|
||||||
_Q_INVALIDATE((elm)->field.cqe_next); \
|
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#endif /* !_SYS_QUEUE_H_ */
|
#endif /* !_SYS_QUEUE_H_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
|
||||||
|
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
|
||||||
|
*/
|
||||||
|
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
|
||||||
|
|
||||||
|
void *
|
||||||
|
reallocarray(void *optr, size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
|
||||||
|
nmemb > 0 && SIZE_MAX / nmemb < size) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return realloc(optr, size * nmemb);
|
||||||
|
}
|
||||||
+52
-118
@@ -1,6 +1,6 @@
|
|||||||
/* Micro "rsync" implementation.
|
/* Micro "rsync" implementation.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2011, 2012 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2011-2020 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
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h> /* AT_* macros */
|
||||||
#include <stdlib.h> /* NULL, free() */
|
#include <stdlib.h> /* NULL, free() */
|
||||||
#include <string.h> /* strlen() */
|
#include <string.h> /* strlen() */
|
||||||
#include <strings.h> /* rindex() */
|
#include <strings.h> /* rindex() */
|
||||||
@@ -26,16 +27,17 @@
|
|||||||
|
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
|
|
||||||
static int copy(char *src, char *dst);
|
static int copy(char *src, char *dst, int opt);
|
||||||
static int mdir(char *buf, size_t buf_len, char *dir, char *name, mode_t mode);
|
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st);
|
||||||
static int prune(char *dst, char **new_files, int new_num);
|
static int prune(char *dst, char **new_files, int new_num);
|
||||||
|
static int set_mtime(char *fn, struct stat *st);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rsync - Synchronize contents and optionally remove non-existing backups
|
* rsync - Synchronize contents and optionally remove non-existing backups
|
||||||
* @src: Source directory
|
* @src: Source directory
|
||||||
* @dst: Destination directory
|
* @dst: Destination directory
|
||||||
* @delete: Prune files from @dst that no longer exist in @src.
|
* @opt: An option mask of %LITE_FOPT_RSYNC_DELETE, %LITE_FOPT_KEEP_MTIME
|
||||||
* @filter: Optional filtering function for source directory.
|
* @filter: Optional filtering function for source directory.
|
||||||
*
|
*
|
||||||
* This is a miniature implementation of the famous rsync for local use only.
|
* This is a miniature implementation of the famous rsync for local use only.
|
||||||
@@ -44,19 +46,29 @@ static int prune(char *dst, char **new_files, int new_num);
|
|||||||
* files removed from src since last backup are pruned from the destination
|
* files removed from src since last backup are pruned from the destination
|
||||||
* (backup) directory.
|
* (backup) directory.
|
||||||
*
|
*
|
||||||
* The filter callback, @filter, if provided, is used to determine what files to
|
* The @opt parameter to rsync() is an option mask for the most common
|
||||||
* include from the source directory when backing up. If a file is to be skipped
|
* rsync(1) options. Previously this argument was called @delete and
|
||||||
* the callback should simply return zero.
|
* to maintain backwards compatibility the value 1 is reserved:
|
||||||
|
*
|
||||||
|
* %LITE_FOPT_RSYNC_DELETE: Prune files from @dst that no longer exist in @src.
|
||||||
|
* %LITE_FOPT_KEEP_MTIME: Preserve modification time
|
||||||
|
*
|
||||||
|
* The filter callback, @filter, if provided, is used to determine what
|
||||||
|
* files to include from the source directory when backing up. If a
|
||||||
|
* file is to be skipped the callback should simply return zero.
|
||||||
*
|
*
|
||||||
* 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 rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
|
||||||
{
|
{
|
||||||
char source[256];
|
char source[256];
|
||||||
char dest[256];
|
char dest[256];
|
||||||
int i = 0, num = 0, result = 0;
|
int delete = (opt & LITE_FOPT_RSYNC_DELETE) != 0;
|
||||||
|
int keep_mtim = (opt & LITE_FOPT_KEEP_MTIME);
|
||||||
|
int i = 0, num = 0, result = 0, do_mtim = 0;
|
||||||
char **files; /* Array of file names. */
|
char **files; /* Array of file names. */
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
if (!fisdir(dst))
|
if (!fisdir(dst))
|
||||||
makedir(dst, 0755);
|
makedir(dst, 0755);
|
||||||
@@ -65,7 +77,7 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
if (!fexist(src))
|
if (!fexist(src))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (copy(src, dst))
|
if (copy(src, dst, keep_mtim))
|
||||||
result++;
|
result++;
|
||||||
|
|
||||||
return errno;
|
return errno;
|
||||||
@@ -80,9 +92,13 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
else
|
else
|
||||||
ptr++;
|
ptr++;
|
||||||
|
|
||||||
if (mdir(dest, sizeof(dest), dst, ptr, fmode(src)))
|
if (stat(src, &st))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (mdir(dest, sizeof(dest), dst, ptr, &st))
|
||||||
return 1;
|
return 1;
|
||||||
dst = dest;
|
dst = dest;
|
||||||
|
do_mtim = keep_mtim;
|
||||||
}
|
}
|
||||||
|
|
||||||
num = dir(src, "", filter, &files, 0);
|
num = dir(src, "", filter, &files, 0);
|
||||||
@@ -91,21 +107,31 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
snprintf(source, sizeof(source), "%s%s%s", src, fisslashdir(src) ? "" : "/", files[i]);
|
snprintf(source, sizeof(source), "%s%s%s", src, fisslashdir(src) ? "" : "/", files[i]);
|
||||||
if (fisdir(source)) {
|
if (fisdir(source)) {
|
||||||
char dst2[256];
|
char dst2[256];
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
strcat(source, "/");
|
strcat(source, "/");
|
||||||
if (mdir (dst2, sizeof(dst2), dst, files[i], fmode(source))) {
|
if (stat(source, &st))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (mdir(dst2, sizeof(dst2), dst, files[i], &st)) {
|
||||||
result++;
|
result++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
rsync(source, dst2, delete, filter);
|
rsync(source, dst2, opt, filter);
|
||||||
|
if (keep_mtim)
|
||||||
|
set_mtime(dst2, &st);
|
||||||
|
|
||||||
continue; /* Next file/dir in @src to copy... */
|
continue; /* Next file/dir in @src to copy... */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (copy(source, dst))
|
if (copy(source, dst, keep_mtim))
|
||||||
result++;
|
result++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (do_mtim)
|
||||||
|
set_mtime(dest, &st);
|
||||||
|
|
||||||
/* 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
|
||||||
* cosmetic. --Jocke 2011-03-24 */
|
* cosmetic. --Jocke 2011-03-24 */
|
||||||
if (delete)
|
if (delete)
|
||||||
@@ -120,11 +146,9 @@ int rsync(char *src, char *dst, int delete, int (*filter) (const char *file))
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int copy(char *src, char *dst)
|
static int copy(char *src, char *dst, int opt)
|
||||||
{
|
{
|
||||||
errno = 0;
|
copyfile(src, dst, 0, opt | LITE_FOPT_COPYFILE_SYM);
|
||||||
|
|
||||||
copyfile(src, dst, 0, 1);
|
|
||||||
if (errno) {
|
if (errno) {
|
||||||
if (errno != EEXIST)
|
if (errno != EEXIST)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -136,10 +160,10 @@ static int copy(char *src, char *dst)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Creates dir/name @mode ... skipping / if dir already ends so. */
|
/* 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)
|
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st)
|
||||||
{
|
{
|
||||||
snprintf(buf, buf_len, "%s%s%s/", dir, fisslashdir(dir) ? "" : "/", name);
|
snprintf(buf, buf_len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name);
|
||||||
if (mkdir(buf, mode)) {
|
if (mkdir(buf, st->st_mode)) {
|
||||||
if (EEXIST != errno)
|
if (EEXIST != errno)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
@@ -193,108 +217,18 @@ static int prune(char *dst, char **new_files, int new_num)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITTEST
|
static int set_mtime(char *fn, struct stat *st)
|
||||||
#define BASE "/tmp/.unittest/"
|
|
||||||
#define SRC BASE "src/"
|
|
||||||
#define DST BASE "dst/"
|
|
||||||
|
|
||||||
char *files[] = {
|
|
||||||
SRC "sub1/1.tst",
|
|
||||||
SRC "sub1/2.tst",
|
|
||||||
SRC "sub1/3.tst",
|
|
||||||
SRC "sub2/4.tst",
|
|
||||||
SRC "sub2/5.tst",
|
|
||||||
SRC "sub2/6.tst",
|
|
||||||
SRC "sub3/7.tst",
|
|
||||||
SRC "sub3/8.tst",
|
|
||||||
SRC "sub3/9.tst",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
void cleanup_test(void)
|
|
||||||
{
|
{
|
||||||
system("rm -rf " BASE);
|
struct timespec tv[2];
|
||||||
|
|
||||||
|
tv[0] = st->st_atim;
|
||||||
|
tv[1] = st->st_mtim;
|
||||||
|
|
||||||
|
return utimensat(AT_FDCWD, fn, tv, AT_SYMLINK_NOFOLLOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_test(void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char cmd[256];
|
|
||||||
mode_t dir_modes[] = { 755, 700 };
|
|
||||||
mode_t file_modes[] = { 644, 600 };
|
|
||||||
|
|
||||||
cleanup_test();
|
|
||||||
|
|
||||||
mkdir(BASE, 0755);
|
|
||||||
mkdir(SRC, 0755);
|
|
||||||
mkdir(DST, 0755);
|
|
||||||
|
|
||||||
for (i = 0; files[i]; i++) {
|
|
||||||
snprintf(cmd, sizeof(cmd), "mkdir -m %d -p `dirname %s`",
|
|
||||||
dir_modes[i % 2], files[i]);
|
|
||||||
system(cmd);
|
|
||||||
|
|
||||||
snprintf(cmd, sizeof(cmd), "touch %s; chmod %d %s", files[i],
|
|
||||||
file_modes[i % 2], files[i]);
|
|
||||||
system(cmd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int run_test()
|
|
||||||
{
|
|
||||||
#if 0
|
|
||||||
setup_test();
|
|
||||||
puts("Before:");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
rsync(SRC, DST, 0, NULL);
|
|
||||||
puts("After:");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
cleanup_test();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
setup_test();
|
|
||||||
rsync(BASE "src", DST, 0, NULL);
|
|
||||||
puts("Only partial rsync of src <-- No slash!");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
#if 0
|
|
||||||
cleanup_test();
|
|
||||||
setup_test();
|
|
||||||
rsync(BASE "src/sub1", BASE "dst", 0, NULL);
|
|
||||||
puts("Only partial rsync of src/sub1 <-- No slashes!!");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
cleanup_test();
|
|
||||||
setup_test();
|
|
||||||
rsync(BASE "src/sub1/", DST, 0, NULL);
|
|
||||||
puts("Only partial rsync of src/sub1/");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
cleanup_test();
|
|
||||||
setup_test();
|
|
||||||
rsync(BASE "src/sub1", DST, 0, NULL);
|
|
||||||
puts("Only partial rsync of src/sub1 <-- No slash!");
|
|
||||||
system("tree -np " BASE);
|
|
||||||
|
|
||||||
rsync("/etc", "/var/tmp", 0, NULL);
|
|
||||||
puts("Real life test:");
|
|
||||||
system("tree -np /var/tmp");
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
atexit(cleanup_test);
|
|
||||||
|
|
||||||
return run_test();
|
|
||||||
}
|
|
||||||
#endif /* UNITTEST */
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "make -f rsync.mk"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/* ==========================================================================
|
||||||
|
* strdupa.h - Re-implementation of glibc strdupa.
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Copyright (c) 2009 William Ahern
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
* ==========================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LITE_STRDUPA_H
|
||||||
|
#define LITE_STRDUPA_H
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRDUPA)
|
||||||
|
#if defined(strdupa)
|
||||||
|
#define HAVE_STRDUPA 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !HAVE_STRDUPA
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
#include <string.h> /* memcpy(3) strlen(3) */
|
||||||
|
|
||||||
|
#define strdupa(src) (__extension__ ({ \
|
||||||
|
size_t len_ = strlen(src); \
|
||||||
|
char *dst_ = __builtin_alloca(len_ + 1); \
|
||||||
|
dst_[len_] = '\0'; \
|
||||||
|
(char *)memcpy(dst_, src, len_); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#else /* If not GCC, e.g. Clang */
|
||||||
|
#error strdupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
#endif /* !HAVE_STRDUPA */
|
||||||
|
|
||||||
|
#endif /* LITE_STRDUPA_H */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+24
-33
@@ -1,7 +1,7 @@
|
|||||||
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
|
/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -21,46 +21,37 @@
|
|||||||
|
|
||||||
#ifndef strlcat
|
#ifndef strlcat
|
||||||
/*
|
/*
|
||||||
* Appends src to string dst of size siz (unlike strncat, siz is the
|
* Appends src to string dst of size dsize (unlike strncat, dsize is the
|
||||||
* full size of dst, not space left). At most siz-1 characters
|
* full size of dst, not space left). At most dsize-1 characters
|
||||||
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
|
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
|
||||||
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
|
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
|
||||||
* If retval >= siz, truncation occurred.
|
* If retval >= dsize, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcat(char *dst, const char *src, size_t siz)
|
strlcat(char *dst, const char *src, size_t dsize)
|
||||||
{
|
{
|
||||||
char *d = dst;
|
const char *odst = dst;
|
||||||
const char *s = src;
|
const char *osrc = src;
|
||||||
size_t n = siz;
|
size_t n = dsize;
|
||||||
size_t dlen;
|
size_t dlen;
|
||||||
|
|
||||||
/* Find the end of dst and adjust bytes left but don't go past end */
|
/* Find the end of dst and adjust bytes left but don't go past end. */
|
||||||
while (n-- != 0 && *d != '\0')
|
while (n-- != 0 && *dst != '\0')
|
||||||
d++;
|
dst++;
|
||||||
dlen = d - dst;
|
dlen = dst - odst;
|
||||||
n = siz - dlen;
|
n = dsize - dlen;
|
||||||
|
|
||||||
if (n == 0)
|
if (n-- == 0)
|
||||||
return(dlen + strlen(s));
|
return(dlen + strlen(src));
|
||||||
while (*s != '\0') {
|
while (*src != '\0') {
|
||||||
if (n != 1) {
|
if (n != 0) {
|
||||||
*d++ = *s;
|
*dst++ = *src;
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
s++;
|
src++;
|
||||||
}
|
}
|
||||||
*d = '\0';
|
*dst = '\0';
|
||||||
|
|
||||||
return(dlen + (s - src)); /* count does not include NUL */
|
return(dlen + (src - osrc)); /* count does not include NUL */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* c-file-style: "bsd"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
+18
-27
@@ -1,7 +1,7 @@
|
|||||||
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
|
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
@@ -21,41 +21,32 @@
|
|||||||
|
|
||||||
#ifndef strlcpy
|
#ifndef strlcpy
|
||||||
/*
|
/*
|
||||||
* Copy src to string dst of size siz. At most siz-1 characters
|
* Copy string src to buffer dst of size dsize. At most dsize-1
|
||||||
* will be copied. Always NUL terminates (unless siz == 0).
|
* chars will be copied. Always NUL terminates (unless dsize == 0).
|
||||||
* Returns strlen(src); if retval >= siz, truncation occurred.
|
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcpy(char *dst, const char *src, size_t siz)
|
strlcpy(char *dst, const char *src, size_t dsize)
|
||||||
{
|
{
|
||||||
char *d = dst;
|
const char *osrc = src;
|
||||||
const char *s = src;
|
size_t nleft = dsize;
|
||||||
size_t n = siz;
|
|
||||||
|
|
||||||
/* Copy as many bytes as will fit */
|
/* Copy as many bytes as will fit. */
|
||||||
if (n != 0) {
|
if (nleft != 0) {
|
||||||
while (--n != 0) {
|
while (--nleft != 0) {
|
||||||
if ((*d++ = *s++) == '\0')
|
if ((*dst++ = *src++) == '\0')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not enough room in dst, add NUL and traverse rest of src */
|
/* Not enough room in dst, add NUL and traverse rest of src. */
|
||||||
if (n == 0) {
|
if (nleft == 0) {
|
||||||
if (siz != 0)
|
if (dsize != 0)
|
||||||
*d = '\0'; /* NUL-terminate dst */
|
*dst = '\0'; /* NUL-terminate dst */
|
||||||
while (*s++)
|
while (*src++)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return(s - src - 1); /* count does not include NUL */
|
return(src - osrc - 1); /* count does not include NUL */
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* Local Variables:
|
|
||||||
* version-control: t
|
|
||||||
* c-file-style: "bsd"
|
|
||||||
* End:
|
|
||||||
*/
|
|
||||||
+117
@@ -0,0 +1,117 @@
|
|||||||
|
/* Collection of frog DNA
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008-2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LITE_STRING_H_
|
||||||
|
#define LITE_STRING_H_
|
||||||
|
|
||||||
|
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||||
|
|
||||||
|
#include "strdupa.h"
|
||||||
|
#include "strndupa.h"
|
||||||
|
#include "strnlen.h"
|
||||||
|
|
||||||
|
#ifndef min
|
||||||
|
#define min(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a < _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
#endif
|
||||||
|
#ifndef max
|
||||||
|
#define max(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a > _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int strnmatch (const char *str, const char **list, size_t num);
|
||||||
|
int strmatch (const char *str, const char **list);
|
||||||
|
|
||||||
|
#ifndef strlcpy
|
||||||
|
size_t strlcpy (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strlcat
|
||||||
|
size_t strlcat (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strtonum
|
||||||
|
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Convert string to natural number (0-2147483647), returns -1 on error. */
|
||||||
|
static inline int atonum(const char *str)
|
||||||
|
{
|
||||||
|
int val = -1;
|
||||||
|
const char *errstr;
|
||||||
|
|
||||||
|
if (str) {
|
||||||
|
val = strtonum(str, 0, INT32_MAX, &errstr);
|
||||||
|
if (errstr)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Validate string, non NULL and not zero length */
|
||||||
|
static inline int string_valid(const char *s)
|
||||||
|
{
|
||||||
|
return s && strlen(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Relaxed comparison, e.g., sys_string_match("small", "smaller") => TRUE */
|
||||||
|
static inline int string_match(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
size_t min = MIN(strlen(a), strlen(b));
|
||||||
|
|
||||||
|
return !strncasecmp(a, b, min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, e.g., sys_string_match("small", "smaller") => FALSE */
|
||||||
|
static inline int string_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen(a) == strlen(b) && !strcmp(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, like sys_string_compare(), but case insensitive,
|
||||||
|
* e.g., sys_string_match("small", "SmAlL") => TRUE
|
||||||
|
*/
|
||||||
|
static inline int string_case_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen (a) == strlen (b) && !strcasecmp (a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LITE_STRING_H_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
/* Simple string matcher function, finds partial matches.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2020 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>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* strnmatch - Finds matching strings from a finite list
|
||||||
|
* @str: String to look for
|
||||||
|
* @list: List of strings to search.
|
||||||
|
* @num: Number of entries in @list.
|
||||||
|
*
|
||||||
|
* This function searches the @list of strings for @str. If a (partial) match
|
||||||
|
* is found it returns the index in the @list.
|
||||||
|
*
|
||||||
|
* Very similar in function to strmatch(), but works for sets of strings that
|
||||||
|
* are not %NULL terminated.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* -1 on error, otherwise the index to the matching string.
|
||||||
|
*/
|
||||||
|
int strnmatch(const char *str, const char **list, size_t num)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!str || !list) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < num; i++) {
|
||||||
|
if (!strncasecmp (str, list[i], strlen (str)))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* strmatch - Finds matching strings from a list
|
||||||
|
* @str: String to look for
|
||||||
|
* @list: %NULL terminated list of strings to search.
|
||||||
|
*
|
||||||
|
* This function searches the @list of strings for @str. If a (partial) match
|
||||||
|
* is found it returns the index in the @list.
|
||||||
|
*
|
||||||
|
* Please note, the @list MUST be terminated by a %NULL element. If that is
|
||||||
|
* not possible for you, we recommend using strnmatch() instead.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* -1 on error, otherwise the index to the matching string.
|
||||||
|
*/
|
||||||
|
int strmatch(const char *str, const char **list)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!list) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; list[i]; i++)
|
||||||
|
;
|
||||||
|
|
||||||
|
return strnmatch(str, list, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/* ==========================================================================
|
||||||
|
* strndupa.h - Re-implementation of glibc strndupa.
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Copyright (c) 2009 William Ahern
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
* ==========================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LITE_STRNDUPA_H_
|
||||||
|
#define LITE_STRNDUPA_H_
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRNDUPA)
|
||||||
|
#if defined(strndupa)
|
||||||
|
#define HAVE_STRNDUPA 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !HAVE_STRNDUPA
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
#include <string.h> /* memcpy(3) */
|
||||||
|
#include "strnlen.h"
|
||||||
|
|
||||||
|
#define strndupa(src, lim) (__extension__ ({ \
|
||||||
|
size_t len_ = strnlen(src, lim); \
|
||||||
|
char *dst_ = __builtin_alloca(len_ + 1); \
|
||||||
|
dst_[len_] = '\0'; \
|
||||||
|
(char *)memcpy(dst_, src, len_); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#else /* If not GCC, e.g. Clang */
|
||||||
|
+#error strndupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
#endif /* !HAVE_STRNDUPA */
|
||||||
|
|
||||||
|
#endif /* GNU_STRING_STRNDUPA_H */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/* strnlen.h - Re-implementation of GLIBC strnlen()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2016-2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LITE_STRNLEN_H_
|
||||||
|
#define LITE_STRNLEN_H_
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRNLEN)
|
||||||
|
#if defined(strnlen)
|
||||||
|
#define HAVE_STRNLEN 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !HAVE_STRNLEN
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
#define strnlen(str, lim) xstrnlen(str, lim)
|
||||||
|
|
||||||
|
static inline size_t xstrnlen(const char *str, size_t lim)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (str[i] && i < lim)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* LITE_STRNLEN_H_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
|
/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
||||||
@@ -22,17 +22,26 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifndef strtonum
|
#ifndef strtonum
|
||||||
#define INVALID 1
|
#define INVALID 1
|
||||||
#define TOOSMALL 2
|
#define TOOSMALL 2
|
||||||
#define TOOLARGE 3
|
#define TOOLARGE 3
|
||||||
|
|
||||||
|
#ifndef LLONG_MAX
|
||||||
|
# define LLONG_MAX 0x7fffffffffffffffLL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LLONG_MIN
|
||||||
|
# define LLONG_MIN (-0x7fffffffffffffffLL - 1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
long long
|
long long
|
||||||
strtonum(const char *numstr, long long minval, long long maxval,
|
strtonum(const char *numstr, long long minval, long long maxval,
|
||||||
const char **errstrp)
|
const char **errstrp)
|
||||||
{
|
{
|
||||||
long long ll = 0;
|
long long ll = 0;
|
||||||
char *ep;
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
char *ep;
|
||||||
struct errval {
|
struct errval {
|
||||||
const char *errstr;
|
const char *errstr;
|
||||||
int err;
|
int err;
|
||||||
@@ -45,11 +54,11 @@ strtonum(const char *numstr, long long minval, long long maxval,
|
|||||||
|
|
||||||
ev[0].err = errno;
|
ev[0].err = errno;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (minval > maxval)
|
if (minval > maxval) {
|
||||||
error = INVALID;
|
error = INVALID;
|
||||||
else {
|
} else {
|
||||||
ll = strtoll(numstr, &ep, 10);
|
ll = strtoll(numstr, &ep, 10);
|
||||||
if (numstr == ep || *ep != '\0')
|
if (errno == EINVAL || numstr == ep || *ep != '\0')
|
||||||
error = INVALID;
|
error = INVALID;
|
||||||
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
|
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
|
||||||
error = TOOSMALL;
|
error = TOOSMALL;
|
||||||
+291
@@ -0,0 +1,291 @@
|
|||||||
|
/* Poor mans telnet expect in C.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010-2020 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 <poll.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
#define EXPECT_DEBUG 0
|
||||||
|
#define EXPECT_POLL_TIMEOUT 3000 /* 3 sec timeout waiting for server. */
|
||||||
|
|
||||||
|
#if EXPECT_DEBUG
|
||||||
|
# define TELL(fmt, args...) fprintf (stderr, fmt "\n", ##args)
|
||||||
|
#else
|
||||||
|
# define TELL(fmt, args...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct sdbuf {
|
||||||
|
int sd;
|
||||||
|
char *buf;
|
||||||
|
} sdbuf;
|
||||||
|
|
||||||
|
/* Open telnet connection to @addr:@port and connect. */
|
||||||
|
sdbuf_t *telnet_open(int addr, short port)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *sin;
|
||||||
|
struct sockaddr s;
|
||||||
|
sdbuf_t *ctx;
|
||||||
|
int saved_errno;
|
||||||
|
|
||||||
|
ctx = (sdbuf_t *)malloc(sizeof(sdbuf_t));
|
||||||
|
if (!ctx) {
|
||||||
|
// perror("Failed allocating expect ctxect");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->buf = (char *)malloc(BUFSIZ);
|
||||||
|
if (!ctx->buf) {
|
||||||
|
// perror("Failed allocating expect buffer");
|
||||||
|
free(ctx);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->sd = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
if (-1 == ctx->sd) {
|
||||||
|
saved_errno = errno;
|
||||||
|
// perror ("Failed creating socket");
|
||||||
|
free(ctx->buf);
|
||||||
|
free(ctx);
|
||||||
|
|
||||||
|
errno = saved_errno;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
sin = (struct sockaddr_in *)&s;
|
||||||
|
sin->sin_family = AF_INET;
|
||||||
|
sin->sin_port = port;
|
||||||
|
sin->sin_addr.s_addr = addr;
|
||||||
|
if (-1 == connect(ctx->sd, &s, sizeof(s))) {
|
||||||
|
saved_errno = errno;
|
||||||
|
// perror ("Cannot connect to telnet daemon");
|
||||||
|
telnet_close(ctx);
|
||||||
|
errno = saved_errno;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
int telnet_close(sdbuf_t *ctx)
|
||||||
|
{
|
||||||
|
free(ctx->buf);
|
||||||
|
close(ctx->sd);
|
||||||
|
free(ctx);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int wait_substr(sdbuf_t *ctx, char *str)
|
||||||
|
{
|
||||||
|
struct pollfd pfd = { ctx->sd, POLLIN, 0 };
|
||||||
|
ssize_t len, tot = 0;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
err = poll(&pfd, 1, EXPECT_POLL_TIMEOUT);
|
||||||
|
if (err <= 0) {
|
||||||
|
if (0 == err)
|
||||||
|
errno = ETIMEDOUT;
|
||||||
|
|
||||||
|
if (EINTR == errno)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// perror("No prompt");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = read(ctx->sd, &ctx->buf[tot], BUFSIZ);
|
||||||
|
if (len <= 0) {
|
||||||
|
errno = ENOMSG;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
tot += len;
|
||||||
|
ctx->buf[tot] = 0;
|
||||||
|
TELL("Got line: '%s' is it '%s'?", ctx->buf, str);
|
||||||
|
|
||||||
|
if (strstr(ctx->buf, str))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* telnet_expect - Poor man's telnet expect
|
||||||
|
* @ctx: Telnet session context from telnet_open()
|
||||||
|
* @script: %NULL terminated list of expect and response strings
|
||||||
|
* @output: Optional output from session
|
||||||
|
*
|
||||||
|
* Issues @script sequence on telnet session specified in @ctx, with
|
||||||
|
* optional @output from session.
|
||||||
|
*
|
||||||
|
* The @script consists of strings of expect and response pairs. For
|
||||||
|
* example, expect "ogin: " followed by response "root\n", expect
|
||||||
|
* "assword: "with response "secret\n", concluded by %NULL.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0) or non-zero on error.
|
||||||
|
*/
|
||||||
|
int telnet_expect(sdbuf_t *ctx, char *script[], FILE *output)
|
||||||
|
{
|
||||||
|
struct pollfd fd = { ctx->sd, POLLIN, 0 };
|
||||||
|
ssize_t len;
|
||||||
|
int i, first = 1, cont = 1, result = 0;
|
||||||
|
|
||||||
|
for (i = 0; script[i]; i++) {
|
||||||
|
/* when expecting the empty string, just wipe any available input */
|
||||||
|
if (!strlen(script[i])) {
|
||||||
|
result = poll(&fd, 1, 0);
|
||||||
|
if (result > 0)
|
||||||
|
lseek(ctx->sd, 0, SEEK_END);
|
||||||
|
} else {
|
||||||
|
if (wait_substr(ctx, script[i])) {
|
||||||
|
result = ENOMSG;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (strlen(script[i])) {
|
||||||
|
TELL("Sending line: '%s'", script[i]);
|
||||||
|
if (-1 == write(ctx->sd, script[i], strlen(script[i]))) {
|
||||||
|
errno = ENOSPC;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (cont) {
|
||||||
|
char *ptr = ctx->buf;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
result = poll(&fd, 1, EXPECT_POLL_TIMEOUT);
|
||||||
|
if (result <= 0) {
|
||||||
|
if (0 == result)
|
||||||
|
errno = ETIMEDOUT;
|
||||||
|
|
||||||
|
if (EINTR == errno)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// perror ("No result");
|
||||||
|
result = errno;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* On VERY long outputs we need to send some love back to the
|
||||||
|
* server, otherwise it terminates the connection. --Jocke
|
||||||
|
*/
|
||||||
|
if (-1 == write(ctx->sd, "\n", 1))
|
||||||
|
goto retry;
|
||||||
|
|
||||||
|
len = read(ctx->sd, ctx->buf, BUFSIZ);
|
||||||
|
if (len == -1) {
|
||||||
|
retry:
|
||||||
|
if (EINTR == errno)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
result = errno;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ctx->buf[len] = 0;
|
||||||
|
|
||||||
|
TELL("Read line: '%s'", ctx->buf);
|
||||||
|
|
||||||
|
/* Skip first line -- only an echo of the last command */
|
||||||
|
if (first) {
|
||||||
|
first = 0;
|
||||||
|
ptr = strchr(ctx->buf, '\n');
|
||||||
|
if (ptr)
|
||||||
|
ptr++;
|
||||||
|
else
|
||||||
|
ptr = ctx->buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
TELL("Comparing '%s' with '%s'", ptr, script[i - 2]);
|
||||||
|
|
||||||
|
/* Skip last line -- only the returning prompt. */
|
||||||
|
tmp = strstr(ptr, script[i - 2]);
|
||||||
|
if (tmp) {
|
||||||
|
TELL("OK, last line. Good bye!");
|
||||||
|
result = 0;
|
||||||
|
cont = 0;
|
||||||
|
*tmp = 0;
|
||||||
|
|
||||||
|
tmp = strrchr(ptr, '\n');
|
||||||
|
if (tmp)
|
||||||
|
*++tmp = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (output && *ptr) {
|
||||||
|
if (EOF == fputs(ptr, output)) {
|
||||||
|
/* Only happens when /tmp is full :( */
|
||||||
|
result = ENOSPC;
|
||||||
|
/* We must complete read-out of descriptor */
|
||||||
|
output = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* telnet_session - Very simple expect-like implementation for telnet.
|
||||||
|
* @addr: Must be in network byte order, use htonl().
|
||||||
|
* @port: IP port to connect to, use htons().
|
||||||
|
* @script: Expect like script of paired 'expect', 'response' strings.
|
||||||
|
* @output: A &FILE pointer to a tmpfile() for output from the last command.
|
||||||
|
*
|
||||||
|
* This is a very simple expect-like implementation for querying and
|
||||||
|
* operating daemons remotely over telnet.
|
||||||
|
*
|
||||||
|
* The @script is a query-response type of list of strings leading up to
|
||||||
|
* a final command, which output is then written to the given @output file.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* POSIX OK(0), or non-zero with errno set on error.
|
||||||
|
*/
|
||||||
|
int telnet_session(int addr, short port, char *script[], FILE *output)
|
||||||
|
{
|
||||||
|
sdbuf_t *sdbuf;
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
sdbuf = telnet_open(addr, port);
|
||||||
|
if (!sdbuf)
|
||||||
|
return errno;
|
||||||
|
|
||||||
|
if (telnet_expect(sdbuf, script, output))
|
||||||
|
result = errno;
|
||||||
|
|
||||||
|
telnet_close(sdbuf);
|
||||||
|
|
||||||
|
return errno = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/* A secure tmpfile() replacement.
|
/* A secure tmpfile() replacement.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015 Joachim Nilsson <troglobit@gmail.com>
|
* Copyright (c) 2015-2020 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
|
||||||
@@ -17,15 +17,9 @@
|
|||||||
|
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||||
#include <stdlib.h> /* mkstemp() */
|
|
||||||
#include <stdio.h> /* fdopen() */
|
#include <stdio.h> /* fdopen() */
|
||||||
#include <sys/stat.h> /* umask() */
|
#include <sys/stat.h> /* umask() */
|
||||||
|
|
||||||
#ifndef O_TMPFILE /* Too old GLIBC or kernel */
|
|
||||||
#warning O_TMPFILE missing on your system, tempfile() may not work!
|
|
||||||
#define O_TMPFILE 020200000 /* Define and let it fail at runtime */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tempfile - A secure tmpfile() replacement
|
* tempfile - A secure tmpfile() replacement
|
||||||
*
|
*
|
||||||
@@ -41,6 +35,7 @@
|
|||||||
*/
|
*/
|
||||||
FILE *tempfile(void)
|
FILE *tempfile(void)
|
||||||
{
|
{
|
||||||
|
#ifdef O_TMPFILE /* Only on Linux, with fairly recent (G)LIBC */
|
||||||
int fd;
|
int fd;
|
||||||
mode_t oldmask;
|
mode_t oldmask;
|
||||||
|
|
||||||
@@ -50,21 +45,14 @@ FILE *tempfile(void)
|
|||||||
if (-1 == fd)
|
if (-1 == fd)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return fdopen(fd, "rw");
|
return fdopen(fd, "w+");
|
||||||
}
|
#else
|
||||||
|
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
||||||
#ifdef UNITTEST
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
FILE *fp = tempfile(); system("ls -lrt "
|
|
||||||
_PATH_TMP " | tail -10"); return fclose(fp);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* compile-command: "gcc -D_GNU_SOURCE -DUNITTEST -o tempy tempfile.c && ./tempy"
|
|
||||||
* version-control: t
|
|
||||||
* indent-tabs-mode: t
|
* indent-tabs-mode: t
|
||||||
* c-file-style: "linux"
|
* c-file-style: "linux"
|
||||||
* End:
|
* End:
|
||||||
+757
@@ -0,0 +1,757 @@
|
|||||||
|
/* $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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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_ */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+127
@@ -0,0 +1,127 @@
|
|||||||
|
/* C implementation of UNIX which(1)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017-2020 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 <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h> /* snprintf() */
|
||||||
|
#include <stdlib.h> /* realloc(), free() */
|
||||||
|
#include <string.h> /* strtok_r() */
|
||||||
|
#include <unistd.h> /* access() */
|
||||||
|
|
||||||
|
/* Strip any arguments in "/path/to/bin --some args", modifies input string */
|
||||||
|
static char *strip_args(char *path)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* Find first whitespace (space/tab/etc.) */
|
||||||
|
while (path[i] && !isspace(path[i]))
|
||||||
|
i++;
|
||||||
|
path[i] = 0;
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Like which(1), returns a malloc'ed path to cmd on success, or NULL
|
||||||
|
*/
|
||||||
|
char *which(const char *cmd)
|
||||||
|
{
|
||||||
|
size_t pathlen = 0;
|
||||||
|
char *ptr, *tok, *env, *path = NULL;
|
||||||
|
|
||||||
|
if (!cmd) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd[0] == '/') {
|
||||||
|
path = strip_args(strdup(cmd));
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!access(path, X_OK))
|
||||||
|
return path;
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
free(path);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = getenv("PATH");
|
||||||
|
if (!ptr)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
env = strdup(ptr);
|
||||||
|
if (!env)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
tok = strtok_r(env, ":", &ptr);
|
||||||
|
while (tok) {
|
||||||
|
size_t len = strlen(tok) + strlen(cmd) + 2;
|
||||||
|
|
||||||
|
if (pathlen < len) {
|
||||||
|
path = realloc(path, len);
|
||||||
|
if (!path) {
|
||||||
|
free(env);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
pathlen = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(path, pathlen, "%s/%s", tok, cmd);
|
||||||
|
path = strip_args(path);
|
||||||
|
if (!path || !access(path, X_OK)) {
|
||||||
|
free(env);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
tok = strtok_r(NULL, ":", &ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
free(path);
|
||||||
|
free(env);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Like which(), above, but only answers TRUE(1)/FALSE(0)
|
||||||
|
*/
|
||||||
|
int whichp(const char *cmd)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
|
||||||
|
path = which(cmd);
|
||||||
|
if (path) {
|
||||||
|
free(path);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
/* Safe yes-or-no with prompt
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2020 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 <stdio.h>
|
||||||
|
#include <stdio_ext.h> /* __fpurge() */
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static char rawgetch(void)
|
||||||
|
{
|
||||||
|
struct termios savemodes, modmodes;
|
||||||
|
char val;
|
||||||
|
|
||||||
|
if (!isatty(STDIN_FILENO))
|
||||||
|
return getchar();
|
||||||
|
|
||||||
|
/* Backup terminal settings. */
|
||||||
|
if (tcgetattr(STDIN_FILENO, &savemodes) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "stty cbreak -echo" */
|
||||||
|
modmodes = savemodes;
|
||||||
|
modmodes.c_lflag &= ~ICANON;
|
||||||
|
modmodes.c_lflag &= ~ECHO;
|
||||||
|
modmodes.c_cc[VMIN] = 1;
|
||||||
|
modmodes.c_cc[VTIME] = 0;
|
||||||
|
|
||||||
|
/* Set terminal in raw mode. */
|
||||||
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &modmodes) < 0) {
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &savemodes);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
val = getchar();
|
||||||
|
|
||||||
|
/* Restore terminal to previous state. */
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &savemodes);
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* yorn - Pose a a Yes or No question and return answer
|
||||||
|
* @fmt: Standard printf() style argument(s).
|
||||||
|
*
|
||||||
|
* This function prints the given question on screen, waits for user
|
||||||
|
* input in the form of yes or no.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* True(1) or False(0). True if the answer is yes.
|
||||||
|
*/
|
||||||
|
int yorn(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
char yorn;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vfprintf(stderr, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
__fpurge(stdin);
|
||||||
|
yorn = rawgetch();
|
||||||
|
printf("%c\n", yorn);
|
||||||
|
fflush(stdout);
|
||||||
|
if (yorn != 'y' && yorn != 'Y')
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
*.o
|
||||||
|
*.log
|
||||||
|
*.trs
|
||||||
|
chomp
|
||||||
|
copyfile
|
||||||
|
dir
|
||||||
|
fexist
|
||||||
|
fisdir
|
||||||
|
fsendfile
|
||||||
|
lfile
|
||||||
|
makepath
|
||||||
|
pidfile
|
||||||
|
printhdr
|
||||||
|
progress
|
||||||
|
rsync
|
||||||
|
str
|
||||||
|
strmatch
|
||||||
|
tempfile
|
||||||
|
touch
|
||||||
|
tree
|
||||||
|
which
|
||||||
|
yorn
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
EXTRA_DIST = check.h
|
||||||
|
CLEANFILES = *~ *.trs *.log
|
||||||
|
|
||||||
|
TESTS =
|
||||||
|
TESTS += chomp
|
||||||
|
TESTS += copyfile
|
||||||
|
TESTS += dir
|
||||||
|
TESTS += fexist
|
||||||
|
TESTS += fisdir
|
||||||
|
TESTS += fsendfile
|
||||||
|
TESTS += lfile
|
||||||
|
TESTS += makepath
|
||||||
|
TESTS += pidfile
|
||||||
|
TESTS += printhdr
|
||||||
|
TESTS += progress
|
||||||
|
TESTS += rsync
|
||||||
|
TESTS += str
|
||||||
|
TESTS += strmatch
|
||||||
|
TESTS += touch
|
||||||
|
TESTS += tempfile
|
||||||
|
TESTS += which
|
||||||
|
TESTS += yorn
|
||||||
|
|
||||||
|
check_PROGRAMS = $(TESTS)
|
||||||
|
|
||||||
|
# Ignore warnings about unused result, in e.g. write()
|
||||||
|
CFLAGS += -Wno-unused-result
|
||||||
|
CPPFLAGS += -D_GNU_SOURCE
|
||||||
|
LDADD = -L../src/ ../src/libite.la
|
||||||
|
LDFLAGS += -static
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#ifndef LITE_TESTS_CHECK_H_
|
||||||
|
#define LITE_TESTS_CHECK_H_
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "../src/conio.h"
|
||||||
|
#include "../src/lite.h"
|
||||||
|
|
||||||
|
#define PRINT(args...) if (verbose) printf(args)
|
||||||
|
|
||||||
|
#define fail_unless(test) \
|
||||||
|
do { \
|
||||||
|
if (!(test)) { \
|
||||||
|
fprintf(stderr, \
|
||||||
|
"----------------------------------------------\n" \
|
||||||
|
"%s:%d: test FAILED:\nFailed test: %s\n" \
|
||||||
|
"----------------------------------------------\n", \
|
||||||
|
__FILE__, __LINE__, #test); \
|
||||||
|
exit(1); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
static inline int test(int result, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
char buf[80];
|
||||||
|
size_t len;
|
||||||
|
va_list ap;
|
||||||
|
const char success[] = " \033[1m[ OK ]\033[0m\n";
|
||||||
|
const char failure[] = " \033[7m[FAIL]\033[0m\n";
|
||||||
|
const char dots[] = " .....................................................................";
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
write(STDERR_FILENO, buf, len);
|
||||||
|
write(STDERR_FILENO, dots, 60 - len); /* pad with dots. */
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
write(STDERR_FILENO, success, strlen(success));
|
||||||
|
else
|
||||||
|
write(STDERR_FILENO, failure, strlen(failure));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int timespec_newer(const struct timespec *a, const struct timespec *b)
|
||||||
|
{
|
||||||
|
if (a->tv_sec != b->tv_sec)
|
||||||
|
return a->tv_sec > b->tv_sec;
|
||||||
|
|
||||||
|
return a->tv_nsec > b->tv_nsec;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *timespec2str(struct timespec *ts, char *buf, size_t len)
|
||||||
|
{
|
||||||
|
size_t ret, pos;
|
||||||
|
struct tm t;
|
||||||
|
|
||||||
|
memset(buf, 0, len);
|
||||||
|
|
||||||
|
tzset();
|
||||||
|
if (localtime_r(&(ts->tv_sec), &t) == NULL)
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
ret = strftime(buf, len, "%F %T", &t);
|
||||||
|
if (ret == 0)
|
||||||
|
return buf;
|
||||||
|
len -= ret - 1;
|
||||||
|
|
||||||
|
pos = strlen(buf);
|
||||||
|
len -= pos;
|
||||||
|
snprintf(&buf[pos], len, ".%09ld", ts->tv_nsec);
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LITE_TESTS_CHECK_H_ */
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char t[][16] = {
|
||||||
|
"hej\ndej",
|
||||||
|
"Slime\n\n\\n",
|
||||||
|
"Tripple\n\n\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(t); i++) {
|
||||||
|
char *str = t[i];
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
chomp(str);
|
||||||
|
len = strlen(str);
|
||||||
|
printf("[%02zd]: '%s'\n", i, str);
|
||||||
|
|
||||||
|
fail_unless(str[len] != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
char *files[] = {
|
||||||
|
"/etc/passwd", "/tmp/mypwd", "/tmp/mypwd",
|
||||||
|
"/etc/passwd", "/tmp/", "/tmp/passwd",
|
||||||
|
"/etc/passwd", "/tmp", "/tmp/passwd",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
FILE *src, *dst;
|
||||||
|
|
||||||
|
printf("=>Start testing fcopyfile()\n");
|
||||||
|
while (files[i]) {
|
||||||
|
printf("fcopyfile(%s, %s)\t", files[i], files[i + 1]);
|
||||||
|
src = fopen(files[i], "r");
|
||||||
|
dst = fopen(files[i + 1], "w");
|
||||||
|
if (fcopyfile(src, dst)) {
|
||||||
|
if (!fisdir(files[i + 1]))
|
||||||
|
err(1, "Failed fcopyfile(%s, %s)", files[i], files[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src)
|
||||||
|
fclose(src);
|
||||||
|
if (dst)
|
||||||
|
fclose(dst);
|
||||||
|
|
||||||
|
if (fexist(files[i + 2]))
|
||||||
|
printf("OK => %s", files[i + 2]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
erase(files[i + 2]);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n=>Start testing copyfile()\n");
|
||||||
|
i = 0;
|
||||||
|
while (files[i]) {
|
||||||
|
printf("copyfile(%s, %s)\t", files[i], files[i + 1]);
|
||||||
|
if (!copyfile(files[i], files[i + 1], 0, 0))
|
||||||
|
err(1, "Failed copyfile(%s, %s)", files[i], files[i + 1]);
|
||||||
|
|
||||||
|
if (fexist(files[i + 2]))
|
||||||
|
printf("OK => %s", files[i + 2]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
erase(files[i + 2]);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+99
@@ -0,0 +1,99 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#define DIR_TYPE_IMAGE ".img"
|
||||||
|
#define DIR_TYPE_SYSLOG ""
|
||||||
|
#define DIR_TYPE_CONFIG ".cfg"
|
||||||
|
#define STARTUP_CONFIG "startup-config.cfg"
|
||||||
|
|
||||||
|
int simulate_files(int creat)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char *files[] =
|
||||||
|
{ "config0.cfg", "config1.cfg", "config2.cfg", "config3.cfg",
|
||||||
|
"rr109.img", "rx100.img", "rx107.img", "rm957.img", "messages"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(files); i++) {
|
||||||
|
if (creat)
|
||||||
|
touch(files[i]);
|
||||||
|
else
|
||||||
|
erase(files[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (creat)
|
||||||
|
symlink("config2.cfg", STARTUP_CONFIG);
|
||||||
|
else
|
||||||
|
erase(STARTUP_CONFIG);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cfg_dir_filter(const char *file)
|
||||||
|
{
|
||||||
|
/* Skip the STARTUP_CONFIG file, it is a symbolic link to the
|
||||||
|
* current startup configuration. */
|
||||||
|
return ! !strcmp(file, STARTUP_CONFIG);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_startup_config(const char *entry)
|
||||||
|
{
|
||||||
|
static int once = 1;
|
||||||
|
static char file[80];
|
||||||
|
|
||||||
|
if (once) {
|
||||||
|
int len = readlink(STARTUP_CONFIG, file, sizeof(file));
|
||||||
|
|
||||||
|
if (len == -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
file[len] = 0;
|
||||||
|
once = 0; /* Only once per call to dir() */
|
||||||
|
}
|
||||||
|
//printf ("Comparing link %s with entry %s\n", file, entry);
|
||||||
|
return !strcmp(file, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i, num;
|
||||||
|
char *type = DIR_TYPE_CONFIG;
|
||||||
|
char **files;
|
||||||
|
|
||||||
|
simulate_files(1);
|
||||||
|
|
||||||
|
if (argc >= 2) {
|
||||||
|
if (!strcasecmp("CONFIG", argv[1])) {
|
||||||
|
type = DIR_TYPE_CONFIG;
|
||||||
|
system("ls -l *" DIR_TYPE_CONFIG);
|
||||||
|
}
|
||||||
|
if (!strcasecmp("IMAGE", argv[1])) {
|
||||||
|
type = DIR_TYPE_IMAGE;
|
||||||
|
system("ls -l *" DIR_TYPE_IMAGE);
|
||||||
|
}
|
||||||
|
if (!strcasecmp("SYSLOG", argv[1])) {
|
||||||
|
type = DIR_TYPE_SYSLOG;
|
||||||
|
system("ls -l *");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
num = dir(NULL, type, cfg_dir_filter, &files, 0);
|
||||||
|
if (num) {
|
||||||
|
for (i = 0; i < num; i++) {
|
||||||
|
printf("%s", files[i]);
|
||||||
|
if (is_startup_config(files[i]))
|
||||||
|
printf(" --> startup-config");
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
free(files[i]);
|
||||||
|
}
|
||||||
|
free(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
simulate_files(0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
struct { char *file; int exist; } arr[] = {
|
||||||
|
{ "/etc/passwd", 1 },
|
||||||
|
{ "/etc/kalle", 0 },
|
||||||
|
{ "/sbin/init", 1 },
|
||||||
|
{ "/dev/null", 1 },
|
||||||
|
{ NULL, 0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
struct { char *file; int exist; } arr[] = {
|
||||||
|
{ "/etc/passwd", 0 },
|
||||||
|
{ "/etc/", 1 },
|
||||||
|
{ "/sbin/init", 0 },
|
||||||
|
{ "/dev/null", 0 },
|
||||||
|
{ "/dev/", 1 },
|
||||||
|
{ NULL, 0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "check.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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#define FSTAB "/etc/fstab"
|
||||||
|
|
||||||
|
static int fstab(void)
|
||||||
|
{
|
||||||
|
int field = 1;
|
||||||
|
char *token;
|
||||||
|
lfile_t *lf;
|
||||||
|
|
||||||
|
lf = lfopen(FSTAB, " \t\n");
|
||||||
|
if (!lf) {
|
||||||
|
perror("Failed opening " FSTAB);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printheader(NULL, "FILE SYSTEM MOUNT OPTIONS D P", 0);
|
||||||
|
while ((token = lftok(lf))) {
|
||||||
|
if (field == 1)
|
||||||
|
printf("%-41s ", token);
|
||||||
|
if (field == 2)
|
||||||
|
printf("%-9s ", token);
|
||||||
|
if (field == 4)
|
||||||
|
printf("%-17s ", token);
|
||||||
|
if (field == 5)
|
||||||
|
printf("%d ", atoi(token));
|
||||||
|
if (field == 6) {
|
||||||
|
field = 0;
|
||||||
|
printf("%d\n", atoi(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
field++;
|
||||||
|
}
|
||||||
|
|
||||||
|
lfclose(lf);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
|
||||||
|
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 fstab();
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int checkpath(const 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(const 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/libite-test/tok/",
|
||||||
|
"/tmp/libite-test/tok2",
|
||||||
|
"/tmp/libite-test/ab",
|
||||||
|
"/tmp/libite-test/b",
|
||||||
|
"/tmp/libite-test/a/",
|
||||||
|
"/tmp/libite-test/a/b",
|
||||||
|
"/tmp/libite-test/a/c/",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
printf("Testing makepath() ...\n");
|
||||||
|
mkdir("/tmp/libite-test", 0755);
|
||||||
|
|
||||||
|
for (i = 0; list[i] && !ret; i++)
|
||||||
|
ret |= test_makepath(list[i]);
|
||||||
|
|
||||||
|
printf("\nCleaning up ...\n");
|
||||||
|
system("rm -rf /tmp/libite-test");
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* compile-command: "make makepath && ./makepath"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
+129
@@ -0,0 +1,129 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
extern char *__pidfile_name;
|
||||||
|
extern char *__pidfile_path;
|
||||||
|
|
||||||
|
static char *ident;
|
||||||
|
static char *pidfile_arg;
|
||||||
|
static char PIDFILE[42];
|
||||||
|
static int verbose = 0;
|
||||||
|
static struct stat before, after;
|
||||||
|
|
||||||
|
static void sigterm_handler(int signo)
|
||||||
|
{
|
||||||
|
PRINT("Exiting ...\n");
|
||||||
|
remove(PIDFILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sigalrm_handler(int signo)
|
||||||
|
{
|
||||||
|
PRINT("Calling pidfile(%s)\n", pidfile_arg ?: "NULL");
|
||||||
|
if (pidfile(pidfile_arg))
|
||||||
|
err(1, "failed creating1 %s", pidfile_arg);
|
||||||
|
if (!fexist(PIDFILE))
|
||||||
|
err(1, "failed creating2 %s, created %s", PIDFILE, __pidfile_name);
|
||||||
|
stat(PIDFILE, &before);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mtime()
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
char buf[80];
|
||||||
|
|
||||||
|
/* Must sleep a while here otherwise we execute too fast => no mtime change :-( */
|
||||||
|
usleep(10000);
|
||||||
|
|
||||||
|
PRINT("Calling pidfile() again to update mtime ...\n");
|
||||||
|
if (pidfile(pidfile_arg))
|
||||||
|
err(1, "failed creating3 %s", pidfile_arg);
|
||||||
|
stat(PIDFILE, &after);
|
||||||
|
|
||||||
|
ret = timespec_newer(&after.st_mtim, &before.st_mtim);
|
||||||
|
PRINT("Before: %s\n", timespec2str(&before.st_mtim, buf, sizeof(buf)));
|
||||||
|
PRINT("After : %s\n", timespec2str(&after.st_mtim, buf, sizeof(buf)));
|
||||||
|
|
||||||
|
return !ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_pidfile(char *arg0)
|
||||||
|
{
|
||||||
|
/* pidfile() argument */
|
||||||
|
pidfile_arg = arg0;
|
||||||
|
|
||||||
|
/* Expected PID file name */
|
||||||
|
if (!arg0)
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", __pidfile_path, ident);
|
||||||
|
else if (arg0[0] != '/')
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", __pidfile_path, arg0);
|
||||||
|
else
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s", arg0);
|
||||||
|
|
||||||
|
PRINT("Verifying pidfile(%s) ... PID: %d\n", PIDFILE, getpid());
|
||||||
|
|
||||||
|
/* Remove any lingering files from previous test runs */
|
||||||
|
remove(PIDFILE);
|
||||||
|
|
||||||
|
signal(SIGTERM, sigterm_handler);
|
||||||
|
signal(SIGALRM, sigalrm_handler);
|
||||||
|
|
||||||
|
/* For signalling test */
|
||||||
|
alarm(1);
|
||||||
|
|
||||||
|
return test(pidfile_poll(PIDFILE) != getpid(), "Testing pidfile_poll()")
|
||||||
|
|| test(strcmp(__pidfile_name, PIDFILE), "Testing __pidfile_name vs guessed PID filename")
|
||||||
|
|| test(mtime(), "Testing mtime update of followup pidfile() call")
|
||||||
|
|| test(pidfile_signal(PIDFILE, SIGTERM), "Testing signalling ourselves")
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *progname(char *arg0)
|
||||||
|
{
|
||||||
|
char *nm;
|
||||||
|
|
||||||
|
nm = strrchr(arg0, '/');
|
||||||
|
if (nm)
|
||||||
|
nm++;
|
||||||
|
else
|
||||||
|
nm = arg0;
|
||||||
|
|
||||||
|
return nm;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
size_t i;
|
||||||
|
char *pidfiles[] = {
|
||||||
|
NULL,
|
||||||
|
"pidfile_test1",
|
||||||
|
"/var/tmp/pidfile_test2.pid",
|
||||||
|
};
|
||||||
|
|
||||||
|
if (argc > 1 && !strcmp(argv[1], "-v"))
|
||||||
|
verbose = 1;
|
||||||
|
|
||||||
|
/* Override default _PATH_VARRUN in pidfile() */
|
||||||
|
__pidfile_path = _PATH_TMP;
|
||||||
|
|
||||||
|
/* Find __progname, not all C libraries support this */
|
||||||
|
ident = progname(argv[0]);
|
||||||
|
|
||||||
|
for (i = 0; i < NELEMS(pidfiles); i++)
|
||||||
|
ret += test_pidfile(pidfiles[i]);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* compile-command: "make pidfile && ./pidfile -v"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <stdlib.h> /* atexit() */
|
||||||
|
#include <unistd.h> /* usleep() */
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#define SCREEN_WIDTH 80
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE *fp = stderr;
|
||||||
|
char buf[SCREEN_WIDTH];
|
||||||
|
|
||||||
|
printhdr(fp, "", 0, UNDERSCORE);
|
||||||
|
printhdr(fp, "Neighbor Table", 0, 0);
|
||||||
|
snprintf(buf, sizeof(buf), "%-15s %-15s %7s %-5s%10s %6s",
|
||||||
|
"Neighbor", "Interface", "Version", "Flags", "Uptime", "Expire");
|
||||||
|
printhdr(fp, buf, 0, REVERSE);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <stdlib.h> /* atexit() */
|
||||||
|
#include <unistd.h> /* usleep() */
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#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 run_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(1);
|
||||||
|
}
|
||||||
|
block++;
|
||||||
|
msleep(1);
|
||||||
|
}
|
||||||
|
testit(fn, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
atexit(bye);
|
||||||
|
hidecursor();
|
||||||
|
|
||||||
|
printf("\nAdvanced:\n");
|
||||||
|
run_test(1);
|
||||||
|
printf("\nSimple:\n");
|
||||||
|
run_test(0);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#define BASE "/tmp/.unittest/"
|
||||||
|
#define SRC BASE "src/"
|
||||||
|
#define DST BASE "dst/"
|
||||||
|
|
||||||
|
static int verbose = 0;
|
||||||
|
static char *files[] = {
|
||||||
|
SRC "sub1/1.tst",
|
||||||
|
SRC "sub1/2.tst",
|
||||||
|
SRC "sub1/3.tst",
|
||||||
|
SRC "sub2/4.tst",
|
||||||
|
SRC "sub2/5.tst",
|
||||||
|
SRC "sub2/6.tst",
|
||||||
|
SRC "sub3/7.tst",
|
||||||
|
SRC "sub3/8.tst",
|
||||||
|
SRC "sub3/9.tst",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
void cleanup_test(void)
|
||||||
|
{
|
||||||
|
system("rm -rf " BASE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_test(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char cmd[256];
|
||||||
|
mode_t dir_modes[] = { 755, 700 };
|
||||||
|
mode_t file_modes[] = { 644, 600 };
|
||||||
|
|
||||||
|
cleanup_test();
|
||||||
|
|
||||||
|
mkdir(BASE, 0755);
|
||||||
|
mkdir(SRC, 0755);
|
||||||
|
mkdir(DST, 0755);
|
||||||
|
|
||||||
|
for (i = 0; files[i]; i++) {
|
||||||
|
snprintf(cmd, sizeof(cmd), "mkdir -m %d -p `dirname %s`",
|
||||||
|
dir_modes[i % 2], files[i]);
|
||||||
|
system(cmd);
|
||||||
|
|
||||||
|
snprintf(cmd, sizeof(cmd), "touch %s; chmod %d %s", files[i],
|
||||||
|
file_modes[i % 2], files[i]);
|
||||||
|
system(cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void check_tree(char *heading, char *dir)
|
||||||
|
{
|
||||||
|
if (verbose) {
|
||||||
|
if (heading)
|
||||||
|
puts(heading);
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX: Fixme, add code to list tree, nftw() possibly
|
||||||
|
}
|
||||||
|
|
||||||
|
int run_test(void)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
setup_test();
|
||||||
|
check_tree("Before:", BASE);
|
||||||
|
|
||||||
|
result += rsync(SRC, DST, 0, NULL);
|
||||||
|
check_tree("After:", BASE);
|
||||||
|
cleanup_test();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
setup_test();
|
||||||
|
sleep(1);
|
||||||
|
result += rsync(BASE "src", DST, LITE_FOPT_KEEP_MTIME, NULL);
|
||||||
|
check_tree("Only partial rsync of src <-- No slash!", BASE);
|
||||||
|
#if 0
|
||||||
|
cleanup_test();
|
||||||
|
setup_test();
|
||||||
|
result += rsync(BASE "src/sub1", BASE "dst", 0, NULL);
|
||||||
|
check_tree("Only partial rsync of src/sub1 <-- No slashes!!", BASE);
|
||||||
|
|
||||||
|
cleanup_test();
|
||||||
|
setup_test();
|
||||||
|
result += rsync(BASE "src/sub1/", DST, 0, NULL);
|
||||||
|
check_tree("Only partial rsync of src/sub1/", BASE);
|
||||||
|
|
||||||
|
cleanup_test();
|
||||||
|
setup_test();
|
||||||
|
result += rsync(BASE "src/sub1", DST, 0, NULL);
|
||||||
|
check_tree("Only partial rsync of src/sub1 <-- No slash!", BASE);
|
||||||
|
|
||||||
|
result += rsync("/etc", "/var/tmp", 0, NULL);
|
||||||
|
check_tree("Real life test:", "/var/tmp");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc > 1)
|
||||||
|
verbose = !strncmp("-v", argv[1], 2);
|
||||||
|
|
||||||
|
atexit(cleanup_test);
|
||||||
|
|
||||||
|
return run_test();
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
#include "../src/strlite.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char buf[10];
|
||||||
|
int val;
|
||||||
|
|
||||||
|
val = atonum("42");
|
||||||
|
if (val != 42)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!string_match("eth1", "eth1234"))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (string_compare("eth1", "eth1234"))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!string_case_compare("somestring", "SoMeSTRING"))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
strlcpy(buf, "tooloongstring", sizeof(buf));
|
||||||
|
if (buf[9] == 'r')
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include "check.h"
|
||||||
|
#define SYSLOG_NAMES
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
|
#define SEVERITY_LEVEL { \
|
||||||
|
CHOOSE(SEVERITY_NONE, "none"), \
|
||||||
|
CHOOSE(SEVERITY_DEBUG, "debug"), \
|
||||||
|
CHOOSE(SEVERITY_INFO, "info"), \
|
||||||
|
CHOOSE(SEVERITY_NOTICE, "notice"), \
|
||||||
|
CHOOSE(SEVERITY_WARNING, "warning"), \
|
||||||
|
CHOOSE(SEVERITY_ERR, "err"), \
|
||||||
|
CHOOSE(SEVERITY_CRIT, "crit"), \
|
||||||
|
CHOOSE(SEVERITY_ALERT, "alert"), \
|
||||||
|
CHOOSE(SEVERITY_EMERG, "emerg") \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CHOOSE(a, b) b
|
||||||
|
const char *levels[] = SEVERITY_LEVEL;
|
||||||
|
|
||||||
|
#undef CHOOSE
|
||||||
|
#define CHOOSE(a, b) a
|
||||||
|
typedef enum SEVERITY_LEVEL severity_level_t;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int num;
|
||||||
|
struct {
|
||||||
|
char *str;
|
||||||
|
int val;
|
||||||
|
} t[] = {
|
||||||
|
{ "warn", SEVERITY_WARNING },
|
||||||
|
{ "alert", SEVERITY_ALERT },
|
||||||
|
{ NULL, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; t[i].str; i++) {
|
||||||
|
num = strmatch(t[i].str, levels);
|
||||||
|
fail_unless(num >= 0);
|
||||||
|
fail_unless(num == t[i].val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||||
|
#include <paths.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#ifndef O_TMPFILE
|
||||||
|
#warning O_TMPFILE is missing, either too old GLIBC, Linux, or non-Linux system
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define READBACK "Reality is frequently inaccurate.\n"
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
FILE *fp;
|
||||||
|
char buf[80] = "";
|
||||||
|
|
||||||
|
printf("Before tempfile():\n");
|
||||||
|
ret = system("ls -lrt " _PATH_TMP " | tail -3");
|
||||||
|
|
||||||
|
fp = tempfile();
|
||||||
|
fail_unless(fp != NULL);
|
||||||
|
|
||||||
|
ret = fputs(READBACK, fp);
|
||||||
|
rewind(fp);
|
||||||
|
if (fgets(buf, sizeof(buf), fp))
|
||||||
|
fprintf(stderr, "\nRead-back from tempfile: %s", buf);
|
||||||
|
fail_unless(!strcmp(buf, READBACK));
|
||||||
|
|
||||||
|
printf("\nAfter tempfile(), should be same list:\n");
|
||||||
|
ret = system("ls -lrt " _PATH_TMP " | tail -3");
|
||||||
|
|
||||||
|
return fclose(fp);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#include <err.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int verbose = 0;
|
||||||
|
|
||||||
|
|
||||||
|
static int mtime(char *file)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
char buf[80];
|
||||||
|
struct stat before, after;
|
||||||
|
|
||||||
|
PRINT("Calling touch() to create file ...\n");
|
||||||
|
touch(file);
|
||||||
|
stat(file, &before);
|
||||||
|
|
||||||
|
/* Must sleep a while here otherwise we execute too fast => no mtime change :-( */
|
||||||
|
usleep(10000);
|
||||||
|
|
||||||
|
PRINT("Calling touch() again to update mtime ...\n");
|
||||||
|
if (touch(file)) {
|
||||||
|
erase(file);
|
||||||
|
err(1, "Failed creating %s", file);
|
||||||
|
}
|
||||||
|
stat(file, &after);
|
||||||
|
|
||||||
|
ret = timespec_newer(&after.st_mtim, &before.st_mtim);
|
||||||
|
PRINT("Before: %s\n", timespec2str(&before.st_mtim, buf, sizeof(buf)));
|
||||||
|
PRINT("After : %s\n", timespec2str(&after.st_mtim, buf, sizeof(buf)));
|
||||||
|
|
||||||
|
return !ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char file[42] = "/tmp/touch_test.XXXXXX";
|
||||||
|
|
||||||
|
if (argc > 1 && !strcmp(argv[1], "-v"))
|
||||||
|
verbose = 1;
|
||||||
|
|
||||||
|
fd = mkstemp(file);
|
||||||
|
if (fd < 0)
|
||||||
|
err(1, "Failed creating tempfile, %s", file);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return mtime(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* compile-command: "make pidfile && ./pidfile -v"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
struct tc {
|
||||||
|
char *cmd;
|
||||||
|
int exp;
|
||||||
|
int res;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
size_t i;
|
||||||
|
struct tc test[] = {
|
||||||
|
{ "ls", 1 },
|
||||||
|
{ "free", 1 },
|
||||||
|
{ "modinfo", 1 },
|
||||||
|
{ "useradd", 1 },
|
||||||
|
{ "/usr/bin/which", 1 },
|
||||||
|
{ "/bin/ps aux", 1 },
|
||||||
|
{ "/etc/passwd", 0 },
|
||||||
|
{ NULL, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
setenv("PATH", "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin", 1);
|
||||||
|
for (i = 0; test[i].cmd; i++) {
|
||||||
|
char *path;
|
||||||
|
|
||||||
|
path = which(test[i].cmd);
|
||||||
|
if (!path) {
|
||||||
|
test[i].res = 0;
|
||||||
|
fprintf(stderr, "%20s : Not hot dog\n", test[i].cmd);
|
||||||
|
} else {
|
||||||
|
test[i].res = 1;
|
||||||
|
fprintf(stderr, "%20s : hot dog --> %s\n", test[i].cmd, path);
|
||||||
|
free(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; test[i].cmd; i++)
|
||||||
|
result += test[i].exp != test[i].res;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int chan[2];
|
||||||
|
int status;
|
||||||
|
|
||||||
|
pipe(chan);
|
||||||
|
|
||||||
|
if (fork() > 0) {
|
||||||
|
dup2(chan[0], STDIN_FILENO);
|
||||||
|
if (yorn("Do you really wanna do this (y/N)? ")) {
|
||||||
|
printf("Affirmative!\n");
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("OK, aborting!\n");
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
if (write(chan[1], "Y", 1) != 1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
wait(&status);
|
||||||
|
|
||||||
|
return WEXITSTATUS(status);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user