2 * Copyright 1986, Larry Wall
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * patch - a program to apply diffs to original files
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
26 * $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $
31 #include <sys/types.h>
46 #include "backupfile.h"
47 #include "pathnames.h"
49 mode_t filemode = 0644;
51 char *buf; /* general purpose buffer */
52 size_t buf_size; /* size of the general purpose buffer */
54 bool using_plan_a = true; /* try to keep everything in memory */
55 bool out_of_mem = false; /* ran out of memory in plan a */
56 bool nonempty_patchf_seen = false; /* seen nonempty patch file? */
60 char *filearg[MAXFILEC];
61 bool ok_to_create_file = false;
63 char *origprae = NULL;
68 bool toutkeep = false;
69 bool trejkeep = false;
70 bool warn_on_invalid_line;
71 bool last_line_missing_eol;
81 bool noreverse = false;
82 bool skip_rest_of_patch = false;
84 bool canonicalize = false;
85 bool check_only = false;
87 char *revision = NULL; /* prerequisite revision, if any */
88 LINENUM input_lines = 0; /* how long is input file in lines */
89 int posix = 0; /* strict POSIX mode? */
91 static void reinitialize_almost_everything(void);
92 static void get_some_switches(void);
93 static LINENUM locate_hunk(LINENUM);
94 static void abort_context_hunk(void);
95 static void rej_line(int, LINENUM);
96 static void abort_hunk(void);
97 static void apply_hunk(LINENUM);
98 static void init_output(const char *);
99 static void init_reject(const char *);
100 static void copy_till(LINENUM, bool);
101 static bool spew_output(void);
102 static void dump_line(LINENUM, bool);
103 static bool patch_match(LINENUM, LINENUM, LINENUM);
104 static bool similar(const char *, const char *, int);
105 static void usage(void);
107 /* true if -E was specified on command line. */
108 static bool remove_empty_files = false;
110 /* true if -R was specified on command line. */
111 static bool reverse_flag_specified = false;
113 static bool Vflag = false;
115 /* buffer holding the name of the rejected patch file. */
116 static char rejname[PATH_MAX];
118 /* how many input lines have been irretractibly output */
119 static LINENUM last_frozen_line = 0;
121 static int Argc; /* guess */
123 static int Argc_last; /* for restarting plan_b */
124 static char **Argv_last;
126 static FILE *ofp = NULL; /* output file pointer */
127 static FILE *rejfp = NULL; /* reject file pointer */
129 static int filec = 0; /* how many file arguments? */
130 static LINENUM last_offset = 0;
131 static LINENUM maxfuzz = 2;
133 /* patch using ifdef, ifndef, etc. */
134 static bool do_defines = false;
136 static char if_defined[128];
138 static char not_defined[128];
140 static const char else_defined[] = "#else\n";
142 static char end_defined[128];
145 /* Apply a set of diffs as appropriate. */
148 main(int argc, char *argv[])
150 int error = 0, hunk, failed, i, fd;
151 bool patch_seen, reverse_seen;
152 LINENUM where = 0, newwhere, fuzz, mymaxfuzz;
156 setvbuf(stdout, NULL, _IOLBF, 0);
157 setvbuf(stderr, NULL, _IOLBF, 0);
158 for (i = 0; i < MAXFILEC; i++)
161 buf_size = INITLINELEN;
162 buf = malloc((unsigned)(buf_size));
164 fatal("out of memory\n");
166 /* Cons up the names of the temporary files. */
167 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
169 for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
172 if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
173 fatal("cannot allocate memory");
174 if ((fd = mkstemp(TMPOUTNAME)) < 0)
175 pfatal("can't create %s", TMPOUTNAME);
178 if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
179 fatal("cannot allocate memory");
180 if ((fd = mkstemp(TMPINNAME)) < 0)
181 pfatal("can't create %s", TMPINNAME);
184 if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
185 fatal("cannot allocate memory");
186 if ((fd = mkstemp(TMPREJNAME)) < 0)
187 pfatal("can't create %s", TMPREJNAME);
190 if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
191 fatal("cannot allocate memory");
192 if ((fd = mkstemp(TMPPATNAME)) < 0)
193 pfatal("can't create %s", TMPPATNAME);
196 v = getenv("SIMPLE_BACKUP_SUFFIX");
198 simple_backup_suffix = v;
200 simple_backup_suffix = ORIGEXT;
208 if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
209 v = getenv("VERSION_CONTROL");
210 if (v != NULL || !posix)
211 backup_type = get_version(v); /* OK to pass NULL. */
214 /* make sure we clean up /tmp in case of disaster */
218 for (open_patch_file(filearg[1]); there_is_another_patch();
219 reinitialize_almost_everything()) {
220 /* for each patch in patch file */
224 warn_on_invalid_line = true;
227 outname = xstrdup(filearg[0]);
229 /* for ed script just up and do it and exit */
230 if (diff_type == ED_DIFF) {
234 /* initialize the patched file */
235 if (!skip_rest_of_patch)
236 init_output(TMPOUTNAME);
238 /* initialize reject file */
239 init_reject(TMPREJNAME);
241 /* find out where all the lines are */
242 if (!skip_rest_of_patch)
243 scan_input(filearg[0]);
246 * from here on, open no standard i/o files, because
247 * malloc might misfire and we can't catch it easily
250 /* apply each hunk of patch */
253 reverse_seen = false;
255 while (another_hunk()) {
258 mymaxfuzz = pch_context();
259 if (maxfuzz < mymaxfuzz)
261 if (!skip_rest_of_patch) {
263 where = locate_hunk(fuzz);
264 if (hunk == 1 && where == 0 && !force && !reverse_seen) {
265 /* dwim for reversed patch? */
268 say("Not enough memory to try swapped hunk! Assuming unswapped.\n");
273 where = locate_hunk(fuzz);
275 /* didn't find it swapped */
277 /* put it back to normal */
278 fatal("lost hunk on alloc error!\n");
280 } else if (noreverse) {
282 /* put it back to normal */
283 fatal("lost hunk on alloc error!\n");
285 say("Ignoring previously applied (or reversed) patch.\n");
286 skip_rest_of_patch = true;
289 say("%seversed (or previously applied) patch detected! %s -R.",
290 reverse ? "R" : "Unr",
291 reverse ? "Assuming" : "Ignoring");
293 ask("%seversed (or previously applied) patch detected! %s -R? [y] ",
294 reverse ? "R" : "Unr",
295 reverse ? "Assume" : "Ignore");
297 ask("Apply anyway? [n] ");
299 skip_rest_of_patch = true;
305 /* put it back to normal */
306 fatal("lost hunk on alloc error!\n");
310 } while (!skip_rest_of_patch && where == 0 &&
311 ++fuzz <= mymaxfuzz);
313 if (skip_rest_of_patch) { /* just got decided */
314 if (ferror(ofp) || fclose(ofp)) {
315 say("Error writing %s\n",
322 newwhere = pch_newfirst() + last_offset;
323 if (skip_rest_of_patch) {
327 say("Hunk #%d ignored at %ld.\n",
329 } else if (where == 0) {
333 say("Hunk #%d failed at %ld.\n",
338 say("Hunk #%d succeeded at %ld",
341 say(" with fuzz %ld", fuzz);
343 say(" (offset %ld line%s)",
345 last_offset == 1L ? "" : "s");
351 if (out_of_mem && using_plan_a) {
354 say("\n\nRan out of memory using Plan A--trying again...\n\n");
364 fatal("Internal error: hunk should not be 0\n");
366 /* finish spewing out the new file */
367 if (!skip_rest_of_patch && !spew_output()) {
368 say("Can't write %s\n", TMPOUTNAME);
372 /* and put the output where desired */
374 if (!skip_rest_of_patch) {
376 char *realout = outname;
379 if (move_file(TMPOUTNAME, outname) < 0) {
381 realout = TMPOUTNAME;
382 chmod(TMPOUTNAME, filemode);
384 chmod(outname, filemode);
386 if (remove_empty_files &&
387 stat(realout, &statbuf) == 0 &&
388 statbuf.st_size == 0) {
390 say("Removing %s (empty after patching).\n",
396 if (ferror(rejfp) || fclose(rejfp)) {
397 say("Error writing %s\n", rejname);
403 if (*rejname == '\0') {
404 if (strlcpy(rejname, outname,
405 sizeof(rejname)) >= sizeof(rejname))
406 fatal("filename %s is too long\n", outname);
407 if (strlcat(rejname, REJEXT,
408 sizeof(rejname)) >= sizeof(rejname))
409 fatal("filename %s is too long\n", outname);
412 say("%d out of %d hunks %s--saving rejects to %s\n",
413 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname);
415 say("%d out of %d hunks %s while patching %s\n",
416 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", filearg[0]);
417 if (!check_only && move_file(TMPREJNAME, rejname) < 0)
423 if (!patch_seen && nonempty_patchf_seen)
430 /* Prepare to find the next patch to do in the patch file. */
433 reinitialize_almost_everything(void)
439 last_frozen_line = 0;
456 reverse = reverse_flag_specified;
457 skip_rest_of_patch = false;
462 /* Process switches and filenames. */
465 get_some_switches(void)
467 const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
468 static struct option longopts[] = {
469 {"backup", no_argument, 0, 'b'},
470 {"batch", no_argument, 0, 't'},
471 {"check", no_argument, 0, 'C'},
472 {"context", no_argument, 0, 'c'},
473 {"debug", required_argument, 0, 'x'},
474 {"directory", required_argument, 0, 'd'},
475 {"dry-run", no_argument, 0, 'C'},
476 {"ed", no_argument, 0, 'e'},
477 {"force", no_argument, 0, 'f'},
478 {"forward", no_argument, 0, 'N'},
479 {"fuzz", required_argument, 0, 'F'},
480 {"ifdef", required_argument, 0, 'D'},
481 {"input", required_argument, 0, 'i'},
482 {"ignore-whitespace", no_argument, 0, 'l'},
483 {"normal", no_argument, 0, 'n'},
484 {"output", required_argument, 0, 'o'},
485 {"prefix", required_argument, 0, 'B'},
486 {"quiet", no_argument, 0, 's'},
487 {"reject-file", required_argument, 0, 'r'},
488 {"remove-empty-files", no_argument, 0, 'E'},
489 {"reverse", no_argument, 0, 'R'},
490 {"silent", no_argument, 0, 's'},
491 {"strip", required_argument, 0, 'p'},
492 {"suffix", required_argument, 0, 'z'},
493 {"unified", no_argument, 0, 'u'},
494 {"version", no_argument, 0, 'v'},
495 {"version-control", required_argument, 0, 'V'},
496 {"posix", no_argument, &posix, 1},
506 optreset = optind = 1;
507 while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
510 if (backup_type == none)
511 backup_type = numbered_existing;
515 say("Warning, the ``-b suffix'' option has been"
516 " obsoleted by the -z option.\n");
519 /* must directly follow 'b' case for backwards compat */
520 simple_backup_suffix = xstrdup(optarg);
523 origprae = xstrdup(optarg);
526 diff_type = CONTEXT_DIFF;
532 if (chdir(optarg) < 0)
533 pfatal("can't cd to %s", optarg);
537 if (!isalpha((unsigned char)*optarg) && *optarg != '_')
538 fatal("argument to -D is not an identifier\n");
539 snprintf(if_defined, sizeof if_defined,
540 "#ifdef %s\n", optarg);
541 snprintf(not_defined, sizeof not_defined,
542 "#ifndef %s\n", optarg);
543 snprintf(end_defined, sizeof end_defined,
544 "#endif /* %s */\n", optarg);
550 remove_empty_files = true;
556 maxfuzz = atoi(optarg);
559 if (++filec == MAXFILEC)
560 fatal("too many file arguments\n");
561 filearg[filec] = xstrdup(optarg);
567 diff_type = NORMAL_DIFF;
573 outname = xstrdup(optarg);
576 strippath = atoi(optarg);
579 if (strlcpy(rejname, optarg,
580 sizeof(rejname)) >= sizeof(rejname))
581 fatal("argument for -r is too long\n");
585 reverse_flag_specified = true;
594 diff_type = UNI_DIFF;
600 backup_type = get_version(optarg);
605 debug = atoi(optarg);
618 filearg[0] = xstrdup(*Argv++);
621 if (++filec == MAXFILEC)
622 fatal("too many file arguments\n");
623 filearg[filec] = xstrdup(*Argv++);
628 if (getenv("POSIXLY_CORRECT") != NULL)
636 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
637 " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
638 " [-r rej-name] [-V t | nil | never | none] [-x number]\n"
639 " [-z backup-ext] [--posix] [origfile [patchfile]]\n"
640 " patch <patchfile\n");
641 my_exit(EXIT_FAILURE);
645 * Attempt to find the right place to apply this hunk of patch.
648 locate_hunk(LINENUM fuzz)
650 LINENUM first_guess = pch_first() + last_offset;
652 LINENUM pat_lines = pch_ptrn_lines();
653 LINENUM max_pos_offset = input_lines - first_guess - pat_lines + 1;
654 LINENUM max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
656 if (pat_lines == 0) { /* null range matches always */
657 if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
658 || diff_type == NEW_CONTEXT_DIFF
659 || diff_type == UNI_DIFF)) {
660 say("Empty context always matches.\n");
662 return (first_guess);
664 if (max_neg_offset >= first_guess) /* do not try lines < 0 */
665 max_neg_offset = first_guess - 1;
666 if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
668 for (offset = 1; ; offset++) {
669 bool check_after = (offset <= max_pos_offset);
670 bool check_before = (offset <= max_neg_offset);
672 if (check_after && patch_match(first_guess, offset, fuzz)) {
675 say("Offset changing from %ld to %ld\n",
676 last_offset, offset);
678 last_offset = offset;
679 return first_guess + offset;
680 } else if (check_before && patch_match(first_guess, -offset, fuzz)) {
683 say("Offset changing from %ld to %ld\n",
684 last_offset, -offset);
686 last_offset = -offset;
687 return first_guess - offset;
688 } else if (!check_before && !check_after)
693 /* We did not find the pattern, dump out the hunk so they can handle it. */
696 abort_context_hunk(void)
699 const LINENUM pat_end = pch_end();
701 * add in last_offset to guess the same as the previous successful
704 const LINENUM oldfirst = pch_first() + last_offset;
705 const LINENUM newfirst = pch_newfirst() + last_offset;
706 const LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
707 const LINENUM newlast = newfirst + pch_repl_lines() - 1;
708 const char *stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
709 const char *minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
711 fprintf(rejfp, "***************\n");
712 for (i = 0; i <= pat_end; i++) {
713 switch (pch_char(i)) {
715 if (oldlast < oldfirst)
716 fprintf(rejfp, "*** 0%s\n", stars);
717 else if (oldlast == oldfirst)
718 fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
720 fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
724 if (newlast < newfirst)
725 fprintf(rejfp, "--- 0%s\n", minuses);
726 else if (newlast == newfirst)
727 fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
729 fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
733 fprintf(rejfp, "%s", pfetch(i));
739 fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
742 fatal("fatal internal error in abort_context_hunk\n");
748 rej_line(int ch, LINENUM i)
751 const char *line = pfetch(i);
755 fprintf(rejfp, "%c%s", ch, line);
756 if (len == 0 || line[len - 1] != '\n') {
757 if (len >= USHRT_MAX)
758 fprintf(rejfp, "\n\\ Line too long\n");
760 fprintf(rejfp, "\n\\ No newline at end of line\n");
769 const LINENUM pat_end = pch_end();
770 const LINENUM oldfirst = pch_first() + last_offset;
771 const LINENUM newfirst = pch_newfirst() + last_offset;
773 if (diff_type != UNI_DIFF) {
774 abort_context_hunk();
778 for (i = 0; i <= pat_end; i++) {
779 if (pch_char(i) == '=') {
785 fprintf(rejfp, "malformed hunk: no split found\n");
790 fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
791 pch_ptrn_lines() ? oldfirst : 0,
792 pch_ptrn_lines(), newfirst, pch_repl_lines());
793 while (i < split || j <= pat_end) {
794 ch1 = i < split ? pch_char(i) : -1;
795 ch2 = j <= pat_end ? pch_char(j) : -1;
799 } else if (ch1 == ' ' && ch2 == ' ') {
803 } else if (ch1 == '!' && ch2 == '!') {
804 while (i < split && ch1 == '!') {
807 ch1 = i < split ? pch_char(i) : -1;
809 while (j <= pat_end && ch2 == '!') {
812 ch2 = j <= pat_end ? pch_char(j) : -1;
814 } else if (ch1 == '*') {
816 } else if (ch2 == '+' || ch2 == ' ') {
820 fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
829 /* We found where to apply it (we hope), so do it. */
832 apply_hunk(LINENUM where)
835 const LINENUM lastline = pch_ptrn_lines();
836 LINENUM new = lastline + 1;
841 int def_state = OUTSIDE;
842 const LINENUM pat_end = pch_end();
845 while (pch_char(new) == '=' || pch_char(new) == '\n')
848 while (old <= lastline) {
849 if (pch_char(old) == '-') {
850 copy_till(where + old - 1, false);
852 if (def_state == OUTSIDE) {
853 fputs(not_defined, ofp);
854 def_state = IN_IFNDEF;
855 } else if (def_state == IN_IFDEF) {
856 fputs(else_defined, ofp);
859 fputs(pfetch(old), ofp);
863 } else if (new > pat_end) {
865 } else if (pch_char(new) == '+') {
866 copy_till(where + old - 1, false);
868 if (def_state == IN_IFNDEF) {
869 fputs(else_defined, ofp);
871 } else if (def_state == OUTSIDE) {
872 fputs(if_defined, ofp);
873 def_state = IN_IFDEF;
876 fputs(pfetch(new), ofp);
878 } else if (pch_char(new) != pch_char(old)) {
879 say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
880 pch_hunk_beg() + old,
881 pch_hunk_beg() + new);
883 say("oldchar = '%c', newchar = '%c'\n",
884 pch_char(old), pch_char(new));
887 } else if (pch_char(new) == '!') {
888 copy_till(where + old - 1, false);
890 fputs(not_defined, ofp);
891 def_state = IN_IFNDEF;
893 while (pch_char(old) == '!') {
895 fputs(pfetch(old), ofp);
901 fputs(else_defined, ofp);
904 while (pch_char(new) == '!') {
905 fputs(pfetch(new), ofp);
909 if (pch_char(new) != ' ')
910 fatal("Internal error: expected ' '\n");
913 if (do_defines && def_state != OUTSIDE) {
914 fputs(end_defined, ofp);
919 if (new <= pat_end && pch_char(new) == '+') {
920 copy_till(where + old - 1, false);
922 if (def_state == OUTSIDE) {
923 fputs(if_defined, ofp);
924 def_state = IN_IFDEF;
925 } else if (def_state == IN_IFNDEF) {
926 fputs(else_defined, ofp);
930 while (new <= pat_end && pch_char(new) == '+') {
931 fputs(pfetch(new), ofp);
935 if (do_defines && def_state != OUTSIDE) {
936 fputs(end_defined, ofp);
944 init_output(const char *name)
946 ofp = fopen(name, "w");
948 pfatal("can't create %s", name);
952 * Open a file to put hunks we can't locate.
955 init_reject(const char *name)
957 rejfp = fopen(name, "w");
959 pfatal("can't create %s", name);
963 * Copy input file to output, up to wherever hunk is to be applied.
964 * If endoffile is true, treat the last line specially since it may
968 copy_till(LINENUM lastline, bool endoffile)
970 if (last_frozen_line > lastline)
971 fatal("misordered hunks! output would be garbled\n");
972 while (last_frozen_line < lastline) {
973 if (++last_frozen_line == lastline && endoffile)
974 dump_line(last_frozen_line, !last_line_missing_eol);
976 dump_line(last_frozen_line, true);
981 * Finish copying the input file to the output file.
990 say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
993 copy_till(input_lines, true); /* dump remainder of file */
994 rv = ferror(ofp) == 0 && fclose(ofp) == 0;
1000 * Copy one line from input to output.
1003 dump_line(LINENUM line, bool write_newline)
1007 s = ifetch(line, 0);
1010 /* Note: string is not NUL terminated. */
1011 for (; *s != '\n'; s++)
1018 * Does the patch pattern match at line base+offset?
1021 patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
1023 LINENUM pline = 1 + fuzz;
1025 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
1026 const char *ilineptr;
1027 const char *plineptr;
1028 unsigned short plinelen;
1030 /* Patch does not match if we don't have any more context to use */
1031 if (pline > pat_lines)
1033 for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
1034 ilineptr = ifetch(iline, offset >= 0);
1035 if (ilineptr == NULL)
1037 plineptr = pfetch(pline);
1038 plinelen = pch_line_len(pline);
1040 if (!similar(ilineptr, plineptr, plinelen))
1042 } else if (strnNE(ilineptr, plineptr, plinelen))
1044 if (iline == input_lines) {
1046 * We are looking at the last line of the file.
1047 * If the file has no eol, the patch line should
1048 * not have one either and vice-versa. Note that
1051 if (last_line_missing_eol) {
1052 if (plineptr[plinelen - 1] == '\n')
1055 if (plineptr[plinelen - 1] != '\n')
1064 * Do two lines match with canonicalized white space?
1067 similar(const char *a, const char *b, int len)
1070 if (isspace((unsigned char)*b)) { /* whitespace (or \n) to match? */
1071 if (!isspace((unsigned char)*a)) /* no corresponding whitespace? */
1073 while (len && isspace((unsigned char)*b) && *b != '\n')
1074 b++, len--; /* skip pattern whitespace */
1075 while (isspace((unsigned char)*a) && *a != '\n')
1076 a++; /* skip target whitespace */
1077 if (*a == '\n' || *b == '\n')
1078 return (*a == *b); /* should end in sync */
1079 } else if (*a++ != *b++) /* match non-whitespace chars */
1082 len--; /* probably not necessary */
1084 return true; /* actually, this is not reached */
1085 /* since there is always a \n */