display.c

#include <sys/stat.h>

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lootsack.h"

int verbose = 0;

__dead void usage(const char *progname);

__dead void
usage(const char *progname)
{
	fprintf(stderr, "Usage:\n\t%s [-s] infile\n", progname);
	exit(1);
}


int
main(int argc, char **argv)
{
	struct treasure_file_header h;
	struct treasure *items;
	int stats = 0;
	int fd;
	size_t index;

	if (argc < 2)
		usage(*argv); /* does not return */

	if (pledge("stdio unveil rpath", NULL) == -1)
		err(1, "pledge");

	if (pledge(NULL, NULL) == -1)
		err(1, "finalize pledge");

	index = 1;
	if (strncmp("-s", argv[index], 2) == 0) {
		stats = 1;
		index++;
	}

	if (unveil(argv[index], "r") == -1)
		err(1, "unveil");

	if (unveil(NULL, NULL) == -1)
		err(1, "finalize unveil");

	if ((fd = open(argv[index], O_RDONLY)) == -1)
		err(1, "%s", argv[index]);

	if (read_treasure_bin(fd, &h, &items) == -1)
		err(1, "read_treasure_bin");

	if (stats == 0)
		print_treasure(items, h.num_entries);
	else
		print_stats(items, h.num_entries);

	return 0;
}