mirror of
https://github.com/troglobit/libite.git
synced 2026-11-06 15:50:57 +00:00
Merge branch 'abs0-master'
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
commit
de73e268d3
5 files changed
+164
-126
No files matched your search
@@ -28,11 +28,11 @@
|
|||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "strlite.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* fparseln() specific operation flags.
|
* fparseln() specific operation flags.
|
||||||
@@ -80,16 +80,6 @@ pid_t pidfile_poll (const char *pidfile);
|
|||||||
|
|
||||||
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||||
|
|
||||||
#ifndef strlcpy
|
|
||||||
size_t strlcpy (char *dst, const char *src, size_t siz);
|
|
||||||
#endif
|
|
||||||
#ifndef strlcat
|
|
||||||
size_t strlcat (char *dst, const char *src, size_t siz);
|
|
||||||
#endif
|
|
||||||
#ifndef strtonum
|
|
||||||
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int tree(char *path, int show_perms);
|
int tree(char *path, int show_perms);
|
||||||
|
|
||||||
#ifndef touch
|
#ifndef touch
|
||||||
@@ -154,63 +144,6 @@ static inline int fisslashdir(char *dir)
|
|||||||
return 0;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define min(a,b) \
|
|
||||||
({ \
|
|
||||||
__typeof__ (a) _a = (a); \
|
|
||||||
__typeof__ (b) _b = (b); \
|
|
||||||
_a < _b ? _a : _b; \
|
|
||||||
})
|
|
||||||
|
|
||||||
#define max(a,b) \
|
|
||||||
({ \
|
|
||||||
__typeof__ (a) _a = (a); \
|
|
||||||
__typeof__ (b) _b = (b); \
|
|
||||||
_a > _b ? _a : _b; \
|
|
||||||
})
|
|
||||||
|
|
||||||
/* Compat */
|
/* Compat */
|
||||||
#define copy_filep(src,dst) fcopyfile(src,dst)
|
#define copy_filep(src,dst) fcopyfile(src,dst)
|
||||||
#define pidfile_read_pid(file) pifile_read(file)
|
#define pidfile_read_pid(file) pifile_read(file)
|
||||||
|
|||||||
@@ -1,30 +1,12 @@
|
|||||||
.PHONY: all clean distclean
|
|
||||||
|
|
||||||
# Figure out root of library, unless used as submodule
|
|
||||||
ROOTDIR ?= $(shell pwd)
|
|
||||||
|
|
||||||
CC ?= $(CROSS)gcc
|
|
||||||
AR ?= $(CROSS)ar
|
|
||||||
STRIP ?= $(CROSS)strip
|
|
||||||
INSTALL := install --backup=off
|
|
||||||
STRIPINST := $(INSTALL) -s --strip-program=$(CROSS)strip -m 0755
|
|
||||||
CFLAGS += -g -O0
|
CFLAGS += -g -O0
|
||||||
CPPFLAGS += -D_GNU_SOURCE
|
CPPFLAGS += -D_GNU_SOURCE
|
||||||
ARFLAGS = crus
|
|
||||||
|
|
||||||
# Default to silent build, use V=1 to get verbose mode.
|
|
||||||
ifeq ($V,1)
|
ifeq ($V,1)
|
||||||
Q =
|
|
||||||
PRINT = @true
|
|
||||||
REDIRECT =
|
|
||||||
MAKEFLAGS =
|
|
||||||
VERBOSE = -v
|
VERBOSE = -v
|
||||||
|
REDIRECT =
|
||||||
else
|
else
|
||||||
Q = @
|
|
||||||
PRINT = @printf
|
|
||||||
REDIRECT = >/dev/null
|
|
||||||
MAKEFLAGS = --no-print-directory --silent
|
|
||||||
VERBOSE =
|
VERBOSE =
|
||||||
|
REDIRECT = >/dev/null
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Some tests may need to be run as root, e.g. pidfile()
|
# Some tests may need to be run as root, e.g. pidfile()
|
||||||
@@ -34,35 +16,15 @@ else
|
|||||||
SUDO =
|
SUDO =
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Default install paths
|
all: $(TARGET)
|
||||||
prefix ?= /usr/local
|
|
||||||
libdir ?= $(prefix)/lib
|
|
||||||
datadir ?= $(prefix)/share/doc/$(LIBNAME)
|
|
||||||
incdir ?= $(prefix)/include
|
|
||||||
|
|
||||||
# Pretty printing and GCC -M for auto dep files
|
|
||||||
%.o: %.c
|
|
||||||
$(PRINT) " CC $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
|
||||||
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -c -MMD -MP -o $@ $<
|
|
||||||
|
|
||||||
%: %.o
|
|
||||||
$(PRINT) " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
|
||||||
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
|
|
||||||
|
|
||||||
.PHONY: %.test
|
|
||||||
%.test: %.c
|
%.test: %.c
|
||||||
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
|
@printf " TEST $@\n"
|
||||||
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
@$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
||||||
$(Q)$(RM) $(OBJS)
|
@$(RM) $(OBJS)
|
||||||
$(Q)$(SUDO) ./$@ $(VERBOSE) $(REDIRECT)
|
@$(SUDO) ./$@ $(VERBOSE) $(REDIRECT)
|
||||||
|
|
||||||
|
|
||||||
# Default build rules for both main and unit test makefiles
|
|
||||||
all:: $(TARGET)
|
|
||||||
|
|
||||||
clean::
|
clean::
|
||||||
-$(Q)$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
-@$(RM) $(OBJS) $(DEPS) $(TARGET) $(SOLIB) $(STATICLIB)
|
||||||
|
-@$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest *.test
|
||||||
distclean::
|
|
||||||
-$(Q)$(RM) $(JUNK) unittest *.o *.a *.so* *.unittest *.test
|
|
||||||
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/* ==========================================================================
|
||||||
|
* strdupa.h - Re-implementation of glibc strdupa.
|
||||||
|
* --------------------------------------------------------------------------
|
||||||
|
* Copyright (c) 2009 William Ahern
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
* ==========================================================================
|
||||||
|
*/
|
||||||
|
#ifndef GNU_STRING_STRDUPA_H
|
||||||
|
#define GNU_STRING_STRDUPA_H
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(HAVE_STRDUPA)
|
||||||
|
#if defined(strdupa)
|
||||||
|
#define HAVE_STRDUPA 1
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !HAVE_STRDUPA
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
#include <string.h> /* memcpy(3) strlen(3) */
|
||||||
|
|
||||||
|
#define strdupa(src) (__extension__ ({ \
|
||||||
|
size_t len_ = strlen(src); \
|
||||||
|
char *dst_ = __builtin_alloca(len_ + 1); \
|
||||||
|
dst_[len_] = '\0'; \
|
||||||
|
(char *)memcpy(dst_, src, len_); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
#endif /* !HAVE_STRDUPA */
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* GNU_STRING_STRDUPA_H */
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
/* Collection of frog DNA
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008-2016 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LITE_STRING_H_
|
||||||
|
#define LITE_STRING_H_
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "strdupa.h"
|
||||||
|
|
||||||
|
#ifndef strlcpy
|
||||||
|
size_t strlcpy (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strlcat
|
||||||
|
size_t strlcat (char *dst, const char *src, size_t siz);
|
||||||
|
#endif
|
||||||
|
#ifndef strtonum
|
||||||
|
long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Convert string to natural number (0-2147483647), returns -1 on error. */
|
||||||
|
static inline int atonum(const char *str)
|
||||||
|
{
|
||||||
|
int val = -1;
|
||||||
|
const char *errstr;
|
||||||
|
|
||||||
|
if (str) {
|
||||||
|
val = strtonum(str, 0, INT32_MAX, &errstr);
|
||||||
|
if (errstr)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Validate string, non NULL and not zero length */
|
||||||
|
static inline int string_valid(const char *s)
|
||||||
|
{
|
||||||
|
return s && strlen(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Relaxed comparison, e.g., sys_string_match("small", "smaller") => TRUE */
|
||||||
|
static inline int string_match(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
size_t min = MIN(strlen(a), strlen(b));
|
||||||
|
|
||||||
|
return !strncasecmp(a, b, min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, e.g., sys_string_match("small", "smaller") => FALSE */
|
||||||
|
static inline int string_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen(a) == strlen(b) && !strcmp(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Strict comparison, like sys_string_compare(), but case insensitive,
|
||||||
|
* e.g., sys_string_match("small", "SmAlL") => TRUE
|
||||||
|
*/
|
||||||
|
static inline int string_case_compare(const char *a, const char *b)
|
||||||
|
{
|
||||||
|
return strlen (a) == strlen (b) && !strcasecmp (a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define min(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a < _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define max(a,b) \
|
||||||
|
({ \
|
||||||
|
__typeof__ (a) _a = (a); \
|
||||||
|
__typeof__ (b) _b = (b); \
|
||||||
|
_a > _b ? _a : _b; \
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LITE_STRING_H_ */
|
||||||
+2
-11
@@ -17,17 +17,9 @@
|
|||||||
|
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
|
||||||
#include <linux/version.h>
|
|
||||||
#include <stdlib.h> /* mkstemp() */
|
|
||||||
#include <stdio.h> /* fdopen() */
|
#include <stdio.h> /* fdopen() */
|
||||||
#include <sys/stat.h> /* umask() */
|
#include <sys/stat.h> /* umask() */
|
||||||
|
|
||||||
#ifndef O_TMPFILE /* Too old GLIBC or kernel */
|
|
||||||
#warning O_TMPFILE missing on your system, tempfile() may not work!
|
|
||||||
#define __O_TMPFILE 020000000
|
|
||||||
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) /* Define and let it fail at runtime */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tempfile - A secure tmpfile() replacement
|
* tempfile - A secure tmpfile() replacement
|
||||||
*
|
*
|
||||||
@@ -43,7 +35,7 @@
|
|||||||
*/
|
*/
|
||||||
FILE *tempfile(void)
|
FILE *tempfile(void)
|
||||||
{
|
{
|
||||||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
|
#ifdef O_TMPFILE /* Only on Linux, with fairly recent (G)LIBC */
|
||||||
int fd;
|
int fd;
|
||||||
mode_t oldmask;
|
mode_t oldmask;
|
||||||
|
|
||||||
@@ -55,8 +47,7 @@ FILE *tempfile(void)
|
|||||||
|
|
||||||
return fdopen(fd, "rw");
|
return fdopen(fd, "rw");
|
||||||
#else
|
#else
|
||||||
#warning Too old kernel, reverting to wrap unsafe tmpfile() ...
|
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
||||||
return tmpfile();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user