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>
33 #define __OWN_USERDB_PARSER__
37 #define __OWN_STRLCPYCAT__
41 #define __OWN_USERDB_PARSER__
44 #ifdef __OWN_STRLCPYCAT__
45 size_t strlcpy(char *dst, const char *src, size_t size)
58 /* If size == 0 there is no space for a final null... */
65 size_t strlcat(char *dst, const char *src, size_t size)
72 while (bytes < size && *q) {
77 return (bytes + strlen(src));
88 #endif /* __OWN_STRLCPYCAT__ */
90 #ifndef __OWN_USERDB_PARSER__
91 #include <sys/types.h>
95 uid_t lookup_user(const char *user)
102 info("specified user unknown '%s'", user);
109 gid_t lookup_group(const char *group)
114 gr = getgrnam(group);
116 info("specified group unknown '%s'", group);
123 #else /* __OWN_USERDB_PARSER__ */
125 #define PASSWD_FILE "/etc/passwd"
126 #define GROUP_FILE "/etc/group"
128 /* return the id of a passwd style line, selected by the users name */
129 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
131 unsigned long id = 0;
132 char line[LINE_SIZE];
143 if (file_map(dbfile, &buf, &bufsize) != 0) {
144 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
147 dbg("search '%s' in '%s'", uname, dbfile);
149 /* loop through the whole file */
151 while (cur < bufsize) {
152 count = buf_get_line(buf, bufsize, cur);
156 if (count >= sizeof(line))
159 memcpy(line, bufline, count-1);
160 line[count-1] = '\0';
164 name = strsep(&pos, ":");
169 if (strsep(&pos, ":") == NULL)
173 idstr = strsep(&pos, ":");
177 if (strcmp(uname, name) == 0) {
178 id = strtoul(idstr, &tail, 10);
179 if (tail[0] != '\0') {
181 dbg("no id found for '%s'", name);
183 dbg("id for '%s' is '%li'", name, id);
188 file_unmap(buf, bufsize);
192 uid_t lookup_user(const char *user)
196 id = get_id_by_name(user, PASSWD_FILE);
200 gid_t lookup_group(const char *group)
204 id = get_id_by_name(group, GROUP_FILE);
207 #endif /* __OWN_USERDB_PARSER__ */