mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-17 14:04:52 +02:00
Clean up previous PR
This commit is contained in:
51
kitty/utmp.c
51
kitty/utmp.c
@@ -1,50 +1,35 @@
|
||||
#if defined(__unix__)
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <Python.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <utmp.h>
|
||||
#include "data-types.h"
|
||||
#ifdef __unix__
|
||||
#include <utmpx.h>
|
||||
|
||||
static PyObject*
|
||||
num_users(PyObject *const self, PyObject *const args) {
|
||||
(void)self; (void)args;
|
||||
num_users(PyObject *const self UNUSED, PyObject *const args UNUSED) {
|
||||
size_t users = 0;
|
||||
struct utmpx *ut;
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
#ifdef UTENT_REENTRANT
|
||||
struct utmp *result = NULL;
|
||||
struct utmp buffer = { 0, };
|
||||
while (true) {
|
||||
if (getutent_r(&buffer, &result) == -1) {
|
||||
Py_BLOCK_THREADS
|
||||
return PyErr_SetFromErrno(PyExc_OSError);
|
||||
}
|
||||
if (result == NULL) { break; }
|
||||
if (result->ut_type == USER_PROCESS) { users++; }
|
||||
setutxent();
|
||||
while ((ut = getutxent())) {
|
||||
if (ut->ut_type == USER_PROCESS) users++;
|
||||
}
|
||||
#else
|
||||
struct utmp *ut;
|
||||
setutent();
|
||||
while ((ut = getutent())) {
|
||||
if (ut->ut_type == USER_PROCESS) {
|
||||
users++;
|
||||
}
|
||||
}
|
||||
endutent();
|
||||
#endif
|
||||
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 UtmpMethods[] = {
|
||||
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) {
|
||||
// 0 = success
|
||||
return PyModule_AddFunctions(module, UtmpMethods) == 0;
|
||||
return PyModule_AddFunctions(module, methods) == 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user