/* -*-c-*-
*
- * $Id: name.c,v 1.3 1997/08/07 09:49:39 mdw Exp $
+ * $Id: name.c,v 1.6 1998/01/12 16:46:14 mdw Exp $
*
* Looking up of names in symbol tables
*
- * (c) 1997 EBI
+ * (c) 1998 EBI
*/
/*----- Licensing notice --------------------------------------------------*
/*----- Revision history --------------------------------------------------*
*
* $Log: name.c,v $
+ * Revision 1.6 1998/01/12 16:46:14 mdw
+ * Fix copyright date.
+ *
+ * Revision 1.5 1997/09/17 10:26:11 mdw
+ * Use rewritten class handler. Support `none' class.
+ *
+ * Revision 1.4 1997/08/20 16:17:59 mdw
+ * Replace `name_reinit' by `name_end' for more sensible restart.
+ *
* Revision 1.3 1997/08/07 09:49:39 mdw
* Extensive modifications to handle netgroups. Also sanitise user and group
* names before adding them to the symbol table.
{
unsigned f;
name *n = sym_find(&name__table, p, -1, sizeof(*n), &f);
- if (!f) {
- sym_table *t = xmalloc(sizeof(*t));
- sym_createTable(t);
- n->c = class_create(type, t);
- }
- return (n->c->type == type ? n : 0);
+ if (!f)
+ n->c = 0;
+ return ((n->c && !(n->c->type & type)) ? 0 : n);
}
/* --- @name__sanitise@ --- *
userdb_iterateUsers();
while ((pw = userdb_nextUser()) != 0) {
name *n;
- int u = pw->pw_uid;
-
- /* --- Make the name into something nice --- */
+ uid_t u = pw->pw_uid;
/* --- First, add the user to the table --- */
if (name__sanitise(pw->pw_name, buf, sizeof(buf)) &&
(n = name__get(buf, clType_user)) != 0)
- sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
+ n->c = class_addUser(n->c, u);
/* --- Now handle the user's default group --- */
if ((gr = userdb_groupById(pw->pw_gid)) != 0 &&
name__sanitise(gr->gr_name, buf, sizeof(buf)) &&
(n = name__get(buf, clType_user)) != 0)
- sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
+ n->c = class_addUser(n->c, u);
}
}
userdb_iterateGroups();
while ((gr = userdb_nextGroup()) != 0) {
name *n;
- int u;
if (name__sanitise(gr->gr_name, buf, sizeof(buf)) &&
(n = name__get(buf, clType_user)) != 0) {
/* --- Now add all of the members --- */
for (p = gr->gr_mem; *p; p++) {
- if ((pw = userdb_userByName(*p)) != 0) {
- u = pw->pw_uid;
- sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
- }
+ if ((pw = userdb_userByName(*p)) != 0)
+ n->c = class_addUser(n->c, pw->pw_uid);
}
}
}
memcpy(&in, h->h_addr, sizeof(in));
if ((a = inet_ntoa(in)) == 0)
goto done_host;
- sym_find(sc->h->c->t, a, -1, sizeof(sym_base), 0);
+ sc->h->c = class_addString(sc->h->c, a);
done_host:;
}
if (sc->f & f_user && user) {
struct passwd *pw;
- int u;
/* --- First ensure that I have a user class --- */
if ((pw = userdb_userByName(user)) == 0)
goto done_user;
- u = pw->pw_uid;
- sym_find(sc->u->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
+ sc->u->c = class_addUser(sc->u->c, pw->pw_uid);
done_user:;
}
name__groups();
name__netgroups();
- /* --- Finally add in the `all' class --- *
+ /* --- Finally add in the `all' and `none' classes --- *
*
- * Do that now, to prevent it being overwritten by the above.
+ * Do that now, to prevent them being overwritten by the above.
*/
{
if (f)
class_dec(n->c);
n->c = class_all;
+
+ n = sym_find(&name__table, "none", -1, sizeof(name), &f);
+ if (f)
+ class_dec(n->c);
+ n->c = class_none;
}
}
-/* --- @name_reinit@ --- *
+/* --- @name_end@ --- *
*
* Arguments: ---
*
* Returns: ---
*
- * Use: Reinitialises the names table. It's cleared and then
- * initialised with the current user and group ids as for
- * @name_init@ above.
+ * Use: Closes down the name database, so that it can be
+ * reinitialised.
*/
-void name_reinit(void)
+void name_end(void)
{
/* --- Empty the symbol table --- */
/* --- Destroy and recreate the table --- */
sym_destroyTable(&name__table);
- name_init();
}
/* --- @name_find@ --- *
void name_dump(void)
{
+#ifdef TRACING
sym_iter i;
name *n;
trace(TRACE_DEBUG, "name: dumping names");
for (sym_createIter(&i, &name__table); (n = sym_next(&i)) != 0; ) {
trace(TRACE_DEBUG, "name: dumping `%s'", n->base.name);
- class_dump(n->c);
+ class_dump(n->c, 1);
}
+#endif
}
/*----- Test driver -------------------------------------------------------*/
int main(void)
{
ego("name-test");
- traceon(stdout, TRACE_ALL);
+ /* traceon(stdout, TRACE_ALL); */
userdb_init();
userdb_local();
userdb_yp();
netg_init();
name_init();
- name_dump();
+ printf("loaded (%lu)\n", track_memused());
+ getchar();
+ for (;;) {
+ name_end();
+ netg_end();
+ userdb_end();
+ printf("cleared (%lu)\n", track_memused());
+ track_memlist();
+ userdb_init();
+ userdb_local();
+ userdb_yp();
+ netg_init();
+ name_init();
+ printf("reloaded (%lu)\n", track_memused());
+ getchar();
+ }
return (0);
}