[hackers] [sbase] Handle stdin properly in cmp(1) and remove path comparison || FRIGN

From: <git_AT_suckless.org>
Date: Sat, 7 Feb 2015 22:54:32 +0100 (CET)

commit c0a2030c14c98f3eb08b8f5ee9ed939383de2296
Author: FRIGN <dev_AT_frign.de>
Date: Sat Feb 7 21:36:36 2015 +0100

    Handle stdin properly in cmp(1) and remove path comparison
    
    Same paths are undefined behaviour, let's not endorse it.

diff --git a/cmp.c b/cmp.c
index 435048f..b6ff20b 100644
--- a/cmp.c
+++ b/cmp.c
_AT_@ -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);
+ }
                 }
         }
 
_AT_@ -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;
 }
Received on Sat Feb 07 2015 - 22:54:32 CET

This archive was generated by hypermail 2.3.0 : Sat Feb 07 2015 - 23:00:14 CET