1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Keith Muller of the University of California, San Diego and Lance
|
|
|
|
* Visser of Convex Computer Corporation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-06 06:51:42 +00:00
|
|
|
#if 0
|
1998-05-13 07:33:54 +00:00
|
|
|
static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
|
1998-05-06 06:51:42 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:13:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2003-02-27 18:04:54 +00:00
|
|
|
#include <inttypes.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <limits.h>
|
2013-05-10 18:43:36 +00:00
|
|
|
#include <signal.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "dd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2002-02-02 06:24:13 +00:00
|
|
|
static int c_arg(const void *, const void *);
|
|
|
|
static int c_conv(const void *, const void *);
|
|
|
|
static void f_bs(char *);
|
|
|
|
static void f_cbs(char *);
|
|
|
|
static void f_conv(char *);
|
|
|
|
static void f_count(char *);
|
|
|
|
static void f_files(char *);
|
2004-08-15 19:10:05 +00:00
|
|
|
static void f_fillchar(char *);
|
2002-02-02 06:24:13 +00:00
|
|
|
static void f_ibs(char *);
|
|
|
|
static void f_if(char *);
|
|
|
|
static void f_obs(char *);
|
|
|
|
static void f_of(char *);
|
|
|
|
static void f_seek(char *);
|
|
|
|
static void f_skip(char *);
|
2003-02-27 18:04:54 +00:00
|
|
|
static uintmax_t get_num(const char *);
|
2002-02-02 06:24:13 +00:00
|
|
|
static off_t get_off_t(const char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
static const struct arg {
|
1999-12-07 03:32:37 +00:00
|
|
|
const char *name;
|
2002-02-02 06:24:13 +00:00
|
|
|
void (*f)(char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
u_int set, noset;
|
|
|
|
} args[] = {
|
|
|
|
{ "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
|
|
|
|
{ "cbs", f_cbs, C_CBS, C_CBS },
|
|
|
|
{ "conv", f_conv, 0, 0 },
|
|
|
|
{ "count", f_count, C_COUNT, C_COUNT },
|
|
|
|
{ "files", f_files, C_FILES, C_FILES },
|
2004-08-15 19:10:05 +00:00
|
|
|
{ "fillchar", f_fillchar, C_FILL, C_FILL },
|
1994-05-26 06:18:55 +00:00
|
|
|
{ "ibs", f_ibs, C_IBS, C_BS|C_IBS },
|
|
|
|
{ "if", f_if, C_IF, C_IF },
|
2000-02-26 21:29:44 +00:00
|
|
|
{ "iseek", f_skip, C_SKIP, C_SKIP },
|
1994-05-26 06:18:55 +00:00
|
|
|
{ "obs", f_obs, C_OBS, C_BS|C_OBS },
|
|
|
|
{ "of", f_of, C_OF, C_OF },
|
2000-02-26 21:29:44 +00:00
|
|
|
{ "oseek", f_seek, C_SEEK, C_SEEK },
|
1994-05-26 06:18:55 +00:00
|
|
|
{ "seek", f_seek, C_SEEK, C_SEEK },
|
|
|
|
{ "skip", f_skip, C_SKIP, C_SKIP },
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *oper;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* args -- parse JCL syntax of dd.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:24:13 +00:00
|
|
|
jcl(char **argv)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
struct arg *ap, tmp;
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
in.dbsz = out.dbsz = 512;
|
|
|
|
|
1996-11-13 20:00:03 +00:00
|
|
|
while ((oper = *++argv) != NULL) {
|
1997-10-08 12:10:33 +00:00
|
|
|
if ((oper = strdup(oper)) == NULL)
|
1999-06-20 14:58:55 +00:00
|
|
|
errx(1, "unable to allocate space for the argument \"%s\"", *argv);
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((arg = strchr(oper, '=')) == NULL)
|
|
|
|
errx(1, "unknown operand %s", oper);
|
|
|
|
*arg++ = '\0';
|
|
|
|
if (!*arg)
|
|
|
|
errx(1, "no value specified for %s", oper);
|
|
|
|
tmp.name = oper;
|
1999-06-20 14:58:55 +00:00
|
|
|
if (!(ap = (struct arg *)bsearch(&tmp, args,
|
|
|
|
sizeof(args)/sizeof(struct arg), sizeof(struct arg),
|
|
|
|
c_arg)))
|
1994-05-26 06:18:55 +00:00
|
|
|
errx(1, "unknown operand %s", tmp.name);
|
|
|
|
if (ddflags & ap->noset)
|
1999-06-20 14:58:55 +00:00
|
|
|
errx(1, "%s: illegal argument combination or already set",
|
|
|
|
tmp.name);
|
1994-05-26 06:18:55 +00:00
|
|
|
ddflags |= ap->set;
|
|
|
|
ap->f(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final sanity checks. */
|
|
|
|
|
|
|
|
if (ddflags & C_BS) {
|
|
|
|
/*
|
|
|
|
* Bs is turned off by any conversion -- we assume the user
|
|
|
|
* just wanted to set both the input and output block sizes
|
|
|
|
* and didn't want the bs semantics, so we don't warn.
|
|
|
|
*/
|
2000-07-01 05:36:25 +00:00
|
|
|
if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
|
|
|
|
C_UNBLOCK))
|
1994-05-26 06:18:55 +00:00
|
|
|
ddflags &= ~C_BS;
|
|
|
|
|
|
|
|
/* Bs supersedes ibs and obs. */
|
2000-07-01 05:36:25 +00:00
|
|
|
if (ddflags & C_BS && ddflags & (C_IBS | C_OBS))
|
1994-05-26 06:18:55 +00:00
|
|
|
warnx("bs supersedes ibs and obs");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ascii/ebcdic and cbs implies block/unblock.
|
|
|
|
* Block/unblock requires cbs and vice-versa.
|
|
|
|
*/
|
1999-09-13 21:47:10 +00:00
|
|
|
if (ddflags & (C_BLOCK | C_UNBLOCK)) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (!(ddflags & C_CBS))
|
|
|
|
errx(1, "record operations require cbs");
|
|
|
|
if (cbsz == 0)
|
|
|
|
errx(1, "cbs cannot be zero");
|
|
|
|
cfunc = ddflags & C_BLOCK ? block : unblock;
|
|
|
|
} else if (ddflags & C_CBS) {
|
1999-09-13 21:47:10 +00:00
|
|
|
if (ddflags & (C_ASCII | C_EBCDIC)) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (ddflags & C_ASCII) {
|
|
|
|
ddflags |= C_UNBLOCK;
|
|
|
|
cfunc = unblock;
|
|
|
|
} else {
|
|
|
|
ddflags |= C_BLOCK;
|
|
|
|
cfunc = block;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
errx(1, "cbs meaningless if not doing record operations");
|
|
|
|
} else
|
|
|
|
cfunc = def;
|
1999-09-13 21:47:10 +00:00
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
/*
|
|
|
|
* Bail out if the calculation of a file offset would overflow.
|
|
|
|
*/
|
2003-02-27 18:04:54 +00:00
|
|
|
if (in.offset > OFF_MAX / (ssize_t)in.dbsz ||
|
|
|
|
out.offset > OFF_MAX / (ssize_t)out.dbsz)
|
|
|
|
errx(1, "seek offsets cannot be larger than %jd",
|
|
|
|
(intmax_t)OFF_MAX);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-02-02 06:24:13 +00:00
|
|
|
c_arg(const void *a, const void *b)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
1999-12-08 02:44:46 +00:00
|
|
|
return (strcmp(((const struct arg *)a)->name,
|
|
|
|
((const struct arg *)b)->name));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_bs(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
uintmax_t res;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
res = get_num(arg);
|
|
|
|
if (res < 1 || res > SSIZE_MAX)
|
2003-03-15 13:34:48 +00:00
|
|
|
errx(1, "bs must be between 1 and %jd", (intmax_t)SSIZE_MAX);
|
1999-09-12 16:51:53 +00:00
|
|
|
in.dbsz = out.dbsz = (size_t)res;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_cbs(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
uintmax_t res;
|
1999-06-20 14:58:55 +00:00
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
res = get_num(arg);
|
|
|
|
if (res < 1 || res > SSIZE_MAX)
|
2003-03-15 13:34:48 +00:00
|
|
|
errx(1, "cbs must be between 1 and %jd", (intmax_t)SSIZE_MAX);
|
1999-09-12 16:51:53 +00:00
|
|
|
cbsz = (size_t)res;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_count(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
intmax_t res;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2003-02-27 18:04:54 +00:00
|
|
|
res = (intmax_t)get_num(arg);
|
|
|
|
if (res < 0)
|
1999-09-16 05:12:59 +00:00
|
|
|
errx(1, "count cannot be negative");
|
2002-01-25 17:44:47 +00:00
|
|
|
if (res == 0)
|
2003-02-27 18:04:54 +00:00
|
|
|
cpy_cnt = (uintmax_t)-1;
|
2002-01-25 17:44:47 +00:00
|
|
|
else
|
2003-02-27 18:04:54 +00:00
|
|
|
cpy_cnt = (uintmax_t)res;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_files(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
1999-09-11 00:02:42 +00:00
|
|
|
files_cnt = get_num(arg);
|
1999-09-13 21:47:10 +00:00
|
|
|
if (files_cnt < 1)
|
2003-02-27 18:04:54 +00:00
|
|
|
errx(1, "files must be between 1 and %jd", (uintmax_t)-1);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2004-08-15 19:10:05 +00:00
|
|
|
static void
|
|
|
|
f_fillchar(char *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (strlen(arg) != 1)
|
|
|
|
errx(1, "need exactly one fill char");
|
|
|
|
|
|
|
|
fill_char = arg[0];
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_ibs(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
uintmax_t res;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-06-20 14:58:55 +00:00
|
|
|
if (!(ddflags & C_BS)) {
|
1999-09-12 16:51:53 +00:00
|
|
|
res = get_num(arg);
|
|
|
|
if (res < 1 || res > SSIZE_MAX)
|
2003-03-15 13:34:48 +00:00
|
|
|
errx(1, "ibs must be between 1 and %jd",
|
|
|
|
(intmax_t)SSIZE_MAX);
|
1999-09-13 21:47:10 +00:00
|
|
|
in.dbsz = (size_t)res;
|
1999-06-20 14:58:55 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_if(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
in.name = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_obs(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
uintmax_t res;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1999-06-20 14:58:55 +00:00
|
|
|
if (!(ddflags & C_BS)) {
|
1999-09-12 16:51:53 +00:00
|
|
|
res = get_num(arg);
|
|
|
|
if (res < 1 || res > SSIZE_MAX)
|
2003-03-15 13:34:48 +00:00
|
|
|
errx(1, "obs must be between 1 and %jd",
|
|
|
|
(intmax_t)SSIZE_MAX);
|
1999-09-12 16:51:53 +00:00
|
|
|
out.dbsz = (size_t)res;
|
1999-06-20 14:58:55 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_of(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
out.name = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_seek(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
out.offset = get_off_t(arg);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_skip(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
in.offset = get_off_t(arg);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
static const struct conv {
|
1999-12-07 03:32:37 +00:00
|
|
|
const char *name;
|
1994-05-26 06:18:55 +00:00
|
|
|
u_int set, noset;
|
1999-09-12 16:51:53 +00:00
|
|
|
const u_char *ctab;
|
1994-05-26 06:18:55 +00:00
|
|
|
} clist[] = {
|
|
|
|
{ "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
|
|
|
|
{ "block", C_BLOCK, C_UNBLOCK, NULL },
|
|
|
|
{ "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
|
|
|
|
{ "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
|
|
|
|
{ "lcase", C_LCASE, C_UCASE, NULL },
|
|
|
|
{ "noerror", C_NOERROR, 0, NULL },
|
|
|
|
{ "notrunc", C_NOTRUNC, 0, NULL },
|
|
|
|
{ "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
|
|
|
|
{ "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
|
|
|
|
{ "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
|
|
|
|
{ "osync", C_OSYNC, C_BS, NULL },
|
2004-03-05 19:35:51 +00:00
|
|
|
{ "pareven", C_PAREVEN, C_PARODD|C_PARSET|C_PARNONE, NULL},
|
|
|
|
{ "parnone", C_PARNONE, C_PARODD|C_PARSET|C_PAREVEN, NULL},
|
|
|
|
{ "parodd", C_PARODD, C_PAREVEN|C_PARSET|C_PARNONE, NULL},
|
|
|
|
{ "parset", C_PARSET, C_PARODD|C_PAREVEN|C_PARNONE, NULL},
|
1997-11-11 20:35:29 +00:00
|
|
|
{ "sparse", C_SPARSE, 0, NULL },
|
1994-05-26 06:18:55 +00:00
|
|
|
{ "swab", C_SWAB, 0, NULL },
|
|
|
|
{ "sync", C_SYNC, 0, NULL },
|
|
|
|
{ "ucase", C_UCASE, C_LCASE, NULL },
|
|
|
|
{ "unblock", C_UNBLOCK, C_BLOCK, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2002-02-02 06:24:13 +00:00
|
|
|
f_conv(char *arg)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
struct conv *cp, tmp;
|
|
|
|
|
|
|
|
while (arg != NULL) {
|
|
|
|
tmp.name = strsep(&arg, ",");
|
1999-09-12 16:51:53 +00:00
|
|
|
cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
|
|
|
|
sizeof(struct conv), c_conv);
|
|
|
|
if (cp == NULL)
|
1994-05-26 06:18:55 +00:00
|
|
|
errx(1, "unknown conversion %s", tmp.name);
|
|
|
|
if (ddflags & cp->noset)
|
|
|
|
errx(1, "%s: illegal conversion combination", tmp.name);
|
|
|
|
ddflags |= cp->set;
|
|
|
|
if (cp->ctab)
|
|
|
|
ctab = cp->ctab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-02-02 06:24:13 +00:00
|
|
|
c_conv(const void *a, const void *b)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
|
1999-12-08 02:44:46 +00:00
|
|
|
return (strcmp(((const struct conv *)a)->name,
|
|
|
|
((const struct conv *)b)->name));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-27 18:04:54 +00:00
|
|
|
* Convert an expression of the following forms to a uintmax_t.
|
1994-05-26 06:18:55 +00:00
|
|
|
* 1) A positive decimal number.
|
2004-07-31 15:13:08 +00:00
|
|
|
* 2) A positive decimal number followed by a 'b' or 'B' (mult by 512).
|
|
|
|
* 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
|
|
|
|
* 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
|
|
|
|
* 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
|
|
|
|
* 5) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int).
|
|
|
|
* 6) Two or more positive decimal numbers (with/without [BbKkMmGgWw])
|
|
|
|
* separated by 'x' or 'X' (also '*' for backwards compatibility),
|
|
|
|
* specifying the product of the indicated values.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2003-02-27 18:04:54 +00:00
|
|
|
static uintmax_t
|
2002-02-02 06:24:13 +00:00
|
|
|
get_num(const char *val)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
uintmax_t num, mult, prevnum;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *expr;
|
|
|
|
|
1999-06-20 14:58:55 +00:00
|
|
|
errno = 0;
|
2002-01-25 17:44:47 +00:00
|
|
|
num = strtouq(val, &expr, 0);
|
1999-09-12 16:51:53 +00:00
|
|
|
if (errno != 0) /* Overflow or underflow. */
|
1999-06-20 14:58:55 +00:00
|
|
|
err(1, "%s", oper);
|
|
|
|
|
1999-09-12 16:51:53 +00:00
|
|
|
if (expr == val) /* No valid digits. */
|
1994-05-26 06:18:55 +00:00
|
|
|
errx(1, "%s: illegal numeric value", oper);
|
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = 0;
|
1999-09-11 00:02:42 +00:00
|
|
|
switch (*expr) {
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'B':
|
1994-05-26 06:18:55 +00:00
|
|
|
case 'b':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = 512;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'K':
|
1994-05-26 06:18:55 +00:00
|
|
|
case 'k':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = 1 << 10;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'M':
|
1994-05-26 06:18:55 +00:00
|
|
|
case 'm':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = 1 << 20;
|
1999-06-20 14:58:55 +00:00
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'G':
|
1999-06-20 14:58:55 +00:00
|
|
|
case 'g':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = 1 << 30;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'W':
|
1994-05-26 06:18:55 +00:00
|
|
|
case 'w':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = sizeof(int);
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
2002-02-22 20:51:00 +00:00
|
|
|
default:
|
2002-05-11 03:12:02 +00:00
|
|
|
;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
if (mult != 0) {
|
|
|
|
prevnum = num;
|
|
|
|
num *= mult;
|
|
|
|
/* Check for overflow. */
|
|
|
|
if (num / mult != prevnum)
|
|
|
|
goto erange;
|
|
|
|
expr++;
|
|
|
|
}
|
|
|
|
|
1999-09-11 00:02:42 +00:00
|
|
|
switch (*expr) {
|
1994-05-26 06:18:55 +00:00
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case '*': /* Backward compatible. */
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'X':
|
1994-05-26 06:18:55 +00:00
|
|
|
case 'x':
|
2002-01-25 17:44:47 +00:00
|
|
|
mult = get_num(expr + 1);
|
|
|
|
prevnum = num;
|
|
|
|
num *= mult;
|
|
|
|
if (num / mult == prevnum)
|
1999-09-11 00:02:42 +00:00
|
|
|
break;
|
|
|
|
erange:
|
|
|
|
errx(1, "%s: %s", oper, strerror(ERANGE));
|
1994-05-26 06:18:55 +00:00
|
|
|
default:
|
|
|
|
errx(1, "%s: illegal numeric value", oper);
|
|
|
|
}
|
|
|
|
return (num);
|
|
|
|
}
|
1999-09-12 16:51:53 +00:00
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
/*
|
|
|
|
* Convert an expression of the following forms to an off_t. This is the
|
|
|
|
* same as get_num(), but it uses signed numbers.
|
|
|
|
*
|
2003-02-27 18:04:54 +00:00
|
|
|
* The major problem here is that an off_t may not necessarily be a intmax_t.
|
2002-01-25 17:44:47 +00:00
|
|
|
*/
|
1999-09-12 16:51:53 +00:00
|
|
|
static off_t
|
2002-02-02 06:24:13 +00:00
|
|
|
get_off_t(const char *val)
|
1999-09-12 16:51:53 +00:00
|
|
|
{
|
2003-02-27 18:04:54 +00:00
|
|
|
intmax_t num, mult, prevnum;
|
2002-01-25 17:44:47 +00:00
|
|
|
char *expr;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
num = strtoq(val, &expr, 0);
|
|
|
|
if (errno != 0) /* Overflow or underflow. */
|
|
|
|
err(1, "%s", oper);
|
|
|
|
|
|
|
|
if (expr == val) /* No valid digits. */
|
|
|
|
errx(1, "%s: illegal numeric value", oper);
|
|
|
|
|
|
|
|
mult = 0;
|
|
|
|
switch (*expr) {
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'B':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'b':
|
|
|
|
mult = 512;
|
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'K':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'k':
|
|
|
|
mult = 1 << 10;
|
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'M':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'm':
|
|
|
|
mult = 1 << 20;
|
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'G':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'g':
|
|
|
|
mult = 1 << 30;
|
|
|
|
break;
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'W':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'w':
|
|
|
|
mult = sizeof(int);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mult != 0) {
|
|
|
|
prevnum = num;
|
|
|
|
num *= mult;
|
|
|
|
/* Check for overflow. */
|
|
|
|
if ((prevnum > 0) != (num > 0) || num / mult != prevnum)
|
|
|
|
goto erange;
|
|
|
|
expr++;
|
|
|
|
}
|
1999-09-12 16:51:53 +00:00
|
|
|
|
2002-01-25 17:44:47 +00:00
|
|
|
switch (*expr) {
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case '*': /* Backward compatible. */
|
2004-07-31 15:13:08 +00:00
|
|
|
case 'X':
|
2002-01-25 17:44:47 +00:00
|
|
|
case 'x':
|
2003-02-27 18:04:54 +00:00
|
|
|
mult = (intmax_t)get_off_t(expr + 1);
|
2002-01-25 17:44:47 +00:00
|
|
|
prevnum = num;
|
|
|
|
num *= mult;
|
2002-02-07 02:54:30 +00:00
|
|
|
if ((prevnum > 0) == (num > 0) && num / mult == prevnum)
|
2002-01-25 17:44:47 +00:00
|
|
|
break;
|
|
|
|
erange:
|
|
|
|
errx(1, "%s: %s", oper, strerror(ERANGE));
|
|
|
|
default:
|
|
|
|
errx(1, "%s: illegal numeric value", oper);
|
|
|
|
}
|
|
|
|
return (num);
|
1999-09-12 16:51:53 +00:00
|
|
|
}
|