-
Notifications
You must be signed in to change notification settings - Fork 2
/
passwd.cc
47 lines (40 loc) · 1012 Bytes
/
passwd.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) 2010-2013 David Caldwell <[email protected]>
// Licenced under the GPL 3.0 or any later version. See LICENSE file for details.
#include "passwd.h"
#include "strprintf.h"
#include <string>
#include <pwd.h>
#include <grp.h>
using namespace std;
int uid_from_name(string name)
{
struct passwd *p = getpwnam(name.c_str());
if (!p) return -1;
return p->pw_uid;
}
string name_from_uid(int uid)
{
struct passwd *p = getpwuid(uid);
if (!p) return strprintf("%d", uid);
return string(p->pw_name);
}
string name_from_gid(int gid)
{
struct group *g = getgrgid(gid);
if (!g) return strprintf("%d", gid);
return string(g->gr_name);
}
pwent::pwent(std::string user) : valid(0)
{
struct passwd *p = getpwnam(user.c_str());
if (!p)
return;
valid = true;
name = p->pw_name ;
passwd = p->pw_passwd;
uid = p->pw_uid ;
gid = p->pw_gid ;
gecos = p->pw_gecos ;
dir = p->pw_dir ;
shell = p->pw_shell ;
}