htop/UsersTable.c

47 lines
1.0 KiB
C
Raw Permalink Normal View History

2006-03-04 18:16:49 +00:00
/*
htop - UsersTable.c
2011-05-26 16:35:07 +00:00
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPLv2, see the COPYING file
2006-03-04 18:16:49 +00:00
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "UsersTable.h"
2006-03-04 18:16:49 +00:00
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
2011-12-26 21:35:57 +00:00
#include <stdlib.h>
2006-03-04 18:16:49 +00:00
2020-10-14 18:21:09 +00:00
#include "XUtils.h"
2006-03-04 18:16:49 +00:00
UsersTable* UsersTable_new() {
UsersTable* this;
2016-02-02 14:53:02 +00:00
this = xMalloc(sizeof(UsersTable));
this->users = Hashtable_new(10, true);
2006-03-04 18:16:49 +00:00
return this;
}
void UsersTable_delete(UsersTable* this) {
Hashtable_delete(this->users);
free(this);
}
char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
char* name = Hashtable_get(this->users, uid);
2006-03-04 18:16:49 +00:00
if (name == NULL) {
const struct passwd* userData = getpwuid(uid);
2006-03-04 18:16:49 +00:00
if (userData != NULL) {
2016-02-02 14:53:02 +00:00
name = xStrdup(userData->pw_name);
2006-03-04 18:16:49 +00:00
Hashtable_put(this->users, uid, name);
}
}
return name;
}
inline void UsersTable_foreach(UsersTable* this, Hashtable_PairFunction f, void* userData) {
Hashtable_foreach(this->users, f, userData);
}