aboutsummaryrefslogtreecommitdiffstats
path: root/tools/python/ptsname/ptsname.c
blob: 3849bb8867358e3f4663d8180326687fe05b518c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/******************************************************************************
 * ptsname.c
 * 
 * A python extension to expose the POSIX ptsname() function.
 * 
 * Copyright (C) 2007 XenSource Ltd
 */

#include <Python.h>
#include <stdlib.h>

/* Needed for Python versions earlier than 2.3. */
#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC DL_EXPORT(void)
#endif

static PyObject *do_ptsname(PyObject *self, PyObject *args)
{
    int fd;
    char *path;

    if (!PyArg_ParseTuple(args, "i", &fd))
        return NULL;

    path = ptsname(fd);

    if (!path)
    {
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    } 

    return PyString_FromString(path);
}

static PyMethodDef ptsname_methods[] = { 
    { "ptsname", do_ptsname, METH_VARARGS }, 
    { NULL }
};

PyMODINIT_FUNC initptsname(void)
{
    Py_InitModule("ptsname", ptsname_methods);
}