@@ -90,7 +90,8 @@ struct lines_info {
90
90
};
91
91
92
92
static int human_readable = 1 ;
93
- static char diff_opts [4 ];
93
+ static char * diff_opts [100 ];
94
+ static int num_diff_opts = 0 ;
94
95
static unsigned int max_context_real = 3 , max_context = 3 ;
95
96
static int context_specified = 0 ;
96
97
static int ignore_components = 0 ;
@@ -708,7 +709,7 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
708
709
int diff_is_empty = 1 ;
709
710
unsigned int use_context = max_context ;
710
711
711
- if (diff_opts [ 0 ] == '\0' && !context_specified )
712
+ if (! num_diff_opts && !context_specified )
712
713
return do_output_patch1_only (p1 , out , not_reverted );
713
714
714
715
/* We want to redo the diff using the supplied options. */
@@ -757,9 +758,9 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
757
758
use_context = file_orig .min_context ;
758
759
759
760
if (use_context == 3 )
760
- sprintf (options , "-%su" , diff_opts );
761
+ strcpy (options , "-u" );
761
762
else
762
- sprintf (options , "-%sU%d" , diff_opts , use_context );
763
+ sprintf (options , "-U%d" , use_context );
763
764
764
765
/* Write it out. */
765
766
write_file (& file_orig , tmpp1fd );
@@ -770,7 +771,11 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
770
771
clear_lines_info (& file_new );
771
772
772
773
fflush (NULL );
773
- in = xpipe (DIFF , & child , "r" , (char * * ) (const char * []) { DIFF , options , tmpp1 , tmpp2 , NULL });
774
+ char * argv [2 + num_diff_opts + 2 + 1 ];
775
+ memcpy (argv , (const char * []) { DIFF , options }, 2 * sizeof (char * ));
776
+ memcpy (argv + 2 , diff_opts , num_diff_opts * sizeof (char * ));
777
+ memcpy (argv + 2 + num_diff_opts , (char * []) { tmpp1 , tmpp2 , NULL }, (2 + 1 ) * sizeof (char * ));
778
+ in = xpipe (DIFF , & child , "r" , argv );
774
779
775
780
/* Eat the first line */
776
781
for (;;) {
@@ -1043,9 +1048,9 @@ output_delta (FILE *p1, FILE *p2, FILE *out)
1043
1048
memcpy (tmpp2 + tmplen , tail2 , sizeof (tail2 ));
1044
1049
1045
1050
if (max_context == 3 )
1046
- sprintf (options , "-%su" , diff_opts );
1051
+ strcpy (options , "-u" );
1047
1052
else
1048
- sprintf (options , "-%sU%d" , diff_opts , max_context );
1053
+ sprintf (options , "-U%d" , max_context );
1049
1054
1050
1055
tmpp1fd = xmkstemp (tmpp1 );
1051
1056
tmpp2fd = xmkstemp (tmpp2 );
@@ -1102,7 +1107,11 @@ output_delta (FILE *p1, FILE *p2, FILE *out)
1102
1107
1103
1108
fflush (NULL );
1104
1109
1105
- in = xpipe (DIFF , & child , "r" , (char * * ) (const char * []) { DIFF , options , tmpp1 , tmpp2 , NULL });
1110
+ char * argv [2 + num_diff_opts + 2 + 1 ];
1111
+ memcpy (argv , (const char * []) { DIFF , options }, 2 * sizeof (char * ));
1112
+ memcpy (argv + 2 , diff_opts , num_diff_opts * sizeof (char * ));
1113
+ memcpy (argv + 2 + num_diff_opts , (char * []) { tmpp1 , tmpp2 , NULL }, (2 + 1 ) * sizeof (char * ));
1114
+ in = xpipe (DIFF , & child , "r" , argv );
1106
1115
1107
1116
/* Eat the first line */
1108
1117
for (;;) {
@@ -1500,14 +1509,23 @@ take_diff (const char *f1, const char *f2, char *headers[2],
1500
1509
FILE * in ;
1501
1510
1502
1511
if (max_context == 3 )
1503
- sprintf (options , "-%su" , diff_opts );
1512
+ strcpy (options , "-u" );
1504
1513
else
1505
- sprintf (options , "-%sU%d" , diff_opts , max_context );
1506
-
1507
- if (debug )
1508
- printf ("+ " DIFF " %s %s %s\n" , options , f1 , f2 );
1514
+ sprintf (options , "-U%d" , max_context );
1515
+
1516
+ char * argv [2 + num_diff_opts + 2 + 1 ];
1517
+ memcpy (argv , (const char * []) { DIFF , options }, 2 * sizeof (char * ));
1518
+ memcpy (argv + 2 , diff_opts , num_diff_opts * sizeof (char * ));
1519
+ memcpy (argv + 2 + num_diff_opts , (const char * []) { f1 , f2 , NULL }, (2 + 1 ) * sizeof (char * ));
1520
+ if (debug ) {
1521
+ fputs ("+" , stdout );
1522
+ for (int i = 0 ; argv [i ]; i ++ ) {
1523
+ printf (" %s" , argv [i ]);
1524
+ }
1525
+ puts ("" );
1526
+ }
1509
1527
1510
- in = xpipe (DIFF , & child , "r" , ( char * * ) ( const char * []) { DIFF , options , f1 , f2 , NULL } );
1528
+ in = xpipe (DIFF , & child , "r" , argv );
1511
1529
1512
1530
/* Eat the first line */
1513
1531
for (;;) {
@@ -1996,6 +2014,9 @@ syntax (int err)
1996
2014
" ignore changes in the amount of whitespace\n"
1997
2015
" -B, --ignore-blank-lines\n"
1998
2016
" ignore changes whose lines are all blank\n"
2017
+ " --color[=WHEN]\n"
2018
+ " colorize the output; WHEN can be 'never', 'always',\n"
2019
+ " or 'auto' (the default)\n"
1999
2020
" -p N, --strip-match=N\n"
2000
2021
" pathname components to ignore\n"
2001
2022
" -q, --quiet\n"
@@ -2061,11 +2082,9 @@ int
2061
2082
main (int argc , char * argv [])
2062
2083
{
2063
2084
FILE * p1 , * p2 ;
2064
- int num_diff_opts = 0 ;
2065
2085
int ret ;
2066
2086
2067
2087
get_mode_from_name (argv [0 ]);
2068
- diff_opts [0 ] = '\0' ;
2069
2088
for (;;) {
2070
2089
static struct option long_options [] = {
2071
2090
{"help" , 0 , 0 , 1000 + 'H' },
@@ -2083,6 +2102,7 @@ main (int argc, char *argv[])
2083
2102
{"ignore-space-change" , 0 , 0 , 'b' },
2084
2103
{"ignore-case" , 0 , 0 , 'i' },
2085
2104
{"ignore-all-space" , 0 , 0 , 'w' },
2105
+ {"color" , 2 , 0 , 1000 + 'c' },
2086
2106
{"decompress" , 0 , 0 , 'z' },
2087
2107
{"quiet" , 0 , 0 , 'q' },
2088
2108
{0 , 0 , 0 , 0 }
@@ -2127,9 +2147,13 @@ main (int argc, char *argv[])
2127
2147
case 'b' :
2128
2148
case 'i' :
2129
2149
case 'w' :
2130
- if (!memchr (diff_opts , c , num_diff_opts )) {
2131
- diff_opts [num_diff_opts ++ ] = c ;
2132
- diff_opts [num_diff_opts ] = '\0' ;
2150
+ asprintf (diff_opts + num_diff_opts ++ , "-%c" , c );
2151
+ break ;
2152
+ case 1000 + 'c' :
2153
+ if (optarg ) {
2154
+ asprintf (diff_opts + num_diff_opts ++ , "--color=%s" , optarg );
2155
+ } else {
2156
+ diff_opts [num_diff_opts ++ ] = "--color" ;
2133
2157
}
2134
2158
break ;
2135
2159
case 1000 + 'I' :
0 commit comments