3 * $Id: name.c,v 1.1 1997/07/21 13:47:46 mdw Exp $
5 * Looking up of names in symbol tables
10 /*----- Licencing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1997/07/21 13:47:46 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
45 /* --- Unix headers --- */
50 /* --- Local headers --- */
58 /*----- Static variables --------------------------------------------------*/
60 static sym_table name__table
; /* Symbol table for everything */
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @name_init@ --- *
70 * Use: Initialises the name table. Requires the user database to
71 * be populated (see @userdb_local@ and @userdb_yp@).
76 /* --- Initialise the name table --- */
78 sym_createTable(&name__table
);
80 /* --- Insert all the users and groups into the table --- */
86 userdb_iterateUsers();
87 while ((pw
= userdb_nextUser()) != 0) {
92 /* --- First, add the user to the table --- */
94 n
= sym_find(&name__table
, pw
->pw_name
, -1, sizeof(name
), &f
);
96 sym_table
*t
= xmalloc(sizeof(*t
));
98 n
->c
= class_create(clType_user
, t
);
101 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
103 /* --- Now handle the user's default group --- */
105 if ((gr
= userdb_groupById(pw
->pw_gid
)) != 0) {
106 n
= sym_find(&name__table
, gr
->gr_name
, -1, sizeof(name
), &f
);
108 sym_table
*t
= xmalloc(sizeof(*t
));
110 n
->c
= class_create(clType_user
, t
);
112 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
117 /* --- Now get the subsidiary groups --- */
124 userdb_iterateGroups();
125 while ((gr
= userdb_nextGroup()) != 0) {
130 n
= sym_find(&name__table
, gr
->gr_name
, -1, sizeof(name
), &f
);
132 sym_table
*t
= xmalloc(sizeof(*t
));
134 n
->c
= class_create(clType_user
, t
);
137 for (p
= gr
->gr_mem
; *p
; p
++) {
138 if ((pw
= userdb_userByName(*p
)) != 0) {
140 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
146 /* --- Finally add in the `all' class --- *
148 * Do that now, to prevent it being overwritten by the above.
155 n
= sym_find(&name__table
, "all", -1, sizeof(name
), &f
);
162 /* --- @name_reinit@ --- *
168 * Use: Reinitialises the names table. It's cleared and then
169 * initialised with the current user and group ids as for
173 void name_reinit(void)
175 /* --- Empty the symbol table --- */
181 for (sym_createIter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
187 /* --- Destroy and recreate the table --- */
189 sym_destroyTable(&name__table
);
193 /* --- @name_find@ --- *
195 * Arguments: @const char *p@ = pointer to name to look up
196 * @unsigned create@ = whether to create the item
197 * @unsigned *f@ = whether the item was created
199 * Returns: Pointer to a @name@ block containing the symbol, or
200 * zero if it wasn't found and we didn't want to create a
203 * Use: Looks up a name in the symbol table and returns the
207 name
*name_find(const char *p
, unsigned create
, unsigned *f
)
209 /* --- This is a trivial veneer onto @sym_find@ --- */
211 return (sym_find(&name__table
, p
, -1, create ?
sizeof(name
) : 0, f
));
214 /* --- @name_dump@ --- *
216 * Arguments: @FILE *fp@ = stream to dump on
220 * Use: Dumps a complete listing of the symbol table.
223 void name_dump(FILE *fp
)
228 for (sym_createIter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
229 fprintf(fp
, "\n--- name `%s'\n", n
->base
.name
);
230 class_dump(n
->c
, fp
);
234 /*----- Test driver -------------------------------------------------------*/