int gid_name(char *name, gid_t *gid) { struct group *gr; GIDC *ptr; int namelen; /* * return -1 for mangled names */ if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) return(-1); if ((grptb == NULL) && (grptb_start() < 0)) return(-1); /* * 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)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) return(-1); *gid = ptr->gid; return(0); } if (!gropn) { setgroupent(1); ++gropn; } if (ptr == NULL) ptr = grptb[st_hash(name, namelen, GID_SZ)] = (GIDC *)malloc(sizeof(GIDC)); /* * no match, look it up, if no match store it as an invalid entry, * or store the matching gid */ if (ptr == NULL) { if ((gr = getgrnam(name)) == NULL) return(-1); *gid = gr->gr_gid; return(0); } (void)strncpy(ptr->name, name, GNMLEN - 1); ptr->name[GNMLEN-1] = '\0'; if ((gr = getgrnam(name)) == NULL) { ptr->valid = INVALID; return(-1); } ptr->valid = VALID; *gid = ptr->gid = gr->gr_gid; return(0); }
int uid_name(char *name, uid_t *uid) { struct passwd *pw; UIDC *ptr; int namelen; /* * return -1 for mangled names */ if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) return(-1); if ((usrtb == NULL) && (usrtb_start() < 0)) return(-1); /* * look up in hash table, if found and valid return the uid, * if found and invalid, return a -1 */ ptr = usrtb[st_hash(name, namelen, UNM_SZ)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) return(-1); *uid = ptr->uid; return(0); } if (!pwopn) { setpassent(1); ++pwopn; } if (ptr == NULL) ptr = usrtb[st_hash(name, namelen, UNM_SZ)] = (UIDC *)malloc(sizeof(UIDC)); /* * no match, look it up, if no match store it as an invalid entry, * or store the matching uid */ if (ptr == NULL) { if ((pw = getpwnam(name)) == NULL) return(-1); *uid = pw->pw_uid; return(0); } (void)strncpy(ptr->name, name, UNMLEN - 1); ptr->name[UNMLEN-1] = '\0'; if ((pw = getpwnam(name)) == NULL) { ptr->valid = INVALID; return(-1); } ptr->valid = VALID; *uid = ptr->uid = pw->pw_uid; return(0); }
void sub_name(char *oname, int *onamelen, size_t onamesize) { NAMT *pt; u_int indx; if (ntab == NULL) return; /* * look the name up in the hash table */ indx = st_hash(oname, *onamelen, N_TAB_SZ); if ((pt = ntab[indx]) == NULL) return; while (pt != NULL) { /* * walk down the hash chain looking for a match */ if (strcmp(oname, pt->oname) == 0) { /* * found it, replace it with the new name * and return (we know that oname has enough space) */ *onamelen = strlcpy(oname, pt->nname, onamesize); return; } pt = pt->fow; } /* * no match, just return */ return; }
int add_name(char *oname, int onamelen, char *nname) { NAMT *pt; u_int indx; if (ntab == NULL) { /* * should never happen */ tty_warn(0, "No interactive rename table, links may fail\n"); return 0; } /* * look to see if we have already mapped this file, if so we * will update it */ indx = st_hash(oname, onamelen, N_TAB_SZ); if ((pt = ntab[indx]) != NULL) { /* * look down the has chain for the file */ while ((pt != NULL) && (strcmp(oname, pt->oname) != 0)) pt = pt->fow; if (pt != NULL) { /* * found an old mapping, replace it with the new one * the user just input (if it is different) */ if (strcmp(nname, pt->nname) == 0) return 0; (void)free((char *)pt->nname); if ((pt->nname = strdup(nname)) == NULL) { tty_warn(1, "Cannot update rename table"); return -1; } return 0; } } /* * this is a new mapping, add it to the table */ if ((pt = (NAMT *)malloc(sizeof(NAMT))) != NULL) { if ((pt->oname = strdup(oname)) != NULL) { if ((pt->nname = strdup(nname)) != NULL) { pt->fow = ntab[indx]; ntab[indx] = pt; return 0; } (void)free((char *)pt->oname); } (void)free((char *)pt); } tty_warn(1, "Interactive rename table out of memory"); return -1; }
int chk_ftime(ARCHD *arcn) { FTM *pt; int namelen; u_int indx; char ckname[PAXPATHLEN+1]; /* * no info, go ahead and add to archive */ if (ftab == NULL) return 0; /* * hash the pathname and look up in table */ namelen = arcn->nlen; indx = st_hash(arcn->name, namelen, F_TAB_SZ); if ((pt = ftab[indx]) != NULL) { /* * the hash chain is not empty, walk down looking for match * only read up the path names if the lengths match, speeds * up the search a lot */ while (pt != NULL) { if (pt->namelen == namelen) { /* * potential match, have to read the name * from the scratch file. */ if (lseek(ffd,pt->seek,SEEK_SET) != pt->seek) { syswarn(1, errno, "Failed ftime table seek"); return -1; } if (xread(ffd, ckname, namelen) != namelen) { syswarn(1, errno, "Failed ftime table read"); return -1; } /* * if the names match, we are done */ if (!strncmp(ckname, arcn->name, namelen)) break; } /* * try the next entry on the chain */ pt = pt->fow; } if (pt != NULL) { /* * found the file, compare the times, save the newer */ if (arcn->sb.st_mtime > pt->mtime) { /* * file is newer */ pt->mtime = arcn->sb.st_mtime; return 0; } /* * file is older */ return 1; } } /* * not in table, add it */ if ((pt = (FTM *)malloc(sizeof(FTM))) != NULL) { /* * add the name at the end of the scratch file, saving the * offset. add the file to the head of the hash chain */ if ((pt->seek = lseek(ffd, (off_t)0, SEEK_END)) >= 0) { if (xwrite(ffd, arcn->name, namelen) == namelen) { pt->mtime = arcn->sb.st_mtime; pt->namelen = namelen; pt->fow = ftab[indx]; ftab[indx] = pt; return 0; } syswarn(1, errno, "Failed write to file time table"); } else syswarn(1, errno, "Failed seek on file time table"); } else tty_warn(1, "File time table ran out of memory"); if (pt != NULL) (void)free((char *)pt); return -1; }
static st_data_t feature_key(const char *str, size_t len) { return st_hash(str, len, 0xfea7009e); }