billdb_inspect.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2021 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <getopt.h>
  29. #include <errno.h>
  30. #include "../common/common_config.h"
  31. #include "../billdb/billdb.h"
  32. #define VERSION "0.1.0"
  33. void _version(FILE *fp) {
  34. fprintf(fp, "version: %s\n", VERSION);
  35. }
  36. void _usage(FILE *fp) {
  37. _version(fp);
  38. fprintf(fp, "usage:\n\n");
  39. fprintf(fp, " billdb_inspect [-h] [-v] [billdb]\n\n");
  40. fprintf(fp, " [billdb] billdbd file (defualt '%s')\n", BILLING_FILE);
  41. fprintf(fp, " [-h] help (this screen)\n");
  42. fprintf(fp, " [-v] show version\n");
  43. fprintf(fp, "\n");
  44. }
  45. struct option g_long_option[] = {
  46. {"version", no_argument, 0, 'v'},
  47. {"help", no_argument, 0, 'h'},
  48. {0,0,0,0}
  49. };
  50. char *g_bill_file = NULL;
  51. int main(int argc, char **argv) {
  52. int i, j, k;
  53. extern char *optarg;
  54. extern int optind;
  55. int option_index;
  56. int ch;
  57. int retcode = 0;
  58. int fd;
  59. ssize_t del_s=0;
  60. size_t offset=0, bill_size=0;
  61. billing_record *billmem = NULL;
  62. while ((ch = getopt_long(argc, argv, "hv", g_long_option, &option_index)) > 0) switch(ch) {
  63. case 'h':
  64. _usage(stdout);
  65. exit(0);
  66. break;
  67. case 'v':
  68. _version(stdout);
  69. exit(0);
  70. break;
  71. case 0:
  72. default:
  73. fprintf(stderr, "bad option (%i, %i)\n", option_index, (int)ch);
  74. _usage(stderr);
  75. exit(-2);
  76. break;
  77. }
  78. if (optind < argc) {
  79. g_bill_file = strdup(argv[optind]);
  80. } else {
  81. g_bill_file = strdup(BILLING_FILE);
  82. }
  83. //---
  84. bill_size = sizeof(billing_record)*NUM_BILLING_ENTRIES;
  85. billmem = (billing_record *)malloc(bill_size);
  86. fd = open(g_bill_file, O_RDONLY);
  87. if (fd<0) {
  88. perror(g_bill_file);
  89. retcode = -1;
  90. goto free_and_exit;
  91. }
  92. while (offset < bill_size) {
  93. del_s = read(fd, ((void *)billmem) + offset, bill_size - offset);
  94. if (del_s<0) {
  95. perror(g_bill_file);
  96. goto free_and_exit;
  97. }
  98. offset += del_s;
  99. }
  100. close(fd);
  101. //---
  102. for (i=0; i<NUM_BILLING_ENTRIES; i++) {
  103. printf("[%4i] ", i);
  104. for (j=0; j<BILLING_LINE_SIZE; j++) {
  105. if (isalpha(billmem[i].data[j])) {
  106. printf("%c", billmem[i].data[j]);
  107. }
  108. else {
  109. printf(".");
  110. }
  111. }
  112. printf(" (");
  113. for (j=0; j<BILLING_CHECKSUM_SIZE; j++) {
  114. printf("%02x", billmem[i].checksum[j]);
  115. }
  116. printf(")\n");
  117. }
  118. //---
  119. free_and_exit:
  120. if (g_bill_file) {
  121. free(g_bill_file);
  122. }
  123. if (billmem) {
  124. free(billmem);
  125. }
  126. exit(retcode);
  127. }