chiark / gitweb /
[PATCH] udev - man page update
[elogind.git] / klibc_fixups.c
1 /*
2  * klibc_fixups.c - very simple implementation of stuff missing in klibc
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6  *
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.
10  * 
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.
15  * 
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.
19  *
20  */
21
22 #ifdef __KLIBC__
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29
30 #include "klibc_fixups.h"
31 #include "logging.h"
32
33 #define PW_FILE         "/etc/passwd"
34 #define GR_FILE         "/etc/group"
35
36 /* return the id of a passwd style line, selected by the users name */
37 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
38 {
39         unsigned long id = -1;
40         FILE *file;
41         char buf[255];
42         char *pos;
43         char *name;
44         char *idstr;
45         char *tail;
46
47         file = fopen(dbfile, "r");
48         if (file == NULL) {
49                 dbg("unable to open file '%s'", dbfile);
50                 return -1;
51         }
52
53         while (1) {
54                 pos = fgets(buf, sizeof(buf), file);
55                 if (pos == NULL)
56                         break;
57
58                 /* get name */
59                 name = strsep(&pos, ":");
60                 if (name == NULL)
61                         continue;
62
63                 /* skip pass */
64                 if (strsep(&pos, ":") == NULL)
65                         continue;
66
67                 /* get id */
68                 idstr = strsep(&pos, ":");
69                 if (idstr == NULL)
70                         continue;
71
72                 if (strcmp(uname, name) == 0) {
73                         id = strtoul(idstr, &tail, 10);
74                         if (tail == NULL)
75                                 id = -1;
76                         else
77                                 dbg("id for '%s' is '%li'", name, id);
78                         break;
79                 }
80         }
81
82         fclose(file);
83         return id;
84 }
85
86 struct passwd *getpwnam(const char *name)
87 {
88         static struct passwd pw;
89
90         memset(&pw, 0x00, sizeof(struct passwd));
91         pw.pw_uid = (uid_t) get_id_by_name(name, PW_FILE);
92         if (pw.pw_uid < 0)
93                 return NULL;
94         else
95                 return &pw;
96 }
97
98 struct group *getgrnam(const char *name)
99 {
100         static struct group gr;
101
102         memset(&gr, 0x00, sizeof(struct group));
103         gr.gr_gid = (gid_t) get_id_by_name(name, GR_FILE);
104         if (gr.gr_gid < 0)
105                 return NULL;
106         else
107                 return &gr;
108 }
109
110 #endif