From: Sascha Wildner Date: Sat, 29 Sep 2007 23:11:10 +0000 (+0000) Subject: Sync with OpenBSD: X-Git-Tag: v2.0.1~2117 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/9d62a0f98f0de1bb82fb9d9c510d597e1e9a188d Sync with OpenBSD: * Instead of calling out mkdir -p, reuse code from mkdir(1). * Use calloc() to avoid malloc(n * m) overflows. * Add detailed SYNOPSIS to the manual page, sort options and add missing arguments. * Various cleanups. --- diff --git a/usr.bin/patch/Makefile b/usr.bin/patch/Makefile index 04f922bdc7..a8f01406c7 100644 --- a/usr.bin/patch/Makefile +++ b/usr.bin/patch/Makefile @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile,v 1.3 2003/07/18 02:00:09 deraadt Exp $ -# $DragonFly: src/usr.bin/patch/Makefile,v 1.6 2007/08/27 16:50:57 pavalos Exp $ +# $OpenBSD: Makefile,v 1.4 2005/05/16 15:22:46 espie Exp $ +# $DragonFly: src/usr.bin/patch/Makefile,v 1.7 2007/09/29 23:11:10 swildner Exp $ PROG= patch -SRCS= patch.c pch.c inp.c util.c backupfile.c +SRCS= patch.c pch.c inp.c util.c backupfile.c mkpath.c .if defined(BOOTSTRAPPING) CFLAGS+= -DBOOTSTRAPPING diff --git a/usr.bin/patch/backupfile.c b/usr.bin/patch/backupfile.c index d0e4859dc3..d4d35ad3e7 100644 --- a/usr.bin/patch/backupfile.c +++ b/usr.bin/patch/backupfile.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: backupfile.c,v 1.18 2004/08/05 21:47:24 deraadt Exp $ - * $DragonFly: src/usr.bin/patch/backupfile.c,v 1.3 2006/04/10 08:11:43 joerg Exp $ + * $OpenBSD: backupfile.c,v 1.19 2006/03/11 19:41:30 otto Exp $ + * $DragonFly: src/usr.bin/patch/backupfile.c,v 1.4 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -44,7 +44,7 @@ const char *simple_backup_suffix = "~"; static char *concat(const char *, const char *); static char *make_version_name(const char *, int); static int max_backup_version(const char *, const char *); -static int version_number(const char *, const char *, int); +static int version_number(const char *, const char *, size_t); static int argmatch(const char *, const char **); static void invalid_arg(const char *, const char *, int); @@ -98,7 +98,8 @@ max_backup_version(const char *file, const char *dir) { DIR *dirp; struct dirent *dp; - int highest_version, this_version, file_name_length; + int highest_version, this_version; + size_t file_name_length; dirp = opendir(dir); if (dirp == NULL) @@ -139,7 +140,7 @@ make_version_name(const char *file, int version) * already have ".~" appended to it. */ static int -version_number(const char *base, const char *backup, int base_length) +version_number(const char *base, const char *backup, size_t base_length) { int version; const char *p; diff --git a/usr.bin/patch/backupfile.h b/usr.bin/patch/backupfile.h index ce32274966..34532a68da 100644 --- a/usr.bin/patch/backupfile.h +++ b/usr.bin/patch/backupfile.h @@ -1,6 +1,6 @@ /* * $OpenBSD: backupfile.h,v 1.6 2003/07/28 18:35:36 otto Exp $ - * $DragonFly: src/usr.bin/patch/backupfile.h,v 1.2 2004/09/28 19:09:50 joerg Exp $ + * $DragonFly: src/usr.bin/patch/backupfile.h,v 1.3 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -38,4 +38,4 @@ extern enum backup_type backup_type; extern const char *simple_backup_suffix; char *find_backup_file_name(const char *file); -enum backup_type get_version(const char *vers); +enum backup_type get_version(const char *version); diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index d31448db5e..5f34af9e55 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,6 +1,6 @@ /* - * $OpenBSD: common.h,v 1.25 2003/10/31 20:20:45 millert Exp $ - * $DragonFly: src/usr.bin/patch/common.h,v 1.3 2006/04/18 22:11:35 joerg Exp $ + * $OpenBSD: common.h,v 1.26 2006/03/11 19:41:30 otto Exp $ + * $DragonFly: src/usr.bin/patch/common.h,v 1.4 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -29,6 +29,8 @@ * behaviour */ +#include + #include /* @@ -71,7 +73,7 @@ typedef long LINENUM; /* must be signed */ /* globals */ -extern int filemode; +extern mode_t filemode; extern char buf[MAXLINELEN];/* general purpose buffer */ extern size_t buf_len; diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index dceef21ed3..958c9af3e7 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: inp.c,v 1.32 2004/08/05 21:47:24 deraadt Exp $ - * $DragonFly: src/usr.bin/patch/inp.c,v 1.5 2006/04/18 22:11:35 joerg Exp $ + * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $ + * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -95,7 +95,7 @@ re_input(void) } } -/* Constuct the line index, somehow or other. */ +/* Construct the line index, somehow or other. */ void scan_input(const char *filename) @@ -136,11 +136,10 @@ plan_a(const char *filename) { int ifd, statfailed; char *p, *s, lbuf[MAXLINELEN]; - LINENUM iline; struct stat filestat; off_t i; ptrdiff_t sz; - size_t lines_allocated; + size_t iline, lines_allocated; #ifdef DEBUGGING if (debug & 8) @@ -283,7 +282,7 @@ plan_a(const char *filename) /* test for NUL too, to maintain the behavior of the original code */ for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) { if (*s == '\n') { - if (iline == (LINENUM)lines_allocated) { + if (iline == lines_allocated) { if (!reallocate_lines(&lines_allocated)) return false; } @@ -348,7 +347,7 @@ static void plan_b(const char *filename) { FILE *ifp; - int i = 0, j, maxlen = 1; + size_t i = 0, j, maxlen = 1; char *p; bool found_revision = (revision == NULL); @@ -448,8 +447,9 @@ ifetch(LINENUM line, int whichbuf) else { tiline[whichbuf] = baseline; - lseek(tifd, (off_t) (baseline / lines_per_buf * - BUFFERSIZE), SEEK_SET); + if (lseek(tifd, (off_t) (baseline / lines_per_buf * + BUFFERSIZE), SEEK_SET) < 0) + pfatal("cannot seek in the temporary input file"); if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) pfatal("error reading tmp file %s", TMPINNAME); @@ -465,7 +465,7 @@ static bool rev_in_string(const char *string) { const char *s; - int patlen; + size_t patlen; if (revision == NULL) return true; diff --git a/usr.bin/patch/mkpath.c b/usr.bin/patch/mkpath.c new file mode 100644 index 0000000000..f230da387d --- /dev/null +++ b/usr.bin/patch/mkpath.c @@ -0,0 +1,80 @@ +/* + * $OpenBSD: mkpath.c,v 1.2 2005/06/20 07:14:06 otto Exp $ + * $DragonFly: src/usr.bin/patch/mkpath.c,v 1.1 2007/09/29 23:11:10 swildner Exp $ + */ + +/* + * Copyright (c) 1983, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +int mkpath(char *); + +/* Code taken directly from mkdir(1). + + * mkpath -- create directories. + * path - path + */ +int +mkpath(char *path) +{ + struct stat sb; + char *slash; + int done = 0; + + slash = path; + + while (!done) { + slash += strspn(slash, "/"); + slash += strcspn(slash, "/"); + + done = (*slash == '\0'); + *slash = '\0'; + + if (stat(path, &sb)) { + if (errno != ENOENT || (mkdir(path, 0777) && + errno != EEXIST)) { + warn("%s", path); + return (-1); + } + } else if (!S_ISDIR(sb.st_mode)) { + warnx("%s: %s", path, strerror(ENOTDIR)); + return (-1); + } + + *slash = '/'; + } + + return (0); +} + diff --git a/usr.bin/patch/patch.1 b/usr.bin/patch/patch.1 index 06cc85c8e1..8a16a0ecc1 100644 --- a/usr.bin/patch/patch.1 +++ b/usr.bin/patch/patch.1 @@ -1,5 +1,5 @@ -.\" $OpenBSD: patch.1,v 1.17 2003/10/31 20:20:45 millert Exp $ -.\" $DragonFly: src/usr.bin/patch/patch.1,v 1.7 2007/07/29 17:27:45 swildner Exp $ +.\" $OpenBSD: patch.1,v 1.20 2007/05/31 19:20:14 jmc Exp $ +.\" $DragonFly: src/usr.bin/patch/patch.1,v 1.8 2007/09/29 23:11:10 swildner Exp $ .\" .\" Copyright 1986, Larry Wall .\" @@ -21,7 +21,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd July 23, 2003 +.Dd $Mdocdate$ .Dt PATCH 1 .Os .Sh NAME @@ -29,8 +29,22 @@ .Nd apply a diff file to an original .Sh SYNOPSIS .Nm -.Op Cm options +.Bk -words +.Op Fl bCcEeflNnRstuv +.Op Fl B Ar backup-prefix +.Op Fl D Ar symbol +.Op Fl d Ar directory +.Op Fl F Ar max-fuzz +.Op Fl i Ar patchfile +.Op Fl o Ar out-file +.Op Fl p Ar strip-count +.Op Fl r Ar rej-name +.Op Fl V Cm t | nil | never +.Op Fl x Ar number +.Op Fl z Ar backup-ext +.Op Fl Fl posix .Op Ar origfile Op Ar patchfile +.Ek .Nm .Pf \*(Lt Ar patchfile .Sh DESCRIPTION @@ -73,6 +87,15 @@ below). .Pp The options are as follows: .Bl -tag -width Ds +.It Xo +.Fl B Ar backup-prefix , +.Fl Fl prefix Ar backup-prefix +.Xc +Causes the next argument to be interpreted as a prefix to the backup file +name. +If this argument is specified, any argument to +.Fl z +will be ignored. .It Fl b , Fl Fl backup Save a backup copy of the file before it is modified. By default the original file is saved with a backup extension of @@ -80,27 +103,18 @@ By default the original file is saved with a backup extension of unless the file already has a numbered backup, in which case a numbered backup is made. This is equivalent to specifying -.Qo Fl V Ar existing Qc . +.Qo Fl V Cm existing Qc . This option is currently the default but that will change in a future release. -.It Fl B , Fl Fl prefix -Causes the next argument to be interpreted as a prefix to the backup file -name. -If this argument is specified, any argument to -.Fl z -will be ignored. +.It Fl C , Fl Fl check +Checks that the patch would apply cleanly, but does not modify anything. .It Fl c , Fl Fl context Forces .Nm to interpret the patch file as a context diff. -.It Fl C , Fl Fl check -Checks that the patch would apply cleanly, but does not modify anything. -.It Fl d , Fl Fl directory -Causes -.Nm -to interpret the next argument as a directory, and -.Xr cd 1 -to it before doing anything else. -.It Fl D , Fl Fl ifdef +.It Xo +.Fl D Ar symbol , +.Fl Fl ifdef Ar symbol +.Xc Causes .Nm to use the @@ -110,17 +124,37 @@ The argument following will be used as the differentiating symbol. Note that, unlike the C compiler, there must be a space between the .Fl D and the argument. +.It Xo +.Fl d Ar directory , +.Fl Fl directory Ar directory +.Xc +Causes +.Nm +to interpret the next argument as a directory, and +.Xr cd 1 +to it before doing anything else. +.It Fl E , Fl Fl remove-empty-files +Causes +.Nm +to remove output files that are empty after the patches have been applied. +This option is useful when applying patches that create or remove files. .It Fl e , Fl Fl ed Forces .Nm to interpret the patch file as an .Xr ed 1 script. -.It Fl E , Fl Fl remove-empty-files -Causes +.It Xo +.Fl F Ar max-fuzz , +.Fl Fl fuzz Ar max-fuzz +.Xc +Sets the maximum fuzz factor. +This option only applies to context diffs, and causes .Nm -to remove output files that are empty after the patches have been applied. -This option is useful when applying patches that create or remove files. +to ignore up to that many lines in looking for places to install a hunk. +Note that a larger fuzz factor increases the odds of a faulty patch. +The default fuzz factor is 2, and it may not be set to more than +the number of lines of context in the context diff, ordinarily 3. .It Fl f , Fl Fl force Forces .Nm @@ -136,17 +170,9 @@ This option does not suppress commentary; use .Fl s for that. .It Xo -.Fl F Ns Aq Ar number , -.Fl Fl fuzz Aq Ar number +.Fl i Ar patchfile , +.Fl Fl input Ar patchfile .Xc -Sets the maximum fuzz factor. -This option only applies to context diffs, and causes -.Nm -to ignore up to that many lines in looking for places to install a hunk. -Note that a larger fuzz factor increases the odds of a faulty patch. -The default fuzz factor is 2, and it may not be set to more than -the number of lines of context in the context diff, ordinarily 3. -.It Fl i , Fl Fl input Causes the next argument to be interpreted as the input file name (i.e. a patchfile). This option may be specified multiple times. @@ -157,21 +183,24 @@ Any sequence of whitespace in the pattern line will match any sequence in the input file. Normal characters must still match exactly. Each line of the context must still match a line in the input file. -.It Fl n , Fl Fl normal -Forces -.Nm -to interpret the patch file as a normal diff. .It Fl N , Fl Fl forward Causes .Nm to ignore patches that it thinks are reversed or already applied. See also .Fl R . -.It Fl o , Fl Fl output +.It Fl n , Fl Fl normal +Forces +.Nm +to interpret the patch file as a normal diff. +.It Xo +.Fl o Ar out-file , +.Fl Fl output Ar out-file +.Xc Causes the next argument to be interpreted as the output file name. .It Xo -.Fl p Ns Aq Ar number , -.Fl Fl strip Aq Ar number +.Fl p Ar strip-count , +.Fl Fl strip Ar strip-count .Xc Sets the pathname strip count, which controls how pathnames found in the patch file are treated, @@ -211,8 +240,6 @@ Whatever you end up with is looked for either in the current directory, or the directory specified by the .Fl d option. -.It Fl r , Fl Fl reject-file -Causes the next argument to be interpreted as the reject file name. .It Fl R , Fl Fl reverse Tells .Nm @@ -242,6 +269,11 @@ Luckily, most patches add or change lines rather than delete them, so most reversed normal diffs will begin with a delete, which will fail, triggering the heuristic.) .It Xo +.Fl r Ar rej-name , +.Fl Fl reject-file Ar rej-name +.Xc +Causes the next argument to be interpreted as the reject file name. +.It Xo .Fl s , Fl Fl quiet , .Fl Fl silent .Xc @@ -262,11 +294,10 @@ and assume that patches are reversed if they look like they are. Forces .Nm to interpret the patch file as a unified context diff (a unidiff). -.It Fl v , Fl Fl version -Causes -.Nm -to print out its revision header and patch level. -.It Fl V , Fl Fl version-control +.It Xo +.Fl V Cm t | nil | never , +.Fl Fl version-control Cm t | nil | never +.Xc Causes the next argument to be interpreted as a method for creating backup file names. The type of backups made can also be given in the @@ -289,22 +320,29 @@ option are like the GNU Emacs variable; they also recognize synonyms that are more descriptive. The valid values are (unique abbreviations are accepted): .Bl -tag -width Ds -offset indent -.It t , numbered +.It Cm t , numbered Always make numbered backups. -.It nil , existing +.It Cm nil , existing Make numbered backups of files that already have them, simple backups of the others. -.It never , simple +.It Cm never , simple Always make simple backups. .El +.It Fl v , Fl Fl version +Causes +.Nm +to print out its revision header and patch level. .It Xo -.Fl x Ns Aq Ar number , -.Fl Fl debug Aq Ar number +.Fl x Ar number , +.Fl Fl debug Ar number .Xc Sets internal debugging flags, and is of interest only to .Nm patchers. -.It Fl z , Fl Fl suffix +.It Xo +.Fl z Ar backup-ext , +.Fl Fl suffix Ar backup-ext +.Xc Causes the next argument to be interpreted as the backup extension, to be used in place of .Qq .orig . @@ -595,6 +633,19 @@ When applying a set of patches in a loop it behooves you to check this exit status so you don't apply a later patch to a partially patched file. .Sh SEE ALSO .Xr diff 1 +.Sh STANDARDS +The +.Nm +utility is compliant with the +.St -p1003.1-2004 +specification, +but its presence is optional. +.Pp +The flags +.Op Fl CEfstuvBFVxz +and +.Op Fl -posix +are extensions to that specification. .Sh AUTHORS .An Larry Wall with many other contributors. diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index d482fbc490..f984e59ed6 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,6 +1,6 @@ /* - * $OpenBSD: patch.c,v 1.43 2004/11/19 20:08:11 otto Exp $ - * $DragonFly: src/usr.bin/patch/patch.c,v 1.6 2006/04/18 22:11:35 joerg Exp $ + * $OpenBSD: patch.c,v 1.45 2007/04/18 21:52:24 sobrado Exp $ + * $DragonFly: src/usr.bin/patch/patch.c,v 1.7 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -47,7 +47,7 @@ #include "backupfile.h" #include "pathnames.h" -int filemode = 0644; +mode_t filemode = 0644; char buf[MAXLINELEN]; /* general purpose buffer */ size_t buf_len = sizeof(buf); @@ -605,10 +605,11 @@ static void usage(void) { fprintf(stderr, -"usage: patch [-bcCeEflnNRstuv] [-B backup-prefix] [-d directory] [-D symbol]\n" +"usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n" " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n" -" [-r rej-name] [-V {numbered,existing,simple}] [-z backup-ext]\n" -" [origfile [patchfile]]\n"); +" [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n" +" [--posix] [origfile [patchfile]]\n" +" patch #include #include +#include #include #include #include @@ -48,7 +49,6 @@ #include "backupfile.h" #include "pathnames.h" - /* Rename a file, copying it if necessary. */ int @@ -326,12 +326,9 @@ makedirs(const char *filename, bool striplast) return; /* nothing to be done */ *s = '\0'; } - if (snprintf(buf, buf_len, "%s -p %s", _PATH_MKDIR, tmpbuf) - >= (int)buf_len) - fatal("buffer too small to hold %.20s...\n", tmpbuf); - - if (system(buf)) - pfatal("%.40s failed", buf); + if (mkpath(tmpbuf) != 0) + pfatal("creation of %s failed", tmpbuf); + free(tmpbuf); } /* diff --git a/usr.bin/patch/util.h b/usr.bin/patch/util.h index 3c99a6b460..ed2205bd92 100644 --- a/usr.bin/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,6 +1,6 @@ /* - * $OpenBSD: util.h,v 1.13 2004/08/05 21:47:24 deraadt Exp $ - * $DragonFly: src/usr.bin/patch/util.h,v 1.1 2004/09/24 18:44:28 joerg Exp $ + * $OpenBSD: util.h,v 1.15 2005/06/20 07:14:06 otto Exp $ + * $DragonFly: src/usr.bin/patch/util.h,v 1.2 2007/09/29 23:11:10 swildner Exp $ */ /* @@ -48,3 +48,6 @@ void ignore_signals(void); void makedirs(const char *, bool); void version(void); void my_exit(int) __attribute__((noreturn)); + +/* in mkpath.c */ +extern int mkpath(char *);