diff options
author | Davidlohr Bueso <dave@gnu.org> | 2010-11-16 10:47:35 -0300 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2010-11-23 21:06:49 +0100 |
commit | 8abcf2900297c6d53ead867c42f7c1688e8d52ca (patch) | |
tree | 77e2d666cd76d9d4c37e1c1864415c1e52d37926 /lib | |
parent | e8fc977aba09ddbd89b25276fd777c3f0eef9299 (diff) | |
download | util-linux-old-8abcf2900297c6d53ead867c42f7c1688e8d52ca.tar.gz |
lib: [strutils] general purpose string handling functions
This patch replaces a few functions used throughout the source:
* Renames getnum (from schedutils) to strtol_or_err
* Moves strtosize (from lib/strtosize.c)
* Moves xstrncpy (from include/xstrncpy.h)
* Adds strnlen, strnchr and strndup if not available (remove it from libmount utils)
A few Makefile.am files were modified to compile accordingly along with trivial renaming
in schedutils source code.
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 3 | ||||
-rw-r--r-- | lib/strutils.c (renamed from lib/strtosize.c) | 108 |
2 files changed, 74 insertions, 37 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index 45d319d3..9a3bf35b 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,7 +3,7 @@ include $(top_srcdir)/config/include-Makefile.am AM_CPPFLAGS += -DTEST_PROGRAM noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk test_mangle \ - test_strtosize test_tt test_canonicalize + test_tt test_canonicalize if LINUX if HAVE_CPU_SET_T noinst_PROGRAMS += test_cpuset @@ -14,7 +14,6 @@ test_blkdev_SOURCES = blkdev.c test_ismounted_SOURCES = ismounted.c test_wholedisk_SOURCES = wholedisk.c test_mangle_SOURCES = mangle.c -test_strtosize_SOURCES = strtosize.c if LINUX test_cpuset_SOURCES = cpuset.c endif diff --git a/lib/strtosize.c b/lib/strutils.c index 068c5429..f394800d 100644 --- a/lib/strtosize.c +++ b/lib/strutils.c @@ -1,4 +1,25 @@ /* + * Copyright (C) 2010 Karel Zak <kzak@redhat.com> + * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> + */ + +#include <stdlib.h> +#include <inttypes.h> +#include <ctype.h> +#include <errno.h> +#include <err.h> + +static int do_scale_by_power (uintmax_t *x, int base, int power) +{ + while (power--) { + if (UINTMAX_MAX / base < *x) + return -2; + *x *= base; + } + return 0; +} + +/* * strtosize() - convert string to size (uintmax_t). * * Supported suffixes: @@ -17,26 +38,7 @@ * * Note that the function does not accept numbers with '-' (negative sign) * prefix. - * - * Returns 0 on success, -1 in case of error, -2 in case of overflow. - * - * Copyright (C) 2010 Karel Zak <kzak@redhat.com> */ -#include <stdio.h> -#include <inttypes.h> -#include <ctype.h> -#include <errno.h> - -static int do_scale_by_power (uintmax_t *x, int base, int power) -{ - while (power--) { - if (UINTMAX_MAX / base < *x) - return -2; - *x *= base; - } - return 0; -} - int strtosize(const char *str, uintmax_t *res) { char *p; @@ -123,26 +125,62 @@ err: return -1; } -#ifdef TEST_PROGRAM +#ifndef HAVE_STRNLEN +size_t strnlen(const char *s, size_t maxlen) +{ + int i; -#include <stdio.h> -#include <stdlib.h> -#include <err.h> + for (i = 0; i < maxlen; i++) { + if (s[i] == '\0') + return i + 1; + } + return maxlen; +} +#endif -int main(int argc, char *argv[]) +#ifndef HAVE_STRNCHR +char *strnchr(const char *s, size_t maxlen, int c) { - uintmax_t size = 0; + for (; maxlen-- && *s != '\0'; ++s) + if (*s == (char)c) + return (char *)s; + return NULL; +} +#endif - if (argc < 2) { - fprintf(stderr, "usage: %s <number>[suffix]\n", argv[0]); - exit(EXIT_FAILURE); - } +#ifndef HAVE_STRNDUP +char *strndup(const char *s, size_t n) +{ + size_t len = strnlen(s, n); + char *new = (char *) malloc((len + 1) * sizeof(char)); + if (!new) + return NULL; + new[len] = '\0'; + return (char *) memcpy(new, s, len); +} +#endif - if (strtosize(argv[1], &size)) - errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]); +/* + * same as strtol(3) but exit on failure instead of returning crap + */ +long strtol_or_err(const char *str, const char *errmesg) +{ + long num; + char *end = NULL; - printf("%25s : %20ju\n", argv[1], size); - return EXIT_FAILURE; -} -#endif /* TEST_PROGRAM */ + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtol(str, &end, 10); + if (errno || (end && *end)) + goto err; + + return num; +err: + if (errno) + err(EXIT_FAILURE, "%s: '%s'", errmesg, str); + else + errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); + return 0; +} |