2 * udev_sysdeps.c - wrapping of libc features and kernel defines
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 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)
103 err("specified user unknown '%s'", user);
105 err("error resolving user '%s': %s", user, strerror(errno));
112 gid_t lookup_group(const char *group)
117 gr = getgrnam(group);
120 err("specified group unknown '%s'", group);
122 err("error resolving group '%s': %s", group, strerror(errno));
129 #else /* __OWN_USERDB_PARSER__ */
131 #define PASSWD_FILE "/etc/passwd"
132 #define GROUP_FILE "/etc/group"
134 /* return the id of a passwd style line, selected by the users name */
135 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
137 unsigned long id = 0;
138 char line[LINE_SIZE];
149 if (file_map(dbfile, &buf, &bufsize) != 0) {
150 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
153 dbg("search '%s' in '%s'", uname, dbfile);
155 /* loop through the whole file */
157 while (cur < bufsize) {
158 count = buf_get_line(buf, bufsize, cur);
162 if (count >= sizeof(line))
165 memcpy(line, bufline, count-1);
166 line[count-1] = '\0';
170 name = strsep(&pos, ":");
175 if (strsep(&pos, ":") == NULL)
179 idstr = strsep(&pos, ":");
183 if (strcmp(uname, name) == 0) {
184 id = strtoul(idstr, &tail, 10);
185 if (tail[0] != '\0') {
187 dbg("no id found for '%s'", name);
189 dbg("id for '%s' is '%li'", name, id);
194 file_unmap(buf, bufsize);
198 uid_t lookup_user(const char *user)
202 id = get_id_by_name(user, PASSWD_FILE);
206 gid_t lookup_group(const char *group)
210 id = get_id_by_name(group, GROUP_FILE);
213 #endif /* __OWN_USERDB_PARSER__ */