mirror of
https://github.com/troglobit/libite.git
synced 2026-10-14 10:19:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
No files matched your search
@@ -22,3 +22,4 @@ aclocal.m4
|
||||
ar-lib
|
||||
autom4te.cache
|
||||
libite*
|
||||
test-driver
|
||||
+48
-1
@@ -4,6 +4,50 @@ Change Log
|
||||
All notable changes to the project are documented in this file.
|
||||
|
||||
|
||||
[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
|
||||
-----------------------
|
||||
|
||||
@@ -231,7 +275,10 @@ problems this may cause!
|
||||
Initial extraction of frog DNA from [Finit][]. See [README][] for API details.
|
||||
|
||||
|
||||
[UNRELEASED]: https://github.com/troglobit/libite/compare/v1.8.3...HEAD
|
||||
[UNRELEASED]: https://github.com/troglobit/libite/compare/v1.9.2...HEAD
|
||||
[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
|
||||
|
||||
+24
-5
@@ -4,9 +4,10 @@ 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
|
||||
* Encourage contributors to write unit tests
|
||||
* Bump library ABI version just before release!
|
||||
|
||||
|
||||
@@ -17,6 +18,7 @@ Release Checklist
|
||||
- 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
|
||||
@@ -25,7 +27,7 @@ Release Checklist
|
||||
make distclean
|
||||
./autogen.sh
|
||||
./configure
|
||||
make release
|
||||
make release
|
||||
|
||||
* Create new release in GitHub releases page
|
||||
* Copy and paste ChangeLog entry, check any stale links!
|
||||
@@ -54,14 +56,31 @@ It must be updated according to the [GNU Libtool recommendations][1]:
|
||||
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` --> `c:r+1:a`)
|
||||
4. If any *interfaces have been added, removed, or changed* since last
|
||||
update, increment current, and zero revision (`c:r:a` --> `c+1:0:0`)
|
||||
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
|
||||
|
||||
+6
-2
@@ -6,11 +6,11 @@ pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa
|
||||
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \
|
||||
fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \
|
||||
progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \
|
||||
tempfile.c tree.c
|
||||
tempfile.c tree.c which.c
|
||||
|
||||
libite_la_CPPFLAGS = -D_GNU_SOURCE
|
||||
libite_la_CFLAGS = -W -Wall -Wextra
|
||||
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 3:1:0
|
||||
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 4:1:0
|
||||
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
pkgconfig_DATA = libite.pc
|
||||
@@ -18,6 +18,10 @@ doc_DATA = README.md LICENSE
|
||||
EXTRA_DIST = README.md LICENSE ChangeLog.md
|
||||
SUBDIRS = tests
|
||||
|
||||
## Generate .deb package
|
||||
package:
|
||||
@dpkg-buildpackage -uc -us -B
|
||||
|
||||
## Generate MD5 checksum file
|
||||
MD5 = md5sum
|
||||
md5-dist:
|
||||
|
||||
@@ -143,10 +143,6 @@ Helper Macros
|
||||
Returns the number of elements in an array. From the great book, The
|
||||
Practice of Programming, by Kernighan and Pike.
|
||||
|
||||
- `UNUSED(var)`
|
||||
|
||||
Shorter and more readable version of `var __attribute__ ((unused))`
|
||||
|
||||
|
||||
Generic Functions
|
||||
-----------------
|
||||
@@ -292,6 +288,16 @@ Generic Functions
|
||||
optional listing of file and directory permissions if `show_perms` is
|
||||
set. The `path` argument should be a directory.
|
||||
|
||||
- `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.
|
||||
|
||||
|
||||
GNU Functions
|
||||
-------------
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
AC_INIT(libite, 1.8.3, https://github.com/troglobit/libite/issues)
|
||||
AC_INIT(libite, 1.9.3, https://github.com/troglobit/libite/issues)
|
||||
AM_INIT_AUTOMAKE([1.11 foreign no-dist-gzip dist-xz])
|
||||
AM_SILENT_RULES([yes])
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* A conio.h like implementation for VTANSI displays.
|
||||
*
|
||||
* Copyright (c) 2009-2013 Joachim Nilsson <troglobit@gmail.com>
|
||||
* Copyright (c) 2009-2017 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
|
||||
@@ -19,6 +19,8 @@
|
||||
#define CONIO_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Attributes */
|
||||
#define RESETATTR 0
|
||||
@@ -48,29 +50,98 @@
|
||||
#define LIGHTCYAN 0x16
|
||||
#define WHITE 0x17
|
||||
|
||||
/* 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) printf("\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)
|
||||
#ifndef SCREEN_WIDTH
|
||||
#define SCREEN_WIDTH 80 /* Override using, e.g. #define SCREEN_WIDTH tty_width() */
|
||||
#endif
|
||||
|
||||
/* Esc[Value;...;Valuem - Set Graphics Mode */
|
||||
#define __set_gm(attr,color,val) \
|
||||
if (!color) \
|
||||
printf("\033[%dm", attr); \
|
||||
/* 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 \
|
||||
printf("\033[%d;%dm", color & 0x10 ? 1 : 0, (color & 0xF) + val)
|
||||
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)
|
||||
|
||||
/* Print table heading @line to @fp, with optional leading newline */
|
||||
static inline void printheader(FILE *fp, const char *line, int nl)
|
||||
{
|
||||
fprintf(fp ?: stdout,
|
||||
"%s\033[7m%s%*s\033[0m\n",
|
||||
nl ? "\n" : "",
|
||||
line, SCREEN_WIDTH - (int)strlen(line), "");
|
||||
}
|
||||
|
||||
/* Requries: #inlude <termios.h> and #include <poll.h> */
|
||||
static inline 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
|
||||
}
|
||||
|
||||
#endif /* CONIO_H_ */
|
||||
|
||||
/**
|
||||
|
||||
Vendored
+30
@@ -1,3 +1,33 @@
|
||||
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.
|
||||
|
||||
@@ -85,11 +85,19 @@ int tree(char *path, int show_perms);
|
||||
void progress (int percent, int max_width);
|
||||
void progress_simple(int percent);
|
||||
|
||||
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) && errno != EEXIST)
|
||||
if (mknod((path), S_IFREG|0644, 0)) {
|
||||
if (errno == EEXIST)
|
||||
return utimensat(0, path, NULL, 0);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -151,11 +159,6 @@ static inline int erase(const char *path)
|
||||
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
|
||||
#endif
|
||||
|
||||
/* Mark a function variable as unused, useful for generic callbacks */
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||
#endif
|
||||
|
||||
/* Does directory end with a slash? */
|
||||
static inline int fisslashdir(char *dir)
|
||||
{
|
||||
@@ -169,9 +172,9 @@ static inline int fisslashdir(char *dir)
|
||||
}
|
||||
|
||||
/* 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)
|
||||
#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_ */
|
||||
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ FILE *tempfile(void)
|
||||
if (-1 == fd)
|
||||
return NULL;
|
||||
|
||||
return fdopen(fd, "rw");
|
||||
return fdopen(fd, "w+");
|
||||
#else
|
||||
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
||||
#endif
|
||||
|
||||
@@ -12,4 +12,6 @@ pidfile
|
||||
progress
|
||||
rsync
|
||||
tempfile
|
||||
touch
|
||||
tree
|
||||
which
|
||||
+5
-2
@@ -11,14 +11,17 @@ TESTS += fsendfile
|
||||
TESTS += lfile
|
||||
TESTS += makepath
|
||||
TESTS += pidfile
|
||||
TESTS += touch
|
||||
TESTS += progress
|
||||
TESTS += rsync
|
||||
TESTS += tempfile
|
||||
TESTS += tree
|
||||
TESTS += which
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
# Ignore warnings about unused result, in e.g. write()
|
||||
CPPFLAGS = -D_GNU_SOURCE
|
||||
CFLAGS += -Wno-unused-result
|
||||
CPPFLAGS += -D_GNU_SOURCE
|
||||
LDADD = -L../ ../libite.la
|
||||
LDFLAGS = -static
|
||||
LDFLAGS += -static
|
||||
@@ -6,8 +6,11 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../conio.h"
|
||||
#include "../lite.h"
|
||||
|
||||
#define PRINT(args...) if (verbose) printf(args)
|
||||
|
||||
#define fail_unless(test) \
|
||||
do { \
|
||||
if (!(test)) { \
|
||||
|
||||
+10
-3
@@ -6,7 +6,7 @@
|
||||
static int fstab(void)
|
||||
{
|
||||
int field = 1;
|
||||
char *token, *dev;
|
||||
char *token;
|
||||
lfile_t *lf;
|
||||
|
||||
lf = lfopen(FSTAB, " \t\n");
|
||||
@@ -15,12 +15,19 @@ static int fstab(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
printheader(NULL, "FILE SYSTEM MOUNT OPTIONS D P", 0);
|
||||
while ((token = lftok(lf))) {
|
||||
if (field == 1)
|
||||
dev = token;
|
||||
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("Found %s passno %d\n", dev, atoi(token));
|
||||
printf("%d\n", atoi(token));
|
||||
}
|
||||
|
||||
field++;
|
||||
|
||||
+2
-4
@@ -6,8 +6,6 @@
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#define PRINT(args...) if (verbose) printf(args)
|
||||
|
||||
extern char *__pidfile_name;
|
||||
extern char *__pidfile_path;
|
||||
|
||||
@@ -17,13 +15,13 @@ static char PIDFILE[42];
|
||||
static int verbose = 0;
|
||||
static struct stat before, after;
|
||||
|
||||
static void sigterm_handler(int UNUSED(signo))
|
||||
static void sigterm_handler(int signo)
|
||||
{
|
||||
PRINT("Exiting ...\n");
|
||||
remove(PIDFILE);
|
||||
}
|
||||
|
||||
static void sigalrm_handler(int UNUSED(signo))
|
||||
static void sigalrm_handler(int signo)
|
||||
{
|
||||
PRINT("Calling pidfile(%s)\n", pidfile_arg ?: "NULL");
|
||||
if (pidfile(pidfile_arg))
|
||||
|
||||
+13
-2
@@ -8,18 +8,29 @@
|
||||
#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");
|
||||
system("ls -lrt " _PATH_TMP " | tail -3");
|
||||
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");
|
||||
system("ls -lrt " _PATH_TMP " | tail -3");
|
||||
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:
|
||||
*/
|
||||
+1
-1
@@ -7,7 +7,7 @@ int main(void)
|
||||
struct { char *path; int fail; } arr[] = {
|
||||
{ "/etc/passwd", -1 },
|
||||
{ "/dev", 0 },
|
||||
{ "/sbin", 0 },
|
||||
{ "/usr/sbin", 0 },
|
||||
};
|
||||
|
||||
for (i = 0; i < NELEMS(arr); i++) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#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 },
|
||||
{ "/bin/which", 1 },
|
||||
{ "/bin/ps aux", 1 },
|
||||
{ "/etc/passwd", 0 },
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
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:
|
||||
*/
|
||||
@@ -59,56 +59,54 @@ static void get_perms(struct stat *st, char *buf, size_t len)
|
||||
|
||||
static int descend(char *path, int show_perms, char *pfx)
|
||||
{
|
||||
int result = 0;
|
||||
int i, n, result = 0;
|
||||
struct stat st;
|
||||
struct dirent **namelist = NULL;
|
||||
|
||||
if (-1 == lstat(path, &st))
|
||||
return 1;
|
||||
|
||||
if ((st.st_mode & S_IFMT) == S_IFDIR) {
|
||||
int i, n;
|
||||
struct dirent **namelist = NULL;
|
||||
if ((st.st_mode & S_IFMT) != S_IFDIR) {
|
||||
errno = ENOTDIR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
n = scandir(path, &namelist, filter, alphasort);
|
||||
if (n) {
|
||||
for (i = 0; i < n; i++) {
|
||||
char t = ' ', p[14] = "", s[256] = "";
|
||||
char buf[256];
|
||||
char dir[80];
|
||||
n = scandir(path, &namelist, filter, alphasort);
|
||||
if (n) {
|
||||
for (i = 0; i < n; i++) {
|
||||
char t = ' ', p[14] = "", s[256] = "";
|
||||
char buf[512];
|
||||
char dir[80];
|
||||
|
||||
if (i + 1 == n) {
|
||||
printf("%s `- ", pfx);
|
||||
snprintf(dir, sizeof(dir), "%s ", pfx);
|
||||
} else {
|
||||
printf("%s|-- ", pfx);
|
||||
snprintf(dir, sizeof(dir), "%s| ", pfx);
|
||||
}
|
||||
|
||||
snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name);
|
||||
if (!lstat(buf, &st)) {
|
||||
if (show_perms)
|
||||
get_perms(&st, p, sizeof(p));
|
||||
if ((st.st_mode & S_IFMT) == S_IFDIR)
|
||||
t = '/';
|
||||
if (S_ISLNK(st.st_mode)) {
|
||||
snprintf(s, sizeof(s), "-> ");
|
||||
if (-1 == readlink(buf, &s[3], sizeof(s) - 3))
|
||||
s[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s);
|
||||
if (t == '/')
|
||||
result += descend(buf, show_perms, dir);
|
||||
|
||||
free(namelist[i]);
|
||||
if (i + 1 == n) {
|
||||
printf("%s `- ", pfx);
|
||||
snprintf(dir, sizeof(dir), "%s ", pfx);
|
||||
} else {
|
||||
printf("%s|-- ", pfx);
|
||||
snprintf(dir, sizeof(dir), "%s| ", pfx);
|
||||
}
|
||||
|
||||
free(namelist);
|
||||
snprintf(buf, sizeof(buf), "%s/%s", path, namelist[i]->d_name);
|
||||
if (!lstat(buf, &st)) {
|
||||
if (show_perms)
|
||||
get_perms(&st, p, sizeof(p));
|
||||
if ((st.st_mode & S_IFMT) == S_IFDIR)
|
||||
t = '/';
|
||||
if (S_ISLNK(st.st_mode)) {
|
||||
snprintf(s, sizeof(s), "-> ");
|
||||
if (-1 == readlink(buf, &s[3], sizeof(s) - 3))
|
||||
s[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s%s%c%s\n", p, namelist[i]->d_name, t, s);
|
||||
if (t == '/')
|
||||
result += descend(buf, show_perms, dir);
|
||||
|
||||
free(namelist[i]);
|
||||
}
|
||||
} else {
|
||||
errno = ENOTDIR;
|
||||
result = -1;
|
||||
|
||||
free(namelist);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/* C implementation of UNIX which(1)
|
||||
*
|
||||
* Copyright (c) 2017 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:
|
||||
*/
|
||||
Reference in new issue
Block a user