summaryrefslogtreecommitdiff
path: root/tools/user_group.c
blob: bcfb600cc50e989916aad47d5dd3ae1dd0d75138 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
  File: user_group.c
  (Linux Access Control List Management)

  Copyright (C) 1999, 2000
  Andreas Gruenbacher, <andreas.gruenbacher@gmail.com>
 	
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at
  your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  USA.
*/

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include "user_group.h"


const char *
user_name(uid_t uid, int numeric)
{
	struct passwd *passwd = numeric ? NULL : getpwuid(uid);
	static char uid_str[22];
	int ret;

	if (passwd != NULL)
		return passwd->pw_name;
	ret = snprintf(uid_str, sizeof(uid_str), "%ld", (long)uid);
	if (ret < 1 || (size_t)ret >= sizeof(uid_str))
		return "?";
	return uid_str;
}


const char *
group_name(gid_t gid, int numeric)
{
	struct group *group = numeric ? NULL : getgrgid(gid);
	static char gid_str[22];
	int ret;

	if (group != NULL)
		return group->gr_name;
	ret = snprintf(gid_str, sizeof(gid_str), "%ld", (long)gid);
	if (ret < 1 || (size_t)ret >= sizeof(gid_str))
		return "?";
	return gid_str;
}