From 0fa85040b328a9ac0c4294207ddbec7eeee4f2de Mon Sep 17 00:00:00 2001 From: Kat Richey Date: Sun, 27 Feb 2022 21:38:10 -0600 Subject: [PATCH] -u triggers setvbuf with _IONBF --- cat.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/cat.c b/cat.c index 118af26..3ccd213 100755 --- a/cat.c +++ b/cat.c @@ -18,15 +18,15 @@ int main(int argc, char *argv[]) { } } + if(using_unbuffered_output) { + setvbuf(stdin, NULL, _IONBF, 15); + setvbuf(stdout, NULL, _IONBF, 15); + } + if(argc == 1 || optind == argc) { cur_in_file = stdin; for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) { - if(using_unbuffered_output) { - write(1, &c, 1); - } - else { - fprintf(stdout, "%c", c); - } + fprintf(stdout, "%c", c); } fclose(cur_in_file); } @@ -43,12 +43,7 @@ int main(int argc, char *argv[]) { } } for(c = fgetc(cur_in_file); c != EOF; c = fgetc(cur_in_file)) { - if(using_unbuffered_output) { - write(1, &c, 1); - } - else { - fprintf(stdout, "%c", c); - } + fprintf(stdout, "%c", c); } fclose(cur_in_file); }