From: Lionel Sambuc Date: Wed, 19 Feb 2014 16:10:34 +0000 (+0100) Subject: updating bsd patch, moving it back to usr.bin X-Git-Tag: v3.3.0~436 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/Bv9ARM.ch08.html?a=commitdiff_plain;h=757e83288ac54fa2d663d17e4a302156fdb3650a;p=minix.git updating bsd patch, moving it back to usr.bin Change-Id: Ibc356c932693729ebf86cc4cc51aeca65163af9b --- diff --git a/commands/Makefile b/commands/Makefile index e9f1dbaa5..29579854d 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -17,7 +17,7 @@ SUBDIR= add_route arp ash at backup btrace \ lpd lspci mail MAKEDEV \ mined \ mount mt netconf \ - nonamed patch \ + nonamed \ ping postinstall prep printroot \ profile progressbar pr_routes ps pwdauth \ ramdisk rarpd rawspeed rcp readclock \ diff --git a/usr.bin/Makefile b/usr.bin/Makefile index f96672c51..7a53784d2 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -19,7 +19,7 @@ SUBDIR= asa \ mkdep mkfifo mkstr mktemp \ msgc \ nbperf newgrp nice nl nohup \ - passwd paste pathchk pr \ + passwd paste patch pathchk pr \ printenv printf pwhash \ renice rev \ \ diff --git a/commands/patch/Makefile b/usr.bin/patch/Makefile similarity index 100% rename from commands/patch/Makefile rename to usr.bin/patch/Makefile diff --git a/commands/patch/backupfile.c b/usr.bin/patch/backupfile.c similarity index 94% rename from commands/patch/backupfile.c rename to usr.bin/patch/backupfile.c index f4574ad45..59a2de975 100644 --- a/commands/patch/backupfile.c +++ b/usr.bin/patch/backupfile.c @@ -22,6 +22,7 @@ */ #include +__RCSID("$NetBSD: backupfile.c,v 1.14 2008/09/19 18:33:34 joerg Exp $"); #include #include @@ -113,7 +114,7 @@ max_backup_version(const char *file, const char *dir) file_name_length = strlen(file); while ((dp = readdir(dirp)) != NULL) { - if (strlen(dp->d_name) <= file_name_length) + if (dp->d_namlen <= file_name_length) continue; this_version = version_number(file, dp->d_name, file_name_length); @@ -132,14 +133,9 @@ static char * make_version_name(const char *file, int version) { char *backup_name; - int len = strlen(file)+20; - if(!(backup_name = malloc(len))) + if (asprintf(&backup_name, "%s.~%d~", file, version) == -1) return NULL; - - if (snprintf(backup_name, len, "%s.~%d~", file, version) == -1) - return NULL; - return backup_name; } @@ -172,14 +168,9 @@ static char * concat(const char *str1, const char *str2) { char *newstr; - int len = strlen(str1) + strlen(str2) + 1; - if(!(newstr = malloc(strlen(str1) + strlen(str2) + 1))) + if (asprintf(&newstr, "%s%s", str1, str2) == -1) return NULL; - - if (snprintf(newstr, len, "%s%s", str1, str2) == -1) - return NULL; - return newstr; } diff --git a/commands/patch/backupfile.h b/usr.bin/patch/backupfile.h similarity index 100% rename from commands/patch/backupfile.h rename to usr.bin/patch/backupfile.h diff --git a/commands/patch/common.h b/usr.bin/patch/common.h similarity index 100% rename from commands/patch/common.h rename to usr.bin/patch/common.h diff --git a/commands/patch/inp.c b/usr.bin/patch/inp.c similarity index 93% rename from commands/patch/inp.c rename to usr.bin/patch/inp.c index e104cb122..6bf1b662a 100644 --- a/commands/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,7 +1,7 @@ /* * $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 $ - * $NetBSD: inp.c,v 1.19 2008/09/19 18:33:34 joerg Exp $ + * $NetBSD: inp.c,v 1.23 2009/10/21 17:16:11 joerg Exp $ */ /* @@ -31,6 +31,7 @@ */ #include +__RCSID("$NetBSD: inp.c,v 1.23 2009/10/21 17:16:11 joerg Exp $"); #include #include @@ -38,6 +39,7 @@ #include #include +#include #include #include #include @@ -45,7 +47,6 @@ #include #include #include -#include #include "common.h" #include "util.h" @@ -74,24 +75,6 @@ static bool plan_a(const char *); static void plan_b(const char *); -static int readfile(int fd, char *buf, size_t s) -{ - int ntoread, nread; - - ntoread = s; - nread = 0; - - while(ntoread > 0) { - if((nread = read(fd, buf, ntoread)) < 0) { - return nread; - } - buf += nread; - ntoread -= nread; - } - - return 0; -} - /* New patch--prepare to edit another file. */ void @@ -102,11 +85,7 @@ re_input(void) free(i_ptr); i_ptr = NULL; if (i_womp != NULL) { -#ifndef __minix munmap(i_womp, i_size); -#else - free(i_womp); -#endif i_womp = NULL; } } else { @@ -143,11 +122,7 @@ reallocate_lines(size_t *lines_allocated) new_size = *lines_allocated * 3 / 2; p = realloc(i_ptr, (new_size + 2) * sizeof(char *)); if (p == NULL) { /* shucks, it was a near thing */ -#ifndef __minix munmap(i_womp, i_size); -#else - free(i_womp); -#endif i_womp = NULL; free(i_ptr); i_ptr = NULL; @@ -279,44 +254,30 @@ plan_a(const char *filename) out_of_mem = false; return false; /* force plan b because plan a bombed */ } -#ifndef __minix - if (i_size > SIZE_MAX) { + if ((uintmax_t)i_size > (uintmax_t)SIZE_MAX) { say("block too large to mmap\n"); return false; } -#endif if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); -#ifndef __minix - i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); - if (i_womp == MAP_FAILED) { - perror("mmap failed"); - i_womp = NULL; - close(ifd); - return false; - } -#else - i_womp = malloc(i_size); - if(i_size && i_womp == NULL) { - fprintf(stderr, "Malloc failed.\n"); - i_womp = NULL; - close(ifd); - return false; - } - if(readfile(ifd, i_womp, i_size) < 0) { - perror("Readfile failed."); + if (i_size) { + i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); + if (i_womp == MAP_FAILED) { + perror("mmap failed"); + i_womp = NULL; + close(ifd); + return false; + } + } else { i_womp = NULL; - close(ifd); - return false; } -#endif close(ifd); -#ifndef __minix +#if !defined(__minix) if (i_size) madvise(i_womp, i_size, MADV_SEQUENTIAL); -#endif +#endif /* !defined(__minix) */ /* estimate the number of lines */ lines_allocated = i_size / 25; @@ -349,11 +310,7 @@ plan_a(const char *filename) if (p == NULL) { free(i_ptr); i_ptr = NULL; -#ifndef __minix munmap(i_womp, i_size); -#else - free(i_womp); -#endif i_womp = NULL; return false; } diff --git a/commands/patch/inp.h b/usr.bin/patch/inp.h similarity index 100% rename from commands/patch/inp.h rename to usr.bin/patch/inp.h diff --git a/commands/patch/mkpath.c b/usr.bin/patch/mkpath.c similarity index 97% rename from commands/patch/mkpath.c rename to usr.bin/patch/mkpath.c index 3b5c2ca6a..3d18036f9 100644 --- a/commands/patch/mkpath.c +++ b/usr.bin/patch/mkpath.c @@ -34,6 +34,7 @@ */ #include +__RCSID("$NetBSD: mkpath.c,v 1.1 2008/09/19 18:33:34 joerg Exp $"); #include #include diff --git a/commands/patch/patch.1 b/usr.bin/patch/patch.1 similarity index 95% rename from commands/patch/patch.1 rename to usr.bin/patch/patch.1 index b07efdcd7..b84c2aa7c 100644 --- a/commands/patch/patch.1 +++ b/usr.bin/patch/patch.1 @@ -1,6 +1,6 @@ .\" $OpenBSD: patch.1,v 1.22 2008/06/06 20:44:00 jmc Exp $ .\" $DragonFly: src/usr.bin/patch/patch.1,v 1.10 2008/08/18 19:15:55 joerg Exp $ -.\" $NetBSD: patch.1,v 1.13 2008/09/19 18:33:34 joerg Exp $ +.\" $NetBSD: patch.1,v 1.16 2013/01/29 09:30:11 wiz Exp $ .\" Copyright 1986, Larry Wall .\" .\" Redistribution and use in source and binary forms, with or without @@ -29,7 +29,6 @@ .Nd apply a diff file to an original .Sh SYNOPSIS .Nm -.Bk -words .Op Fl bCcEeflNnRstuv .Op Fl B Ar backup-prefix .Op Fl D Ar symbol @@ -44,7 +43,6 @@ .Op Fl z Ar backup-ext .Op Fl Fl posix .Op Ar origfile Op Ar patchfile -.Ek .Nm .Pf \*(Lt Ar patchfile .Sh DESCRIPTION @@ -87,10 +85,7 @@ 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 +.It Fl B Ar backup-prefix , Fl Fl prefix Ar backup-prefix Causes the next argument to be interpreted as a prefix to the backup file name. If this argument is specified, any argument to @@ -113,10 +108,7 @@ Checks that the patch would apply cleanly, but does not modify anything. Forces .Nm to interpret the patch file as a context diff. -.It Xo -.Fl D Ar symbol , -.Fl Fl ifdef Ar symbol -.Xc +.It Fl D Ar symbol , Fl Fl ifdef Ar symbol Causes .Nm to use the @@ -126,10 +118,7 @@ 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 +.It Fl d Ar directory , Fl Fl directory Ar directory Causes .Nm to interpret the next argument as a directory, @@ -145,10 +134,7 @@ Forces to interpret the patch file as an .Xr ed 1 script. -.It Xo -.Fl F Ar max-fuzz , -.Fl Fl fuzz Ar max-fuzz -.Xc +.It Fl F Ar max-fuzz , Fl Fl fuzz Ar max-fuzz Sets the maximum fuzz factor. This option only applies to context diffs, and causes .Nm @@ -170,12 +156,9 @@ and assume that patches are not reversed even if they look like they are. This option does not suppress commentary; use .Fl s for that. -.It Xo -.Fl i Ar patchfile , -.Fl Fl input Ar patchfile -.Xc +.It Fl i Ar patchfile , Fl Fl input Ar patchfile Causes the next argument to be interpreted as the input file name -(i.e. a patchfile). +(i.e., a patchfile). This option may be specified multiple times. .It Fl l , Fl Fl ignore-whitespace Causes the pattern matching to be done loosely, in case the tabs and @@ -194,15 +177,9 @@ See also 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 +.It Fl o Ar out-file , Fl Fl output Ar out-file Causes the next argument to be interpreted as the output file name. -.It Xo -.Fl p Ar strip-count , -.Fl Fl strip Ar strip-count -.Xc +.It Fl p Ar strip-count , Fl Fl strip Ar strip-count Sets the pathname strip count, which controls how pathnames found in the patch file are treated, in case you keep your files in a different directory than the person who sent @@ -263,21 +240,15 @@ If it can, you will be asked if you want to have the option set. If it can't, the patch will continue to be applied normally. (Note: this method cannot detect a reversed patch if it is a normal diff -and if the first command is an append (i.e. it should have been a delete) +and if the first command is an append (i.e., it should have been a delete) since appends always succeed, due to the fact that a null context will match anywhere. 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 +.It Fl r Ar rej-name , Fl Fl reject-file Ar rej-name Causes the next argument to be interpreted as the reject file name. -.It Xo -.Fl s , Fl Fl quiet , -.Fl Fl silent -.Xc +.It Fl s , Fl Fl quiet , Fl Fl silent Makes .Nm do its work silently, unless an error occurs. @@ -295,10 +266,7 @@ 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 Xo -.Fl V Cm t | nil | never , -.Fl Fl version-control Cm t | nil | never -.Xc +.It Fl V Cm t | nil | never , Fl Fl version-control Cm t | nil | never 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 @@ -333,17 +301,11 @@ Always make simple backups. Causes .Nm to print out its revision header and patch level. -.It Xo -.Fl x Ar number , -.Fl Fl debug Ar number -.Xc +.It Fl x Ar number , Fl Fl debug Ar number Sets internal debugging flags, and is of interest only to .Nm patchers. -.It Xo -.Fl z Ar backup-ext , -.Fl Fl suffix Ar backup-ext -.Xc +.It Fl z Ar backup-ext , Fl Fl suffix Ar backup-ext Causes the next argument to be interpreted as the backup extension, to be used in place of .Qq .orig . diff --git a/commands/patch/patch.c b/usr.bin/patch/patch.c similarity index 97% rename from commands/patch/patch.c rename to usr.bin/patch/patch.c index b5092d16b..9f49e7f96 100644 --- a/commands/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,7 +1,7 @@ /* * $OpenBSD: patch.c,v 1.45 2007/04/18 21:52:24 sobrado Exp $ * $DragonFly: src/usr.bin/patch/patch.c,v 1.10 2008/08/10 23:39:56 joerg Exp $ - * $NetBSD: patch.c,v 1.27 2008/09/19 18:33:34 joerg Exp $ + * $NetBSD: patch.c,v 1.29 2011/09/06 18:25:14 joerg Exp $ */ /* @@ -31,6 +31,7 @@ */ #include +__RCSID("$NetBSD: patch.c,v 1.29 2011/09/06 18:25:14 joerg Exp $"); #include #include @@ -105,7 +106,7 @@ static bool spew_output(void); static void dump_line(LINENUM, bool); static bool patch_match(LINENUM, LINENUM, LINENUM); static bool similar(const char *, const char *, int); -static void usage(void); +__dead static void usage(void); /* true if -E was specified on command line. */ static bool remove_empty_files = false; @@ -155,46 +156,36 @@ main(int argc, char *argv[]) LINENUM where = 0, newwhere, fuzz, mymaxfuzz; const char *tmpdir; char *v; - int alloclen; setbuf(stderr, serrbuf); for (i = 0; i < MAXFILEC; i++) filearg[i] = NULL; - /* Cons up the names of the temporary files. */ if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') tmpdir = _PATH_TMP; for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--) ; i++; - - alloclen = i + 100; -#define TMPALLOC(var) if(!(var = malloc(alloclen))) { fatal(#var); exit(1); } - TMPALLOC(TMPOUTNAME); - TMPALLOC(TMPINNAME); - TMPALLOC(TMPREJNAME); - TMPALLOC(TMPPATNAME); - - if (snprintf(TMPOUTNAME, alloclen, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1) + if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1) fatal("cannot allocate memory"); if ((fd = mkstemp(TMPOUTNAME)) < 0) pfatal("can't create %s", TMPOUTNAME); close(fd); - if (snprintf(TMPINNAME, alloclen, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1) + if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1) fatal("cannot allocate memory"); if ((fd = mkstemp(TMPINNAME)) < 0) pfatal("can't create %s", TMPINNAME); close(fd); - if (snprintf(TMPREJNAME, alloclen, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1) + if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1) fatal("cannot allocate memory"); if ((fd = mkstemp(TMPREJNAME)) < 0) pfatal("can't create %s", TMPREJNAME); close(fd); - if (snprintf(TMPPATNAME, alloclen, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1) + if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1) fatal("cannot allocate memory"); if ((fd = mkstemp(TMPPATNAME)) < 0) pfatal("can't create %s", TMPPATNAME); @@ -632,7 +623,7 @@ usage(void) " [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n" " [--posix] [origfile [patchfile]]\n" " patch +__RCSID("$NetBSD: pch.c,v 1.25 2013/01/29 09:30:11 wiz Exp $"); #include #include @@ -447,7 +448,7 @@ skip_to(LINENUM file_pos, LINENUM file_line) } /* Make this a function for better debugging. */ -static void +__dead static void malformed(void) { fatal("malformed patch at line %ld: %s", p_input_line, buf); @@ -455,7 +456,7 @@ malformed(void) } /* - * True if the line has been discarded (i.e. it is a line saying + * True if the line has been discarded (i.e., it is a line saying * "\ No newline at end of file".) */ static bool diff --git a/commands/patch/pch.h b/usr.bin/patch/pch.h similarity index 100% rename from commands/patch/pch.h rename to usr.bin/patch/pch.h diff --git a/commands/patch/util.c b/usr.bin/patch/util.c similarity index 97% rename from commands/patch/util.c rename to usr.bin/patch/util.c index f7c94a652..a964894df 100644 --- a/commands/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,7 +1,7 @@ /* * $OpenBSD: util.c,v 1.32 2006/03/11 19:41:30 otto Exp $ * $DragonFly: src/usr.bin/patch/util.c,v 1.9 2007/09/29 23:11:10 swildner Exp $ - * $NetBSD: util.c,v 1.24 2008/09/19 18:33:34 joerg Exp $ + * $NetBSD: util.c,v 1.26 2010/10/02 19:31:14 wiz Exp $ */ /* @@ -31,6 +31,7 @@ */ #include +__RCSID("$NetBSD: util.c,v 1.26 2010/10/02 19:31:14 wiz Exp $"); #include #include @@ -325,8 +326,10 @@ makedirs(const char *filename, bool striplast) if (striplast) { char *s = strrchr(tmpbuf, '/'); - if (s == NULL) + if (s == NULL) { + free(tmpbuf); return; /* nothing to be done */ + } *s = '\0'; } if (mkpath(tmpbuf) != 0) @@ -415,7 +418,7 @@ checked_in(char *file) void version(void) { - fprintf(stderr, "Patch version 2.0-12u8-NetBSD\n"); + printf("Patch version 2.0-12u8-NetBSD\n"); my_exit(EXIT_SUCCESS); } diff --git a/commands/patch/util.h b/usr.bin/patch/util.h similarity index 79% rename from commands/patch/util.h rename to usr.bin/patch/util.h index 222136c45..d5a7e16ac 100644 --- a/commands/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,7 +1,7 @@ /* * $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 $ - * $NetBSD: util.h,v 1.11 2008/09/19 18:33:34 joerg Exp $ + * $NetBSD: util.h,v 1.12 2011/09/06 18:25:14 joerg Exp $ */ /* @@ -35,20 +35,16 @@ char *checked_in(char *); int backup_file(const char *); int move_file(const char *, const char *); int copy_file(const char *, const char *); -void say(const char *, ...) - __attribute__((__format__(__printf__, 1, 2))); -void fatal(const char *, ...) - __attribute__((__format__(__printf__, 1, 2))); -void pfatal(const char *, ...) - __attribute__((__format__(__printf__, 1, 2))); -void ask(const char *, ...) - __attribute__((__format__(__printf__, 1, 2))); +void say(const char *, ...) __printflike(1, 2); +void fatal(const char *, ...) __printflike(1, 2) __dead; +void pfatal(const char *, ...) __printflike(1, 2) __dead; +void ask(const char *, ...) __printflike(1, 2); char *savestr(const char *); void set_signals(int); void ignore_signals(void); void makedirs(const char *, bool); -void version(void); -void my_exit(int) __attribute__((noreturn)); +void version(void) __dead; +void my_exit(int) __dead; /* in mkpath.c */ extern int mkpath(char *);