group_from_gid(). Eliminate some superfluous strncpy() calls.
ok millert@
-/* $OpenBSD: ar_subs.c,v 1.45 2015/03/19 05:14:24 guenther Exp $ */
+/* $OpenBSD: ar_subs.c,v 1.46 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $ */
/*-
((*frmt->st_rd)() < 0))
return;
- if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
- return;
-
now = time(NULL);
/*
-/* $OpenBSD: cache.c,v 1.21 2014/05/24 18:51:00 guenther Exp $ */
+/* $OpenBSD: cache.c,v 1.22 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */
/*-
static int pwopn = 0; /* is password file open */
static int gropn = 0; /* is group file open */
-static UIDC **uidtb = NULL; /* uid to name cache */
-static GIDC **gidtb = NULL; /* gid to name cache */
static UIDC **usrtb = NULL; /* user name to uid cache */
static GIDC **grptb = NULL; /* group name to gid cache */
-/*
- * uidtb_start
- * creates an empty uidtb
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-uidtb_start(void)
-{
- static int fail = 0;
-
- if (uidtb != NULL)
- return(0);
- if (fail)
- return(-1);
- if ((uidtb = calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
- ++fail;
- paxwarn(1, "Unable to allocate memory for user id cache table");
- return(-1);
- }
- return(0);
-}
-
-/*
- * gidtb_start
- * creates an empty gidtb
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-gidtb_start(void)
-{
- static int fail = 0;
-
- if (gidtb != NULL)
- return(0);
- if (fail)
- return(-1);
- if ((gidtb = calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
- ++fail;
- paxwarn(1, "Unable to allocate memory for group id cache table");
- return(-1);
- }
- return(0);
-}
-
/*
* usrtb_start
* creates an empty usrtb
return(0);
}
-/*
- * name_uid()
- * caches the name (if any) for the uid. If frc set, we always return the
- * the stored name (if valid or invalid match). We use a simple hash table.
- * Return
- * Pointer to stored name (or a empty string)
- */
-
-char *
-name_uid(uid_t uid, int frc)
-{
- struct passwd *pw;
- UIDC *ptr;
-
- if ((uidtb == NULL) && (uidtb_start() < 0))
- return("");
-
- /*
- * see if we have this uid cached
- */
- ptr = uidtb[uid % UID_SZ];
- if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
- /*
- * have an entry for this uid
- */
- if (frc || (ptr->valid == VALID))
- return(ptr->name);
- return("");
- }
-
- /*
- * No entry for this uid, we will add it
- */
- if (!pwopn) {
- setpassent(1);
- ++pwopn;
- }
- if (ptr == NULL)
- ptr = uidtb[uid % UID_SZ] = malloc(sizeof(UIDC));
-
- if ((pw = getpwuid(uid)) == NULL) {
- /*
- * no match for this uid in the local password file
- * a string that is the uid in numeric format
- */
- if (ptr == NULL)
- return("");
- ptr->uid = uid;
- ptr->valid = INVALID;
- (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
- (unsigned long)uid);
- if (frc == 0)
- return("");
- } else {
- /*
- * there is an entry for this uid in the password file
- */
- if (ptr == NULL)
- return(pw->pw_name);
- ptr->uid = uid;
- (void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name));
- ptr->valid = VALID;
- }
- return(ptr->name);
-}
-
-/*
- * name_gid()
- * caches the name (if any) for the gid. If frc set, we always return the
- * the stored name (if valid or invalid match). We use a simple hash table.
- * Return
- * Pointer to stored name (or a empty string)
- */
-
-char *
-name_gid(gid_t gid, int frc)
-{
- struct group *gr;
- GIDC *ptr;
-
- if ((gidtb == NULL) && (gidtb_start() < 0))
- return("");
-
- /*
- * see if we have this gid cached
- */
- ptr = gidtb[gid % GID_SZ];
- if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
- /*
- * have an entry for this gid
- */
- if (frc || (ptr->valid == VALID))
- return(ptr->name);
- return("");
- }
-
- /*
- * No entry for this gid, we will add it
- */
- if (!gropn) {
- setgroupent(1);
- ++gropn;
- }
- if (ptr == NULL)
- ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC));
-
- if ((gr = getgrgid(gid)) == NULL) {
- /*
- * no match for this gid in the local group file, put in
- * a string that is the gid in numeric format
- */
- if (ptr == NULL)
- return("");
- ptr->gid = gid;
- ptr->valid = INVALID;
- (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
- (unsigned long)gid);
- if (frc == 0)
- return("");
- } else {
- /*
- * there is an entry for this group in the group file
- */
- if (ptr == NULL)
- return(gr->gr_name);
- ptr->gid = gid;
- (void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name));
- ptr->valid = VALID;
- }
- return(ptr->name);
-}
-
/*
* uid_name()
* caches the uid for a given user name. We use a simple hash table.
* look up in hash table, if found and valid return the uid,
* if found and invalid, return a -1
*/
- ptr = grptb[st_hash(name, namelen, GID_SZ)];
+ ptr = grptb[st_hash(name, namelen, GNM_SZ)];
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
if (ptr->valid == INVALID)
return(-1);
++gropn;
}
if (ptr == NULL)
- ptr = grptb[st_hash(name, namelen, GID_SZ)] =
+ ptr = grptb[st_hash(name, namelen, GNM_SZ)] =
malloc(sizeof(GIDC));
/*
-/* $OpenBSD: cache.h,v 1.7 2015/12/24 05:50:15 mmcc Exp $ */
+/* $OpenBSD: cache.h,v 1.8 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: cache.h,v 1.3 1995/03/21 09:07:12 cgd Exp $ */
/*-
*/
#define UNMLEN 32 /* >= user name found in any protocol */
#define GNMLEN 32 /* >= group name found in any protocol */
-#define UID_SZ 317 /* size of user_name/uid cache */
-#define UNM_SZ 317 /* size of user_name/uid cache */
-#define GID_SZ 251 /* size of gid cache */
-#define GNM_SZ 317 /* size of group name cache */
+#define UNM_SZ 317 /* size of uid_name() cache */
+#define GNM_SZ 317 /* size of gid_name() cache */
#define VALID 1 /* entry and name are valid */
#define INVALID 2 /* entry valid, name NOT valid */
-/* $OpenBSD: extern.h,v 1.56 2016/08/23 06:00:28 guenther Exp $ */
+/* $OpenBSD: extern.h,v 1.57 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: extern.h,v 1.5 1996/03/26 23:54:16 mrg Exp $ */
/*-
/*
* cache.c
*/
-int uidtb_start(void);
-int gidtb_start(void);
int usrtb_start(void);
int grptb_start(void);
-char * name_uid(uid_t, int);
-char * name_gid(gid_t, int);
int uid_name(char *, uid_t *);
int gid_name(char *, gid_t *);
int tar_rd(ARCHD *, char *);
int tar_wr(ARCHD *);
int ustar_strd(void);
-int ustar_stwr(void);
int ustar_id(char *, int);
int ustar_rd(ARCHD *, char *);
int ustar_wr(ARCHD *);
-/* $OpenBSD: gen_subs.c,v 1.29 2016/08/14 04:47:52 guenther Exp $ */
+/* $OpenBSD: gen_subs.c,v 1.30 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */
/*-
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
+#include <grp.h>
+#include <pwd.h>
#include <stdio.h>
-#include <utmp.h>
-#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+#include <utmp.h>
#include <vis.h>
+
#include "pax.h"
#include "extern.h"
localtime(&(sbp->st_mtime))) == 0)
f_date[0] = '\0';
(void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink,
- NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
- NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
+ NAME_WIDTH, UT_NAMESIZE, user_from_uid(sbp->st_uid, 0),
+ NAME_WIDTH, UT_NAMESIZE, group_from_gid(sbp->st_gid, 0));
/*
* print device id's for devices, or sizes for other nodes
-/* $OpenBSD: options.c,v 1.97 2016/08/24 19:15:42 guenther Exp $ */
+/* $OpenBSD: options.c,v 1.98 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: options.c,v 1.6 1996/03/26 23:54:18 mrg Exp $ */
/*-
/* 5: POSIX USTAR */
{"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
- ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
+ ustar_rd, tar_endrd, no_op, ustar_wr, tar_endwr, tar_trail,
tar_opt},
#ifdef SMALL
-/* $OpenBSD: tar.c,v 1.61 2016/08/14 18:30:33 guenther Exp $ */
+/* $OpenBSD: tar.c,v 1.62 2016/08/25 01:44:55 guenther Exp $ */
/* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */
/*-
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
+#include <grp.h>
#include <limits.h>
-#include <string.h>
+#include <pwd.h>
#include <stdio.h>
-#include <unistd.h>
#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
#include "pax.h"
#include "extern.h"
#include "tar.h"
return(0);
}
-/*
- * ustar_stwr()
- * initialization for ustar write
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-ustar_stwr(void)
-{
- if ((uidtb_start() < 0) || (gidtb_start() < 0))
- return(-1);
- return(0);
-}
-
/*
* ustar_id()
* determine if a block given to us is a valid ustar header. We have to
ustar_wr(ARCHD *arcn)
{
HD_USTAR *hd;
- char *pt;
+ char *pt, *name;
char hdblk[sizeof(HD_USTAR)];
/*
ul_oct(arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3))
goto out;
if (!Nflag) {
- strncpy(hd->uname, name_uid(arcn->sb.st_uid, 0), sizeof(hd->uname));
- strncpy(hd->gname, name_gid(arcn->sb.st_gid, 0), sizeof(hd->gname));
- } else {
- strncpy(hd->uname, "", sizeof(hd->uname));
- strncpy(hd->gname, "", sizeof(hd->gname));
+ if ((name = user_from_uid(arcn->sb.st_uid, 1)) != NULL)
+ strncpy(hd->uname, name, sizeof(hd->uname));
+ if ((name = group_from_gid(arcn->sb.st_gid, 1)) != NULL)
+ strncpy(hd->gname, name, sizeof(hd->gname));
}
/*