Beispiel #1
0
static const char *
odformatfp(char fchar __unused, const char *fmt)
{
	size_t isize;
	int digits;
	char *end, *hdfmt;

	isize = sizeof(double);
	switch (*fmt) {
	case 'F':
		isize = sizeof(float);
		fmt++;
		break;
	case 'D':
		isize = sizeof(double);
		fmt++;
		break;
	case 'L':
		isize = sizeof(long double);
		fmt++;
		break;
	default:
		if (isdigit((unsigned char)*fmt)) {
			errno = 0;
			isize = (size_t)strtoul(fmt, &end, 10);
			if (errno != 0 || isize == 0)
				errx(1, "%s: invalid size", fmt);
			fmt = (const char *)end;
		}
	}
	switch (isize) {
	case sizeof(float):
		digits = FLT_DIG;
		break;
	case sizeof(double):
		digits = DBL_DIG;
		break;
	default:
		if (isize == sizeof(long double))
			digits = LDBL_DIG;
		else
			errx(1, "unsupported floating point size %lu",
			    (u_long)isize);
	}

	asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"",
	    16UL / (u_long)isize, (u_long)isize, digits + 8, digits);
	if (hdfmt == NULL)
		err(1, NULL);
	odadd(hdfmt);
	free(hdfmt);

	return (fmt);
}
Beispiel #2
0
static void
odformat(const char *fmt)
{
	char fchar;

	while (*fmt != '\0') {
		switch ((fchar = *fmt++)) {
		case 'a':
			odadd("16/1 \"%3_u \" \"\\n\"");
			break;
		case 'c':
			odadd("16/1 \"%3_c \" \"\\n\"");
			break;
		case 'o': case 'u': case 'd': case 'x':
			fmt = odformatint(fchar, fmt);
			break;
		case 'f':
			fmt = odformatfp(fchar, fmt);
			break;
		default:
			errx(1, "%c: unrecognised format character", fchar);
		}
	}
}
Beispiel #3
0
static const char *
odformatint(char fchar, const char *fmt)
{
	unsigned long long n;
	size_t isize;
	int digits;
	char *end, *hdfmt;

	isize = sizeof(int);
	switch (*fmt) {
	case 'C':
		isize = sizeof(char);
		fmt++;
		break;
	case 'I':
		isize = sizeof(int);
		fmt++;
		break;
	case 'L':
		isize = sizeof(long);
		fmt++;
		break;
	case 'S':
		isize = sizeof(short);
		fmt++;
		break;
	default:
		if (isdigit((unsigned char)*fmt)) {
			errno = 0;
			isize = (size_t)strtoul(fmt, &end, 10);
			if (errno != 0 || isize == 0)
				errx(1, "%s: invalid size", fmt);
			if (isize != sizeof(char) && isize != sizeof(short) &&
			    isize != sizeof(int) && isize != sizeof(long))
				errx(1, "unsupported int size %lu",
				    (u_long)isize);
			fmt = (const char *)end;
		}
	}

	/*
	 * Calculate the maximum number of digits we need to
	 * fit the number. Overestimate for decimal with log
	 * base 8. We need one extra space for signed numbers
	 * to store the sign.
	 */
	n = (1ULL << (8 * isize)) - 1;
	digits = 0;
	while (n != 0) {
		digits++;
		n >>= (fchar == 'x') ? 4 : 3;
	}
	if (fchar == 'd')
		digits++;
	asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"",
	    16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits),
	    "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar);
	if (hdfmt == NULL)
		err(1, NULL);
	odadd(hdfmt);
	free(hdfmt);

	return (fmt);
}
Beispiel #4
0
void
oldsyntax(int argc, char ***argvp)
{
	static char empty[] = "", padding[] = PADDING;
	int ch;
	char *p, **argv;

#define	TYPE_OFFSET	7
	add("\"%07.7_Ao\n\"");
	add("\"%07.7_ao  \"");

	deprecated = 1;
	argv = *argvp;
	while ((ch = getopt(argc, argv,
	    "A:aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
		switch (ch) {
		case 'A':
			switch (*optarg) {
			case 'd': case 'o': case 'x':
				fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
				fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
				    *optarg;
				break;
			case 'n':
				fshead->nextfu->fmt = empty;
				fshead->nextfs->nextfu->fmt = padding;
				break;
			default:
				errx(1, "%s: invalid address base", optarg);
			}
			break;
		case 'a':
			odadd("16/1 \"%3_u \" \"\\n\"");
			break;
		case 'B':
		case 'o':
			odadd("8/2 \" %06o \" \"\\n\"");
			break;
		case 'b':
			odadd("16/1 \"%03o \" \"\\n\"");
			break;
		case 'c':
			odadd("16/1 \"%3_c \" \"\\n\"");
			break;
		case 'd':
			odadd("8/2 \"  %05u \" \"\\n\"");
			break;
		case 'D':
			odadd("4/4 \"     %010u \" \"\\n\"");
			break;
		case 'e':
		case 'F':
			odadd("2/8 \"          %21.14e \" \"\\n\"");
			break;
		case 'f':
			odadd("4/4 \" %14.7e \" \"\\n\"");
			break;
		case 'H':
		case 'X':
			odadd("4/4 \"       %08x \" \"\\n\"");
			break;
		case 'h':
		case 'x':
			odadd("8/2 \"   %04x \" \"\\n\"");
			break;
		case 'I':
		case 'L':
		case 'l':
			odadd("4/4 \"    %11d \" \"\\n\"");
			break;
		case 'i':
			odadd("8/2 \" %6d \" \"\\n\"");
			break;
		case 'j':
			if ((skip = strtol(optarg, &p, 0)) < 0)
				errx(1, "%s: bad skip value", optarg);
			switch(*p) {
			case 'b':
				skip *= 512;
				break;
			case 'k':
				skip *= 1024;
				break;
			case 'm':
				skip *= 1048576;
				break;
			}
			break;
		case 'N':
			if ((length = atoi(optarg)) < 0)
				errx(1, "%s: bad length value", optarg);
			break;
		case 'O':
			odadd("4/4 \"    %011o \" \"\\n\"");
			break;
		case 't':
			posixtypes(optarg);
			break;
		case 'v':
			vflag = ALL;
			break;
		case 'P':
		case 'p':
		case 's':
		case 'w':
		case '?':
		default:
			warnx("od(1) has been deprecated for hexdump(1).");
			if (ch != '?')
				warnx(
				    "hexdump(1) compatibility doesn't"
				    " support the -%c option%s",
				    ch, ch == 's' ? "; see strings(1)." : ".");
			oldusage();
		}

	if (fshead->nextfs->nextfs == NULL)
		odadd(" 8/2 \"%06o \" \"\\n\"");

	argc -= optind;
	*argvp += optind;

	if (argc)
		odoffset(argc, argvp);
}
Beispiel #5
0
/*
 * Interpret a POSIX-style -t argument.
 */
static void
posixtypes(char *type_string)
{
	int x, y, nbytes;

	while (*type_string) {
		switch (*type_string) {
		case 'a':
			type_string++;
			odadd("16/1 \"%3_u \" \"\\n\"");
			break;
		case 'c':
			type_string++;
			odadd("16/1 \"%3_c \" \"\\n\"");
			break;
		case 'f':
			type_string++;
			if        (*type_string == 'F' ||
				   *type_string == '4') {
				type_string++;
				odadd("4/4 \" %14.7e\" \"\\n\"");
			} else if (*type_string == 'L' ||
				   *type_string == '8') {
				type_string++;
				odadd("2/8 \" %16.14e\" \"\\n\"");
			} else if (*type_string == 'D')
				/* long doubles vary in size */
				oldusage();
			else
				odadd("2/8 \" %16.14e\" \"\\n\"");
			break;
		case 'd':
			x = 0;
			goto extensions;
		case 'o':
			x = 1;
			goto extensions;
		case 'u':
			x = 2;
			goto extensions;
		case 'x':
			x = 3;
		extensions:
			type_string++;
			y = 2;
			if (isupper(*type_string)) {
				switch(*type_string) {
				case 'C':
					nbytes = sizeof(char);
					break;
				case 'S':
					nbytes = sizeof(short);
					break;
				case 'I':
					nbytes = sizeof(int);
					break;
				case 'L':
					nbytes = sizeof(long);
					break;
				default:
					warnx("Bad type-size qualifier '%c'",
					    *type_string);
					oldusage();
				}
				type_string++;
			} else if (isdigit(*type_string))
				nbytes = strtol(type_string, &type_string, 10);
			else
				nbytes = 4;

			switch (nbytes) {
			case 1:
				y = 0;
				break;
			case 2:
				y = 1;
				break;
			case 4:
				y = 2;
				break;
			case 8:
				y = 3;
				break;
			default:
				warnx("%d-byte integer formats are not "
				    "supported", nbytes);
				oldusage();
			}
			odadd(fmt[x][y]);
			break;
		default:
			oldusage();
		}
	}
}