Files
kitty/kitty/utmp.c
Kovid Goyal edac0e8023 Check for utmpx.h instead of __unix__
There is no reliable macro to test for UNIX
2021-10-05 11:58:09 +05:30

35 lines
874 B
C

#include "data-types.h"
#if __has_include(<utmpx.h>)
#include <utmpx.h>
static PyObject*
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
size_t users = 0;
struct utmpx *ut;
Py_BEGIN_ALLOW_THREADS
setutxent();
while ((ut = getutxent())) {
if (ut->ut_type == USER_PROCESS) users++;
}
endutxent();
Py_END_ALLOW_THREADS
return PyLong_FromSize_t(users);
}
#else
static PyObject*
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
PyErr_SetString(PyExc_RuntimeError, "Counting the number of users is not supported");
return NULL;
}
#endif
static PyMethodDef methods[] = {
{"num_users", num_users, METH_NOARGS, "Get the number of users using UTMP data" },
{ NULL, NULL, 0, NULL },
};
bool
init_utmp(PyObject *module) {
return PyModule_AddFunctions(module, methods) == 0;
}