Annotation of /trunk/mkinitrd-magellan/busybox/libbb/bb_pwd.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 2199 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 2199 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * password utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | niro | 984 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> |
7 | niro | 532 | * |
8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
9 | */ | ||
10 | |||
11 | #include "libbb.h" | ||
12 | |||
13 | niro | 984 | /* TODO: maybe change API to return malloced data? |
14 | * This will allow to stop using libc functions returning | ||
15 | * pointers to static data (getpwuid) | ||
16 | */ | ||
17 | niro | 532 | |
18 | niro | 984 | struct passwd* FAST_FUNC xgetpwnam(const char *name) |
19 | niro | 532 | { |
20 | niro | 984 | struct passwd *pw = getpwnam(name); |
21 | if (!pw) | ||
22 | bb_error_msg_and_die("unknown user %s", name); | ||
23 | return pw; | ||
24 | niro | 532 | } |
25 | |||
26 | niro | 984 | struct group* FAST_FUNC xgetgrnam(const char *name) |
27 | niro | 816 | { |
28 | niro | 984 | struct group *gr = getgrnam(name); |
29 | if (!gr) | ||
30 | bb_error_msg_and_die("unknown group %s", name); | ||
31 | return gr; | ||
32 | } | ||
33 | niro | 532 | |
34 | niro | 984 | |
35 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) | ||
36 | { | ||
37 | struct passwd *pw = getpwuid(uid); | ||
38 | if (!pw) | ||
39 | bb_error_msg_and_die("unknown uid %u", (unsigned)uid); | ||
40 | return pw; | ||
41 | niro | 816 | } |
42 | niro | 984 | |
43 | struct group* FAST_FUNC xgetgrgid(gid_t gid) | ||
44 | niro | 532 | { |
45 | niro | 984 | struct group *gr = getgrgid(gid); |
46 | if (!gr) | ||
47 | bb_error_msg_and_die("unknown gid %u", (unsigned)gid); | ||
48 | return gr; | ||
49 | } | ||
50 | niro | 532 | |
51 | niro | 984 | char* FAST_FUNC xuid2uname(uid_t uid) |
52 | { | ||
53 | struct passwd *pw = xgetpwuid(uid); | ||
54 | return pw->pw_name; | ||
55 | niro | 532 | } |
56 | |||
57 | niro | 984 | char* FAST_FUNC xgid2group(gid_t gid) |
58 | niro | 532 | { |
59 | niro | 984 | struct group *gr = xgetgrgid(gid); |
60 | return gr->gr_name; | ||
61 | } | ||
62 | niro | 532 | |
63 | niro | 984 | char* FAST_FUNC uid2uname(uid_t uid) |
64 | { | ||
65 | struct passwd *pw = getpwuid(uid); | ||
66 | return (pw) ? pw->pw_name : NULL; | ||
67 | } | ||
68 | niro | 532 | |
69 | niro | 984 | char* FAST_FUNC gid2group(gid_t gid) |
70 | { | ||
71 | struct group *gr = getgrgid(gid); | ||
72 | return (gr) ? gr->gr_name : NULL; | ||
73 | niro | 532 | } |
74 | |||
75 | niro | 984 | char* FAST_FUNC uid2uname_utoa(long uid) |
76 | { | ||
77 | char *name = uid2uname(uid); | ||
78 | return (name) ? name : utoa(uid); | ||
79 | } | ||
80 | |||
81 | char* FAST_FUNC gid2group_utoa(long gid) | ||
82 | { | ||
83 | char *name = gid2group(gid); | ||
84 | return (name) ? name : utoa(gid); | ||
85 | } | ||
86 | |||
87 | niro | 816 | long FAST_FUNC xuname2uid(const char *name) |
88 | niro | 532 | { |
89 | struct passwd *myuser; | ||
90 | |||
91 | niro | 984 | myuser = xgetpwnam(name); |
92 | niro | 532 | return myuser->pw_uid; |
93 | } | ||
94 | |||
95 | niro | 984 | long FAST_FUNC xgroup2gid(const char *name) |
96 | { | ||
97 | struct group *mygroup; | ||
98 | |||
99 | mygroup = xgetgrnam(name); | ||
100 | return mygroup->gr_gid; | ||
101 | } | ||
102 | |||
103 | niro | 816 | unsigned long FAST_FUNC get_ug_id(const char *s, |
104 | long FAST_FUNC (*xname2id)(const char *)) | ||
105 | niro | 532 | { |
106 | unsigned long r; | ||
107 | |||
108 | r = bb_strtoul(s, NULL, 10); | ||
109 | if (errno) | ||
110 | return xname2id(s); | ||
111 | return r; | ||
112 | } |