/*
* Copyright (c) 2021 Clementine Computing LLC.
*
* This file is part of PopuFare.
*
* PopuFare is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PopuFare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PopuFare. If not, see .
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "../common/common_config.h"
#include "../billdb/billdb.h"
#define VERSION "0.1.0"
void _version(FILE *fp) {
fprintf(fp, "version: %s\n", VERSION);
}
void _usage(FILE *fp) {
_version(fp);
fprintf(fp, "usage:\n\n");
fprintf(fp, " billdb_inspect [-h] [-v] [billdb]\n\n");
fprintf(fp, " [billdb] billdbd file (defualt '%s')\n", BILLING_FILE);
fprintf(fp, " [-h] help (this screen)\n");
fprintf(fp, " [-v] show version\n");
fprintf(fp, "\n");
}
struct option g_long_option[] = {
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
char *g_bill_file = NULL;
int main(int argc, char **argv) {
int i, j, k;
extern char *optarg;
extern int optind;
int option_index;
int ch;
int retcode = 0;
int fd;
ssize_t del_s=0;
size_t offset=0, bill_size=0;
billing_record *billmem = NULL;
while ((ch = getopt_long(argc, argv, "hv", g_long_option, &option_index)) > 0) switch(ch) {
case 'h':
_usage(stdout);
exit(0);
break;
case 'v':
_version(stdout);
exit(0);
break;
case 0:
default:
fprintf(stderr, "bad option (%i, %i)\n", option_index, (int)ch);
_usage(stderr);
exit(-2);
break;
}
if (optind < argc) {
g_bill_file = strdup(argv[optind]);
} else {
g_bill_file = strdup(BILLING_FILE);
}
//---
bill_size = sizeof(billing_record)*NUM_BILLING_ENTRIES;
billmem = (billing_record *)malloc(bill_size);
fd = open(g_bill_file, O_RDONLY);
if (fd<0) {
perror(g_bill_file);
retcode = -1;
goto free_and_exit;
}
while (offset < bill_size) {
del_s = read(fd, ((void *)billmem) + offset, bill_size - offset);
if (del_s<0) {
perror(g_bill_file);
goto free_and_exit;
}
offset += del_s;
}
close(fd);
//---
for (i=0; i