sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit cb1b5d532b998148c6feb03269bf9f76e4c7ca86
parent d756dca999e856ce1d71d3ecd5ef15bf7f32b9f5
Author: FRIGN <dev@frign.de>
Date:   Sat Feb  7 21:36:36 +0100

Handle stdin properly in cmp(1) and remove path comparison

Same paths are undefined behaviour, let's not endorse it.

Diffstat:
cmp.c | 25+++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/cmp.c b/cmp.c @@ -35,17 +35,17 @@ main(int argc, char *argv[]) if (argc != 2 || (lflag && sflag)) usage(); - if (!strcmp(argv[0], argv[1])) - return Same; - for (n = 0; n < 2; n++) { - if (argv[n][0] == '-' && !argv[n][1]) - argv[n] = "/dev/fd/0"; - fp[n] = fopen(argv[n], "r"); - if (!fp[n]) { - if (!sflag) - weprintf("fopen %s:", argv[n]); - exit(Error); + if (argv[n][0] == '-' && !argv[n][1]) { + argv[n] = "<stdin>"; + fp[n] = stdin; + } else { + fp[n] = fopen(argv[n], "r"); + if (!fp[n]) { + if (!sflag) + weprintf("fopen %s:", argv[n]); + exit(Error); + } } } @@ -76,5 +76,10 @@ main(int argc, char *argv[]) same = 0; } } + + for (n = 1; n < 2; n++) + if (fp[n] != stdin) + fclose(fp[n]); + return same ? Same : Diff; }