2 * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2001 , Michał Moskal
5 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
6 * Copyright (c) 2007 - 2009, Nicolas François
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the copyright holders or contributors may not be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ident "$Id: pwio.c 2783 2009-04-23 21:19:02Z nekral-guest $"
38 #include "prototypes.h"
45 static /*@null@*/ /*@only@*/void *passwd_dup (const void *ent)
47 const struct passwd *pw = ent;
52 static void passwd_free (/*@out@*/ /*@only@*/void *ent)
54 struct passwd *pw = ent;
59 static const char *passwd_getname (const void *ent)
61 const struct passwd *pw = ent;
66 static void *passwd_parse (const char *line)
68 return (void *) sgetpwent (line);
71 static int passwd_put (const void *ent, FILE * file)
73 const struct passwd *pw = ent;
75 return (putpwent (pw, file) == -1) ? -1 : 0;
78 static struct commonio_ops passwd_ops = {
90 static struct commonio_db passwd_db = {
91 PASSWD_FILE, /* filename */
92 &passwd_ops, /* ops */
106 int pw_setdbname (const char *filename)
108 return commonio_setname (&passwd_db, filename);
111 /*@observer@*/const char *pw_dbname (void)
113 return passwd_db.filename;
118 return commonio_lock (&passwd_db);
121 int pw_open (int mode)
123 return commonio_open (&passwd_db, mode);
126 /*@observer@*/ /*@null@*/const struct passwd *pw_locate (const char *name)
128 return commonio_locate (&passwd_db, name);
131 /*@observer@*/ /*@null@*/const struct passwd *pw_locate_uid (uid_t uid)
133 const struct passwd *pwd;
136 while ( ((pwd = pw_next ()) != NULL)
137 && (pwd->pw_uid != uid)) {
143 int pw_update (const struct passwd *pw)
145 return commonio_update (&passwd_db, (const void *) pw);
148 int pw_remove (const char *name)
150 return commonio_remove (&passwd_db, name);
155 return commonio_rewind (&passwd_db);
158 /*@observer@*/ /*@null@*/const struct passwd *pw_next (void)
160 return commonio_next (&passwd_db);
165 return commonio_close (&passwd_db);
170 return commonio_unlock (&passwd_db);
173 /*@null@*/struct commonio_entry *__pw_get_head (void)
175 return passwd_db.head;
178 void __pw_del_entry (const struct commonio_entry *ent)
180 commonio_del_entry (&passwd_db, ent);
183 struct commonio_db *__pw_get_db (void)
188 static int pw_cmp (const void *p1, const void *p2)
192 if ((*(struct commonio_entry **) p1)->eptr == NULL)
194 if ((*(struct commonio_entry **) p2)->eptr == NULL)
197 u1 = ((struct passwd *) (*(struct commonio_entry **) p1)->eptr)->pw_uid;
198 u2 = ((struct passwd *) (*(struct commonio_entry **) p2)->eptr)->pw_uid;
208 /* Sort entries by UID */
211 return commonio_sort (&passwd_db, pw_cmp);