2 * udev_libc_wrapper - wrapping of functions missing in a specific libc
3 * or not working in a statically compiled binary
5 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/types.h>
30 #include "../udev_libc_wrapper.h"
32 #include "../udev_utils.h"
33 #include "../logging.h"
36 #define __OWN_USERDB_PARSER__
40 #define __OWN_USERDB_PARSER__
44 size_t strlcpy(char *dst
, const char *src
, size_t size
)
63 size_t strlcat(char *dst
, const char *src
, size_t size
)
70 while (bytes
< size
&& *q
) {
86 #ifndef __OWN_USERDB_PARSER__
87 #include <sys/types.h>
91 uid_t
lookup_user(const char *user
)
98 dbg("specified user unknown '%s'", user
);
105 gid_t
lookup_group(const char *group
)
110 gr
= getgrnam(group
);
112 dbg("specified group unknown '%s'", group
);
119 #else /* __OWN_USERDB_PARSER__ */
121 #define PASSWD_FILE "/etc/passwd"
122 #define GROUP_FILE "/etc/group"
124 /* return the id of a passwd style line, selected by the users name */
125 static unsigned long get_id_by_name(const char *uname
, const char *dbfile
)
127 unsigned long id
= 0;
128 char line
[LINE_SIZE
];
139 if (file_map(dbfile
, &buf
, &bufsize
) != 0) {
140 dbg("can't open '%s' as db file", dbfile
);
143 dbg("reading '%s' as db file", dbfile
);
145 /* loop through the whole file */
147 while (cur
< bufsize
) {
148 count
= buf_get_line(buf
, bufsize
, cur
);
152 if (count
>= sizeof(line
))
155 strlcpy(line
, bufline
, count
);
159 name
= strsep(&pos
, ":");
164 if (strsep(&pos
, ":") == NULL
)
168 idstr
= strsep(&pos
, ":");
172 if (strcmp(uname
, name
) == 0) {
173 id
= strtoul(idstr
, &tail
, 10);
174 if (tail
[0] != '\0') {
176 dbg("no id found for '%s'", name
);
178 dbg("id for '%s' is '%li'", name
, id
);
183 file_unmap(buf
, bufsize
);
187 uid_t
lookup_user(const char *user
)
191 id
= get_id_by_name(user
, PASSWD_FILE
);
195 gid_t
lookup_group(const char *group
)
199 id
= get_id_by_name(group
, GROUP_FILE
);
202 #endif /* __OWN_USERDB_PARSER__ */