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 */
59 char *filearg[MAXFILEC];
60 bool ok_to_create_file = false;
62 char *origprae = NULL;
67 bool toutkeep = false;
68 bool trejkeep = false;
69 bool warn_on_invalid_line;
70 bool last_line_missing_eol;
80 bool noreverse = false;
81 bool skip_rest_of_patch = false;
83 bool canonicalize = false;
84 bool check_only = false;
86 char *revision = NULL; /* prerequisite revision, if any */
87 LINENUM input_lines = 0; /* how long is input file in lines */
88 int posix = 0; /* strict POSIX mode? */
90 static void reinitialize_almost_everything(void);
91 static void get_some_switches(void);
92 static LINENUM locate_hunk(LINENUM);
93 static void abort_context_hunk(void);
94 static void rej_line(int, LINENUM);
95 static void abort_hunk(void);
96 static void apply_hunk(LINENUM);
97 static void init_output(const char *);
98 static void init_reject(const char *);
99 static void copy_till(LINENUM, bool);
100 static bool spew_output(void);
101 static void dump_line(LINENUM, bool);
102 static bool patch_match(LINENUM, LINENUM, LINENUM);
103 static bool similar(const char *, const char *, int);
104 static void usage(void);
106 /* true if -E was specified on command line. */
107 static bool remove_empty_files = false;
109 /* true if -R was specified on command line. */
110 static bool reverse_flag_specified = false;
112 static bool Vflag = false;
114 /* buffer holding the name of the rejected patch file. */
115 static char rejname[NAME_MAX + 1];
117 /* how many input lines have been irretractibly output */
118 static LINENUM last_frozen_line = 0;
120 static int Argc; /* guess */
122 static int Argc_last; /* for restarting plan_b */
123 static char **Argv_last;
125 static FILE *ofp = NULL; /* output file pointer */
126 static FILE *rejfp = NULL; /* reject file pointer */
128 static int filec = 0; /* how many file arguments? */
129 static LINENUM last_offset = 0;
130 static LINENUM maxfuzz = 2;
132 /* patch using ifdef, ifndef, etc. */
133 static bool do_defines = false;
135 static char if_defined[128];
137 static char not_defined[128];
139 static const char else_defined[] = "#else\n";
141 static char end_defined[128];
144 /* Apply a set of diffs as appropriate. */
147 main(int argc, char *argv[])
149 int error = 0, hunk, failed, i, fd;
150 bool patch_seen, reverse_seen;
151 LINENUM where = 0, newwhere, fuzz, mymaxfuzz;
155 setvbuf(stdout, NULL, _IOLBF, 0);
156 setvbuf(stderr, NULL, _IOLBF, 0);
157 for (i = 0; i < MAXFILEC; i++)
160 buf_size = INITLINELEN;
161 buf = malloc((unsigned)(buf_size));
163 fatal("out of memory\n");
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--)
171 if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
172 fatal("cannot allocate memory");
173 if ((fd = mkstemp(TMPOUTNAME)) < 0)
174 pfatal("can't create %s", TMPOUTNAME);
177 if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
178 fatal("cannot allocate memory");
179 if ((fd = mkstemp(TMPINNAME)) < 0)
180 pfatal("can't create %s", TMPINNAME);
183 if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
184 fatal("cannot allocate memory");
185 if ((fd = mkstemp(TMPREJNAME)) < 0)
186 pfatal("can't create %s", TMPREJNAME);
189 if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
190 fatal("cannot allocate memory");
191 if ((fd = mkstemp(TMPPATNAME)) < 0)
192 pfatal("can't create %s", TMPPATNAME);
195 v = getenv("SIMPLE_BACKUP_SUFFIX");
197 simple_backup_suffix = v;
199 simple_backup_suffix = ORIGEXT;
207 if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
208 v = getenv("VERSION_CONTROL");
209 if (v != NULL || !posix)
210 backup_type = get_version(v); /* OK to pass NULL. */
213 /* make sure we clean up /tmp in case of disaster */
217 for (open_patch_file(filearg[1]); there_is_another_patch();
218 reinitialize_almost_everything()) {
219 /* for each patch in patch file */
223 warn_on_invalid_line = true;
226 outname = xstrdup(filearg[0]);
228 /* for ed script just up and do it and exit */
229 if (diff_type == ED_DIFF) {
233 /* initialize the patched file */
234 if (!skip_rest_of_patch)
235 init_output(TMPOUTNAME);
237 /* initialize reject file */
238 init_reject(TMPREJNAME);
240 /* find out where all the lines are */
241 if (!skip_rest_of_patch)
242 scan_input(filearg[0]);
245 * from here on, open no standard i/o files, because
246 * malloc might misfire and we can't catch it easily
249 /* apply each hunk of patch */
252 reverse_seen = false;
254 while (another_hunk()) {
257 mymaxfuzz = pch_context();
258 if (maxfuzz < mymaxfuzz)
260 if (!skip_rest_of_patch) {
262 where = locate_hunk(fuzz);
263 if (hunk == 1 && where == 0 && !force && !reverse_seen) {
264 /* dwim for reversed patch? */
267 say("Not enough memory to try swapped hunk! Assuming unswapped.\n");
272 where = locate_hunk(fuzz);
274 /* didn't find it swapped */
276 /* put it back to normal */
277 fatal("lost hunk on alloc error!\n");
279 } else if (noreverse) {
281 /* put it back to normal */
282 fatal("lost hunk on alloc error!\n");
284 say("Ignoring previously applied (or reversed) patch.\n");
285 skip_rest_of_patch = true;
288 say("%seversed (or previously applied) patch detected! %s -R.",
289 reverse ? "R" : "Unr",
290 reverse ? "Assuming" : "Ignoring");
292 ask("%seversed (or previously applied) patch detected! %s -R? [y] ",
293 reverse ? "R" : "Unr",
294 reverse ? "Assume" : "Ignore");
296 ask("Apply anyway? [n] ");
298 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);
411 say("%d out of %d hunks %s--saving rejects to %s\n",
412 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname);
414 say("%d out of %d hunks %s while patching %s\n",
415 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", filearg[0]);
416 if (!check_only && move_file(TMPREJNAME, rejname) < 0)
429 /* Prepare to find the next patch to do in the patch file. */
432 reinitialize_almost_everything(void)
438 last_frozen_line = 0;
455 reverse = reverse_flag_specified;
456 skip_rest_of_patch = false;
461 /* Process switches and filenames. */
464 get_some_switches(void)
466 const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
467 static struct option longopts[] = {
468 {"backup", no_argument, 0, 'b'},
469 {"batch", no_argument, 0, 't'},
470 {"check", no_argument, 0, 'C'},
471 {"context", no_argument, 0, 'c'},
472 {"debug", required_argument, 0, 'x'},
473 {"directory", required_argument, 0, 'd'},
474 {"dry-run", no_argument, 0, 'C'},
475 {"ed", no_argument, 0, 'e'},
476 {"force", no_argument, 0, 'f'},
477 {"forward", no_argument, 0, 'N'},
478 {"fuzz", required_argument, 0, 'F'},
479 {"ifdef", required_argument, 0, 'D'},
480 {"input", required_argument, 0, 'i'},
481 {"ignore-whitespace", no_argument, 0, 'l'},
482 {"normal", no_argument, 0, 'n'},
483 {"output", required_argument, 0, 'o'},
484 {"prefix", required_argument, 0, 'B'},
485 {"quiet", no_argument, 0, 's'},
486 {"reject-file", required_argument, 0, 'r'},
487 {"remove-empty-files", no_argument, 0, 'E'},
488 {"reverse", no_argument, 0, 'R'},
489 {"silent", no_argument, 0, 's'},
490 {"strip", required_argument, 0, 'p'},
491 {"suffix", required_argument, 0, 'z'},
492 {"unified", no_argument, 0, 'u'},
493 {"version", no_argument, 0, 'v'},
494 {"version-control", required_argument, 0, 'V'},
495 {"posix", no_argument, &posix, 1},
505 optreset = optind = 1;
506 while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
509 if (backup_type == none)
510 backup_type = numbered_existing;
514 say("Warning, the ``-b suffix'' option has been"
515 " obsoleted by the -z option.\n");
518 /* must directly follow 'b' case for backwards compat */
519 simple_backup_suffix = xstrdup(optarg);
522 origprae = xstrdup(optarg);
525 diff_type = CONTEXT_DIFF;
531 if (chdir(optarg) < 0)
532 pfatal("can't cd to %s", optarg);
536 if (!isalpha((unsigned char)*optarg) && *optarg != '_')
537 fatal("argument to -D is not an identifier\n");
538 snprintf(if_defined, sizeof if_defined,
539 "#ifdef %s\n", optarg);
540 snprintf(not_defined, sizeof not_defined,
541 "#ifndef %s\n", optarg);
542 snprintf(end_defined, sizeof end_defined,
543 "#endif /* %s */\n", optarg);
549 remove_empty_files = true;
555 maxfuzz = atoi(optarg);
558 if (++filec == MAXFILEC)
559 fatal("too many file arguments\n");
560 filearg[filec] = xstrdup(optarg);
566 diff_type = NORMAL_DIFF;
572 outname = xstrdup(optarg);
575 strippath = atoi(optarg);
578 if (strlcpy(rejname, optarg,
579 sizeof(rejname)) >= sizeof(rejname))
580 fatal("argument for -r is too long\n");
584 reverse_flag_specified = true;
593 diff_type = UNI_DIFF;
599 backup_type = get_version(optarg);
604 debug = atoi(optarg);
617 filearg[0] = xstrdup(*Argv++);
620 if (++filec == MAXFILEC)
621 fatal("too many file arguments\n");
622 filearg[filec] = xstrdup(*Argv++);
627 if (getenv("POSIXLY_CORRECT") != NULL)
635 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
636 " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
637 " [-r rej-name] [-V t | nil | never | none] [-x number]\n"
638 " [-z backup-ext] [--posix] [origfile [patchfile]]\n"
639 " patch <patchfile\n");
640 my_exit(EXIT_FAILURE);
644 * Attempt to find the right place to apply this hunk of patch.
647 locate_hunk(LINENUM fuzz)
649 LINENUM first_guess = pch_first() + last_offset;
651 LINENUM pat_lines = pch_ptrn_lines();
652 LINENUM max_pos_offset = input_lines - first_guess - pat_lines + 1;
653 LINENUM max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
655 if (pat_lines == 0) { /* null range matches always */
656 if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
657 || diff_type == NEW_CONTEXT_DIFF
658 || diff_type == UNI_DIFF)) {
659 say("Empty context always matches.\n");
661 return (first_guess);
663 if (max_neg_offset >= first_guess) /* do not try lines < 0 */
664 max_neg_offset = first_guess - 1;
665 if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
667 for (offset = 1; ; offset++) {
668 bool check_after = (offset <= max_pos_offset);
669 bool check_before = (offset <= max_neg_offset);
671 if (check_after && patch_match(first_guess, offset, fuzz)) {
674 say("Offset changing from %ld to %ld\n",
675 last_offset, offset);
677 last_offset = offset;
678 return first_guess + offset;
679 } else if (check_before && patch_match(first_guess, -offset, fuzz)) {
682 say("Offset changing from %ld to %ld\n",
683 last_offset, -offset);
685 last_offset = -offset;
686 return first_guess - offset;
687 } else if (!check_before && !check_after)
692 /* We did not find the pattern, dump out the hunk so they can handle it. */
695 abort_context_hunk(void)
698 const LINENUM pat_end = pch_end();
700 * add in last_offset to guess the same as the previous successful
703 const LINENUM oldfirst = pch_first() + last_offset;
704 const LINENUM newfirst = pch_newfirst() + last_offset;
705 const LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
706 const LINENUM newlast = newfirst + pch_repl_lines() - 1;
707 const char *stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
708 const char *minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
710 fprintf(rejfp, "***************\n");
711 for (i = 0; i <= pat_end; i++) {
712 switch (pch_char(i)) {
714 if (oldlast < oldfirst)
715 fprintf(rejfp, "*** 0%s\n", stars);
716 else if (oldlast == oldfirst)
717 fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
719 fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
723 if (newlast < newfirst)
724 fprintf(rejfp, "--- 0%s\n", minuses);
725 else if (newlast == newfirst)
726 fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
728 fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
732 fprintf(rejfp, "%s", pfetch(i));
738 fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
741 fatal("fatal internal error in abort_context_hunk\n");
747 rej_line(int ch, LINENUM i)
750 const char *line = pfetch(i);
754 fprintf(rejfp, "%c%s", ch, line);
755 if (len == 0 || line[len - 1] != '\n') {
756 if (len >= USHRT_MAX)
757 fprintf(rejfp, "\n\\ Line too long\n");
759 fprintf(rejfp, "\n\\ No newline at end of line\n");
768 const LINENUM pat_end = pch_end();
769 const LINENUM oldfirst = pch_first() + last_offset;
770 const LINENUM newfirst = pch_newfirst() + last_offset;
772 if (diff_type != UNI_DIFF) {
773 abort_context_hunk();
777 for (i = 0; i <= pat_end; i++) {
778 if (pch_char(i) == '=') {
784 fprintf(rejfp, "malformed hunk: no split found\n");
789 fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
790 pch_ptrn_lines() ? oldfirst : 0,
791 pch_ptrn_lines(), newfirst, pch_repl_lines());
792 while (i < split || j <= pat_end) {
793 ch1 = i < split ? pch_char(i) : -1;
794 ch2 = j <= pat_end ? pch_char(j) : -1;
798 } else if (ch1 == ' ' && ch2 == ' ') {
802 } else if (ch1 == '!' && ch2 == '!') {
803 while (i < split && ch1 == '!') {
806 ch1 = i < split ? pch_char(i) : -1;
808 while (j <= pat_end && ch2 == '!') {
811 ch2 = j <= pat_end ? pch_char(j) : -1;
813 } else if (ch1 == '*') {
815 } else if (ch2 == '+' || ch2 == ' ') {
819 fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
828 /* We found where to apply it (we hope), so do it. */
831 apply_hunk(LINENUM where)
834 const LINENUM lastline = pch_ptrn_lines();
835 LINENUM new = lastline + 1;
840 int def_state = OUTSIDE;
841 const LINENUM pat_end = pch_end();
844 while (pch_char(new) == '=' || pch_char(new) == '\n')
847 while (old <= lastline) {
848 if (pch_char(old) == '-') {
849 copy_till(where + old - 1, false);
851 if (def_state == OUTSIDE) {
852 fputs(not_defined, ofp);
853 def_state = IN_IFNDEF;
854 } else if (def_state == IN_IFDEF) {
855 fputs(else_defined, ofp);
858 fputs(pfetch(old), ofp);
862 } else if (new > pat_end) {
864 } else if (pch_char(new) == '+') {
865 copy_till(where + old - 1, false);
867 if (def_state == IN_IFNDEF) {
868 fputs(else_defined, ofp);
870 } else if (def_state == OUTSIDE) {
871 fputs(if_defined, ofp);
872 def_state = IN_IFDEF;
875 fputs(pfetch(new), ofp);
877 } else if (pch_char(new) != pch_char(old)) {
878 say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
879 pch_hunk_beg() + old,
880 pch_hunk_beg() + new);
882 say("oldchar = '%c', newchar = '%c'\n",
883 pch_char(old), pch_char(new));
886 } else if (pch_char(new) == '!') {
887 copy_till(where + old - 1, false);
889 fputs(not_defined, ofp);
890 def_state = IN_IFNDEF;
892 while (pch_char(old) == '!') {
894 fputs(pfetch(old), ofp);
900 fputs(else_defined, ofp);
903 while (pch_char(new) == '!') {
904 fputs(pfetch(new), ofp);
908 if (pch_char(new) != ' ')
909 fatal("Internal error: expected ' '\n");
912 if (do_defines && def_state != OUTSIDE) {
913 fputs(end_defined, ofp);
918 if (new <= pat_end && pch_char(new) == '+') {
919 copy_till(where + old - 1, false);
921 if (def_state == OUTSIDE) {
922 fputs(if_defined, ofp);
923 def_state = IN_IFDEF;
924 } else if (def_state == IN_IFNDEF) {
925 fputs(else_defined, ofp);
929 while (new <= pat_end && pch_char(new) == '+') {
930 fputs(pfetch(new), ofp);
934 if (do_defines && def_state != OUTSIDE) {
935 fputs(end_defined, ofp);
943 init_output(const char *name)
945 ofp = fopen(name, "w");
947 pfatal("can't create %s", name);
951 * Open a file to put hunks we can't locate.
954 init_reject(const char *name)
956 rejfp = fopen(name, "w");
958 pfatal("can't create %s", name);
962 * Copy input file to output, up to wherever hunk is to be applied.
963 * If endoffile is true, treat the last line specially since it may
967 copy_till(LINENUM lastline, bool endoffile)
969 if (last_frozen_line > lastline)
970 fatal("misordered hunks! output would be garbled\n");
971 while (last_frozen_line < lastline) {
972 if (++last_frozen_line == lastline && endoffile)
973 dump_line(last_frozen_line, !last_line_missing_eol);
975 dump_line(last_frozen_line, true);
980 * Finish copying the input file to the output file.
989 say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
992 copy_till(input_lines, true); /* dump remainder of file */
993 rv = ferror(ofp) == 0 && fclose(ofp) == 0;
999 * Copy one line from input to output.
1002 dump_line(LINENUM line, bool write_newline)
1006 s = ifetch(line, 0);
1009 /* Note: string is not NUL terminated. */
1010 for (; *s != '\n'; s++)
1017 * Does the patch pattern match at line base+offset?
1020 patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
1022 LINENUM pline = 1 + fuzz;
1024 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
1025 const char *ilineptr;
1026 const char *plineptr;
1027 unsigned short plinelen;
1029 for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
1030 ilineptr = ifetch(iline, offset >= 0);
1031 if (ilineptr == NULL)
1033 plineptr = pfetch(pline);
1034 plinelen = pch_line_len(pline);
1036 if (!similar(ilineptr, plineptr, plinelen))
1038 } else if (strnNE(ilineptr, plineptr, plinelen))
1040 if (iline == input_lines) {
1042 * We are looking at the last line of the file.
1043 * If the file has no eol, the patch line should
1044 * not have one either and vice-versa. Note that
1047 if (last_line_missing_eol) {
1048 if (plineptr[plinelen - 1] == '\n')
1051 if (plineptr[plinelen - 1] != '\n')
1060 * Do two lines match with canonicalized white space?
1063 similar(const char *a, const char *b, int len)
1066 if (isspace((unsigned char)*b)) { /* whitespace (or \n) to match? */
1067 if (!isspace((unsigned char)*a)) /* no corresponding whitespace? */
1069 while (len && isspace((unsigned char)*b) && *b != '\n')
1070 b++, len--; /* skip pattern whitespace */
1071 while (isspace((unsigned char)*a) && *a != '\n')
1072 a++; /* skip target whitespace */
1073 if (*a == '\n' || *b == '\n')
1074 return (*a == *b); /* should end in sync */
1075 } else if (*a++ != *b++) /* match non-whitespace chars */
1078 len--; /* probably not necessary */
1080 return true; /* actually, this is not reached */
1081 /* since there is always a \n */