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"
36 #define __OWN_USERDB_PARSER__
40 #define __OWN_STRLCPYCAT__
44 #define __OWN_USERDB_PARSER__
47 #ifdef __OWN_STRLCPYCAT__
48 size_t strlcpy(char *dst, const char *src, size_t size)
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 dbg("specified user unknown '%s'", user);
109 gid_t lookup_group(const char *group)
114 gr = getgrnam(group);
116 dbg("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 dbg("can't open '%s' as db file", dbfile);
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 strlcpy(line, bufline, count);
163 name = strsep(&pos, ":");
168 if (strsep(&pos, ":") == NULL)
172 idstr = strsep(&pos, ":");
176 if (strcmp(uname, name) == 0) {
177 id = strtoul(idstr, &tail, 10);
178 if (tail[0] != '\0') {
180 dbg("no id found for '%s'", name);
182 dbg("id for '%s' is '%li'", name, id);
187 file_unmap(buf, bufsize);
191 uid_t lookup_user(const char *user)
195 id = get_id_by_name(user, PASSWD_FILE);
199 gid_t lookup_group(const char *group)
203 id = get_id_by_name(group, GROUP_FILE);
206 #endif /* __OWN_USERDB_PARSER__ */