1 /*
2 File: user_group.c
3 (Linux Access Control List Management)
4
5 Copyright (C) 1999, 2000
6 Andreas Gruenbacher, <andreas.gruenbacher@gmail.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 USA.
22 */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "user_group.h"
28
29
30 const char *
31 user_name(uid_t uid, int numeric)
32 {
33 struct passwd *passwd = numeric ? NULL : getpwuid(uid);
34 static char uid_str[22];
35 int ret;
36
37 if (passwd != NULL)
38 return passwd->pw_name;
39 ret = snprintf(uid_str, sizeof(uid_str), "%ld", (long)uid);
40 if (ret < 1 || (size_t)ret >= sizeof(uid_str))
41 return "?";
42 return uid_str;
43 }
44
45
46 const char *
47 group_name(gid_t gid, int numeric)
48 {
49 struct group *group = numeric ? NULL : getgrgid(gid);
50 static char gid_str[22];
51 int ret;
52
53 if (group != NULL)
54 return group->gr_name;
55 ret = snprintf(gid_str, sizeof(gid_str), "%ld", (long)gid);
56 if (ret < 1 || (size_t)ret >= sizeof(gid_str))
57 return "?";
58 return gid_str;
59 }
60