summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/pax/extern.h3
-rw-r--r--bin/pax/gen_subs.c26
-rw-r--r--bin/pax/tar.c21
3 files changed, 34 insertions, 16 deletions
diff --git a/bin/pax/extern.h b/bin/pax/extern.h
index 50e4eeef1be..9ebdc74097f 100644
--- a/bin/pax/extern.h
+++ b/bin/pax/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.30 2005/04/25 19:39:52 otto Exp $ */
+/* $OpenBSD: extern.h,v 1.31 2005/04/28 06:58:07 otto Exp $ */
/* $NetBSD: extern.h,v 1.5 1996/03/26 23:54:16 mrg Exp $ */
/*-
@@ -177,6 +177,7 @@ int ul_asc(u_long, char *, int, int);
u_quad_t asc_uqd(char *, int, int);
int uqd_asc(u_quad_t, char *, int, int);
#endif
+size_t fieldcpy(char *, size_t, const char *, size_t);
/*
* getoldopt.c
diff --git a/bin/pax/gen_subs.c b/bin/pax/gen_subs.c
index 00ae4b674af..4acd1b28b7a 100644
--- a/bin/pax/gen_subs.c
+++ b/bin/pax/gen_subs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gen_subs.c,v 1.17 2003/06/13 17:51:14 millert Exp $ */
+/* $OpenBSD: gen_subs.c,v 1.18 2005/04/28 06:58:07 otto Exp $ */
/* $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */
/*-
@@ -38,7 +38,7 @@
#if 0
static const char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
#else
-static const char rcsid[] = "$OpenBSD: gen_subs.c,v 1.17 2003/06/13 17:51:14 millert Exp $";
+static const char rcsid[] = "$OpenBSD: gen_subs.c,v 1.18 2005/04/28 06:58:07 otto Exp $";
#endif
#endif /* not lint */
@@ -408,3 +408,25 @@ uqd_asc(u_quad_t val, char *str, int len, int base)
return(0);
}
#endif
+
+/*
+ * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
+ * at the first NUL char. NUL terminate buf if there is room left.
+ */
+size_t
+fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz)
+{
+ char *p = buf;
+ const char *q = field;
+ size_t i = 0;
+
+ if (fieldsz > bufsz)
+ fieldsz = bufsz;
+ while (i < fieldsz && *q != '\0') {
+ *p++ = *q++;
+ i++;
+ }
+ if (i < bufsz)
+ *p = '\0';
+ return(i);
+}
diff --git a/bin/pax/tar.c b/bin/pax/tar.c
index 1583048ebb9..ff4cfee6b7e 100644
--- a/bin/pax/tar.c
+++ b/bin/pax/tar.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tar.c,v 1.37 2005/04/21 21:47:18 beck Exp $ */
+/* $OpenBSD: tar.c,v 1.38 2005/04/28 06:58:07 otto Exp $ */
/* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */
/*-
@@ -38,7 +38,7 @@
#if 0
static const char sccsid[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
#else
-static const char rcsid[] = "$OpenBSD: tar.c,v 1.37 2005/04/21 21:47:18 beck Exp $";
+static const char rcsid[] = "$OpenBSD: tar.c,v 1.38 2005/04/28 06:58:07 otto Exp $";
#endif
#endif /* not lint */
@@ -721,7 +721,7 @@ ustar_id(char *blk, int size)
* programs are fouled up and create archives missing the \0. Last we
* check the checksum. If ok we have to assume it is a valid header.
*/
- if (hd->name[0] == '\0')
+ if (hd->prefix[0] == '\0' && hd->name[0] == '\0')
return(-1);
if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
return(-1);
@@ -763,9 +763,8 @@ ustar_rd(ARCHD *arcn, char *buf)
*/
dest = arcn->name;
if (*(hd->prefix) != '\0') {
- cnt = strlcpy(dest, hd->prefix, sizeof(arcn->name) - 1);
- if (cnt >= sizeof(arcn->name) - 1)
- cnt = sizeof(arcn->name) - 2; /* XXX truncate? */
+ cnt = fieldcpy(dest, sizeof(arcn->name) - 1, hd->prefix,
+ sizeof(hd->prefix));
dest += cnt;
*dest++ = '/';
cnt++;
@@ -1157,16 +1156,12 @@ expandname(char *buf, size_t len, char **gnu_name, const char *name,
size_t nlen;
if (*gnu_name) {
+ /* *gnu_name is NUL terminated */
if ((nlen = strlcpy(buf, *gnu_name, len)) >= len)
nlen = len - 1;
free(*gnu_name);
*gnu_name = NULL;
- } else {
- /* name is not necessarily NUL terminated */
- if (len > limit)
- len = limit + 1;
- if ((nlen = strlcpy(buf, name, len)) >= len)
- nlen = len - 1;
- }
+ } else
+ nlen = fieldcpy(buf, len, name, limit);
return(nlen);
}