summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/patch/patch.c43
-rw-r--r--usr.bin/patch/pathnames.h11
-rw-r--r--usr.bin/patch/pch.c11
-rw-r--r--usr.bin/patch/util.c14
4 files changed, 47 insertions, 32 deletions
diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c
index 096a3e8388f..7e847065947 100644
--- a/usr.bin/patch/patch.c
+++ b/usr.bin/patch/patch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: patch.c,v 1.30 2003/07/28 19:15:34 deraadt Exp $ */
+/* $OpenBSD: patch.c,v 1.31 2003/07/29 20:10:17 millert Exp $ */
/*
* patch - a program to apply diffs to original files
@@ -27,7 +27,7 @@
*/
#ifndef lint
-static const char rcsid[] = "$OpenBSD: patch.c,v 1.30 2003/07/28 19:15:34 deraadt Exp $";
+static const char rcsid[] = "$OpenBSD: patch.c,v 1.31 2003/07/29 20:10:17 millert Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -47,6 +47,7 @@ static const char rcsid[] = "$OpenBSD: patch.c,v 1.30 2003/07/28 19:15:34 deraad
#include "pch.h"
#include "inp.h"
#include "backupfile.h"
+#include "pathnames.h"
int filemode = 0644;
@@ -143,42 +144,44 @@ static char end_defined[128];
int
main(int argc, char *argv[])
{
- int error = 0, hunk, failed, patch_seen = 0, i;
+ int error = 0, hunk, failed, patch_seen = 0, i, fd;
LINENUM where, newwhere, fuzz, mymaxfuzz;
- char *tmpdir, *v;
+ const char *tmpdir;
+ char *v;
setbuf(stderr, serrbuf);
for (i = 0; i < MAXFILEC; i++)
filearg[i] = NULL;
/* Cons up the names of the temporary files. */
- tmpdir = getenv("TMPDIR");
- if (tmpdir == NULL) {
- tmpdir = "/tmp";
- }
- if (asprintf(&TMPOUTNAME, "%s/patchoXXXXXXXXXX", tmpdir) == -1)
+ if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
+ tmpdir = _PATH_TMP;
+ for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
+ ;
+ i++;
+ if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
fatal("cannot allocate memory");
- if ((i = mkstemp(TMPOUTNAME)) < 0)
+ if ((fd = mkstemp(TMPOUTNAME)) < 0)
pfatal("can't create %s", TMPOUTNAME);
- close(i);
+ close(fd);
- if (asprintf(&TMPINNAME, "%s/patchiXXXXXXXXXX", tmpdir) == -1)
+ if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
fatal("cannot allocate memory");
- if ((i = mkstemp(TMPINNAME)) < 0)
+ if ((fd = mkstemp(TMPINNAME)) < 0)
pfatal("can't create %s", TMPINNAME);
- close(i);
+ close(fd);
- if (asprintf(&TMPREJNAME, "%s/patchrXXXXXXXXXX", tmpdir) == -1)
+ if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
fatal("cannot allocate memory");
- if ((i = mkstemp(TMPREJNAME)) < 0)
+ if ((fd = mkstemp(TMPREJNAME)) < 0)
pfatal("can't create %s", TMPREJNAME);
- close(i);
+ close(fd);
- if (asprintf(&TMPPATNAME, "%s/patchpXXXXXXXXXX", tmpdir) == -1)
+ if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
fatal("cannot allocate memory");
- if ((i = mkstemp(TMPPATNAME)) < 0)
+ if ((fd = mkstemp(TMPPATNAME)) < 0)
pfatal("can't create %s", TMPPATNAME);
- close(i);
+ close(fd);
v = getenv("SIMPLE_BACKUP_SUFFIX");
if (v)
diff --git a/usr.bin/patch/pathnames.h b/usr.bin/patch/pathnames.h
new file mode 100644
index 00000000000..397e3fabe37
--- /dev/null
+++ b/usr.bin/patch/pathnames.h
@@ -0,0 +1,11 @@
+/* $OpenBSD: pathnames.h,v 1.1 2003/07/29 20:10:17 millert Exp $ */
+
+/*
+ * Placed in the public domain by Todd C. Miller <Todd.Miller@courtesan.com>
+ * on July 29, 2003.
+ */
+
+#include <paths.h>
+
+#define _PATH_ED "/bin/ed"
+#define _PATH_MKDIR "/bin/mkdir"
diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c
index be863a9ba9d..79fb6696dcf 100644
--- a/usr.bin/patch/pch.c
+++ b/usr.bin/patch/pch.c
@@ -1,7 +1,7 @@
-/* $OpenBSD: pch.c,v 1.26 2003/07/28 19:15:34 deraadt Exp $ */
+/* $OpenBSD: pch.c,v 1.27 2003/07/29 20:10:17 millert Exp $ */
#ifndef lint
-static const char rcsid[] = "$OpenBSD: pch.c,v 1.26 2003/07/28 19:15:34 deraadt Exp $";
+static const char rcsid[] = "$OpenBSD: pch.c,v 1.27 2003/07/29 20:10:17 millert Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -17,6 +17,7 @@ static const char rcsid[] = "$OpenBSD: pch.c,v 1.26 2003/07/28 19:15:34 deraadt
#include "common.h"
#include "util.h"
#include "pch.h"
+#include "pathnames.h"
/* Patch (diff listing) abstract type. */
@@ -1320,10 +1321,8 @@ do_ed_script(void)
unlink(TMPOUTNAME);
fatal("can't create temp file %s", TMPOUTNAME);
}
- if (verbose)
- snprintf(buf, sizeof buf, "/bin/ed %s", TMPOUTNAME);
- else
- snprintf(buf, sizeof buf, "/bin/ed - %s", TMPOUTNAME);
+ snprintf(buf, sizeof buf, "%s%s%s", _PATH_ED,
+ verbose ? " " : " -s ", TMPOUTNAME);
pipefp = popen(buf, "w");
}
for (;;) {
diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c
index 32d25e451cf..afb49286b51 100644
--- a/usr.bin/patch/util.c
+++ b/usr.bin/patch/util.c
@@ -1,7 +1,7 @@
-/* $OpenBSD: util.c,v 1.21 2003/07/28 19:05:26 millert Exp $ */
+/* $OpenBSD: util.c,v 1.22 2003/07/29 20:10:17 millert Exp $ */
#ifndef lint
-static const char rcsid[] = "$OpenBSD: util.c,v 1.21 2003/07/28 19:05:26 millert Exp $";
+static const char rcsid[] = "$OpenBSD: util.c,v 1.22 2003/07/29 20:10:17 millert Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -21,6 +21,7 @@ static const char rcsid[] = "$OpenBSD: util.c,v 1.21 2003/07/28 19:05:26 mil
#include "common.h"
#include "util.h"
#include "backupfile.h"
+#include "pathnames.h"
/* Rename a file, copying it if necessary. */
@@ -307,8 +308,8 @@ makedirs(const char *filename, bool striplast)
return; /* nothing to be done */
*s = '\0';
}
- strlcpy(buf, "/bin/mkdir -p ", sizeof buf);
- if (strlcat(buf, tmpbuf, sizeof(buf)) >= sizeof(buf))
+ if (snprintf(buf, sizeof(buf), "%s -p %s", _PATH_MKDIR, tmpbuf)
+ >= sizeof(buf))
fatal("buffer too small to hold %.20s...\n", tmpbuf);
if (system(buf))
@@ -332,8 +333,9 @@ fetchname(const char *at, int strip_leading, int assume_exists)
if (debug & 128)
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 NULL; /* against /dev/null. */
+ /* So files can be created by diffing against /dev/null. */
+ if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
+ return NULL;
name = fullname = t = savestr(at);
/* Strip off up to `strip_leading' path components and NUL terminate. */