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.29 2011/09/06 18:25:14 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>
34 __RCSID("$NetBSD: patch.c,v 1.29 2011/09/06 18:25:14 joerg Exp $");
36 #include <sys/types.h>
51 #include "backupfile.h"
52 #include "pathnames.h"
54 mode_t filemode
= 0644;
56 char buf
[MAXLINELEN
]; /* general purpose buffer */
57 size_t buf_len
= sizeof(buf
);
59 bool using_plan_a
= true; /* try to keep everything in memory */
60 bool out_of_mem
= false; /* ran out of memory in plan a */
64 char *filearg
[MAXFILEC
];
65 bool ok_to_create_file
= false;
67 char *origprae
= NULL
;
72 bool toutkeep
= false;
73 bool trejkeep
= false;
74 bool warn_on_invalid_line
;
75 bool last_line_missing_eol
;
85 bool noreverse
= false;
86 bool skip_rest_of_patch
= false;
88 bool canonicalize
= false;
89 bool check_only
= false;
91 char *revision
= NULL
; /* prerequisite revision, if any */
92 LINENUM input_lines
= 0; /* how long is input file in lines */
93 int posix
= 0; /* strict POSIX mode? */
95 static void reinitialize_almost_everything(void);
96 static void get_some_switches(void);
97 static LINENUM
locate_hunk(LINENUM
);
98 static void abort_context_hunk(void);
99 static void rej_line(int, LINENUM
);
100 static void abort_hunk(void);
101 static void apply_hunk(LINENUM
);
102 static void init_output(const char *);
103 static void init_reject(const char *);
104 static void copy_till(LINENUM
, bool);
105 static bool spew_output(void);
106 static void dump_line(LINENUM
, bool);
107 static bool patch_match(LINENUM
, LINENUM
, LINENUM
);
108 static bool similar(const char *, const char *, int);
109 __dead
static void usage(void);
111 /* true if -E was specified on command line. */
112 static bool remove_empty_files
= false;
114 /* true if -R was specified on command line. */
115 static bool reverse_flag_specified
= false;
117 /* buffer holding the name of the rejected patch file. */
118 static char rejname
[NAME_MAX
+ 1];
120 /* buffer for stderr */
121 static char serrbuf
[BUFSIZ
];
123 /* how many input lines have been irretractibly output */
124 static LINENUM last_frozen_line
= 0;
126 static int Argc
; /* guess */
128 static int Argc_last
; /* for restarting plan_b */
129 static char **Argv_last
;
131 static FILE *ofp
= NULL
; /* output file pointer */
132 static FILE *rejfp
= NULL
; /* reject file pointer */
134 static int filec
= 0; /* how many file arguments? */
135 static LINENUM last_offset
= 0;
136 static LINENUM maxfuzz
= 2;
138 /* patch using ifdef, ifndef, etc. */
139 static bool do_defines
= false;
141 static char if_defined
[128];
143 static char not_defined
[128];
145 static const char else_defined
[] = "#else\n";
147 static char end_defined
[128];
150 /* Apply a set of diffs as appropriate. */
153 main(int argc
, char *argv
[])
155 int error
= 0, hunk
, failed
, i
, fd
;
156 LINENUM where
= 0, newwhere
, fuzz
, mymaxfuzz
;
160 setbuf(stderr
, serrbuf
);
161 for (i
= 0; i
< MAXFILEC
; i
++)
164 /* Cons up the names of the temporary files. */
165 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| *tmpdir
== '\0')
167 for (i
= strlen(tmpdir
) - 1; i
> 0 && tmpdir
[i
] == '/'; i
--)
170 if (asprintf(&TMPOUTNAME
, "%.*s/patchoXXXXXXXXXX", i
, tmpdir
) == -1)
171 fatal("cannot allocate memory");
172 if ((fd
= mkstemp(TMPOUTNAME
)) < 0)
173 pfatal("can't create %s", TMPOUTNAME
);
176 if (asprintf(&TMPINNAME
, "%.*s/patchiXXXXXXXXXX", i
, tmpdir
) == -1)
177 fatal("cannot allocate memory");
178 if ((fd
= mkstemp(TMPINNAME
)) < 0)
179 pfatal("can't create %s", TMPINNAME
);
182 if (asprintf(&TMPREJNAME
, "%.*s/patchrXXXXXXXXXX", i
, tmpdir
) == -1)
183 fatal("cannot allocate memory");
184 if ((fd
= mkstemp(TMPREJNAME
)) < 0)
185 pfatal("can't create %s", TMPREJNAME
);
188 if (asprintf(&TMPPATNAME
, "%.*s/patchpXXXXXXXXXX", i
, tmpdir
) == -1)
189 fatal("cannot allocate memory");
190 if ((fd
= mkstemp(TMPPATNAME
)) < 0)
191 pfatal("can't create %s", TMPPATNAME
);
194 v
= getenv("SIMPLE_BACKUP_SUFFIX");
196 simple_backup_suffix
= v
;
198 simple_backup_suffix
= ORIGEXT
;
205 if (backup_type
== none
) {
206 if ((v
= getenv("PATCH_VERSION_CONTROL")) == NULL
)
207 v
= getenv("VERSION_CONTROL");
208 if (v
!= NULL
|| !posix
)
209 backup_type
= get_version(v
); /* OK to pass NULL. */
212 /* make sure we clean up /tmp in case of disaster */
215 for (open_patch_file(filearg
[1]); there_is_another_patch();
216 reinitialize_almost_everything()) {
217 /* for each patch in patch file */
219 warn_on_invalid_line
= true;
222 outname
= savestr(filearg
[0]);
224 /* for ed script just up and do it and exit */
225 if (diff_type
== ED_DIFF
) {
229 /* initialize the patched file */
230 if (!skip_rest_of_patch
)
231 init_output(TMPOUTNAME
);
233 /* initialize reject file */
234 init_reject(TMPREJNAME
);
236 /* find out where all the lines are */
237 if (!skip_rest_of_patch
)
238 scan_input(filearg
[0]);
240 /* from here on, open no standard i/o files, because malloc */
241 /* might misfire and we can't catch it easily */
243 /* apply each hunk of patch */
247 while (another_hunk()) {
250 mymaxfuzz
= pch_context();
251 if (maxfuzz
< mymaxfuzz
)
253 if (!skip_rest_of_patch
) {
255 where
= locate_hunk(fuzz
);
256 if (hunk
== 1 && where
== 0 && !force
) {
257 /* dwim for reversed patch? */
260 say("Not enough memory to try swapped hunk! Assuming unswapped.\n");
265 where
= locate_hunk(fuzz
);
267 /* didn't find it swapped */
269 /* put it back to normal */
270 fatal("lost hunk on alloc error!\n");
272 } else if (noreverse
) {
274 /* put it back to normal */
275 fatal("lost hunk on alloc error!\n");
277 say("Ignoring previously applied (or reversed) patch.\n");
278 skip_rest_of_patch
= true;
281 say("%seversed (or previously applied) patch detected! %s -R.",
282 reverse
? "R" : "Unr",
283 reverse
? "Assuming" : "Ignoring");
285 ask("%seversed (or previously applied) patch detected! %s -R? [y] ",
286 reverse
? "R" : "Unr",
287 reverse
? "Assume" : "Ignore");
289 ask("Apply anyway? [n] ");
291 skip_rest_of_patch
= true;
295 /* put it back to normal */
296 fatal("lost hunk on alloc error!\n");
300 } while (!skip_rest_of_patch
&& where
== 0 &&
301 ++fuzz
<= mymaxfuzz
);
303 if (skip_rest_of_patch
) { /* just got decided */
304 if (ferror(ofp
) || fclose(ofp
)) {
305 say("Error writing %s\n",
312 newwhere
= pch_newfirst() + last_offset
;
313 if (skip_rest_of_patch
) {
317 say("Hunk #%d ignored at %ld.\n",
319 } else if (where
== 0) {
323 say("Hunk #%d failed at %ld.\n",
328 say("Hunk #%d succeeded at %ld",
331 say(" with fuzz %ld", fuzz
);
333 say(" (offset %ld line%s)",
335 last_offset
== 1L ? "" : "s");
341 if (out_of_mem
&& using_plan_a
) {
344 say("\n\nRan out of memory using Plan A--trying again...\n\n");
354 fatal("Internal error: hunk should not be 0\n");
356 /* finish spewing out the new file */
357 if (!skip_rest_of_patch
&& !spew_output()) {
358 say("Can't write %s\n", TMPOUTNAME
);
362 /* and put the output where desired */
364 if (!skip_rest_of_patch
) {
366 char *realout
= outname
;
369 if (move_file(TMPOUTNAME
, outname
) < 0) {
371 realout
= TMPOUTNAME
;
372 chmod(TMPOUTNAME
, filemode
);
374 chmod(outname
, filemode
);
376 if (remove_empty_files
&&
377 stat(realout
, &statbuf
) == 0 &&
378 statbuf
.st_size
== 0) {
380 say("Removing %s (empty after patching).\n",
386 if (ferror(rejfp
) || fclose(rejfp
)) {
387 say("Error writing %s\n", rejname
);
393 if (*rejname
== '\0') {
394 if (strlcpy(rejname
, outname
,
395 sizeof(rejname
)) >= sizeof(rejname
))
396 fatal("filename %s is too long\n", outname
);
397 if (strlcat(rejname
, REJEXT
,
398 sizeof(rejname
)) >= sizeof(rejname
))
399 fatal("filename %s is too long\n", outname
);
401 if (skip_rest_of_patch
) {
402 say("%d out of %d hunks ignored--saving rejects to %s\n",
403 failed
, hunk
, rejname
);
405 say("%d out of %d hunks failed--saving rejects to %s\n",
406 failed
, hunk
, rejname
);
408 if (!check_only
&& move_file(TMPREJNAME
, rejname
) < 0)
417 /* Prepare to find the next patch to do in the patch file. */
420 reinitialize_almost_everything(void)
426 last_frozen_line
= 0;
443 reverse
= reverse_flag_specified
;
444 skip_rest_of_patch
= false;
449 /* Process switches and filenames. */
452 get_some_switches(void)
454 const char *options
= "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
455 static struct option longopts
[] = {
456 {"backup", no_argument
, 0, 'b'},
457 {"batch", no_argument
, 0, 't'},
458 {"check", no_argument
, 0, 'C'},
459 {"context", no_argument
, 0, 'c'},
460 {"debug", required_argument
, 0, 'x'},
461 {"directory", required_argument
, 0, 'd'},
462 {"ed", no_argument
, 0, 'e'},
463 {"force", no_argument
, 0, 'f'},
464 {"forward", no_argument
, 0, 'N'},
465 {"fuzz", required_argument
, 0, 'F'},
466 {"ifdef", required_argument
, 0, 'D'},
467 {"input", required_argument
, 0, 'i'},
468 {"ignore-whitespace", no_argument
, 0, 'l'},
469 {"normal", no_argument
, 0, 'n'},
470 {"output", required_argument
, 0, 'o'},
471 {"prefix", required_argument
, 0, 'B'},
472 {"quiet", no_argument
, 0, 's'},
473 {"reject-file", required_argument
, 0, 'r'},
474 {"remove-empty-files", no_argument
, 0, 'E'},
475 {"reverse", no_argument
, 0, 'R'},
476 {"silent", no_argument
, 0, 's'},
477 {"strip", required_argument
, 0, 'p'},
478 {"suffix", required_argument
, 0, 'z'},
479 {"unified", no_argument
, 0, 'u'},
480 {"version", no_argument
, 0, 'v'},
481 {"version-control", required_argument
, 0, 'V'},
482 {"posix", no_argument
, &posix
, 1},
492 optreset
= optind
= 1;
493 while ((ch
= getopt_long(Argc
, Argv
, options
, longopts
, NULL
)) != -1) {
496 if (backup_type
== none
)
497 backup_type
= numbered_existing
;
501 say("Warning, the ``-b suffix'' option has been"
502 " obsoleted by the -z option.\n");
505 /* must directly follow 'b' case for backwards compat */
506 simple_backup_suffix
= savestr(optarg
);
509 origprae
= savestr(optarg
);
512 diff_type
= CONTEXT_DIFF
;
518 if (chdir(optarg
) < 0)
519 pfatal("can't cd to %s", optarg
);
523 if (!isalpha((unsigned char)*optarg
) && *optarg
!= '_')
524 fatal("argument to -D is not an identifier\n");
525 snprintf(if_defined
, sizeof if_defined
,
526 "#ifdef %s\n", optarg
);
527 snprintf(not_defined
, sizeof not_defined
,
528 "#ifndef %s\n", optarg
);
529 snprintf(end_defined
, sizeof end_defined
,
530 "#endif /* %s */\n", optarg
);
536 remove_empty_files
= true;
542 maxfuzz
= atoi(optarg
);
545 if (++filec
== MAXFILEC
)
546 fatal("too many file arguments\n");
547 filearg
[filec
] = savestr(optarg
);
553 diff_type
= NORMAL_DIFF
;
559 outname
= savestr(optarg
);
562 strippath
= atoi(optarg
);
565 if (strlcpy(rejname
, optarg
,
566 sizeof(rejname
)) >= sizeof(rejname
))
567 fatal("argument for -r is too long\n");
571 reverse_flag_specified
= true;
580 diff_type
= UNI_DIFF
;
586 backup_type
= get_version(optarg
);
590 debug
= atoi(optarg
);
603 filearg
[0] = savestr(*Argv
++);
606 if (++filec
== MAXFILEC
)
607 fatal("too many file arguments\n");
608 filearg
[filec
] = savestr(*Argv
++);
613 if (getenv("POSIXLY_CORRECT") != NULL
)
621 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
622 " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
623 " [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n"
624 " [--posix] [origfile [patchfile]]\n"
625 " patch <patchfile\n");
626 my_exit(EXIT_FAILURE
);
630 * Attempt to find the right place to apply this hunk of patch.
633 locate_hunk(LINENUM fuzz
)
635 LINENUM first_guess
= pch_first() + last_offset
;
637 LINENUM pat_lines
= pch_ptrn_lines();
638 LINENUM max_pos_offset
= input_lines
- first_guess
- pat_lines
+ 1;
639 LINENUM max_neg_offset
= first_guess
- last_frozen_line
- 1 + pch_context();
641 if (pat_lines
== 0) { /* null range matches always */
642 if (verbose
&& fuzz
== 0 && (diff_type
== CONTEXT_DIFF
643 || diff_type
== NEW_CONTEXT_DIFF
644 || diff_type
== UNI_DIFF
)) {
645 say("Empty context always matches.\n");
647 return (first_guess
);
649 if (max_neg_offset
>= first_guess
) /* do not try lines < 0 */
650 max_neg_offset
= first_guess
- 1;
651 if (first_guess
<= input_lines
&& patch_match(first_guess
, 0, fuzz
))
653 for (offset
= 1; ; offset
++) {
654 bool check_after
= (offset
<= max_pos_offset
);
655 bool check_before
= (offset
<= max_neg_offset
);
657 if (check_after
&& patch_match(first_guess
, offset
, fuzz
)) {
660 say("Offset changing from %ld to %ld\n",
661 last_offset
, offset
);
663 last_offset
= offset
;
664 return first_guess
+ offset
;
665 } else if (check_before
&& patch_match(first_guess
, -offset
, fuzz
)) {
668 say("Offset changing from %ld to %ld\n",
669 last_offset
, -offset
);
671 last_offset
= -offset
;
672 return first_guess
- offset
;
673 } else if (!check_before
&& !check_after
)
678 /* We did not find the pattern, dump out the hunk so they can handle it. */
681 abort_context_hunk(void)
684 const LINENUM pat_end
= pch_end();
686 * add in last_offset to guess the same as the previous successful
689 const LINENUM oldfirst
= pch_first() + last_offset
;
690 const LINENUM newfirst
= pch_newfirst() + last_offset
;
691 const LINENUM oldlast
= oldfirst
+ pch_ptrn_lines() - 1;
692 const LINENUM newlast
= newfirst
+ pch_repl_lines() - 1;
693 const char *stars
= (diff_type
>= NEW_CONTEXT_DIFF
? " ****" : "");
694 const char *minuses
= (diff_type
>= NEW_CONTEXT_DIFF
? " ----" : " -----");
696 fprintf(rejfp
, "***************\n");
697 for (i
= 0; i
<= pat_end
; i
++) {
698 switch (pch_char(i
)) {
700 if (oldlast
< oldfirst
)
701 fprintf(rejfp
, "*** 0%s\n", stars
);
702 else if (oldlast
== oldfirst
)
703 fprintf(rejfp
, "*** %ld%s\n", oldfirst
, stars
);
705 fprintf(rejfp
, "*** %ld,%ld%s\n", oldfirst
,
709 if (newlast
< newfirst
)
710 fprintf(rejfp
, "--- 0%s\n", minuses
);
711 else if (newlast
== newfirst
)
712 fprintf(rejfp
, "--- %ld%s\n", newfirst
, minuses
);
714 fprintf(rejfp
, "--- %ld,%ld%s\n", newfirst
,
718 fprintf(rejfp
, "%s", pfetch(i
));
724 fprintf(rejfp
, "%c %s", pch_char(i
), pfetch(i
));
727 fatal("fatal internal error in abort_context_hunk\n");
733 rej_line(int ch
, LINENUM i
)
736 const char *line
= pfetch(i
);
740 fprintf(rejfp
, "%c%s", ch
, line
);
741 if (len
== 0 || line
[len
-1] != '\n')
742 fprintf(rejfp
, "\n\\ No newline at end of file\n");
750 const LINENUM pat_end
= pch_end();
751 const LINENUM oldfirst
= pch_first() + last_offset
;
752 const LINENUM newfirst
= pch_newfirst() + last_offset
;
754 if (diff_type
!= UNI_DIFF
) {
755 abort_context_hunk();
759 for (i
= 0; i
<= pat_end
; i
++) {
760 if (pch_char(i
) == '=') {
766 fprintf(rejfp
, "malformed hunk: no split found\n");
771 fprintf(rejfp
, "@@ -%ld,%ld +%ld,%ld @@\n",
772 pch_ptrn_lines() ? oldfirst
: 0,
773 pch_ptrn_lines(), newfirst
, pch_repl_lines());
774 while (i
< split
|| j
<= pat_end
) {
775 ch1
= i
< split
? pch_char(i
) : -1;
776 ch2
= j
<= pat_end
? pch_char(j
) : -1;
780 } else if (ch1
== ' ' && ch2
== ' ') {
784 } else if (ch1
== '!' && ch2
== '!') {
785 while (i
< split
&& ch1
== '!') {
788 ch1
= i
< split
? pch_char(i
) : -1;
790 while (j
<= pat_end
&& ch2
== '!') {
793 ch2
= j
<= pat_end
? pch_char(j
) : -1;
795 } else if (ch1
== '*') {
797 } else if (ch2
== '+' || ch2
== ' ') {
801 fprintf(rejfp
, "internal error on (%ld %ld %ld)\n",
810 /* We found where to apply it (we hope), so do it. */
813 apply_hunk(LINENUM where
)
816 const LINENUM lastline
= pch_ptrn_lines();
817 LINENUM
new = lastline
+ 1;
822 int def_state
= OUTSIDE
;
823 const LINENUM pat_end
= pch_end();
826 while (pch_char(new) == '=' || pch_char(new) == '\n')
829 while (old
<= lastline
) {
830 if (pch_char(old
) == '-') {
831 copy_till(where
+ old
- 1, false);
833 if (def_state
== OUTSIDE
) {
834 fputs(not_defined
, ofp
);
835 def_state
= IN_IFNDEF
;
836 } else if (def_state
== IN_IFDEF
) {
837 fputs(else_defined
, ofp
);
840 fputs(pfetch(old
), ofp
);
844 } else if (new > pat_end
) {
846 } else if (pch_char(new) == '+') {
847 copy_till(where
+ old
- 1, false);
849 if (def_state
== IN_IFNDEF
) {
850 fputs(else_defined
, ofp
);
852 } else if (def_state
== OUTSIDE
) {
853 fputs(if_defined
, ofp
);
854 def_state
= IN_IFDEF
;
857 fputs(pfetch(new), ofp
);
859 } else if (pch_char(new) != pch_char(old
)) {
860 say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
861 pch_hunk_beg() + old
,
862 pch_hunk_beg() + new);
864 say("oldchar = '%c', newchar = '%c'\n",
865 pch_char(old
), pch_char(new));
868 } else if (pch_char(new) == '!') {
869 copy_till(where
+ old
- 1, false);
871 fputs(not_defined
, ofp
);
872 def_state
= IN_IFNDEF
;
874 while (pch_char(old
) == '!') {
876 fputs(pfetch(old
), ofp
);
882 fputs(else_defined
, ofp
);
885 while (pch_char(new) == '!') {
886 fputs(pfetch(new), ofp
);
890 if (pch_char(new) != ' ')
891 fatal("Internal error: expected ' '\n");
894 if (do_defines
&& def_state
!= OUTSIDE
) {
895 fputs(end_defined
, ofp
);
900 if (new <= pat_end
&& pch_char(new) == '+') {
901 copy_till(where
+ old
- 1, false);
903 if (def_state
== OUTSIDE
) {
904 fputs(if_defined
, ofp
);
905 def_state
= IN_IFDEF
;
906 } else if (def_state
== IN_IFNDEF
) {
907 fputs(else_defined
, ofp
);
911 while (new <= pat_end
&& pch_char(new) == '+') {
912 fputs(pfetch(new), ofp
);
916 if (do_defines
&& def_state
!= OUTSIDE
) {
917 fputs(end_defined
, ofp
);
925 init_output(const char *name
)
927 ofp
= fopen(name
, "w");
929 pfatal("can't create %s", name
);
933 * Open a file to put hunks we can't locate.
936 init_reject(const char *name
)
938 rejfp
= fopen(name
, "w");
940 pfatal("can't create %s", name
);
944 * Copy input file to output, up to wherever hunk is to be applied.
945 * If endoffile is true, treat the last line specially since it may
949 copy_till(LINENUM lastline
, bool endoffile
)
951 if (last_frozen_line
> lastline
)
952 fatal("misordered hunks! output would be garbled\n");
953 while (last_frozen_line
< lastline
) {
954 if (++last_frozen_line
== lastline
&& endoffile
)
955 dump_line(last_frozen_line
, !last_line_missing_eol
);
957 dump_line(last_frozen_line
, true);
962 * Finish copying the input file to the output file.
971 say("il=%ld lfl=%ld\n", input_lines
, last_frozen_line
);
974 copy_till(input_lines
, true); /* dump remainder of file */
975 rv
= ferror(ofp
) == 0 && fclose(ofp
) == 0;
981 * Copy one line from input to output.
984 dump_line(LINENUM line
, bool write_newline
)
991 /* Note: string is not NUL terminated. */
992 for (; *s
!= '\n'; s
++)
999 * Does the patch pattern match at line base+offset?
1002 patch_match(LINENUM base
, LINENUM offset
, LINENUM fuzz
)
1004 LINENUM pline
= 1 + fuzz
;
1006 LINENUM pat_lines
= pch_ptrn_lines() - fuzz
;
1007 const char *ilineptr
;
1008 const char *plineptr
;
1011 for (iline
= base
+ offset
+ fuzz
; pline
<= pat_lines
; pline
++, iline
++) {
1012 ilineptr
= ifetch(iline
, offset
>= 0);
1013 if (ilineptr
== NULL
)
1015 plineptr
= pfetch(pline
);
1016 plinelen
= pch_line_len(pline
);
1018 if (!similar(ilineptr
, plineptr
, plinelen
))
1020 } else if (strnNE(ilineptr
, plineptr
, plinelen
))
1022 if (iline
== input_lines
) {
1024 * We are looking at the last line of the file.
1025 * If the file has no eol, the patch line should
1026 * not have one either and vice-versa. Note that
1029 if (last_line_missing_eol
) {
1030 if (plineptr
[plinelen
- 1] == '\n')
1033 if (plineptr
[plinelen
- 1] != '\n')
1042 * Do two lines match with canonicalized white space?
1045 similar(const char *a
, const char *b
, int len
)
1048 if (isspace((unsigned char)*b
)) { /* whitespace (or \n) to match? */
1049 if (!isspace((unsigned char)*a
)) /* no corresponding whitespace? */
1051 while (len
&& isspace((unsigned char)*b
) && *b
!= '\n')
1052 b
++, len
--; /* skip pattern whitespace */
1053 while (isspace((unsigned char)*a
) && *a
!= '\n')
1054 a
++; /* skip target whitespace */
1055 if (*a
== '\n' || *b
== '\n')
1056 return (*a
== *b
); /* should end in sync */
1057 } else if (*a
++ != *b
++) /* match non-whitespace chars */
1060 len
--; /* probably not necessary */
1062 return true; /* actually, this is not reached */
1063 /* since there is always a \n */