-
avihhi, unix question if you don't mind. when using fread, it's possible to detect error from EOF using the feof and/or ferror interfaces, however, can such read error be detected in a shell script reading from stdin? can stdin have read error at all? if yes, as far as i know "read" does not distinguish read error from EOF, but data=$(cat) (with the caveats of LF truncation and finite stream) might error on read, right?
-
avihthe goal is to be absolutely sure that as far as the script can tell, the whole input has reached the shell script successfully.
-
bahamatavih: If there a short read of the file then cat would return an error and exit non-zero.
-
tomwww
-
avihbahamat: thanks. i think the first question is whether at all stdin can have read error which is distinguishable from EOF. first in a C program, and then in a shell script. and if the answer is yes for a shell script, then continue to ways of detecting this case
-
bahamatavih: Well fread() and a shell script using cat are different things. A userland process using fread is supposed to use ferror/feof. If it doesn't, that's probably a bug in that program. If you're writing a shell script you don't need to worry about what functions or system calls are being used. However the command behaves is the interface you need to deal with.
-
avihok, let's forget the shell script. if a c program uses fread on stdin, is there ever a use case where ferror returns true?
-
avihif yes, how to synthesize it?
-
avihi.e. how to invoke/set-it-up so that ferror(stdin) ends up true?
-
avihit's easy to synthesize a use case where fwrite ends in ferror, e.g. if the receiving side of pipe closes, then the writing side gets a write error. but how is it possible to get a read error? in a pipe, if the writing side closes, then i don't think the reading side gets an error. it only sees feof, not ferror