summaryrefslogtreecommitdiffstats
path: root/testutil/parse-signed.c
blob: f5e6d3570615c0598ae3be6057c11aabf7dbba88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* See LICENSE file for copyright and license details. */
#include "../libsyscalls.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage" /* clang is just being silly */
#endif


int
main(int argc, char **argv)
{
	int sign, neg = -1;
	size_t bits;
	const char *xval;
	enum libsyscalls_error err;
	unsigned long long int value;
	char *end;

	if (argc != 4) {
	usage:
		fprintf(stderr, "usage error\n");
		return 1;
	}

	sign = atoi(argv[1]);
	bits = (unsigned long long int)atoll(argv[2]);
	xval = argv[3];
	errno = 0;
	value = strtoull(xval, &end, 16);
	if (errno || *end)
		goto usage;

	err = libsyscalls_parse_signed_integer(value, (enum libsyscalls_datatype_sign_representation)sign, bits, &value, &neg);
	if (err == LIBSYSCALLS_E_INVAL) {
		printf("inval\n");
		goto out;
	} else if (err) {
		fprintf(stderr, "parse-signed %s %s %s: ", argv[1], argv[2], argv[3]);
		libsyscalls_perror(NULL, err);
		return 1;
	}
	if (neg != 0 && neg != 1)
		abort();
	printf("%.*s%0*llX\n", neg, "-", (int)strlen(xval), value);

out:
	if (fflush(stdout) || fclose(stdout)) {
		perror(NULL);
		return 1;
	}
	return 0;
}