blob: 613e173c8460cfd2c3a7fc9c076697bb09f45eeb [file] [log] [blame]
Guido van Rossum29c1ea51997-08-07 00:11:34 +00001/*********************************************************
2
3 msvcrtmodule.c
4
5 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but
7 still useful routines.
8
9 Only ever compiled with an MS compiler, so no attempt
10 has been made to avoid MS language extensions, etc...
11
Guido van Rossum407a22d1997-08-13 19:57:53 +000012 This may only work on NT or 95...
13
14 Author: Mark Hammond and Guido van Rossum.
15 Maintenance: Guido van Rossum.
16
Guido van Rossum29c1ea51997-08-07 00:11:34 +000017***********************************************************/
Guido van Rossum407a22d1997-08-13 19:57:53 +000018
Guido van Rossum29c1ea51997-08-07 00:11:34 +000019#include "Python.h"
20#include "malloc.h"
Guido van Rossum29c1ea51997-08-07 00:11:34 +000021
Guido van Rossum407a22d1997-08-13 19:57:53 +000022// Force the malloc heap to clean itself up, and free unused blocks
23// back to the OS. (According to the docs, only works on NT.)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000024static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args)
25{
Guido van Rossum407a22d1997-08-13 19:57:53 +000026 if (!PyArg_ParseTuple(args, ":heapmin"))
Guido van Rossum29c1ea51997-08-07 00:11:34 +000027 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000028
29 if (_heapmin() != 0)
30 return PyErr_SetFromErrno(PyExc_IOError);
31
Guido van Rossum29c1ea51997-08-07 00:11:34 +000032 Py_INCREF(Py_None);
33 return Py_None;
34}
35
Guido van Rossum407a22d1997-08-13 19:57:53 +000036// Perform locking operations on a C runtime file descriptor.
37static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000038{
Guido van Rossum407a22d1997-08-13 19:57:53 +000039 int fd;
40 int mode;
41 long nbytes;
Guido van Rossume4e021b1998-05-29 01:27:07 +000042 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +000043
44 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
Guido van Rossum29c1ea51997-08-07 00:11:34 +000045 return NULL;
46
Guido van Rossume4e021b1998-05-29 01:27:07 +000047 Py_BEGIN_ALLOW_THREADS
48 err = _locking(fd, mode, nbytes);
49 Py_END_ALLOW_THREADS
50 if (err != 0)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000051 return PyErr_SetFromErrno(PyExc_IOError);
52
Guido van Rossum407a22d1997-08-13 19:57:53 +000053 Py_INCREF(Py_None);
54 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000055}
Guido van Rossum407a22d1997-08-13 19:57:53 +000056
57// Set the file translation mode for a C runtime file descriptor.
58static PyObject *msvcrt_setmode(PyObject *self, PyObject *args)
59{
60 int fd;
61 int flags;
62 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
63 return NULL;
64
65 flags = _setmode(fd, flags);
66 if (flags == -1)
67 return PyErr_SetFromErrno(PyExc_IOError);
68
69 return PyInt_FromLong(flags);
70}
71
72// Convert an OS file handle to a C runtime file descriptor.
73static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
74{
75 long handle;
76 int flags;
77 int fd;
78
79 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
Guido van Rossume4e021b1998-05-29 01:27:07 +000080 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000081
82 fd = _open_osfhandle(handle, flags);
83 if (fd == -1)
84 return PyErr_SetFromErrno(PyExc_IOError);
85
86 return PyInt_FromLong(fd);
87}
88
89// Convert a C runtime file descriptor to an OS file handle.
90static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
91{
92 int fd;
Fred Drake25e17262000-06-30 17:48:51 +000093 intptr_t handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +000094
95 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
96 return NULL;
97
98 handle = _get_osfhandle(fd);
99 if (handle == -1)
100 return PyErr_SetFromErrno(PyExc_IOError);
101
Fred Drake25e17262000-06-30 17:48:51 +0000102 /* technically 'handle' is not a pointer, but a integer as
103 large as a pointer, Python's *VoidPtr interface is the
104 most appropriate here */
105 return PyLong_FromVoidPtr((void*)handle);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000106}
107
108/* Console I/O */
109#include <conio.h>
110
111static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args)
112{
113 int ok;
114
115 if (!PyArg_ParseTuple(args, ":kbhit"))
116 return NULL;
117
118 ok = _kbhit();
119 return PyInt_FromLong(ok);
120}
121
122static PyObject *msvcrt_getch(PyObject *self, PyObject *args)
123{
124 int ch;
125 char s[1];
126
127 if (!PyArg_ParseTuple(args, ":getch"))
128 return NULL;
129
Guido van Rossume4e021b1998-05-29 01:27:07 +0000130 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000131 ch = _getch();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000132 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000133 s[0] = ch;
134 return PyString_FromStringAndSize(s, 1);
135}
136
137static PyObject *msvcrt_getche(PyObject *self, PyObject *args)
138{
139 int ch;
140 char s[1];
141
142 if (!PyArg_ParseTuple(args, ":getche"))
143 return NULL;
144
Guido van Rossume4e021b1998-05-29 01:27:07 +0000145 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000146 ch = _getche();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000147 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000148 s[0] = ch;
149 return PyString_FromStringAndSize(s, 1);
150}
151
152static PyObject *msvcrt_putch(PyObject *self, PyObject *args)
153{
154 char ch;
155
156 if (!PyArg_ParseTuple(args, "c:putch", &ch))
157 return NULL;
158
159 _putch(ch);
160 Py_INCREF(Py_None);
161 return Py_None;
162}
163
164static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
165{
166 char ch;
167
168 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
169 return NULL;
170
Guido van Rossum6543e881999-02-16 19:40:02 +0000171 if (_ungetch(ch) == EOF)
172 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000173 Py_INCREF(Py_None);
174 return Py_None;
175}
176
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000177
178/* List of functions exported by this module */
179static struct PyMethodDef msvcrt_functions[] = {
Guido van Rossum407a22d1997-08-13 19:57:53 +0000180 {"heapmin", msvcrt_heapmin, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000181 {"locking", msvcrt_locking, 1},
Guido van Rossum407a22d1997-08-13 19:57:53 +0000182 {"setmode", msvcrt_setmode, 1},
183 {"open_osfhandle", msvcrt_open_osfhandle, 1},
184 {"get_osfhandle", msvcrt_get_osfhandle, 1},
185 {"kbhit", msvcrt_kbhit, 1},
186 {"getch", msvcrt_getch, 1},
187 {"getche", msvcrt_getche, 1},
188 {"putch", msvcrt_putch, 1},
189 {"ungetch", msvcrt_ungetch, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000190 {NULL, NULL}
191};
192
193__declspec(dllexport) void
194initmsvcrt(void)
195{
196 Py_InitModule("msvcrt", msvcrt_functions);
197}