mirror of
https://github.com/troglobit/libite.git
synced 2026-10-21 11:19:38 +00:00
doc: initial effort to enable doxygen
Framework from libuEv. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
03598fc01e
commit
6537b11e34
33 files changed
+3781
-275
No files matched your search
+11
-5
@@ -15,19 +15,25 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file chomp.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2014-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* chomp - Perl like chomp function, chop off last char(s) if newline.
|
||||
* @str: String to chomp
|
||||
* Perl like chomp function, chop off last char(s) if newline.
|
||||
* @param str String to chomp
|
||||
*
|
||||
* This function is like Perl chomp, but it's set to chop of all
|
||||
* trailing newlines. Useful in combination with fgets().
|
||||
*
|
||||
* Returns:
|
||||
* If @str is a valid pointer this function returns @str, otherwise
|
||||
* @errno is set to %EINVAL and this function returns %NULL.
|
||||
* @returns @a str, or @c NULL with @a errno set, if @a str is not a valid pointer.
|
||||
* @exception EINVAL if the input argument is not a valid pointer.
|
||||
*/
|
||||
char *chomp(char *str)
|
||||
{
|
||||
|
||||
+21
@@ -15,11 +15,32 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file conio.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2009-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* Probe terminal size
|
||||
* @param row pointer to integer to store number of rows
|
||||
* @param col pointer to integer to store number of columns
|
||||
*
|
||||
* This function checks if stdin and stdout isatty() and then sets the
|
||||
* TTY in raw mode to silently ask the size using ANSI escape sequences.
|
||||
* This is achieved by trying to go to corner 999,999 followed by
|
||||
* querying the cursor position. Afterwards the TTY is returned to the
|
||||
* state if was before, e.g. cooked. The number of rows and columns is
|
||||
* returned in the input arguments to this function.
|
||||
*
|
||||
* If stdio is @a not a TTY, then a default 24x80 is returned.
|
||||
*/
|
||||
void initscr(int *row, int *col)
|
||||
{
|
||||
if (!row || !col)
|
||||
|
||||
+34
-30
@@ -21,6 +21,13 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file copyfile.c
|
||||
* @author Claudio Matsuoka
|
||||
* @date 2008
|
||||
* @copyright MIT License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
@@ -94,28 +101,30 @@ static void set_mtime(int in, int out)
|
||||
}
|
||||
|
||||
/**
|
||||
* copyfile - Copy a file to another.
|
||||
* @src: Full path name to source file.
|
||||
* @dst: Full path name to target file.
|
||||
* @len: Number of bytes to copy, zero (0) for entire file.
|
||||
* @opt: An option mask of %LITE_FOPT_COPYFILE_SYM, %LITE_FOPT_KEEP_MTIME
|
||||
* Copy a file to another.
|
||||
* @param src Full path name to source file.
|
||||
* @param dst Full path name to target file.
|
||||
* @param len Number of bytes to copy, zero (0) for entire file.
|
||||
* @param 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
|
||||
* 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
|
||||
* 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:
|
||||
* The @a opt field replaces the @a sym argument in previous releases
|
||||
* and works as follows. To maintain backwards compatibility with @a
|
||||
* sym the ::LITE_FOPT_COPYFILE_SYM has a value of @c 1. Supported
|
||||
* option flags are:
|
||||
*
|
||||
* %LITE_FOPT_COPYFILE_SYM: Recreate symlink or follow to copy target
|
||||
* %LITE_FOPT_KEEP_MTIME: Preserve modification time
|
||||
* - ::LITE_FOPT_COPYFILE_SYM Recreate symlink or follow to copy target
|
||||
* - ::LITE_FOPT_KEEP_MTIME Preserve modification time
|
||||
*
|
||||
* Returns:
|
||||
* 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
|
||||
* will be set to %EISDIR since copyfile() is not recursive.
|
||||
* @returns The number of bytes copied, or zero, which may be an error
|
||||
* (check @a errno, see Exceptions below), but it may also indicate that
|
||||
* @a src was empty. See exceptions, below.
|
||||
*
|
||||
* @exception EISDIR if @a src is a directory, since copyfile() is not recursive.
|
||||
*/
|
||||
ssize_t copyfile(const char *src, const char *dst, int len, int opt)
|
||||
{
|
||||
@@ -203,20 +212,19 @@ exit:
|
||||
}
|
||||
|
||||
/**
|
||||
* movefile - Move a file to another location
|
||||
* @src: Source file.
|
||||
* @dst: Target file, or location.
|
||||
* Move a file to another location
|
||||
* @param src Source file.
|
||||
* @param dst Target file, or location.
|
||||
*
|
||||
* This is a C implementation of the command line mv(1) utility.
|
||||
* Usually the rename() API is sufficient, but not when moving across
|
||||
* file system boundaries.
|
||||
*
|
||||
* The @src argument must include the full path to the source file,
|
||||
* whereas the @dst argument may only be a directory, in which case the
|
||||
* same file name from @src is used.
|
||||
* The @p src argument must include the full path to the source file,
|
||||
* whereas the @p dst argument may only be a directory, in which case
|
||||
* the same file name from @p src is used.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with errno set.
|
||||
* @returns POSIX OK(0), or non-zero with @a errno set.
|
||||
*/
|
||||
int movefile(const char *src, const char *dst)
|
||||
{
|
||||
@@ -246,15 +254,11 @@ int movefile(const char *src, const char *dst)
|
||||
}
|
||||
|
||||
/**
|
||||
* fcopyfile - Copy between FILE *fp.
|
||||
* @src: Source FILE.
|
||||
* @dst: Destination FILE.
|
||||
* Copy between FILE *fp.
|
||||
* @param src Source FILE.
|
||||
* @param dst Destination FILE.
|
||||
*
|
||||
* Function takes signals into account and will restart the syscalls as
|
||||
* long as error is %EINTR.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with errno set on error.
|
||||
* @returns POSIX OK(0), or non-zero with @a errno set on error.
|
||||
*/
|
||||
int fcopyfile(FILE *src, FILE *dst)
|
||||
{
|
||||
|
||||
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file dir.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2008-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
@@ -48,32 +55,29 @@ static int matcher(const struct dirent *entry)
|
||||
}
|
||||
|
||||
/**
|
||||
* dir - List all files of a certain type in the given directory.
|
||||
* @dir: Base directory for dir operation.
|
||||
* @type: File type suffix, e.g. ".cfg".
|
||||
* @filter: Optional file name filter.
|
||||
* @list: Pointer to an array of file names.
|
||||
* @strip: Flag, if set dir() strips the file type.
|
||||
* List all files of a certain type in the given directory.
|
||||
* @param dir Base directory for dir operation.
|
||||
* @param type File type suffix, e.g. ".cfg".
|
||||
* @param filter Optional file name filter.
|
||||
* @param list Pointer to an array of file names.
|
||||
* @param strip Flag, if set dir() strips the file type.
|
||||
*
|
||||
* This function returns a @list of files, matching the @type suffix,
|
||||
* in the given directory @dir.
|
||||
* This function returns a @a list of files, matching the @a type
|
||||
* suffix, in the given directory @a dir.
|
||||
*
|
||||
* The @list argument is a pointer to where to store the dynamically
|
||||
* The @a list argument is a pointer to where to store the dynamically
|
||||
* allocated list of file names. This list should be free'd by first
|
||||
* calling free() on each file name and then on the list itself.
|
||||
*
|
||||
* If @filter is not %NULL it will be called for each file found. If
|
||||
* @filter returns non-zero the @file argument will be included in the
|
||||
* resulting @list. If @filter returns zero for given @file it will
|
||||
* be discarded.
|
||||
* If @a filter is not @c NULL it will be called for each file found.
|
||||
* If @a filter returns non-zero the @a file argument is included in the
|
||||
* resulting @a list. If @a filter returns zero for given @a file it is
|
||||
* discarded. If the @a strip flag is set the resulting @a list of
|
||||
* files has their file type stripped, including the dot. So a match
|
||||
* "config0.cfg" would be returned as "config0".
|
||||
*
|
||||
* If the @strip flag is set the resulting @list of files has their
|
||||
* file type stripped, including the dot. So a match "config0.cfg"
|
||||
* would be returned as "config0".
|
||||
*
|
||||
* Returns:
|
||||
* Number of files in @list, zero if no matching files of @type, or
|
||||
* non-zero on error with @errno set.
|
||||
* @returns The number of files in @a list, zero if no matching files of
|
||||
* @a type, or non-zero on error with @a errno set.
|
||||
*/
|
||||
int dir(const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip)
|
||||
{
|
||||
|
||||
+11
-5
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file erasef.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
@@ -24,16 +31,15 @@
|
||||
#include "lite.h"
|
||||
|
||||
/**
|
||||
* erasef - Like erase() but with formatted string support
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Like erase() but with formatted string support.
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* This is a wrapper for the erase() function in lite.h, lessening the
|
||||
* burden of having to compose the filename from parts in a seprate
|
||||
* buffer.
|
||||
*
|
||||
* Returns:
|
||||
* Upon successful completion erasef() returns POSIX OK(0), otherwise,
|
||||
* -1 is returned and errno is set to indicate the error.
|
||||
* @returns Upon successful completion erasef() returns POSIX OK(0),
|
||||
* otherwise, -1 is returned and @a errno is set to indicate the error.
|
||||
*/
|
||||
int erasef(const char *fmt, ...)
|
||||
{
|
||||
|
||||
+10
-4
@@ -21,15 +21,21 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fexist.c
|
||||
* @author Claudio Matsuoka
|
||||
* @date 2008
|
||||
* @copyright MIT License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* fexist - Check if a file exists in the file system.
|
||||
* @file: File to look for, with full path.
|
||||
* Check if a file exists in the file system.
|
||||
* @param file File to look for, with full path.
|
||||
*
|
||||
* Returns:
|
||||
* %TRUE(1) if the file exists, otherwise %FALSE(0).
|
||||
* @returns @c TRUE(1) if the @a file exists, otherwise @c FALSE(0).
|
||||
*/
|
||||
int fexist(const char *file)
|
||||
{
|
||||
|
||||
+10
-4
@@ -21,15 +21,21 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fisdir.c
|
||||
* @author Claudio Matsuoka
|
||||
* @date 2008
|
||||
* @copyright MIT License
|
||||
*/
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* fisdir - Check if a path exists and is a directory.
|
||||
* @path: Path to file or directory
|
||||
* Check if a path exists and is a directory.
|
||||
* @param path Path to file or directory
|
||||
*
|
||||
* Returns:
|
||||
* %TRUE(1) if @path exists and is a directory, otherwise %FALSE(0).
|
||||
* @returns @c TRUE(1) if @p path exists and is a directory, otherwise @c FALSE(0).
|
||||
*/
|
||||
int fisdir(const char *path)
|
||||
{
|
||||
|
||||
+13
-6
@@ -15,22 +15,29 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fopenf.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* fopenf - Open a file based on the formatted string and optional arguments
|
||||
* @mode: Last argument in optional list, if omitted EINVAL
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Open a file based on the formatted string and optional arguments
|
||||
* @param mode An fopen() mode string, e.g. "w+"
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* This function is an extension to the fopen() family, lessening the burden
|
||||
* of first having to compose the filename from parts in a seprate buffer.
|
||||
*
|
||||
* Returns:
|
||||
* Upon successful completion fopenf() a FILE pointer. Otherwise, NULL
|
||||
* is returned and errno is set to indicate the error.
|
||||
* @returns Upon successful completion, fopenf() returns a FILE pointer.
|
||||
* Otherwise, @c NULL is returned and @a errno is set to indicate the
|
||||
* error.
|
||||
*/
|
||||
FILE *fopenf(const char *mode, const char *fmt, ...)
|
||||
{
|
||||
|
||||
+26
-4
@@ -24,6 +24,13 @@
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fparseln.c
|
||||
* @author Christos Zoulas
|
||||
* @date 1997
|
||||
* @copyright 2-clause BSD License
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
@@ -63,10 +70,25 @@ isescaped(const char *sp, const char *p, int esc)
|
||||
}
|
||||
|
||||
|
||||
/* fparseln():
|
||||
* Read a line from a file parsing continuations ending in \
|
||||
* and eliminating trailing newlines, or comments starting with
|
||||
* the comment char.
|
||||
/**
|
||||
* Read a line from a file parsing continuations and trailing newlines
|
||||
* @param fp FILE pointer to read from
|
||||
* @param size The resulting length of the string, unused if @c NULL
|
||||
* @param lineno Incremented with number of lines read, unused if @c NULL
|
||||
* @param str Characters to look for, escape character, continuation, and comment
|
||||
* @param flags ::FPARSELN_UNESCCOMM, ::FPARSELN_UNESCCONT, ::FPARSELN_UNESCESC, ::FPARSELN_UNESCREST, ::FPARSELN_UNESCALL
|
||||
*
|
||||
* This function reads a line from a file, parsing continuations ending
|
||||
* in '\' and eliminating trailing newlines, or comments starting with
|
||||
* the comment char '#'.
|
||||
*
|
||||
* If @a size is not @c NULL, the resulting length of the returned string
|
||||
* is stored in @a size.
|
||||
*
|
||||
* If @a lineno is not @c NULL, it is incremented for each actual line
|
||||
* read from @a fp.
|
||||
*
|
||||
* @returns the line read from @a fp, or @c NULL on EOF or error.
|
||||
*/
|
||||
char *
|
||||
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
|
||||
|
||||
+11
-4
@@ -15,20 +15,27 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fremove.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* fremove - Remove a file based on the formatted string and optional arguments
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Remove a file based on the formatted string and optional arguments
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* This function is an extension to remove(), lessening the burden of
|
||||
* first having to compose the filename from parts in a seprate buffer.
|
||||
*
|
||||
* Returns:
|
||||
* Return value for remove(3).
|
||||
* @returns same as remove(3), with an extra @a errno, @c ENOBUFS if
|
||||
* alloca() fails to get a temporary buffer for composing the file name.
|
||||
*/
|
||||
int fremove(const char *fmt, ...)
|
||||
{
|
||||
|
||||
+18
-11
@@ -21,25 +21,32 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file fsendfile.c
|
||||
* @author Tobias Waldekranz
|
||||
* @date 2013
|
||||
* @copyright MIT License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/**
|
||||
* fsendfile - copy data between file streams
|
||||
* @src: Source stream
|
||||
* @dst: Destination stream
|
||||
* @len: Number of bytes to copy
|
||||
* Copy data between file streams.
|
||||
* @param src Source stream
|
||||
* @param dst Destination stream
|
||||
* @param len Number of bytes to copy
|
||||
*
|
||||
* @dst may be %NULL, in which case @len bytes are read and discarded
|
||||
* from @src. This can be useful for streams where seeking is not
|
||||
* permitted. Additionally, @len may be the special value zero (0), in
|
||||
* which case fsendfile() will copy until %EOF is seen on @src.
|
||||
* The @p dst argument may be @c NULL, in which case @a len bytes are
|
||||
* read and discarded from @a src. This can be useful for streams where
|
||||
* seeking is not permitted. Additionally, @a len may be the special
|
||||
* value zero (0), in which case fsendfile() copies until @c EOF is seen
|
||||
* on @a src.
|
||||
*
|
||||
* Returns:
|
||||
* The number of bytes copied. If an error is detected -1 is returned
|
||||
* and @errno will be set accordingly.
|
||||
* @returns The number of bytes copied. If an error is detected -1 is
|
||||
* returned and @a errno will be set accordingly.
|
||||
*/
|
||||
ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
|
||||
{
|
||||
|
||||
+14
-7
@@ -22,6 +22,14 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ifconfig.c
|
||||
* @author Claudio Matsuoka
|
||||
* @author Joachim Wiberg
|
||||
* @date 2008-2021
|
||||
* @copyright MIT License
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <net/if.h>
|
||||
@@ -34,14 +42,13 @@
|
||||
extern size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||
|
||||
/**
|
||||
* ifconfig - Basic ifconfig like operations on an interface
|
||||
* @ifname: Name of interface to operate on
|
||||
* @addr: If @up then set this optional IPv4 address
|
||||
* @mask: If @up and @addr, and @addr is not INADDR_ANY, then set netmask
|
||||
* @up: Control %IFF_UP flag on interface
|
||||
* Basic ifconfig like operations on an interface
|
||||
* @param ifname Name of interface to operate on
|
||||
* @param addr If @p up then set this optional IPv4 address
|
||||
* @param mask If @p up and @p addr, and @p addr is not @c INADDR_ANY, then set netmask
|
||||
* @param up Control @c IFF_UP flag on interface
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0) on success, or non-zero on error.
|
||||
* @returns POSIX OK(0) on success, or non-zero on error.
|
||||
*/
|
||||
int ifconfig(const char *ifname, const char *addr, const char *mask, int up)
|
||||
{
|
||||
|
||||
+38
-35
@@ -15,26 +15,33 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file lfile.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2015-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h> /* FILE */
|
||||
#include <stdlib.h> /* atoi() */
|
||||
#include <string.h> /* strlen(), strncmp(), strtok_r() */
|
||||
#include <sys/param.h> /* MAX() */
|
||||
|
||||
/** @private for internal use only */
|
||||
typedef struct lfile {
|
||||
FILE *fp;
|
||||
char buf[256];
|
||||
char *save;
|
||||
const char *sep;
|
||||
FILE *fp; /**< FILE pointer to current stream */
|
||||
char buf[256]; /**< Internal buffer, limted to 256 bytes per line */
|
||||
char *save; /**< Internal save pointer */
|
||||
const char *sep; /**< Record separator, from lfopen() */
|
||||
} lfile_t;
|
||||
|
||||
/**
|
||||
* lfopen - Open file and return parsing context
|
||||
* @file: File to parse
|
||||
* @sep: Separator(s) to use in lftok()
|
||||
* Open file and return parsing context.
|
||||
* @param file File to parse
|
||||
* @param sep Separator(s) to use in lftok()
|
||||
*
|
||||
* Returns:
|
||||
* Pointer to a &lftile_t parser context, or %NULL on error.
|
||||
* @returns Pointer to an @a lfile_t parser context, or @c NULL on error.
|
||||
*/
|
||||
lfile_t *lfopen(const char *file, const char *sep)
|
||||
{
|
||||
@@ -62,8 +69,8 @@ lfile_t *lfopen(const char *file, const char *sep)
|
||||
}
|
||||
|
||||
/**
|
||||
* lfclose - Close a parser context
|
||||
* @lf: Pointer to &lfile_t context from lfopen()
|
||||
* Close a parser context.
|
||||
* @param lf: Pointer to @a lfile_t parser context from lfopen()
|
||||
*/
|
||||
void lfclose(lfile_t *lf)
|
||||
{
|
||||
@@ -76,12 +83,11 @@ void lfclose(lfile_t *lf)
|
||||
}
|
||||
|
||||
/**
|
||||
* lftok - Get next token in file
|
||||
* @lf: Pointer to &lfile_t context from lfopen()
|
||||
* Get next token in file
|
||||
* @param lf: Pointer to @a lfile_t parser context from lfopen()
|
||||
*
|
||||
* Returns:
|
||||
* Next token, read from file previously opened with lfopen(),
|
||||
* or %NULL if EOF.
|
||||
* @returns Next token, read from file previously opened with lfopen(),
|
||||
* or @c NULL if EOF.
|
||||
*/
|
||||
char *lftok(lfile_t *lf)
|
||||
{
|
||||
@@ -110,18 +116,17 @@ char *lftok(lfile_t *lf)
|
||||
}
|
||||
|
||||
/**
|
||||
* lfgetkey - Find key in file
|
||||
* @lf: Pointer to &lfile_t context from lfopen()
|
||||
* @key: Key to look for
|
||||
* Find key in file
|
||||
* @param lf Pointer to @a lfile_t parser context from lfopen()
|
||||
* @param 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
|
||||
* Locate @a key from the current position in the file parser context
|
||||
* returned from lfopen(). Please note, the search for @a 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.
|
||||
* @returns The value to @a key, or @c NULL if not found.
|
||||
*/
|
||||
char *lfgetkey(lfile_t *lf, const char *key)
|
||||
{
|
||||
@@ -139,15 +144,14 @@ char *lfgetkey(lfile_t *lf, const char *key)
|
||||
}
|
||||
|
||||
/**
|
||||
* lfgetint - Same as lfgetkey() but returns an integer
|
||||
* @lf: Pointer to &lfile_t context from lfopen()
|
||||
* @key: Key to look for
|
||||
* Same as lfgetkey() but returns an integer.
|
||||
* @param lf Pointer to @a lfile_t parser context from lfopen()
|
||||
* @param key Key to look for
|
||||
*
|
||||
* This function is the same as lfgetkey() but returns the positive
|
||||
* integer value for the matching @key, if found.
|
||||
* integer value for the matching @a key, if found.
|
||||
*
|
||||
* Returns:
|
||||
* The positive integer value for @key, or -1 if not found.
|
||||
* @returns The positive integer value for @a key, or -1 if not found.
|
||||
*/
|
||||
int lfgetint(lfile_t *lf, const char *key)
|
||||
{
|
||||
@@ -160,16 +164,15 @@ int lfgetint(lfile_t *lf, const char *key)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Find the integer value for key in a file.
|
||||
* @param file File to search for @a key
|
||||
* @param sep Separator for tokens in @a file
|
||||
* @param key Key to look for in @a file
|
||||
*
|
||||
* This is a convenience wrapper for lfopen(), lfgetint(), and
|
||||
* lfclose().
|
||||
*
|
||||
* Returns:
|
||||
* The positive integer value for @key, or -1 if not found.
|
||||
* @returns The positive integer value for @a key, or -1 if not found.
|
||||
*/
|
||||
int fgetint(const char *file, const char *sep, const char *key)
|
||||
{
|
||||
|
||||
+58
-18
@@ -22,6 +22,17 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Collection of frog DNA
|
||||
* @file lite.h
|
||||
* @author Claudio Matsuoka (2008-2010)
|
||||
* @author Joachim Wiberg (2008-2021)
|
||||
* @copyright MIT License
|
||||
*
|
||||
* The latest version of this manual and the libite (-lite) software
|
||||
* library are available at https://github.com/troglobit/libite/
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
@@ -43,21 +54,21 @@ extern "C"
|
||||
/*
|
||||
* fparseln() specific operation flags.
|
||||
*/
|
||||
#define FPARSELN_UNESCESC 0x01
|
||||
#define FPARSELN_UNESCCONT 0x02
|
||||
#define FPARSELN_UNESCCOMM 0x04
|
||||
#define FPARSELN_UNESCREST 0x08
|
||||
#define FPARSELN_UNESCALL 0x0f
|
||||
#define FPARSELN_UNESCESC 0x01 /**< Remove escape preceding an escaped comment. */
|
||||
#define FPARSELN_UNESCCONT 0x02 /**< Remove escape preceding an escaped continuation. */
|
||||
#define FPARSELN_UNESCCOMM 0x04 /**< Remove escape preceding an escaped escape. */
|
||||
#define FPARSELN_UNESCREST 0x08 /**< Remove escape preceding any other character. */
|
||||
#define FPARSELN_UNESCALL 0x0f /**< All of the above. */
|
||||
|
||||
/*
|
||||
* copyfile() and rsync() option flags
|
||||
*/
|
||||
#define LITE_FOPT_RSYNC_DELETE 0x01
|
||||
#define LITE_FOPT_COPYFILE_SYM 0x01
|
||||
#define LITE_FOPT_KEEP_MTIME 0x02
|
||||
#define LITE_FOPT_RSYNC_DELETE 0x01 /**< Prune files from destination that are not in source */
|
||||
#define LITE_FOPT_COPYFILE_SYM 0x01 /**< Recreate symlink or follow to copy target */
|
||||
#define LITE_FOPT_KEEP_MTIME 0x02 /**< Preserve modification time */
|
||||
|
||||
typedef struct lfile lfile_t;
|
||||
typedef struct sdbuf sdbuf_t;
|
||||
typedef struct lfile lfile_t; /**< Opqaue context struct for lfile APIs */
|
||||
typedef struct sdbuf sdbuf_t; /**< Opqaue context struct for telnet APIs */
|
||||
|
||||
char *chomp (char *str);
|
||||
|
||||
@@ -120,6 +131,12 @@ int whichp (const char *cmd);
|
||||
#ifndef touch
|
||||
#include <sys/stat.h> /* utimensat() */
|
||||
#include <sys/time.h> /* utimensat() on *BSD */
|
||||
/**
|
||||
* Create a file, ignoring errors for already existing files
|
||||
* @param path Path to file to create.
|
||||
*
|
||||
* @returns POSIX OK(0), or non-zero on error.
|
||||
*/
|
||||
static inline int touch(const char *path)
|
||||
{
|
||||
if (utimensat(AT_FDCWD, path, NULL, 0)) {
|
||||
@@ -131,6 +148,12 @@ static inline int touch(const char *path)
|
||||
}
|
||||
#endif
|
||||
#ifndef makedir
|
||||
/**
|
||||
* Create a directory, ignoring errors for already existing files
|
||||
* @param path Path to directory to create.
|
||||
*
|
||||
* @returns POSIX OK(0), or non-zero on error.
|
||||
*/
|
||||
static inline int makedir(const char *path, mode_t mode)
|
||||
{
|
||||
if (mkdir(path, mode) && errno != EEXIST)
|
||||
@@ -139,6 +162,12 @@ static inline int makedir(const char *path, mode_t mode)
|
||||
}
|
||||
#endif
|
||||
#ifndef makefifo
|
||||
/**
|
||||
* Create a FIFO, ignoring errors for already existing files
|
||||
* @param path Path to FIFO to create.
|
||||
*
|
||||
* @returns POSIX OK(0), or non-zero on error.
|
||||
*/
|
||||
static inline int makefifo(const char *path, mode_t mode)
|
||||
{
|
||||
if (mkfifo(path, mode) && errno != EEXIST)
|
||||
@@ -147,6 +176,12 @@ static inline int makefifo(const char *path, mode_t mode)
|
||||
}
|
||||
#endif
|
||||
#ifndef erase
|
||||
/**
|
||||
* Remove a file, ignoring errors for missing files
|
||||
* @param path Path to file to remove.
|
||||
*
|
||||
* @returns POSIX OK(0), or non-zero on error.
|
||||
*/
|
||||
static inline int erase(const char *path)
|
||||
{
|
||||
if (remove(path) && errno != ENOENT)
|
||||
@@ -168,27 +203,32 @@ static inline int erase(const char *path)
|
||||
|
||||
/* Unline isset(), setbit() et al, these work with integers/shorts/longwords/etc. */
|
||||
#ifndef ISCLR
|
||||
#define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1))
|
||||
#define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1)) /**< Is bit cleared in word? */
|
||||
#endif
|
||||
#ifndef ISSET
|
||||
#define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0))
|
||||
#define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0)) /**< Is bit set in word? */
|
||||
#endif
|
||||
#ifndef ISOTHER
|
||||
#define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) /* Is any other bit set? */
|
||||
#define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) /**< Is any other bit set? */
|
||||
#endif
|
||||
#ifndef SETBIT
|
||||
#define SETBIT(word,bit) (word |= (1 << (bit)))
|
||||
#define SETBIT(word,bit) (word |= (1 << (bit))) /**< Set bit in word. */
|
||||
#endif
|
||||
#ifndef CLRBIT
|
||||
#define CLRBIT(word,bit) (word &= ~(1 << (bit)))
|
||||
#define CLRBIT(word,bit) (word &= ~(1 << (bit))) /**< Clear bit in word. */
|
||||
#endif
|
||||
|
||||
/* From The Practice of Programming, by Kernighan and Pike */
|
||||
#ifndef NELEMS
|
||||
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
|
||||
#define NELEMS(array) (sizeof(array) / sizeof(array[0])) /**< Number of elements in array. */
|
||||
#endif
|
||||
|
||||
/* Does directory end with a slash? */
|
||||
/**
|
||||
* Does directory end with a slash?
|
||||
* @param dir Path string to check.
|
||||
*
|
||||
* @returns @c TRUE(1) or @c FALSE(0)
|
||||
*/
|
||||
static inline int fisslashdir(const char *dir)
|
||||
{
|
||||
if (!dir)
|
||||
@@ -202,7 +242,7 @@ static inline int fisslashdir(const char *dir)
|
||||
|
||||
/* Compat */
|
||||
#define copy_filep(src, dst) fcopyfile(src, dst)
|
||||
#define pidfile_read_pid(file) pifile_read(file)
|
||||
#define pidfile_read_pid(file) pidfile_read(file)
|
||||
#define signal_pidfile(file, signo) pidfile_signal(file, signo)
|
||||
|
||||
#endif /* LITE_H_ */
|
||||
|
||||
+28
-23
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file makepath.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2013-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <stdarg.h>
|
||||
@@ -24,12 +31,11 @@
|
||||
#include "lite.h"
|
||||
|
||||
/**
|
||||
* 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
|
||||
* makepath() but takes a mode_t argument.
|
||||
* @param dir Directory to created, relative or absolute
|
||||
* @param mode A &mode_t mode to create @a dir with
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0) on success, otherwise -1 with @errno set.
|
||||
* @returns POSIX OK(0) on success, otherwise -1 with @a errno set.
|
||||
*/
|
||||
int mkpath(const char *dir, mode_t mode)
|
||||
{
|
||||
@@ -49,15 +55,13 @@ int mkpath(const char *dir, mode_t mode)
|
||||
}
|
||||
|
||||
/**
|
||||
* fmkpath - Formatted version of mkpath()
|
||||
* @mode: A &mode_t mode to create directories with
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Formatted version of mkpath().
|
||||
* @param mode A mode_t mode to create directories with
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* Note:
|
||||
* Notice the swapped arguments, compared to mkpath()!
|
||||
* @note Notice the swapped arguments, compared to mkpath()!
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0) on success, otherwise -1 with @errno set.
|
||||
* @returns POSIX OK(0) on success, otherwise -1 with @a errno set.
|
||||
*/
|
||||
int fmkpath(mode_t mode, const char *fmt, ...)
|
||||
{
|
||||
@@ -83,19 +87,20 @@ int fmkpath(mode_t mode, const char *fmt, ...)
|
||||
}
|
||||
|
||||
/**
|
||||
* makepath - Create all components of the specified directory.
|
||||
* @dir: Directory to create.
|
||||
* Create all components of the specified directory.
|
||||
* @param 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.
|
||||
* @note It is strongly recommended to use mkpath() over this function
|
||||
* since it has the @a mode argument while this function default to
|
||||
* 0777, which in most cases is insecure.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK (0) on success, otherwise -1 and errno set appropriately.
|
||||
* This function returns EINVAL on bad argument, or ENOMEM when it
|
||||
* fails allocating temporary memory. For other error codes see the
|
||||
* mkdir() syscall description.
|
||||
* @returns POSIX OK (0) on success, otherwise -1 and @a errno set
|
||||
* appropriately.
|
||||
*
|
||||
* @exception EINVAL on bad argument, or
|
||||
* @exception ENOMEM when it fails allocating temporary memory.
|
||||
*
|
||||
* For other error codes see the mkdir(2) syscall description.
|
||||
*/
|
||||
int makepath(const char *dir)
|
||||
{
|
||||
|
||||
+19
-2
@@ -31,6 +31,13 @@
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file pidfile.c
|
||||
* @author NetBSD Foundation Inc.
|
||||
* @date 1999
|
||||
* @copyright 2-clause BSD License
|
||||
*/
|
||||
|
||||
#include <sys/stat.h> /* utimensat() */
|
||||
#include <sys/time.h> /* utimensat() on *BSD */
|
||||
#include <sys/types.h>
|
||||
@@ -50,8 +57,18 @@ const char *__pidfile_path = _PATH_VARRUN; /* Note: includes trailing slash '/'
|
||||
const char *__pidfile_name = NULL;
|
||||
extern char *__progname;
|
||||
|
||||
int
|
||||
pidfile(const char *basename)
|
||||
/**
|
||||
* Create or update mtime of process PID file.
|
||||
* @param basename Program name, or @c NULL, may start with '/'
|
||||
*
|
||||
* This function is intended to be used by UNIX daemons to save the PID of the main process
|
||||
* responsible for handling signals. If @p basename is @c NULL the implicit @a __progname
|
||||
* variable from the C-library is used. The @p basename may also start with '/', in which
|
||||
* case it is interpreted as the absolute path to the PID file.
|
||||
*
|
||||
* @returns POSIX OK(0) on success, otherwise non-zero on error.
|
||||
*/
|
||||
int pidfile(const char *basename)
|
||||
{
|
||||
int save_errno;
|
||||
int atexit_already;
|
||||
|
||||
+31
-22
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file pidfilefn.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2009-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -24,16 +31,20 @@
|
||||
extern char *chomp(char *str);
|
||||
|
||||
/**
|
||||
* pidfile_read - Reads a PID value from a pidfile.
|
||||
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||
* Reads a PID value from a pidfile.
|
||||
* @param pidfile File containing PID, usually in @c /var/run/PROCNAME.pid
|
||||
*
|
||||
* This function takes a @pidfile and returns the PID found therein.
|
||||
* This function takes a @p pidfile and returns the PID found therein.
|
||||
*
|
||||
* Returns:
|
||||
* On invalid @pidfile -1 and errno set to %EINVAL, when @pidfile does not exist -1
|
||||
* and errno set to %ENOENT. When the pidfile is empty or when its contents cannot
|
||||
* be translated this function returns zero (0), on success this function returns
|
||||
* a PID value greater than one. PID 1 is reserved for the system init process.
|
||||
* @returns On invalid @p pidfile, -1 with @a errno set. If the @p
|
||||
* pidfile is empty, or when its contents cannot be translated, this
|
||||
* function returns zero (0), on success this function returns a PID
|
||||
* value greater than one.
|
||||
*
|
||||
* @note PID 1 is reserved for the system init process.
|
||||
*
|
||||
* @exception EINVAL on invalid @p pidfile, or
|
||||
* @exception ENOENT when @p pidfile does not exist.
|
||||
*/
|
||||
pid_t pidfile_read(const char *pidfile)
|
||||
{
|
||||
@@ -66,15 +77,14 @@ pid_t pidfile_read(const char *pidfile)
|
||||
}
|
||||
|
||||
/**
|
||||
* pidfile_poll - Poll for the existence of a pidfile and return PID
|
||||
* @pidfile: Path to pidfile to poll for
|
||||
* Poll for the existence of a pidfile and return PID.
|
||||
* @param pidfile Path to pidfile to poll for
|
||||
*
|
||||
* This function polls for the pidfile at @pidfile for at most 5 seconds
|
||||
* before timing out. If the file is created within that time span the
|
||||
* file is read and its PID contents returned.
|
||||
* This function polls for the pidfile at @p pidfile for at most 5
|
||||
* seconds before timing out. If the file is created within that time
|
||||
* span the file is read and its PID contents returned.
|
||||
*
|
||||
* Returns:
|
||||
* The PID read from @pidfile, or zero on timeout.
|
||||
* @returns The PID read from @p pidfile, or zero on timeout.
|
||||
*/
|
||||
pid_t pidfile_poll(const char *pidfile)
|
||||
{
|
||||
@@ -92,15 +102,14 @@ pid_t pidfile_poll(const char *pidfile)
|
||||
}
|
||||
|
||||
/**
|
||||
* pidfile_signal - Send signal to a PID and cleanup pidfile afterwards
|
||||
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||
* @signal: Signal to send to PID found in @pidfile.
|
||||
* Send signal to a PID and cleanup pidfile afterwards.
|
||||
* @param pidfile File containing PID, usually in @c /var/run/PROCNAME.pid
|
||||
* @param signal Signal to send to PID found in @p pidfile.
|
||||
*
|
||||
* If @signal is any of %SIGTERM, %SIGKILL, or if kill() returns -1 the
|
||||
* @pidfile is removed.
|
||||
* If @p signal is any of @c SIGTERM or @c SIGKILL, or if kill(2)
|
||||
* returns -1, the @p pidfile is removed.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0) on success, non-zero otherwise.
|
||||
* @returns POSIX OK(0) on success, non-zero otherwise.
|
||||
*/
|
||||
int pidfile_signal(const char *pidfile, int signal)
|
||||
{
|
||||
|
||||
+18
-7
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file progress.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2012-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <limits.h> /* INT_MAX */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -40,21 +47,21 @@ static char spinner(char *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.
|
||||
* Advanced ASCII progress bar with spinner
|
||||
* @param percent Start first call with this set to 0, end with 100
|
||||
* @param 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.
|
||||
* The progress bar will hide the cursor if started with @p percent 0
|
||||
* and show it again at the end, when called with @p 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.
|
||||
* 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)
|
||||
{
|
||||
@@ -85,6 +92,10 @@ void progress(int percent, int max_width)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative progress bar on systems where progress() doesn't work
|
||||
* @param percent Start first call with this set to 0, end with 100
|
||||
*/
|
||||
void progress_simple(int percent)
|
||||
{
|
||||
static int last = 1;
|
||||
|
||||
+16
-2
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file reallocarray.c
|
||||
* @author Otto Moerbeek
|
||||
* @date 2008
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
@@ -26,8 +33,15 @@
|
||||
*/
|
||||
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
|
||||
|
||||
void *
|
||||
reallocarray(void *optr, size_t nmemb, size_t size)
|
||||
/**
|
||||
* Similar to realloc() but for an array of items, like calloc()
|
||||
* @param optr Pointer to old (current) array
|
||||
* @param nmemb Number of elements
|
||||
* @param size Size of each element, in bytes
|
||||
*
|
||||
* @returns A pointer to the new array, or @c NULL on error.
|
||||
*/
|
||||
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) {
|
||||
|
||||
+24
-17
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file rsync.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2011-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h> /* AT_* macros */
|
||||
#include <stdlib.h> /* NULL, free() */
|
||||
@@ -34,31 +41,31 @@ static int set_mtime(char *fn, struct stat *st);
|
||||
|
||||
|
||||
/**
|
||||
* rsync - Synchronize contents and optionally remove non-existing backups
|
||||
* @src: Source directory
|
||||
* @dst: Destination directory
|
||||
* @opt: An option mask of %LITE_FOPT_RSYNC_DELETE, %LITE_FOPT_KEEP_MTIME
|
||||
* @filter: Optional filtering function for source directory.
|
||||
* Synchronize contents and optionally remove non-existing backups
|
||||
* @param src Source directory
|
||||
* @param dst Destination directory
|
||||
* @param opt An option mask of ::LITE_FOPT_RSYNC_DELETE, ::LITE_FOPT_KEEP_MTIME
|
||||
* @param filter Optional filtering function for source directory.
|
||||
*
|
||||
* This is a miniature implementation of the famous rsync for local use only.
|
||||
* In fact, it is not even a true rsync since it copies all files from @src
|
||||
* to @dst. The @delete option is useful for creating backups, when set all
|
||||
* files removed from src since last backup are pruned from the destination
|
||||
* (backup) directory.
|
||||
* This is a miniature implementation of the famous rsync for local use
|
||||
* only. In fact, it is not even a true rsync since it copies all files
|
||||
* from @p src to @p dst. The ::LITE_FOPT_RSYNC_DELETE @p opt flag is
|
||||
* useful for creating backups, when set all files removed from src
|
||||
* since last backup are pruned from the destination (backup) directory.
|
||||
*
|
||||
* The @opt parameter to rsync() is an option mask for the most common
|
||||
* rsync(1) options. Previously this argument was called @delete and
|
||||
* The @p opt parameter to rsync() is an option mask for the most common
|
||||
* rsync(1) options. Previously this argument was called @p delete and
|
||||
* 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_RSYNC_DELETE: Prune files from @p dst that no longer exist in @p 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.
|
||||
* The filter callback, @p 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:
|
||||
* POSIX OK(0), or non-zero with @errno set on error.
|
||||
* POSIX OK(0), or non-zero with @a errno set on error.
|
||||
*/
|
||||
int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
|
||||
{
|
||||
|
||||
@@ -15,12 +15,32 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file systemf.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
/**
|
||||
* Like system(), but takes a formatted string as argument.
|
||||
* @param fmt printf style format list to command to run
|
||||
*
|
||||
* This system() wrapper greatly simplifies operations that usually
|
||||
* consist of composing a command from parts into a dynamic buffer
|
||||
* before calling it. The return value from system() is also parsed,
|
||||
* checking for proper exit and signals.
|
||||
*
|
||||
* @returns If the command exits normally, the return code of the command
|
||||
* is returned. Otherwise, if the command is signalled, the return code
|
||||
* is -1 and @a errno is set to @c EINTR.
|
||||
*/
|
||||
int systemf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
+41
-22
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file telnet.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2010-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
@@ -33,12 +40,19 @@
|
||||
# define TELL(fmt, args...)
|
||||
#endif
|
||||
|
||||
/** @private for internal use only */
|
||||
struct sdbuf {
|
||||
int sd;
|
||||
int sd;
|
||||
char *buf;
|
||||
} sdbuf;
|
||||
};
|
||||
|
||||
/* Open telnet connection to @addr:@port and connect. */
|
||||
/**
|
||||
* Open telnet connection to addr:port and connect.
|
||||
* @param addr Integer encoded IPv4 address in network byte order
|
||||
* @param port Internet port number in network byte order
|
||||
*
|
||||
* @returns An @ref sdbuf_t socket buffer context.
|
||||
*/
|
||||
sdbuf_t *telnet_open(int addr, short port)
|
||||
{
|
||||
struct sockaddr_in *sin;
|
||||
@@ -93,6 +107,12 @@ sdbuf_t *telnet_open(int addr, short port)
|
||||
return ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a telnet session previously opened with telnet_open()
|
||||
* @param ctx An @ref sdbuf_t socket buffer context
|
||||
*
|
||||
* @returns Always returns POSIX OK(0).
|
||||
*/
|
||||
int telnet_close(sdbuf_t *ctx)
|
||||
{
|
||||
free(ctx->buf);
|
||||
@@ -137,20 +157,19 @@ static int wait_substr(sdbuf_t *ctx, char *str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Poor man's telnet expect
|
||||
* @param ctx Telnet session context from telnet_open()
|
||||
* @param script @c NULL terminated list of expect and response strings
|
||||
* @param output Optional output from session
|
||||
*
|
||||
* Issues @script sequence on telnet session specified in @ctx, with
|
||||
* optional @output from session.
|
||||
* Issues @p script sequence on telnet session specified in @p ctx, with
|
||||
* optional @p output from session.
|
||||
*
|
||||
* The @script consists of strings of expect and response pairs. For
|
||||
* The @p 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.
|
||||
* "assword: "with response "secret\n", concluded by @c NULL.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0) or non-zero on error.
|
||||
* @returns POSIX OK(0) or non-zero on error.
|
||||
*/
|
||||
int telnet_expect(sdbuf_t *ctx, char *script[], FILE *output)
|
||||
{
|
||||
@@ -258,20 +277,20 @@ int telnet_expect(sdbuf_t *ctx, char *script[], FILE *output)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Very simple expect-like implementation for telnet.
|
||||
* @param addr Integer encoded IPv4 address, in network byte order, use htonl().
|
||||
* @param port Internet port to connect to, in network byte order, use htons().
|
||||
* @param script Expect like script of paired 'expect', 'response' strings.
|
||||
* @param output A FILE pointer to a tempfile() 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.
|
||||
* The @p script is a query-response type of list of strings leading up
|
||||
* to a final command, which output is then written to the given @p
|
||||
* output file.
|
||||
*
|
||||
* Returns:
|
||||
* POSIX OK(0), or non-zero with errno set on error.
|
||||
* @returns POSIX OK(0), or non-zero with @a errno set on error.
|
||||
*/
|
||||
int telnet_session(int addr, short port, char *script[], FILE *output)
|
||||
{
|
||||
|
||||
+16
-7
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file tempfile.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2015-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <paths.h>
|
||||
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||
@@ -22,17 +29,19 @@
|
||||
#include <sys/stat.h> /* umask() */
|
||||
|
||||
/**
|
||||
* tempfile - A secure tmpfile() replacement
|
||||
* A secure tmpfile() replacement
|
||||
*
|
||||
* This is the secure replacement for tmpfile() that does not exist in
|
||||
* GLIBC. The function uses the Linux specific %O_TMPFILE and %O_EXCL
|
||||
* for security. When the %FILE is fclose()'ed the file contents is
|
||||
* lost. The file is hidden in the %_PATH_TMP directory on the system.
|
||||
* GLIBC. It uses the Linux specific @c O_TMPFILE and @c O_EXCL to hide
|
||||
* the filename. When the @c FILE is fclose()'ed, the file contents is
|
||||
* lost. The file is hidden in the @c _PATH_TMP ("/tmp") directory in
|
||||
* the system.
|
||||
*
|
||||
* This function requires Linux 3.11, or later, due to %O_TMPFILE.
|
||||
* This function requires Linux 3.11, or later, due to @c O_TMPFILE.
|
||||
* Not all file systems support hidden inodes, in which case this
|
||||
* function defaults to call tmpfile() as a fallback.
|
||||
*
|
||||
* Returns:
|
||||
* An open %FILE pointer, or %NULL on error.
|
||||
* @returns An open @c FILE pointer, or @c NULL on error.
|
||||
*/
|
||||
FILE *tempfile(void)
|
||||
{
|
||||
|
||||
+11
-5
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file touchf.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
@@ -24,16 +31,15 @@
|
||||
#include "lite.h"
|
||||
|
||||
/**
|
||||
* touchf - Like touch() but with formatted string support
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Like touch() but with formatted string support
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* This is a wrapper for the touch() function in lite.h, lessening the
|
||||
* burden of having to compose the filename from parts in a seprate
|
||||
* buffer.
|
||||
*
|
||||
* Returns:
|
||||
* Upon successful completion touchf() returns POSIX OK(0), otherwise,
|
||||
* -1 is returned and errno is set to indicate the error.
|
||||
* @returns Upon successful completion touchf() returns POSIX OK(0),
|
||||
* otherwise, -1 is returned and @a errno is set to indicate error.
|
||||
*/
|
||||
int touchf(const char *fmt, ...)
|
||||
{
|
||||
|
||||
+12
-6
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file truncatef.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
@@ -22,16 +29,15 @@
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* truncatef - Truncate a file based on the formatted string
|
||||
* @length: Size in bytes to truncate file to, zero to empty it
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
* Truncate a file based on the formatted string
|
||||
* @param length Size in bytes to truncate file to, zero to empty it
|
||||
* @param fmt Formatted string to be composed into a pathname
|
||||
*
|
||||
* This is an extension to the truncate() family, lessening the burden
|
||||
* of having to compose the filename from parts in a seprate buffer.
|
||||
*
|
||||
* Returns:
|
||||
* Upon successful completion truncate() returns POSIX OK(0), otherwise,
|
||||
* -1 is returned and errno is set to indicate the error.
|
||||
* @returns Upon successful completion truncate() returns POSIX OK(0),
|
||||
* otherwise, -1 is returned and @a errno is set to indicate error.
|
||||
*/
|
||||
int truncatef(off_t length, const char *fmt, ...)
|
||||
{
|
||||
|
||||
+17
-4
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file which.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2017-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h> /* snprintf() */
|
||||
@@ -38,8 +45,11 @@ static char *strip_args(char *path)
|
||||
return path;
|
||||
}
|
||||
|
||||
/*
|
||||
* Like which(1), returns a malloc'ed path to cmd on success, or NULL
|
||||
/**
|
||||
* Like which(1), or `command -v foo`
|
||||
* @param cmd Command to look for in $PATH
|
||||
*
|
||||
* @returns A malloc()'ed path to @a cmd on success, or @c NULL.
|
||||
*/
|
||||
char *which(const char *cmd)
|
||||
{
|
||||
@@ -103,8 +113,11 @@ char *which(const char *cmd)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Like which(), above, but only answers TRUE(1)/FALSE(0)
|
||||
/**
|
||||
* Predicate variant of which()
|
||||
* @param cmd Command to look for in $PATH
|
||||
*
|
||||
* @returns @c TRUE(1) or @c FALSE(0) if @p cmd exists in $PATH.
|
||||
*/
|
||||
int whichp(const char *cmd)
|
||||
{
|
||||
|
||||
+10
-4
@@ -15,6 +15,13 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file yorn.c
|
||||
* @author Joachim Wiberg
|
||||
* @date 2009-2021
|
||||
* @copyright ISC License
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio_ext.h> /* __fpurge() */
|
||||
#include <stdarg.h>
|
||||
@@ -56,14 +63,13 @@ static char rawgetch(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* yorn - Pose a a Yes or No question and return answer
|
||||
* @fmt: Standard printf() style argument(s).
|
||||
* Pose a a Yes or No question and return answer
|
||||
* @param 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.
|
||||
* @returns TRUE(1) or FALSE(0). True if the answer is yes.
|
||||
*/
|
||||
int yorn(const char *fmt, ...)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user