Closed oliverkwebb closed 8 months ago
Thanks for the PR!
I like most of the changes, but isn't introducing a global variable a step in the wrong direction? At least to me, ideally, there should be as little global state as possible, and functions should be as pure and "functional" as possible.
While yes, I would mostly agree that global variables are bad and there should be as little global state as possible. I felt like the code would be cleaner without having to specify fpo
in the output functions.
Error messages use fprintf(stderr, ...)
and xxd never acts like tee, it always has one output. So for most of the output functions it wouldn't make sense to specify anything but fpo
.
And the only reason fpo really exists in the code is because xxd can output to files, so fpo means "stdout, unless there is a second file argument, then that file". It's never modified beyond argument parsing.
If the original xxd didn't have a built-in "output to file" feature. The code would have used the global variable stdout
.
I WOULD suggest modifying stdout
since it's not const and you can do stdout = fopen(...
making fpo
completely redundant. But that breaks on different implementations of libc and is generally hack-y since the fileno of stdout
and STDOUT_FILENO
can be different
I agree with the reasoning! Thanks for testing it on musl as well. Merged.
This is some minor cleanup of the code with no big alterations to the logic or function of the program. Making
fpo
a global variable and removing it as a function argument for the*_or_die()
functions andxxdline()
because those functions are never invoked on anything butfpo
. And replacing most of the numbers that represent ASCII characters with their corresponding escape codes, since'\r'
is easier to interpret than13
.