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
|
2021-09-22 09:33:00 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2020-09-19 18:22:34 +00:00
|
|
|
#include "UsersTable.h"
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <pwd.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#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"
|
2020-09-19 18:22:34 +00:00
|
|
|
|
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));
|
2020-10-22 13:43:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-04-05 19:53:23 +00:00
|
|
|
char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
|
2020-10-27 10:46:29 +00:00
|
|
|
char* name = Hashtable_get(this->users, uid);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (name == NULL) {
|
2020-10-27 10:46:29 +00:00
|
|
|
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);
|
|
|
|
}
|