Author: bapt
Date: Mon Jun  1 09:09:36 2020
New Revision: 361689
URL: https://svnweb.freebsd.org/changeset/base/361689

Log:
  diff: restore compatibility with GNU diff regarding -N option
  
  When -N is used the missing files are treated as empty.
  
  PR:           233402
  Submitted by: Fehmi Noyan Isi <fnoyan...@yahoo.com>
  Reported by:  Roman Neuhauser <ro...@sigpipe.cz>
  MFC after:    3 days
  Differential Revision:        D25081

Modified:
  head/usr.bin/diff/diff.c
  head/usr.bin/diff/tests/diff_test.sh

Modified: head/usr.bin/diff/diff.c
==============================================================================
--- head/usr.bin/diff/diff.c    Mon Jun  1 09:01:13 2020        (r361688)
+++ head/usr.bin/diff/diff.c    Mon Jun  1 09:09:36 2020        (r361689)
@@ -27,6 +27,7 @@ __FBSDID("$FreeBSD$");
 
 #include <ctype.h>
 #include <err.h>
+#include <errno.h>
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -351,13 +352,33 @@ main(int argc, char **argv)
        if (strcmp(argv[0], "-") == 0) {
                fstat(STDIN_FILENO, &stb1);
                gotstdin = 1;
-       } else if (stat(argv[0], &stb1) != 0)
-               err(2, "%s", argv[0]);
+       } else if (stat(argv[0], &stb1) != 0) {
+               if (!Nflag || errno != ENOENT)
+                       err(2, "%s", argv[0]);
+               dflags |= D_EMPTY1;
+               memset(&stb1, 0, sizeof(struct stat));
+       }
+
        if (strcmp(argv[1], "-") == 0) {
                fstat(STDIN_FILENO, &stb2);
                gotstdin = 1;
-       } else if (stat(argv[1], &stb2) != 0)
-               err(2, "%s", argv[1]);
+       } else if (stat(argv[1], &stb2) != 0) {
+               if (!Nflag || errno != ENOENT)
+                       err(2, "%s", argv[1]);
+               dflags |= D_EMPTY2;
+               memset(&stb2, 0, sizeof(stb2));
+               stb2.st_mode = stb1.st_mode;
+       }
+
+       if (dflags & D_EMPTY1 && dflags & D_EMPTY2){
+               warn("%s", argv[0]);    
+               warn("%s", argv[1]);
+               exit(2);        
+       }
+
+       if (stb1.st_mode == 0)
+               stb1.st_mode = stb2.st_mode;
+
        if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
                errx(2, "can't compare - to a directory");
        set_argstr(oargv, argv);

Modified: head/usr.bin/diff/tests/diff_test.sh
==============================================================================
--- head/usr.bin/diff/tests/diff_test.sh        Mon Jun  1 09:01:13 2020        
(r361688)
+++ head/usr.bin/diff/tests/diff_test.sh        Mon Jun  1 09:09:36 2020        
(r361689)
@@ -10,6 +10,7 @@ atf_test_case side_by_side
 atf_test_case brief_format
 atf_test_case b230049
 atf_test_case Bflag
+atf_test_case Nflag
 atf_test_case tabsize
 atf_test_case conflicting_format
 atf_test_case label
@@ -165,6 +166,15 @@ Bflag_body()
        atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
 }
 
+Nflag_body()
+{
+       atf_check -x 'printf "foo" > A'
+
+       atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE 
+       atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A 
+       atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2 
+}
+
 tabsize_body()
 {
        printf "\tA\n" > A
@@ -219,6 +229,7 @@ atf_init_test_cases()
        atf_add_test_case brief_format
        atf_add_test_case b230049
        atf_add_test_case Bflag
+       atf_add_test_case Nflag
        atf_add_test_case tabsize
        atf_add_test_case conflicting_format
        atf_add_test_case label
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to