_utmp.c
author Tero Marttila <terom@fixme.fi>
Sun, 14 Sep 2008 23:02:19 +0300
changeset 20 1711f40a7c39
parent 7 6a49fc285842
permissions -rw-r--r--
misc. changes, can't remember /o\

committer: Tero Marttila <terom@fixme.fi>
7
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#include <Python.h>
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
#include <utmp.h>
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
#include <stdio.h>
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
PyObject *parse (PyObject *self, PyObject *args) {
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
    char *bytes;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
    size_t size;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
    struct utmp *item;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
    
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
    /* not unicode */
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
    if (!PyArg_ParseTuple(args, "t#", &bytes, &size))
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
        return NULL;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
    if (size != sizeof(struct utmp)) {
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
        PyErr_SetString(PyExc_ValueError, "given buffer is of the wrong length");
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
        return NULL;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
    }
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
    item = (struct utmp *) bytes;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
    
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
    /* parse utmp from bytes to result */
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    25
    return Py_BuildValue("hIs#s#s#s#(hh)i(ii)s#",
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    26
        item->ut_type, item->ut_pid,
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
        item->ut_line, sizeof(item->ut_line),
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
        item->ut_id, sizeof(item->ut_id),
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
        item->ut_user, sizeof(item->ut_user),
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
        item->ut_host, sizeof(item->ut_host),
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
        item->ut_exit.e_termination, item->ut_exit.e_exit,
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
        item->ut_session,
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
        item->ut_tv.tv_sec, item->ut_tv.tv_usec,
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
        item->ut_addr_v6, sizeof(item->ut_addr_v6)
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
    );
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
}
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
PyObject *size (PyObject *self, PyObject *args) {
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
    /* return the size of an UTMP struct */
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
    if (!PyArg_ParseTuple(args, ""))
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
        return NULL;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
    return PyInt_FromSsize_t(sizeof(struct utmp));
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
}
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
static PyMethodDef module_methods[] = {
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
    {"parse",       parse,      METH_VARARGS,   "parse a utmp struct from a byte string"},
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
    {"size",        size,       METH_VARARGS,   "return the size of an utmp record in bytes"},
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
    {NULL}
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
};
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
PyMODINIT_FUNC init_utmp(void) {
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
    PyObject *m;
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
    m = Py_InitModule3("_utmp", module_methods, "utmp struct parsing");
6a49fc285842 C module to handle utmp entires
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
}