source: branches/3.2/mindi-busybox/libbb/bb_pwd.c @ 3232

Last change on this file since 3232 was 3232, checked in by bruno, 5 years ago
  • Update mindi-busybox to 1.21.1
File size: 2.1 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * password utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 */
10
11#include "libbb.h"
12
13/* 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
18struct passwd* FAST_FUNC xgetpwnam(const char *name)
19{
20    struct passwd *pw = getpwnam(name);
21    if (!pw)
22        bb_error_msg_and_die("unknown user %s", name);
23    return pw;
24}
25
26struct group* FAST_FUNC xgetgrnam(const char *name)
27{
28    struct group *gr = getgrnam(name);
29    if (!gr)
30        bb_error_msg_and_die("unknown group %s", name);
31    return gr;
32}
33
34
35struct 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}
42
43struct group* FAST_FUNC xgetgrgid(gid_t gid)
44{
45    struct group *gr = getgrgid(gid);
46    if (!gr)
47        bb_error_msg_and_die("unknown gid %u", (unsigned)gid);
48    return gr;
49}
50
51char* FAST_FUNC xuid2uname(uid_t uid)
52{
53    struct passwd *pw = xgetpwuid(uid);
54    return pw->pw_name;
55}
56
57char* FAST_FUNC xgid2group(gid_t gid)
58{
59    struct group *gr = xgetgrgid(gid);
60    return gr->gr_name;
61}
62
63char* FAST_FUNC uid2uname(uid_t uid)
64{
65    struct passwd *pw = getpwuid(uid);
66    return (pw) ? pw->pw_name : NULL;
67}
68
69char* FAST_FUNC gid2group(gid_t gid)
70{
71    struct group *gr = getgrgid(gid);
72    return (gr) ? gr->gr_name : NULL;
73}
74
75char* FAST_FUNC uid2uname_utoa(uid_t uid)
76{
77    char *name = uid2uname(uid);
78    return (name) ? name : utoa(uid);
79}
80
81char* FAST_FUNC gid2group_utoa(gid_t gid)
82{
83    char *name = gid2group(gid);
84    return (name) ? name : utoa(gid);
85}
86
87long FAST_FUNC xuname2uid(const char *name)
88{
89    struct passwd *myuser;
90
91    myuser = xgetpwnam(name);
92    return myuser->pw_uid;
93}
94
95long FAST_FUNC xgroup2gid(const char *name)
96{
97    struct group *mygroup;
98
99    mygroup = xgetgrnam(name);
100    return mygroup->gr_gid;
101}
102
103unsigned long FAST_FUNC get_ug_id(const char *s,
104        long FAST_FUNC (*xname2id)(const char *))
105{
106    unsigned long r;
107
108    r = bb_strtoul(s, NULL, 10);
109    if (errno)
110        return xname2id(s);
111    return r;
112}
Note: See TracBrowser for help on using the repository browser.