2 * $OpenBSD: patch.c,v 1.45 2007/04/18 21:52:24 sobrado Exp $
3 * $DragonFly: src/usr.bin/patch/patch.c,v 1.10 2008/08/10 23:39:56 joerg Exp $
4 * $NetBSD: patch.c,v 1.27 2008/09/19 18:33:34 joerg Exp $
8 * patch - a program to apply diffs to original files
10 * Copyright 1986, Larry Wall
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following condition is met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this condition and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
33 #include <sys/cdefs.h>
35 #include <sys/types.h>
50 #include "backupfile.h"
51 #include "pathnames.h"
53 mode_t filemode
= 0644;
55 char buf
[MAXLINELEN
]; /* general purpose buffer */
56 size_t buf_len
= sizeof(buf
);
58 bool using_plan_a
= true; /* try to keep everything in memory */
59 bool out_of_mem
= false; /* ran out of memory in plan a */
63 char *filearg
[MAXFILEC
];
64 bool ok_to_create_file
= false;
66 char *origprae
= NULL
;
71 bool toutkeep
= false;
72 bool trejkeep
= false;
73 bool warn_on_invalid_line
;
74 bool last_line_missing_eol
;
84 bool noreverse
= false;
85 bool skip_rest_of_patch
= false;
87 bool canonicalize
= false;
88 bool check_only
= false;
90 char *revision
= NULL
; /* prerequisite revision, if any */
91 LINENUM input_lines
= 0; /* how long is input file in lines */
92 int posix
= 0; /* strict POSIX mode? */
94 static void reinitialize_almost_everything(void);
95 static void get_some_switches(void);
96 static LINENUM
locate_hunk(LINENUM
);
97 static void abort_context_hunk(void);
98 static void rej_line(int, LINENUM
);
99 static void abort_hunk(void);
100 static void apply_hunk(LINENUM
);
101 static void init_output(const char *);
102 static void init_reject(const char *);
103 static void copy_till(LINENUM
, bool);
104 static bool spew_output(void);
105 static void dump_line(LINENUM
, bool);
106 static bool patch_match(LINENUM
, LINENUM
, LINENUM
);
107 static bool similar(const char *, const char *, int);
108 static void usage(void);
110 /* true if -E was specified on command line. */
111 static bool remove_empty_files
= false;
113 /* true if -R was specified on command line. */
114 static bool reverse_flag_specified
= false;
116 /* buffer holding the name of the rejected patch file. */
117 static char rejname
[NAME_MAX
+ 1];
119 /* buffer for stderr */
120 static char serrbuf
[BUFSIZ
];
122 /* how many input lines have been irretractibly output */
123 static LINENUM last_frozen_line
= 0;
125 static int Argc
; /* guess */
127 static int Argc_last
; /* for restarting plan_b */
128 static char **Argv_last
;
130 static FILE *ofp
= NULL
; /* output file pointer */
131 static FILE *rejfp
= NULL
; /* reject file pointer */
133 static int filec
= 0; /* how many file arguments? */
134 static LINENUM last_offset
= 0;
135 static LINENUM maxfuzz
= 2;
137 /* patch using ifdef, ifndef, etc. */
138 static bool do_defines
= false;
140 static char if_defined
[128];
142 static char not_defined
[128];
144 static const char else_defined
[] = "#else\n";
146 static char end_defined
[128];
149 /* Apply a set of diffs as appropriate. */
152 main(int argc
, char *argv
[])
154 int error
= 0, hunk
, failed
, i
, fd
;
155 LINENUM where
= 0, newwhere
, fuzz
, mymaxfuzz
;
160 setbuf(stderr
, serrbuf
);
161 for (i
= 0; i
< MAXFILEC
; i
++)
165 /* Cons up the names of the temporary files. */
166 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| *tmpdir
== '\0')
168 for (i
= strlen(tmpdir
) - 1; i
> 0 && tmpdir
[i
] == '/'; i
--)
173 #define TMPALLOC(var) if(!(var = malloc(alloclen))) { fatal(#var); exit(1); }
174 TMPALLOC(TMPOUTNAME
);
176 TMPALLOC(TMPREJNAME
);
177 TMPALLOC(TMPPATNAME
);
179 if (snprintf(TMPOUTNAME
, alloclen
, "%.*s/patchoXXXXXXXXXX", i
, tmpdir
) == -1)
180 fatal("cannot allocate memory");
181 if ((fd
= mkstemp(TMPOUTNAME
)) < 0)
182 pfatal("can't create %s", TMPOUTNAME
);
185 if (snprintf(TMPINNAME
, alloclen
, "%.*s/patchiXXXXXXXXXX", i
, tmpdir
) == -1)
186 fatal("cannot allocate memory");
187 if ((fd
= mkstemp(TMPINNAME
)) < 0)
188 pfatal("can't create %s", TMPINNAME
);
191 if (snprintf(TMPREJNAME
, alloclen
, "%.*s/patchrXXXXXXXXXX", i
, tmpdir
) == -1)
192 fatal("cannot allocate memory");
193 if ((fd
= mkstemp(TMPREJNAME
)) < 0)
194 pfatal("can't create %s", TMPREJNAME
);
197 if (snprintf(TMPPATNAME
, alloclen
, "%.*s/patchpXXXXXXXXXX", i
, tmpdir
) == -1)
198 fatal("cannot allocate memory");
199 if ((fd
= mkstemp(TMPPATNAME
)) < 0)
200 pfatal("can't create %s", TMPPATNAME
);
203 v
= getenv("SIMPLE_BACKUP_SUFFIX");
205 simple_backup_suffix
= v
;
207 simple_backup_suffix
= ORIGEXT
;
214 if (backup_type
== none
) {
215 if ((v
= getenv("PATCH_VERSION_CONTROL")) == NULL
)
216 v
= getenv("VERSION_CONTROL");
217 if (v
!= NULL
|| !posix
)
218 backup_type
= get_version(v
); /* OK to pass NULL. */
221 /* make sure we clean up /tmp in case of disaster */
224 for (open_patch_file(filearg
[1]); there_is_another_patch();
225 reinitialize_almost_everything()) {
226 /* for each patch in patch file */
228 warn_on_invalid_line
= true;
231 outname
= savestr(filearg
[0]);
233 /* for ed script just up and do it and exit */
234 if (diff_type
== ED_DIFF
) {
238 /* initialize the patched file */
239 if (!skip_rest_of_patch
)
240 init_output(TMPOUTNAME
);
242 /* initialize reject file */
243 init_reject(TMPREJNAME
);
245 /* find out where all the lines are */
246 if (!skip_rest_of_patch
)
247 scan_input(filearg
[0]);
249 /* from here on, open no standard i/o files, because malloc */
250 /* might misfire and we can't catch it easily */
252 /* apply each hunk of patch */
256 while (another_hunk()) {
259 mymaxfuzz
= pch_context();
260 if (maxfuzz
< mymaxfuzz
)
262 if (!skip_rest_of_patch
) {
264 where
= locate_hunk(fuzz
);
265 if (hunk
== 1 && where
== 0 && !force
) {
266 /* dwim for reversed patch? */
269 say("Not enough memory to try swapped hunk! Assuming unswapped.\n");
274 where
= locate_hunk(fuzz
);
276 /* didn't find it swapped */
278 /* put it back to normal */
279 fatal("lost hunk on alloc error!\n");
281 } else if (noreverse
) {
283 /* put it back to normal */
284 fatal("lost hunk on alloc error!\n");
286 say("Ignoring previously applied (or reversed) patch.\n");
287 skip_rest_of_patch
= true;
290 say("%seversed (or previously applied) patch detected! %s -R.",
291 reverse
? "R" : "Unr",
292 reverse
? "Assuming" : "Ignoring");
294 ask("%seversed (or previously applied) patch detected! %s -R? [y] ",
295 reverse
? "R" : "Unr",
296 reverse
? "Assume" : "Ignore");
298 ask("Apply anyway? [n] ");
300 skip_rest_of_patch
= true;
304 /* put it back to normal */
305 fatal("lost hunk on alloc error!\n");
309 } while (!skip_rest_of_patch
&& where
== 0 &&
310 ++fuzz
<= mymaxfuzz
);
312 if (skip_rest_of_patch
) { /* just got decided */
313 if (ferror(ofp
) || fclose(ofp
)) {
314 say("Error writing %s\n",
321 newwhere
= pch_newfirst() + last_offset
;
322 if (skip_rest_of_patch
) {
326 say("Hunk #%d ignored at %ld.\n",
328 } else if (where
== 0) {
332 say("Hunk #%d failed at %ld.\n",
337 say("Hunk #%d succeeded at %ld",
340 say(" with fuzz %ld", fuzz
);
342 say(" (offset %ld line%s)",
344 last_offset
== 1L ? "" : "s");
350 if (out_of_mem
&& using_plan_a
) {
353 say("\n\nRan out of memory using Plan A--trying again...\n\n");
363 fatal("Internal error: hunk should not be 0\n");
365 /* finish spewing out the new file */
366 if (!skip_rest_of_patch
&& !spew_output()) {
367 say("Can't write %s\n", TMPOUTNAME
);
371 /* and put the output where desired */
373 if (!skip_rest_of_patch
) {
375 char *realout
= outname
;
378 if (move_file(TMPOUTNAME
, outname
) < 0) {
380 realout
= TMPOUTNAME
;
381 chmod(TMPOUTNAME
, filemode
);
383 chmod(outname
, filemode
);
385 if (remove_empty_files
&&
386 stat(realout
, &statbuf
) == 0 &&
387 statbuf
.st_size
== 0) {
389 say("Removing %s (empty after patching).\n",
395 if (ferror(rejfp
) || fclose(rejfp
)) {
396 say("Error writing %s\n", rejname
);
402 if (*rejname
== '\0') {
403 if (strlcpy(rejname
, outname
,
404 sizeof(rejname
)) >= sizeof(rejname
))
405 fatal("filename %s is too long\n", outname
);
406 if (strlcat(rejname
, REJEXT
,
407 sizeof(rejname
)) >= sizeof(rejname
))
408 fatal("filename %s is too long\n", outname
);
410 if (skip_rest_of_patch
) {
411 say("%d out of %d hunks ignored--saving rejects to %s\n",
412 failed
, hunk
, rejname
);
414 say("%d out of %d hunks failed--saving rejects to %s\n",
415 failed
, hunk
, rejname
);
417 if (!check_only
&& move_file(TMPREJNAME
, rejname
) < 0)
426 /* Prepare to find the next patch to do in the patch file. */
429 reinitialize_almost_everything(void)
435 last_frozen_line
= 0;
452 reverse
= reverse_flag_specified
;
453 skip_rest_of_patch
= false;
458 /* Process switches and filenames. */
461 get_some_switches(void)
463 const char *options
= "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
464 static struct option longopts
[] = {
465 {"backup", no_argument
, 0, 'b'},
466 {"batch", no_argument
, 0, 't'},
467 {"check", no_argument
, 0, 'C'},
468 {"context", no_argument
, 0, 'c'},
469 {"debug", required_argument
, 0, 'x'},
470 {"directory", required_argument
, 0, 'd'},
471 {"ed", no_argument
, 0, 'e'},
472 {"force", no_argument
, 0, 'f'},
473 {"forward", no_argument
, 0, 'N'},
474 {"fuzz", required_argument
, 0, 'F'},
475 {"ifdef", required_argument
, 0, 'D'},
476 {"input", required_argument
, 0, 'i'},
477 {"ignore-whitespace", no_argument
, 0, 'l'},
478 {"normal", no_argument
, 0, 'n'},
479 {"output", required_argument
, 0, 'o'},
480 {"prefix", required_argument
, 0, 'B'},
481 {"quiet", no_argument
, 0, 's'},
482 {"reject-file", required_argument
, 0, 'r'},
483 {"remove-empty-files", no_argument
, 0, 'E'},
484 {"reverse", no_argument
, 0, 'R'},
485 {"silent", no_argument
, 0, 's'},
486 {"strip", required_argument
, 0, 'p'},
487 {"suffix", required_argument
, 0, 'z'},
488 {"unified", no_argument
, 0, 'u'},
489 {"version", no_argument
, 0, 'v'},
490 {"version-control", required_argument
, 0, 'V'},
491 {"posix", no_argument
, &posix
, 1},
501 optreset
= optind
= 1;
502 while ((ch
= getopt_long(Argc
, Argv
, options
, longopts
, NULL
)) != -1) {
505 if (backup_type
== none
)
506 backup_type
= numbered_existing
;
510 say("Warning, the ``-b suffix'' option has been"
511 " obsoleted by the -z option.\n");
514 /* must directly follow 'b' case for backwards compat */
515 simple_backup_suffix
= savestr(optarg
);
518 origprae
= savestr(optarg
);
521 diff_type
= CONTEXT_DIFF
;
527 if (chdir(optarg
) < 0)
528 pfatal("can't cd to %s", optarg
);
532 if (!isalpha((unsigned char)*optarg
) && *optarg
!= '_')
533 fatal("argument to -D is not an identifier\n");
534 snprintf(if_defined
, sizeof if_defined
,
535 "#ifdef %s\n", optarg
);
536 snprintf(not_defined
, sizeof not_defined
,
537 "#ifndef %s\n", optarg
);
538 snprintf(end_defined
, sizeof end_defined
,
539 "#endif /* %s */\n", optarg
);
545 remove_empty_files
= true;
551 maxfuzz
= atoi(optarg
);
554 if (++filec
== MAXFILEC
)
555 fatal("too many file arguments\n");
556 filearg
[filec
] = savestr(optarg
);
562 diff_type
= NORMAL_DIFF
;
568 outname
= savestr(optarg
);
571 strippath
= atoi(optarg
);
574 if (strlcpy(rejname
, optarg
,
575 sizeof(rejname
)) >= sizeof(rejname
))
576 fatal("argument for -r is too long\n");
580 reverse_flag_specified
= true;
589 diff_type
= UNI_DIFF
;
595 backup_type
= get_version(optarg
);
599 debug
= atoi(optarg
);
612 filearg
[0] = savestr(*Argv
++);
615 if (++filec
== MAXFILEC
)
616 fatal("too many file arguments\n");
617 filearg
[filec
] = savestr(*Argv
++);
622 if (getenv("POSIXLY_CORRECT") != NULL
)
630 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
631 " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
632 " [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n"
633 " [--posix] [origfile [patchfile]]\n"
634 " patch <patchfile\n");
635 my_exit(EXIT_SUCCESS
);
639 * Attempt to find the right place to apply this hunk of patch.
642 locate_hunk(LINENUM fuzz
)
644 LINENUM first_guess
= pch_first() + last_offset
;
646 LINENUM pat_lines
= pch_ptrn_lines();
647 LINENUM max_pos_offset
= input_lines
- first_guess
- pat_lines
+ 1;
648 LINENUM max_neg_offset
= first_guess
- last_frozen_line
- 1 + pch_context();
650 if (pat_lines
== 0) { /* null range matches always */
651 if (verbose
&& fuzz
== 0 && (diff_type
== CONTEXT_DIFF
652 || diff_type
== NEW_CONTEXT_DIFF
653 || diff_type
== UNI_DIFF
)) {
654 say("Empty context always matches.\n");
656 return (first_guess
);
658 if (max_neg_offset
>= first_guess
) /* do not try lines < 0 */
659 max_neg_offset
= first_guess
- 1;
660 if (first_guess
<= input_lines
&& patch_match(first_guess
, 0, fuzz
))
662 for (offset
= 1; ; offset
++) {
663 bool check_after
= (offset
<= max_pos_offset
);
664 bool check_before
= (offset
<= max_neg_offset
);
666 if (check_after
&& patch_match(first_guess
, offset
, fuzz
)) {
669 say("Offset changing from %ld to %ld\n",
670 last_offset
, offset
);
672 last_offset
= offset
;
673 return first_guess
+ offset
;
674 } else if (check_before
&& patch_match(first_guess
, -offset
, fuzz
)) {
677 say("Offset changing from %ld to %ld\n",
678 last_offset
, -offset
);
680 last_offset
= -offset
;
681 return first_guess
- offset
;
682 } else if (!check_before
&& !check_after
)
687 /* We did not find the pattern, dump out the hunk so they can handle it. */
690 abort_context_hunk(void)
693 const LINENUM pat_end
= pch_end();
695 * add in last_offset to guess the same as the previous successful
698 const LINENUM oldfirst
= pch_first() + last_offset
;
699 const LINENUM newfirst
= pch_newfirst() + last_offset
;
700 const LINENUM oldlast
= oldfirst
+ pch_ptrn_lines() - 1;
701 const LINENUM newlast
= newfirst
+ pch_repl_lines() - 1;
702 const char *stars
= (diff_type
>= NEW_CONTEXT_DIFF
? " ****" : "");
703 const char *minuses
= (diff_type
>= NEW_CONTEXT_DIFF
? " ----" : " -----");
705 fprintf(rejfp
, "***************\n");
706 for (i
= 0; i
<= pat_end
; i
++) {
707 switch (pch_char(i
)) {
709 if (oldlast
< oldfirst
)
710 fprintf(rejfp
, "*** 0%s\n", stars
);
711 else if (oldlast
== oldfirst
)
712 fprintf(rejfp
, "*** %ld%s\n", oldfirst
, stars
);
714 fprintf(rejfp
, "*** %ld,%ld%s\n", oldfirst
,
718 if (newlast
< newfirst
)
719 fprintf(rejfp
, "--- 0%s\n", minuses
);
720 else if (newlast
== newfirst
)
721 fprintf(rejfp
, "--- %ld%s\n", newfirst
, minuses
);
723 fprintf(rejfp
, "--- %ld,%ld%s\n", newfirst
,
727 fprintf(rejfp
, "%s", pfetch(i
));
733 fprintf(rejfp
, "%c %s", pch_char(i
), pfetch(i
));
736 fatal("fatal internal error in abort_context_hunk\n");
742 rej_line(int ch
, LINENUM i
)
745 const char *line
= pfetch(i
);
749 fprintf(rejfp
, "%c%s", ch
, line
);
750 if (len
== 0 || line
[len
-1] != '\n')
751 fprintf(rejfp
, "\n\\ No newline at end of file\n");
759 const LINENUM pat_end
= pch_end();
760 const LINENUM oldfirst
= pch_first() + last_offset
;
761 const LINENUM newfirst
= pch_newfirst() + last_offset
;
763 if (diff_type
!= UNI_DIFF
) {
764 abort_context_hunk();
768 for (i
= 0; i
<= pat_end
; i
++) {
769 if (pch_char(i
) == '=') {
775 fprintf(rejfp
, "malformed hunk: no split found\n");
780 fprintf(rejfp
, "@@ -%ld,%ld +%ld,%ld @@\n",
781 pch_ptrn_lines() ? oldfirst
: 0,
782 pch_ptrn_lines(), newfirst
, pch_repl_lines());
783 while (i
< split
|| j
<= pat_end
) {
784 ch1
= i
< split
? pch_char(i
) : -1;
785 ch2
= j
<= pat_end
? pch_char(j
) : -1;
789 } else if (ch1
== ' ' && ch2
== ' ') {
793 } else if (ch1
== '!' && ch2
== '!') {
794 while (i
< split
&& ch1
== '!') {
797 ch1
= i
< split
? pch_char(i
) : -1;
799 while (j
<= pat_end
&& ch2
== '!') {
802 ch2
= j
<= pat_end
? pch_char(j
) : -1;
804 } else if (ch1
== '*') {
806 } else if (ch2
== '+' || ch2
== ' ') {
810 fprintf(rejfp
, "internal error on (%ld %ld %ld)\n",
819 /* We found where to apply it (we hope), so do it. */
822 apply_hunk(LINENUM where
)
825 const LINENUM lastline
= pch_ptrn_lines();
826 LINENUM
new = lastline
+ 1;
831 int def_state
= OUTSIDE
;
832 const LINENUM pat_end
= pch_end();
835 while (pch_char(new) == '=' || pch_char(new) == '\n')
838 while (old
<= lastline
) {
839 if (pch_char(old
) == '-') {
840 copy_till(where
+ old
- 1, false);
842 if (def_state
== OUTSIDE
) {
843 fputs(not_defined
, ofp
);
844 def_state
= IN_IFNDEF
;
845 } else if (def_state
== IN_IFDEF
) {
846 fputs(else_defined
, ofp
);
849 fputs(pfetch(old
), ofp
);
853 } else if (new > pat_end
) {
855 } else if (pch_char(new) == '+') {
856 copy_till(where
+ old
- 1, false);
858 if (def_state
== IN_IFNDEF
) {
859 fputs(else_defined
, ofp
);
861 } else if (def_state
== OUTSIDE
) {
862 fputs(if_defined
, ofp
);
863 def_state
= IN_IFDEF
;
866 fputs(pfetch(new), ofp
);
868 } else if (pch_char(new) != pch_char(old
)) {
869 say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
870 pch_hunk_beg() + old
,
871 pch_hunk_beg() + new);
873 say("oldchar = '%c', newchar = '%c'\n",
874 pch_char(old
), pch_char(new));
877 } else if (pch_char(new) == '!') {
878 copy_till(where
+ old
- 1, false);
880 fputs(not_defined
, ofp
);
881 def_state
= IN_IFNDEF
;
883 while (pch_char(old
) == '!') {
885 fputs(pfetch(old
), ofp
);
891 fputs(else_defined
, ofp
);
894 while (pch_char(new) == '!') {
895 fputs(pfetch(new), ofp
);
899 if (pch_char(new) != ' ')
900 fatal("Internal error: expected ' '\n");
903 if (do_defines
&& def_state
!= OUTSIDE
) {
904 fputs(end_defined
, ofp
);
909 if (new <= pat_end
&& pch_char(new) == '+') {
910 copy_till(where
+ old
- 1, false);
912 if (def_state
== OUTSIDE
) {
913 fputs(if_defined
, ofp
);
914 def_state
= IN_IFDEF
;
915 } else if (def_state
== IN_IFNDEF
) {
916 fputs(else_defined
, ofp
);
920 while (new <= pat_end
&& pch_char(new) == '+') {
921 fputs(pfetch(new), ofp
);
925 if (do_defines
&& def_state
!= OUTSIDE
) {
926 fputs(end_defined
, ofp
);
934 init_output(const char *name
)
936 ofp
= fopen(name
, "w");
938 pfatal("can't create %s", name
);
942 * Open a file to put hunks we can't locate.
945 init_reject(const char *name
)
947 rejfp
= fopen(name
, "w");
949 pfatal("can't create %s", name
);
953 * Copy input file to output, up to wherever hunk is to be applied.
954 * If endoffile is true, treat the last line specially since it may
958 copy_till(LINENUM lastline
, bool endoffile
)
960 if (last_frozen_line
> lastline
)
961 fatal("misordered hunks! output would be garbled\n");
962 while (last_frozen_line
< lastline
) {
963 if (++last_frozen_line
== lastline
&& endoffile
)
964 dump_line(last_frozen_line
, !last_line_missing_eol
);
966 dump_line(last_frozen_line
, true);
971 * Finish copying the input file to the output file.
980 say("il=%ld lfl=%ld\n", input_lines
, last_frozen_line
);
983 copy_till(input_lines
, true); /* dump remainder of file */
984 rv
= ferror(ofp
) == 0 && fclose(ofp
) == 0;
990 * Copy one line from input to output.
993 dump_line(LINENUM line
, bool write_newline
)
1000 /* Note: string is not NUL terminated. */
1001 for (; *s
!= '\n'; s
++)
1008 * Does the patch pattern match at line base+offset?
1011 patch_match(LINENUM base
, LINENUM offset
, LINENUM fuzz
)
1013 LINENUM pline
= 1 + fuzz
;
1015 LINENUM pat_lines
= pch_ptrn_lines() - fuzz
;
1016 const char *ilineptr
;
1017 const char *plineptr
;
1020 for (iline
= base
+ offset
+ fuzz
; pline
<= pat_lines
; pline
++, iline
++) {
1021 ilineptr
= ifetch(iline
, offset
>= 0);
1022 if (ilineptr
== NULL
)
1024 plineptr
= pfetch(pline
);
1025 plinelen
= pch_line_len(pline
);
1027 if (!similar(ilineptr
, plineptr
, plinelen
))
1029 } else if (strnNE(ilineptr
, plineptr
, plinelen
))
1031 if (iline
== input_lines
) {
1033 * We are looking at the last line of the file.
1034 * If the file has no eol, the patch line should
1035 * not have one either and vice-versa. Note that
1038 if (last_line_missing_eol
) {
1039 if (plineptr
[plinelen
- 1] == '\n')
1042 if (plineptr
[plinelen
- 1] != '\n')
1051 * Do two lines match with canonicalized white space?
1054 similar(const char *a
, const char *b
, int len
)
1057 if (isspace((unsigned char)*b
)) { /* whitespace (or \n) to match? */
1058 if (!isspace((unsigned char)*a
)) /* no corresponding whitespace? */
1060 while (len
&& isspace((unsigned char)*b
) && *b
!= '\n')
1061 b
++, len
--; /* skip pattern whitespace */
1062 while (isspace((unsigned char)*a
) && *a
!= '\n')
1063 a
++; /* skip target whitespace */
1064 if (*a
== '\n' || *b
== '\n')
1065 return (*a
== *b
); /* should end in sync */
1066 } else if (*a
++ != *b
++) /* match non-whitespace chars */
1069 len
--; /* probably not necessary */
1071 return true; /* actually, this is not reached */
1072 /* since there is always a \n */