The code is very well written in my opinion. I have only very marginal questions:
in the main function you have a small repetition when checking argv to determine if the FILE should be closed or not. I would prefer checking
(in != stdin)
which is telling the real reason for the check. Alternatively I would simply neglect closing the files... since you are in the main function, at the end all file descriptors are guaranteed to be closed anyway.i would prefer
for(;;){...}
instead ofdo {...} while(1);
. I see three reasonreasons for this: it is shorter, you don't introduce the arbitrary constant 1, it is clear from the beginning that the loop has no termination condition.i would expect less invasive error checking. Since your
encode
anddecode
functions are perfect candidates to be reused in a library, you should not write errors tostderr
and should not terminate the execution withexit
. A better approach would be to use return codes as signals for errors and let the main function write the messages and terminate.i would always enclose with braces the blocks of an
if
statement even when there is a single line. This is to avoid the risk to forget the braces when a line is later added. As an alternative I would put the single line on the same line as theif
statement.