[go: up one dir, main page]

File: names.c

package info (click to toggle)
rdup 1.1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,324 kB
  • sloc: ansic: 3,840; sh: 3,361; exp: 271; makefile: 78; ruby: 36; perl: 4
file content (119 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (2)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * lookup the user/group names associated with the uid/gid
 * Use hashes to speed things up
 */

#include <glib.h>
#include "config.h"
#include <pwd.h>
#include <grp.h>

#include <stdio.h>

/* lookup the uid belonging to username
 * if the uid is not found, return uid_given
 * otherwise return the uid belonging to the username
 * ON THIS SYSTEM
 */
uid_t
lookup_uid(GHashTable *u, gchar *user, uid_t uid_given)
{
	uid_t uid, *uid_tmp;
	struct passwd *p;

	if (uid_given == 0)
		return 0;

	uid_tmp = (uid_t*)g_hash_table_lookup(u, user);
	if (uid_tmp)
		return *uid_tmp;

	p = getpwnam(user);
	if (!p) /* user does not exist on this system */
		return uid_given;

	uid = p->pw_uid;
	uid_tmp = g_malloc(sizeof(uid_t));
	*uid_tmp = uid;
	g_hash_table_insert(u, g_strdup(user), (gpointer)uid_tmp);

	uid_tmp = (uid_t *)g_hash_table_lookup(u, user);

	return *uid_tmp;
}

/* see lookup_uid, but now for groups */
gid_t
lookup_gid(GHashTable *g, gchar *group, gid_t gid_given)
{
	gid_t gid, *gid_tmp;
	struct group *p;

	if (gid_given == 0)
		return 0;

	gid_tmp = (gid_t*)g_hash_table_lookup(g, group);
	if (gid_tmp)
		return *gid_tmp;

	p = getgrnam(group);
	if (!p) /* grp does not exist on this system */
		return gid_given;

	gid = p->gr_gid;
	gid_tmp = g_malloc(sizeof(gid_t));
	*gid_tmp = gid;

	g_hash_table_insert(g, g_strdup(group), (gpointer)gid_tmp);
	
	gid_tmp = (gid_t *)g_hash_table_lookup(g, group);

	return *gid_tmp;
}

gchar *
lookup_user(GHashTable *u, uid_t uid)
{
	gchar *n;
	struct passwd *p;

	n = (gchar *)g_hash_table_lookup(u, (gpointer)&uid);
	if (n)
		return n;

	/* if nothing found also add to hash? */
	p = getpwuid(uid);
	if (!p) /* user only has ID */
		return NULL;

	/* don't return the string as it might be overwritten in
	 * subsequent calls to getpwnam. Use the pointer stored
	 * in the hash. This is also the case for getgrgid()
	 */
	n = g_strdup(p->pw_name);
	uid_t* uid_tmp = g_malloc(sizeof(uid_t));
        *uid_tmp = uid;
	g_hash_table_insert(u, (gpointer)uid_tmp, n);
	return (gchar *)g_hash_table_lookup(u, (gpointer)&uid);
}

gchar *
lookup_group(GHashTable *g, gid_t gid)
{
	gchar *n;
	struct group *p;

	n = (gchar *)g_hash_table_lookup(g, (gpointer)&gid);
	if (n)
		return n;

	p = getgrgid(gid);
	if (!p) /* group only has ID */
		return NULL;

	n = g_strdup(p->gr_name);
	gid_t* gid_tmp = g_malloc(sizeof(gid_t));
	*gid_tmp = gid;
	g_hash_table_insert(g, (gpointer)gid_tmp, n);
	return (gchar *)g_hash_table_lookup(g, (gpointer)&gid);
}