diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/patch/backupfile.c | 30 | ||||
-rw-r--r-- | usr.bin/patch/common.h | 42 | ||||
-rw-r--r-- | usr.bin/patch/config.h | 33 | ||||
-rw-r--r-- | usr.bin/patch/inp.c | 88 | ||||
-rw-r--r-- | usr.bin/patch/inp.h | 7 | ||||
-rw-r--r-- | usr.bin/patch/patch.c | 139 | ||||
-rw-r--r-- | usr.bin/patch/pch.c | 210 | ||||
-rw-r--r-- | usr.bin/patch/pch.h | 9 | ||||
-rw-r--r-- | usr.bin/patch/util.c | 47 | ||||
-rw-r--r-- | usr.bin/patch/util.h | 16 |
10 files changed, 296 insertions, 325 deletions
diff --git a/usr.bin/patch/backupfile.c b/usr.bin/patch/backupfile.c index d418549d78d..0c925d2a555 100644 --- a/usr.bin/patch/backupfile.c +++ b/usr.bin/patch/backupfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: backupfile.c,v 1.12 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: backupfile.c,v 1.13 2003/07/22 17:18:49 otto Exp $ */ /* * backupfile.c -- make Emacs style backup file names Copyright (C) 1990 Free @@ -17,17 +17,22 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: backupfile.c,v 1.12 2003/07/21 14:32:21 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: backupfile.c,v 1.13 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ +#include <ctype.h> +#include <libgen.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <ctype.h> -#include <libgen.h> -#include <sys/types.h> + #include "backupfile.h" -#include "config.h" + +/* + * DIRHEADER: This definition indicates which directory library header to + * use. + */ +#define DIRENT #ifdef DIRENT #include <dirent.h> @@ -64,7 +69,6 @@ enum backup_type backup_type = none; char *simple_backup_suffix = "~"; static char *concat(char *, char *); -char *find_backup_file_name(char *); static char *make_version_name(char *, int); static int max_backup_version(char *, char *); static int version_number(char *, char *, int); @@ -86,11 +90,11 @@ find_backup_file_name(char *file) return concat(file, simple_backup_suffix); base_versions = concat(basename(file), ".~"); if (base_versions == 0) - return 0; + return NULL; dir = dirname(file); if (dir == 0) { free(base_versions); - return 0; + return NULL; } highest_backup = max_backup_version(base_versions, dir); free(base_versions); @@ -141,7 +145,7 @@ make_version_name(char *file, int version) char *backup_name; if (asprintf(&backup_name, "%s.~%d~", file, version) == -1) - return 0; + return NULL; return backup_name; } @@ -176,7 +180,7 @@ concat(char *str1, char *str2) char *newstr; if (asprintf(&newstr, "%s%s", str1, str2) == -1) - return 0; + return NULL; return newstr; } @@ -186,7 +190,7 @@ concat(char *str1, char *str2) * does not match any element or -2 if it is ambiguous (is a prefix of more * than one element). */ -int +static int argmatch(char *arg, char **optlist) { int i; /* Temporary index in OPTLIST. */ @@ -221,7 +225,7 @@ argmatch(char *arg, char **optlist) * that was being matched. VALUE is the invalid value that was given. PROBLEM * is the return value from argmatch. */ -void +static void invalid_arg(char *kind, char *value, int problem) { fprintf(stderr, "patch: "); diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index 24ac2d4b090..82f4b9ffeaf 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,26 +1,8 @@ -/* $OpenBSD: common.h,v 1.16 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: common.h,v 1.17 2003/07/22 17:18:49 otto Exp $ */ #define DEBUGGING -#include "config.h" - -/* shut lint up about the following when return value ignored */ - #include <stdio.h> -#include <string.h> -#include <assert.h> -#include <sys/types.h> -#include <sys/file.h> -#include <sys/stat.h> -#include <sys/param.h> -#include <ctype.h> -#include <paths.h> -#include <signal.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdarg.h> -#include <libgen.h> -#include <errno.h> /* constants */ @@ -45,13 +27,6 @@ /* handy definitions */ -#define Null(t) ((t)0) -#define Nullch Null(char *) -#define Nullfp Null(FILE *) -#define Nulline Null(LINENUM) - -#define Ctl(ch) ((ch) & 037) - #define strNE(s1,s2) (strcmp(s1, s2)) #define strEQ(s1,s2) (!strcmp(s1, s2)) #define strnNE(s1,s2,l) (strncmp(s1, s2, l)) @@ -73,8 +48,8 @@ EXT struct stat filestat; /* file statistics area */ EXT int filemode INIT(0644); EXT char buf[MAXLINELEN];/* general purpose buffer */ -EXT FILE *ofp INIT(Nullfp); /* output file pointer */ -EXT FILE *rejfp INIT(Nullfp); /* reject file pointer */ +EXT FILE *ofp INIT(NULL); /* output file pointer */ +EXT FILE *rejfp INIT(NULL); /* reject file pointer */ EXT int myuid; /* cache getuid return value */ @@ -85,12 +60,12 @@ EXT bool out_of_mem INIT(FALSE);/* ran out of memory in plan a */ EXT int filec INIT(0); /* how many file arguments? */ EXT char *filearg[MAXFILEC]; EXT bool ok_to_create_file INIT(FALSE); -EXT char *bestguess INIT(Nullch); /* guess at correct filename */ +EXT char *bestguess INIT(NULL); /* guess at correct filename */ -EXT char *outname INIT(Nullch); +EXT char *outname INIT(NULL); EXT char rejname[128]; -EXT char *origprae INIT(Nullch); +EXT char *origprae INIT(NULL); EXT char *TMPOUTNAME; EXT char *TMPINNAME; @@ -112,6 +87,9 @@ EXT bool noreverse INIT(FALSE); EXT bool skip_rest_of_patch INIT(FALSE); EXT int strippath INIT(957); EXT bool canonicalize INIT(FALSE); +/* TRUE if -C was specified on command line. */ +EXT bool check_only INIT(FALSE); + #define CONTEXT_DIFF 1 #define NORMAL_DIFF 2 @@ -126,4 +104,4 @@ EXT char not_defined[128]; /* #ifndef xyzzy */ EXT char else_defined[] INIT("#else\n"); /* #else */ EXT char end_defined[128]; /* #endif xyzzy */ -EXT char *revision INIT(Nullch); /* prerequisite revision, if any */ +EXT char *revision INIT(NULL); /* prerequisite revision, if any */ diff --git a/usr.bin/patch/config.h b/usr.bin/patch/config.h deleted file mode 100644 index 7b1f7b5e86e..00000000000 --- a/usr.bin/patch/config.h +++ /dev/null @@ -1,33 +0,0 @@ -/* $OpenBSD: config.h,v 1.6 2003/07/21 14:32:21 deraadt Exp $ */ - -/* - * config.h This file was produced by running the config.h.SH script, which - * gets its values from config.sh, which is generally produced by running - * Configure. - * - * Feel free to modify any of this as the need arises. Note, however, that - * running config.h.SH again will wipe out any changes you've made. For a - * more permanent change edit config.sh and rerun config.h.SH. - * - */ - - -/* - * CPPSTDIN: This symbol contains the first part of the string which will - * invoke the C preprocessor on the standard input and produce to standard - * output. Typical value of "cc -E" or "/lib/cpp". - */ -/* - * CPPMINUS: This symbol contains the second part of the string which will - * invoke the C preprocessor on the standard input and produce to standard - * output. This symbol will have the value "-" if CPPSTDIN needs a minus to - * specify standard input, otherwise the value is "". - */ -#define CPPSTDIN "/usr/bin/cpp" -#define CPPMINUS "" - -/* - * DIRHEADER: This definition indicates which directory library header to - * use. - */ -#define DIRENT diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index 9928ab6ed2e..f89c924ab9a 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,9 +1,20 @@ -/* $OpenBSD: inp.c,v 1.15 2003/07/21 21:01:45 otto Exp $ */ +/* $OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: inp.c,v 1.15 2003/07/21 21:01:45 otto Exp $"; +static const char rcsid[] = "$OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ +#include <sys/types.h> +#include <sys/file.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <libgen.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + #include "EXTERN.h" #include "common.h" #include "util.h" @@ -11,7 +22,6 @@ static char rcsid[] = "$OpenBSD: inp.c,v 1.15 2003/07/21 21:01:45 otto Exp $ #include "INTERN.h" #include "inp.h" -extern bool check_only; /* Input-file-with-indexable-lines abstract type */ @@ -25,7 +35,9 @@ static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */ static LINENUM lines_per_buf; /* how many lines per buffer */ static int tireclen; /* length of records in tmp file */ -void re_input(void); +static bool rev_in_string(char *); +static bool plan_a(char *); /* returns false if insufficient memory */ +static void plan_b(char *); /* New patch--prepare to edit another file. */ @@ -34,21 +46,17 @@ re_input(void) { if (using_plan_a) { i_size = 0; -#ifndef lint - if (i_ptr != Null(char **)) - free((char *) i_ptr); -#endif - if (i_womp != Nullch) - free(i_womp); - i_womp = Nullch; - i_ptr = Null(char **); + free(i_ptr); + free(i_womp); + i_womp = NULL; + i_ptr = NULL; } else { using_plan_a = TRUE; /* maybe the next one is smaller */ close(tifd); tifd = -1; free(tibuf[0]); free(tibuf[1]); - tibuf[0] = tibuf[1] = Nullch; + tibuf[0] = tibuf[1] = NULL; tiline[0] = tiline[1] = -1; tireclen = 0; } @@ -69,7 +77,7 @@ scan_input(char *filename) /* Try keeping everything in memory. */ -bool +static bool plan_a(char *filename) { int ifd, statfailed; @@ -103,7 +111,7 @@ plan_a(char *filename) (filestat.st_mode & 0222) == 0 || /* I can't write to it. */ ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { - char *cs = Nullch, *filebase, *filedir; + char *cs = NULL, *filebase, *filedir; struct stat cstat; filebase = basename(filename); @@ -170,18 +178,20 @@ plan_a(char *filename) out_of_mem = FALSE; return FALSE; /* force plan b because plan a bombed */ } - i_womp = malloc(i_size + 2); - if (i_womp == Nullch) + if (i_size > SIZE_MAX - 2) + fatal("block too large to allocate"); + i_womp = malloc((size_t)(i_size + 2)); + if (i_womp == NULL) return FALSE; if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); -#ifndef lint + if (read(ifd, i_womp, (size_t) i_size) != i_size) { close(ifd); /* probably means i_size > 15 or 16 bits worth */ free(i_womp); /* at this point it doesn't matter if i_womp was */ return FALSE; /* undersized. */ } -#endif + close(ifd); if (i_size && i_womp[i_size - 1] != '\n') i_womp[i_size++] = '\n'; @@ -194,12 +204,10 @@ plan_a(char *filename) if (*s == '\n') iline++; } -#ifdef lint - i_ptr = Null(char **); -#else + i_ptr = (char **) malloc((iline + 2) * sizeof(char *)); -#endif - if (i_ptr == Null(char **)) { /* shucks, it was a near thing */ + + if (i_ptr == NULL) { /* shucks, it was a near thing */ free((char *) i_womp); return FALSE; } @@ -216,7 +224,7 @@ plan_a(char *filename) /* now check for revision, if any */ - if (revision != Nullch) { + if (revision != NULL) { if (!rev_in_string(i_womp)) { if (force) { if (verbose) @@ -243,26 +251,26 @@ plan_a(char *filename) /* Keep (virtually) nothing in memory. */ -void +static void plan_b(char *filename) { FILE *ifp; int i = 0, maxlen = 1; - bool found_revision = (revision == Nullch); + bool found_revision = (revision == NULL); using_plan_a = FALSE; - if ((ifp = fopen(filename, "r")) == Nullfp) + if ((ifp = fopen(filename, "r")) == NULL) pfatal("can't open file %s", filename); (void) unlink(TMPINNAME); if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) pfatal("can't open file %s", TMPINNAME); - while (fgets(buf, sizeof buf, ifp) != Nullch) { - if (revision != Nullch && !found_revision && rev_in_string(buf)) + while (fgets(buf, sizeof buf, ifp) != NULL) { + if (revision != NULL && !found_revision && rev_in_string(buf)) found_revision = TRUE; if ((i = strlen(buf)) > maxlen) maxlen = i; /* find longest line */ } - if (revision != Nullch) { + if (revision != NULL) { if (!found_revision) { if (force) { if (verbose) @@ -284,21 +292,21 @@ plan_b(char *filename) say("Good. This file appears to be the %s version.\n", revision); } - fseek(ifp, 0L, 0); /* rewind file */ + fseek(ifp, 0L, SEEK_SET); /* rewind file */ lines_per_buf = BUFFERSIZE / maxlen; tireclen = maxlen; tibuf[0] = malloc(BUFFERSIZE + 1); - if (tibuf[0] == Nullch) + if (tibuf[0] == NULL) fatal("out of memory\n"); tibuf[1] = malloc(BUFFERSIZE + 1); - if (tibuf[1] == Nullch) + if (tibuf[1] == NULL) fatal("out of memory\n"); for (i = 1;; i++) { if (!(i % lines_per_buf)) /* new block */ if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) pfatal("can't write temp file"); if (fgets(tibuf[0] + maxlen * (i % lines_per_buf), - maxlen + 1, ifp) == Nullch) { + maxlen + 1, ifp) == NULL) { input_lines = i - 1; if (i % lines_per_buf) if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) @@ -334,10 +342,10 @@ ifetch(LINENUM line, int whichbuf) whichbuf = 1; else { tiline[whichbuf] = baseline; -#ifndef lint /* complains of long accuracy */ + lseek(tifd, (off_t) (baseline / lines_per_buf * - BUFFERSIZE), 0); -#endif + BUFFERSIZE), SEEK_SET); + if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) pfatal("error reading tmp file %s", TMPINNAME); } @@ -348,13 +356,13 @@ ifetch(LINENUM line, int whichbuf) /* * True if the string argument contains the revision number we want. */ -bool +static bool rev_in_string(char *string) { char *s; int patlen; - if (revision == Nullch) + if (revision == NULL) return TRUE; patlen = strlen(revision); if (strnEQ(string, revision, patlen) && isspace(string[patlen])) diff --git a/usr.bin/patch/inp.h b/usr.bin/patch/inp.h index 81670d852ea..fc453297cbd 100644 --- a/usr.bin/patch/inp.h +++ b/usr.bin/patch/inp.h @@ -1,11 +1,10 @@ -/* $OpenBSD: inp.h,v 1.5 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: inp.h,v 1.6 2003/07/22 17:18:49 otto Exp $ */ EXT LINENUM input_lines INIT(0); /* how long is input file in lines */ EXT LINENUM last_frozen_line INIT(0); /* how many input lines have been */ /* irretractibly output */ -bool rev_in_string(char *); + +void re_input(void); void scan_input(char *); -bool plan_a(char *); /* returns false if insufficient memory */ -void plan_b(char *); char *ifetch(LINENUM, int); diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index d3a10bcb36c..348399f2c21 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: patch.c,v 1.22 2003/07/21 21:01:45 otto Exp $ */ +/* $OpenBSD: patch.c,v 1.23 2003/07/22 17:18:49 otto Exp $ */ /* * patch - a program to apply diffs to original files @@ -27,9 +27,18 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: patch.c,v 1.22 2003/07/21 21:01:45 otto Exp $"; +static const char rcsid[] = "$OpenBSD: patch.c,v 1.23 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <assert.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> + #include "INTERN.h" #include "common.h" #include "EXTERN.h" @@ -38,22 +47,21 @@ static char rcsid[] = "$OpenBSD: patch.c,v 1.22 2003/07/21 21:01:45 otto Exp $"; #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 *); + +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; @@ -61,8 +69,8 @@ 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; + /* buffer for stderr */ +static char serrbuf[BUFSIZ]; /* Apply a set of diffs as appropriate. */ @@ -75,7 +83,7 @@ main(int argc, char *argv[]) setbuf(stderr, serrbuf); for (i = 0; i < MAXFILEC; i++) - filearg[i] = Nullch; + filearg[i] = NULL; myuid = getuid(); @@ -131,7 +139,7 @@ main(int argc, char *argv[]) patch_seen = TRUE; - if (outname == Nullch) + if (outname == NULL) outname = savestr(filearg[0]); /* for ed script just up and do it and exit */ @@ -159,24 +167,24 @@ main(int argc, char *argv[]) out_of_mem = FALSE; while (another_hunk()) { hunk++; - fuzz = Nulline; + fuzz = NULL; mymaxfuzz = pch_context(); if (maxfuzz < mymaxfuzz) mymaxfuzz = maxfuzz; if (!skip_rest_of_patch) { do { where = locate_hunk(fuzz); - if (hunk == 1 && where == Nulline && !force) { + if (hunk == 1 && where == NULL && !force) { /* dwim for reversed patch? */ if (!pch_swap()) { - if (fuzz == Nulline) + 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 == Nulline) { + if (where == NULL) { /* didn't find it swapped */ if (!pch_swap()) /* put it back to normal */ @@ -202,7 +210,7 @@ main(int argc, char *argv[]) ask("Apply anyway? [n] "); if (*buf != 'y') skip_rest_of_patch = TRUE; - where = Nulline; + where = NULL; reverse = !reverse; if (!pch_swap()) /* put it back to normal */ @@ -210,12 +218,12 @@ main(int argc, char *argv[]) } } } - } while (!skip_rest_of_patch && where == Nulline && + } while (!skip_rest_of_patch && where == NULL && ++fuzz <= mymaxfuzz); if (skip_rest_of_patch) { /* just got decided */ fclose(ofp); - ofp = Nullfp; + ofp = NULL; } } newwhere = pch_newfirst() + last_offset; @@ -225,7 +233,7 @@ main(int argc, char *argv[]) if (verbose) say("Hunk #%d ignored at %ld.\n", hunk, newwhere); - } else if (where == Nulline) { + } else if (where == NULL) { abort_hunk(); failed++; if (verbose) @@ -253,10 +261,10 @@ main(int argc, char *argv[]) say("\n\nRan out of memory using Plan A--trying again...\n\n"); if (ofp) fclose(ofp); - ofp = Nullfp; + ofp = NULL; if (rejfp) fclose(rejfp); - rejfp = Nullfp; + rejfp = NULL; continue; } assert(hunk); @@ -290,7 +298,7 @@ main(int argc, char *argv[]) } } fclose(rejfp); - rejfp = Nullfp; + rejfp = NULL; if (failed) { failtotal += failed; if (!*rejname) { @@ -321,7 +329,7 @@ main(int argc, char *argv[]) /* Prepare to find the next patch to do in the patch file. */ -void +static void reinitialize_almost_everything(void) { re_patch(); @@ -331,21 +339,21 @@ reinitialize_almost_everything(void) last_frozen_line = 0; filec = 0; - if (filearg[0] != Nullch && !out_of_mem) { + if (filearg[0] != NULL && !out_of_mem) { free(filearg[0]); - filearg[0] = Nullch; + filearg[0] = NULL; } - if (outname != Nullch) { + if (outname != NULL) { free(outname); - outname = Nullch; + outname = NULL; } last_offset = 0; diff_type = 0; - if (revision != Nullch) { + if (revision != NULL) { free(revision); - revision = Nullch; + revision = NULL; } reverse = reverse_flag_specified; skip_rest_of_patch = FALSE; @@ -371,7 +379,7 @@ struct option { char short_opt; }; -int +static int optcmp(const void *v1, const void *v2) { const struct option *a = v1, *b = v2; @@ -381,7 +389,7 @@ optcmp(const void *v1, const void *v2) /* Decode Long options beginning with "--" to their short equivalents. */ -char +static char decode_long_option(char *opt) { /* @@ -426,7 +434,7 @@ decode_long_option(char *opt) /* Process switches and filenames up to next '+' or end of list. */ -void +static void get_some_switches(void) { char *s; @@ -572,7 +580,7 @@ Options:\n\ /* * Attempt to find the right place to apply this hunk of patch. */ -LINENUM +static LINENUM locate_hunk(LINENUM fuzz) { LINENUM first_guess = pch_first() + last_offset; @@ -585,7 +593,7 @@ locate_hunk(LINENUM fuzz) 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)) + 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,13 +616,13 @@ locate_hunk(LINENUM fuzz) last_offset = -offset; return first_guess - offset; } else if (!check_before && !check_after) - return Nulline; + return NULL; } } /* We did not find the pattern, dump out the hunk so they can handle it. */ -void +static void abort_hunk(void) { LINENUM i; @@ -668,7 +676,7 @@ abort_hunk(void) /* We found where to apply it (we hope), so do it. */ -void +static void apply_hunk(LINENUM where) { LINENUM old = 1; @@ -780,29 +788,29 @@ apply_hunk(LINENUM where) /* * Open the new file. */ -void +static void init_output(char *name) { ofp = fopen(name, "w"); - if (ofp == Nullfp) + if (ofp == NULL) pfatal("can't create %s", name); } /* * Open a file to put hunks we can't locate. */ -void +static void init_reject(char *name) { rejfp = fopen(name, "w"); - if (rejfp == Nullfp) + if (rejfp == NULL) pfatal("can't create %s", name); } /* * Copy input file to output, up to wherever hunk is to be applied. */ -void +static void copy_till(LINENUM lastline) { LINENUM R_last_frozen_line = last_frozen_line; @@ -817,7 +825,7 @@ copy_till(LINENUM lastline) /* * Finish copying the input file to the output file. */ -void +static void spew_output(void) { #ifdef DEBUGGING @@ -827,13 +835,13 @@ spew_output(void) if (input_lines) copy_till(input_lines); /* dump remainder of file */ fclose(ofp); - ofp = Nullfp; + ofp = NULL; } /* * Copy one line from input to output. */ -void +static void dump_line(LINENUM line) { char *s, R_newline = '\n'; @@ -846,7 +854,7 @@ dump_line(LINENUM line) /* * Does the patch pattern match at line base+offset? */ -bool +static bool patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) { LINENUM pline = 1 + fuzz; @@ -868,7 +876,7 @@ patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) /* * Do two lines match with canonicalized white space? */ -bool +static bool similar(char *a, char *b, int len) { while (len) { @@ -889,18 +897,3 @@ similar(char *a, char *b, int len) 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); -} diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 2a2c8fb7eaf..2761d2089ef 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1,16 +1,24 @@ -/* $OpenBSD: pch.c,v 1.20 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: pch.c,v 1.21 2003/07/22 17:18:49 otto Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: pch.c,v 1.20 2003/07/21 14:32:21 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: pch.c,v 1.21 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ +#include <sys/types.h> +#include <sys/stat.h> + +#include <assert.h> +#include <ctype.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + #include "EXTERN.h" #include "common.h" #include "util.h" #include "INTERN.h" #include "pch.h" -extern bool check_only; /* Patch (diff listing) abstract type. */ static long p_filesize; /* size of the patch file */ @@ -22,9 +30,9 @@ static LINENUM p_end = -1; /* last line in hunk */ static LINENUM p_max; /* max allowed value of p_end */ static LINENUM p_context = 3; /* # of context lines */ static LINENUM p_input_line = 0; /* current line # from patch file */ -static char **p_line = Null(char **); /* the text of the hunk */ -static short *p_len = Null(short *); /* length of each line */ -static char *p_char = Nullch;/* +, -, and ! */ +static char **p_line = NULL; /* the text of the hunk */ +static short *p_len = NULL; /* length of each line */ +static char *p_char = NULL;/* +, -, and ! */ static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */ static int p_indent; /* indent to patch */ static LINENUM p_base; /* where to intuit this time */ @@ -35,18 +43,24 @@ static LINENUM p_hunk_beg; /* line number of current hunk */ static LINENUM p_efake = -1; /* end of faked up lines--don't free */ static LINENUM p_bfake = -1; /* beg of faked up lines */ +static void grow_hunkmax(void); +static int intuit_diff_type(void); +static void next_intuit_at(long, long); +static void skip_to(long, long); +static char *pgets(char *, int, FILE *); + /* * Prepare to look for the next patch in the patch file. */ void re_patch(void) { - p_first = Nulline; - p_newfirst = Nulline; - p_ptrn_lines = Nulline; - p_repl_lines = Nulline; + p_first = NULL; + p_newfirst = NULL; + p_ptrn_lines = NULL; + p_repl_lines = NULL; p_end = (LINENUM) - 1; - p_max = Nulline; + p_max = NULL; p_indent = 0; } @@ -56,17 +70,17 @@ re_patch(void) void open_patch_file(char *filename) { - if (filename == Nullch || !*filename || strEQ(filename, "-")) { + if (filename == NULL || !*filename || strEQ(filename, "-")) { pfp = fopen(TMPPATNAME, "w"); - if (pfp == Nullfp) + if (pfp == NULL) pfatal("can't create %s", TMPPATNAME); - while (fgets(buf, sizeof buf, stdin) != Nullch) + while (fgets(buf, sizeof buf, stdin) != NULL) fputs(buf, pfp); fclose(pfp); filename = TMPPATNAME; } pfp = fopen(filename, "r"); - if (pfp == Nullfp) + if (pfp == NULL) pfatal("patch file %s not found", filename); fstat(fileno(pfp), &filestat); p_filesize = filestat.st_size; @@ -80,20 +94,18 @@ open_patch_file(char *filename) void set_hunkmax(void) { -#ifndef lint - if (p_line == Null(char **)) - p_line = (char **) malloc((size_t) hunkmax * sizeof(char *)); - if (p_len == Null(short *)) - p_len = (short *) malloc((size_t) hunkmax * sizeof(short)); -#endif - if (p_char == Nullch) - p_char = (char *) malloc((size_t) hunkmax * sizeof(char)); + if (p_line == NULL) + p_line = malloc((size_t) hunkmax * sizeof(char *)); + if (p_len == NULL) + p_len = malloc((size_t) hunkmax * sizeof(short)); + if (p_char == NULL) + p_char = malloc((size_t) hunkmax * sizeof(char)); } /* * Enlarge the arrays containing the current hunk of patch. */ -void +static void grow_hunkmax(void) { hunkmax *= 2; @@ -103,13 +115,13 @@ grow_hunkmax(void) * since p_len can move into p_line's old space, and p_char can move into * p_len's old space. Not on PDP-11's however. But it doesn't matter. */ - assert(p_line != Null(char **) &&p_len != Null(short *) &&p_char != Nullch); -#ifndef lint - p_line = (char **) realloc((char *) p_line, hunkmax * sizeof(char *)); - p_len = (short *) realloc((char *) p_len, hunkmax * sizeof(short)); - p_char = (char *) realloc((char *) p_char, hunkmax * sizeof(char)); -#endif - if (p_line != Null(char **) &&p_len != Null(short *) &&p_char != Nullch) + assert(p_line != NULL &&p_len != NULL &&p_char != NULL); + + p_line = realloc(p_line, hunkmax * sizeof(char *)); + p_len = realloc(p_len, hunkmax * sizeof(short)); + p_char = realloc(p_char, hunkmax * sizeof(char)); + + if (p_line != NULL &&p_len != NULL &&p_char != NULL) return; if (!using_plan_a) fatal("out of memory\n"); @@ -150,7 +162,7 @@ there_is_another_patch(void) say("(Patch is indented %d space%s.)\n", p_indent, p_indent == 1 ? "" : "s"); skip_to(p_start, p_sline); - while (filearg[0] == Nullch) { + while (filearg[0] == NULL) { if (force || batch) { say("No file to patch. Skipping...\n"); filearg[0] = savestr(bestguess); @@ -164,7 +176,7 @@ there_is_another_patch(void) bestguess = savestr(buf); filearg[0] = fetchname(buf, 0, FALSE); } - if (filearg[0] == Nullch) { + if (filearg[0] == NULL) { ask("No file found--skip this patch? [n] "); if (*buf != 'y') continue; @@ -180,7 +192,7 @@ there_is_another_patch(void) /* Determine what kind of diff is in the remaining part of the patch file. */ -int +static int intuit_diff_type(void) { long this_line = 0, previous_line; @@ -188,17 +200,17 @@ intuit_diff_type(void) bool last_line_was_command = FALSE, this_is_a_command = FALSE; bool stars_last_line = FALSE, stars_this_line = FALSE; char *s, *t; - char *indtmp = Nullch; - char *oldtmp = Nullch; - char *newtmp = Nullch; - char *indname = Nullch; - char *oldname = Nullch; - char *newname = Nullch; + char *indtmp = NULL; + char *oldtmp = NULL; + char *newtmp = NULL; + char *indname = NULL; + char *oldname = NULL; + char *newname = NULL; int indent, retval; - bool no_filearg = (filearg[0] == Nullch); + bool no_filearg = (filearg[0] == NULL); ok_to_create_file = FALSE; - fseek(pfp, p_base, 0); + fseek(pfp, p_base, SEEK_SET); p_input_line = p_bline - 1; for (;;) { previous_line = this_line; @@ -207,7 +219,7 @@ intuit_diff_type(void) this_line = ftell(pfp); indent = 0; p_input_line++; - if (fgets(buf, sizeof buf, pfp) == Nullch) { + if (fgets(buf, sizeof buf, pfp) == NULL) { if (first_command_line >= 0L) { /* nothing but deletes!? */ p_start = first_command_line; @@ -254,7 +266,7 @@ intuit_diff_type(void) *t = '\0'; if (!*revision) { free(revision); - revision = Nullch; + revision = NULL; } } if ((!diff_type || diff_type == ED_DIFF) && @@ -305,11 +317,11 @@ intuit_diff_type(void) } scan_exit: if (no_filearg) { - if (indtmp != Nullch) + if (indtmp != NULL) indname = fetchname(indtmp, strippath, ok_to_create_file); - if (oldtmp != Nullch) + if (oldtmp != NULL) oldname = fetchname(oldtmp, strippath, ok_to_create_file); - if (newtmp != Nullch) + if (newtmp != NULL) newname = fetchname(newtmp, strippath, ok_to_create_file); if (indname) filearg[0] = savestr(indname); @@ -325,16 +337,16 @@ scan_exit: } if (bestguess) { free(bestguess); - bestguess = Nullch; + bestguess = NULL; } - if (filearg[0] != Nullch) + if (filearg[0] != NULL) bestguess = savestr(filearg[0]); - else if (indtmp != Nullch) + else if (indtmp != NULL) bestguess = fetchname(indtmp, strippath, TRUE); else { - if (oldtmp != Nullch) + if (oldtmp != NULL) oldname = fetchname(oldtmp, strippath, TRUE); - if (newtmp != Nullch) + if (newtmp != NULL) newname = fetchname(newtmp, strippath, TRUE); if (oldname && newname) { if (strlen(oldname) < strlen(newname)) @@ -346,25 +358,19 @@ scan_exit: else if (newname) bestguess = savestr(newname); } - if (indtmp != Nullch) - free(indtmp); - if (oldtmp != Nullch) - free(oldtmp); - if (newtmp != Nullch) - free(newtmp); - if (indname != Nullch) - free(indname); - if (oldname != Nullch) - free(oldname); - if (newname != Nullch) - free(newname); + free(indtmp); + free(oldtmp); + free(newtmp); + free(indname); + free(oldname); + free(newname); return retval; } /* * Remember where this patch ends so we know where to start up again. */ -void +static void next_intuit_at(long file_pos, long file_line) { p_base = file_pos; @@ -374,23 +380,23 @@ next_intuit_at(long file_pos, long file_line) /* * Basically a verbose fseek() to the actual diff listing. */ -void +static void skip_to(long file_pos, long file_line) { char *ret; assert(p_base <= file_pos); if (verbose && p_base < file_pos) { - fseek(pfp, p_base, 0); + fseek(pfp, p_base, SEEK_SET); say("The text leading up to this was:\n--------------------------\n"); while (ftell(pfp) < file_pos) { ret = fgets(buf, sizeof buf, pfp); - assert(ret != Nullch); + assert(ret != NULL); say("|%s", buf); } say("--------------------------\n"); } else - fseek(pfp, file_pos, 0); + fseek(pfp, file_pos, SEEK_SET); p_input_line = file_line - 1; } @@ -468,7 +474,7 @@ another_hunk(void) ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch || strnNE(buf, "********", 8)) { + if (ret == NULL || strnNE(buf, "********", 8)) { next_intuit_at(line_beginning, p_input_line); return FALSE; } @@ -478,7 +484,7 @@ another_hunk(void) line_beginning = ftell(pfp); ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch) { + if (ret == NULL) { if (p_max - p_end < 4) { /* assume blank lines got chopped */ strlcpy(buf, " \n", sizeof buf); @@ -493,7 +499,7 @@ another_hunk(void) p_end++; assert(p_end < hunkmax); p_char[p_end] = *buf; - p_line[p_end] = Nullch; + p_line[p_end] = NULL; switch (*buf) { case '*': if (strnEQ(buf, "********", 8)) { @@ -711,7 +717,7 @@ hunk_done: p_input_line = repl_patch_line; for (p_end--; p_end > repl_beginning; p_end--) free(p_line[p_end]); - fseek(pfp, repl_backtrack_position, 0); + fseek(pfp, repl_backtrack_position, SEEK_SET); /* redundant 'new' context lines were omitted - set */ /* up to fill them in from the old file context */ @@ -792,7 +798,7 @@ hunk_done: ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch || strnNE(buf, "@@ -", 4)) { + if (ret == NULL || strnNE(buf, "@@ -", 4)) { next_intuit_at(line_beginning, p_input_line); return FALSE; } @@ -856,7 +862,7 @@ hunk_done: line_beginning = ftell(pfp); ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch) { + if (ret == NULL) { if (p_max - filldst < 3) { /* assume blank lines got chopped */ strlcpy(buf, " \n", sizeof buf); @@ -954,7 +960,7 @@ hunk_done: p_context = 0; ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch || !isdigit(*buf)) { + if (ret == NULL || !isdigit(*buf)) { next_intuit_at(line_beginning, p_input_line); return FALSE; } @@ -998,7 +1004,7 @@ hunk_done: for (i = 1; i <= p_ptrn_lines; i++) { ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch) + if (ret == NULL) fatal("unexpected end of file in patch at line %ld\n", p_input_line); if (*buf != '<') @@ -1020,7 +1026,7 @@ hunk_done: if (hunk_type == 'c') { ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch) + if (ret == NULL) fatal("unexpected end of file in patch at line %ld\n", p_input_line); if (*buf != '-') @@ -1037,7 +1043,7 @@ hunk_done: for (i++; i <= p_end; i++) { ret = pgets(buf, sizeof buf, pfp); p_input_line++; - if (ret == Nullch) + if (ret == NULL) fatal("unexpected end of file in patch at line %ld\n", p_input_line); if (*buf != '>') @@ -1084,13 +1090,13 @@ hunk_done: /* * Input a line from the patch file, worrying about indentation. */ -char * +static char * pgets(char *bf, int sz, FILE *fp) { char *s, *ret = fgets(bf, sz, fp); int indent = 0; - if (p_indent && ret != Nullch) { + if (p_indent && ret != NULL) { for (s = buf; indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) { @@ -1128,21 +1134,20 @@ pch_swap(void) tp_line = p_line; tp_len = p_len; tp_char = p_char; - p_line = Null(char **); /* force set_hunkmax to allocate again */ - p_len = Null(short *); - p_char = Nullch; + p_line = NULL; /* force set_hunkmax to allocate again */ + p_len = NULL; + p_char = NULL; set_hunkmax(); - if (p_line == Null(char **) ||p_len == Null(short *) ||p_char == Nullch) { -#ifndef lint - if (p_line == Null(char **)) - free((char *) p_line); + if (p_line == NULL ||p_len == NULL ||p_char == NULL) { + + if (p_line == NULL) /* XXX */ + free(p_line); p_line = tp_line; - if (p_len == Null(short *)) - free((char *) p_len); + if (p_len == NULL) /* XXX */ + free(p_len); p_len = tp_len; -#endif - if (p_char == Nullch) - free((char *) p_char); + if (p_char == NULL) /* XXX */ + free(p_char); p_char = tp_char; return FALSE; /* not enough memory to swap hunk! */ } @@ -1199,14 +1204,13 @@ pch_swap(void) i = p_ptrn_lines; p_ptrn_lines = p_repl_lines; p_repl_lines = i; -#ifndef lint - if (tp_line == Null(char **)) - free((char *) tp_line); - if (tp_len == Null(short *)) - free((char *) tp_len); -#endif - if (tp_char == Nullch) - free((char *) tp_char); + + if (tp_line == NULL) /* XXX */ + free(tp_line); + if (tp_len == NULL) /* XXX */ + free(tp_len); + if (tp_char == NULL) /* XXX */ + free(tp_char); return TRUE; } @@ -1322,7 +1326,7 @@ do_ed_script(void) } for (;;) { beginning_of_this_line = ftell(pfp); - if (pgets(buf, sizeof buf, pfp) == Nullch) { + if (pgets(buf, sizeof buf, pfp) == NULL) { next_intuit_at(beginning_of_this_line, p_input_line); break; } @@ -1335,7 +1339,7 @@ do_ed_script(void) if (!skip_rest_of_patch) fputs(buf, pipefp); if (*t != 'd') { - while (pgets(buf, sizeof buf, pfp) != Nullch) { + while (pgets(buf, sizeof buf, pfp) != NULL) { p_input_line++; if (!skip_rest_of_patch) fputs(buf, pipefp); diff --git a/usr.bin/patch/pch.h b/usr.bin/patch/pch.h index 9f20d696123..9c8633f4f53 100644 --- a/usr.bin/patch/pch.h +++ b/usr.bin/patch/pch.h @@ -1,15 +1,11 @@ -/* $OpenBSD: pch.h,v 1.5 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: pch.h,v 1.6 2003/07/22 17:18:49 otto Exp $ */ -EXT FILE *pfp INIT(Nullfp); /* patch file pointer */ +EXT FILE *pfp INIT(NULL); /* patch file pointer */ void re_patch(void); void open_patch_file(char *); void set_hunkmax(void); -void grow_hunkmax(void); bool there_is_another_patch(void); -int intuit_diff_type(void); -void next_intuit_at(long, long); -void skip_to(long, long); bool another_hunk(void); bool pch_swap(void); char *pfetch(LINENUM); @@ -23,5 +19,4 @@ LINENUM pch_context(void); LINENUM pch_hunk_beg(void); char pch_char(LINENUM); char *pfetch(LINENUM); -char *pgets(char *, int, FILE *); void do_ed_script(void); diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index 6340266a7a9..800097c3972 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,16 +1,28 @@ -/* $OpenBSD: util.c,v 1.15 2003/07/21 14:32:21 deraadt Exp $ */ +/* $OpenBSD: util.c,v 1.16 2003/07/22 17:18:49 otto Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: util.c,v 1.15 2003/07/21 14:32:21 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: util.c,v 1.16 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ +#include <sys/param.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <paths.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + #include "EXTERN.h" #include "common.h" #include "INTERN.h" #include "util.h" #include "backupfile.h" -void my_exit(int) __attribute__((noreturn)); /* Rename a file, copying it if necessary. */ @@ -31,7 +43,7 @@ move_file(char *from, char *to) if (fromfd < 0) pfatal("internal error, can't reopen %s", from); while ((i = read(fromfd, buf, sizeof buf)) > 0) - if (write(1, buf, i) != 1) + if (write(STDOUT_FILENO, buf, i) != i) pfatal("write failed"); close(fromfd); return 0; @@ -167,7 +179,7 @@ savestr(char *s) while (*t++) ; rv = malloc((size_t) (t - s)); - if (rv == Nullch) { + if (rv == NULL) { if (using_plan_a) out_of_mem = TRUE; else @@ -275,7 +287,6 @@ ask(char *fmt, ...) void set_signals(int reset) { -#ifndef lint static sig_t hupval, intval; if (!reset) { @@ -288,7 +299,6 @@ set_signals(int reset) } signal(SIGHUP, hupval); signal(SIGINT, intval); -#endif } /* @@ -297,10 +307,8 @@ set_signals(int reset) void ignore_signals(void) { -#ifndef lint signal(SIGHUP, SIG_IGN); signal(SIGINT, SIG_IGN); -#endif } /* @@ -340,7 +348,7 @@ fetchname(char *at, int strip_leading, int assume_exists) int sleading = strip_leading; if (!at || *at == '\0') - return Nullch; + return NULL; while (isspace(*at)) at++; #ifdef DEBUGGING @@ -348,7 +356,7 @@ fetchname(char *at, int strip_leading, int assume_exists) say("fetchname %s %d %d\n", at, strip_leading, assume_exists); #endif if (strnEQ(at, "/dev/null", 9)) /* so files can be created by diffing */ - return Nullch; /* against /dev/null. */ + return NULL; /* against /dev/null. */ name = fullname = t = savestr(at); /* Strip off up to `sleading' leading slashes and null terminate. */ @@ -387,7 +395,7 @@ fetchname(char *at, int strip_leading, int assume_exists) try("%s/%s%s", filedir, SCCSPREFIX, filebase)) return name; free(name); - name = Nullch; + name = NULL; } return name; } @@ -398,3 +406,18 @@ version(void) fprintf(stderr, "Patch version 2.0-12u8-OpenBSD\n"); my_exit(0); } + +/* + * Exit with cleanup. + */ +void +my_exit(int status) +{ + unlink(TMPINNAME); + if (!toutkeep) + unlink(TMPOUTNAME); + if (!trejkeep) + unlink(TMPREJNAME); + unlink(TMPPATNAME); + exit(status); +} diff --git a/usr.bin/patch/util.h b/usr.bin/patch/util.h index 3b81cd8da68..4f0d42c10cd 100644 --- a/usr.bin/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,18 +1,18 @@ -/* $OpenBSD: util.h,v 1.6 2003/07/21 14:32:21 deraadt Exp $ */ - -/* and for those machine that can't handle a variable argument list */ - -EXT char serrbuf[BUFSIZ];/* buffer for stderr */ +/* $OpenBSD: util.h,v 1.7 2003/07/22 17:18:49 otto Exp $ */ char *fetchname(char *, int, int); int move_file(char *, char *); void copy_file(char *, char *); void say(char *, ...); -void fatal(char *, ...); -void pfatal(char *, ...); -void ask(char *, ...); +void fatal(char *, ...) + __attribute__((__format__(__printf__, 1, 2))); +void pfatal(char *, ...) + __attribute__((__format__(__printf__, 1, 2))); +void ask(char *, ...) + __attribute__((__format__(__printf__, 1, 2))); char *savestr(char *); void set_signals(int); void ignore_signals(void); void makedirs(char *, bool); void version(void); +void my_exit(int) __attribute__((noreturn)); |