summaryrefslogtreecommitdiffstats
path: root/src/cumsum.c
diff options
context:
space:
mode:
authorDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-08-29 18:24:43 -0400
committerDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2025-08-29 18:24:43 -0400
commitbeb3e03072c706554acdfdd38dc7fb920ff2bb41 (patch)
tree0d0f199aa2b57fc9efccb6e5f769d5a0e814e0bb /src/cumsum.c
parent90e3df1407b6b8c608b362081d5d9b4802259aff (diff)
downloadmath-utils-beb3e03072c706554acdfdd38dc7fb920ff2bb41.tar.gz
Code cleanup+documentation
Diffstat (limited to 'src/cumsum.c')
-rw-r--r--src/cumsum.c63
1 files changed, 30 insertions, 33 deletions
diff --git a/src/cumsum.c b/src/cumsum.c
index 5648661..268a548 100644
--- a/src/cumsum.c
+++ b/src/cumsum.c
@@ -1,8 +1,7 @@
/*
- *
+ *
*/
-
#include "cumsum.h"
/*
@@ -13,32 +12,32 @@ static bool ARG_FP_INPUT = false;
static bool ARG_UINT_INPUT = false;
static bool ARG_HELP = false;
-static int parse_options(int argc, char*const* argv) {
+static int parse_options(int argc, char *const *argv) {
int arg_index = 0;
int arg;
bool error = false;
while ((arg = getopt(argc, argv, "frhu")) != -1) {
switch (arg) {
- case 'f':
- ARG_FP_INPUT = true;
- break;
- case 'u':
- ARG_UINT_INPUT = true;
- case 'h':
- ARG_HELP = true;
- break;
- case '?':
- if (isprint(optopt)) {
- fprintf(stderr, "Unknown option `-%c`.\n", optopt);
- } else {
- fprintf(stderr, "Unknown option character `\\x%x`.\n", optopt);
- }
- error = true;
- break;
- default:
- error = true;
- break;
+ case 'f':
+ ARG_FP_INPUT = true;
+ break;
+ case 'u':
+ ARG_UINT_INPUT = true;
+ case 'h':
+ ARG_HELP = true;
+ break;
+ case '?':
+ if (isprint(optopt)) {
+ fprintf(stderr, "Unknown option `-%c`.\n", optopt);
+ } else {
+ fprintf(stderr, "Unknown option character `\\x%x`.\n", optopt);
+ }
+ error = true;
+ break;
+ default:
+ error = true;
+ break;
}
}
@@ -54,17 +53,15 @@ static int parse_options(int argc, char*const* argv) {
return arg_index;
}
-static void help() {
- fprintf(stderr, "Usage:\ncumsum [-f|-u] [filename]\n");
-}
+static void help() { fprintf(stderr, "Usage:\ncumsum [-f|-u] [filename]\n"); }
void print_sum(Number sum) {
if (ARG_FP_INPUT) {
fprintf(stdout, "%lf\n", sum.d);
} else if (ARG_UINT_INPUT) {
- fprintf(stdout, "%ld\n", sum.u);
+ fprintf(stdout, "%lld\n", sum.u);
} else {
- fprintf(stdout, "%ld\n", sum.i);
+ fprintf(stdout, "%lld\n", sum.i);
}
}
@@ -79,7 +76,7 @@ static int read_data_fp(FILE *file, Number *num) {
static int read_data_int(FILE *file, Number *num) {
int64_t val;
- while (fscanf(file, "%ld ", &val) != EOF) {
+ while (fscanf(file, "%lld ", &val) != EOF) {
num->i += val;
}
@@ -88,7 +85,7 @@ static int read_data_int(FILE *file, Number *num) {
static int read_data_uint(FILE *file, Number *num) {
uint64_t val;
- while (fscanf(file, "%ld ", &val) != EOF) {
+ while (fscanf(file, "%lld ", &val) != EOF) {
num->u += val;
}
@@ -97,10 +94,10 @@ static int read_data_uint(FILE *file, Number *num) {
static int process_data(FILE *file) {
int rc = 1;
-
+
Number sum = {};
- /* FIXME: this could probably use a type-based macro to collapse the
+ /* FIXME: this could probably use a type-based macro to collapse the
if statements into a single macro call
*/
if (ARG_FP_INPUT) {
@@ -119,7 +116,6 @@ process_data_end:
return rc;
}
-
int main(int argc, char **argv) {
int rc = EXIT_SUCCESS;
int file_index = 0;
@@ -139,7 +135,8 @@ int main(int argc, char **argv) {
FILE *input_file;
if (file_index < argc && strcmp(argv[file_index], "-") != 0) {
if (!(input_file = fopen(argv[file_index], "r"))) {
- fprintf(stderr, "Error: Unable to open input file %s\n", argv[file_index]);
+ fprintf(stderr, "Error: Unable to open input file %s\n",
+ argv[file_index]);
rc = EXIT_FAILURE;
goto program_exit;
}