]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/idcache.c
2 * No copyright is claimed. This code is in the public domain; do with
5 * Written by Karel Zak <kzak@redhat.com>
10 #include <sys/types.h>
15 struct identry
*get_id(struct idcache
*ic
, unsigned long int id
)
22 for (ent
= ic
->ent
; ent
; ent
= ent
->next
) {
30 struct idcache
*new_idcache(void)
32 return calloc(1, sizeof(struct idcache
));
35 void free_idcache(struct idcache
*ic
)
37 struct identry
*ent
= ic
->ent
;
40 struct identry
*next
= ent
->next
;
49 static void add_id(struct idcache
*ic
, char *name
, unsigned long int id
)
51 struct identry
*ent
, *x
;
54 ent
= calloc(1, sizeof(struct identry
));
61 wchar_t wc
[LOGIN_NAME_MAX
+ 1];
63 if (mbstowcs(wc
, name
, LOGIN_NAME_MAX
) > 0) {
64 wc
[LOGIN_NAME_MAX
] = '\0';
65 w
= wcswidth(wc
, LOGIN_NAME_MAX
);
72 /* note, we ignore names with non-printable widechars */
74 ent
->name
= strdup(name
);
80 if (asprintf(&ent
->name
, "%lu", id
) < 0) {
86 for (x
= ic
->ent
; x
&& x
->next
; x
= x
->next
);
94 w
= ent
->name
? strlen(ent
->name
) : 0;
95 ic
->width
= ic
->width
< w
? w
: ic
->width
;
99 void add_uid(struct idcache
*cache
, unsigned long int id
)
101 struct identry
*ent
= get_id(cache
, id
);
104 struct passwd
*pw
= getpwuid((uid_t
) id
);
105 add_id(cache
, pw
? pw
->pw_name
: NULL
, id
);
109 void add_gid(struct idcache
*cache
, unsigned long int id
)
111 struct identry
*ent
= get_id(cache
, id
);
114 struct group
*gr
= getgrgid((gid_t
) id
);
115 add_id(cache
, gr
? gr
->gr_name
: NULL
, id
);