]>
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
)
45 struct identry
*next
= ent
->next
;
54 static void add_id(struct idcache
*ic
, char *name
, unsigned long int id
)
56 struct identry
*ent
, *x
;
62 ent
= calloc(1, sizeof(struct identry
));
69 wchar_t wc
[LOGIN_NAME_MAX
+ 1];
71 if (mbstowcs(wc
, name
, LOGIN_NAME_MAX
) > 0) {
72 wc
[LOGIN_NAME_MAX
] = '\0';
73 w
= wcswidth(wc
, LOGIN_NAME_MAX
);
80 /* note, we ignore names with non-printable widechars */
82 ent
->name
= strdup(name
);
88 if (asprintf(&ent
->name
, "%lu", id
) < 0) {
94 for (x
= ic
->ent
; x
&& x
->next
; x
= x
->next
);
102 w
= ent
->name
? strlen(ent
->name
) : 0;
103 ic
->width
= ic
->width
< w
? w
: ic
->width
;
106 void add_uid(struct idcache
*cache
, unsigned long int id
)
108 struct identry
*ent
= get_id(cache
, id
);
111 struct passwd
*pw
= getpwuid((uid_t
) id
);
112 add_id(cache
, pw
? pw
->pw_name
: NULL
, id
);
116 void add_gid(struct idcache
*cache
, unsigned long int id
)
118 struct identry
*ent
= get_id(cache
, id
);
121 struct group
*gr
= getgrgid((gid_t
) id
);
122 add_id(cache
, gr
? gr
->gr_name
: NULL
, id
);