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.
29 #include <sys/types.h>
34 #define __OWN_USERDB_PARSER__
38 #define __OWN_STRLCPYCAT__
42 #define __OWN_USERDB_PARSER__
45 #ifdef __OWN_STRLCPYCAT__
46 size_t strlcpy(char *dst, const char *src, size_t size)
59 /* If size == 0 there is no space for a final null... */
66 size_t strlcat(char *dst, const char *src, size_t size)
73 while (bytes < size && *q) {
78 return (bytes + strlen(src));
89 #endif /* __OWN_STRLCPYCAT__ */
91 #ifndef __OWN_USERDB_PARSER__
92 #include <sys/types.h>
96 uid_t lookup_user(const char *user)
103 info("specified user unknown '%s'", user);
110 gid_t lookup_group(const char *group)
115 gr = getgrnam(group);
117 info("specified group unknown '%s'", group);
124 #else /* __OWN_USERDB_PARSER__ */
126 #define PASSWD_FILE "/etc/passwd"
127 #define GROUP_FILE "/etc/group"
129 /* return the id of a passwd style line, selected by the users name */
130 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
132 unsigned long id = 0;
133 char line[LINE_SIZE];
144 if (file_map(dbfile, &buf, &bufsize) != 0) {
145 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
148 dbg("search '%s' in '%s'", uname, dbfile);
150 /* loop through the whole file */
152 while (cur < bufsize) {
153 count = buf_get_line(buf, bufsize, cur);
157 if (count >= sizeof(line))
160 memcpy(line, bufline, count-1);
161 line[count-1] = '\0';
165 name = strsep(&pos, ":");
170 if (strsep(&pos, ":") == NULL)
174 idstr = strsep(&pos, ":");
178 if (strcmp(uname, name) == 0) {
179 id = strtoul(idstr, &tail, 10);
180 if (tail[0] != '\0') {
182 dbg("no id found for '%s'", name);
184 dbg("id for '%s' is '%li'", name, id);
189 file_unmap(buf, bufsize);
193 uid_t lookup_user(const char *user)
197 id = get_id_by_name(user, PASSWD_FILE);
201 gid_t lookup_group(const char *group)
205 id = get_id_by_name(group, GROUP_FILE);
208 #endif /* __OWN_USERDB_PARSER__ */