Imported Upstream version 2.7.4
This commit is contained in:
parent
fd413a3168
commit
c9cb2187ee
290 changed files with 7473 additions and 2607 deletions
|
|
@ -9,8 +9,8 @@ libparseconf_la_SOURCES = parseconf.c
|
|||
# 'dist', and is only required for actual build, in which case
|
||||
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
|
||||
# be built before anything else
|
||||
libcommon_la_SOURCES = common.c state.c upsconf.c
|
||||
libcommonclient_la_SOURCES = common.c state.c
|
||||
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
|
||||
libcommonclient_la_SOURCES = common.c state.c str.c
|
||||
# ensure inclusion of local implementation of missing systems functions
|
||||
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
|
||||
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
|
||||
|
|
|
|||
|
|
@ -82,8 +82,8 @@ build_triplet = @build@
|
|||
host_triplet = @host@
|
||||
target_triplet = @target@
|
||||
subdir = common
|
||||
DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am strerror.c \
|
||||
atexit.c setenv.c snprintf.c $(top_srcdir)/depcomp
|
||||
DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am atexit.c \
|
||||
snprintf.c setenv.c strerror.c $(top_srcdir)/depcomp
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_compare_version.m4 \
|
||||
$(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \
|
||||
|
|
@ -114,14 +114,14 @@ CONFIG_CLEAN_FILES =
|
|||
CONFIG_CLEAN_VPATH_FILES =
|
||||
LTLIBRARIES = $(noinst_LTLIBRARIES)
|
||||
libcommon_la_DEPENDENCIES = libparseconf.la @LTLIBOBJS@
|
||||
am_libcommon_la_OBJECTS = common.lo state.lo upsconf.lo
|
||||
am_libcommon_la_OBJECTS = common.lo state.lo str.lo upsconf.lo
|
||||
libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS)
|
||||
AM_V_lt = $(am__v_lt_@AM_V@)
|
||||
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
|
||||
am__v_lt_0 = --silent
|
||||
am__v_lt_1 =
|
||||
libcommonclient_la_DEPENDENCIES = libparseconf.la @LTLIBOBJS@
|
||||
am_libcommonclient_la_OBJECTS = common.lo state.lo
|
||||
am_libcommonclient_la_OBJECTS = common.lo state.lo str.lo
|
||||
libcommonclient_la_OBJECTS = $(am_libcommonclient_la_OBJECTS)
|
||||
libparseconf_la_LIBADD =
|
||||
am_libparseconf_la_OBJECTS = parseconf.lo
|
||||
|
|
@ -241,6 +241,7 @@ LD = @LD@
|
|||
LDFLAGS = @LDFLAGS@
|
||||
LIBAVAHI_CFLAGS = @LIBAVAHI_CFLAGS@
|
||||
LIBAVAHI_LIBS = @LIBAVAHI_LIBS@
|
||||
LIBDIR = @LIBDIR@
|
||||
LIBGD_CFLAGS = @LIBGD_CFLAGS@
|
||||
LIBGD_LDFLAGS = @LIBGD_LDFLAGS@
|
||||
LIBIPMI_CFLAGS = @LIBIPMI_CFLAGS@
|
||||
|
|
@ -382,8 +383,8 @@ libparseconf_la_SOURCES = parseconf.c
|
|||
# 'dist', and is only required for actual build, in which case
|
||||
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
|
||||
# be built before anything else
|
||||
libcommon_la_SOURCES = common.c state.c upsconf.c
|
||||
libcommonclient_la_SOURCES = common.c state.c
|
||||
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
|
||||
libcommonclient_la_SOURCES = common.c state.c str.c
|
||||
# ensure inclusion of local implementation of missing systems functions
|
||||
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
|
||||
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
|
||||
|
|
@ -456,6 +457,7 @@ distclean-compile:
|
|||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/common.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parseconf.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/state.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/upsconf.Plo@am__quote@
|
||||
|
||||
.c.o:
|
||||
|
|
|
|||
|
|
@ -584,47 +584,6 @@ char *xstrdup(const char *string)
|
|||
return p;
|
||||
}
|
||||
|
||||
/* modify in - strip all trailing instances of <sep> */
|
||||
char *rtrim(char *in, const char sep)
|
||||
{
|
||||
char seps[2] = { sep, '\0' };
|
||||
|
||||
return rtrim_m(in, seps);
|
||||
}
|
||||
|
||||
/* modify in - strip all trailing instances of each char in <seps> */
|
||||
char *rtrim_m(char *in, const char *seps)
|
||||
{
|
||||
char *p;
|
||||
|
||||
if (in && strlen(in)) {
|
||||
p = &in[strlen(in) - 1];
|
||||
|
||||
while ((p >= in) && (strchr(seps, *p) != NULL))
|
||||
*p-- = '\0';
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
/* modify in - strip all leading instances of <sep> */
|
||||
char* ltrim(char *in, const char sep)
|
||||
{
|
||||
char seps[2] = { sep, '\0' };
|
||||
|
||||
return ltrim_m(in, seps);
|
||||
}
|
||||
|
||||
/* modify in - strip all leading instances of each char in <seps> */
|
||||
char* ltrim_m(char *in, const char *seps)
|
||||
{
|
||||
if (in && strlen(in)) {
|
||||
while ((*in != '\0') && (strchr(seps, *in) != NULL))
|
||||
memmove(in, in + 1, strlen(in));
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
/* Read up to buflen bytes from fd and return the number of bytes
|
||||
read. If no data is available within d_sec + d_usec, return 0.
|
||||
On error, a value < 0 is returned (errno indicates error). */
|
||||
|
|
|
|||
607
common/str.c
Normal file
607
common/str.c
Normal file
|
|
@ -0,0 +1,607 @@
|
|||
/* str.c - Common string-related functions
|
||||
*
|
||||
* Copyright (C)
|
||||
* 2000 Russell Kroll <rkroll@exploits.org>
|
||||
* 2015 Daniele Pezzini <hyouko@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "str.h"
|
||||
|
||||
char *str_trim(char *string, const char character)
|
||||
{
|
||||
return str_rtrim(str_ltrim(string, character), character);
|
||||
}
|
||||
|
||||
char *str_trim_m(char *string, const char *characters)
|
||||
{
|
||||
return str_rtrim_m(str_ltrim_m(string, characters), characters);
|
||||
}
|
||||
|
||||
char *str_ltrim(char *string, const char character)
|
||||
{
|
||||
char characters[2] = { character, '\0' };
|
||||
|
||||
return str_ltrim_m(string, characters);
|
||||
}
|
||||
|
||||
char *str_ltrim_m(char *string, const char *characters)
|
||||
{
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0' ||
|
||||
characters == NULL ||
|
||||
*characters == '\0'
|
||||
)
|
||||
return string;
|
||||
|
||||
while (
|
||||
*string != '\0' &&
|
||||
strchr(characters, *string) != NULL
|
||||
)
|
||||
memmove(string, string + 1, strlen(string));
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
char *str_rtrim(char *string, const char character)
|
||||
{
|
||||
char characters[2] = { character, '\0' };
|
||||
|
||||
return str_rtrim_m(string, characters);
|
||||
}
|
||||
|
||||
char *str_rtrim_m(char *string, const char *characters)
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0' ||
|
||||
characters == NULL ||
|
||||
*characters == '\0'
|
||||
)
|
||||
return string;
|
||||
|
||||
ptr = &string[strlen(string) - 1];
|
||||
|
||||
while (
|
||||
ptr >= string &&
|
||||
strchr(characters, *ptr) != NULL
|
||||
)
|
||||
*ptr-- = '\0';
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
char *str_trim_space(char *string)
|
||||
{
|
||||
return str_rtrim_space(str_ltrim_space(string));
|
||||
}
|
||||
|
||||
char *str_ltrim_space(char *string)
|
||||
{
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0'
|
||||
)
|
||||
return string;
|
||||
|
||||
while (
|
||||
*string != '\0' &&
|
||||
isspace(*string)
|
||||
)
|
||||
memmove(string, string + 1, strlen(string));
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
char *str_rtrim_space(char *string)
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0'
|
||||
)
|
||||
return string;
|
||||
|
||||
ptr = &string[strlen(string) - 1];
|
||||
|
||||
while (
|
||||
ptr >= string &&
|
||||
isspace(*ptr)
|
||||
)
|
||||
*ptr-- = '\0';
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
int str_is_short(const char *string, const int base)
|
||||
{
|
||||
short number;
|
||||
|
||||
return str_to_short(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_short_strict(const char *string, const int base)
|
||||
{
|
||||
short number;
|
||||
|
||||
return str_to_short_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_ushort(const char *string, const int base)
|
||||
{
|
||||
unsigned short number;
|
||||
|
||||
return str_to_ushort(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_ushort_strict(const char *string, const int base)
|
||||
{
|
||||
unsigned short number;
|
||||
|
||||
return str_to_ushort_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_int(const char *string, const int base)
|
||||
{
|
||||
int number;
|
||||
|
||||
return str_to_int(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_int_strict(const char *string, const int base)
|
||||
{
|
||||
int number;
|
||||
|
||||
return str_to_int_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_uint(const char *string, const int base)
|
||||
{
|
||||
unsigned int number;
|
||||
|
||||
return str_to_uint(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_uint_strict(const char *string, const int base)
|
||||
{
|
||||
unsigned int number;
|
||||
|
||||
return str_to_uint_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_long(const char *string, const int base)
|
||||
{
|
||||
long number;
|
||||
|
||||
return str_to_long(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_long_strict(const char *string, const int base)
|
||||
{
|
||||
long number;
|
||||
|
||||
return str_to_long_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_ulong(const char *string, const int base)
|
||||
{
|
||||
unsigned long number;
|
||||
|
||||
return str_to_ulong(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_ulong_strict(const char *string, const int base)
|
||||
{
|
||||
unsigned long number;
|
||||
|
||||
return str_to_ulong_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_double(const char *string, const int base)
|
||||
{
|
||||
double number;
|
||||
|
||||
return str_to_double(string, &number, base);
|
||||
}
|
||||
|
||||
int str_is_double_strict(const char *string, const int base)
|
||||
{
|
||||
double number;
|
||||
|
||||
return str_to_double_strict(string, &number, base);
|
||||
}
|
||||
|
||||
int str_to_short(const char *string, short *number, const int base)
|
||||
{
|
||||
long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_long(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (
|
||||
num < SHRT_MIN ||
|
||||
num > SHRT_MAX
|
||||
) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_short_strict(const char *string, short *number, const int base)
|
||||
{
|
||||
long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_long_strict(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (
|
||||
num < SHRT_MIN ||
|
||||
num > SHRT_MAX
|
||||
) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_ushort(const char *string, unsigned short *number, const int base)
|
||||
{
|
||||
unsigned long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_ulong(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (num > USHRT_MAX) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_ushort_strict(const char *string, unsigned short *number, const int base)
|
||||
{
|
||||
unsigned long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_ulong_strict(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (num > USHRT_MAX) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_int(const char *string, int *number, const int base)
|
||||
{
|
||||
long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_long(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (
|
||||
num < INT_MIN ||
|
||||
num > INT_MAX
|
||||
) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_int_strict(const char *string, int *number, const int base)
|
||||
{
|
||||
long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_long_strict(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (
|
||||
num < INT_MIN ||
|
||||
num > INT_MAX
|
||||
) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_uint(const char *string, unsigned int *number, const int base)
|
||||
{
|
||||
unsigned long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_ulong(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (num > UINT_MAX) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_uint_strict(const char *string, unsigned int *number, const int base)
|
||||
{
|
||||
unsigned long num;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (!str_to_ulong_strict(string, &num, base))
|
||||
return 0;
|
||||
|
||||
if (num > UINT_MAX) {
|
||||
errno = ERANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*number = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_long(const char *string, long *number, const int base)
|
||||
{
|
||||
char *str;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0'
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
str = strdup(string);
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
|
||||
str_trim_space(str);
|
||||
|
||||
if (!str_to_long_strict(str, number, base)) {
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_long_strict(const char *string, long *number, const int base)
|
||||
{
|
||||
char *ptr = NULL;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0' ||
|
||||
isspace(*string)
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
*number = strtol(string, &ptr, base);
|
||||
|
||||
if (
|
||||
errno == EINVAL ||
|
||||
*ptr != '\0'
|
||||
) {
|
||||
*number = 0;
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (errno == ERANGE) {
|
||||
*number = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_ulong(const char *string, unsigned long *number, const int base)
|
||||
{
|
||||
char *str;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0'
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
str = strdup(string);
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
|
||||
str_trim_space(str);
|
||||
|
||||
if (!str_to_ulong_strict(str, number, base)) {
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_ulong_strict(const char *string, unsigned long *number, const int base)
|
||||
{
|
||||
char *ptr = NULL;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0' ||
|
||||
*string == '+' ||
|
||||
*string == '-' ||
|
||||
isspace(*string)
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
*number = strtoul(string, &ptr, base);
|
||||
|
||||
if (
|
||||
errno == EINVAL ||
|
||||
*ptr != '\0'
|
||||
) {
|
||||
*number = 0;
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (errno == ERANGE) {
|
||||
*number = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_double(const char *string, double *number, const int base)
|
||||
{
|
||||
char *str;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0'
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
str = strdup(string);
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
|
||||
str_trim_space(str);
|
||||
|
||||
if (!str_to_double_strict(str, number, base)) {
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int str_to_double_strict(const char *string, double *number, const int base)
|
||||
{
|
||||
char *ptr = NULL;
|
||||
|
||||
*number = 0;
|
||||
|
||||
if (
|
||||
string == NULL ||
|
||||
*string == '\0' ||
|
||||
isspace(*string)
|
||||
) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (base)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case 10:
|
||||
if (strlen(string) != strspn(string, "-+.0123456789Ee")) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
if (strlen(string) != strspn(string, "-+.0123456789ABCDEFabcdefXxPp")) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
*number = strtod(string, &ptr);
|
||||
|
||||
if (
|
||||
errno == EINVAL ||
|
||||
*ptr != '\0'
|
||||
) {
|
||||
*number = 0;
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (errno == ERANGE) {
|
||||
*number = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue