blob: 3c19ce77678362e5a68bc5fa325025d265191ac4 [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;
42
43 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
Guido van Rossum29c1ea51997-08-07 00:11:34 +000044 return NULL;
45
Guido van Rossum407a22d1997-08-13 19:57:53 +000046 if (_locking(fd, mode, nbytes) != 0)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000047 return PyErr_SetFromErrno(PyExc_IOError);
48
Guido van Rossum407a22d1997-08-13 19:57:53 +000049 Py_INCREF(Py_None);
50 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000051}
Guido van Rossum407a22d1997-08-13 19:57:53 +000052
53// Set the file translation mode for a C runtime file descriptor.
54static PyObject *msvcrt_setmode(PyObject *self, PyObject *args)
55{
56 int fd;
57 int flags;
58 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
59 return NULL;
60
61 flags = _setmode(fd, flags);
62 if (flags == -1)
63 return PyErr_SetFromErrno(PyExc_IOError);
64
65 return PyInt_FromLong(flags);
66}
67
68// Convert an OS file handle to a C runtime file descriptor.
69static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
70{
71 long handle;
72 int flags;
73 int fd;
74
75 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
76 return PyErr_SetFromErrno(PyExc_IOError);
77
78 fd = _open_osfhandle(handle, flags);
79 if (fd == -1)
80 return PyErr_SetFromErrno(PyExc_IOError);
81
82 return PyInt_FromLong(fd);
83}
84
85// Convert a C runtime file descriptor to an OS file handle.
86static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
87{
88 int fd;
89 long handle;
90
91 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
92 return NULL;
93
94 handle = _get_osfhandle(fd);
95 if (handle == -1)
96 return PyErr_SetFromErrno(PyExc_IOError);
97
98 return PyInt_FromLong(handle);
99}
100
101/* Console I/O */
102#include <conio.h>
103
104static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args)
105{
106 int ok;
107
108 if (!PyArg_ParseTuple(args, ":kbhit"))
109 return NULL;
110
111 ok = _kbhit();
112 return PyInt_FromLong(ok);
113}
114
115static PyObject *msvcrt_getch(PyObject *self, PyObject *args)
116{
117 int ch;
118 char s[1];
119
120 if (!PyArg_ParseTuple(args, ":getch"))
121 return NULL;
122
123 ch = _getch();
124 s[0] = ch;
125 return PyString_FromStringAndSize(s, 1);
126}
127
128static PyObject *msvcrt_getche(PyObject *self, PyObject *args)
129{
130 int ch;
131 char s[1];
132
133 if (!PyArg_ParseTuple(args, ":getche"))
134 return NULL;
135
136 ch = _getche();
137 s[0] = ch;
138 return PyString_FromStringAndSize(s, 1);
139}
140
141static PyObject *msvcrt_putch(PyObject *self, PyObject *args)
142{
143 char ch;
144
145 if (!PyArg_ParseTuple(args, "c:putch", &ch))
146 return NULL;
147
148 _putch(ch);
149 Py_INCREF(Py_None);
150 return Py_None;
151}
152
153static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
154{
155 char ch;
156
157 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
158 return NULL;
159
160 _ungetch(ch);
161 Py_INCREF(Py_None);
162 return Py_None;
163}
164
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000165
166/* List of functions exported by this module */
167static struct PyMethodDef msvcrt_functions[] = {
Guido van Rossum407a22d1997-08-13 19:57:53 +0000168 {"heapmin", msvcrt_heapmin, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000169 {"locking", msvcrt_locking, 1},
Guido van Rossum407a22d1997-08-13 19:57:53 +0000170 {"setmode", msvcrt_setmode, 1},
171 {"open_osfhandle", msvcrt_open_osfhandle, 1},
172 {"get_osfhandle", msvcrt_get_osfhandle, 1},
173 {"kbhit", msvcrt_kbhit, 1},
174 {"getch", msvcrt_getch, 1},
175 {"getche", msvcrt_getche, 1},
176 {"putch", msvcrt_putch, 1},
177 {"ungetch", msvcrt_ungetch, 1},
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000178 {NULL, NULL}
179};
180
181__declspec(dllexport) void
182initmsvcrt(void)
183{
184 Py_InitModule("msvcrt", msvcrt_functions);
185}