#include "linux_version.h"
#include "swapheader.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "blkdev.h"
#include "pathnames.h"
#include "c.h"
#include "writeall.h"
#include "swapheader.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#define SWAP_UUID_OFFSET (offsetof(struct swap_header_v1_2, uuid))
#include "nls.h"
#include "blkdev.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "common.h"
#include "gpt.h"
#include "mbsalign.h"
nls.h \
pathnames.h \
setproctitle.h \
- strtosize.h \
+ strutils.h \
swapheader.h \
tt.h \
usleep.h \
wholedisk.h \
widechar.h \
- writeall.h \
- xstrncpy.h
+ writeall.h
+++ /dev/null
-#ifndef UTIL_LINUX_STRTOSIZE
-#define UTIL_LINUX_STRTOSIZE
-
-#include <inttypes.h>
-
-extern int strtosize(const char *str, uintmax_t *res);
-
-#endif /* UTIL_LINUX_STRING_TO_NUMBE */
--- /dev/null
+#ifndef UTIL_LINUX_STRUTILS
+#define UTIL_LINUX_STRUTILS
+
+#include <inttypes.h>
+#include <string.h>
+
+extern int strtosize(const char *str, uintmax_t *res);
+extern long strtol_or_err(const char *str, const char *errmesg);
+
+#ifndef HAVE_STRNLEN
+extern size_t strnlen(const char *s, size_t maxlen);
+#endif
+#ifndef HAVE_STRNDUP
+extern char *strndup(const char *s, size_t n);
+#endif
+#ifndef HAVE_STRNCHR
+extern char *strnchr(const char *s, size_t maxlen, int c);
+#endif
+
+/* caller guarantees n > 0 */
+static inline void xstrncpy(char *dest, const char *src, size_t n)
+{
+ strncpy(dest, src, n-1);
+ dest[n-1] = 0;
+}
+#endif
+++ /dev/null
-/* NUL-terminated version of strncpy() */
-#include <string.h>
-
-/* caller guarantees n > 0 */
-static inline void xstrncpy(char *dest, const char *src, size_t n) {
- strncpy(dest, src, n-1);
- dest[n-1] = 0;
-}
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
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
+/*
+ * 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).
*
*
* 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;
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;
+}
#include <sys/socket.h>
#include <netdb.h>
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "pathnames.h"
#include "pathnames.h"
#include "login.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#ifndef TTY_MAJOR
#define TTY_MAJOR 4
#include "my_crypt.h"
#include "islocal.h"
#include "setpwnam.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "env.h"
#include "pathnames.h"
#include "my_crypt.h"
#include "login.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include <sys/utsname.h>
#include "linux_reboot.h"
#include "pathnames.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "usleep.h"
#include "my_crypt.h"
#include "pathnames.h"
#include "linux_reboot.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "simpleinit.h"
#include <unistd.h>
#include "setpwnam.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#ifdef HAVE_LIBSELINUX
#include <utmp.h>
#include "nls.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "ttymsg.h"
#include "pathnames.h"
#include "carefulputc.h"
sbin_PROGRAMS += blkid findfs wipefs
dist_man_MANS += blkid.8 findfs.8 wipefs.8
blkid_SOURCES = blkid.c $(top_srcdir)/lib/ismounted.c \
- $(top_srcdir)/lib/strtosize.c
+ $(top_srcdir)/lib/strutils.c
blkid_LDADD = $(ul_libblkid_la)
blkid_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
findfs_LDADD = $(ul_libblkid_la)
findfs_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
-wipefs_SOURCES = wipefs.c $(top_srcdir)/lib/strtosize.c
+wipefs_SOURCES = wipefs.c $(top_srcdir)/lib/strutils.c
wipefs_LDADD = $(ul_libblkid_la)
wipefs_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
if HAVE_STATIC_BLKID
#include <blkid.h>
#include "ismounted.h"
-#include "strtosize.h"
+#include "strutils.h"
const char *progname = "blkid";
#include "nls.h"
#include "xalloc.h"
-#include "strtosize.h"
+#include "strutils.h"
struct wipe_desc {
loff_t offset; /* magic string offset */
ldflags_static = -all-static
mount_SOURCES = mount.c $(srcs_mount) $(top_srcdir)/lib/setproctitle.c \
- $(top_srcdir)/lib/strtosize.c
+ $(top_srcdir)/lib/strutils.c
mount_CFLAGS = $(SUID_CFLAGS) $(cflags_common)
mount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
mount_LDADD = $(ldadd_common)
-umount_SOURCES = umount.c $(srcs_mount) $(top_srcdir)/lib/strtosize.c
+umount_SOURCES = umount.c $(srcs_mount) $(top_srcdir)/lib/strutils.c
umount_CFLAGS = $(SUID_CFLAGS) $(cflags_common)
umount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
umount_LDADD = $(ldadd_common)
swapon_LDADD = $(ldadd_common)
losetup_SOURCES = lomount.c $(srcs_common) loop.h lomount.h \
- $(top_srcdir)/lib/strtosize.c
+ $(top_srcdir)/lib/strutils.c
losetup_CPPFLAGS = -DMAIN $(AM_CPPFLAGS)
mount_static_LDADD =
#include "loop.h"
#include "lomount.h"
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "sundries.h"
#include "xmalloc.h"
#include <getopt.h>
#include <stdarg.h>
-#include "strtosize.h"
+#include "strutils.h"
static void
usage(FILE *f) {
#include "env.h"
#include "nls.h"
#include "blkdev.h"
-#include "strtosize.h"
+#include "strutils.h"
#define DO_PS_FIDDLING
#include "fstab.h"
#include "env.h"
#include "nls.h"
-#include "strtosize.h"
+#include "strutils.h"
#if defined(MNT_FORCE)
/* Interesting ... it seems libc knows about MNT_FORCE and presumably
if BUILD_SCHEDUTILS
-srcs_common = schedutils.c schedutils.h
+srcs_common = $(top_srcdir)/lib/strutils.c
usrbin_exec_PROGRAMS = chrt
dist_man_MANS = chrt.1
#include "c.h"
#include "nls.h"
-#include "schedutils.h"
+#include "strutils.h"
/* the SCHED_BATCH is supported since Linux 2.6.16
* -- temporary workaround for people with old glibc headers
break;
case 'p':
errno = 0;
- pid = getnum(argv[argc - 1], _("failed to parse pid"));
+ pid = strtol_or_err(argv[argc - 1], _("failed to parse pid"));
break;
case 'r':
policy = SCHED_RR;
}
errno = 0;
- priority = getnum(argv[optind], _("failed to parse priority"));
+ priority = strtol_or_err(argv[optind], _("failed to parse priority"));
#ifdef SCHED_RESET_ON_FORK
/* sanity check */
#include "nls.h"
-#include "schedutils.h"
+#include "strutils.h"
static int tolerant;
while ((c = getopt(argc, argv, "+n:c:p:th")) != EOF) {
switch (c) {
case 'n':
- ioprio = getnum(optarg, _("failed to parse class data"));
+ ioprio = strtol_or_err(optarg, _("failed to parse class data"));
set |= 1;
break;
case 'c':
- ioclass = getnum(optarg, _("failed to parse class"));
+ ioclass = strtol_or_err(optarg, _("failed to parse class"));
set |= 2;
break;
case 'p':
- pid = getnum(optarg, _("failed to parse pid"));
+ pid = strtol_or_err(optarg, _("failed to parse pid"));
break;
case 't':
tolerant = 1;
ioprio_print(pid);
for(; argv[optind]; ++optind) {
- pid = getnum(argv[optind], _("failed to parse pid"));
+ pid = strtol_or_err(argv[optind], _("failed to parse pid"));
ioprio_print(pid);
}
} else {
for(; argv[optind]; ++optind)
{
- pid = getnum(argv[optind], _("failed to parse pid"));
+ pid = strtol_or_err(argv[optind], _("failed to parse pid"));
ioprio_setpid(pid, ioprio, ioclass);
}
}
+++ /dev/null
-/*
- * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
- *
- * Released under the terms of the GNU General Public License version 2
- *
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <err.h>
-
-#include "nls.h"
-
-long getnum(const char *str, const char *errmesg)
-{
- long num;
- char *end = NULL;
-
- 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;
-}
+++ /dev/null
-#ifndef UTIL_LINUX_SCHED_UTILS
-#define UTIL_LINUX_SCHED_UTILS
-
-extern long getnum(const char *str, const char *errmesg);
-
-#endif
-
#include "cpuset.h"
#include "nls.h"
-#include "schedutils.h"
+#include "strutils.h"
static void __attribute__((__noreturn__)) usage(FILE *out)
{
while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) {
switch (opt) {
case 'p':
- pid = getnum(argv[argc - 1], _("failed to parse pid"));
+ pid = strtol_or_err(argv[argc - 1], _("failed to parse pid"));
break;
case 'c':
c_opt = 1;
/* utils.c */
extern char *mnt_getenv_safe(const char *arg);
-#ifndef HAVE_STRNLEN
-extern size_t strnlen(const char *s, size_t maxlen);
-#endif
-#ifndef HAVE_STRNDUP
-extern char *strndup(const char *s, size_t n);
-#endif
-#ifndef HAVE_STRNCHR
-extern char *strnchr(const char *s, size_t maxlen, int c);
-#endif
extern char *mnt_get_username(const uid_t uid);
/*
#endif
}
-/* TODO: move strn<...> functions to top-level lib/strn.c */
-#ifndef HAVE_STRNLEN
-size_t strnlen(const char *s, size_t maxlen)
-{
- int i;
-
- for (i = 0; i < maxlen; i++) {
- if (s[i] == '\0')
- return i + 1;
- }
- return maxlen;
-}
-#endif
-
-#ifndef HAVE_STRNCHR
-char *strnchr(const char *s, size_t maxlen, int c)
-{
- for (; maxlen-- && *s != '\0'; ++s)
- if (*s == (char)c)
- return (char *)s;
- return NULL;
-}
-#endif
-
-#ifndef HAVE_STRNDUP
-char *strndup(const char *s, size_t n)
-{
- size_t len = strnlen (s, n);
- char *new = (char *) malloc (len + 1);
-
- if (new == NULL)
- return NULL;
-
- new[len] = '\0';
- return (char *) memcpy (new, s, len);
-}
-#endif
-
-
/**
* mnt_fstype_is_pseudofs:
* @type: filesystem name
if BUILD_FALLOCATE
usrbin_exec_PROGRAMS += fallocate
-fallocate_SOURCES = fallocate.c $(top_srcdir)/lib/strtosize.c
+fallocate_SOURCES = fallocate.c $(top_srcdir)/lib/strutils.c
dist_man_MANS += fallocate.1
endif
#endif
#include "nls.h"
-#include "strtosize.h"
+#include "strutils.h"
static void __attribute__((__noreturn__)) usage(FILE *out)
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>
-#include "xstrncpy.h"
+#include "strutils.h"
#include "nls.h"
#include "xalloc.h"