Signed-off-by: Mattias Andrée <[email protected]>
---
 patch.c | 182 +++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 95 insertions(+), 87 deletions(-)

diff --git a/patch.c b/patch.c
index c118dc9..caf34be 100644
--- a/patch.c
+++ b/patch.c
@@ -41,7 +41,7 @@
 #define linecpy2mem(d, s)  (memcpy(d, (s).data, (s).len + 1))
 #define missinglf(l)       ((l).len && (l).data[(l).len - 1] != '\n')
 #define fwriteline(f, l)   (fwrite((l).data, 1, (l).len, f))
-#define enmemdup(f, s, n)  ((n) ? memcpy(enmalloc(f, n), s, n) : 0)
+#define enmemdup(f, s, n)  ((n) ? memcpy(enmalloc(f, n), s, n) : NULL)
 
 enum { REJECTED = 1, FAILURE = 2 };
 enum applicability { APPLICABLE, APPLIED, INAPPLICABLE };
@@ -104,10 +104,10 @@ struct patched_file {
 };
 
 static enum format specified_format = GUESS;
-static const char *patchfile = 0;
-static char *rejectfile = 0;
-static const char *outfile = 0;
-static char *apply_patches_to = 0;
+static const char *patchfile = NULL;
+static char *rejectfile = NULL;
+static const char *outfile = NULL;
+static char *apply_patches_to = NULL;
 static size_t pflag = SIZE_MAX;
 static int bflag = 0;
 static int fflag = 0;
@@ -115,16 +115,16 @@ static int lflag = 0;
 static int Rflag = 0;
 static int Nflag = 0;
 static int Uflag = 0;
-static char *dflag = 0;
+static char *dflag = NULL;
 static int rejected = 0;
-static struct patched_file *prevpatch = 0;
+static struct patched_file *prevpatch = NULL;
 static size_t prevpatchn = 0;
-static struct patched_file *prevout = 0;
+static struct patched_file *prevout = NULL;
 static size_t prevoutn = 0;
 static char stdin_dash[sizeof("-")];
 static char stdout_dash[sizeof("-")];
-static char *ifdef = 0;
-static char *ifndef = 0;
+static char *ifdef = NULL;
+static char *ifndef = NULL;
 
 static void
 usage(void)
@@ -134,7 +134,7 @@ usage(void)
 }
 
 static void
-load_lines(const char *path, struct file_data *out, int skip_lf, int orig)
+load_lines(struct file_data *out, const char *path, int skip_lf, int orig)
 {
        FILE *f;
        struct linebuf b = EMPTY_LINEBUF;
@@ -165,12 +165,12 @@ static char *
 ask(const char *instruction)
 {
        FILE *f;
-       char *answer = 0;
+       char *answer = NULL;
        size_t size = 0;
        ssize_t n;
 
        if (fflag)
-               return 0;
+               return NULL;
 
        if (!(f = fopen("/dev/tty", "r+")))
                enprintf(FAILURE, "fopen /dev/tty:");
@@ -180,13 +180,13 @@ ask(const char *instruction)
        fflush(stdout);
 
        if ((n = getline(&answer, &size, f)) <= 0) {
-               answer = 0;
+               answer = NULL;
        } else {
                n -= (answer[n - 1] == '\n');
-               answer[n] = 0;
+               answer[n] = '\0';
                if (!*answer) {
                        free(answer);
-                       answer = 0;
+                       answer = NULL;
                }
        }
 
@@ -202,13 +202,13 @@ adjust_filename(const char *filename)
        const char *stripped = filename;
        char *rc;
 
-       if (p == filename || p[-1] == '/')
-               return 0;
+       if (p == filename || *(p - 1) == '/')
+               return NULL;
 
        for (; strips && (p = strchr(stripped, '/')); strips--)
                for (stripped = p; *stripped == '/'; stripped++);
        if (strips && pflag != SIZE_MAX)
-               return 0;
+               return NULL;
 
        if (dflag && *stripped != '/')
                enasprintf(FAILURE, &rc, "%s/%s", dflag, stripped);
@@ -283,7 +283,7 @@ unquote(char *str)
                }
        }
 
-       str[w] = 0;
+       str[w] = '\0';
        return 0;
 }
 
@@ -302,7 +302,7 @@ parse_diff_line(char *str, char **old, char **new)
        char *s = strchr(str, '\0');
        int ret = 0;
 
-       *new = 0;
+       *new = NULL;
        if (s == str)
                return -1;
 
@@ -313,7 +313,7 @@ again:
                                goto found;
        } else {
                while (--s != str && s - 1 != str)
-                       if (s[-1] == ' ' && s[0] == '"')
+                       if (*(s - 1) == ' ' && s[0] == '"')
                                goto found;
        }
 
@@ -348,19 +348,24 @@ ask_for_filename(struct patchset *patchset)
 {
        size_t i;
        char *answer;
+       int missing = 0;
 
-       for (i = 0; i < patchset->npatches; i++)
-               if (!patchset->patches[i].path)
-                       goto found_unset;
-       return;
+       for (i = 0; i < patchset->npatches; i++) {
+               if (!patchset->patches[i].path) {
+                       missing = 1;
+                       break;
+               }
+       }
+
+       if (!missing)
+               return;
 
-found_unset:
        if (!(answer = ask("please enter name of file to patch")))
                exit(FAILURE);
 
-       /* Two assumtions are made here. (1) if there are multiple
-        * patches to unnamed failes, they are all to the same file,
-        * (with the specifications for -o to justify this assumption,)
+       /* Two assumptions are made here. (1) if there are multiple
+        * patches to unnamed files, they are all to the same file,
+        * (with the specifications for -o to justify this assumption)
         * and (2) no developer is going to free the filenames. */
 
        if (access(answer, F_OK)) {
@@ -387,7 +392,7 @@ reverse_hunk(struct parsed_hunk *hunk)
 {
        struct hunk_content cont;
        char *annot;
-       cont = hunk->old, hunk->old  = hunk->new, hunk->new = cont;
+       cont = hunk->old, hunk->old = hunk->new, hunk->new = cont;
        annot = hunk->annot, hunk->annot = hunk->rannot, hunk->rannot = annot;
 }
 
@@ -400,7 +405,7 @@ get_last_patch(const char *path)
 
        if (!outfile && !ifdef) {
                data = enmalloc(FAILURE, sizeof(*data));
-               load_lines(path, data, 0, 0);
+               load_lines(data, path, 0, 0);
                return data;
        }
 
@@ -423,7 +428,7 @@ get_last_patch(const char *path)
        prevpatch[prevpatchn++].data = data = enmalloc(FAILURE, 
sizeof(*prevpatch->data));
 
 load_data:
-       load_lines(path, data, 0, 1);
+       load_lines(data, path, 0, 1);
        return data;
 }
 
@@ -442,7 +447,7 @@ is_first_patch(const char *path)
 
        prevpatch = enrealloc(FAILURE, prevpatch, (prevpatchn + 1) * 
sizeof(*prevpatch));
        storefile(prevpatch + prevpatchn, attr);
-       prevpatch[prevpatchn].data = 0;
+       prevpatch[prevpatchn].data = NULL;
        prevpatchn++;
 
        return 1;
@@ -487,7 +492,7 @@ start_over:
 
        prevout = enrealloc(FAILURE, prevout, (prevoutn + 1) * 
sizeof(*prevout));
        storefile(prevout + prevoutn, attr);
-       prevout[prevoutn].data = 0;
+       prevout[prevoutn].data = NULL;
        prevoutn++;
 
        return "w";
@@ -534,11 +539,11 @@ get_filename_from_context_header(char *line)
                        break;
        }
 
-       old_end = *end, *end = 0;
+       old_end = *end, *end = '\0';
        rc = enstrdup(FAILURE, line);
        *end = old_end;
 
-       return unquote(rc) ? 0 : rc;
+       return unquote(rc) ? NULL : rc;
 }
 
 static int
@@ -574,7 +579,7 @@ parse_range(char *str, size_t *first, size_t *last, int 
*have_last)
        errno = 0;
        *first = strtoul(str, &str, 10);
        if (errno)
-               return 0;
+               return NULL;
        *last = 1;
        if (have_last)
                *have_last = *str == ',';
@@ -582,7 +587,7 @@ parse_range(char *str, size_t *first, size_t *last, int 
*have_last)
                errno = 0;
                *last = strtoul(str + 1, &str, 10);
                if (errno)
-                       return 0;
+                       return NULL;
        }
        return str;
 }
@@ -597,7 +602,7 @@ parse_patch_normal(struct patch *patch, struct line *lines)
        struct hunk hunk;
 
        patch->nhunks = 0;
-       patch->hunks = 0;
+       patch->hunks = NULL;
 
        while (guess_format(lines) == NORMAL) {
                added = deleted = 0;
@@ -663,9 +668,8 @@ out:
                lines = patch->hunks[patch->nhunks - 1].lines;
                lines += patch->hunks[patch->nhunks - 1].nlines;
                return lines - lines_orig;
-       } else {
-               return 0;
        }
+       return 0;
 }
 
 static size_t
@@ -673,12 +677,12 @@ parse_patch_copied(struct patch *patch, struct line 
*lines)
 {
        struct line *lines_orig = lines;
        char *p;
-       size_t n, first, last = 0;
+       size_t n, first = 0, last = 0;
        int have_last;
        struct hunk hunk;
 
        patch->nhunks = 0;
-       patch->hunks = 0;
+       patch->hunks = NULL;
 
        if (parse_context_header(patch, lines, "***", "---"))
                return 0;
@@ -751,9 +755,8 @@ out:
                lines = patch->hunks[patch->nhunks - 1].lines;
                lines += patch->hunks[patch->nhunks - 1].nlines;
                return lines - lines_orig;
-       } else {
-               return 0;
        }
+       return 0;
 }
 
 static size_t
@@ -767,7 +770,7 @@ parse_patch_unified(struct patch *patch, struct line *lines)
        (void) unused;
 
        patch->nhunks = 0;
-       patch->hunks = 0;
+       patch->hunks = NULL;
 
        if (parse_context_header(patch, lines, "---", "+++"))
                return 0;
@@ -777,9 +780,9 @@ parse_patch_unified(struct patch *patch, struct line *lines)
                hunk.head = lines++;
                hunk.lines = lines;
 
-               if (!(p = parse_range(hunk.head->data + 4, &unused, &a_count, 
0)) ||
+               if (!(p = parse_range(hunk.head->data + 4, &unused, &a_count, 
NULL)) ||
                    !strstart(p, " +") ||
-                   !(p = parse_range(p + 2, &unused, &b_count, 0)) ||
+                   !(p = parse_range(p + 2, &unused, &b_count, NULL)) ||
                    !strstart(p, " @@") || !isnulblank(p[3]))
                        goto out;
 
@@ -813,9 +816,8 @@ out:
                lines = patch->hunks[patch->nhunks - 1].lines;
                lines += patch->hunks[patch->nhunks - 1].nlines;
                return lines - lines_orig;
-       } else {
-               return 0;
        }
+       return 0;
 }
 
 static size_t
@@ -828,7 +830,7 @@ parse_patch_ed(struct patch *patch, struct line *lines)
                        lines_last = ++lines;
                        continue;
                }
-               last_addition = 0;
+               last_addition = NULL;
        again:
                while ((++lines)->data && !lineeq(*lines, "."))
                        last_addition = lines;
@@ -838,7 +840,7 @@ parse_patch_ed(struct patch *patch, struct line *lines)
                        if ((!lineeq(*lines, "s/.//")  && !lineeq(*lines, 
"s/\\.//") &&
                             !lineeq(*lines, "s/^.//") && !lineeq(*lines, 
"s/^\\.//")) ||
                            !last_addition || !last_addition->len || 
last_addition->data[0] != '.') {
-                               /* This is just so parse_ed_script does not 
have too be overkill. */
+                               /* This is just so parse_ed_script does not 
have to be overkill. */
                                weprintf("suspicious line in ed-script, 
treating hunk as garbage: ");
                                fwriteline(stderr, *lines);
                                fprintf(stderr, "\n");
@@ -857,7 +859,7 @@ parse_patch_ed(struct patch *patch, struct line *lines)
 
        patch->nhunks = 1;
        patch->hunks = enmalloc(FAILURE, sizeof(*(patch->hunks)));
-       patch->hunks->head = 0;
+       patch->hunks->head = NULL;
        patch->hunks->lines = lines_orig;
        patch->hunks->nlines = (size_t)(lines_last - lines_orig);
 
@@ -906,12 +908,12 @@ parse_patchfile(struct patchset *ps, struct line *lines)
                        continue;
                }
 
-               diff_line = index_line = 0;
+               diff_line = index_line = NULL;
                for (i = 1; i <= garbage_since_last_patch; i++) {
-                       if (!diff_line && linestart(lines[-i], "diff ") && 
!containsnul(lines[-i]))
-                               diff_line = lines[-i].data;
-                       if (!index_line && linestart(lines[-i], "Index:") && 
!containsnul(lines[-i]))
-                               index_line = lines[-i].data;
+                       if (!diff_line && linestart(*(lines - i), "diff ") && 
!containsnul(*(lines - i)))
+                               diff_line = (lines - i)->data;
+                       if (!index_line && linestart(*(lines - i), "Index:") && 
!containsnul(*(lines - i)))
+                               index_line = (lines - i)->data;
                }
                old_garbage = garbage_since_last_patch;
                garbage_since_last_patch = 0;
@@ -981,8 +983,10 @@ save_file_cpp(FILE *f, struct file_data *file)
                        }
                        annot = '-';
                }
+
                if (i == file->n)
                        break;
+
                if (annot == '-')
                        fprintf(f, "%s\n", file->d[i].new ? "#else" : "#endif");
                else if (annot == ' ' && file->d[i].new)
@@ -1012,10 +1016,19 @@ save_file(FILE *f, struct file_data *file)
 static void
 save_rejected_line(FILE *f, int annot, int copied, struct line *line)
 {
-       fprintf(f, "%c%s",
-               copied ? (strchr("<>", annot) ? '!' : annot)
-                      : (annot == '<' ? '-' : annot == '>' ? '+' : annot),
-               copied ? " " : "");
+       if (copied) {
+               if (strchr("<>", annot))
+                       fprintf(f, "! ");
+               else
+                       fprintf(f, "%c ", annot);
+       } else {
+               if (annot == '<')
+                       fprintf(f, "-");
+               else if (annot == '>')
+                       fprintf(f, "+");
+               else
+                       fprintf(f, "%c", annot);
+       }
        fwriteline(f, *line);
        if (line->len && line->data[line->len - 1] != '\n')
                fprintf(f, "\n%s\n", NO_LF_MARK);
@@ -1151,7 +1164,7 @@ parse_hunk_normal(struct hunk *hunk, struct parsed_hunk 
*parsed)
                        parsed->annot[j++] = action == 'c' ? '>' : '+';
                }
        }
-       parsed->annot[j] = 0;
+       parsed->annot[j] = '\0';
 }
 
 static void
@@ -1178,24 +1191,19 @@ parse_hunk_copied(struct hunk *hunk, struct parsed_hunk 
*parsed)
        new->start = strtoul(p, &p, 10);
        new->len = 0;
 
-       if (old->len) {
-               for (; i < hunk->nlines; i++)
-                       subline(new->lines + new->len++, hunk->lines + i, 2);
-       } else {
-               for (; i < hunk->nlines; i++) {
-                       subline(new->lines + new->len++, hunk->lines + i, 2);
-                       if (hunk->lines[i].data[0] != '+')
-                               subline(old->lines + old->len++, hunk->lines + 
i, 2);
-               }
+       for (; i < hunk->nlines; i++) {
+               subline(new->lines + new->len++, hunk->lines + i, 2);
+               if (!old->len && hunk->lines[i].data[0] != '+')
+                       subline(old->lines + old->len++, hunk->lines + i, 2);
        }
 
        if (!new->len)
                for (i = 0; i < old->len; i++)
-                       if (old->lines[i].data[-2] != '-')
+                       if (*(old->lines[i].data - 2) != '-')
                                new->lines[new->len++] = old->lines[i];
 
-#define OLDLINE  a < old->len && old->lines[a].data[-2]
-#define NEWLINE  b < new->len && new->lines[b].data[-2]
+#define OLDLINE  a < old->len && *(old->lines[a].data - 2)
+#define NEWLINE  b < new->len && *(new->lines[b].data - 2)
 
        for (i = a = b = 0; a < old->len || b < new->len;) {
                if (OLDLINE == '-')  parsed->annot[i++] = '-', a++;
@@ -1205,7 +1213,7 @@ parse_hunk_copied(struct hunk *hunk, struct parsed_hunk 
*parsed)
                while (OLDLINE == '!')  parsed->annot[i++] = '<', a++;
                while (NEWLINE == '!')  parsed->annot[i++] = '>', b++;
        }
-       parsed->annot[i] = 0;
+       parsed->annot[i] = '\0';
 }
 
 static void
@@ -1237,7 +1245,7 @@ parse_hunk_unified(struct hunk *hunk, struct parsed_hunk 
*parsed)
 
        for (i = 0; i < hunk->nlines; i++)
                parsed->annot[i] = hunk->lines[i].data[0];
-       parsed->annot[i] = 0;
+       parsed->annot[i] = '\0';
        for (;;) {
                p = strstr(parsed->annot, "-+");
                q = strstr(parsed->annot, "+-");
@@ -1245,7 +1253,7 @@ parse_hunk_unified(struct hunk *hunk, struct parsed_hunk 
*parsed)
                        break;
                if (!p || (q && p > q))
                        p = q;
-               for (; p != parsed->annot && strchr("-+", p[-1]); p--);
+               for (; p != parsed->annot && strchr("-+", *(p - 1)); p--);
                for (; *p == '-' || *p == '+'; p++)
                        *p = "<>"[*p == '+'];
        }
@@ -1285,7 +1293,7 @@ parse_hunk(struct hunk *hunk, enum format format, struct 
parsed_hunk *parsed)
                default:   parsed->rannot[i] = ' ';  break;
                }
        }
-       parsed->rannot[n] = 0;
+       parsed->rannot[n] = '\0';
 }
 
 static int
@@ -1497,7 +1505,7 @@ rejected:
 static int
 parse_ed_script(struct patch *patch, struct file_data *file)
 {
-       /* Not relying on ed(1p) lets us implemented -D for
+       /* Not relying on ed(1p) lets us implement -D for
         * ed-scripts too without diff(1p):ing afterwards.
         * This is not significantly more complex than
         * relying on ed(1p) anyways. */
@@ -1600,7 +1608,7 @@ parse_ed_script(struct patch *patch, struct file_data 
*file)
                start = strtoul(hunk->head->data + 4, &p, 10);
                count = strtoul(p + 1, &p, 10);
                p = strchr(q = p + 2, ',') + 1;
-               added = strtoul(p, 0, 10);
+               added = strtoul(p, NULL, 10);
                q += sprintf(q, "%zu", start + offset - !added + !count);
                *q++ = ',';
                memmove(q, p, strlen(p) + 1);
@@ -1624,7 +1632,7 @@ fuzzstr(ssize_t fuzz)
 }
 
 static void
-apply_patch(struct patch *patch, size_t patch_index)
+apply_patch(struct patch *patch)
 {
        struct file_data *file;
        struct parsed_hunk hunk;
@@ -1633,7 +1641,7 @@ apply_patch(struct patch *patch, size_t patch_index)
        int firstrej = 1, edscript = patch->format == ED;
        ssize_t offset = 0, fuzz;
        size_t i;
-       FILE *f = 0;
+       FILE *f = NULL;
 
        if (!rejectfile)
                enasprintf(FAILURE, &rejfile, "%s.rej", path);
@@ -1708,7 +1716,7 @@ apply_patchset(struct patchset *ps)
 {
        size_t i = 0;
        for (i = 0; i < ps->npatches; i++)
-               apply_patch(ps->patches + i, i);
+               apply_patch(ps->patches + i);
        free(ps->patches);
 }
 
@@ -1727,7 +1735,7 @@ main(int argc, char *argv[])
 {
        struct patchset patchset;
        struct file_data patchfile_data;
-       char *p, *Dflag = 0;
+       char *p, *Dflag = NULL;
 
        stdin_dash[0] = stdout_dash[0] = '-';
        stdin_dash[1] = stdout_dash[1] = '\0';
@@ -1825,7 +1833,7 @@ main(int argc, char *argv[])
                        p = ifdef, ifdef = ifndef, ifndef = p;
        }
 
-       load_lines(patchfile, &patchfile_data, 1, 0);
+       load_lines(&patchfile_data, patchfile, 1, 0);
        parse_patchfile(&patchset, get_lines(&patchfile_data));
        ask_for_filename(&patchset);
        apply_patchset(&patchset);
-- 
2.11.1


Reply via email to