diff options
Diffstat (limited to 'src/utilities/util_misc.c')
-rw-r--r-- | src/utilities/util_misc.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/utilities/util_misc.c b/src/utilities/util_misc.c new file mode 100644 index 00000000000..cf5f3e9c690 --- /dev/null +++ b/src/utilities/util_misc.c @@ -0,0 +1,115 @@ +/*- + * Copyright (c) 2008-2012 WiredTiger, Inc. + * All rights reserved. + * + * See the file LICENSE for redistribution information. + */ + +#include "util.h" + +int +util_cerr(const char *uri, const char *op, int ret) +{ + return (util_err(ret, "%s: cursor.%s", uri, op)); +} + +/* + * util_err -- + * Report an error. + */ +int +util_err(int e, const char *fmt, ...) +{ + va_list ap; + + (void)fprintf(stderr, "%s: ", progname); + if (fmt != NULL) { + va_start(ap, fmt); + (void)vfprintf(stderr, fmt, ap); + va_end(ap); + if (e != 0) + (void)fprintf(stderr, ": "); + } + if (e != 0) + (void)fprintf(stderr, "%s", wiredtiger_strerror(e)); + (void)fprintf(stderr, "\n"); + return (1); +} + +/* + * util_read_line -- + * Read a line from stdin into a ULINE. + */ +int +util_read_line(ULINE *l, int eof_expected, int *eofp) +{ + static unsigned long long line = 0; + uint32_t len; + int ch; + + ++line; + *eofp = 0; + + for (len = 0;; ++len) { + if ((ch = getchar()) == EOF) { + if (len == 0) { + if (eof_expected) { + *eofp = 1; + return (0); + } + return (util_err(0, + "line %llu: unexpected end-of-file", line)); + } + return (util_err(0, + "line %llu: no newline terminator", line)); + } + if (ch == '\n') + break; + /* + * We nul-terminate the string so it's easier to convert the + * line into a record number, that means we always need one + * extra byte at the end. + */ + if (l->memsize == 0 || len >= l->memsize - 1) { + if ((l->mem = + realloc(l->mem, l->memsize + 1024)) == NULL) + return (util_err(errno, NULL)); + l->memsize += 1024; + } + ((uint8_t *)l->mem)[len] = (uint8_t)ch; + } + + ((uint8_t *)l->mem)[len] = '\0'; /* nul-terminate */ + + return (0); +} + +/* + * util_str2recno -- + * Convert a string to a record number. + */ +int +util_str2recno(const char *p, uint64_t *recnop) +{ + uint64_t recno; + char *endptr; + + /* + * strtouq takes lots of things like hex values, signs and so on and so + * forth -- none of them are OK with us. Check the string starts with + * digit, that turns off the special processing. + */ + if (!isdigit(p[0])) + goto format; + + errno = 0; + recno = strtouq(p, &endptr, 0); + if (recno == ULLONG_MAX && errno == ERANGE) + return (util_err(ERANGE, "%s: invalid record number", p)); + + if (endptr[0] != '\0') +format: return (util_err(EINVAL, "%s: invalid record number", p)); + + *recnop = recno; + return (0); +} |