blob: 21be21a5c9f2cb1b6791dc19a935ad2841749739 [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;
93 long handle;
94
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
102 return PyInt_FromLong(handle);
103}
104
105/* Console I/O */
106#include <conio.h>
107
108static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args)
109{
110 int ok;
111
112 if (!PyArg_ParseTuple(args, ":kbhit"))
113 return NULL;
114
115 ok = _kbhit();
116 return PyInt_FromLong(ok);
117}
118
119static PyObject *msvcrt_getch(PyObject *self, PyObject *args)
120{
121 int ch;
122 char s[1];
123
124 if (!PyArg_ParseTuple(args, ":getch"))
125 return NULL;
126
Guido van Rossume4e021b1998-05-29 01:27:07 +0000127 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000128 ch = _getch();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000129 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000130 s[0] = ch;
131 return PyString_FromStringAndSize(s, 1);
132}
133
134static PyObject *msvcrt_getche(PyObject *self, PyObject *args)
135{
136 int ch;
137 char s[1];
138
139 if (!PyArg_ParseTuple(args, ":getche"))
140 return NULL;
141
Guido van Rossume4e021b1998-05-29 01:27:07 +0000142 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000143 ch = _getche();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000144 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000145 s[0] = ch;
146 return PyString_FromStringAndSize(s, 1);
147}
148
149static PyObject *msvcrt_putch(PyObject *self, PyObject *args)
150{
151 char ch;
152
153 if (!PyArg_ParseTuple(args, "c:putch", &ch))
154 return NULL;
155
156 _putch(ch);
157 Py_INCREF(Py_None);
158 return Py_None;
159}
160
161static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
162{
163 char ch;
164
165 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
166 return NULL;
167
Guido van Rossum6543e881999-02-16 19:40:02 +0000168 if (_ungetch(ch) == EOF)
169 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000170 Py_INCREF(Py_None);
171 return Py_None;
172}
173
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000174
175/* List of functions exported by this module */
176static struct PyMethodDef msvcrt_functions[] = {
Guido van Rossum407a22d1997-08-13 19:57:53 +0000177 {"heapmin", msvcrt_heapmin, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000178 {"locking", msvcrt_locking, 1},
Guido van Rossum407a22d1997-08-13 19:57:53 +0000179 {"setmode", msvcrt_setmode, 1},
180 {"open_osfhandle", msvcrt_open_osfhandle, 1},
181 {"get_osfhandle", msvcrt_get_osfhandle, 1},
182 {"kbhit", msvcrt_kbhit, 1},
183 {"getch", msvcrt_getch, 1},
184 {"getche", msvcrt_getche, 1},
185 {"putch", msvcrt_putch, 1},
186 {"ungetch", msvcrt_ungetch, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000187 {NULL, NULL}
188};
189
190__declspec(dllexport) void
191initmsvcrt(void)
192{
193 Py_InitModule("msvcrt", msvcrt_functions);
194}