=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/patch/patch.c,v retrieving revision 1.22 retrieving revision 1.23 diff -c -r1.22 -r1.23 *** src/usr.bin/patch/patch.c 2003/07/21 21:01:45 1.22 --- src/usr.bin/patch/patch.c 2003/07/22 17:18:49 1.23 *************** *** 1,4 **** ! /* $OpenBSD: patch.c,v 1.22 2003/07/21 21:01:45 otto Exp $ */ /* * patch - a program to apply diffs to original files --- 1,4 ---- ! /* $OpenBSD: patch.c,v 1.23 2003/07/22 17:18:49 otto Exp $ */ /* * patch - a program to apply diffs to original files *************** *** 27,35 **** */ #ifndef lint ! static char rcsid[] = "$OpenBSD: patch.c,v 1.22 2003/07/21 21:01:45 otto Exp $"; #endif /* not lint */ #include "INTERN.h" #include "common.h" #include "EXTERN.h" --- 27,44 ---- */ #ifndef lint ! static const char rcsid[] = "$OpenBSD: patch.c,v 1.23 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ + #include + #include + #include + + #include + #include + #include + #include + #include "INTERN.h" #include "common.h" #include "EXTERN.h" *************** *** 38,68 **** #include "inp.h" #include "backupfile.h" - void reinitialize_almost_everything(void); - void get_some_switches(void); - LINENUM locate_hunk(LINENUM); - void abort_hunk(void); - void apply_hunk(LINENUM); - void init_output(char *); - void init_reject(char *); - void copy_till(LINENUM); - void spew_output(void); - void dump_line(LINENUM); - bool patch_match(LINENUM, LINENUM, LINENUM); - bool similar(char *, char *, int); - void re_input(void); - void my_exit(int) __attribute__((noreturn)); - int optcmp(const void *, const void *); - char decode_long_option(char *); /* TRUE if -E was specified on command line. */ static int remove_empty_files = FALSE; /* TRUE if -R was specified on command line. */ static int reverse_flag_specified = FALSE; ! /* TRUE if -C was specified on command line. */ ! bool check_only = FALSE; /* Apply a set of diffs as appropriate. */ --- 47,76 ---- #include "inp.h" #include "backupfile.h" + static void reinitialize_almost_everything(void); + static void get_some_switches(void); + static LINENUM locate_hunk(LINENUM); + static void abort_hunk(void); + static void apply_hunk(LINENUM); + static void init_output(char *); + static void init_reject(char *); + static void copy_till(LINENUM); + static void spew_output(void); + static void dump_line(LINENUM); + static bool patch_match(LINENUM, LINENUM, LINENUM); + static bool similar(char *, char *, int); + static int optcmp(const void *, const void *); + static char decode_long_option(char *); + /* TRUE if -E was specified on command line. */ static int remove_empty_files = FALSE; /* TRUE if -R was specified on command line. */ static int reverse_flag_specified = FALSE; ! /* buffer for stderr */ ! static char serrbuf[BUFSIZ]; /* Apply a set of diffs as appropriate. */ *************** *** 75,81 **** setbuf(stderr, serrbuf); for (i = 0; i < MAXFILEC; i++) ! filearg[i] = Nullch; myuid = getuid(); --- 83,89 ---- setbuf(stderr, serrbuf); for (i = 0; i < MAXFILEC; i++) ! filearg[i] = NULL; myuid = getuid(); *************** *** 131,137 **** patch_seen = TRUE; ! if (outname == Nullch) outname = savestr(filearg[0]); /* for ed script just up and do it and exit */ --- 139,145 ---- patch_seen = TRUE; ! if (outname == NULL) outname = savestr(filearg[0]); /* for ed script just up and do it and exit */ *************** *** 159,182 **** out_of_mem = FALSE; while (another_hunk()) { hunk++; ! fuzz = Nulline; mymaxfuzz = pch_context(); if (maxfuzz < mymaxfuzz) mymaxfuzz = maxfuzz; if (!skip_rest_of_patch) { do { where = locate_hunk(fuzz); ! if (hunk == 1 && where == Nulline && !force) { /* dwim for reversed patch? */ if (!pch_swap()) { ! if (fuzz == Nulline) say("Not enough memory to try swapped hunk! Assuming unswapped.\n"); continue; } reverse = !reverse; /* try again */ where = locate_hunk(fuzz); ! if (where == Nulline) { /* didn't find it swapped */ if (!pch_swap()) /* put it back to normal */ --- 167,190 ---- out_of_mem = FALSE; while (another_hunk()) { hunk++; ! fuzz = NULL; mymaxfuzz = pch_context(); if (maxfuzz < mymaxfuzz) mymaxfuzz = maxfuzz; if (!skip_rest_of_patch) { do { where = locate_hunk(fuzz); ! if (hunk == 1 && where == NULL && !force) { /* dwim for reversed patch? */ if (!pch_swap()) { ! if (fuzz == NULL) say("Not enough memory to try swapped hunk! Assuming unswapped.\n"); continue; } reverse = !reverse; /* try again */ where = locate_hunk(fuzz); ! if (where == NULL) { /* didn't find it swapped */ if (!pch_swap()) /* put it back to normal */ *************** *** 202,208 **** ask("Apply anyway? [n] "); if (*buf != 'y') skip_rest_of_patch = TRUE; ! where = Nulline; reverse = !reverse; if (!pch_swap()) /* put it back to normal */ --- 210,216 ---- ask("Apply anyway? [n] "); if (*buf != 'y') skip_rest_of_patch = TRUE; ! where = NULL; reverse = !reverse; if (!pch_swap()) /* put it back to normal */ *************** *** 210,221 **** } } } ! } while (!skip_rest_of_patch && where == Nulline && ++fuzz <= mymaxfuzz); if (skip_rest_of_patch) { /* just got decided */ fclose(ofp); ! ofp = Nullfp; } } newwhere = pch_newfirst() + last_offset; --- 218,229 ---- } } } ! } while (!skip_rest_of_patch && where == NULL && ++fuzz <= mymaxfuzz); if (skip_rest_of_patch) { /* just got decided */ fclose(ofp); ! ofp = NULL; } } newwhere = pch_newfirst() + last_offset; *************** *** 225,231 **** if (verbose) say("Hunk #%d ignored at %ld.\n", hunk, newwhere); ! } else if (where == Nulline) { abort_hunk(); failed++; if (verbose) --- 233,239 ---- if (verbose) say("Hunk #%d ignored at %ld.\n", hunk, newwhere); ! } else if (where == NULL) { abort_hunk(); failed++; if (verbose) *************** *** 253,262 **** say("\n\nRan out of memory using Plan A--trying again...\n\n"); if (ofp) fclose(ofp); ! ofp = Nullfp; if (rejfp) fclose(rejfp); ! rejfp = Nullfp; continue; } assert(hunk); --- 261,270 ---- say("\n\nRan out of memory using Plan A--trying again...\n\n"); if (ofp) fclose(ofp); ! ofp = NULL; if (rejfp) fclose(rejfp); ! rejfp = NULL; continue; } assert(hunk); *************** *** 290,296 **** } } fclose(rejfp); ! rejfp = Nullfp; if (failed) { failtotal += failed; if (!*rejname) { --- 298,304 ---- } } fclose(rejfp); ! rejfp = NULL; if (failed) { failtotal += failed; if (!*rejname) { *************** *** 321,327 **** /* Prepare to find the next patch to do in the patch file. */ ! void reinitialize_almost_everything(void) { re_patch(); --- 329,335 ---- /* Prepare to find the next patch to do in the patch file. */ ! static void reinitialize_almost_everything(void) { re_patch(); *************** *** 331,351 **** last_frozen_line = 0; filec = 0; ! if (filearg[0] != Nullch && !out_of_mem) { free(filearg[0]); ! filearg[0] = Nullch; } ! if (outname != Nullch) { free(outname); ! outname = Nullch; } last_offset = 0; diff_type = 0; ! if (revision != Nullch) { free(revision); ! revision = Nullch; } reverse = reverse_flag_specified; skip_rest_of_patch = FALSE; --- 339,359 ---- last_frozen_line = 0; filec = 0; ! if (filearg[0] != NULL && !out_of_mem) { free(filearg[0]); ! filearg[0] = NULL; } ! if (outname != NULL) { free(outname); ! outname = NULL; } last_offset = 0; diff_type = 0; ! if (revision != NULL) { free(revision); ! revision = NULL; } reverse = reverse_flag_specified; skip_rest_of_patch = FALSE; *************** *** 371,377 **** char short_opt; }; ! int optcmp(const void *v1, const void *v2) { const struct option *a = v1, *b = v2; --- 379,385 ---- char short_opt; }; ! static int optcmp(const void *v1, const void *v2) { const struct option *a = v1, *b = v2; *************** *** 381,387 **** /* Decode Long options beginning with "--" to their short equivalents. */ ! char decode_long_option(char *opt) { /* --- 389,395 ---- /* Decode Long options beginning with "--" to their short equivalents. */ ! static char decode_long_option(char *opt) { /* *************** *** 426,432 **** /* Process switches and filenames up to next '+' or end of list. */ ! void get_some_switches(void) { char *s; --- 434,440 ---- /* Process switches and filenames up to next '+' or end of list. */ ! static void get_some_switches(void) { char *s; *************** *** 572,578 **** /* * Attempt to find the right place to apply this hunk of patch. */ ! LINENUM locate_hunk(LINENUM fuzz) { LINENUM first_guess = pch_first() + last_offset; --- 580,586 ---- /* * Attempt to find the right place to apply this hunk of patch. */ ! static LINENUM locate_hunk(LINENUM fuzz) { LINENUM first_guess = pch_first() + last_offset; *************** *** 585,591 **** return first_guess; if (max_neg_offset >= first_guess) /* do not try lines < 0 */ max_neg_offset = first_guess - 1; ! if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz)) return first_guess; for (offset = 1; ; offset++) { bool check_after = (offset <= max_pos_offset); --- 593,599 ---- return first_guess; if (max_neg_offset >= first_guess) /* do not try lines < 0 */ max_neg_offset = first_guess - 1; ! if (first_guess <= input_lines && patch_match(first_guess, NULL, fuzz)) return first_guess; for (offset = 1; ; offset++) { bool check_after = (offset <= max_pos_offset); *************** *** 608,620 **** last_offset = -offset; return first_guess - offset; } else if (!check_before && !check_after) ! return Nulline; } } /* We did not find the pattern, dump out the hunk so they can handle it. */ ! void abort_hunk(void) { LINENUM i; --- 616,628 ---- last_offset = -offset; return first_guess - offset; } else if (!check_before && !check_after) ! return NULL; } } /* We did not find the pattern, dump out the hunk so they can handle it. */ ! static void abort_hunk(void) { LINENUM i; *************** *** 668,674 **** /* We found where to apply it (we hope), so do it. */ ! void apply_hunk(LINENUM where) { LINENUM old = 1; --- 676,682 ---- /* We found where to apply it (we hope), so do it. */ ! static void apply_hunk(LINENUM where) { LINENUM old = 1; *************** *** 780,808 **** /* * Open the new file. */ ! void init_output(char *name) { ofp = fopen(name, "w"); ! if (ofp == Nullfp) pfatal("can't create %s", name); } /* * Open a file to put hunks we can't locate. */ ! void init_reject(char *name) { rejfp = fopen(name, "w"); ! if (rejfp == Nullfp) pfatal("can't create %s", name); } /* * Copy input file to output, up to wherever hunk is to be applied. */ ! void copy_till(LINENUM lastline) { LINENUM R_last_frozen_line = last_frozen_line; --- 788,816 ---- /* * Open the new file. */ ! static void init_output(char *name) { ofp = fopen(name, "w"); ! if (ofp == NULL) pfatal("can't create %s", name); } /* * Open a file to put hunks we can't locate. */ ! static void init_reject(char *name) { rejfp = fopen(name, "w"); ! if (rejfp == NULL) pfatal("can't create %s", name); } /* * Copy input file to output, up to wherever hunk is to be applied. */ ! static void copy_till(LINENUM lastline) { LINENUM R_last_frozen_line = last_frozen_line; *************** *** 817,823 **** /* * Finish copying the input file to the output file. */ ! void spew_output(void) { #ifdef DEBUGGING --- 825,831 ---- /* * Finish copying the input file to the output file. */ ! static void spew_output(void) { #ifdef DEBUGGING *************** *** 827,839 **** if (input_lines) copy_till(input_lines); /* dump remainder of file */ fclose(ofp); ! ofp = Nullfp; } /* * Copy one line from input to output. */ ! void dump_line(LINENUM line) { char *s, R_newline = '\n'; --- 835,847 ---- if (input_lines) copy_till(input_lines); /* dump remainder of file */ fclose(ofp); ! ofp = NULL; } /* * Copy one line from input to output. */ ! static void dump_line(LINENUM line) { char *s, R_newline = '\n'; *************** *** 846,852 **** /* * Does the patch pattern match at line base+offset? */ ! bool patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) { LINENUM pline = 1 + fuzz; --- 854,860 ---- /* * Does the patch pattern match at line base+offset? */ ! static bool patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) { LINENUM pline = 1 + fuzz; *************** *** 868,874 **** /* * Do two lines match with canonicalized white space? */ ! bool similar(char *a, char *b, int len) { while (len) { --- 876,882 ---- /* * Do two lines match with canonicalized white space? */ ! static bool similar(char *a, char *b, int len) { while (len) { *************** *** 888,906 **** } return TRUE; /* actually, this is not reached */ /* since there is always a \n */ - } - - /* - * Exit with cleanup. - */ - void - my_exit(int status) - { - unlink(TMPINNAME); - if (!toutkeep) - unlink(TMPOUTNAME); - if (!trejkeep) - unlink(TMPREJNAME); - unlink(TMPPATNAME); - exit(status); } --- 896,899 ----