Fix a refleak introduced by r66677.
[python.git] / Modules / posixmodule.c
blob79c3a4437d1e89b02c3a684c67d47076d50a0170
2 /* POSIX module implementation */
4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
14 /* See also ../Dos/dosmodule.c */
16 #ifdef __APPLE__
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
22 # pragma weak lchown
23 # pragma weak statvfs
24 # pragma weak fstatvfs
26 #endif /* __APPLE__ */
28 #define PY_SSIZE_T_CLEAN
30 #include "Python.h"
31 #include "structseq.h"
33 #if defined(__VMS)
34 # include <unixio.h>
35 #endif /* defined(__VMS) */
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
41 PyDoc_STRVAR(posix__doc__,
42 "This module provides access to operating system functionality that is\n\
43 standardized by the C Standard and the POSIX standard (a thinly\n\
44 disguised Unix interface). Refer to the library manual and\n\
45 corresponding Unix manual entries for more information on calls.");
47 #ifndef Py_USING_UNICODE
48 /* This is used in signatures of functions. */
49 #define Py_UNICODE void
50 #endif
52 #if defined(PYOS_OS2)
53 #define INCL_DOS
54 #define INCL_DOSERRORS
55 #define INCL_DOSPROCESS
56 #define INCL_NOPMAPI
57 #include <os2.h>
58 #if defined(PYCC_GCC)
59 #include <ctype.h>
60 #include <io.h>
61 #include <stdio.h>
62 #include <process.h>
63 #endif
64 #include "osdefs.h"
65 #endif
67 #ifdef HAVE_SYS_TYPES_H
68 #include <sys/types.h>
69 #endif /* HAVE_SYS_TYPES_H */
71 #ifdef HAVE_SYS_STAT_H
72 #include <sys/stat.h>
73 #endif /* HAVE_SYS_STAT_H */
75 #ifdef HAVE_SYS_WAIT_H
76 #include <sys/wait.h> /* For WNOHANG */
77 #endif
79 #ifdef HAVE_SIGNAL_H
80 #include <signal.h>
81 #endif
83 #ifdef HAVE_FCNTL_H
84 #include <fcntl.h>
85 #endif /* HAVE_FCNTL_H */
87 #ifdef HAVE_GRP_H
88 #include <grp.h>
89 #endif
91 #ifdef HAVE_SYSEXITS_H
92 #include <sysexits.h>
93 #endif /* HAVE_SYSEXITS_H */
95 #ifdef HAVE_SYS_LOADAVG_H
96 #include <sys/loadavg.h>
97 #endif
99 /* Various compilers have only certain posix functions */
100 /* XXX Gosh I wish these were all moved into pyconfig.h */
101 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
102 #include <process.h>
103 #else
104 #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
105 #define HAVE_GETCWD 1
106 #define HAVE_OPENDIR 1
107 #define HAVE_SYSTEM 1
108 #if defined(__OS2__)
109 #define HAVE_EXECV 1
110 #define HAVE_WAIT 1
111 #endif
112 #include <process.h>
113 #else
114 #ifdef __BORLANDC__ /* Borland compiler */
115 #define HAVE_EXECV 1
116 #define HAVE_GETCWD 1
117 #define HAVE_OPENDIR 1
118 #define HAVE_PIPE 1
119 #define HAVE_POPEN 1
120 #define HAVE_SYSTEM 1
121 #define HAVE_WAIT 1
122 #else
123 #ifdef _MSC_VER /* Microsoft compiler */
124 #define HAVE_GETCWD 1
125 #define HAVE_SPAWNV 1
126 #define HAVE_EXECV 1
127 #define HAVE_PIPE 1
128 #define HAVE_POPEN 1
129 #define HAVE_SYSTEM 1
130 #define HAVE_CWAIT 1
131 #define HAVE_FSYNC 1
132 #define fsync _commit
133 #else
134 #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135 /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
136 #else /* all other compilers */
137 /* Unix functions that the configure script doesn't check for */
138 #define HAVE_EXECV 1
139 #define HAVE_FORK 1
140 #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141 #define HAVE_FORK1 1
142 #endif
143 #define HAVE_GETCWD 1
144 #define HAVE_GETEGID 1
145 #define HAVE_GETEUID 1
146 #define HAVE_GETGID 1
147 #define HAVE_GETPPID 1
148 #define HAVE_GETUID 1
149 #define HAVE_KILL 1
150 #define HAVE_OPENDIR 1
151 #define HAVE_PIPE 1
152 #ifndef __rtems__
153 #define HAVE_POPEN 1
154 #endif
155 #define HAVE_SYSTEM 1
156 #define HAVE_WAIT 1
157 #define HAVE_TTYNAME 1
158 #endif /* PYOS_OS2 && PYCC_GCC && __VMS */
159 #endif /* _MSC_VER */
160 #endif /* __BORLANDC__ */
161 #endif /* ! __WATCOMC__ || __QNX__ */
162 #endif /* ! __IBMC__ */
164 #ifndef _MSC_VER
166 #if defined(__sgi)&&_COMPILER_VERSION>=700
167 /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169 extern char *ctermid_r(char *);
170 #endif
172 #ifndef HAVE_UNISTD_H
173 #if defined(PYCC_VACPP)
174 extern int mkdir(char *);
175 #else
176 #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
177 extern int mkdir(const char *);
178 #else
179 extern int mkdir(const char *, mode_t);
180 #endif
181 #endif
182 #if defined(__IBMC__) || defined(__IBMCPP__)
183 extern int chdir(char *);
184 extern int rmdir(char *);
185 #else
186 extern int chdir(const char *);
187 extern int rmdir(const char *);
188 #endif
189 #ifdef __BORLANDC__
190 extern int chmod(const char *, int);
191 #else
192 extern int chmod(const char *, mode_t);
193 #endif
194 /*#ifdef HAVE_FCHMOD
195 extern int fchmod(int, mode_t);
196 #endif*/
197 /*#ifdef HAVE_LCHMOD
198 extern int lchmod(const char *, mode_t);
199 #endif*/
200 extern int chown(const char *, uid_t, gid_t);
201 extern char *getcwd(char *, int);
202 extern char *strerror(int);
203 extern int link(const char *, const char *);
204 extern int rename(const char *, const char *);
205 extern int stat(const char *, struct stat *);
206 extern int unlink(const char *);
207 extern int pclose(FILE *);
208 #ifdef HAVE_SYMLINK
209 extern int symlink(const char *, const char *);
210 #endif /* HAVE_SYMLINK */
211 #ifdef HAVE_LSTAT
212 extern int lstat(const char *, struct stat *);
213 #endif /* HAVE_LSTAT */
214 #endif /* !HAVE_UNISTD_H */
216 #endif /* !_MSC_VER */
218 #ifdef HAVE_UTIME_H
219 #include <utime.h>
220 #endif /* HAVE_UTIME_H */
222 #ifdef HAVE_SYS_UTIME_H
223 #include <sys/utime.h>
224 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
225 #endif /* HAVE_SYS_UTIME_H */
227 #ifdef HAVE_SYS_TIMES_H
228 #include <sys/times.h>
229 #endif /* HAVE_SYS_TIMES_H */
231 #ifdef HAVE_SYS_PARAM_H
232 #include <sys/param.h>
233 #endif /* HAVE_SYS_PARAM_H */
235 #ifdef HAVE_SYS_UTSNAME_H
236 #include <sys/utsname.h>
237 #endif /* HAVE_SYS_UTSNAME_H */
239 #ifdef HAVE_DIRENT_H
240 #include <dirent.h>
241 #define NAMLEN(dirent) strlen((dirent)->d_name)
242 #else
243 #if defined(__WATCOMC__) && !defined(__QNX__)
244 #include <direct.h>
245 #define NAMLEN(dirent) strlen((dirent)->d_name)
246 #else
247 #define dirent direct
248 #define NAMLEN(dirent) (dirent)->d_namlen
249 #endif
250 #ifdef HAVE_SYS_NDIR_H
251 #include <sys/ndir.h>
252 #endif
253 #ifdef HAVE_SYS_DIR_H
254 #include <sys/dir.h>
255 #endif
256 #ifdef HAVE_NDIR_H
257 #include <ndir.h>
258 #endif
259 #endif
261 #ifdef _MSC_VER
262 #ifdef HAVE_DIRECT_H
263 #include <direct.h>
264 #endif
265 #ifdef HAVE_IO_H
266 #include <io.h>
267 #endif
268 #ifdef HAVE_PROCESS_H
269 #include <process.h>
270 #endif
271 #include "osdefs.h"
272 #include <windows.h>
273 #include <shellapi.h> /* for ShellExecute() */
274 #define popen _popen
275 #define pclose _pclose
276 #endif /* _MSC_VER */
278 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
279 #include <io.h>
280 #endif /* OS2 */
282 #ifndef MAXPATHLEN
283 #if defined(PATH_MAX) && PATH_MAX > 1024
284 #define MAXPATHLEN PATH_MAX
285 #else
286 #define MAXPATHLEN 1024
287 #endif
288 #endif /* MAXPATHLEN */
290 #ifdef UNION_WAIT
291 /* Emulate some macros on systems that have a union instead of macros */
293 #ifndef WIFEXITED
294 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
295 #endif
297 #ifndef WEXITSTATUS
298 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
299 #endif
301 #ifndef WTERMSIG
302 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
303 #endif
305 #define WAIT_TYPE union wait
306 #define WAIT_STATUS_INT(s) (s.w_status)
308 #else /* !UNION_WAIT */
309 #define WAIT_TYPE int
310 #define WAIT_STATUS_INT(s) (s)
311 #endif /* UNION_WAIT */
313 /* Don't use the "_r" form if we don't need it (also, won't have a
314 prototype for it, at least on Solaris -- maybe others as well?). */
315 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
316 #define USE_CTERMID_R
317 #endif
319 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
320 #define USE_TMPNAM_R
321 #endif
323 /* choose the appropriate stat and fstat functions and return structs */
324 #undef STAT
325 #if defined(MS_WIN64) || defined(MS_WINDOWS)
326 # define STAT win32_stat
327 # define FSTAT win32_fstat
328 # define STRUCT_STAT struct win32_stat
329 #else
330 # define STAT stat
331 # define FSTAT fstat
332 # define STRUCT_STAT struct stat
333 #endif
335 #if defined(MAJOR_IN_MKDEV)
336 #include <sys/mkdev.h>
337 #else
338 #if defined(MAJOR_IN_SYSMACROS)
339 #include <sys/sysmacros.h>
340 #endif
341 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
342 #include <sys/mkdev.h>
343 #endif
344 #endif
346 /* Return a dictionary corresponding to the POSIX environment table */
347 #ifdef WITH_NEXT_FRAMEWORK
348 /* On Darwin/MacOSX a shared library or framework has no access to
349 ** environ directly, we must obtain it with _NSGetEnviron().
351 #include <crt_externs.h>
352 static char **environ;
353 #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
354 extern char **environ;
355 #endif /* !_MSC_VER */
357 static PyObject *
358 convertenviron(void)
360 PyObject *d;
361 char **e;
362 d = PyDict_New();
363 if (d == NULL)
364 return NULL;
365 #ifdef WITH_NEXT_FRAMEWORK
366 if (environ == NULL)
367 environ = *_NSGetEnviron();
368 #endif
369 if (environ == NULL)
370 return d;
371 /* This part ignores errors */
372 for (e = environ; *e != NULL; e++) {
373 PyObject *k;
374 PyObject *v;
375 char *p = strchr(*e, '=');
376 if (p == NULL)
377 continue;
378 k = PyString_FromStringAndSize(*e, (int)(p-*e));
379 if (k == NULL) {
380 PyErr_Clear();
381 continue;
383 v = PyString_FromString(p+1);
384 if (v == NULL) {
385 PyErr_Clear();
386 Py_DECREF(k);
387 continue;
389 if (PyDict_GetItem(d, k) == NULL) {
390 if (PyDict_SetItem(d, k, v) != 0)
391 PyErr_Clear();
393 Py_DECREF(k);
394 Py_DECREF(v);
396 #if defined(PYOS_OS2)
398 APIRET rc;
399 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
401 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
402 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
403 PyObject *v = PyString_FromString(buffer);
404 PyDict_SetItemString(d, "BEGINLIBPATH", v);
405 Py_DECREF(v);
407 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
408 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
409 PyObject *v = PyString_FromString(buffer);
410 PyDict_SetItemString(d, "ENDLIBPATH", v);
411 Py_DECREF(v);
414 #endif
415 return d;
419 /* Set a POSIX-specific error from errno, and return NULL */
421 static PyObject *
422 posix_error(void)
424 return PyErr_SetFromErrno(PyExc_OSError);
426 static PyObject *
427 posix_error_with_filename(char* name)
429 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
432 #ifdef Py_WIN_WIDE_FILENAMES
433 static PyObject *
434 posix_error_with_unicode_filename(Py_UNICODE* name)
436 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
438 #endif /* Py_WIN_WIDE_FILENAMES */
441 static PyObject *
442 posix_error_with_allocated_filename(char* name)
444 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
445 PyMem_Free(name);
446 return rc;
449 #ifdef MS_WINDOWS
450 static PyObject *
451 win32_error(char* function, char* filename)
453 /* XXX We should pass the function name along in the future.
454 (_winreg.c also wants to pass the function name.)
455 This would however require an additional param to the
456 Windows error object, which is non-trivial.
458 errno = GetLastError();
459 if (filename)
460 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
461 else
462 return PyErr_SetFromWindowsErr(errno);
465 #ifdef Py_WIN_WIDE_FILENAMES
466 static PyObject *
467 win32_error_unicode(char* function, Py_UNICODE* filename)
469 /* XXX - see win32_error for comments on 'function' */
470 errno = GetLastError();
471 if (filename)
472 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
473 else
474 return PyErr_SetFromWindowsErr(errno);
477 static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
481 static int
482 convert_to_unicode(PyObject **param)
484 if (PyUnicode_CheckExact(*param))
485 Py_INCREF(*param);
486 else if (PyUnicode_Check(*param))
487 /* For a Unicode subtype that's not a Unicode object,
488 return a true Unicode object with the same data. */
489 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
490 PyUnicode_GET_SIZE(*param));
491 else
492 *param = PyUnicode_FromEncodedObject(*param,
493 Py_FileSystemDefaultEncoding,
494 "strict");
495 return (*param) != NULL;
498 #endif /* Py_WIN_WIDE_FILENAMES */
500 #endif
502 #if defined(PYOS_OS2)
503 /**********************************************************************
504 * Helper Function to Trim and Format OS/2 Messages
505 **********************************************************************/
506 static void
507 os2_formatmsg(char *msgbuf, int msglen, char *reason)
509 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
511 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
512 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
514 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
515 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
518 /* Add Optional Reason Text */
519 if (reason) {
520 strcat(msgbuf, " : ");
521 strcat(msgbuf, reason);
525 /**********************************************************************
526 * Decode an OS/2 Operating System Error Code
528 * A convenience function to lookup an OS/2 error code and return a
529 * text message we can use to raise a Python exception.
531 * Notes:
532 * The messages for errors returned from the OS/2 kernel reside in
533 * the file OSO001.MSG in the \OS2 directory hierarchy.
535 **********************************************************************/
536 static char *
537 os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
539 APIRET rc;
540 ULONG msglen;
542 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
543 Py_BEGIN_ALLOW_THREADS
544 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
545 errorcode, "oso001.msg", &msglen);
546 Py_END_ALLOW_THREADS
548 if (rc == NO_ERROR)
549 os2_formatmsg(msgbuf, msglen, reason);
550 else
551 PyOS_snprintf(msgbuf, msgbuflen,
552 "unknown OS error #%d", errorcode);
554 return msgbuf;
557 /* Set an OS/2-specific error and return NULL. OS/2 kernel
558 errors are not in a global variable e.g. 'errno' nor are
559 they congruent with posix error numbers. */
561 static PyObject * os2_error(int code)
563 char text[1024];
564 PyObject *v;
566 os2_strerror(text, sizeof(text), code, "");
568 v = Py_BuildValue("(is)", code, text);
569 if (v != NULL) {
570 PyErr_SetObject(PyExc_OSError, v);
571 Py_DECREF(v);
573 return NULL; /* Signal to Python that an Exception is Pending */
576 #endif /* OS2 */
578 /* POSIX generic methods */
580 static PyObject *
581 posix_fildes(PyObject *fdobj, int (*func)(int))
583 int fd;
584 int res;
585 fd = PyObject_AsFileDescriptor(fdobj);
586 if (fd < 0)
587 return NULL;
588 Py_BEGIN_ALLOW_THREADS
589 res = (*func)(fd);
590 Py_END_ALLOW_THREADS
591 if (res < 0)
592 return posix_error();
593 Py_INCREF(Py_None);
594 return Py_None;
597 #ifdef Py_WIN_WIDE_FILENAMES
598 static int
599 unicode_file_names(void)
601 static int canusewide = -1;
602 if (canusewide == -1) {
603 /* As per doc for ::GetVersion(), this is the correct test for
604 the Windows NT family. */
605 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
607 return canusewide;
609 #endif
611 static PyObject *
612 posix_1str(PyObject *args, char *format, int (*func)(const char*))
614 char *path1 = NULL;
615 int res;
616 if (!PyArg_ParseTuple(args, format,
617 Py_FileSystemDefaultEncoding, &path1))
618 return NULL;
619 Py_BEGIN_ALLOW_THREADS
620 res = (*func)(path1);
621 Py_END_ALLOW_THREADS
622 if (res < 0)
623 return posix_error_with_allocated_filename(path1);
624 PyMem_Free(path1);
625 Py_INCREF(Py_None);
626 return Py_None;
629 static PyObject *
630 posix_2str(PyObject *args,
631 char *format,
632 int (*func)(const char *, const char *))
634 char *path1 = NULL, *path2 = NULL;
635 int res;
636 if (!PyArg_ParseTuple(args, format,
637 Py_FileSystemDefaultEncoding, &path1,
638 Py_FileSystemDefaultEncoding, &path2))
639 return NULL;
640 Py_BEGIN_ALLOW_THREADS
641 res = (*func)(path1, path2);
642 Py_END_ALLOW_THREADS
643 PyMem_Free(path1);
644 PyMem_Free(path2);
645 if (res != 0)
646 /* XXX how to report both path1 and path2??? */
647 return posix_error();
648 Py_INCREF(Py_None);
649 return Py_None;
652 #ifdef Py_WIN_WIDE_FILENAMES
653 static PyObject*
654 win32_1str(PyObject* args, char* func,
655 char* format, BOOL (__stdcall *funcA)(LPCSTR),
656 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
658 PyObject *uni;
659 char *ansi;
660 BOOL result;
661 if (unicode_file_names()) {
662 if (!PyArg_ParseTuple(args, wformat, &uni))
663 PyErr_Clear();
664 else {
665 Py_BEGIN_ALLOW_THREADS
666 result = funcW(PyUnicode_AsUnicode(uni));
667 Py_END_ALLOW_THREADS
668 if (!result)
669 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
670 Py_INCREF(Py_None);
671 return Py_None;
674 if (!PyArg_ParseTuple(args, format, &ansi))
675 return NULL;
676 Py_BEGIN_ALLOW_THREADS
677 result = funcA(ansi);
678 Py_END_ALLOW_THREADS
679 if (!result)
680 return win32_error(func, ansi);
681 Py_INCREF(Py_None);
682 return Py_None;
686 /* This is a reimplementation of the C library's chdir function,
687 but one that produces Win32 errors instead of DOS error codes.
688 chdir is essentially a wrapper around SetCurrentDirectory; however,
689 it also needs to set "magic" environment variables indicating
690 the per-drive current directory, which are of the form =<drive>: */
691 BOOL __stdcall
692 win32_chdir(LPCSTR path)
694 char new_path[MAX_PATH+1];
695 int result;
696 char env[4] = "=x:";
698 if(!SetCurrentDirectoryA(path))
699 return FALSE;
700 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
701 if (!result)
702 return FALSE;
703 /* In the ANSI API, there should not be any paths longer
704 than MAX_PATH. */
705 assert(result <= MAX_PATH+1);
706 if (strncmp(new_path, "\\\\", 2) == 0 ||
707 strncmp(new_path, "//", 2) == 0)
708 /* UNC path, nothing to do. */
709 return TRUE;
710 env[1] = new_path[0];
711 return SetEnvironmentVariableA(env, new_path);
714 /* The Unicode version differs from the ANSI version
715 since the current directory might exceed MAX_PATH characters */
716 BOOL __stdcall
717 win32_wchdir(LPCWSTR path)
719 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
720 int result;
721 wchar_t env[4] = L"=x:";
723 if(!SetCurrentDirectoryW(path))
724 return FALSE;
725 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
726 if (!result)
727 return FALSE;
728 if (result > MAX_PATH+1) {
729 new_path = malloc(result);
730 if (!new_path) {
731 SetLastError(ERROR_OUTOFMEMORY);
732 return FALSE;
735 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
736 wcsncmp(new_path, L"//", 2) == 0)
737 /* UNC path, nothing to do. */
738 return TRUE;
739 env[1] = new_path[0];
740 result = SetEnvironmentVariableW(env, new_path);
741 if (new_path != _new_path)
742 free(new_path);
743 return result;
745 #endif
747 #ifdef MS_WINDOWS
748 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
749 - time stamps are restricted to second resolution
750 - file modification times suffer from forth-and-back conversions between
751 UTC and local time
752 Therefore, we implement our own stat, based on the Win32 API directly.
754 #define HAVE_STAT_NSEC 1
756 struct win32_stat{
757 int st_dev;
758 __int64 st_ino;
759 unsigned short st_mode;
760 int st_nlink;
761 int st_uid;
762 int st_gid;
763 int st_rdev;
764 __int64 st_size;
765 int st_atime;
766 int st_atime_nsec;
767 int st_mtime;
768 int st_mtime_nsec;
769 int st_ctime;
770 int st_ctime_nsec;
773 static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
775 static void
776 FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
778 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
779 /* Cannot simply cast and dereference in_ptr,
780 since it might not be aligned properly */
781 __int64 in;
782 memcpy(&in, in_ptr, sizeof(in));
783 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
784 /* XXX Win32 supports time stamps past 2038; we currently don't */
785 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
788 static void
789 time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
791 /* XXX endianness */
792 __int64 out;
793 out = time_in + secs_between_epochs;
794 out = out * 10000000 + nsec_in / 100;
795 memcpy(out_ptr, &out, sizeof(out));
798 /* Below, we *know* that ugo+r is 0444 */
799 #if _S_IREAD != 0400
800 #error Unsupported C library
801 #endif
802 static int
803 attributes_to_mode(DWORD attr)
805 int m = 0;
806 if (attr & FILE_ATTRIBUTE_DIRECTORY)
807 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
808 else
809 m |= _S_IFREG;
810 if (attr & FILE_ATTRIBUTE_READONLY)
811 m |= 0444;
812 else
813 m |= 0666;
814 return m;
817 static int
818 attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
820 memset(result, 0, sizeof(*result));
821 result->st_mode = attributes_to_mode(info->dwFileAttributes);
822 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
823 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
824 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
825 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
827 return 0;
830 /* Emulate GetFileAttributesEx[AW] on Windows 95 */
831 static int checked = 0;
832 static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
833 static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
834 static void
835 check_gfax()
837 HINSTANCE hKernel32;
838 if (checked)
839 return;
840 checked = 1;
841 hKernel32 = GetModuleHandle("KERNEL32");
842 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
843 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
846 static BOOL
847 attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
849 HANDLE hFindFile;
850 WIN32_FIND_DATAA FileData;
851 hFindFile = FindFirstFileA(pszFile, &FileData);
852 if (hFindFile == INVALID_HANDLE_VALUE)
853 return FALSE;
854 FindClose(hFindFile);
855 pfad->dwFileAttributes = FileData.dwFileAttributes;
856 pfad->ftCreationTime = FileData.ftCreationTime;
857 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
858 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
859 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
860 pfad->nFileSizeLow = FileData.nFileSizeLow;
861 return TRUE;
864 static BOOL
865 attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
867 HANDLE hFindFile;
868 WIN32_FIND_DATAW FileData;
869 hFindFile = FindFirstFileW(pszFile, &FileData);
870 if (hFindFile == INVALID_HANDLE_VALUE)
871 return FALSE;
872 FindClose(hFindFile);
873 pfad->dwFileAttributes = FileData.dwFileAttributes;
874 pfad->ftCreationTime = FileData.ftCreationTime;
875 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
876 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
877 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
878 pfad->nFileSizeLow = FileData.nFileSizeLow;
879 return TRUE;
882 static BOOL WINAPI
883 Py_GetFileAttributesExA(LPCSTR pszFile,
884 GET_FILEEX_INFO_LEVELS level,
885 LPVOID pv)
887 BOOL result;
888 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
889 /* First try to use the system's implementation, if that is
890 available and either succeeds to gives an error other than
891 that it isn't implemented. */
892 check_gfax();
893 if (gfaxa) {
894 result = gfaxa(pszFile, level, pv);
895 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
896 return result;
898 /* It's either not present, or not implemented.
899 Emulate using FindFirstFile. */
900 if (level != GetFileExInfoStandard) {
901 SetLastError(ERROR_INVALID_PARAMETER);
902 return FALSE;
904 /* Use GetFileAttributes to validate that the file name
905 does not contain wildcards (which FindFirstFile would
906 accept). */
907 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
908 return FALSE;
909 return attributes_from_dir(pszFile, pfad);
912 static BOOL WINAPI
913 Py_GetFileAttributesExW(LPCWSTR pszFile,
914 GET_FILEEX_INFO_LEVELS level,
915 LPVOID pv)
917 BOOL result;
918 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
919 /* First try to use the system's implementation, if that is
920 available and either succeeds to gives an error other than
921 that it isn't implemented. */
922 check_gfax();
923 if (gfaxa) {
924 result = gfaxw(pszFile, level, pv);
925 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
926 return result;
928 /* It's either not present, or not implemented.
929 Emulate using FindFirstFile. */
930 if (level != GetFileExInfoStandard) {
931 SetLastError(ERROR_INVALID_PARAMETER);
932 return FALSE;
934 /* Use GetFileAttributes to validate that the file name
935 does not contain wildcards (which FindFirstFile would
936 accept). */
937 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
938 return FALSE;
939 return attributes_from_dir_w(pszFile, pfad);
942 static int
943 win32_stat(const char* path, struct win32_stat *result)
945 WIN32_FILE_ATTRIBUTE_DATA info;
946 int code;
947 char *dot;
948 /* XXX not supported on Win95 and NT 3.x */
949 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
950 if (GetLastError() != ERROR_SHARING_VIOLATION) {
951 /* Protocol violation: we explicitly clear errno, instead of
952 setting it to a POSIX error. Callers should use GetLastError. */
953 errno = 0;
954 return -1;
955 } else {
956 /* Could not get attributes on open file. Fall back to
957 reading the directory. */
958 if (!attributes_from_dir(path, &info)) {
959 /* Very strange. This should not fail now */
960 errno = 0;
961 return -1;
965 code = attribute_data_to_stat(&info, result);
966 if (code != 0)
967 return code;
968 /* Set S_IFEXEC if it is an .exe, .bat, ... */
969 dot = strrchr(path, '.');
970 if (dot) {
971 if (stricmp(dot, ".bat") == 0 ||
972 stricmp(dot, ".cmd") == 0 ||
973 stricmp(dot, ".exe") == 0 ||
974 stricmp(dot, ".com") == 0)
975 result->st_mode |= 0111;
977 return code;
980 static int
981 win32_wstat(const wchar_t* path, struct win32_stat *result)
983 int code;
984 const wchar_t *dot;
985 WIN32_FILE_ATTRIBUTE_DATA info;
986 /* XXX not supported on Win95 and NT 3.x */
987 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
988 if (GetLastError() != ERROR_SHARING_VIOLATION) {
989 /* Protocol violation: we explicitly clear errno, instead of
990 setting it to a POSIX error. Callers should use GetLastError. */
991 errno = 0;
992 return -1;
993 } else {
994 /* Could not get attributes on open file. Fall back to
995 reading the directory. */
996 if (!attributes_from_dir_w(path, &info)) {
997 /* Very strange. This should not fail now */
998 errno = 0;
999 return -1;
1003 code = attribute_data_to_stat(&info, result);
1004 if (code < 0)
1005 return code;
1006 /* Set IFEXEC if it is an .exe, .bat, ... */
1007 dot = wcsrchr(path, '.');
1008 if (dot) {
1009 if (_wcsicmp(dot, L".bat") == 0 ||
1010 _wcsicmp(dot, L".cmd") == 0 ||
1011 _wcsicmp(dot, L".exe") == 0 ||
1012 _wcsicmp(dot, L".com") == 0)
1013 result->st_mode |= 0111;
1015 return code;
1018 static int
1019 win32_fstat(int file_number, struct win32_stat *result)
1021 BY_HANDLE_FILE_INFORMATION info;
1022 HANDLE h;
1023 int type;
1025 h = (HANDLE)_get_osfhandle(file_number);
1027 /* Protocol violation: we explicitly clear errno, instead of
1028 setting it to a POSIX error. Callers should use GetLastError. */
1029 errno = 0;
1031 if (h == INVALID_HANDLE_VALUE) {
1032 /* This is really a C library error (invalid file handle).
1033 We set the Win32 error to the closes one matching. */
1034 SetLastError(ERROR_INVALID_HANDLE);
1035 return -1;
1037 memset(result, 0, sizeof(*result));
1039 type = GetFileType(h);
1040 if (type == FILE_TYPE_UNKNOWN) {
1041 DWORD error = GetLastError();
1042 if (error != 0) {
1043 return -1;
1045 /* else: valid but unknown file */
1048 if (type != FILE_TYPE_DISK) {
1049 if (type == FILE_TYPE_CHAR)
1050 result->st_mode = _S_IFCHR;
1051 else if (type == FILE_TYPE_PIPE)
1052 result->st_mode = _S_IFIFO;
1053 return 0;
1056 if (!GetFileInformationByHandle(h, &info)) {
1057 return -1;
1060 /* similar to stat() */
1061 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1062 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1063 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1064 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1065 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1066 /* specific to fstat() */
1067 result->st_nlink = info.nNumberOfLinks;
1068 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1069 return 0;
1072 #endif /* MS_WINDOWS */
1074 PyDoc_STRVAR(stat_result__doc__,
1075 "stat_result: Result from stat or lstat.\n\n\
1076 This object may be accessed either as a tuple of\n\
1077 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1078 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1080 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1081 or st_flags, they are available as attributes only.\n\
1083 See os.stat for more information.");
1085 static PyStructSequence_Field stat_result_fields[] = {
1086 {"st_mode", "protection bits"},
1087 {"st_ino", "inode"},
1088 {"st_dev", "device"},
1089 {"st_nlink", "number of hard links"},
1090 {"st_uid", "user ID of owner"},
1091 {"st_gid", "group ID of owner"},
1092 {"st_size", "total size, in bytes"},
1093 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1094 {NULL, "integer time of last access"},
1095 {NULL, "integer time of last modification"},
1096 {NULL, "integer time of last change"},
1097 {"st_atime", "time of last access"},
1098 {"st_mtime", "time of last modification"},
1099 {"st_ctime", "time of last change"},
1100 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1101 {"st_blksize", "blocksize for filesystem I/O"},
1102 #endif
1103 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1104 {"st_blocks", "number of blocks allocated"},
1105 #endif
1106 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1107 {"st_rdev", "device type (if inode device)"},
1108 #endif
1109 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1110 {"st_flags", "user defined flags for file"},
1111 #endif
1112 #ifdef HAVE_STRUCT_STAT_ST_GEN
1113 {"st_gen", "generation number"},
1114 #endif
1115 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1116 {"st_birthtime", "time of creation"},
1117 #endif
1121 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1122 #define ST_BLKSIZE_IDX 13
1123 #else
1124 #define ST_BLKSIZE_IDX 12
1125 #endif
1127 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1128 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1129 #else
1130 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1131 #endif
1133 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1134 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1135 #else
1136 #define ST_RDEV_IDX ST_BLOCKS_IDX
1137 #endif
1139 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1140 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1141 #else
1142 #define ST_FLAGS_IDX ST_RDEV_IDX
1143 #endif
1145 #ifdef HAVE_STRUCT_STAT_ST_GEN
1146 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
1147 #else
1148 #define ST_GEN_IDX ST_FLAGS_IDX
1149 #endif
1151 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1152 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1153 #else
1154 #define ST_BIRTHTIME_IDX ST_GEN_IDX
1155 #endif
1157 static PyStructSequence_Desc stat_result_desc = {
1158 "stat_result", /* name */
1159 stat_result__doc__, /* doc */
1160 stat_result_fields,
1164 PyDoc_STRVAR(statvfs_result__doc__,
1165 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
1166 This object may be accessed either as a tuple of\n\
1167 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1168 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
1170 See os.statvfs for more information.");
1172 static PyStructSequence_Field statvfs_result_fields[] = {
1173 {"f_bsize", },
1174 {"f_frsize", },
1175 {"f_blocks", },
1176 {"f_bfree", },
1177 {"f_bavail", },
1178 {"f_files", },
1179 {"f_ffree", },
1180 {"f_favail", },
1181 {"f_flag", },
1182 {"f_namemax",},
1186 static PyStructSequence_Desc statvfs_result_desc = {
1187 "statvfs_result", /* name */
1188 statvfs_result__doc__, /* doc */
1189 statvfs_result_fields,
1193 static int initialized;
1194 static PyTypeObject StatResultType;
1195 static PyTypeObject StatVFSResultType;
1196 static newfunc structseq_new;
1198 static PyObject *
1199 statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1201 PyStructSequence *result;
1202 int i;
1204 result = (PyStructSequence*)structseq_new(type, args, kwds);
1205 if (!result)
1206 return NULL;
1207 /* If we have been initialized from a tuple,
1208 st_?time might be set to None. Initialize it
1209 from the int slots. */
1210 for (i = 7; i <= 9; i++) {
1211 if (result->ob_item[i+3] == Py_None) {
1212 Py_DECREF(Py_None);
1213 Py_INCREF(result->ob_item[i]);
1214 result->ob_item[i+3] = result->ob_item[i];
1217 return (PyObject*)result;
1222 /* If true, st_?time is float. */
1223 static int _stat_float_times = 1;
1225 PyDoc_STRVAR(stat_float_times__doc__,
1226 "stat_float_times([newval]) -> oldval\n\n\
1227 Determine whether os.[lf]stat represents time stamps as float objects.\n\
1228 If newval is True, future calls to stat() return floats, if it is False,\n\
1229 future calls return ints. \n\
1230 If newval is omitted, return the current setting.\n");
1232 static PyObject*
1233 stat_float_times(PyObject* self, PyObject *args)
1235 int newval = -1;
1236 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1237 return NULL;
1238 if (newval == -1)
1239 /* Return old value */
1240 return PyBool_FromLong(_stat_float_times);
1241 _stat_float_times = newval;
1242 Py_INCREF(Py_None);
1243 return Py_None;
1246 static void
1247 fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1249 PyObject *fval,*ival;
1250 #if SIZEOF_TIME_T > SIZEOF_LONG
1251 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
1252 #else
1253 ival = PyInt_FromLong((long)sec);
1254 #endif
1255 if (!ival)
1256 return;
1257 if (_stat_float_times) {
1258 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1259 } else {
1260 fval = ival;
1261 Py_INCREF(fval);
1263 PyStructSequence_SET_ITEM(v, index, ival);
1264 PyStructSequence_SET_ITEM(v, index+3, fval);
1267 /* pack a system stat C structure into the Python stat tuple
1268 (used by posix_stat() and posix_fstat()) */
1269 static PyObject*
1270 _pystat_fromstructstat(STRUCT_STAT *st)
1272 unsigned long ansec, mnsec, cnsec;
1273 PyObject *v = PyStructSequence_New(&StatResultType);
1274 if (v == NULL)
1275 return NULL;
1277 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
1278 #ifdef HAVE_LARGEFILE_SUPPORT
1279 PyStructSequence_SET_ITEM(v, 1,
1280 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
1281 #else
1282 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
1283 #endif
1284 #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
1285 PyStructSequence_SET_ITEM(v, 2,
1286 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
1287 #else
1288 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
1289 #endif
1290 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1291 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1292 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
1293 #ifdef HAVE_LARGEFILE_SUPPORT
1294 PyStructSequence_SET_ITEM(v, 6,
1295 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
1296 #else
1297 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
1298 #endif
1300 #if defined(HAVE_STAT_TV_NSEC)
1301 ansec = st->st_atim.tv_nsec;
1302 mnsec = st->st_mtim.tv_nsec;
1303 cnsec = st->st_ctim.tv_nsec;
1304 #elif defined(HAVE_STAT_TV_NSEC2)
1305 ansec = st->st_atimespec.tv_nsec;
1306 mnsec = st->st_mtimespec.tv_nsec;
1307 cnsec = st->st_ctimespec.tv_nsec;
1308 #elif defined(HAVE_STAT_NSEC)
1309 ansec = st->st_atime_nsec;
1310 mnsec = st->st_mtime_nsec;
1311 cnsec = st->st_ctime_nsec;
1312 #else
1313 ansec = mnsec = cnsec = 0;
1314 #endif
1315 fill_time(v, 7, st->st_atime, ansec);
1316 fill_time(v, 8, st->st_mtime, mnsec);
1317 fill_time(v, 9, st->st_ctime, cnsec);
1319 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1320 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1321 PyInt_FromLong((long)st->st_blksize));
1322 #endif
1323 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1324 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1325 PyInt_FromLong((long)st->st_blocks));
1326 #endif
1327 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1328 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1329 PyInt_FromLong((long)st->st_rdev));
1330 #endif
1331 #ifdef HAVE_STRUCT_STAT_ST_GEN
1332 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1333 PyInt_FromLong((long)st->st_gen));
1334 #endif
1335 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1337 PyObject *val;
1338 unsigned long bsec,bnsec;
1339 bsec = (long)st->st_birthtime;
1340 #ifdef HAVE_STAT_TV_NSEC2
1341 bnsec = st->st_birthtimespec.tv_nsec;
1342 #else
1343 bnsec = 0;
1344 #endif
1345 if (_stat_float_times) {
1346 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1347 } else {
1348 val = PyInt_FromLong((long)bsec);
1350 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1351 val);
1353 #endif
1354 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1355 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1356 PyInt_FromLong((long)st->st_flags));
1357 #endif
1359 if (PyErr_Occurred()) {
1360 Py_DECREF(v);
1361 return NULL;
1364 return v;
1367 #ifdef MS_WINDOWS
1369 /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1370 where / can be used in place of \ and the trailing slash is optional.
1371 Both SERVER and SHARE must have at least one character.
1374 #define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1375 #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1376 #ifndef ARRAYSIZE
1377 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1378 #endif
1380 static BOOL
1381 IsUNCRootA(char *path, int pathlen)
1383 #define ISSLASH ISSLASHA
1385 int i, share;
1387 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1388 /* minimum UNCRoot is \\x\y */
1389 return FALSE;
1390 for (i = 2; i < pathlen ; i++)
1391 if (ISSLASH(path[i])) break;
1392 if (i == 2 || i == pathlen)
1393 /* do not allow \\\SHARE or \\SERVER */
1394 return FALSE;
1395 share = i+1;
1396 for (i = share; i < pathlen; i++)
1397 if (ISSLASH(path[i])) break;
1398 return (i != share && (i == pathlen || i == pathlen-1));
1400 #undef ISSLASH
1403 #ifdef Py_WIN_WIDE_FILENAMES
1404 static BOOL
1405 IsUNCRootW(Py_UNICODE *path, int pathlen)
1407 #define ISSLASH ISSLASHW
1409 int i, share;
1411 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1412 /* minimum UNCRoot is \\x\y */
1413 return FALSE;
1414 for (i = 2; i < pathlen ; i++)
1415 if (ISSLASH(path[i])) break;
1416 if (i == 2 || i == pathlen)
1417 /* do not allow \\\SHARE or \\SERVER */
1418 return FALSE;
1419 share = i+1;
1420 for (i = share; i < pathlen; i++)
1421 if (ISSLASH(path[i])) break;
1422 return (i != share && (i == pathlen || i == pathlen-1));
1424 #undef ISSLASH
1426 #endif /* Py_WIN_WIDE_FILENAMES */
1427 #endif /* MS_WINDOWS */
1429 static PyObject *
1430 posix_do_stat(PyObject *self, PyObject *args,
1431 char *format,
1432 #ifdef __VMS
1433 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1434 #else
1435 int (*statfunc)(const char *, STRUCT_STAT *),
1436 #endif
1437 char *wformat,
1438 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
1440 STRUCT_STAT st;
1441 char *path = NULL; /* pass this to stat; do not free() it */
1442 char *pathfree = NULL; /* this memory must be free'd */
1443 int res;
1444 PyObject *result;
1446 #ifdef Py_WIN_WIDE_FILENAMES
1447 /* If on wide-character-capable OS see if argument
1448 is Unicode and if so use wide API. */
1449 if (unicode_file_names()) {
1450 PyUnicodeObject *po;
1451 if (PyArg_ParseTuple(args, wformat, &po)) {
1452 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1454 Py_BEGIN_ALLOW_THREADS
1455 /* PyUnicode_AS_UNICODE result OK without
1456 thread lock as it is a simple dereference. */
1457 res = wstatfunc(wpath, &st);
1458 Py_END_ALLOW_THREADS
1460 if (res != 0)
1461 return win32_error_unicode("stat", wpath);
1462 return _pystat_fromstructstat(&st);
1464 /* Drop the argument parsing error as narrow strings
1465 are also valid. */
1466 PyErr_Clear();
1468 #endif
1470 if (!PyArg_ParseTuple(args, format,
1471 Py_FileSystemDefaultEncoding, &path))
1472 return NULL;
1473 pathfree = path;
1475 Py_BEGIN_ALLOW_THREADS
1476 res = (*statfunc)(path, &st);
1477 Py_END_ALLOW_THREADS
1479 if (res != 0) {
1480 #ifdef MS_WINDOWS
1481 result = win32_error("stat", pathfree);
1482 #else
1483 result = posix_error_with_filename(pathfree);
1484 #endif
1486 else
1487 result = _pystat_fromstructstat(&st);
1489 PyMem_Free(pathfree);
1490 return result;
1493 /* POSIX methods */
1495 PyDoc_STRVAR(posix_access__doc__,
1496 "access(path, mode) -> True if granted, False otherwise\n\n\
1497 Use the real uid/gid to test for access to a path. Note that most\n\
1498 operations will use the effective uid/gid, therefore this routine can\n\
1499 be used in a suid/sgid environment to test if the invoking user has the\n\
1500 specified access to the path. The mode argument can be F_OK to test\n\
1501 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
1503 static PyObject *
1504 posix_access(PyObject *self, PyObject *args)
1506 char *path;
1507 int mode;
1509 #ifdef Py_WIN_WIDE_FILENAMES
1510 DWORD attr;
1511 if (unicode_file_names()) {
1512 PyUnicodeObject *po;
1513 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1514 Py_BEGIN_ALLOW_THREADS
1515 /* PyUnicode_AS_UNICODE OK without thread lock as
1516 it is a simple dereference. */
1517 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1518 Py_END_ALLOW_THREADS
1519 goto finish;
1521 /* Drop the argument parsing error as narrow strings
1522 are also valid. */
1523 PyErr_Clear();
1525 if (!PyArg_ParseTuple(args, "eti:access",
1526 Py_FileSystemDefaultEncoding, &path, &mode))
1527 return 0;
1528 Py_BEGIN_ALLOW_THREADS
1529 attr = GetFileAttributesA(path);
1530 Py_END_ALLOW_THREADS
1531 PyMem_Free(path);
1532 finish:
1533 if (attr == 0xFFFFFFFF)
1534 /* File does not exist, or cannot read attributes */
1535 return PyBool_FromLong(0);
1536 /* Access is possible if either write access wasn't requested, or
1537 the file isn't read-only, or if it's a directory, as there are
1538 no read-only directories on Windows. */
1539 return PyBool_FromLong(!(mode & 2)
1540 || !(attr & FILE_ATTRIBUTE_READONLY)
1541 || (attr & FILE_ATTRIBUTE_DIRECTORY));
1542 #else
1543 int res;
1544 if (!PyArg_ParseTuple(args, "eti:access",
1545 Py_FileSystemDefaultEncoding, &path, &mode))
1546 return NULL;
1547 Py_BEGIN_ALLOW_THREADS
1548 res = access(path, mode);
1549 Py_END_ALLOW_THREADS
1550 PyMem_Free(path);
1551 return PyBool_FromLong(res == 0);
1552 #endif
1555 #ifndef F_OK
1556 #define F_OK 0
1557 #endif
1558 #ifndef R_OK
1559 #define R_OK 4
1560 #endif
1561 #ifndef W_OK
1562 #define W_OK 2
1563 #endif
1564 #ifndef X_OK
1565 #define X_OK 1
1566 #endif
1568 #ifdef HAVE_TTYNAME
1569 PyDoc_STRVAR(posix_ttyname__doc__,
1570 "ttyname(fd) -> string\n\n\
1571 Return the name of the terminal device connected to 'fd'.");
1573 static PyObject *
1574 posix_ttyname(PyObject *self, PyObject *args)
1576 int id;
1577 char *ret;
1579 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1580 return NULL;
1582 #if defined(__VMS)
1583 /* file descriptor 0 only, the default input device (stdin) */
1584 if (id == 0) {
1585 ret = ttyname();
1587 else {
1588 ret = NULL;
1590 #else
1591 ret = ttyname(id);
1592 #endif
1593 if (ret == NULL)
1594 return posix_error();
1595 return PyString_FromString(ret);
1597 #endif
1599 #ifdef HAVE_CTERMID
1600 PyDoc_STRVAR(posix_ctermid__doc__,
1601 "ctermid() -> string\n\n\
1602 Return the name of the controlling terminal for this process.");
1604 static PyObject *
1605 posix_ctermid(PyObject *self, PyObject *noargs)
1607 char *ret;
1608 char buffer[L_ctermid];
1610 #ifdef USE_CTERMID_R
1611 ret = ctermid_r(buffer);
1612 #else
1613 ret = ctermid(buffer);
1614 #endif
1615 if (ret == NULL)
1616 return posix_error();
1617 return PyString_FromString(buffer);
1619 #endif
1621 PyDoc_STRVAR(posix_chdir__doc__,
1622 "chdir(path)\n\n\
1623 Change the current working directory to the specified path.");
1625 static PyObject *
1626 posix_chdir(PyObject *self, PyObject *args)
1628 #ifdef MS_WINDOWS
1629 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
1630 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
1631 return posix_1str(args, "et:chdir", _chdir2);
1632 #elif defined(__VMS)
1633 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
1634 #else
1635 return posix_1str(args, "et:chdir", chdir);
1636 #endif
1639 #ifdef HAVE_FCHDIR
1640 PyDoc_STRVAR(posix_fchdir__doc__,
1641 "fchdir(fildes)\n\n\
1642 Change to the directory of the given file descriptor. fildes must be\n\
1643 opened on a directory, not a file.");
1645 static PyObject *
1646 posix_fchdir(PyObject *self, PyObject *fdobj)
1648 return posix_fildes(fdobj, fchdir);
1650 #endif /* HAVE_FCHDIR */
1653 PyDoc_STRVAR(posix_chmod__doc__,
1654 "chmod(path, mode)\n\n\
1655 Change the access permissions of a file.");
1657 static PyObject *
1658 posix_chmod(PyObject *self, PyObject *args)
1660 char *path = NULL;
1661 int i;
1662 int res;
1663 #ifdef Py_WIN_WIDE_FILENAMES
1664 DWORD attr;
1665 if (unicode_file_names()) {
1666 PyUnicodeObject *po;
1667 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1668 Py_BEGIN_ALLOW_THREADS
1669 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1670 if (attr != 0xFFFFFFFF) {
1671 if (i & _S_IWRITE)
1672 attr &= ~FILE_ATTRIBUTE_READONLY;
1673 else
1674 attr |= FILE_ATTRIBUTE_READONLY;
1675 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1677 else
1678 res = 0;
1679 Py_END_ALLOW_THREADS
1680 if (!res)
1681 return win32_error_unicode("chmod",
1682 PyUnicode_AS_UNICODE(po));
1683 Py_INCREF(Py_None);
1684 return Py_None;
1686 /* Drop the argument parsing error as narrow strings
1687 are also valid. */
1688 PyErr_Clear();
1690 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1691 &path, &i))
1692 return NULL;
1693 Py_BEGIN_ALLOW_THREADS
1694 attr = GetFileAttributesA(path);
1695 if (attr != 0xFFFFFFFF) {
1696 if (i & _S_IWRITE)
1697 attr &= ~FILE_ATTRIBUTE_READONLY;
1698 else
1699 attr |= FILE_ATTRIBUTE_READONLY;
1700 res = SetFileAttributesA(path, attr);
1702 else
1703 res = 0;
1704 Py_END_ALLOW_THREADS
1705 if (!res) {
1706 win32_error("chmod", path);
1707 PyMem_Free(path);
1708 return NULL;
1710 PyMem_Free(path);
1711 Py_INCREF(Py_None);
1712 return Py_None;
1713 #else /* Py_WIN_WIDE_FILENAMES */
1714 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1715 &path, &i))
1716 return NULL;
1717 Py_BEGIN_ALLOW_THREADS
1718 res = chmod(path, i);
1719 Py_END_ALLOW_THREADS
1720 if (res < 0)
1721 return posix_error_with_allocated_filename(path);
1722 PyMem_Free(path);
1723 Py_INCREF(Py_None);
1724 return Py_None;
1725 #endif
1728 #ifdef HAVE_FCHMOD
1729 PyDoc_STRVAR(posix_fchmod__doc__,
1730 "fchmod(fd, mode)\n\n\
1731 Change the access permissions of the file given by file\n\
1732 descriptor fd.");
1734 static PyObject *
1735 posix_fchmod(PyObject *self, PyObject *args)
1737 int fd, mode, res;
1738 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1739 return NULL;
1740 Py_BEGIN_ALLOW_THREADS
1741 res = fchmod(fd, mode);
1742 Py_END_ALLOW_THREADS
1743 if (res < 0)
1744 return posix_error();
1745 Py_RETURN_NONE;
1747 #endif /* HAVE_FCHMOD */
1749 #ifdef HAVE_LCHMOD
1750 PyDoc_STRVAR(posix_lchmod__doc__,
1751 "lchmod(path, mode)\n\n\
1752 Change the access permissions of a file. If path is a symlink, this\n\
1753 affects the link itself rather than the target.");
1755 static PyObject *
1756 posix_lchmod(PyObject *self, PyObject *args)
1758 char *path = NULL;
1759 int i;
1760 int res;
1761 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1762 &path, &i))
1763 return NULL;
1764 Py_BEGIN_ALLOW_THREADS
1765 res = lchmod(path, i);
1766 Py_END_ALLOW_THREADS
1767 if (res < 0)
1768 return posix_error_with_allocated_filename(path);
1769 PyMem_Free(path);
1770 Py_RETURN_NONE;
1772 #endif /* HAVE_LCHMOD */
1775 #ifdef HAVE_CHFLAGS
1776 PyDoc_STRVAR(posix_chflags__doc__,
1777 "chflags(path, flags)\n\n\
1778 Set file flags.");
1780 static PyObject *
1781 posix_chflags(PyObject *self, PyObject *args)
1783 char *path;
1784 unsigned long flags;
1785 int res;
1786 if (!PyArg_ParseTuple(args, "etk:chflags",
1787 Py_FileSystemDefaultEncoding, &path, &flags))
1788 return NULL;
1789 Py_BEGIN_ALLOW_THREADS
1790 res = chflags(path, flags);
1791 Py_END_ALLOW_THREADS
1792 if (res < 0)
1793 return posix_error_with_allocated_filename(path);
1794 PyMem_Free(path);
1795 Py_INCREF(Py_None);
1796 return Py_None;
1798 #endif /* HAVE_CHFLAGS */
1800 #ifdef HAVE_LCHFLAGS
1801 PyDoc_STRVAR(posix_lchflags__doc__,
1802 "lchflags(path, flags)\n\n\
1803 Set file flags.\n\
1804 This function will not follow symbolic links.");
1806 static PyObject *
1807 posix_lchflags(PyObject *self, PyObject *args)
1809 char *path;
1810 unsigned long flags;
1811 int res;
1812 if (!PyArg_ParseTuple(args, "etk:lchflags",
1813 Py_FileSystemDefaultEncoding, &path, &flags))
1814 return NULL;
1815 Py_BEGIN_ALLOW_THREADS
1816 res = lchflags(path, flags);
1817 Py_END_ALLOW_THREADS
1818 if (res < 0)
1819 return posix_error_with_allocated_filename(path);
1820 PyMem_Free(path);
1821 Py_INCREF(Py_None);
1822 return Py_None;
1824 #endif /* HAVE_LCHFLAGS */
1826 #ifdef HAVE_CHROOT
1827 PyDoc_STRVAR(posix_chroot__doc__,
1828 "chroot(path)\n\n\
1829 Change root directory to path.");
1831 static PyObject *
1832 posix_chroot(PyObject *self, PyObject *args)
1834 return posix_1str(args, "et:chroot", chroot);
1836 #endif
1838 #ifdef HAVE_FSYNC
1839 PyDoc_STRVAR(posix_fsync__doc__,
1840 "fsync(fildes)\n\n\
1841 force write of file with filedescriptor to disk.");
1843 static PyObject *
1844 posix_fsync(PyObject *self, PyObject *fdobj)
1846 return posix_fildes(fdobj, fsync);
1848 #endif /* HAVE_FSYNC */
1850 #ifdef HAVE_FDATASYNC
1852 #ifdef __hpux
1853 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1854 #endif
1856 PyDoc_STRVAR(posix_fdatasync__doc__,
1857 "fdatasync(fildes)\n\n\
1858 force write of file with filedescriptor to disk.\n\
1859 does not force update of metadata.");
1861 static PyObject *
1862 posix_fdatasync(PyObject *self, PyObject *fdobj)
1864 return posix_fildes(fdobj, fdatasync);
1866 #endif /* HAVE_FDATASYNC */
1869 #ifdef HAVE_CHOWN
1870 PyDoc_STRVAR(posix_chown__doc__,
1871 "chown(path, uid, gid)\n\n\
1872 Change the owner and group id of path to the numeric uid and gid.");
1874 static PyObject *
1875 posix_chown(PyObject *self, PyObject *args)
1877 char *path = NULL;
1878 long uid, gid;
1879 int res;
1880 if (!PyArg_ParseTuple(args, "etll:chown",
1881 Py_FileSystemDefaultEncoding, &path,
1882 &uid, &gid))
1883 return NULL;
1884 Py_BEGIN_ALLOW_THREADS
1885 res = chown(path, (uid_t) uid, (gid_t) gid);
1886 Py_END_ALLOW_THREADS
1887 if (res < 0)
1888 return posix_error_with_allocated_filename(path);
1889 PyMem_Free(path);
1890 Py_INCREF(Py_None);
1891 return Py_None;
1893 #endif /* HAVE_CHOWN */
1895 #ifdef HAVE_FCHOWN
1896 PyDoc_STRVAR(posix_fchown__doc__,
1897 "fchown(fd, uid, gid)\n\n\
1898 Change the owner and group id of the file given by file descriptor\n\
1899 fd to the numeric uid and gid.");
1901 static PyObject *
1902 posix_fchown(PyObject *self, PyObject *args)
1904 int fd, uid, gid;
1905 int res;
1906 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1907 return NULL;
1908 Py_BEGIN_ALLOW_THREADS
1909 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1910 Py_END_ALLOW_THREADS
1911 if (res < 0)
1912 return posix_error();
1913 Py_RETURN_NONE;
1915 #endif /* HAVE_FCHOWN */
1917 #ifdef HAVE_LCHOWN
1918 PyDoc_STRVAR(posix_lchown__doc__,
1919 "lchown(path, uid, gid)\n\n\
1920 Change the owner and group id of path to the numeric uid and gid.\n\
1921 This function will not follow symbolic links.");
1923 static PyObject *
1924 posix_lchown(PyObject *self, PyObject *args)
1926 char *path = NULL;
1927 int uid, gid;
1928 int res;
1929 if (!PyArg_ParseTuple(args, "etii:lchown",
1930 Py_FileSystemDefaultEncoding, &path,
1931 &uid, &gid))
1932 return NULL;
1933 Py_BEGIN_ALLOW_THREADS
1934 res = lchown(path, (uid_t) uid, (gid_t) gid);
1935 Py_END_ALLOW_THREADS
1936 if (res < 0)
1937 return posix_error_with_allocated_filename(path);
1938 PyMem_Free(path);
1939 Py_INCREF(Py_None);
1940 return Py_None;
1942 #endif /* HAVE_LCHOWN */
1945 #ifdef HAVE_GETCWD
1946 PyDoc_STRVAR(posix_getcwd__doc__,
1947 "getcwd() -> path\n\n\
1948 Return a string representing the current working directory.");
1950 static PyObject *
1951 posix_getcwd(PyObject *self, PyObject *noargs)
1953 int bufsize_incr = 1024;
1954 int bufsize = 0;
1955 char *tmpbuf = NULL;
1956 char *res = NULL;
1957 PyObject *dynamic_return;
1959 Py_BEGIN_ALLOW_THREADS
1960 do {
1961 bufsize = bufsize + bufsize_incr;
1962 tmpbuf = malloc(bufsize);
1963 if (tmpbuf == NULL) {
1964 break;
1966 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1967 res = _getcwd2(tmpbuf, bufsize);
1968 #else
1969 res = getcwd(tmpbuf, bufsize);
1970 #endif
1972 if (res == NULL) {
1973 free(tmpbuf);
1975 } while ((res == NULL) && (errno == ERANGE));
1976 Py_END_ALLOW_THREADS
1978 if (res == NULL)
1979 return posix_error();
1981 dynamic_return = PyString_FromString(tmpbuf);
1982 free(tmpbuf);
1984 return dynamic_return;
1987 #ifdef Py_USING_UNICODE
1988 PyDoc_STRVAR(posix_getcwdu__doc__,
1989 "getcwdu() -> path\n\n\
1990 Return a unicode string representing the current working directory.");
1992 static PyObject *
1993 posix_getcwdu(PyObject *self, PyObject *noargs)
1995 char buf[1026];
1996 char *res;
1998 #ifdef Py_WIN_WIDE_FILENAMES
1999 DWORD len;
2000 if (unicode_file_names()) {
2001 wchar_t wbuf[1026];
2002 wchar_t *wbuf2 = wbuf;
2003 PyObject *resobj;
2004 Py_BEGIN_ALLOW_THREADS
2005 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2006 /* If the buffer is large enough, len does not include the
2007 terminating \0. If the buffer is too small, len includes
2008 the space needed for the terminator. */
2009 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2010 wbuf2 = malloc(len * sizeof(wchar_t));
2011 if (wbuf2)
2012 len = GetCurrentDirectoryW(len, wbuf2);
2014 Py_END_ALLOW_THREADS
2015 if (!wbuf2) {
2016 PyErr_NoMemory();
2017 return NULL;
2019 if (!len) {
2020 if (wbuf2 != wbuf) free(wbuf2);
2021 return win32_error("getcwdu", NULL);
2023 resobj = PyUnicode_FromWideChar(wbuf2, len);
2024 if (wbuf2 != wbuf) free(wbuf2);
2025 return resobj;
2027 #endif
2029 Py_BEGIN_ALLOW_THREADS
2030 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2031 res = _getcwd2(buf, sizeof buf);
2032 #else
2033 res = getcwd(buf, sizeof buf);
2034 #endif
2035 Py_END_ALLOW_THREADS
2036 if (res == NULL)
2037 return posix_error();
2038 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2040 #endif
2041 #endif
2044 #ifdef HAVE_LINK
2045 PyDoc_STRVAR(posix_link__doc__,
2046 "link(src, dst)\n\n\
2047 Create a hard link to a file.");
2049 static PyObject *
2050 posix_link(PyObject *self, PyObject *args)
2052 return posix_2str(args, "etet:link", link);
2054 #endif /* HAVE_LINK */
2057 PyDoc_STRVAR(posix_listdir__doc__,
2058 "listdir(path) -> list_of_strings\n\n\
2059 Return a list containing the names of the entries in the directory.\n\
2061 path: path of directory to list\n\
2063 The list is in arbitrary order. It does not include the special\n\
2064 entries '.' and '..' even if they are present in the directory.");
2066 static PyObject *
2067 posix_listdir(PyObject *self, PyObject *args)
2069 /* XXX Should redo this putting the (now four) versions of opendir
2070 in separate files instead of having them all here... */
2071 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
2073 PyObject *d, *v;
2074 HANDLE hFindFile;
2075 BOOL result;
2076 WIN32_FIND_DATA FileData;
2077 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2078 char *bufptr = namebuf;
2079 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
2081 #ifdef Py_WIN_WIDE_FILENAMES
2082 /* If on wide-character-capable OS see if argument
2083 is Unicode and if so use wide API. */
2084 if (unicode_file_names()) {
2085 PyObject *po;
2086 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2087 WIN32_FIND_DATAW wFileData;
2088 Py_UNICODE *wnamebuf;
2089 Py_UNICODE wch;
2090 /* Overallocate for \\*.*\0 */
2091 len = PyUnicode_GET_SIZE(po);
2092 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2093 if (!wnamebuf) {
2094 PyErr_NoMemory();
2095 return NULL;
2097 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2098 wch = len > 0 ? wnamebuf[len-1] : '\0';
2099 if (wch != L'/' && wch != L'\\' && wch != L':')
2100 wnamebuf[len++] = L'\\';
2101 wcscpy(wnamebuf + len, L"*.*");
2102 if ((d = PyList_New(0)) == NULL) {
2103 free(wnamebuf);
2104 return NULL;
2106 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2107 if (hFindFile == INVALID_HANDLE_VALUE) {
2108 int error = GetLastError();
2109 if (error == ERROR_FILE_NOT_FOUND) {
2110 free(wnamebuf);
2111 return d;
2113 Py_DECREF(d);
2114 win32_error_unicode("FindFirstFileW", wnamebuf);
2115 free(wnamebuf);
2116 return NULL;
2118 do {
2119 /* Skip over . and .. */
2120 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2121 wcscmp(wFileData.cFileName, L"..") != 0) {
2122 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2123 if (v == NULL) {
2124 Py_DECREF(d);
2125 d = NULL;
2126 break;
2128 if (PyList_Append(d, v) != 0) {
2129 Py_DECREF(v);
2130 Py_DECREF(d);
2131 d = NULL;
2132 break;
2134 Py_DECREF(v);
2136 Py_BEGIN_ALLOW_THREADS
2137 result = FindNextFileW(hFindFile, &wFileData);
2138 Py_END_ALLOW_THREADS
2139 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2140 it got to the end of the directory. */
2141 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2142 Py_DECREF(d);
2143 win32_error_unicode("FindNextFileW", wnamebuf);
2144 FindClose(hFindFile);
2145 free(wnamebuf);
2146 return NULL;
2148 } while (result == TRUE);
2150 if (FindClose(hFindFile) == FALSE) {
2151 Py_DECREF(d);
2152 win32_error_unicode("FindClose", wnamebuf);
2153 free(wnamebuf);
2154 return NULL;
2156 free(wnamebuf);
2157 return d;
2159 /* Drop the argument parsing error as narrow strings
2160 are also valid. */
2161 PyErr_Clear();
2163 #endif
2165 if (!PyArg_ParseTuple(args, "et#:listdir",
2166 Py_FileSystemDefaultEncoding, &bufptr, &len))
2167 return NULL;
2168 if (len > 0) {
2169 char ch = namebuf[len-1];
2170 if (ch != SEP && ch != ALTSEP && ch != ':')
2171 namebuf[len++] = '/';
2173 strcpy(namebuf + len, "*.*");
2175 if ((d = PyList_New(0)) == NULL)
2176 return NULL;
2178 hFindFile = FindFirstFile(namebuf, &FileData);
2179 if (hFindFile == INVALID_HANDLE_VALUE) {
2180 int error = GetLastError();
2181 if (error == ERROR_FILE_NOT_FOUND)
2182 return d;
2183 Py_DECREF(d);
2184 return win32_error("FindFirstFile", namebuf);
2186 do {
2187 /* Skip over . and .. */
2188 if (strcmp(FileData.cFileName, ".") != 0 &&
2189 strcmp(FileData.cFileName, "..") != 0) {
2190 v = PyString_FromString(FileData.cFileName);
2191 if (v == NULL) {
2192 Py_DECREF(d);
2193 d = NULL;
2194 break;
2196 if (PyList_Append(d, v) != 0) {
2197 Py_DECREF(v);
2198 Py_DECREF(d);
2199 d = NULL;
2200 break;
2202 Py_DECREF(v);
2204 Py_BEGIN_ALLOW_THREADS
2205 result = FindNextFile(hFindFile, &FileData);
2206 Py_END_ALLOW_THREADS
2207 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2208 it got to the end of the directory. */
2209 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2210 Py_DECREF(d);
2211 win32_error("FindNextFile", namebuf);
2212 FindClose(hFindFile);
2213 return NULL;
2215 } while (result == TRUE);
2217 if (FindClose(hFindFile) == FALSE) {
2218 Py_DECREF(d);
2219 return win32_error("FindClose", namebuf);
2222 return d;
2224 #elif defined(PYOS_OS2)
2226 #ifndef MAX_PATH
2227 #define MAX_PATH CCHMAXPATH
2228 #endif
2229 char *name, *pt;
2230 Py_ssize_t len;
2231 PyObject *d, *v;
2232 char namebuf[MAX_PATH+5];
2233 HDIR hdir = 1;
2234 ULONG srchcnt = 1;
2235 FILEFINDBUF3 ep;
2236 APIRET rc;
2238 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
2239 return NULL;
2240 if (len >= MAX_PATH) {
2241 PyErr_SetString(PyExc_ValueError, "path too long");
2242 return NULL;
2244 strcpy(namebuf, name);
2245 for (pt = namebuf; *pt; pt++)
2246 if (*pt == ALTSEP)
2247 *pt = SEP;
2248 if (namebuf[len-1] != SEP)
2249 namebuf[len++] = SEP;
2250 strcpy(namebuf + len, "*.*");
2252 if ((d = PyList_New(0)) == NULL)
2253 return NULL;
2255 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2256 &hdir, /* Handle to Use While Search Directory */
2257 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2258 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2259 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2260 FIL_STANDARD); /* Format of Entry (EAs or Not) */
2262 if (rc != NO_ERROR) {
2263 errno = ENOENT;
2264 return posix_error_with_filename(name);
2267 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
2268 do {
2269 if (ep.achName[0] == '.'
2270 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
2271 continue; /* Skip Over "." and ".." Names */
2273 strcpy(namebuf, ep.achName);
2275 /* Leave Case of Name Alone -- In Native Form */
2276 /* (Removed Forced Lowercasing Code) */
2278 v = PyString_FromString(namebuf);
2279 if (v == NULL) {
2280 Py_DECREF(d);
2281 d = NULL;
2282 break;
2284 if (PyList_Append(d, v) != 0) {
2285 Py_DECREF(v);
2286 Py_DECREF(d);
2287 d = NULL;
2288 break;
2290 Py_DECREF(v);
2291 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2294 return d;
2295 #else
2297 char *name = NULL;
2298 PyObject *d, *v;
2299 DIR *dirp;
2300 struct dirent *ep;
2301 int arg_is_unicode = 1;
2303 errno = 0;
2304 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2305 arg_is_unicode = 0;
2306 PyErr_Clear();
2308 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2309 return NULL;
2310 if ((dirp = opendir(name)) == NULL) {
2311 return posix_error_with_allocated_filename(name);
2313 if ((d = PyList_New(0)) == NULL) {
2314 closedir(dirp);
2315 PyMem_Free(name);
2316 return NULL;
2318 for (;;) {
2319 errno = 0;
2320 Py_BEGIN_ALLOW_THREADS
2321 ep = readdir(dirp);
2322 Py_END_ALLOW_THREADS
2323 if (ep == NULL) {
2324 if (errno == 0) {
2325 break;
2326 } else {
2327 closedir(dirp);
2328 Py_DECREF(d);
2329 return posix_error_with_allocated_filename(name);
2332 if (ep->d_name[0] == '.' &&
2333 (NAMLEN(ep) == 1 ||
2334 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2335 continue;
2336 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2337 if (v == NULL) {
2338 Py_DECREF(d);
2339 d = NULL;
2340 break;
2342 #ifdef Py_USING_UNICODE
2343 if (arg_is_unicode) {
2344 PyObject *w;
2346 w = PyUnicode_FromEncodedObject(v,
2347 Py_FileSystemDefaultEncoding,
2348 "strict");
2349 if (w != NULL) {
2350 Py_DECREF(v);
2351 v = w;
2353 else {
2354 /* fall back to the original byte string, as
2355 discussed in patch #683592 */
2356 PyErr_Clear();
2359 #endif
2360 if (PyList_Append(d, v) != 0) {
2361 Py_DECREF(v);
2362 Py_DECREF(d);
2363 d = NULL;
2364 break;
2366 Py_DECREF(v);
2368 closedir(dirp);
2369 PyMem_Free(name);
2371 return d;
2373 #endif /* which OS */
2374 } /* end of posix_listdir */
2376 #ifdef MS_WINDOWS
2377 /* A helper function for abspath on win32 */
2378 static PyObject *
2379 posix__getfullpathname(PyObject *self, PyObject *args)
2381 /* assume encoded strings wont more than double no of chars */
2382 char inbuf[MAX_PATH*2];
2383 char *inbufp = inbuf;
2384 Py_ssize_t insize = sizeof(inbuf);
2385 char outbuf[MAX_PATH*2];
2386 char *temp;
2387 #ifdef Py_WIN_WIDE_FILENAMES
2388 if (unicode_file_names()) {
2389 PyUnicodeObject *po;
2390 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2391 Py_UNICODE woutbuf[MAX_PATH*2];
2392 Py_UNICODE *wtemp;
2393 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
2394 sizeof(woutbuf)/sizeof(woutbuf[0]),
2395 woutbuf, &wtemp))
2396 return win32_error("GetFullPathName", "");
2397 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2399 /* Drop the argument parsing error as narrow strings
2400 are also valid. */
2401 PyErr_Clear();
2403 #endif
2404 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2405 Py_FileSystemDefaultEncoding, &inbufp,
2406 &insize))
2407 return NULL;
2408 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2409 outbuf, &temp))
2410 return win32_error("GetFullPathName", inbuf);
2411 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2412 return PyUnicode_Decode(outbuf, strlen(outbuf),
2413 Py_FileSystemDefaultEncoding, NULL);
2415 return PyString_FromString(outbuf);
2416 } /* end of posix__getfullpathname */
2417 #endif /* MS_WINDOWS */
2419 PyDoc_STRVAR(posix_mkdir__doc__,
2420 "mkdir(path [, mode=0777])\n\n\
2421 Create a directory.");
2423 static PyObject *
2424 posix_mkdir(PyObject *self, PyObject *args)
2426 int res;
2427 char *path = NULL;
2428 int mode = 0777;
2430 #ifdef Py_WIN_WIDE_FILENAMES
2431 if (unicode_file_names()) {
2432 PyUnicodeObject *po;
2433 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2434 Py_BEGIN_ALLOW_THREADS
2435 /* PyUnicode_AS_UNICODE OK without thread lock as
2436 it is a simple dereference. */
2437 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2438 Py_END_ALLOW_THREADS
2439 if (!res)
2440 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2441 Py_INCREF(Py_None);
2442 return Py_None;
2444 /* Drop the argument parsing error as narrow strings
2445 are also valid. */
2446 PyErr_Clear();
2448 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2449 Py_FileSystemDefaultEncoding, &path, &mode))
2450 return NULL;
2451 Py_BEGIN_ALLOW_THREADS
2452 /* PyUnicode_AS_UNICODE OK without thread lock as
2453 it is a simple dereference. */
2454 res = CreateDirectoryA(path, NULL);
2455 Py_END_ALLOW_THREADS
2456 if (!res) {
2457 win32_error("mkdir", path);
2458 PyMem_Free(path);
2459 return NULL;
2461 PyMem_Free(path);
2462 Py_INCREF(Py_None);
2463 return Py_None;
2464 #else
2466 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2467 Py_FileSystemDefaultEncoding, &path, &mode))
2468 return NULL;
2469 Py_BEGIN_ALLOW_THREADS
2470 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2471 res = mkdir(path);
2472 #else
2473 res = mkdir(path, mode);
2474 #endif
2475 Py_END_ALLOW_THREADS
2476 if (res < 0)
2477 return posix_error_with_allocated_filename(path);
2478 PyMem_Free(path);
2479 Py_INCREF(Py_None);
2480 return Py_None;
2481 #endif
2485 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2486 #if defined(HAVE_SYS_RESOURCE_H)
2487 #include <sys/resource.h>
2488 #endif
2491 #ifdef HAVE_NICE
2492 PyDoc_STRVAR(posix_nice__doc__,
2493 "nice(inc) -> new_priority\n\n\
2494 Decrease the priority of process by inc and return the new priority.");
2496 static PyObject *
2497 posix_nice(PyObject *self, PyObject *args)
2499 int increment, value;
2501 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2502 return NULL;
2504 /* There are two flavours of 'nice': one that returns the new
2505 priority (as required by almost all standards out there) and the
2506 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2507 the use of getpriority() to get the new priority.
2509 If we are of the nice family that returns the new priority, we
2510 need to clear errno before the call, and check if errno is filled
2511 before calling posix_error() on a returnvalue of -1, because the
2512 -1 may be the actual new priority! */
2514 errno = 0;
2515 value = nice(increment);
2516 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2517 if (value == 0)
2518 value = getpriority(PRIO_PROCESS, 0);
2519 #endif
2520 if (value == -1 && errno != 0)
2521 /* either nice() or getpriority() returned an error */
2522 return posix_error();
2523 return PyInt_FromLong((long) value);
2525 #endif /* HAVE_NICE */
2527 PyDoc_STRVAR(posix_rename__doc__,
2528 "rename(old, new)\n\n\
2529 Rename a file or directory.");
2531 static PyObject *
2532 posix_rename(PyObject *self, PyObject *args)
2534 #ifdef MS_WINDOWS
2535 PyObject *o1, *o2;
2536 char *p1, *p2;
2537 BOOL result;
2538 if (unicode_file_names()) {
2539 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2540 goto error;
2541 if (!convert_to_unicode(&o1))
2542 goto error;
2543 if (!convert_to_unicode(&o2)) {
2544 Py_DECREF(o1);
2545 goto error;
2547 Py_BEGIN_ALLOW_THREADS
2548 result = MoveFileW(PyUnicode_AsUnicode(o1),
2549 PyUnicode_AsUnicode(o2));
2550 Py_END_ALLOW_THREADS
2551 Py_DECREF(o1);
2552 Py_DECREF(o2);
2553 if (!result)
2554 return win32_error("rename", NULL);
2555 Py_INCREF(Py_None);
2556 return Py_None;
2557 error:
2558 PyErr_Clear();
2560 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2561 return NULL;
2562 Py_BEGIN_ALLOW_THREADS
2563 result = MoveFileA(p1, p2);
2564 Py_END_ALLOW_THREADS
2565 if (!result)
2566 return win32_error("rename", NULL);
2567 Py_INCREF(Py_None);
2568 return Py_None;
2569 #else
2570 return posix_2str(args, "etet:rename", rename);
2571 #endif
2575 PyDoc_STRVAR(posix_rmdir__doc__,
2576 "rmdir(path)\n\n\
2577 Remove a directory.");
2579 static PyObject *
2580 posix_rmdir(PyObject *self, PyObject *args)
2582 #ifdef MS_WINDOWS
2583 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
2584 #else
2585 return posix_1str(args, "et:rmdir", rmdir);
2586 #endif
2590 PyDoc_STRVAR(posix_stat__doc__,
2591 "stat(path) -> stat result\n\n\
2592 Perform a stat system call on the given path.");
2594 static PyObject *
2595 posix_stat(PyObject *self, PyObject *args)
2597 #ifdef MS_WINDOWS
2598 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
2599 #else
2600 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2601 #endif
2605 #ifdef HAVE_SYSTEM
2606 PyDoc_STRVAR(posix_system__doc__,
2607 "system(command) -> exit_status\n\n\
2608 Execute the command (a string) in a subshell.");
2610 static PyObject *
2611 posix_system(PyObject *self, PyObject *args)
2613 char *command;
2614 long sts;
2615 if (!PyArg_ParseTuple(args, "s:system", &command))
2616 return NULL;
2617 Py_BEGIN_ALLOW_THREADS
2618 sts = system(command);
2619 Py_END_ALLOW_THREADS
2620 return PyInt_FromLong(sts);
2622 #endif
2625 PyDoc_STRVAR(posix_umask__doc__,
2626 "umask(new_mask) -> old_mask\n\n\
2627 Set the current numeric umask and return the previous umask.");
2629 static PyObject *
2630 posix_umask(PyObject *self, PyObject *args)
2632 int i;
2633 if (!PyArg_ParseTuple(args, "i:umask", &i))
2634 return NULL;
2635 i = (int)umask(i);
2636 if (i < 0)
2637 return posix_error();
2638 return PyInt_FromLong((long)i);
2642 PyDoc_STRVAR(posix_unlink__doc__,
2643 "unlink(path)\n\n\
2644 Remove a file (same as remove(path)).");
2646 PyDoc_STRVAR(posix_remove__doc__,
2647 "remove(path)\n\n\
2648 Remove a file (same as unlink(path)).");
2650 static PyObject *
2651 posix_unlink(PyObject *self, PyObject *args)
2653 #ifdef MS_WINDOWS
2654 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
2655 #else
2656 return posix_1str(args, "et:remove", unlink);
2657 #endif
2661 #ifdef HAVE_UNAME
2662 PyDoc_STRVAR(posix_uname__doc__,
2663 "uname() -> (sysname, nodename, release, version, machine)\n\n\
2664 Return a tuple identifying the current operating system.");
2666 static PyObject *
2667 posix_uname(PyObject *self, PyObject *noargs)
2669 struct utsname u;
2670 int res;
2672 Py_BEGIN_ALLOW_THREADS
2673 res = uname(&u);
2674 Py_END_ALLOW_THREADS
2675 if (res < 0)
2676 return posix_error();
2677 return Py_BuildValue("(sssss)",
2678 u.sysname,
2679 u.nodename,
2680 u.release,
2681 u.version,
2682 u.machine);
2684 #endif /* HAVE_UNAME */
2686 static int
2687 extract_time(PyObject *t, long* sec, long* usec)
2689 long intval;
2690 if (PyFloat_Check(t)) {
2691 double tval = PyFloat_AsDouble(t);
2692 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2693 if (!intobj)
2694 return -1;
2695 intval = PyInt_AsLong(intobj);
2696 Py_DECREF(intobj);
2697 if (intval == -1 && PyErr_Occurred())
2698 return -1;
2699 *sec = intval;
2700 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2701 if (*usec < 0)
2702 /* If rounding gave us a negative number,
2703 truncate. */
2704 *usec = 0;
2705 return 0;
2707 intval = PyInt_AsLong(t);
2708 if (intval == -1 && PyErr_Occurred())
2709 return -1;
2710 *sec = intval;
2711 *usec = 0;
2712 return 0;
2715 PyDoc_STRVAR(posix_utime__doc__,
2716 "utime(path, (atime, mtime))\n\
2717 utime(path, None)\n\n\
2718 Set the access and modified time of the file to the given values. If the\n\
2719 second form is used, set the access and modified times to the current time.");
2721 static PyObject *
2722 posix_utime(PyObject *self, PyObject *args)
2724 #ifdef Py_WIN_WIDE_FILENAMES
2725 PyObject *arg;
2726 PyUnicodeObject *obwpath;
2727 wchar_t *wpath = NULL;
2728 char *apath = NULL;
2729 HANDLE hFile;
2730 long atimesec, mtimesec, ausec, musec;
2731 FILETIME atime, mtime;
2732 PyObject *result = NULL;
2734 if (unicode_file_names()) {
2735 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2736 wpath = PyUnicode_AS_UNICODE(obwpath);
2737 Py_BEGIN_ALLOW_THREADS
2738 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2739 NULL, OPEN_EXISTING,
2740 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2741 Py_END_ALLOW_THREADS
2742 if (hFile == INVALID_HANDLE_VALUE)
2743 return win32_error_unicode("utime", wpath);
2744 } else
2745 /* Drop the argument parsing error as narrow strings
2746 are also valid. */
2747 PyErr_Clear();
2749 if (!wpath) {
2750 if (!PyArg_ParseTuple(args, "etO:utime",
2751 Py_FileSystemDefaultEncoding, &apath, &arg))
2752 return NULL;
2753 Py_BEGIN_ALLOW_THREADS
2754 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2755 NULL, OPEN_EXISTING,
2756 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2757 Py_END_ALLOW_THREADS
2758 if (hFile == INVALID_HANDLE_VALUE) {
2759 win32_error("utime", apath);
2760 PyMem_Free(apath);
2761 return NULL;
2763 PyMem_Free(apath);
2766 if (arg == Py_None) {
2767 SYSTEMTIME now;
2768 GetSystemTime(&now);
2769 if (!SystemTimeToFileTime(&now, &mtime) ||
2770 !SystemTimeToFileTime(&now, &atime)) {
2771 win32_error("utime", NULL);
2772 goto done;
2775 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2776 PyErr_SetString(PyExc_TypeError,
2777 "utime() arg 2 must be a tuple (atime, mtime)");
2778 goto done;
2780 else {
2781 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2782 &atimesec, &ausec) == -1)
2783 goto done;
2784 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2785 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2786 &mtimesec, &musec) == -1)
2787 goto done;
2788 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2790 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2791 /* Avoid putting the file name into the error here,
2792 as that may confuse the user into believing that
2793 something is wrong with the file, when it also
2794 could be the time stamp that gives a problem. */
2795 win32_error("utime", NULL);
2797 Py_INCREF(Py_None);
2798 result = Py_None;
2799 done:
2800 CloseHandle(hFile);
2801 return result;
2802 #else /* Py_WIN_WIDE_FILENAMES */
2804 char *path = NULL;
2805 long atime, mtime, ausec, musec;
2806 int res;
2807 PyObject* arg;
2809 #if defined(HAVE_UTIMES)
2810 struct timeval buf[2];
2811 #define ATIME buf[0].tv_sec
2812 #define MTIME buf[1].tv_sec
2813 #elif defined(HAVE_UTIME_H)
2814 /* XXX should define struct utimbuf instead, above */
2815 struct utimbuf buf;
2816 #define ATIME buf.actime
2817 #define MTIME buf.modtime
2818 #define UTIME_ARG &buf
2819 #else /* HAVE_UTIMES */
2820 time_t buf[2];
2821 #define ATIME buf[0]
2822 #define MTIME buf[1]
2823 #define UTIME_ARG buf
2824 #endif /* HAVE_UTIMES */
2827 if (!PyArg_ParseTuple(args, "etO:utime",
2828 Py_FileSystemDefaultEncoding, &path, &arg))
2829 return NULL;
2830 if (arg == Py_None) {
2831 /* optional time values not given */
2832 Py_BEGIN_ALLOW_THREADS
2833 res = utime(path, NULL);
2834 Py_END_ALLOW_THREADS
2836 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2837 PyErr_SetString(PyExc_TypeError,
2838 "utime() arg 2 must be a tuple (atime, mtime)");
2839 PyMem_Free(path);
2840 return NULL;
2842 else {
2843 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2844 &atime, &ausec) == -1) {
2845 PyMem_Free(path);
2846 return NULL;
2848 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2849 &mtime, &musec) == -1) {
2850 PyMem_Free(path);
2851 return NULL;
2853 ATIME = atime;
2854 MTIME = mtime;
2855 #ifdef HAVE_UTIMES
2856 buf[0].tv_usec = ausec;
2857 buf[1].tv_usec = musec;
2858 Py_BEGIN_ALLOW_THREADS
2859 res = utimes(path, buf);
2860 Py_END_ALLOW_THREADS
2861 #else
2862 Py_BEGIN_ALLOW_THREADS
2863 res = utime(path, UTIME_ARG);
2864 Py_END_ALLOW_THREADS
2865 #endif /* HAVE_UTIMES */
2867 if (res < 0) {
2868 return posix_error_with_allocated_filename(path);
2870 PyMem_Free(path);
2871 Py_INCREF(Py_None);
2872 return Py_None;
2873 #undef UTIME_ARG
2874 #undef ATIME
2875 #undef MTIME
2876 #endif /* Py_WIN_WIDE_FILENAMES */
2880 /* Process operations */
2882 PyDoc_STRVAR(posix__exit__doc__,
2883 "_exit(status)\n\n\
2884 Exit to the system with specified status, without normal exit processing.");
2886 static PyObject *
2887 posix__exit(PyObject *self, PyObject *args)
2889 int sts;
2890 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2891 return NULL;
2892 _exit(sts);
2893 return NULL; /* Make gcc -Wall happy */
2896 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2897 static void
2898 free_string_array(char **array, Py_ssize_t count)
2900 Py_ssize_t i;
2901 for (i = 0; i < count; i++)
2902 PyMem_Free(array[i]);
2903 PyMem_DEL(array);
2905 #endif
2908 #ifdef HAVE_EXECV
2909 PyDoc_STRVAR(posix_execv__doc__,
2910 "execv(path, args)\n\n\
2911 Execute an executable path with arguments, replacing current process.\n\
2913 path: path of executable file\n\
2914 args: tuple or list of strings");
2916 static PyObject *
2917 posix_execv(PyObject *self, PyObject *args)
2919 char *path;
2920 PyObject *argv;
2921 char **argvlist;
2922 Py_ssize_t i, argc;
2923 PyObject *(*getitem)(PyObject *, Py_ssize_t);
2925 /* execv has two arguments: (path, argv), where
2926 argv is a list or tuple of strings. */
2928 if (!PyArg_ParseTuple(args, "etO:execv",
2929 Py_FileSystemDefaultEncoding,
2930 &path, &argv))
2931 return NULL;
2932 if (PyList_Check(argv)) {
2933 argc = PyList_Size(argv);
2934 getitem = PyList_GetItem;
2936 else if (PyTuple_Check(argv)) {
2937 argc = PyTuple_Size(argv);
2938 getitem = PyTuple_GetItem;
2940 else {
2941 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2942 PyMem_Free(path);
2943 return NULL;
2946 argvlist = PyMem_NEW(char *, argc+1);
2947 if (argvlist == NULL) {
2948 PyMem_Free(path);
2949 return PyErr_NoMemory();
2951 for (i = 0; i < argc; i++) {
2952 if (!PyArg_Parse((*getitem)(argv, i), "et",
2953 Py_FileSystemDefaultEncoding,
2954 &argvlist[i])) {
2955 free_string_array(argvlist, i);
2956 PyErr_SetString(PyExc_TypeError,
2957 "execv() arg 2 must contain only strings");
2958 PyMem_Free(path);
2959 return NULL;
2963 argvlist[argc] = NULL;
2965 execv(path, argvlist);
2967 /* If we get here it's definitely an error */
2969 free_string_array(argvlist, argc);
2970 PyMem_Free(path);
2971 return posix_error();
2975 PyDoc_STRVAR(posix_execve__doc__,
2976 "execve(path, args, env)\n\n\
2977 Execute a path with arguments and environment, replacing current process.\n\
2979 path: path of executable file\n\
2980 args: tuple or list of arguments\n\
2981 env: dictionary of strings mapping to strings");
2983 static PyObject *
2984 posix_execve(PyObject *self, PyObject *args)
2986 char *path;
2987 PyObject *argv, *env;
2988 char **argvlist;
2989 char **envlist;
2990 PyObject *key, *val, *keys=NULL, *vals=NULL;
2991 Py_ssize_t i, pos, argc, envc;
2992 PyObject *(*getitem)(PyObject *, Py_ssize_t);
2993 Py_ssize_t lastarg = 0;
2995 /* execve has three arguments: (path, argv, env), where
2996 argv is a list or tuple of strings and env is a dictionary
2997 like posix.environ. */
2999 if (!PyArg_ParseTuple(args, "etOO:execve",
3000 Py_FileSystemDefaultEncoding,
3001 &path, &argv, &env))
3002 return NULL;
3003 if (PyList_Check(argv)) {
3004 argc = PyList_Size(argv);
3005 getitem = PyList_GetItem;
3007 else if (PyTuple_Check(argv)) {
3008 argc = PyTuple_Size(argv);
3009 getitem = PyTuple_GetItem;
3011 else {
3012 PyErr_SetString(PyExc_TypeError,
3013 "execve() arg 2 must be a tuple or list");
3014 goto fail_0;
3016 if (!PyMapping_Check(env)) {
3017 PyErr_SetString(PyExc_TypeError,
3018 "execve() arg 3 must be a mapping object");
3019 goto fail_0;
3022 argvlist = PyMem_NEW(char *, argc+1);
3023 if (argvlist == NULL) {
3024 PyErr_NoMemory();
3025 goto fail_0;
3027 for (i = 0; i < argc; i++) {
3028 if (!PyArg_Parse((*getitem)(argv, i),
3029 "et;execve() arg 2 must contain only strings",
3030 Py_FileSystemDefaultEncoding,
3031 &argvlist[i]))
3033 lastarg = i;
3034 goto fail_1;
3037 lastarg = argc;
3038 argvlist[argc] = NULL;
3040 i = PyMapping_Size(env);
3041 if (i < 0)
3042 goto fail_1;
3043 envlist = PyMem_NEW(char *, i + 1);
3044 if (envlist == NULL) {
3045 PyErr_NoMemory();
3046 goto fail_1;
3048 envc = 0;
3049 keys = PyMapping_Keys(env);
3050 vals = PyMapping_Values(env);
3051 if (!keys || !vals)
3052 goto fail_2;
3053 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3054 PyErr_SetString(PyExc_TypeError,
3055 "execve(): env.keys() or env.values() is not a list");
3056 goto fail_2;
3059 for (pos = 0; pos < i; pos++) {
3060 char *p, *k, *v;
3061 size_t len;
3063 key = PyList_GetItem(keys, pos);
3064 val = PyList_GetItem(vals, pos);
3065 if (!key || !val)
3066 goto fail_2;
3068 if (!PyArg_Parse(
3069 key,
3070 "s;execve() arg 3 contains a non-string key",
3071 &k) ||
3072 !PyArg_Parse(
3073 val,
3074 "s;execve() arg 3 contains a non-string value",
3075 &v))
3077 goto fail_2;
3080 #if defined(PYOS_OS2)
3081 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3082 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3083 #endif
3084 len = PyString_Size(key) + PyString_Size(val) + 2;
3085 p = PyMem_NEW(char, len);
3086 if (p == NULL) {
3087 PyErr_NoMemory();
3088 goto fail_2;
3090 PyOS_snprintf(p, len, "%s=%s", k, v);
3091 envlist[envc++] = p;
3092 #if defined(PYOS_OS2)
3094 #endif
3096 envlist[envc] = 0;
3098 execve(path, argvlist, envlist);
3100 /* If we get here it's definitely an error */
3102 (void) posix_error();
3104 fail_2:
3105 while (--envc >= 0)
3106 PyMem_DEL(envlist[envc]);
3107 PyMem_DEL(envlist);
3108 fail_1:
3109 free_string_array(argvlist, lastarg);
3110 Py_XDECREF(vals);
3111 Py_XDECREF(keys);
3112 fail_0:
3113 PyMem_Free(path);
3114 return NULL;
3116 #endif /* HAVE_EXECV */
3119 #ifdef HAVE_SPAWNV
3120 PyDoc_STRVAR(posix_spawnv__doc__,
3121 "spawnv(mode, path, args)\n\n\
3122 Execute the program 'path' in a new process.\n\
3124 mode: mode of process creation\n\
3125 path: path of executable file\n\
3126 args: tuple or list of strings");
3128 static PyObject *
3129 posix_spawnv(PyObject *self, PyObject *args)
3131 char *path;
3132 PyObject *argv;
3133 char **argvlist;
3134 int mode, i;
3135 Py_ssize_t argc;
3136 Py_intptr_t spawnval;
3137 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3139 /* spawnv has three arguments: (mode, path, argv), where
3140 argv is a list or tuple of strings. */
3142 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3143 Py_FileSystemDefaultEncoding,
3144 &path, &argv))
3145 return NULL;
3146 if (PyList_Check(argv)) {
3147 argc = PyList_Size(argv);
3148 getitem = PyList_GetItem;
3150 else if (PyTuple_Check(argv)) {
3151 argc = PyTuple_Size(argv);
3152 getitem = PyTuple_GetItem;
3154 else {
3155 PyErr_SetString(PyExc_TypeError,
3156 "spawnv() arg 2 must be a tuple or list");
3157 PyMem_Free(path);
3158 return NULL;
3161 argvlist = PyMem_NEW(char *, argc+1);
3162 if (argvlist == NULL) {
3163 PyMem_Free(path);
3164 return PyErr_NoMemory();
3166 for (i = 0; i < argc; i++) {
3167 if (!PyArg_Parse((*getitem)(argv, i), "et",
3168 Py_FileSystemDefaultEncoding,
3169 &argvlist[i])) {
3170 free_string_array(argvlist, i);
3171 PyErr_SetString(
3172 PyExc_TypeError,
3173 "spawnv() arg 2 must contain only strings");
3174 PyMem_Free(path);
3175 return NULL;
3178 argvlist[argc] = NULL;
3180 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3181 Py_BEGIN_ALLOW_THREADS
3182 spawnval = spawnv(mode, path, argvlist);
3183 Py_END_ALLOW_THREADS
3184 #else
3185 if (mode == _OLD_P_OVERLAY)
3186 mode = _P_OVERLAY;
3188 Py_BEGIN_ALLOW_THREADS
3189 spawnval = _spawnv(mode, path, argvlist);
3190 Py_END_ALLOW_THREADS
3191 #endif
3193 free_string_array(argvlist, argc);
3194 PyMem_Free(path);
3196 if (spawnval == -1)
3197 return posix_error();
3198 else
3199 #if SIZEOF_LONG == SIZEOF_VOID_P
3200 return Py_BuildValue("l", (long) spawnval);
3201 #else
3202 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3203 #endif
3207 PyDoc_STRVAR(posix_spawnve__doc__,
3208 "spawnve(mode, path, args, env)\n\n\
3209 Execute the program 'path' in a new process.\n\
3211 mode: mode of process creation\n\
3212 path: path of executable file\n\
3213 args: tuple or list of arguments\n\
3214 env: dictionary of strings mapping to strings");
3216 static PyObject *
3217 posix_spawnve(PyObject *self, PyObject *args)
3219 char *path;
3220 PyObject *argv, *env;
3221 char **argvlist;
3222 char **envlist;
3223 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3224 int mode, pos, envc;
3225 Py_ssize_t argc, i;
3226 Py_intptr_t spawnval;
3227 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3228 Py_ssize_t lastarg = 0;
3230 /* spawnve has four arguments: (mode, path, argv, env), where
3231 argv is a list or tuple of strings and env is a dictionary
3232 like posix.environ. */
3234 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3235 Py_FileSystemDefaultEncoding,
3236 &path, &argv, &env))
3237 return NULL;
3238 if (PyList_Check(argv)) {
3239 argc = PyList_Size(argv);
3240 getitem = PyList_GetItem;
3242 else if (PyTuple_Check(argv)) {
3243 argc = PyTuple_Size(argv);
3244 getitem = PyTuple_GetItem;
3246 else {
3247 PyErr_SetString(PyExc_TypeError,
3248 "spawnve() arg 2 must be a tuple or list");
3249 goto fail_0;
3251 if (!PyMapping_Check(env)) {
3252 PyErr_SetString(PyExc_TypeError,
3253 "spawnve() arg 3 must be a mapping object");
3254 goto fail_0;
3257 argvlist = PyMem_NEW(char *, argc+1);
3258 if (argvlist == NULL) {
3259 PyErr_NoMemory();
3260 goto fail_0;
3262 for (i = 0; i < argc; i++) {
3263 if (!PyArg_Parse((*getitem)(argv, i),
3264 "et;spawnve() arg 2 must contain only strings",
3265 Py_FileSystemDefaultEncoding,
3266 &argvlist[i]))
3268 lastarg = i;
3269 goto fail_1;
3272 lastarg = argc;
3273 argvlist[argc] = NULL;
3275 i = PyMapping_Size(env);
3276 if (i < 0)
3277 goto fail_1;
3278 envlist = PyMem_NEW(char *, i + 1);
3279 if (envlist == NULL) {
3280 PyErr_NoMemory();
3281 goto fail_1;
3283 envc = 0;
3284 keys = PyMapping_Keys(env);
3285 vals = PyMapping_Values(env);
3286 if (!keys || !vals)
3287 goto fail_2;
3288 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3289 PyErr_SetString(PyExc_TypeError,
3290 "spawnve(): env.keys() or env.values() is not a list");
3291 goto fail_2;
3294 for (pos = 0; pos < i; pos++) {
3295 char *p, *k, *v;
3296 size_t len;
3298 key = PyList_GetItem(keys, pos);
3299 val = PyList_GetItem(vals, pos);
3300 if (!key || !val)
3301 goto fail_2;
3303 if (!PyArg_Parse(
3304 key,
3305 "s;spawnve() arg 3 contains a non-string key",
3306 &k) ||
3307 !PyArg_Parse(
3308 val,
3309 "s;spawnve() arg 3 contains a non-string value",
3310 &v))
3312 goto fail_2;
3314 len = PyString_Size(key) + PyString_Size(val) + 2;
3315 p = PyMem_NEW(char, len);
3316 if (p == NULL) {
3317 PyErr_NoMemory();
3318 goto fail_2;
3320 PyOS_snprintf(p, len, "%s=%s", k, v);
3321 envlist[envc++] = p;
3323 envlist[envc] = 0;
3325 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3326 Py_BEGIN_ALLOW_THREADS
3327 spawnval = spawnve(mode, path, argvlist, envlist);
3328 Py_END_ALLOW_THREADS
3329 #else
3330 if (mode == _OLD_P_OVERLAY)
3331 mode = _P_OVERLAY;
3333 Py_BEGIN_ALLOW_THREADS
3334 spawnval = _spawnve(mode, path, argvlist, envlist);
3335 Py_END_ALLOW_THREADS
3336 #endif
3338 if (spawnval == -1)
3339 (void) posix_error();
3340 else
3341 #if SIZEOF_LONG == SIZEOF_VOID_P
3342 res = Py_BuildValue("l", (long) spawnval);
3343 #else
3344 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3345 #endif
3347 fail_2:
3348 while (--envc >= 0)
3349 PyMem_DEL(envlist[envc]);
3350 PyMem_DEL(envlist);
3351 fail_1:
3352 free_string_array(argvlist, lastarg);
3353 Py_XDECREF(vals);
3354 Py_XDECREF(keys);
3355 fail_0:
3356 PyMem_Free(path);
3357 return res;
3360 /* OS/2 supports spawnvp & spawnvpe natively */
3361 #if defined(PYOS_OS2)
3362 PyDoc_STRVAR(posix_spawnvp__doc__,
3363 "spawnvp(mode, file, args)\n\n\
3364 Execute the program 'file' in a new process, using the environment\n\
3365 search path to find the file.\n\
3367 mode: mode of process creation\n\
3368 file: executable file name\n\
3369 args: tuple or list of strings");
3371 static PyObject *
3372 posix_spawnvp(PyObject *self, PyObject *args)
3374 char *path;
3375 PyObject *argv;
3376 char **argvlist;
3377 int mode, i, argc;
3378 Py_intptr_t spawnval;
3379 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3381 /* spawnvp has three arguments: (mode, path, argv), where
3382 argv is a list or tuple of strings. */
3384 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3385 Py_FileSystemDefaultEncoding,
3386 &path, &argv))
3387 return NULL;
3388 if (PyList_Check(argv)) {
3389 argc = PyList_Size(argv);
3390 getitem = PyList_GetItem;
3392 else if (PyTuple_Check(argv)) {
3393 argc = PyTuple_Size(argv);
3394 getitem = PyTuple_GetItem;
3396 else {
3397 PyErr_SetString(PyExc_TypeError,
3398 "spawnvp() arg 2 must be a tuple or list");
3399 PyMem_Free(path);
3400 return NULL;
3403 argvlist = PyMem_NEW(char *, argc+1);
3404 if (argvlist == NULL) {
3405 PyMem_Free(path);
3406 return PyErr_NoMemory();
3408 for (i = 0; i < argc; i++) {
3409 if (!PyArg_Parse((*getitem)(argv, i), "et",
3410 Py_FileSystemDefaultEncoding,
3411 &argvlist[i])) {
3412 free_string_array(argvlist, i);
3413 PyErr_SetString(
3414 PyExc_TypeError,
3415 "spawnvp() arg 2 must contain only strings");
3416 PyMem_Free(path);
3417 return NULL;
3420 argvlist[argc] = NULL;
3422 Py_BEGIN_ALLOW_THREADS
3423 #if defined(PYCC_GCC)
3424 spawnval = spawnvp(mode, path, argvlist);
3425 #else
3426 spawnval = _spawnvp(mode, path, argvlist);
3427 #endif
3428 Py_END_ALLOW_THREADS
3430 free_string_array(argvlist, argc);
3431 PyMem_Free(path);
3433 if (spawnval == -1)
3434 return posix_error();
3435 else
3436 return Py_BuildValue("l", (long) spawnval);
3440 PyDoc_STRVAR(posix_spawnvpe__doc__,
3441 "spawnvpe(mode, file, args, env)\n\n\
3442 Execute the program 'file' in a new process, using the environment\n\
3443 search path to find the file.\n\
3445 mode: mode of process creation\n\
3446 file: executable file name\n\
3447 args: tuple or list of arguments\n\
3448 env: dictionary of strings mapping to strings");
3450 static PyObject *
3451 posix_spawnvpe(PyObject *self, PyObject *args)
3453 char *path;
3454 PyObject *argv, *env;
3455 char **argvlist;
3456 char **envlist;
3457 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3458 int mode, i, pos, argc, envc;
3459 Py_intptr_t spawnval;
3460 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3461 int lastarg = 0;
3463 /* spawnvpe has four arguments: (mode, path, argv, env), where
3464 argv is a list or tuple of strings and env is a dictionary
3465 like posix.environ. */
3467 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3468 Py_FileSystemDefaultEncoding,
3469 &path, &argv, &env))
3470 return NULL;
3471 if (PyList_Check(argv)) {
3472 argc = PyList_Size(argv);
3473 getitem = PyList_GetItem;
3475 else if (PyTuple_Check(argv)) {
3476 argc = PyTuple_Size(argv);
3477 getitem = PyTuple_GetItem;
3479 else {
3480 PyErr_SetString(PyExc_TypeError,
3481 "spawnvpe() arg 2 must be a tuple or list");
3482 goto fail_0;
3484 if (!PyMapping_Check(env)) {
3485 PyErr_SetString(PyExc_TypeError,
3486 "spawnvpe() arg 3 must be a mapping object");
3487 goto fail_0;
3490 argvlist = PyMem_NEW(char *, argc+1);
3491 if (argvlist == NULL) {
3492 PyErr_NoMemory();
3493 goto fail_0;
3495 for (i = 0; i < argc; i++) {
3496 if (!PyArg_Parse((*getitem)(argv, i),
3497 "et;spawnvpe() arg 2 must contain only strings",
3498 Py_FileSystemDefaultEncoding,
3499 &argvlist[i]))
3501 lastarg = i;
3502 goto fail_1;
3505 lastarg = argc;
3506 argvlist[argc] = NULL;
3508 i = PyMapping_Size(env);
3509 if (i < 0)
3510 goto fail_1;
3511 envlist = PyMem_NEW(char *, i + 1);
3512 if (envlist == NULL) {
3513 PyErr_NoMemory();
3514 goto fail_1;
3516 envc = 0;
3517 keys = PyMapping_Keys(env);
3518 vals = PyMapping_Values(env);
3519 if (!keys || !vals)
3520 goto fail_2;
3521 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3522 PyErr_SetString(PyExc_TypeError,
3523 "spawnvpe(): env.keys() or env.values() is not a list");
3524 goto fail_2;
3527 for (pos = 0; pos < i; pos++) {
3528 char *p, *k, *v;
3529 size_t len;
3531 key = PyList_GetItem(keys, pos);
3532 val = PyList_GetItem(vals, pos);
3533 if (!key || !val)
3534 goto fail_2;
3536 if (!PyArg_Parse(
3537 key,
3538 "s;spawnvpe() arg 3 contains a non-string key",
3539 &k) ||
3540 !PyArg_Parse(
3541 val,
3542 "s;spawnvpe() arg 3 contains a non-string value",
3543 &v))
3545 goto fail_2;
3547 len = PyString_Size(key) + PyString_Size(val) + 2;
3548 p = PyMem_NEW(char, len);
3549 if (p == NULL) {
3550 PyErr_NoMemory();
3551 goto fail_2;
3553 PyOS_snprintf(p, len, "%s=%s", k, v);
3554 envlist[envc++] = p;
3556 envlist[envc] = 0;
3558 Py_BEGIN_ALLOW_THREADS
3559 #if defined(PYCC_GCC)
3560 spawnval = spawnvpe(mode, path, argvlist, envlist);
3561 #else
3562 spawnval = _spawnvpe(mode, path, argvlist, envlist);
3563 #endif
3564 Py_END_ALLOW_THREADS
3566 if (spawnval == -1)
3567 (void) posix_error();
3568 else
3569 res = Py_BuildValue("l", (long) spawnval);
3571 fail_2:
3572 while (--envc >= 0)
3573 PyMem_DEL(envlist[envc]);
3574 PyMem_DEL(envlist);
3575 fail_1:
3576 free_string_array(argvlist, lastarg);
3577 Py_XDECREF(vals);
3578 Py_XDECREF(keys);
3579 fail_0:
3580 PyMem_Free(path);
3581 return res;
3583 #endif /* PYOS_OS2 */
3584 #endif /* HAVE_SPAWNV */
3587 #ifdef HAVE_FORK1
3588 PyDoc_STRVAR(posix_fork1__doc__,
3589 "fork1() -> pid\n\n\
3590 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3592 Return 0 to child process and PID of child to parent process.");
3594 static PyObject *
3595 posix_fork1(PyObject *self, PyObject *noargs)
3597 pid_t pid = fork1();
3598 if (pid == -1)
3599 return posix_error();
3600 if (pid == 0)
3601 PyOS_AfterFork();
3602 return PyInt_FromLong(pid);
3604 #endif
3607 #ifdef HAVE_FORK
3608 PyDoc_STRVAR(posix_fork__doc__,
3609 "fork() -> pid\n\n\
3610 Fork a child process.\n\
3611 Return 0 to child process and PID of child to parent process.");
3613 static PyObject *
3614 posix_fork(PyObject *self, PyObject *noargs)
3616 pid_t pid = fork();
3617 if (pid == -1)
3618 return posix_error();
3619 if (pid == 0)
3620 PyOS_AfterFork();
3621 return PyInt_FromLong(pid);
3623 #endif
3625 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3626 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3627 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3628 #define DEV_PTY_FILE "/dev/ptc"
3629 #define HAVE_DEV_PTMX
3630 #else
3631 #define DEV_PTY_FILE "/dev/ptmx"
3632 #endif
3634 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3635 #ifdef HAVE_PTY_H
3636 #include <pty.h>
3637 #else
3638 #ifdef HAVE_LIBUTIL_H
3639 #include <libutil.h>
3640 #endif /* HAVE_LIBUTIL_H */
3641 #endif /* HAVE_PTY_H */
3642 #ifdef HAVE_STROPTS_H
3643 #include <stropts.h>
3644 #endif
3645 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3647 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3648 PyDoc_STRVAR(posix_openpty__doc__,
3649 "openpty() -> (master_fd, slave_fd)\n\n\
3650 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3652 static PyObject *
3653 posix_openpty(PyObject *self, PyObject *noargs)
3655 int master_fd, slave_fd;
3656 #ifndef HAVE_OPENPTY
3657 char * slave_name;
3658 #endif
3659 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3660 PyOS_sighandler_t sig_saved;
3661 #ifdef sun
3662 extern char *ptsname(int fildes);
3663 #endif
3664 #endif
3666 #ifdef HAVE_OPENPTY
3667 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3668 return posix_error();
3669 #elif defined(HAVE__GETPTY)
3670 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3671 if (slave_name == NULL)
3672 return posix_error();
3674 slave_fd = open(slave_name, O_RDWR);
3675 if (slave_fd < 0)
3676 return posix_error();
3677 #else
3678 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3679 if (master_fd < 0)
3680 return posix_error();
3681 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3682 /* change permission of slave */
3683 if (grantpt(master_fd) < 0) {
3684 PyOS_setsig(SIGCHLD, sig_saved);
3685 return posix_error();
3687 /* unlock slave */
3688 if (unlockpt(master_fd) < 0) {
3689 PyOS_setsig(SIGCHLD, sig_saved);
3690 return posix_error();
3692 PyOS_setsig(SIGCHLD, sig_saved);
3693 slave_name = ptsname(master_fd); /* get name of slave */
3694 if (slave_name == NULL)
3695 return posix_error();
3696 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3697 if (slave_fd < 0)
3698 return posix_error();
3699 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3700 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3701 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
3702 #ifndef __hpux
3703 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
3704 #endif /* __hpux */
3705 #endif /* HAVE_CYGWIN */
3706 #endif /* HAVE_OPENPTY */
3708 return Py_BuildValue("(ii)", master_fd, slave_fd);
3711 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3713 #ifdef HAVE_FORKPTY
3714 PyDoc_STRVAR(posix_forkpty__doc__,
3715 "forkpty() -> (pid, master_fd)\n\n\
3716 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3717 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3718 To both, return fd of newly opened pseudo-terminal.\n");
3720 static PyObject *
3721 posix_forkpty(PyObject *self, PyObject *noargs)
3723 int master_fd = -1;
3724 pid_t pid;
3726 pid = forkpty(&master_fd, NULL, NULL, NULL);
3727 if (pid == -1)
3728 return posix_error();
3729 if (pid == 0)
3730 PyOS_AfterFork();
3731 return Py_BuildValue("(li)", pid, master_fd);
3733 #endif
3735 #ifdef HAVE_GETEGID
3736 PyDoc_STRVAR(posix_getegid__doc__,
3737 "getegid() -> egid\n\n\
3738 Return the current process's effective group id.");
3740 static PyObject *
3741 posix_getegid(PyObject *self, PyObject *noargs)
3743 return PyInt_FromLong((long)getegid());
3745 #endif
3748 #ifdef HAVE_GETEUID
3749 PyDoc_STRVAR(posix_geteuid__doc__,
3750 "geteuid() -> euid\n\n\
3751 Return the current process's effective user id.");
3753 static PyObject *
3754 posix_geteuid(PyObject *self, PyObject *noargs)
3756 return PyInt_FromLong((long)geteuid());
3758 #endif
3761 #ifdef HAVE_GETGID
3762 PyDoc_STRVAR(posix_getgid__doc__,
3763 "getgid() -> gid\n\n\
3764 Return the current process's group id.");
3766 static PyObject *
3767 posix_getgid(PyObject *self, PyObject *noargs)
3769 return PyInt_FromLong((long)getgid());
3771 #endif
3774 PyDoc_STRVAR(posix_getpid__doc__,
3775 "getpid() -> pid\n\n\
3776 Return the current process id");
3778 static PyObject *
3779 posix_getpid(PyObject *self, PyObject *noargs)
3781 return PyInt_FromLong((long)getpid());
3785 #ifdef HAVE_GETGROUPS
3786 PyDoc_STRVAR(posix_getgroups__doc__,
3787 "getgroups() -> list of group IDs\n\n\
3788 Return list of supplemental group IDs for the process.");
3790 static PyObject *
3791 posix_getgroups(PyObject *self, PyObject *noargs)
3793 PyObject *result = NULL;
3795 #ifdef NGROUPS_MAX
3796 #define MAX_GROUPS NGROUPS_MAX
3797 #else
3798 /* defined to be 16 on Solaris7, so this should be a small number */
3799 #define MAX_GROUPS 64
3800 #endif
3801 gid_t grouplist[MAX_GROUPS];
3802 int n;
3804 n = getgroups(MAX_GROUPS, grouplist);
3805 if (n < 0)
3806 posix_error();
3807 else {
3808 result = PyList_New(n);
3809 if (result != NULL) {
3810 int i;
3811 for (i = 0; i < n; ++i) {
3812 PyObject *o = PyInt_FromLong((long)grouplist[i]);
3813 if (o == NULL) {
3814 Py_DECREF(result);
3815 result = NULL;
3816 break;
3818 PyList_SET_ITEM(result, i, o);
3823 return result;
3825 #endif
3827 #ifdef HAVE_GETPGID
3828 PyDoc_STRVAR(posix_getpgid__doc__,
3829 "getpgid(pid) -> pgid\n\n\
3830 Call the system call getpgid().");
3832 static PyObject *
3833 posix_getpgid(PyObject *self, PyObject *args)
3835 int pid, pgid;
3836 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3837 return NULL;
3838 pgid = getpgid(pid);
3839 if (pgid < 0)
3840 return posix_error();
3841 return PyInt_FromLong((long)pgid);
3843 #endif /* HAVE_GETPGID */
3846 #ifdef HAVE_GETPGRP
3847 PyDoc_STRVAR(posix_getpgrp__doc__,
3848 "getpgrp() -> pgrp\n\n\
3849 Return the current process group id.");
3851 static PyObject *
3852 posix_getpgrp(PyObject *self, PyObject *noargs)
3854 #ifdef GETPGRP_HAVE_ARG
3855 return PyInt_FromLong((long)getpgrp(0));
3856 #else /* GETPGRP_HAVE_ARG */
3857 return PyInt_FromLong((long)getpgrp());
3858 #endif /* GETPGRP_HAVE_ARG */
3860 #endif /* HAVE_GETPGRP */
3863 #ifdef HAVE_SETPGRP
3864 PyDoc_STRVAR(posix_setpgrp__doc__,
3865 "setpgrp()\n\n\
3866 Make this process a session leader.");
3868 static PyObject *
3869 posix_setpgrp(PyObject *self, PyObject *noargs)
3871 #ifdef SETPGRP_HAVE_ARG
3872 if (setpgrp(0, 0) < 0)
3873 #else /* SETPGRP_HAVE_ARG */
3874 if (setpgrp() < 0)
3875 #endif /* SETPGRP_HAVE_ARG */
3876 return posix_error();
3877 Py_INCREF(Py_None);
3878 return Py_None;
3881 #endif /* HAVE_SETPGRP */
3883 #ifdef HAVE_GETPPID
3884 PyDoc_STRVAR(posix_getppid__doc__,
3885 "getppid() -> ppid\n\n\
3886 Return the parent's process id.");
3888 static PyObject *
3889 posix_getppid(PyObject *self, PyObject *noargs)
3891 return PyInt_FromLong(getppid());
3893 #endif
3896 #ifdef HAVE_GETLOGIN
3897 PyDoc_STRVAR(posix_getlogin__doc__,
3898 "getlogin() -> string\n\n\
3899 Return the actual login name.");
3901 static PyObject *
3902 posix_getlogin(PyObject *self, PyObject *noargs)
3904 PyObject *result = NULL;
3905 char *name;
3906 int old_errno = errno;
3908 errno = 0;
3909 name = getlogin();
3910 if (name == NULL) {
3911 if (errno)
3912 posix_error();
3913 else
3914 PyErr_SetString(PyExc_OSError,
3915 "unable to determine login name");
3917 else
3918 result = PyString_FromString(name);
3919 errno = old_errno;
3921 return result;
3923 #endif
3925 #ifdef HAVE_GETUID
3926 PyDoc_STRVAR(posix_getuid__doc__,
3927 "getuid() -> uid\n\n\
3928 Return the current process's user id.");
3930 static PyObject *
3931 posix_getuid(PyObject *self, PyObject *noargs)
3933 return PyInt_FromLong((long)getuid());
3935 #endif
3938 #ifdef HAVE_KILL
3939 PyDoc_STRVAR(posix_kill__doc__,
3940 "kill(pid, sig)\n\n\
3941 Kill a process with a signal.");
3943 static PyObject *
3944 posix_kill(PyObject *self, PyObject *args)
3946 pid_t pid;
3947 int sig;
3948 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
3949 return NULL;
3950 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
3951 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3952 APIRET rc;
3953 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
3954 return os2_error(rc);
3956 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3957 APIRET rc;
3958 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
3959 return os2_error(rc);
3961 } else
3962 return NULL; /* Unrecognized Signal Requested */
3963 #else
3964 if (kill(pid, sig) == -1)
3965 return posix_error();
3966 #endif
3967 Py_INCREF(Py_None);
3968 return Py_None;
3970 #endif
3972 #ifdef HAVE_KILLPG
3973 PyDoc_STRVAR(posix_killpg__doc__,
3974 "killpg(pgid, sig)\n\n\
3975 Kill a process group with a signal.");
3977 static PyObject *
3978 posix_killpg(PyObject *self, PyObject *args)
3980 int pgid, sig;
3981 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3982 return NULL;
3983 if (killpg(pgid, sig) == -1)
3984 return posix_error();
3985 Py_INCREF(Py_None);
3986 return Py_None;
3988 #endif
3990 #ifdef HAVE_PLOCK
3992 #ifdef HAVE_SYS_LOCK_H
3993 #include <sys/lock.h>
3994 #endif
3996 PyDoc_STRVAR(posix_plock__doc__,
3997 "plock(op)\n\n\
3998 Lock program segments into memory.");
4000 static PyObject *
4001 posix_plock(PyObject *self, PyObject *args)
4003 int op;
4004 if (!PyArg_ParseTuple(args, "i:plock", &op))
4005 return NULL;
4006 if (plock(op) == -1)
4007 return posix_error();
4008 Py_INCREF(Py_None);
4009 return Py_None;
4011 #endif
4014 #ifdef HAVE_POPEN
4015 PyDoc_STRVAR(posix_popen__doc__,
4016 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
4017 Open a pipe to/from a command returning a file object.");
4019 #if defined(PYOS_OS2)
4020 #if defined(PYCC_VACPP)
4021 static int
4022 async_system(const char *command)
4024 char errormsg[256], args[1024];
4025 RESULTCODES rcodes;
4026 APIRET rc;
4028 char *shell = getenv("COMSPEC");
4029 if (!shell)
4030 shell = "cmd";
4032 /* avoid overflowing the argument buffer */
4033 if (strlen(shell) + 3 + strlen(command) >= 1024)
4034 return ERROR_NOT_ENOUGH_MEMORY
4036 args[0] = '\0';
4037 strcat(args, shell);
4038 strcat(args, "/c ");
4039 strcat(args, command);
4041 /* execute asynchronously, inheriting the environment */
4042 rc = DosExecPgm(errormsg,
4043 sizeof(errormsg),
4044 EXEC_ASYNC,
4045 args,
4046 NULL,
4047 &rcodes,
4048 shell);
4049 return rc;
4052 static FILE *
4053 popen(const char *command, const char *mode, int pipesize, int *err)
4055 int oldfd, tgtfd;
4056 HFILE pipeh[2];
4057 APIRET rc;
4059 /* mode determines which of stdin or stdout is reconnected to
4060 * the pipe to the child
4062 if (strchr(mode, 'r') != NULL) {
4063 tgt_fd = 1; /* stdout */
4064 } else if (strchr(mode, 'w')) {
4065 tgt_fd = 0; /* stdin */
4066 } else {
4067 *err = ERROR_INVALID_ACCESS;
4068 return NULL;
4071 /* setup the pipe */
4072 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4073 *err = rc;
4074 return NULL;
4077 /* prevent other threads accessing stdio */
4078 DosEnterCritSec();
4080 /* reconnect stdio and execute child */
4081 oldfd = dup(tgtfd);
4082 close(tgtfd);
4083 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4084 DosClose(pipeh[tgtfd]);
4085 rc = async_system(command);
4088 /* restore stdio */
4089 dup2(oldfd, tgtfd);
4090 close(oldfd);
4092 /* allow other threads access to stdio */
4093 DosExitCritSec();
4095 /* if execution of child was successful return file stream */
4096 if (rc == NO_ERROR)
4097 return fdopen(pipeh[1 - tgtfd], mode);
4098 else {
4099 DosClose(pipeh[1 - tgtfd]);
4100 *err = rc;
4101 return NULL;
4105 static PyObject *
4106 posix_popen(PyObject *self, PyObject *args)
4108 char *name;
4109 char *mode = "r";
4110 int err, bufsize = -1;
4111 FILE *fp;
4112 PyObject *f;
4113 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4114 return NULL;
4115 Py_BEGIN_ALLOW_THREADS
4116 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4117 Py_END_ALLOW_THREADS
4118 if (fp == NULL)
4119 return os2_error(err);
4121 f = PyFile_FromFile(fp, name, mode, fclose);
4122 if (f != NULL)
4123 PyFile_SetBufSize(f, bufsize);
4124 return f;
4127 #elif defined(PYCC_GCC)
4129 /* standard posix version of popen() support */
4130 static PyObject *
4131 posix_popen(PyObject *self, PyObject *args)
4133 char *name;
4134 char *mode = "r";
4135 int bufsize = -1;
4136 FILE *fp;
4137 PyObject *f;
4138 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4139 return NULL;
4140 Py_BEGIN_ALLOW_THREADS
4141 fp = popen(name, mode);
4142 Py_END_ALLOW_THREADS
4143 if (fp == NULL)
4144 return posix_error();
4145 f = PyFile_FromFile(fp, name, mode, pclose);
4146 if (f != NULL)
4147 PyFile_SetBufSize(f, bufsize);
4148 return f;
4151 /* fork() under OS/2 has lots'o'warts
4152 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4153 * most of this code is a ripoff of the win32 code, but using the
4154 * capabilities of EMX's C library routines
4157 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4158 #define POPEN_1 1
4159 #define POPEN_2 2
4160 #define POPEN_3 3
4161 #define POPEN_4 4
4163 static PyObject *_PyPopen(char *, int, int, int);
4164 static int _PyPclose(FILE *file);
4167 * Internal dictionary mapping popen* file pointers to process handles,
4168 * for use when retrieving the process exit code. See _PyPclose() below
4169 * for more information on this dictionary's use.
4171 static PyObject *_PyPopenProcs = NULL;
4173 /* os2emx version of popen2()
4175 * The result of this function is a pipe (file) connected to the
4176 * process's stdin, and a pipe connected to the process's stdout.
4179 static PyObject *
4180 os2emx_popen2(PyObject *self, PyObject *args)
4182 PyObject *f;
4183 int tm=0;
4185 char *cmdstring;
4186 char *mode = "t";
4187 int bufsize = -1;
4188 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4189 return NULL;
4191 if (*mode == 't')
4192 tm = O_TEXT;
4193 else if (*mode != 'b') {
4194 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4195 return NULL;
4196 } else
4197 tm = O_BINARY;
4199 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4201 return f;
4205 * Variation on os2emx.popen2
4207 * The result of this function is 3 pipes - the process's stdin,
4208 * stdout and stderr
4211 static PyObject *
4212 os2emx_popen3(PyObject *self, PyObject *args)
4214 PyObject *f;
4215 int tm = 0;
4217 char *cmdstring;
4218 char *mode = "t";
4219 int bufsize = -1;
4220 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4221 return NULL;
4223 if (*mode == 't')
4224 tm = O_TEXT;
4225 else if (*mode != 'b') {
4226 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4227 return NULL;
4228 } else
4229 tm = O_BINARY;
4231 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4233 return f;
4237 * Variation on os2emx.popen2
4239 * The result of this function is 2 pipes - the processes stdin,
4240 * and stdout+stderr combined as a single pipe.
4243 static PyObject *
4244 os2emx_popen4(PyObject *self, PyObject *args)
4246 PyObject *f;
4247 int tm = 0;
4249 char *cmdstring;
4250 char *mode = "t";
4251 int bufsize = -1;
4252 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4253 return NULL;
4255 if (*mode == 't')
4256 tm = O_TEXT;
4257 else if (*mode != 'b') {
4258 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4259 return NULL;
4260 } else
4261 tm = O_BINARY;
4263 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4265 return f;
4268 /* a couple of structures for convenient handling of multiple
4269 * file handles and pipes
4271 struct file_ref
4273 int handle;
4274 int flags;
4277 struct pipe_ref
4279 int rd;
4280 int wr;
4283 /* The following code is derived from the win32 code */
4285 static PyObject *
4286 _PyPopen(char *cmdstring, int mode, int n, int bufsize)
4288 struct file_ref stdio[3];
4289 struct pipe_ref p_fd[3];
4290 FILE *p_s[3];
4291 int file_count, i, pipe_err;
4292 pid_t pipe_pid;
4293 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4294 PyObject *f, *p_f[3];
4296 /* file modes for subsequent fdopen's on pipe handles */
4297 if (mode == O_TEXT)
4299 rd_mode = "rt";
4300 wr_mode = "wt";
4302 else
4304 rd_mode = "rb";
4305 wr_mode = "wb";
4308 /* prepare shell references */
4309 if ((shell = getenv("EMXSHELL")) == NULL)
4310 if ((shell = getenv("COMSPEC")) == NULL)
4312 errno = ENOENT;
4313 return posix_error();
4316 sh_name = _getname(shell);
4317 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4318 opt = "/c";
4319 else
4320 opt = "-c";
4322 /* save current stdio fds + their flags, and set not inheritable */
4323 i = pipe_err = 0;
4324 while (pipe_err >= 0 && i < 3)
4326 pipe_err = stdio[i].handle = dup(i);
4327 stdio[i].flags = fcntl(i, F_GETFD, 0);
4328 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4329 i++;
4331 if (pipe_err < 0)
4333 /* didn't get them all saved - clean up and bail out */
4334 int saved_err = errno;
4335 while (i-- > 0)
4337 close(stdio[i].handle);
4339 errno = saved_err;
4340 return posix_error();
4343 /* create pipe ends */
4344 file_count = 2;
4345 if (n == POPEN_3)
4346 file_count = 3;
4347 i = pipe_err = 0;
4348 while ((pipe_err == 0) && (i < file_count))
4349 pipe_err = pipe((int *)&p_fd[i++]);
4350 if (pipe_err < 0)
4352 /* didn't get them all made - clean up and bail out */
4353 while (i-- > 0)
4355 close(p_fd[i].wr);
4356 close(p_fd[i].rd);
4358 errno = EPIPE;
4359 return posix_error();
4362 /* change the actual standard IO streams over temporarily,
4363 * making the retained pipe ends non-inheritable
4365 pipe_err = 0;
4367 /* - stdin */
4368 if (dup2(p_fd[0].rd, 0) == 0)
4370 close(p_fd[0].rd);
4371 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4372 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4373 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4375 close(p_fd[0].wr);
4376 pipe_err = -1;
4379 else
4381 pipe_err = -1;
4384 /* - stdout */
4385 if (pipe_err == 0)
4387 if (dup2(p_fd[1].wr, 1) == 1)
4389 close(p_fd[1].wr);
4390 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4391 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4392 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4394 close(p_fd[1].rd);
4395 pipe_err = -1;
4398 else
4400 pipe_err = -1;
4404 /* - stderr, as required */
4405 if (pipe_err == 0)
4406 switch (n)
4408 case POPEN_3:
4410 if (dup2(p_fd[2].wr, 2) == 2)
4412 close(p_fd[2].wr);
4413 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4414 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4415 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4417 close(p_fd[2].rd);
4418 pipe_err = -1;
4421 else
4423 pipe_err = -1;
4425 break;
4428 case POPEN_4:
4430 if (dup2(1, 2) != 2)
4432 pipe_err = -1;
4434 break;
4438 /* spawn the child process */
4439 if (pipe_err == 0)
4441 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4442 if (pipe_pid == -1)
4444 pipe_err = -1;
4446 else
4448 /* save the PID into the FILE structure
4449 * NOTE: this implementation doesn't actually
4450 * take advantage of this, but do it for
4451 * completeness - AIM Apr01
4453 for (i = 0; i < file_count; i++)
4454 p_s[i]->_pid = pipe_pid;
4458 /* reset standard IO to normal */
4459 for (i = 0; i < 3; i++)
4461 dup2(stdio[i].handle, i);
4462 fcntl(i, F_SETFD, stdio[i].flags);
4463 close(stdio[i].handle);
4466 /* if any remnant problems, clean up and bail out */
4467 if (pipe_err < 0)
4469 for (i = 0; i < 3; i++)
4471 close(p_fd[i].rd);
4472 close(p_fd[i].wr);
4474 errno = EPIPE;
4475 return posix_error_with_filename(cmdstring);
4478 /* build tuple of file objects to return */
4479 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4480 PyFile_SetBufSize(p_f[0], bufsize);
4481 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4482 PyFile_SetBufSize(p_f[1], bufsize);
4483 if (n == POPEN_3)
4485 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4486 PyFile_SetBufSize(p_f[0], bufsize);
4487 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4489 else
4490 f = PyTuple_Pack(2, p_f[0], p_f[1]);
4493 * Insert the files we've created into the process dictionary
4494 * all referencing the list with the process handle and the
4495 * initial number of files (see description below in _PyPclose).
4496 * Since if _PyPclose later tried to wait on a process when all
4497 * handles weren't closed, it could create a deadlock with the
4498 * child, we spend some energy here to try to ensure that we
4499 * either insert all file handles into the dictionary or none
4500 * at all. It's a little clumsy with the various popen modes
4501 * and variable number of files involved.
4503 if (!_PyPopenProcs)
4505 _PyPopenProcs = PyDict_New();
4508 if (_PyPopenProcs)
4510 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4511 int ins_rc[3];
4513 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4514 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4516 procObj = PyList_New(2);
4517 pidObj = PyInt_FromLong((long) pipe_pid);
4518 intObj = PyInt_FromLong((long) file_count);
4520 if (procObj && pidObj && intObj)
4522 PyList_SetItem(procObj, 0, pidObj);
4523 PyList_SetItem(procObj, 1, intObj);
4525 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4526 if (fileObj[0])
4528 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4529 fileObj[0],
4530 procObj);
4532 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4533 if (fileObj[1])
4535 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4536 fileObj[1],
4537 procObj);
4539 if (file_count >= 3)
4541 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4542 if (fileObj[2])
4544 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4545 fileObj[2],
4546 procObj);
4550 if (ins_rc[0] < 0 || !fileObj[0] ||
4551 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4552 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4554 /* Something failed - remove any dictionary
4555 * entries that did make it.
4557 if (!ins_rc[0] && fileObj[0])
4559 PyDict_DelItem(_PyPopenProcs,
4560 fileObj[0]);
4562 if (!ins_rc[1] && fileObj[1])
4564 PyDict_DelItem(_PyPopenProcs,
4565 fileObj[1]);
4567 if (!ins_rc[2] && fileObj[2])
4569 PyDict_DelItem(_PyPopenProcs,
4570 fileObj[2]);
4576 * Clean up our localized references for the dictionary keys
4577 * and value since PyDict_SetItem will Py_INCREF any copies
4578 * that got placed in the dictionary.
4580 Py_XDECREF(procObj);
4581 Py_XDECREF(fileObj[0]);
4582 Py_XDECREF(fileObj[1]);
4583 Py_XDECREF(fileObj[2]);
4586 /* Child is launched. */
4587 return f;
4591 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4592 * exit code for the child process and return as a result of the close.
4594 * This function uses the _PyPopenProcs dictionary in order to map the
4595 * input file pointer to information about the process that was
4596 * originally created by the popen* call that created the file pointer.
4597 * The dictionary uses the file pointer as a key (with one entry
4598 * inserted for each file returned by the original popen* call) and a
4599 * single list object as the value for all files from a single call.
4600 * The list object contains the Win32 process handle at [0], and a file
4601 * count at [1], which is initialized to the total number of file
4602 * handles using that list.
4604 * This function closes whichever handle it is passed, and decrements
4605 * the file count in the dictionary for the process handle pointed to
4606 * by this file. On the last close (when the file count reaches zero),
4607 * this function will wait for the child process and then return its
4608 * exit code as the result of the close() operation. This permits the
4609 * files to be closed in any order - it is always the close() of the
4610 * final handle that will return the exit code.
4612 * NOTE: This function is currently called with the GIL released.
4613 * hence we use the GILState API to manage our state.
4616 static int _PyPclose(FILE *file)
4618 int result;
4619 int exit_code;
4620 pid_t pipe_pid;
4621 PyObject *procObj, *pidObj, *intObj, *fileObj;
4622 int file_count;
4623 #ifdef WITH_THREAD
4624 PyGILState_STATE state;
4625 #endif
4627 /* Close the file handle first, to ensure it can't block the
4628 * child from exiting if it's the last handle.
4630 result = fclose(file);
4632 #ifdef WITH_THREAD
4633 state = PyGILState_Ensure();
4634 #endif
4635 if (_PyPopenProcs)
4637 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4638 (procObj = PyDict_GetItem(_PyPopenProcs,
4639 fileObj)) != NULL &&
4640 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4641 (intObj = PyList_GetItem(procObj,1)) != NULL)
4643 pipe_pid = (int) PyInt_AsLong(pidObj);
4644 file_count = (int) PyInt_AsLong(intObj);
4646 if (file_count > 1)
4648 /* Still other files referencing process */
4649 file_count--;
4650 PyList_SetItem(procObj,1,
4651 PyInt_FromLong((long) file_count));
4653 else
4655 /* Last file for this process */
4656 if (result != EOF &&
4657 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4659 /* extract exit status */
4660 if (WIFEXITED(exit_code))
4662 result = WEXITSTATUS(exit_code);
4664 else
4666 errno = EPIPE;
4667 result = -1;
4670 else
4672 /* Indicate failure - this will cause the file object
4673 * to raise an I/O error and translate the last
4674 * error code from errno. We do have a problem with
4675 * last errors that overlap the normal errno table,
4676 * but that's a consistent problem with the file object.
4678 result = -1;
4682 /* Remove this file pointer from dictionary */
4683 PyDict_DelItem(_PyPopenProcs, fileObj);
4685 if (PyDict_Size(_PyPopenProcs) == 0)
4687 Py_DECREF(_PyPopenProcs);
4688 _PyPopenProcs = NULL;
4691 } /* if object retrieval ok */
4693 Py_XDECREF(fileObj);
4694 } /* if _PyPopenProcs */
4696 #ifdef WITH_THREAD
4697 PyGILState_Release(state);
4698 #endif
4699 return result;
4702 #endif /* PYCC_??? */
4704 #elif defined(MS_WINDOWS)
4707 * Portable 'popen' replacement for Win32.
4709 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4710 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
4711 * Return code handling by David Bolen <db3l@fitlinxx.com>.
4714 #include <malloc.h>
4715 #include <io.h>
4716 #include <fcntl.h>
4718 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4719 #define POPEN_1 1
4720 #define POPEN_2 2
4721 #define POPEN_3 3
4722 #define POPEN_4 4
4724 static PyObject *_PyPopen(char *, int, int);
4725 static int _PyPclose(FILE *file);
4728 * Internal dictionary mapping popen* file pointers to process handles,
4729 * for use when retrieving the process exit code. See _PyPclose() below
4730 * for more information on this dictionary's use.
4732 static PyObject *_PyPopenProcs = NULL;
4735 /* popen that works from a GUI.
4737 * The result of this function is a pipe (file) connected to the
4738 * processes stdin or stdout, depending on the requested mode.
4741 static PyObject *
4742 posix_popen(PyObject *self, PyObject *args)
4744 PyObject *f;
4745 int tm = 0;
4747 char *cmdstring;
4748 char *mode = "r";
4749 int bufsize = -1;
4750 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4751 return NULL;
4753 if (*mode == 'r')
4754 tm = _O_RDONLY;
4755 else if (*mode != 'w') {
4756 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4757 return NULL;
4758 } else
4759 tm = _O_WRONLY;
4761 if (bufsize != -1) {
4762 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4763 return NULL;
4766 if (*(mode+1) == 't')
4767 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4768 else if (*(mode+1) == 'b')
4769 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4770 else
4771 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4773 return f;
4776 /* Variation on win32pipe.popen
4778 * The result of this function is a pipe (file) connected to the
4779 * process's stdin, and a pipe connected to the process's stdout.
4782 static PyObject *
4783 win32_popen2(PyObject *self, PyObject *args)
4785 PyObject *f;
4786 int tm=0;
4788 char *cmdstring;
4789 char *mode = "t";
4790 int bufsize = -1;
4791 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4792 return NULL;
4794 if (*mode == 't')
4795 tm = _O_TEXT;
4796 else if (*mode != 'b') {
4797 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4798 return NULL;
4799 } else
4800 tm = _O_BINARY;
4802 if (bufsize != -1) {
4803 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4804 return NULL;
4807 f = _PyPopen(cmdstring, tm, POPEN_2);
4809 return f;
4813 * Variation on <om win32pipe.popen>
4815 * The result of this function is 3 pipes - the process's stdin,
4816 * stdout and stderr
4819 static PyObject *
4820 win32_popen3(PyObject *self, PyObject *args)
4822 PyObject *f;
4823 int tm = 0;
4825 char *cmdstring;
4826 char *mode = "t";
4827 int bufsize = -1;
4828 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4829 return NULL;
4831 if (*mode == 't')
4832 tm = _O_TEXT;
4833 else if (*mode != 'b') {
4834 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
4835 return NULL;
4836 } else
4837 tm = _O_BINARY;
4839 if (bufsize != -1) {
4840 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
4841 return NULL;
4844 f = _PyPopen(cmdstring, tm, POPEN_3);
4846 return f;
4850 * Variation on win32pipe.popen
4852 * The result of this function is 2 pipes - the processes stdin,
4853 * and stdout+stderr combined as a single pipe.
4856 static PyObject *
4857 win32_popen4(PyObject *self, PyObject *args)
4859 PyObject *f;
4860 int tm = 0;
4862 char *cmdstring;
4863 char *mode = "t";
4864 int bufsize = -1;
4865 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4866 return NULL;
4868 if (*mode == 't')
4869 tm = _O_TEXT;
4870 else if (*mode != 'b') {
4871 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
4872 return NULL;
4873 } else
4874 tm = _O_BINARY;
4876 if (bufsize != -1) {
4877 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
4878 return NULL;
4881 f = _PyPopen(cmdstring, tm, POPEN_4);
4883 return f;
4886 static BOOL
4887 _PyPopenCreateProcess(char *cmdstring,
4888 HANDLE hStdin,
4889 HANDLE hStdout,
4890 HANDLE hStderr,
4891 HANDLE *hProcess)
4893 PROCESS_INFORMATION piProcInfo;
4894 STARTUPINFO siStartInfo;
4895 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
4896 char *s1,*s2, *s3 = " /c ";
4897 const char *szConsoleSpawn = "w9xpopen.exe";
4898 int i;
4899 Py_ssize_t x;
4901 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
4902 char *comshell;
4904 s1 = (char *)alloca(i);
4905 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4906 /* x < i, so x fits into an integer */
4907 return (int)x;
4909 /* Explicitly check if we are using COMMAND.COM. If we are
4910 * then use the w9xpopen hack.
4912 comshell = s1 + x;
4913 while (comshell >= s1 && *comshell != '\\')
4914 --comshell;
4915 ++comshell;
4917 if (GetVersion() < 0x80000000 &&
4918 _stricmp(comshell, "command.com") != 0) {
4919 /* NT/2000 and not using command.com. */
4920 x = i + strlen(s3) + strlen(cmdstring) + 1;
4921 s2 = (char *)alloca(x);
4922 ZeroMemory(s2, x);
4923 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
4925 else {
4927 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4928 * the workaround listed in KB: Q150956
4930 char modulepath[_MAX_PATH];
4931 struct stat statinfo;
4932 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4933 for (x = i = 0; modulepath[i]; i++)
4934 if (modulepath[i] == SEP)
4935 x = i+1;
4936 modulepath[x] = '\0';
4937 /* Create the full-name to w9xpopen, so we can test it exists */
4938 strncat(modulepath,
4939 szConsoleSpawn,
4940 (sizeof(modulepath)/sizeof(modulepath[0]))
4941 -strlen(modulepath));
4942 if (stat(modulepath, &statinfo) != 0) {
4943 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
4944 /* Eeek - file-not-found - possibly an embedding
4945 situation - see if we can locate it in sys.prefix
4947 strncpy(modulepath,
4948 Py_GetExecPrefix(),
4949 mplen);
4950 modulepath[mplen-1] = '\0';
4951 if (modulepath[strlen(modulepath)-1] != '\\')
4952 strcat(modulepath, "\\");
4953 strncat(modulepath,
4954 szConsoleSpawn,
4955 mplen-strlen(modulepath));
4956 /* No where else to look - raise an easily identifiable
4957 error, rather than leaving Windows to report
4958 "file not found" - as the user is probably blissfully
4959 unaware this shim EXE is used, and it will confuse them.
4960 (well, it confused me for a while ;-)
4962 if (stat(modulepath, &statinfo) != 0) {
4963 PyErr_Format(PyExc_RuntimeError,
4964 "Can not locate '%s' which is needed "
4965 "for popen to work with your shell "
4966 "or platform.",
4967 szConsoleSpawn);
4968 return FALSE;
4971 x = i + strlen(s3) + strlen(cmdstring) + 1 +
4972 strlen(modulepath) +
4973 strlen(szConsoleSpawn) + 1;
4975 s2 = (char *)alloca(x);
4976 ZeroMemory(s2, x);
4977 /* To maintain correct argument passing semantics,
4978 we pass the command-line as it stands, and allow
4979 quoting to be applied. w9xpopen.exe will then
4980 use its argv vector, and re-quote the necessary
4981 args for the ultimate child process.
4983 PyOS_snprintf(
4984 s2, x,
4985 "\"%s\" %s%s%s",
4986 modulepath,
4989 cmdstring);
4990 /* Not passing CREATE_NEW_CONSOLE has been known to
4991 cause random failures on win9x. Specifically a
4992 dialog:
4993 "Your program accessed mem currently in use at xxx"
4994 and a hopeful warning about the stability of your
4995 system.
4996 Cost is Ctrl+C wont kill children, but anyone
4997 who cares can have a go!
4999 dwProcessFlags |= CREATE_NEW_CONSOLE;
5003 /* Could be an else here to try cmd.exe / command.com in the path
5004 Now we'll just error out.. */
5005 else {
5006 PyErr_SetString(PyExc_RuntimeError,
5007 "Cannot locate a COMSPEC environment variable to "
5008 "use as the shell");
5009 return FALSE;
5012 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5013 siStartInfo.cb = sizeof(STARTUPINFO);
5014 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5015 siStartInfo.hStdInput = hStdin;
5016 siStartInfo.hStdOutput = hStdout;
5017 siStartInfo.hStdError = hStderr;
5018 siStartInfo.wShowWindow = SW_HIDE;
5020 if (CreateProcess(NULL,
5022 NULL,
5023 NULL,
5024 TRUE,
5025 dwProcessFlags,
5026 NULL,
5027 NULL,
5028 &siStartInfo,
5029 &piProcInfo) ) {
5030 /* Close the handles now so anyone waiting is woken. */
5031 CloseHandle(piProcInfo.hThread);
5033 /* Return process handle */
5034 *hProcess = piProcInfo.hProcess;
5035 return TRUE;
5037 win32_error("CreateProcess", s2);
5038 return FALSE;
5041 /* The following code is based off of KB: Q190351 */
5043 static PyObject *
5044 _PyPopen(char *cmdstring, int mode, int n)
5046 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5047 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
5048 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
5050 SECURITY_ATTRIBUTES saAttr;
5051 BOOL fSuccess;
5052 int fd1, fd2, fd3;
5053 FILE *f1, *f2, *f3;
5054 long file_count;
5055 PyObject *f;
5057 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5058 saAttr.bInheritHandle = TRUE;
5059 saAttr.lpSecurityDescriptor = NULL;
5061 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5062 return win32_error("CreatePipe", NULL);
5064 /* Create new output read handle and the input write handle. Set
5065 * the inheritance properties to FALSE. Otherwise, the child inherits
5066 * these handles; resulting in non-closeable handles to the pipes
5067 * being created. */
5068 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
5069 GetCurrentProcess(), &hChildStdinWrDup, 0,
5070 FALSE,
5071 DUPLICATE_SAME_ACCESS);
5072 if (!fSuccess)
5073 return win32_error("DuplicateHandle", NULL);
5075 /* Close the inheritable version of ChildStdin
5076 that we're using. */
5077 CloseHandle(hChildStdinWr);
5079 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5080 return win32_error("CreatePipe", NULL);
5082 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
5083 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5084 FALSE, DUPLICATE_SAME_ACCESS);
5085 if (!fSuccess)
5086 return win32_error("DuplicateHandle", NULL);
5088 /* Close the inheritable version of ChildStdout
5089 that we're using. */
5090 CloseHandle(hChildStdoutRd);
5092 if (n != POPEN_4) {
5093 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5094 return win32_error("CreatePipe", NULL);
5095 fSuccess = DuplicateHandle(GetCurrentProcess(),
5096 hChildStderrRd,
5097 GetCurrentProcess(),
5098 &hChildStderrRdDup, 0,
5099 FALSE, DUPLICATE_SAME_ACCESS);
5100 if (!fSuccess)
5101 return win32_error("DuplicateHandle", NULL);
5102 /* Close the inheritable version of ChildStdErr that we're using. */
5103 CloseHandle(hChildStderrRd);
5106 switch (n) {
5107 case POPEN_1:
5108 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5109 case _O_WRONLY | _O_TEXT:
5110 /* Case for writing to child Stdin in text mode. */
5111 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5112 f1 = _fdopen(fd1, "w");
5113 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5114 PyFile_SetBufSize(f, 0);
5115 /* We don't care about these pipes anymore, so close them. */
5116 CloseHandle(hChildStdoutRdDup);
5117 CloseHandle(hChildStderrRdDup);
5118 break;
5120 case _O_RDONLY | _O_TEXT:
5121 /* Case for reading from child Stdout in text mode. */
5122 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5123 f1 = _fdopen(fd1, "r");
5124 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5125 PyFile_SetBufSize(f, 0);
5126 /* We don't care about these pipes anymore, so close them. */
5127 CloseHandle(hChildStdinWrDup);
5128 CloseHandle(hChildStderrRdDup);
5129 break;
5131 case _O_RDONLY | _O_BINARY:
5132 /* Case for readinig from child Stdout in binary mode. */
5133 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5134 f1 = _fdopen(fd1, "rb");
5135 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5136 PyFile_SetBufSize(f, 0);
5137 /* We don't care about these pipes anymore, so close them. */
5138 CloseHandle(hChildStdinWrDup);
5139 CloseHandle(hChildStderrRdDup);
5140 break;
5142 case _O_WRONLY | _O_BINARY:
5143 /* Case for writing to child Stdin in binary mode. */
5144 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5145 f1 = _fdopen(fd1, "wb");
5146 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5147 PyFile_SetBufSize(f, 0);
5148 /* We don't care about these pipes anymore, so close them. */
5149 CloseHandle(hChildStdoutRdDup);
5150 CloseHandle(hChildStderrRdDup);
5151 break;
5153 file_count = 1;
5154 break;
5156 case POPEN_2:
5157 case POPEN_4:
5159 char *m1, *m2;
5160 PyObject *p1, *p2;
5162 if (mode & _O_TEXT) {
5163 m1 = "r";
5164 m2 = "w";
5165 } else {
5166 m1 = "rb";
5167 m2 = "wb";
5170 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5171 f1 = _fdopen(fd1, m2);
5172 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5173 f2 = _fdopen(fd2, m1);
5174 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5175 PyFile_SetBufSize(p1, 0);
5176 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5177 PyFile_SetBufSize(p2, 0);
5179 if (n != 4)
5180 CloseHandle(hChildStderrRdDup);
5182 f = PyTuple_Pack(2,p1,p2);
5183 Py_XDECREF(p1);
5184 Py_XDECREF(p2);
5185 file_count = 2;
5186 break;
5189 case POPEN_3:
5191 char *m1, *m2;
5192 PyObject *p1, *p2, *p3;
5194 if (mode & _O_TEXT) {
5195 m1 = "r";
5196 m2 = "w";
5197 } else {
5198 m1 = "rb";
5199 m2 = "wb";
5202 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5203 f1 = _fdopen(fd1, m2);
5204 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5205 f2 = _fdopen(fd2, m1);
5206 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5207 f3 = _fdopen(fd3, m1);
5208 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5209 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5210 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5211 PyFile_SetBufSize(p1, 0);
5212 PyFile_SetBufSize(p2, 0);
5213 PyFile_SetBufSize(p3, 0);
5214 f = PyTuple_Pack(3,p1,p2,p3);
5215 Py_XDECREF(p1);
5216 Py_XDECREF(p2);
5217 Py_XDECREF(p3);
5218 file_count = 3;
5219 break;
5223 if (n == POPEN_4) {
5224 if (!_PyPopenCreateProcess(cmdstring,
5225 hChildStdinRd,
5226 hChildStdoutWr,
5227 hChildStdoutWr,
5228 &hProcess))
5229 return NULL;
5231 else {
5232 if (!_PyPopenCreateProcess(cmdstring,
5233 hChildStdinRd,
5234 hChildStdoutWr,
5235 hChildStderrWr,
5236 &hProcess))
5237 return NULL;
5241 * Insert the files we've created into the process dictionary
5242 * all referencing the list with the process handle and the
5243 * initial number of files (see description below in _PyPclose).
5244 * Since if _PyPclose later tried to wait on a process when all
5245 * handles weren't closed, it could create a deadlock with the
5246 * child, we spend some energy here to try to ensure that we
5247 * either insert all file handles into the dictionary or none
5248 * at all. It's a little clumsy with the various popen modes
5249 * and variable number of files involved.
5251 if (!_PyPopenProcs) {
5252 _PyPopenProcs = PyDict_New();
5255 if (_PyPopenProcs) {
5256 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5257 int ins_rc[3];
5259 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5260 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5262 procObj = PyList_New(2);
5263 hProcessObj = PyLong_FromVoidPtr(hProcess);
5264 intObj = PyInt_FromLong(file_count);
5266 if (procObj && hProcessObj && intObj) {
5267 PyList_SetItem(procObj,0,hProcessObj);
5268 PyList_SetItem(procObj,1,intObj);
5270 fileObj[0] = PyLong_FromVoidPtr(f1);
5271 if (fileObj[0]) {
5272 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5273 fileObj[0],
5274 procObj);
5276 if (file_count >= 2) {
5277 fileObj[1] = PyLong_FromVoidPtr(f2);
5278 if (fileObj[1]) {
5279 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5280 fileObj[1],
5281 procObj);
5284 if (file_count >= 3) {
5285 fileObj[2] = PyLong_FromVoidPtr(f3);
5286 if (fileObj[2]) {
5287 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5288 fileObj[2],
5289 procObj);
5293 if (ins_rc[0] < 0 || !fileObj[0] ||
5294 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5295 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5296 /* Something failed - remove any dictionary
5297 * entries that did make it.
5299 if (!ins_rc[0] && fileObj[0]) {
5300 PyDict_DelItem(_PyPopenProcs,
5301 fileObj[0]);
5303 if (!ins_rc[1] && fileObj[1]) {
5304 PyDict_DelItem(_PyPopenProcs,
5305 fileObj[1]);
5307 if (!ins_rc[2] && fileObj[2]) {
5308 PyDict_DelItem(_PyPopenProcs,
5309 fileObj[2]);
5315 * Clean up our localized references for the dictionary keys
5316 * and value since PyDict_SetItem will Py_INCREF any copies
5317 * that got placed in the dictionary.
5319 Py_XDECREF(procObj);
5320 Py_XDECREF(fileObj[0]);
5321 Py_XDECREF(fileObj[1]);
5322 Py_XDECREF(fileObj[2]);
5325 /* Child is launched. Close the parents copy of those pipe
5326 * handles that only the child should have open. You need to
5327 * make sure that no handles to the write end of the output pipe
5328 * are maintained in this process or else the pipe will not close
5329 * when the child process exits and the ReadFile will hang. */
5331 if (!CloseHandle(hChildStdinRd))
5332 return win32_error("CloseHandle", NULL);
5334 if (!CloseHandle(hChildStdoutWr))
5335 return win32_error("CloseHandle", NULL);
5337 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5338 return win32_error("CloseHandle", NULL);
5340 return f;
5344 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5345 * exit code for the child process and return as a result of the close.
5347 * This function uses the _PyPopenProcs dictionary in order to map the
5348 * input file pointer to information about the process that was
5349 * originally created by the popen* call that created the file pointer.
5350 * The dictionary uses the file pointer as a key (with one entry
5351 * inserted for each file returned by the original popen* call) and a
5352 * single list object as the value for all files from a single call.
5353 * The list object contains the Win32 process handle at [0], and a file
5354 * count at [1], which is initialized to the total number of file
5355 * handles using that list.
5357 * This function closes whichever handle it is passed, and decrements
5358 * the file count in the dictionary for the process handle pointed to
5359 * by this file. On the last close (when the file count reaches zero),
5360 * this function will wait for the child process and then return its
5361 * exit code as the result of the close() operation. This permits the
5362 * files to be closed in any order - it is always the close() of the
5363 * final handle that will return the exit code.
5365 * NOTE: This function is currently called with the GIL released.
5366 * hence we use the GILState API to manage our state.
5369 static int _PyPclose(FILE *file)
5371 int result;
5372 DWORD exit_code;
5373 HANDLE hProcess;
5374 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5375 long file_count;
5376 #ifdef WITH_THREAD
5377 PyGILState_STATE state;
5378 #endif
5380 /* Close the file handle first, to ensure it can't block the
5381 * child from exiting if it's the last handle.
5383 result = fclose(file);
5384 #ifdef WITH_THREAD
5385 state = PyGILState_Ensure();
5386 #endif
5387 if (_PyPopenProcs) {
5388 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5389 (procObj = PyDict_GetItem(_PyPopenProcs,
5390 fileObj)) != NULL &&
5391 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5392 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5394 hProcess = PyLong_AsVoidPtr(hProcessObj);
5395 file_count = PyInt_AsLong(intObj);
5397 if (file_count > 1) {
5398 /* Still other files referencing process */
5399 file_count--;
5400 PyList_SetItem(procObj,1,
5401 PyInt_FromLong(file_count));
5402 } else {
5403 /* Last file for this process */
5404 if (result != EOF &&
5405 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5406 GetExitCodeProcess(hProcess, &exit_code)) {
5407 /* Possible truncation here in 16-bit environments, but
5408 * real exit codes are just the lower byte in any event.
5410 result = exit_code;
5411 } else {
5412 /* Indicate failure - this will cause the file object
5413 * to raise an I/O error and translate the last Win32
5414 * error code from errno. We do have a problem with
5415 * last errors that overlap the normal errno table,
5416 * but that's a consistent problem with the file object.
5418 if (result != EOF) {
5419 /* If the error wasn't from the fclose(), then
5420 * set errno for the file object error handling.
5422 errno = GetLastError();
5424 result = -1;
5427 /* Free up the native handle at this point */
5428 CloseHandle(hProcess);
5431 /* Remove this file pointer from dictionary */
5432 PyDict_DelItem(_PyPopenProcs, fileObj);
5434 if (PyDict_Size(_PyPopenProcs) == 0) {
5435 Py_DECREF(_PyPopenProcs);
5436 _PyPopenProcs = NULL;
5439 } /* if object retrieval ok */
5441 Py_XDECREF(fileObj);
5442 } /* if _PyPopenProcs */
5444 #ifdef WITH_THREAD
5445 PyGILState_Release(state);
5446 #endif
5447 return result;
5450 #else /* which OS? */
5451 static PyObject *
5452 posix_popen(PyObject *self, PyObject *args)
5454 char *name;
5455 char *mode = "r";
5456 int bufsize = -1;
5457 FILE *fp;
5458 PyObject *f;
5459 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5460 return NULL;
5461 /* Strip mode of binary or text modifiers */
5462 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5463 mode = "r";
5464 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5465 mode = "w";
5466 Py_BEGIN_ALLOW_THREADS
5467 fp = popen(name, mode);
5468 Py_END_ALLOW_THREADS
5469 if (fp == NULL)
5470 return posix_error();
5471 f = PyFile_FromFile(fp, name, mode, pclose);
5472 if (f != NULL)
5473 PyFile_SetBufSize(f, bufsize);
5474 return f;
5477 #endif /* PYOS_??? */
5478 #endif /* HAVE_POPEN */
5481 #ifdef HAVE_SETUID
5482 PyDoc_STRVAR(posix_setuid__doc__,
5483 "setuid(uid)\n\n\
5484 Set the current process's user id.");
5486 static PyObject *
5487 posix_setuid(PyObject *self, PyObject *args)
5489 int uid;
5490 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
5491 return NULL;
5492 if (setuid(uid) < 0)
5493 return posix_error();
5494 Py_INCREF(Py_None);
5495 return Py_None;
5497 #endif /* HAVE_SETUID */
5500 #ifdef HAVE_SETEUID
5501 PyDoc_STRVAR(posix_seteuid__doc__,
5502 "seteuid(uid)\n\n\
5503 Set the current process's effective user id.");
5505 static PyObject *
5506 posix_seteuid (PyObject *self, PyObject *args)
5508 int euid;
5509 if (!PyArg_ParseTuple(args, "i", &euid)) {
5510 return NULL;
5511 } else if (seteuid(euid) < 0) {
5512 return posix_error();
5513 } else {
5514 Py_INCREF(Py_None);
5515 return Py_None;
5518 #endif /* HAVE_SETEUID */
5520 #ifdef HAVE_SETEGID
5521 PyDoc_STRVAR(posix_setegid__doc__,
5522 "setegid(gid)\n\n\
5523 Set the current process's effective group id.");
5525 static PyObject *
5526 posix_setegid (PyObject *self, PyObject *args)
5528 int egid;
5529 if (!PyArg_ParseTuple(args, "i", &egid)) {
5530 return NULL;
5531 } else if (setegid(egid) < 0) {
5532 return posix_error();
5533 } else {
5534 Py_INCREF(Py_None);
5535 return Py_None;
5538 #endif /* HAVE_SETEGID */
5540 #ifdef HAVE_SETREUID
5541 PyDoc_STRVAR(posix_setreuid__doc__,
5542 "setreuid(ruid, euid)\n\n\
5543 Set the current process's real and effective user ids.");
5545 static PyObject *
5546 posix_setreuid (PyObject *self, PyObject *args)
5548 int ruid, euid;
5549 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5550 return NULL;
5551 } else if (setreuid(ruid, euid) < 0) {
5552 return posix_error();
5553 } else {
5554 Py_INCREF(Py_None);
5555 return Py_None;
5558 #endif /* HAVE_SETREUID */
5560 #ifdef HAVE_SETREGID
5561 PyDoc_STRVAR(posix_setregid__doc__,
5562 "setregid(rgid, egid)\n\n\
5563 Set the current process's real and effective group ids.");
5565 static PyObject *
5566 posix_setregid (PyObject *self, PyObject *args)
5568 int rgid, egid;
5569 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5570 return NULL;
5571 } else if (setregid(rgid, egid) < 0) {
5572 return posix_error();
5573 } else {
5574 Py_INCREF(Py_None);
5575 return Py_None;
5578 #endif /* HAVE_SETREGID */
5580 #ifdef HAVE_SETGID
5581 PyDoc_STRVAR(posix_setgid__doc__,
5582 "setgid(gid)\n\n\
5583 Set the current process's group id.");
5585 static PyObject *
5586 posix_setgid(PyObject *self, PyObject *args)
5588 int gid;
5589 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
5590 return NULL;
5591 if (setgid(gid) < 0)
5592 return posix_error();
5593 Py_INCREF(Py_None);
5594 return Py_None;
5596 #endif /* HAVE_SETGID */
5598 #ifdef HAVE_SETGROUPS
5599 PyDoc_STRVAR(posix_setgroups__doc__,
5600 "setgroups(list)\n\n\
5601 Set the groups of the current process to list.");
5603 static PyObject *
5604 posix_setgroups(PyObject *self, PyObject *groups)
5606 int i, len;
5607 gid_t grouplist[MAX_GROUPS];
5609 if (!PySequence_Check(groups)) {
5610 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5611 return NULL;
5613 len = PySequence_Size(groups);
5614 if (len > MAX_GROUPS) {
5615 PyErr_SetString(PyExc_ValueError, "too many groups");
5616 return NULL;
5618 for(i = 0; i < len; i++) {
5619 PyObject *elem;
5620 elem = PySequence_GetItem(groups, i);
5621 if (!elem)
5622 return NULL;
5623 if (!PyInt_Check(elem)) {
5624 if (!PyLong_Check(elem)) {
5625 PyErr_SetString(PyExc_TypeError,
5626 "groups must be integers");
5627 Py_DECREF(elem);
5628 return NULL;
5629 } else {
5630 unsigned long x = PyLong_AsUnsignedLong(elem);
5631 if (PyErr_Occurred()) {
5632 PyErr_SetString(PyExc_TypeError,
5633 "group id too big");
5634 Py_DECREF(elem);
5635 return NULL;
5637 grouplist[i] = x;
5638 /* read back the value to see if it fitted in gid_t */
5639 if (grouplist[i] != x) {
5640 PyErr_SetString(PyExc_TypeError,
5641 "group id too big");
5642 Py_DECREF(elem);
5643 return NULL;
5646 } else {
5647 long x = PyInt_AsLong(elem);
5648 grouplist[i] = x;
5649 if (grouplist[i] != x) {
5650 PyErr_SetString(PyExc_TypeError,
5651 "group id too big");
5652 Py_DECREF(elem);
5653 return NULL;
5656 Py_DECREF(elem);
5659 if (setgroups(len, grouplist) < 0)
5660 return posix_error();
5661 Py_INCREF(Py_None);
5662 return Py_None;
5664 #endif /* HAVE_SETGROUPS */
5666 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5667 static PyObject *
5668 wait_helper(pid_t pid, int status, struct rusage *ru)
5670 PyObject *result;
5671 static PyObject *struct_rusage;
5673 if (pid == -1)
5674 return posix_error();
5676 if (struct_rusage == NULL) {
5677 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5678 if (m == NULL)
5679 return NULL;
5680 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5681 Py_DECREF(m);
5682 if (struct_rusage == NULL)
5683 return NULL;
5686 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5687 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5688 if (!result)
5689 return NULL;
5691 #ifndef doubletime
5692 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5693 #endif
5695 PyStructSequence_SET_ITEM(result, 0,
5696 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5697 PyStructSequence_SET_ITEM(result, 1,
5698 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5699 #define SET_INT(result, index, value)\
5700 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5701 SET_INT(result, 2, ru->ru_maxrss);
5702 SET_INT(result, 3, ru->ru_ixrss);
5703 SET_INT(result, 4, ru->ru_idrss);
5704 SET_INT(result, 5, ru->ru_isrss);
5705 SET_INT(result, 6, ru->ru_minflt);
5706 SET_INT(result, 7, ru->ru_majflt);
5707 SET_INT(result, 8, ru->ru_nswap);
5708 SET_INT(result, 9, ru->ru_inblock);
5709 SET_INT(result, 10, ru->ru_oublock);
5710 SET_INT(result, 11, ru->ru_msgsnd);
5711 SET_INT(result, 12, ru->ru_msgrcv);
5712 SET_INT(result, 13, ru->ru_nsignals);
5713 SET_INT(result, 14, ru->ru_nvcsw);
5714 SET_INT(result, 15, ru->ru_nivcsw);
5715 #undef SET_INT
5717 if (PyErr_Occurred()) {
5718 Py_DECREF(result);
5719 return NULL;
5722 return Py_BuildValue("iiN", pid, status, result);
5724 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5726 #ifdef HAVE_WAIT3
5727 PyDoc_STRVAR(posix_wait3__doc__,
5728 "wait3(options) -> (pid, status, rusage)\n\n\
5729 Wait for completion of a child process.");
5731 static PyObject *
5732 posix_wait3(PyObject *self, PyObject *args)
5734 pid_t pid;
5735 int options;
5736 struct rusage ru;
5737 WAIT_TYPE status;
5738 WAIT_STATUS_INT(status) = 0;
5740 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5741 return NULL;
5743 Py_BEGIN_ALLOW_THREADS
5744 pid = wait3(&status, options, &ru);
5745 Py_END_ALLOW_THREADS
5747 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5749 #endif /* HAVE_WAIT3 */
5751 #ifdef HAVE_WAIT4
5752 PyDoc_STRVAR(posix_wait4__doc__,
5753 "wait4(pid, options) -> (pid, status, rusage)\n\n\
5754 Wait for completion of a given child process.");
5756 static PyObject *
5757 posix_wait4(PyObject *self, PyObject *args)
5759 pid_t pid;
5760 int options;
5761 struct rusage ru;
5762 WAIT_TYPE status;
5763 WAIT_STATUS_INT(status) = 0;
5765 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5766 return NULL;
5768 Py_BEGIN_ALLOW_THREADS
5769 pid = wait4(pid, &status, options, &ru);
5770 Py_END_ALLOW_THREADS
5772 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5774 #endif /* HAVE_WAIT4 */
5776 #ifdef HAVE_WAITPID
5777 PyDoc_STRVAR(posix_waitpid__doc__,
5778 "waitpid(pid, options) -> (pid, status)\n\n\
5779 Wait for completion of a given child process.");
5781 static PyObject *
5782 posix_waitpid(PyObject *self, PyObject *args)
5784 pid_t pid;
5785 int options;
5786 WAIT_TYPE status;
5787 WAIT_STATUS_INT(status) = 0;
5789 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5790 return NULL;
5791 Py_BEGIN_ALLOW_THREADS
5792 pid = waitpid(pid, &status, options);
5793 Py_END_ALLOW_THREADS
5794 if (pid == -1)
5795 return posix_error();
5797 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
5800 #elif defined(HAVE_CWAIT)
5802 /* MS C has a variant of waitpid() that's usable for most purposes. */
5803 PyDoc_STRVAR(posix_waitpid__doc__,
5804 "waitpid(pid, options) -> (pid, status << 8)\n\n"
5805 "Wait for completion of a given process. options is ignored on Windows.");
5807 static PyObject *
5808 posix_waitpid(PyObject *self, PyObject *args)
5810 Py_intptr_t pid;
5811 int status, options;
5813 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5814 return NULL;
5815 Py_BEGIN_ALLOW_THREADS
5816 pid = _cwait(&status, pid, options);
5817 Py_END_ALLOW_THREADS
5818 if (pid == -1)
5819 return posix_error();
5821 /* shift the status left a byte so this is more like the POSIX waitpid */
5822 return Py_BuildValue("ii", pid, status << 8);
5824 #endif /* HAVE_WAITPID || HAVE_CWAIT */
5826 #ifdef HAVE_WAIT
5827 PyDoc_STRVAR(posix_wait__doc__,
5828 "wait() -> (pid, status)\n\n\
5829 Wait for completion of a child process.");
5831 static PyObject *
5832 posix_wait(PyObject *self, PyObject *noargs)
5834 pid_t pid;
5835 WAIT_TYPE status;
5836 WAIT_STATUS_INT(status) = 0;
5838 Py_BEGIN_ALLOW_THREADS
5839 pid = wait(&status);
5840 Py_END_ALLOW_THREADS
5841 if (pid == -1)
5842 return posix_error();
5844 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
5846 #endif
5849 PyDoc_STRVAR(posix_lstat__doc__,
5850 "lstat(path) -> stat result\n\n\
5851 Like stat(path), but do not follow symbolic links.");
5853 static PyObject *
5854 posix_lstat(PyObject *self, PyObject *args)
5856 #ifdef HAVE_LSTAT
5857 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
5858 #else /* !HAVE_LSTAT */
5859 #ifdef MS_WINDOWS
5860 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
5861 #else
5862 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5863 #endif
5864 #endif /* !HAVE_LSTAT */
5868 #ifdef HAVE_READLINK
5869 PyDoc_STRVAR(posix_readlink__doc__,
5870 "readlink(path) -> path\n\n\
5871 Return a string representing the path to which the symbolic link points.");
5873 static PyObject *
5874 posix_readlink(PyObject *self, PyObject *args)
5876 PyObject* v;
5877 char buf[MAXPATHLEN];
5878 char *path;
5879 int n;
5880 #ifdef Py_USING_UNICODE
5881 int arg_is_unicode = 0;
5882 #endif
5884 if (!PyArg_ParseTuple(args, "et:readlink",
5885 Py_FileSystemDefaultEncoding, &path))
5886 return NULL;
5887 #ifdef Py_USING_UNICODE
5888 v = PySequence_GetItem(args, 0);
5889 if (v == NULL) {
5890 PyMem_Free(path);
5891 return NULL;
5894 if (PyUnicode_Check(v)) {
5895 arg_is_unicode = 1;
5897 Py_DECREF(v);
5898 #endif
5900 Py_BEGIN_ALLOW_THREADS
5901 n = readlink(path, buf, (int) sizeof buf);
5902 Py_END_ALLOW_THREADS
5903 if (n < 0)
5904 return posix_error_with_allocated_filename(path);
5906 PyMem_Free(path);
5907 v = PyString_FromStringAndSize(buf, n);
5908 #ifdef Py_USING_UNICODE
5909 if (arg_is_unicode) {
5910 PyObject *w;
5912 w = PyUnicode_FromEncodedObject(v,
5913 Py_FileSystemDefaultEncoding,
5914 "strict");
5915 if (w != NULL) {
5916 Py_DECREF(v);
5917 v = w;
5919 else {
5920 /* fall back to the original byte string, as
5921 discussed in patch #683592 */
5922 PyErr_Clear();
5925 #endif
5926 return v;
5928 #endif /* HAVE_READLINK */
5931 #ifdef HAVE_SYMLINK
5932 PyDoc_STRVAR(posix_symlink__doc__,
5933 "symlink(src, dst)\n\n\
5934 Create a symbolic link pointing to src named dst.");
5936 static PyObject *
5937 posix_symlink(PyObject *self, PyObject *args)
5939 return posix_2str(args, "etet:symlink", symlink);
5941 #endif /* HAVE_SYMLINK */
5944 #ifdef HAVE_TIMES
5945 #ifndef HZ
5946 #define HZ 60 /* Universal constant :-) */
5947 #endif /* HZ */
5949 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
5950 static long
5951 system_uptime(void)
5953 ULONG value = 0;
5955 Py_BEGIN_ALLOW_THREADS
5956 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5957 Py_END_ALLOW_THREADS
5959 return value;
5962 static PyObject *
5963 posix_times(PyObject *self, PyObject *noargs)
5965 /* Currently Only Uptime is Provided -- Others Later */
5966 return Py_BuildValue("ddddd",
5967 (double)0 /* t.tms_utime / HZ */,
5968 (double)0 /* t.tms_stime / HZ */,
5969 (double)0 /* t.tms_cutime / HZ */,
5970 (double)0 /* t.tms_cstime / HZ */,
5971 (double)system_uptime() / 1000);
5973 #else /* not OS2 */
5974 static PyObject *
5975 posix_times(PyObject *self, PyObject *noargs)
5977 struct tms t;
5978 clock_t c;
5979 errno = 0;
5980 c = times(&t);
5981 if (c == (clock_t) -1)
5982 return posix_error();
5983 return Py_BuildValue("ddddd",
5984 (double)t.tms_utime / HZ,
5985 (double)t.tms_stime / HZ,
5986 (double)t.tms_cutime / HZ,
5987 (double)t.tms_cstime / HZ,
5988 (double)c / HZ);
5990 #endif /* not OS2 */
5991 #endif /* HAVE_TIMES */
5994 #ifdef MS_WINDOWS
5995 #define HAVE_TIMES /* so the method table will pick it up */
5996 static PyObject *
5997 posix_times(PyObject *self, PyObject *noargs)
5999 FILETIME create, exit, kernel, user;
6000 HANDLE hProc;
6001 hProc = GetCurrentProcess();
6002 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6003 /* The fields of a FILETIME structure are the hi and lo part
6004 of a 64-bit value expressed in 100 nanosecond units.
6005 1e7 is one second in such units; 1e-7 the inverse.
6006 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6008 return Py_BuildValue(
6009 "ddddd",
6010 (double)(user.dwHighDateTime*429.4967296 +
6011 user.dwLowDateTime*1e-7),
6012 (double)(kernel.dwHighDateTime*429.4967296 +
6013 kernel.dwLowDateTime*1e-7),
6014 (double)0,
6015 (double)0,
6016 (double)0);
6018 #endif /* MS_WINDOWS */
6020 #ifdef HAVE_TIMES
6021 PyDoc_STRVAR(posix_times__doc__,
6022 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
6023 Return a tuple of floating point numbers indicating process times.");
6024 #endif
6027 #ifdef HAVE_GETSID
6028 PyDoc_STRVAR(posix_getsid__doc__,
6029 "getsid(pid) -> sid\n\n\
6030 Call the system call getsid().");
6032 static PyObject *
6033 posix_getsid(PyObject *self, PyObject *args)
6035 pid_t pid;
6036 int sid;
6037 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
6038 return NULL;
6039 sid = getsid(pid);
6040 if (sid < 0)
6041 return posix_error();
6042 return PyInt_FromLong((long)sid);
6044 #endif /* HAVE_GETSID */
6047 #ifdef HAVE_SETSID
6048 PyDoc_STRVAR(posix_setsid__doc__,
6049 "setsid()\n\n\
6050 Call the system call setsid().");
6052 static PyObject *
6053 posix_setsid(PyObject *self, PyObject *noargs)
6055 if (setsid() < 0)
6056 return posix_error();
6057 Py_INCREF(Py_None);
6058 return Py_None;
6060 #endif /* HAVE_SETSID */
6062 #ifdef HAVE_SETPGID
6063 PyDoc_STRVAR(posix_setpgid__doc__,
6064 "setpgid(pid, pgrp)\n\n\
6065 Call the system call setpgid().");
6067 static PyObject *
6068 posix_setpgid(PyObject *self, PyObject *args)
6070 pid_t pid;
6071 int pgrp;
6072 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
6073 return NULL;
6074 if (setpgid(pid, pgrp) < 0)
6075 return posix_error();
6076 Py_INCREF(Py_None);
6077 return Py_None;
6079 #endif /* HAVE_SETPGID */
6082 #ifdef HAVE_TCGETPGRP
6083 PyDoc_STRVAR(posix_tcgetpgrp__doc__,
6084 "tcgetpgrp(fd) -> pgid\n\n\
6085 Return the process group associated with the terminal given by a fd.");
6087 static PyObject *
6088 posix_tcgetpgrp(PyObject *self, PyObject *args)
6090 int fd;
6091 pid_t pgid;
6092 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6093 return NULL;
6094 pgid = tcgetpgrp(fd);
6095 if (pgid < 0)
6096 return posix_error();
6097 return PyInt_FromLong((long)pgid);
6099 #endif /* HAVE_TCGETPGRP */
6102 #ifdef HAVE_TCSETPGRP
6103 PyDoc_STRVAR(posix_tcsetpgrp__doc__,
6104 "tcsetpgrp(fd, pgid)\n\n\
6105 Set the process group associated with the terminal given by a fd.");
6107 static PyObject *
6108 posix_tcsetpgrp(PyObject *self, PyObject *args)
6110 int fd, pgid;
6111 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
6112 return NULL;
6113 if (tcsetpgrp(fd, pgid) < 0)
6114 return posix_error();
6115 Py_INCREF(Py_None);
6116 return Py_None;
6118 #endif /* HAVE_TCSETPGRP */
6120 /* Functions acting on file descriptors */
6122 PyDoc_STRVAR(posix_open__doc__,
6123 "open(filename, flag [, mode=0777]) -> fd\n\n\
6124 Open a file (for low level IO).");
6126 static PyObject *
6127 posix_open(PyObject *self, PyObject *args)
6129 char *file = NULL;
6130 int flag;
6131 int mode = 0777;
6132 int fd;
6134 #ifdef MS_WINDOWS
6135 if (unicode_file_names()) {
6136 PyUnicodeObject *po;
6137 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6138 Py_BEGIN_ALLOW_THREADS
6139 /* PyUnicode_AS_UNICODE OK without thread
6140 lock as it is a simple dereference. */
6141 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6142 Py_END_ALLOW_THREADS
6143 if (fd < 0)
6144 return posix_error();
6145 return PyInt_FromLong((long)fd);
6147 /* Drop the argument parsing error as narrow strings
6148 are also valid. */
6149 PyErr_Clear();
6151 #endif
6153 if (!PyArg_ParseTuple(args, "eti|i",
6154 Py_FileSystemDefaultEncoding, &file,
6155 &flag, &mode))
6156 return NULL;
6158 Py_BEGIN_ALLOW_THREADS
6159 fd = open(file, flag, mode);
6160 Py_END_ALLOW_THREADS
6161 if (fd < 0)
6162 return posix_error_with_allocated_filename(file);
6163 PyMem_Free(file);
6164 return PyInt_FromLong((long)fd);
6168 PyDoc_STRVAR(posix_close__doc__,
6169 "close(fd)\n\n\
6170 Close a file descriptor (for low level IO).");
6172 static PyObject *
6173 posix_close(PyObject *self, PyObject *args)
6175 int fd, res;
6176 if (!PyArg_ParseTuple(args, "i:close", &fd))
6177 return NULL;
6178 Py_BEGIN_ALLOW_THREADS
6179 res = close(fd);
6180 Py_END_ALLOW_THREADS
6181 if (res < 0)
6182 return posix_error();
6183 Py_INCREF(Py_None);
6184 return Py_None;
6188 PyDoc_STRVAR(posix_closerange__doc__,
6189 "closerange(fd_low, fd_high)\n\n\
6190 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6192 static PyObject *
6193 posix_closerange(PyObject *self, PyObject *args)
6195 int fd_from, fd_to, i;
6196 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6197 return NULL;
6198 Py_BEGIN_ALLOW_THREADS
6199 for (i = fd_from; i < fd_to; i++)
6200 close(i);
6201 Py_END_ALLOW_THREADS
6202 Py_RETURN_NONE;
6206 PyDoc_STRVAR(posix_dup__doc__,
6207 "dup(fd) -> fd2\n\n\
6208 Return a duplicate of a file descriptor.");
6210 static PyObject *
6211 posix_dup(PyObject *self, PyObject *args)
6213 int fd;
6214 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6215 return NULL;
6216 Py_BEGIN_ALLOW_THREADS
6217 fd = dup(fd);
6218 Py_END_ALLOW_THREADS
6219 if (fd < 0)
6220 return posix_error();
6221 return PyInt_FromLong((long)fd);
6225 PyDoc_STRVAR(posix_dup2__doc__,
6226 "dup2(old_fd, new_fd)\n\n\
6227 Duplicate file descriptor.");
6229 static PyObject *
6230 posix_dup2(PyObject *self, PyObject *args)
6232 int fd, fd2, res;
6233 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6234 return NULL;
6235 Py_BEGIN_ALLOW_THREADS
6236 res = dup2(fd, fd2);
6237 Py_END_ALLOW_THREADS
6238 if (res < 0)
6239 return posix_error();
6240 Py_INCREF(Py_None);
6241 return Py_None;
6245 PyDoc_STRVAR(posix_lseek__doc__,
6246 "lseek(fd, pos, how) -> newpos\n\n\
6247 Set the current position of a file descriptor.");
6249 static PyObject *
6250 posix_lseek(PyObject *self, PyObject *args)
6252 int fd, how;
6253 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6254 PY_LONG_LONG pos, res;
6255 #else
6256 off_t pos, res;
6257 #endif
6258 PyObject *posobj;
6259 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6260 return NULL;
6261 #ifdef SEEK_SET
6262 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6263 switch (how) {
6264 case 0: how = SEEK_SET; break;
6265 case 1: how = SEEK_CUR; break;
6266 case 2: how = SEEK_END; break;
6268 #endif /* SEEK_END */
6270 #if !defined(HAVE_LARGEFILE_SUPPORT)
6271 pos = PyInt_AsLong(posobj);
6272 #else
6273 pos = PyLong_Check(posobj) ?
6274 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6275 #endif
6276 if (PyErr_Occurred())
6277 return NULL;
6279 Py_BEGIN_ALLOW_THREADS
6280 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6281 res = _lseeki64(fd, pos, how);
6282 #else
6283 res = lseek(fd, pos, how);
6284 #endif
6285 Py_END_ALLOW_THREADS
6286 if (res < 0)
6287 return posix_error();
6289 #if !defined(HAVE_LARGEFILE_SUPPORT)
6290 return PyInt_FromLong(res);
6291 #else
6292 return PyLong_FromLongLong(res);
6293 #endif
6297 PyDoc_STRVAR(posix_read__doc__,
6298 "read(fd, buffersize) -> string\n\n\
6299 Read a file descriptor.");
6301 static PyObject *
6302 posix_read(PyObject *self, PyObject *args)
6304 int fd, size, n;
6305 PyObject *buffer;
6306 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6307 return NULL;
6308 if (size < 0) {
6309 errno = EINVAL;
6310 return posix_error();
6312 buffer = PyString_FromStringAndSize((char *)NULL, size);
6313 if (buffer == NULL)
6314 return NULL;
6315 Py_BEGIN_ALLOW_THREADS
6316 n = read(fd, PyString_AsString(buffer), size);
6317 Py_END_ALLOW_THREADS
6318 if (n < 0) {
6319 Py_DECREF(buffer);
6320 return posix_error();
6322 if (n != size)
6323 _PyString_Resize(&buffer, n);
6324 return buffer;
6328 PyDoc_STRVAR(posix_write__doc__,
6329 "write(fd, string) -> byteswritten\n\n\
6330 Write a string to a file descriptor.");
6332 static PyObject *
6333 posix_write(PyObject *self, PyObject *args)
6335 Py_buffer pbuf;
6336 int fd;
6337 Py_ssize_t size;
6339 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6340 return NULL;
6341 Py_BEGIN_ALLOW_THREADS
6342 size = write(fd, pbuf.buf, (size_t)pbuf.len);
6343 Py_END_ALLOW_THREADS
6344 PyBuffer_Release(&pbuf);
6345 if (size < 0)
6346 return posix_error();
6347 return PyInt_FromSsize_t(size);
6351 PyDoc_STRVAR(posix_fstat__doc__,
6352 "fstat(fd) -> stat result\n\n\
6353 Like stat(), but for an open file descriptor.");
6355 static PyObject *
6356 posix_fstat(PyObject *self, PyObject *args)
6358 int fd;
6359 STRUCT_STAT st;
6360 int res;
6361 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6362 return NULL;
6363 #ifdef __VMS
6364 /* on OpenVMS we must ensure that all bytes are written to the file */
6365 fsync(fd);
6366 #endif
6367 Py_BEGIN_ALLOW_THREADS
6368 res = FSTAT(fd, &st);
6369 Py_END_ALLOW_THREADS
6370 if (res != 0) {
6371 #ifdef MS_WINDOWS
6372 return win32_error("fstat", NULL);
6373 #else
6374 return posix_error();
6375 #endif
6378 return _pystat_fromstructstat(&st);
6382 PyDoc_STRVAR(posix_fdopen__doc__,
6383 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
6384 Return an open file object connected to a file descriptor.");
6386 static PyObject *
6387 posix_fdopen(PyObject *self, PyObject *args)
6389 int fd;
6390 char *orgmode = "r";
6391 int bufsize = -1;
6392 FILE *fp;
6393 PyObject *f;
6394 char *mode;
6395 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6396 return NULL;
6398 /* Sanitize mode. See fileobject.c */
6399 mode = PyMem_MALLOC(strlen(orgmode)+3);
6400 if (!mode) {
6401 PyErr_NoMemory();
6402 return NULL;
6404 strcpy(mode, orgmode);
6405 if (_PyFile_SanitizeMode(mode)) {
6406 PyMem_FREE(mode);
6407 return NULL;
6409 Py_BEGIN_ALLOW_THREADS
6410 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6411 if (mode[0] == 'a') {
6412 /* try to make sure the O_APPEND flag is set */
6413 int flags;
6414 flags = fcntl(fd, F_GETFL);
6415 if (flags != -1)
6416 fcntl(fd, F_SETFL, flags | O_APPEND);
6417 fp = fdopen(fd, mode);
6418 if (fp == NULL && flags != -1)
6419 /* restore old mode if fdopen failed */
6420 fcntl(fd, F_SETFL, flags);
6421 } else {
6422 fp = fdopen(fd, mode);
6424 #else
6425 fp = fdopen(fd, mode);
6426 #endif
6427 Py_END_ALLOW_THREADS
6428 PyMem_FREE(mode);
6429 if (fp == NULL)
6430 return posix_error();
6431 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6432 if (f != NULL)
6433 PyFile_SetBufSize(f, bufsize);
6434 return f;
6437 PyDoc_STRVAR(posix_isatty__doc__,
6438 "isatty(fd) -> bool\n\n\
6439 Return True if the file descriptor 'fd' is an open file descriptor\n\
6440 connected to the slave end of a terminal.");
6442 static PyObject *
6443 posix_isatty(PyObject *self, PyObject *args)
6445 int fd;
6446 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6447 return NULL;
6448 return PyBool_FromLong(isatty(fd));
6451 #ifdef HAVE_PIPE
6452 PyDoc_STRVAR(posix_pipe__doc__,
6453 "pipe() -> (read_end, write_end)\n\n\
6454 Create a pipe.");
6456 static PyObject *
6457 posix_pipe(PyObject *self, PyObject *noargs)
6459 #if defined(PYOS_OS2)
6460 HFILE read, write;
6461 APIRET rc;
6463 Py_BEGIN_ALLOW_THREADS
6464 rc = DosCreatePipe( &read, &write, 4096);
6465 Py_END_ALLOW_THREADS
6466 if (rc != NO_ERROR)
6467 return os2_error(rc);
6469 return Py_BuildValue("(ii)", read, write);
6470 #else
6471 #if !defined(MS_WINDOWS)
6472 int fds[2];
6473 int res;
6474 Py_BEGIN_ALLOW_THREADS
6475 res = pipe(fds);
6476 Py_END_ALLOW_THREADS
6477 if (res != 0)
6478 return posix_error();
6479 return Py_BuildValue("(ii)", fds[0], fds[1]);
6480 #else /* MS_WINDOWS */
6481 HANDLE read, write;
6482 int read_fd, write_fd;
6483 BOOL ok;
6484 Py_BEGIN_ALLOW_THREADS
6485 ok = CreatePipe(&read, &write, NULL, 0);
6486 Py_END_ALLOW_THREADS
6487 if (!ok)
6488 return win32_error("CreatePipe", NULL);
6489 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6490 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6491 return Py_BuildValue("(ii)", read_fd, write_fd);
6492 #endif /* MS_WINDOWS */
6493 #endif
6495 #endif /* HAVE_PIPE */
6498 #ifdef HAVE_MKFIFO
6499 PyDoc_STRVAR(posix_mkfifo__doc__,
6500 "mkfifo(filename [, mode=0666])\n\n\
6501 Create a FIFO (a POSIX named pipe).");
6503 static PyObject *
6504 posix_mkfifo(PyObject *self, PyObject *args)
6506 char *filename;
6507 int mode = 0666;
6508 int res;
6509 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6510 return NULL;
6511 Py_BEGIN_ALLOW_THREADS
6512 res = mkfifo(filename, mode);
6513 Py_END_ALLOW_THREADS
6514 if (res < 0)
6515 return posix_error();
6516 Py_INCREF(Py_None);
6517 return Py_None;
6519 #endif
6522 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
6523 PyDoc_STRVAR(posix_mknod__doc__,
6524 "mknod(filename [, mode=0600, device])\n\n\
6525 Create a filesystem node (file, device special file or named pipe)\n\
6526 named filename. mode specifies both the permissions to use and the\n\
6527 type of node to be created, being combined (bitwise OR) with one of\n\
6528 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
6529 device defines the newly created device special file (probably using\n\
6530 os.makedev()), otherwise it is ignored.");
6533 static PyObject *
6534 posix_mknod(PyObject *self, PyObject *args)
6536 char *filename;
6537 int mode = 0600;
6538 int device = 0;
6539 int res;
6540 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6541 return NULL;
6542 Py_BEGIN_ALLOW_THREADS
6543 res = mknod(filename, mode, device);
6544 Py_END_ALLOW_THREADS
6545 if (res < 0)
6546 return posix_error();
6547 Py_INCREF(Py_None);
6548 return Py_None;
6550 #endif
6552 #ifdef HAVE_DEVICE_MACROS
6553 PyDoc_STRVAR(posix_major__doc__,
6554 "major(device) -> major number\n\
6555 Extracts a device major number from a raw device number.");
6557 static PyObject *
6558 posix_major(PyObject *self, PyObject *args)
6560 int device;
6561 if (!PyArg_ParseTuple(args, "i:major", &device))
6562 return NULL;
6563 return PyInt_FromLong((long)major(device));
6566 PyDoc_STRVAR(posix_minor__doc__,
6567 "minor(device) -> minor number\n\
6568 Extracts a device minor number from a raw device number.");
6570 static PyObject *
6571 posix_minor(PyObject *self, PyObject *args)
6573 int device;
6574 if (!PyArg_ParseTuple(args, "i:minor", &device))
6575 return NULL;
6576 return PyInt_FromLong((long)minor(device));
6579 PyDoc_STRVAR(posix_makedev__doc__,
6580 "makedev(major, minor) -> device number\n\
6581 Composes a raw device number from the major and minor device numbers.");
6583 static PyObject *
6584 posix_makedev(PyObject *self, PyObject *args)
6586 int major, minor;
6587 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6588 return NULL;
6589 return PyInt_FromLong((long)makedev(major, minor));
6591 #endif /* device macros */
6594 #ifdef HAVE_FTRUNCATE
6595 PyDoc_STRVAR(posix_ftruncate__doc__,
6596 "ftruncate(fd, length)\n\n\
6597 Truncate a file to a specified length.");
6599 static PyObject *
6600 posix_ftruncate(PyObject *self, PyObject *args)
6602 int fd;
6603 off_t length;
6604 int res;
6605 PyObject *lenobj;
6607 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6608 return NULL;
6610 #if !defined(HAVE_LARGEFILE_SUPPORT)
6611 length = PyInt_AsLong(lenobj);
6612 #else
6613 length = PyLong_Check(lenobj) ?
6614 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6615 #endif
6616 if (PyErr_Occurred())
6617 return NULL;
6619 Py_BEGIN_ALLOW_THREADS
6620 res = ftruncate(fd, length);
6621 Py_END_ALLOW_THREADS
6622 if (res < 0) {
6623 PyErr_SetFromErrno(PyExc_IOError);
6624 return NULL;
6626 Py_INCREF(Py_None);
6627 return Py_None;
6629 #endif
6631 #ifdef HAVE_PUTENV
6632 PyDoc_STRVAR(posix_putenv__doc__,
6633 "putenv(key, value)\n\n\
6634 Change or add an environment variable.");
6636 /* Save putenv() parameters as values here, so we can collect them when they
6637 * get re-set with another call for the same key. */
6638 static PyObject *posix_putenv_garbage;
6640 static PyObject *
6641 posix_putenv(PyObject *self, PyObject *args)
6643 char *s1, *s2;
6644 char *newenv;
6645 PyObject *newstr;
6646 size_t len;
6648 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6649 return NULL;
6651 #if defined(PYOS_OS2)
6652 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6653 APIRET rc;
6655 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6656 if (rc != NO_ERROR)
6657 return os2_error(rc);
6659 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6660 APIRET rc;
6662 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6663 if (rc != NO_ERROR)
6664 return os2_error(rc);
6665 } else {
6666 #endif
6668 /* XXX This can leak memory -- not easy to fix :-( */
6669 len = strlen(s1) + strlen(s2) + 2;
6670 /* len includes space for a trailing \0; the size arg to
6671 PyString_FromStringAndSize does not count that */
6672 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6673 if (newstr == NULL)
6674 return PyErr_NoMemory();
6675 newenv = PyString_AS_STRING(newstr);
6676 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6677 if (putenv(newenv)) {
6678 Py_DECREF(newstr);
6679 posix_error();
6680 return NULL;
6682 /* Install the first arg and newstr in posix_putenv_garbage;
6683 * this will cause previous value to be collected. This has to
6684 * happen after the real putenv() call because the old value
6685 * was still accessible until then. */
6686 if (PyDict_SetItem(posix_putenv_garbage,
6687 PyTuple_GET_ITEM(args, 0), newstr)) {
6688 /* really not much we can do; just leak */
6689 PyErr_Clear();
6691 else {
6692 Py_DECREF(newstr);
6695 #if defined(PYOS_OS2)
6697 #endif
6698 Py_INCREF(Py_None);
6699 return Py_None;
6701 #endif /* putenv */
6703 #ifdef HAVE_UNSETENV
6704 PyDoc_STRVAR(posix_unsetenv__doc__,
6705 "unsetenv(key)\n\n\
6706 Delete an environment variable.");
6708 static PyObject *
6709 posix_unsetenv(PyObject *self, PyObject *args)
6711 char *s1;
6713 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6714 return NULL;
6716 unsetenv(s1);
6718 /* Remove the key from posix_putenv_garbage;
6719 * this will cause it to be collected. This has to
6720 * happen after the real unsetenv() call because the
6721 * old value was still accessible until then.
6723 if (PyDict_DelItem(posix_putenv_garbage,
6724 PyTuple_GET_ITEM(args, 0))) {
6725 /* really not much we can do; just leak */
6726 PyErr_Clear();
6729 Py_INCREF(Py_None);
6730 return Py_None;
6732 #endif /* unsetenv */
6734 PyDoc_STRVAR(posix_strerror__doc__,
6735 "strerror(code) -> string\n\n\
6736 Translate an error code to a message string.");
6738 static PyObject *
6739 posix_strerror(PyObject *self, PyObject *args)
6741 int code;
6742 char *message;
6743 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6744 return NULL;
6745 message = strerror(code);
6746 if (message == NULL) {
6747 PyErr_SetString(PyExc_ValueError,
6748 "strerror() argument out of range");
6749 return NULL;
6751 return PyString_FromString(message);
6755 #ifdef HAVE_SYS_WAIT_H
6757 #ifdef WCOREDUMP
6758 PyDoc_STRVAR(posix_WCOREDUMP__doc__,
6759 "WCOREDUMP(status) -> bool\n\n\
6760 Return True if the process returning 'status' was dumped to a core file.");
6762 static PyObject *
6763 posix_WCOREDUMP(PyObject *self, PyObject *args)
6765 WAIT_TYPE status;
6766 WAIT_STATUS_INT(status) = 0;
6768 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6769 return NULL;
6771 return PyBool_FromLong(WCOREDUMP(status));
6773 #endif /* WCOREDUMP */
6775 #ifdef WIFCONTINUED
6776 PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
6777 "WIFCONTINUED(status) -> bool\n\n\
6778 Return True if the process returning 'status' was continued from a\n\
6779 job control stop.");
6781 static PyObject *
6782 posix_WIFCONTINUED(PyObject *self, PyObject *args)
6784 WAIT_TYPE status;
6785 WAIT_STATUS_INT(status) = 0;
6787 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6788 return NULL;
6790 return PyBool_FromLong(WIFCONTINUED(status));
6792 #endif /* WIFCONTINUED */
6794 #ifdef WIFSTOPPED
6795 PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
6796 "WIFSTOPPED(status) -> bool\n\n\
6797 Return True if the process returning 'status' was stopped.");
6799 static PyObject *
6800 posix_WIFSTOPPED(PyObject *self, PyObject *args)
6802 WAIT_TYPE status;
6803 WAIT_STATUS_INT(status) = 0;
6805 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6806 return NULL;
6808 return PyBool_FromLong(WIFSTOPPED(status));
6810 #endif /* WIFSTOPPED */
6812 #ifdef WIFSIGNALED
6813 PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
6814 "WIFSIGNALED(status) -> bool\n\n\
6815 Return True if the process returning 'status' was terminated by a signal.");
6817 static PyObject *
6818 posix_WIFSIGNALED(PyObject *self, PyObject *args)
6820 WAIT_TYPE status;
6821 WAIT_STATUS_INT(status) = 0;
6823 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6824 return NULL;
6826 return PyBool_FromLong(WIFSIGNALED(status));
6828 #endif /* WIFSIGNALED */
6830 #ifdef WIFEXITED
6831 PyDoc_STRVAR(posix_WIFEXITED__doc__,
6832 "WIFEXITED(status) -> bool\n\n\
6833 Return true if the process returning 'status' exited using the exit()\n\
6834 system call.");
6836 static PyObject *
6837 posix_WIFEXITED(PyObject *self, PyObject *args)
6839 WAIT_TYPE status;
6840 WAIT_STATUS_INT(status) = 0;
6842 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6843 return NULL;
6845 return PyBool_FromLong(WIFEXITED(status));
6847 #endif /* WIFEXITED */
6849 #ifdef WEXITSTATUS
6850 PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
6851 "WEXITSTATUS(status) -> integer\n\n\
6852 Return the process return code from 'status'.");
6854 static PyObject *
6855 posix_WEXITSTATUS(PyObject *self, PyObject *args)
6857 WAIT_TYPE status;
6858 WAIT_STATUS_INT(status) = 0;
6860 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6861 return NULL;
6863 return Py_BuildValue("i", WEXITSTATUS(status));
6865 #endif /* WEXITSTATUS */
6867 #ifdef WTERMSIG
6868 PyDoc_STRVAR(posix_WTERMSIG__doc__,
6869 "WTERMSIG(status) -> integer\n\n\
6870 Return the signal that terminated the process that provided the 'status'\n\
6871 value.");
6873 static PyObject *
6874 posix_WTERMSIG(PyObject *self, PyObject *args)
6876 WAIT_TYPE status;
6877 WAIT_STATUS_INT(status) = 0;
6879 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6880 return NULL;
6882 return Py_BuildValue("i", WTERMSIG(status));
6884 #endif /* WTERMSIG */
6886 #ifdef WSTOPSIG
6887 PyDoc_STRVAR(posix_WSTOPSIG__doc__,
6888 "WSTOPSIG(status) -> integer\n\n\
6889 Return the signal that stopped the process that provided\n\
6890 the 'status' value.");
6892 static PyObject *
6893 posix_WSTOPSIG(PyObject *self, PyObject *args)
6895 WAIT_TYPE status;
6896 WAIT_STATUS_INT(status) = 0;
6898 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6899 return NULL;
6901 return Py_BuildValue("i", WSTOPSIG(status));
6903 #endif /* WSTOPSIG */
6905 #endif /* HAVE_SYS_WAIT_H */
6908 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
6909 #ifdef _SCO_DS
6910 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6911 needed definitions in sys/statvfs.h */
6912 #define _SVID3
6913 #endif
6914 #include <sys/statvfs.h>
6916 static PyObject*
6917 _pystatvfs_fromstructstatvfs(struct statvfs st) {
6918 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6919 if (v == NULL)
6920 return NULL;
6922 #if !defined(HAVE_LARGEFILE_SUPPORT)
6923 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6924 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6925 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6926 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6927 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6928 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6929 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6930 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6931 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6932 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6933 #else
6934 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6935 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6936 PyStructSequence_SET_ITEM(v, 2,
6937 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6938 PyStructSequence_SET_ITEM(v, 3,
6939 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6940 PyStructSequence_SET_ITEM(v, 4,
6941 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6942 PyStructSequence_SET_ITEM(v, 5,
6943 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6944 PyStructSequence_SET_ITEM(v, 6,
6945 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6946 PyStructSequence_SET_ITEM(v, 7,
6947 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6948 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6949 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6950 #endif
6952 return v;
6955 PyDoc_STRVAR(posix_fstatvfs__doc__,
6956 "fstatvfs(fd) -> statvfs result\n\n\
6957 Perform an fstatvfs system call on the given fd.");
6959 static PyObject *
6960 posix_fstatvfs(PyObject *self, PyObject *args)
6962 int fd, res;
6963 struct statvfs st;
6965 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6966 return NULL;
6967 Py_BEGIN_ALLOW_THREADS
6968 res = fstatvfs(fd, &st);
6969 Py_END_ALLOW_THREADS
6970 if (res != 0)
6971 return posix_error();
6973 return _pystatvfs_fromstructstatvfs(st);
6975 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
6978 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
6979 #include <sys/statvfs.h>
6981 PyDoc_STRVAR(posix_statvfs__doc__,
6982 "statvfs(path) -> statvfs result\n\n\
6983 Perform a statvfs system call on the given path.");
6985 static PyObject *
6986 posix_statvfs(PyObject *self, PyObject *args)
6988 char *path;
6989 int res;
6990 struct statvfs st;
6991 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6992 return NULL;
6993 Py_BEGIN_ALLOW_THREADS
6994 res = statvfs(path, &st);
6995 Py_END_ALLOW_THREADS
6996 if (res != 0)
6997 return posix_error_with_filename(path);
6999 return _pystatvfs_fromstructstatvfs(st);
7001 #endif /* HAVE_STATVFS */
7004 #ifdef HAVE_TEMPNAM
7005 PyDoc_STRVAR(posix_tempnam__doc__,
7006 "tempnam([dir[, prefix]]) -> string\n\n\
7007 Return a unique name for a temporary file.\n\
7008 The directory and a prefix may be specified as strings; they may be omitted\n\
7009 or None if not needed.");
7011 static PyObject *
7012 posix_tempnam(PyObject *self, PyObject *args)
7014 PyObject *result = NULL;
7015 char *dir = NULL;
7016 char *pfx = NULL;
7017 char *name;
7019 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
7020 return NULL;
7022 if (PyErr_Warn(PyExc_RuntimeWarning,
7023 "tempnam is a potential security risk to your program") < 0)
7024 return NULL;
7026 #ifdef MS_WINDOWS
7027 name = _tempnam(dir, pfx);
7028 #else
7029 name = tempnam(dir, pfx);
7030 #endif
7031 if (name == NULL)
7032 return PyErr_NoMemory();
7033 result = PyString_FromString(name);
7034 free(name);
7035 return result;
7037 #endif
7040 #ifdef HAVE_TMPFILE
7041 PyDoc_STRVAR(posix_tmpfile__doc__,
7042 "tmpfile() -> file object\n\n\
7043 Create a temporary file with no directory entries.");
7045 static PyObject *
7046 posix_tmpfile(PyObject *self, PyObject *noargs)
7048 FILE *fp;
7050 fp = tmpfile();
7051 if (fp == NULL)
7052 return posix_error();
7053 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
7055 #endif
7058 #ifdef HAVE_TMPNAM
7059 PyDoc_STRVAR(posix_tmpnam__doc__,
7060 "tmpnam() -> string\n\n\
7061 Return a unique name for a temporary file.");
7063 static PyObject *
7064 posix_tmpnam(PyObject *self, PyObject *noargs)
7066 char buffer[L_tmpnam];
7067 char *name;
7069 if (PyErr_Warn(PyExc_RuntimeWarning,
7070 "tmpnam is a potential security risk to your program") < 0)
7071 return NULL;
7073 #ifdef USE_TMPNAM_R
7074 name = tmpnam_r(buffer);
7075 #else
7076 name = tmpnam(buffer);
7077 #endif
7078 if (name == NULL) {
7079 PyObject *err = Py_BuildValue("is", 0,
7080 #ifdef USE_TMPNAM_R
7081 "unexpected NULL from tmpnam_r"
7082 #else
7083 "unexpected NULL from tmpnam"
7084 #endif
7086 PyErr_SetObject(PyExc_OSError, err);
7087 Py_XDECREF(err);
7088 return NULL;
7090 return PyString_FromString(buffer);
7092 #endif
7095 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7096 * It maps strings representing configuration variable names to
7097 * integer values, allowing those functions to be called with the
7098 * magic names instead of polluting the module's namespace with tons of
7099 * rarely-used constants. There are three separate tables that use
7100 * these definitions.
7102 * This code is always included, even if none of the interfaces that
7103 * need it are included. The #if hackery needed to avoid it would be
7104 * sufficiently pervasive that it's not worth the loss of readability.
7106 struct constdef {
7107 char *name;
7108 long value;
7111 static int
7112 conv_confname(PyObject *arg, int *valuep, struct constdef *table,
7113 size_t tablesize)
7115 if (PyInt_Check(arg)) {
7116 *valuep = PyInt_AS_LONG(arg);
7117 return 1;
7119 if (PyString_Check(arg)) {
7120 /* look up the value in the table using a binary search */
7121 size_t lo = 0;
7122 size_t mid;
7123 size_t hi = tablesize;
7124 int cmp;
7125 char *confname = PyString_AS_STRING(arg);
7126 while (lo < hi) {
7127 mid = (lo + hi) / 2;
7128 cmp = strcmp(confname, table[mid].name);
7129 if (cmp < 0)
7130 hi = mid;
7131 else if (cmp > 0)
7132 lo = mid + 1;
7133 else {
7134 *valuep = table[mid].value;
7135 return 1;
7138 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7140 else
7141 PyErr_SetString(PyExc_TypeError,
7142 "configuration names must be strings or integers");
7143 return 0;
7147 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7148 static struct constdef posix_constants_pathconf[] = {
7149 #ifdef _PC_ABI_AIO_XFER_MAX
7150 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7151 #endif
7152 #ifdef _PC_ABI_ASYNC_IO
7153 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7154 #endif
7155 #ifdef _PC_ASYNC_IO
7156 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7157 #endif
7158 #ifdef _PC_CHOWN_RESTRICTED
7159 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7160 #endif
7161 #ifdef _PC_FILESIZEBITS
7162 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7163 #endif
7164 #ifdef _PC_LAST
7165 {"PC_LAST", _PC_LAST},
7166 #endif
7167 #ifdef _PC_LINK_MAX
7168 {"PC_LINK_MAX", _PC_LINK_MAX},
7169 #endif
7170 #ifdef _PC_MAX_CANON
7171 {"PC_MAX_CANON", _PC_MAX_CANON},
7172 #endif
7173 #ifdef _PC_MAX_INPUT
7174 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7175 #endif
7176 #ifdef _PC_NAME_MAX
7177 {"PC_NAME_MAX", _PC_NAME_MAX},
7178 #endif
7179 #ifdef _PC_NO_TRUNC
7180 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7181 #endif
7182 #ifdef _PC_PATH_MAX
7183 {"PC_PATH_MAX", _PC_PATH_MAX},
7184 #endif
7185 #ifdef _PC_PIPE_BUF
7186 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7187 #endif
7188 #ifdef _PC_PRIO_IO
7189 {"PC_PRIO_IO", _PC_PRIO_IO},
7190 #endif
7191 #ifdef _PC_SOCK_MAXBUF
7192 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7193 #endif
7194 #ifdef _PC_SYNC_IO
7195 {"PC_SYNC_IO", _PC_SYNC_IO},
7196 #endif
7197 #ifdef _PC_VDISABLE
7198 {"PC_VDISABLE", _PC_VDISABLE},
7199 #endif
7202 static int
7203 conv_path_confname(PyObject *arg, int *valuep)
7205 return conv_confname(arg, valuep, posix_constants_pathconf,
7206 sizeof(posix_constants_pathconf)
7207 / sizeof(struct constdef));
7209 #endif
7211 #ifdef HAVE_FPATHCONF
7212 PyDoc_STRVAR(posix_fpathconf__doc__,
7213 "fpathconf(fd, name) -> integer\n\n\
7214 Return the configuration limit name for the file descriptor fd.\n\
7215 If there is no limit, return -1.");
7217 static PyObject *
7218 posix_fpathconf(PyObject *self, PyObject *args)
7220 PyObject *result = NULL;
7221 int name, fd;
7223 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7224 conv_path_confname, &name)) {
7225 long limit;
7227 errno = 0;
7228 limit = fpathconf(fd, name);
7229 if (limit == -1 && errno != 0)
7230 posix_error();
7231 else
7232 result = PyInt_FromLong(limit);
7234 return result;
7236 #endif
7239 #ifdef HAVE_PATHCONF
7240 PyDoc_STRVAR(posix_pathconf__doc__,
7241 "pathconf(path, name) -> integer\n\n\
7242 Return the configuration limit name for the file or directory path.\n\
7243 If there is no limit, return -1.");
7245 static PyObject *
7246 posix_pathconf(PyObject *self, PyObject *args)
7248 PyObject *result = NULL;
7249 int name;
7250 char *path;
7252 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7253 conv_path_confname, &name)) {
7254 long limit;
7256 errno = 0;
7257 limit = pathconf(path, name);
7258 if (limit == -1 && errno != 0) {
7259 if (errno == EINVAL)
7260 /* could be a path or name problem */
7261 posix_error();
7262 else
7263 posix_error_with_filename(path);
7265 else
7266 result = PyInt_FromLong(limit);
7268 return result;
7270 #endif
7272 #ifdef HAVE_CONFSTR
7273 static struct constdef posix_constants_confstr[] = {
7274 #ifdef _CS_ARCHITECTURE
7275 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7276 #endif
7277 #ifdef _CS_HOSTNAME
7278 {"CS_HOSTNAME", _CS_HOSTNAME},
7279 #endif
7280 #ifdef _CS_HW_PROVIDER
7281 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7282 #endif
7283 #ifdef _CS_HW_SERIAL
7284 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7285 #endif
7286 #ifdef _CS_INITTAB_NAME
7287 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7288 #endif
7289 #ifdef _CS_LFS64_CFLAGS
7290 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7291 #endif
7292 #ifdef _CS_LFS64_LDFLAGS
7293 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7294 #endif
7295 #ifdef _CS_LFS64_LIBS
7296 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7297 #endif
7298 #ifdef _CS_LFS64_LINTFLAGS
7299 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7300 #endif
7301 #ifdef _CS_LFS_CFLAGS
7302 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7303 #endif
7304 #ifdef _CS_LFS_LDFLAGS
7305 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7306 #endif
7307 #ifdef _CS_LFS_LIBS
7308 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7309 #endif
7310 #ifdef _CS_LFS_LINTFLAGS
7311 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7312 #endif
7313 #ifdef _CS_MACHINE
7314 {"CS_MACHINE", _CS_MACHINE},
7315 #endif
7316 #ifdef _CS_PATH
7317 {"CS_PATH", _CS_PATH},
7318 #endif
7319 #ifdef _CS_RELEASE
7320 {"CS_RELEASE", _CS_RELEASE},
7321 #endif
7322 #ifdef _CS_SRPC_DOMAIN
7323 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7324 #endif
7325 #ifdef _CS_SYSNAME
7326 {"CS_SYSNAME", _CS_SYSNAME},
7327 #endif
7328 #ifdef _CS_VERSION
7329 {"CS_VERSION", _CS_VERSION},
7330 #endif
7331 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7332 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7333 #endif
7334 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7335 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7336 #endif
7337 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
7338 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7339 #endif
7340 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7341 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7342 #endif
7343 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7344 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7345 #endif
7346 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7347 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7348 #endif
7349 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7350 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7351 #endif
7352 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7353 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7354 #endif
7355 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7356 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7357 #endif
7358 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7359 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7360 #endif
7361 #ifdef _CS_XBS5_LP64_OFF64_LIBS
7362 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7363 #endif
7364 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7365 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7366 #endif
7367 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7368 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7369 #endif
7370 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7371 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7372 #endif
7373 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7374 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7375 #endif
7376 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7377 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7378 #endif
7379 #ifdef _MIPS_CS_AVAIL_PROCESSORS
7380 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7381 #endif
7382 #ifdef _MIPS_CS_BASE
7383 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7384 #endif
7385 #ifdef _MIPS_CS_HOSTID
7386 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7387 #endif
7388 #ifdef _MIPS_CS_HW_NAME
7389 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7390 #endif
7391 #ifdef _MIPS_CS_NUM_PROCESSORS
7392 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7393 #endif
7394 #ifdef _MIPS_CS_OSREL_MAJ
7395 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7396 #endif
7397 #ifdef _MIPS_CS_OSREL_MIN
7398 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7399 #endif
7400 #ifdef _MIPS_CS_OSREL_PATCH
7401 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7402 #endif
7403 #ifdef _MIPS_CS_OS_NAME
7404 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7405 #endif
7406 #ifdef _MIPS_CS_OS_PROVIDER
7407 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7408 #endif
7409 #ifdef _MIPS_CS_PROCESSORS
7410 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7411 #endif
7412 #ifdef _MIPS_CS_SERIAL
7413 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7414 #endif
7415 #ifdef _MIPS_CS_VENDOR
7416 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7417 #endif
7420 static int
7421 conv_confstr_confname(PyObject *arg, int *valuep)
7423 return conv_confname(arg, valuep, posix_constants_confstr,
7424 sizeof(posix_constants_confstr)
7425 / sizeof(struct constdef));
7428 PyDoc_STRVAR(posix_confstr__doc__,
7429 "confstr(name) -> string\n\n\
7430 Return a string-valued system configuration variable.");
7432 static PyObject *
7433 posix_confstr(PyObject *self, PyObject *args)
7435 PyObject *result = NULL;
7436 int name;
7437 char buffer[256];
7439 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
7440 int len;
7442 errno = 0;
7443 len = confstr(name, buffer, sizeof(buffer));
7444 if (len == 0) {
7445 if (errno) {
7446 posix_error();
7448 else {
7449 result = Py_None;
7450 Py_INCREF(Py_None);
7453 else {
7454 if ((unsigned int)len >= sizeof(buffer)) {
7455 result = PyString_FromStringAndSize(NULL, len-1);
7456 if (result != NULL)
7457 confstr(name, PyString_AS_STRING(result), len);
7459 else
7460 result = PyString_FromStringAndSize(buffer, len-1);
7463 return result;
7465 #endif
7468 #ifdef HAVE_SYSCONF
7469 static struct constdef posix_constants_sysconf[] = {
7470 #ifdef _SC_2_CHAR_TERM
7471 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7472 #endif
7473 #ifdef _SC_2_C_BIND
7474 {"SC_2_C_BIND", _SC_2_C_BIND},
7475 #endif
7476 #ifdef _SC_2_C_DEV
7477 {"SC_2_C_DEV", _SC_2_C_DEV},
7478 #endif
7479 #ifdef _SC_2_C_VERSION
7480 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7481 #endif
7482 #ifdef _SC_2_FORT_DEV
7483 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7484 #endif
7485 #ifdef _SC_2_FORT_RUN
7486 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7487 #endif
7488 #ifdef _SC_2_LOCALEDEF
7489 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7490 #endif
7491 #ifdef _SC_2_SW_DEV
7492 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7493 #endif
7494 #ifdef _SC_2_UPE
7495 {"SC_2_UPE", _SC_2_UPE},
7496 #endif
7497 #ifdef _SC_2_VERSION
7498 {"SC_2_VERSION", _SC_2_VERSION},
7499 #endif
7500 #ifdef _SC_ABI_ASYNCHRONOUS_IO
7501 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7502 #endif
7503 #ifdef _SC_ACL
7504 {"SC_ACL", _SC_ACL},
7505 #endif
7506 #ifdef _SC_AIO_LISTIO_MAX
7507 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7508 #endif
7509 #ifdef _SC_AIO_MAX
7510 {"SC_AIO_MAX", _SC_AIO_MAX},
7511 #endif
7512 #ifdef _SC_AIO_PRIO_DELTA_MAX
7513 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7514 #endif
7515 #ifdef _SC_ARG_MAX
7516 {"SC_ARG_MAX", _SC_ARG_MAX},
7517 #endif
7518 #ifdef _SC_ASYNCHRONOUS_IO
7519 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7520 #endif
7521 #ifdef _SC_ATEXIT_MAX
7522 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7523 #endif
7524 #ifdef _SC_AUDIT
7525 {"SC_AUDIT", _SC_AUDIT},
7526 #endif
7527 #ifdef _SC_AVPHYS_PAGES
7528 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7529 #endif
7530 #ifdef _SC_BC_BASE_MAX
7531 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7532 #endif
7533 #ifdef _SC_BC_DIM_MAX
7534 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7535 #endif
7536 #ifdef _SC_BC_SCALE_MAX
7537 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7538 #endif
7539 #ifdef _SC_BC_STRING_MAX
7540 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7541 #endif
7542 #ifdef _SC_CAP
7543 {"SC_CAP", _SC_CAP},
7544 #endif
7545 #ifdef _SC_CHARCLASS_NAME_MAX
7546 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7547 #endif
7548 #ifdef _SC_CHAR_BIT
7549 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7550 #endif
7551 #ifdef _SC_CHAR_MAX
7552 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7553 #endif
7554 #ifdef _SC_CHAR_MIN
7555 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7556 #endif
7557 #ifdef _SC_CHILD_MAX
7558 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7559 #endif
7560 #ifdef _SC_CLK_TCK
7561 {"SC_CLK_TCK", _SC_CLK_TCK},
7562 #endif
7563 #ifdef _SC_COHER_BLKSZ
7564 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7565 #endif
7566 #ifdef _SC_COLL_WEIGHTS_MAX
7567 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7568 #endif
7569 #ifdef _SC_DCACHE_ASSOC
7570 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7571 #endif
7572 #ifdef _SC_DCACHE_BLKSZ
7573 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7574 #endif
7575 #ifdef _SC_DCACHE_LINESZ
7576 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7577 #endif
7578 #ifdef _SC_DCACHE_SZ
7579 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7580 #endif
7581 #ifdef _SC_DCACHE_TBLKSZ
7582 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7583 #endif
7584 #ifdef _SC_DELAYTIMER_MAX
7585 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7586 #endif
7587 #ifdef _SC_EQUIV_CLASS_MAX
7588 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7589 #endif
7590 #ifdef _SC_EXPR_NEST_MAX
7591 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7592 #endif
7593 #ifdef _SC_FSYNC
7594 {"SC_FSYNC", _SC_FSYNC},
7595 #endif
7596 #ifdef _SC_GETGR_R_SIZE_MAX
7597 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7598 #endif
7599 #ifdef _SC_GETPW_R_SIZE_MAX
7600 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7601 #endif
7602 #ifdef _SC_ICACHE_ASSOC
7603 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7604 #endif
7605 #ifdef _SC_ICACHE_BLKSZ
7606 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7607 #endif
7608 #ifdef _SC_ICACHE_LINESZ
7609 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7610 #endif
7611 #ifdef _SC_ICACHE_SZ
7612 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7613 #endif
7614 #ifdef _SC_INF
7615 {"SC_INF", _SC_INF},
7616 #endif
7617 #ifdef _SC_INT_MAX
7618 {"SC_INT_MAX", _SC_INT_MAX},
7619 #endif
7620 #ifdef _SC_INT_MIN
7621 {"SC_INT_MIN", _SC_INT_MIN},
7622 #endif
7623 #ifdef _SC_IOV_MAX
7624 {"SC_IOV_MAX", _SC_IOV_MAX},
7625 #endif
7626 #ifdef _SC_IP_SECOPTS
7627 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7628 #endif
7629 #ifdef _SC_JOB_CONTROL
7630 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7631 #endif
7632 #ifdef _SC_KERN_POINTERS
7633 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7634 #endif
7635 #ifdef _SC_KERN_SIM
7636 {"SC_KERN_SIM", _SC_KERN_SIM},
7637 #endif
7638 #ifdef _SC_LINE_MAX
7639 {"SC_LINE_MAX", _SC_LINE_MAX},
7640 #endif
7641 #ifdef _SC_LOGIN_NAME_MAX
7642 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7643 #endif
7644 #ifdef _SC_LOGNAME_MAX
7645 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7646 #endif
7647 #ifdef _SC_LONG_BIT
7648 {"SC_LONG_BIT", _SC_LONG_BIT},
7649 #endif
7650 #ifdef _SC_MAC
7651 {"SC_MAC", _SC_MAC},
7652 #endif
7653 #ifdef _SC_MAPPED_FILES
7654 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7655 #endif
7656 #ifdef _SC_MAXPID
7657 {"SC_MAXPID", _SC_MAXPID},
7658 #endif
7659 #ifdef _SC_MB_LEN_MAX
7660 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7661 #endif
7662 #ifdef _SC_MEMLOCK
7663 {"SC_MEMLOCK", _SC_MEMLOCK},
7664 #endif
7665 #ifdef _SC_MEMLOCK_RANGE
7666 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7667 #endif
7668 #ifdef _SC_MEMORY_PROTECTION
7669 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7670 #endif
7671 #ifdef _SC_MESSAGE_PASSING
7672 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7673 #endif
7674 #ifdef _SC_MMAP_FIXED_ALIGNMENT
7675 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7676 #endif
7677 #ifdef _SC_MQ_OPEN_MAX
7678 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7679 #endif
7680 #ifdef _SC_MQ_PRIO_MAX
7681 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7682 #endif
7683 #ifdef _SC_NACLS_MAX
7684 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7685 #endif
7686 #ifdef _SC_NGROUPS_MAX
7687 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7688 #endif
7689 #ifdef _SC_NL_ARGMAX
7690 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7691 #endif
7692 #ifdef _SC_NL_LANGMAX
7693 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7694 #endif
7695 #ifdef _SC_NL_MSGMAX
7696 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7697 #endif
7698 #ifdef _SC_NL_NMAX
7699 {"SC_NL_NMAX", _SC_NL_NMAX},
7700 #endif
7701 #ifdef _SC_NL_SETMAX
7702 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7703 #endif
7704 #ifdef _SC_NL_TEXTMAX
7705 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7706 #endif
7707 #ifdef _SC_NPROCESSORS_CONF
7708 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7709 #endif
7710 #ifdef _SC_NPROCESSORS_ONLN
7711 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7712 #endif
7713 #ifdef _SC_NPROC_CONF
7714 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7715 #endif
7716 #ifdef _SC_NPROC_ONLN
7717 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7718 #endif
7719 #ifdef _SC_NZERO
7720 {"SC_NZERO", _SC_NZERO},
7721 #endif
7722 #ifdef _SC_OPEN_MAX
7723 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7724 #endif
7725 #ifdef _SC_PAGESIZE
7726 {"SC_PAGESIZE", _SC_PAGESIZE},
7727 #endif
7728 #ifdef _SC_PAGE_SIZE
7729 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7730 #endif
7731 #ifdef _SC_PASS_MAX
7732 {"SC_PASS_MAX", _SC_PASS_MAX},
7733 #endif
7734 #ifdef _SC_PHYS_PAGES
7735 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7736 #endif
7737 #ifdef _SC_PII
7738 {"SC_PII", _SC_PII},
7739 #endif
7740 #ifdef _SC_PII_INTERNET
7741 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7742 #endif
7743 #ifdef _SC_PII_INTERNET_DGRAM
7744 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7745 #endif
7746 #ifdef _SC_PII_INTERNET_STREAM
7747 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7748 #endif
7749 #ifdef _SC_PII_OSI
7750 {"SC_PII_OSI", _SC_PII_OSI},
7751 #endif
7752 #ifdef _SC_PII_OSI_CLTS
7753 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7754 #endif
7755 #ifdef _SC_PII_OSI_COTS
7756 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7757 #endif
7758 #ifdef _SC_PII_OSI_M
7759 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7760 #endif
7761 #ifdef _SC_PII_SOCKET
7762 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7763 #endif
7764 #ifdef _SC_PII_XTI
7765 {"SC_PII_XTI", _SC_PII_XTI},
7766 #endif
7767 #ifdef _SC_POLL
7768 {"SC_POLL", _SC_POLL},
7769 #endif
7770 #ifdef _SC_PRIORITIZED_IO
7771 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7772 #endif
7773 #ifdef _SC_PRIORITY_SCHEDULING
7774 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7775 #endif
7776 #ifdef _SC_REALTIME_SIGNALS
7777 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7778 #endif
7779 #ifdef _SC_RE_DUP_MAX
7780 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7781 #endif
7782 #ifdef _SC_RTSIG_MAX
7783 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7784 #endif
7785 #ifdef _SC_SAVED_IDS
7786 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7787 #endif
7788 #ifdef _SC_SCHAR_MAX
7789 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7790 #endif
7791 #ifdef _SC_SCHAR_MIN
7792 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7793 #endif
7794 #ifdef _SC_SELECT
7795 {"SC_SELECT", _SC_SELECT},
7796 #endif
7797 #ifdef _SC_SEMAPHORES
7798 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7799 #endif
7800 #ifdef _SC_SEM_NSEMS_MAX
7801 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7802 #endif
7803 #ifdef _SC_SEM_VALUE_MAX
7804 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7805 #endif
7806 #ifdef _SC_SHARED_MEMORY_OBJECTS
7807 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7808 #endif
7809 #ifdef _SC_SHRT_MAX
7810 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7811 #endif
7812 #ifdef _SC_SHRT_MIN
7813 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7814 #endif
7815 #ifdef _SC_SIGQUEUE_MAX
7816 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7817 #endif
7818 #ifdef _SC_SIGRT_MAX
7819 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7820 #endif
7821 #ifdef _SC_SIGRT_MIN
7822 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7823 #endif
7824 #ifdef _SC_SOFTPOWER
7825 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7826 #endif
7827 #ifdef _SC_SPLIT_CACHE
7828 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7829 #endif
7830 #ifdef _SC_SSIZE_MAX
7831 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7832 #endif
7833 #ifdef _SC_STACK_PROT
7834 {"SC_STACK_PROT", _SC_STACK_PROT},
7835 #endif
7836 #ifdef _SC_STREAM_MAX
7837 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7838 #endif
7839 #ifdef _SC_SYNCHRONIZED_IO
7840 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7841 #endif
7842 #ifdef _SC_THREADS
7843 {"SC_THREADS", _SC_THREADS},
7844 #endif
7845 #ifdef _SC_THREAD_ATTR_STACKADDR
7846 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7847 #endif
7848 #ifdef _SC_THREAD_ATTR_STACKSIZE
7849 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7850 #endif
7851 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7852 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7853 #endif
7854 #ifdef _SC_THREAD_KEYS_MAX
7855 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7856 #endif
7857 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
7858 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7859 #endif
7860 #ifdef _SC_THREAD_PRIO_INHERIT
7861 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7862 #endif
7863 #ifdef _SC_THREAD_PRIO_PROTECT
7864 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7865 #endif
7866 #ifdef _SC_THREAD_PROCESS_SHARED
7867 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7868 #endif
7869 #ifdef _SC_THREAD_SAFE_FUNCTIONS
7870 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7871 #endif
7872 #ifdef _SC_THREAD_STACK_MIN
7873 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7874 #endif
7875 #ifdef _SC_THREAD_THREADS_MAX
7876 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7877 #endif
7878 #ifdef _SC_TIMERS
7879 {"SC_TIMERS", _SC_TIMERS},
7880 #endif
7881 #ifdef _SC_TIMER_MAX
7882 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7883 #endif
7884 #ifdef _SC_TTY_NAME_MAX
7885 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7886 #endif
7887 #ifdef _SC_TZNAME_MAX
7888 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7889 #endif
7890 #ifdef _SC_T_IOV_MAX
7891 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7892 #endif
7893 #ifdef _SC_UCHAR_MAX
7894 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7895 #endif
7896 #ifdef _SC_UINT_MAX
7897 {"SC_UINT_MAX", _SC_UINT_MAX},
7898 #endif
7899 #ifdef _SC_UIO_MAXIOV
7900 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7901 #endif
7902 #ifdef _SC_ULONG_MAX
7903 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7904 #endif
7905 #ifdef _SC_USHRT_MAX
7906 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7907 #endif
7908 #ifdef _SC_VERSION
7909 {"SC_VERSION", _SC_VERSION},
7910 #endif
7911 #ifdef _SC_WORD_BIT
7912 {"SC_WORD_BIT", _SC_WORD_BIT},
7913 #endif
7914 #ifdef _SC_XBS5_ILP32_OFF32
7915 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7916 #endif
7917 #ifdef _SC_XBS5_ILP32_OFFBIG
7918 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7919 #endif
7920 #ifdef _SC_XBS5_LP64_OFF64
7921 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7922 #endif
7923 #ifdef _SC_XBS5_LPBIG_OFFBIG
7924 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7925 #endif
7926 #ifdef _SC_XOPEN_CRYPT
7927 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7928 #endif
7929 #ifdef _SC_XOPEN_ENH_I18N
7930 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7931 #endif
7932 #ifdef _SC_XOPEN_LEGACY
7933 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7934 #endif
7935 #ifdef _SC_XOPEN_REALTIME
7936 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7937 #endif
7938 #ifdef _SC_XOPEN_REALTIME_THREADS
7939 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7940 #endif
7941 #ifdef _SC_XOPEN_SHM
7942 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7943 #endif
7944 #ifdef _SC_XOPEN_UNIX
7945 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7946 #endif
7947 #ifdef _SC_XOPEN_VERSION
7948 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7949 #endif
7950 #ifdef _SC_XOPEN_XCU_VERSION
7951 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7952 #endif
7953 #ifdef _SC_XOPEN_XPG2
7954 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7955 #endif
7956 #ifdef _SC_XOPEN_XPG3
7957 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7958 #endif
7959 #ifdef _SC_XOPEN_XPG4
7960 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7961 #endif
7964 static int
7965 conv_sysconf_confname(PyObject *arg, int *valuep)
7967 return conv_confname(arg, valuep, posix_constants_sysconf,
7968 sizeof(posix_constants_sysconf)
7969 / sizeof(struct constdef));
7972 PyDoc_STRVAR(posix_sysconf__doc__,
7973 "sysconf(name) -> integer\n\n\
7974 Return an integer-valued system configuration variable.");
7976 static PyObject *
7977 posix_sysconf(PyObject *self, PyObject *args)
7979 PyObject *result = NULL;
7980 int name;
7982 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7983 int value;
7985 errno = 0;
7986 value = sysconf(name);
7987 if (value == -1 && errno != 0)
7988 posix_error();
7989 else
7990 result = PyInt_FromLong(value);
7992 return result;
7994 #endif
7997 /* This code is used to ensure that the tables of configuration value names
7998 * are in sorted order as required by conv_confname(), and also to build the
7999 * the exported dictionaries that are used to publish information about the
8000 * names available on the host platform.
8002 * Sorting the table at runtime ensures that the table is properly ordered
8003 * when used, even for platforms we're not able to test on. It also makes
8004 * it easier to add additional entries to the tables.
8007 static int
8008 cmp_constdefs(const void *v1, const void *v2)
8010 const struct constdef *c1 =
8011 (const struct constdef *) v1;
8012 const struct constdef *c2 =
8013 (const struct constdef *) v2;
8015 return strcmp(c1->name, c2->name);
8018 static int
8019 setup_confname_table(struct constdef *table, size_t tablesize,
8020 char *tablename, PyObject *module)
8022 PyObject *d = NULL;
8023 size_t i;
8025 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8026 d = PyDict_New();
8027 if (d == NULL)
8028 return -1;
8030 for (i=0; i < tablesize; ++i) {
8031 PyObject *o = PyInt_FromLong(table[i].value);
8032 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8033 Py_XDECREF(o);
8034 Py_DECREF(d);
8035 return -1;
8037 Py_DECREF(o);
8039 return PyModule_AddObject(module, tablename, d);
8042 /* Return -1 on failure, 0 on success. */
8043 static int
8044 setup_confname_tables(PyObject *module)
8046 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8047 if (setup_confname_table(posix_constants_pathconf,
8048 sizeof(posix_constants_pathconf)
8049 / sizeof(struct constdef),
8050 "pathconf_names", module))
8051 return -1;
8052 #endif
8053 #ifdef HAVE_CONFSTR
8054 if (setup_confname_table(posix_constants_confstr,
8055 sizeof(posix_constants_confstr)
8056 / sizeof(struct constdef),
8057 "confstr_names", module))
8058 return -1;
8059 #endif
8060 #ifdef HAVE_SYSCONF
8061 if (setup_confname_table(posix_constants_sysconf,
8062 sizeof(posix_constants_sysconf)
8063 / sizeof(struct constdef),
8064 "sysconf_names", module))
8065 return -1;
8066 #endif
8067 return 0;
8071 PyDoc_STRVAR(posix_abort__doc__,
8072 "abort() -> does not return!\n\n\
8073 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
8074 in the hardest way possible on the hosting operating system.");
8076 static PyObject *
8077 posix_abort(PyObject *self, PyObject *noargs)
8079 abort();
8080 /*NOTREACHED*/
8081 Py_FatalError("abort() called from Python code didn't abort!");
8082 return NULL;
8085 #ifdef MS_WINDOWS
8086 PyDoc_STRVAR(win32_startfile__doc__,
8087 "startfile(filepath [, operation]) - Start a file with its associated\n\
8088 application.\n\
8090 When \"operation\" is not specified or \"open\", this acts like\n\
8091 double-clicking the file in Explorer, or giving the file name as an\n\
8092 argument to the DOS \"start\" command: the file is opened with whatever\n\
8093 application (if any) its extension is associated.\n\
8094 When another \"operation\" is given, it specifies what should be done with\n\
8095 the file. A typical operation is \"print\".\n\
8097 startfile returns as soon as the associated application is launched.\n\
8098 There is no option to wait for the application to close, and no way\n\
8099 to retrieve the application's exit status.\n\
8101 The filepath is relative to the current directory. If you want to use\n\
8102 an absolute path, make sure the first character is not a slash (\"/\");\n\
8103 the underlying Win32 ShellExecute function doesn't work if it is.");
8105 static PyObject *
8106 win32_startfile(PyObject *self, PyObject *args)
8108 char *filepath;
8109 char *operation = NULL;
8110 HINSTANCE rc;
8111 #ifdef Py_WIN_WIDE_FILENAMES
8112 if (unicode_file_names()) {
8113 PyObject *unipath, *woperation = NULL;
8114 if (!PyArg_ParseTuple(args, "U|s:startfile",
8115 &unipath, &operation)) {
8116 PyErr_Clear();
8117 goto normal;
8121 if (operation) {
8122 woperation = PyUnicode_DecodeASCII(operation,
8123 strlen(operation), NULL);
8124 if (!woperation) {
8125 PyErr_Clear();
8126 operation = NULL;
8127 goto normal;
8131 Py_BEGIN_ALLOW_THREADS
8132 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8133 PyUnicode_AS_UNICODE(unipath),
8134 NULL, NULL, SW_SHOWNORMAL);
8135 Py_END_ALLOW_THREADS
8137 Py_XDECREF(woperation);
8138 if (rc <= (HINSTANCE)32) {
8139 PyObject *errval = win32_error_unicode("startfile",
8140 PyUnicode_AS_UNICODE(unipath));
8141 return errval;
8143 Py_INCREF(Py_None);
8144 return Py_None;
8146 #endif
8148 normal:
8149 if (!PyArg_ParseTuple(args, "et|s:startfile",
8150 Py_FileSystemDefaultEncoding, &filepath,
8151 &operation))
8152 return NULL;
8153 Py_BEGIN_ALLOW_THREADS
8154 rc = ShellExecute((HWND)0, operation, filepath,
8155 NULL, NULL, SW_SHOWNORMAL);
8156 Py_END_ALLOW_THREADS
8157 if (rc <= (HINSTANCE)32) {
8158 PyObject *errval = win32_error("startfile", filepath);
8159 PyMem_Free(filepath);
8160 return errval;
8162 PyMem_Free(filepath);
8163 Py_INCREF(Py_None);
8164 return Py_None;
8166 #endif
8168 #ifdef HAVE_GETLOADAVG
8169 PyDoc_STRVAR(posix_getloadavg__doc__,
8170 "getloadavg() -> (float, float, float)\n\n\
8171 Return the number of processes in the system run queue averaged over\n\
8172 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8173 was unobtainable");
8175 static PyObject *
8176 posix_getloadavg(PyObject *self, PyObject *noargs)
8178 double loadavg[3];
8179 if (getloadavg(loadavg, 3)!=3) {
8180 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8181 return NULL;
8182 } else
8183 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8185 #endif
8187 #ifdef MS_WINDOWS
8189 PyDoc_STRVAR(win32_urandom__doc__,
8190 "urandom(n) -> str\n\n\
8191 Return a string of n random bytes suitable for cryptographic use.");
8193 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8194 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8195 DWORD dwFlags );
8196 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8197 BYTE *pbBuffer );
8199 static CRYPTGENRANDOM pCryptGenRandom = NULL;
8200 /* This handle is never explicitly released. Instead, the operating
8201 system will release it when the process terminates. */
8202 static HCRYPTPROV hCryptProv = 0;
8204 static PyObject*
8205 win32_urandom(PyObject *self, PyObject *args)
8207 int howMany;
8208 PyObject* result;
8210 /* Read arguments */
8211 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8212 return NULL;
8213 if (howMany < 0)
8214 return PyErr_Format(PyExc_ValueError,
8215 "negative argument not allowed");
8217 if (hCryptProv == 0) {
8218 HINSTANCE hAdvAPI32 = NULL;
8219 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
8221 /* Obtain handle to the DLL containing CryptoAPI
8222 This should not fail */
8223 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8224 if(hAdvAPI32 == NULL)
8225 return win32_error("GetModuleHandle", NULL);
8227 /* Obtain pointers to the CryptoAPI functions
8228 This will fail on some early versions of Win95 */
8229 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8230 hAdvAPI32,
8231 "CryptAcquireContextA");
8232 if (pCryptAcquireContext == NULL)
8233 return PyErr_Format(PyExc_NotImplementedError,
8234 "CryptAcquireContextA not found");
8236 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8237 hAdvAPI32, "CryptGenRandom");
8238 if (pCryptGenRandom == NULL)
8239 return PyErr_Format(PyExc_NotImplementedError,
8240 "CryptGenRandom not found");
8242 /* Acquire context */
8243 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8244 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8245 return win32_error("CryptAcquireContext", NULL);
8248 /* Allocate bytes */
8249 result = PyString_FromStringAndSize(NULL, howMany);
8250 if (result != NULL) {
8251 /* Get random data */
8252 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
8253 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8254 PyString_AS_STRING(result))) {
8255 Py_DECREF(result);
8256 return win32_error("CryptGenRandom", NULL);
8259 return result;
8261 #endif
8263 #ifdef __VMS
8264 /* Use openssl random routine */
8265 #include <openssl/rand.h>
8266 PyDoc_STRVAR(vms_urandom__doc__,
8267 "urandom(n) -> str\n\n\
8268 Return a string of n random bytes suitable for cryptographic use.");
8270 static PyObject*
8271 vms_urandom(PyObject *self, PyObject *args)
8273 int howMany;
8274 PyObject* result;
8276 /* Read arguments */
8277 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8278 return NULL;
8279 if (howMany < 0)
8280 return PyErr_Format(PyExc_ValueError,
8281 "negative argument not allowed");
8283 /* Allocate bytes */
8284 result = PyString_FromStringAndSize(NULL, howMany);
8285 if (result != NULL) {
8286 /* Get random data */
8287 if (RAND_pseudo_bytes((unsigned char*)
8288 PyString_AS_STRING(result),
8289 howMany) < 0) {
8290 Py_DECREF(result);
8291 return PyErr_Format(PyExc_ValueError,
8292 "RAND_pseudo_bytes");
8295 return result;
8297 #endif
8299 static PyMethodDef posix_methods[] = {
8300 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8301 #ifdef HAVE_TTYNAME
8302 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8303 #endif
8304 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8305 #ifdef HAVE_CHFLAGS
8306 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8307 #endif /* HAVE_CHFLAGS */
8308 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
8309 #ifdef HAVE_FCHMOD
8310 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
8311 #endif /* HAVE_FCHMOD */
8312 #ifdef HAVE_CHOWN
8313 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
8314 #endif /* HAVE_CHOWN */
8315 #ifdef HAVE_LCHMOD
8316 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
8317 #endif /* HAVE_LCHMOD */
8318 #ifdef HAVE_FCHOWN
8319 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
8320 #endif /* HAVE_FCHOWN */
8321 #ifdef HAVE_LCHFLAGS
8322 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8323 #endif /* HAVE_LCHFLAGS */
8324 #ifdef HAVE_LCHOWN
8325 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8326 #endif /* HAVE_LCHOWN */
8327 #ifdef HAVE_CHROOT
8328 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8329 #endif
8330 #ifdef HAVE_CTERMID
8331 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
8332 #endif
8333 #ifdef HAVE_GETCWD
8334 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
8335 #ifdef Py_USING_UNICODE
8336 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
8337 #endif
8338 #endif
8339 #ifdef HAVE_LINK
8340 {"link", posix_link, METH_VARARGS, posix_link__doc__},
8341 #endif /* HAVE_LINK */
8342 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8343 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8344 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
8345 #ifdef HAVE_NICE
8346 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
8347 #endif /* HAVE_NICE */
8348 #ifdef HAVE_READLINK
8349 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
8350 #endif /* HAVE_READLINK */
8351 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8352 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8353 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8354 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
8355 #ifdef HAVE_SYMLINK
8356 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
8357 #endif /* HAVE_SYMLINK */
8358 #ifdef HAVE_SYSTEM
8359 {"system", posix_system, METH_VARARGS, posix_system__doc__},
8360 #endif
8361 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
8362 #ifdef HAVE_UNAME
8363 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
8364 #endif /* HAVE_UNAME */
8365 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8366 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8367 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
8368 #ifdef HAVE_TIMES
8369 {"times", posix_times, METH_NOARGS, posix_times__doc__},
8370 #endif /* HAVE_TIMES */
8371 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
8372 #ifdef HAVE_EXECV
8373 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8374 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
8375 #endif /* HAVE_EXECV */
8376 #ifdef HAVE_SPAWNV
8377 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8378 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
8379 #if defined(PYOS_OS2)
8380 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8381 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8382 #endif /* PYOS_OS2 */
8383 #endif /* HAVE_SPAWNV */
8384 #ifdef HAVE_FORK1
8385 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
8386 #endif /* HAVE_FORK1 */
8387 #ifdef HAVE_FORK
8388 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
8389 #endif /* HAVE_FORK */
8390 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8391 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
8392 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
8393 #ifdef HAVE_FORKPTY
8394 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
8395 #endif /* HAVE_FORKPTY */
8396 #ifdef HAVE_GETEGID
8397 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
8398 #endif /* HAVE_GETEGID */
8399 #ifdef HAVE_GETEUID
8400 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
8401 #endif /* HAVE_GETEUID */
8402 #ifdef HAVE_GETGID
8403 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
8404 #endif /* HAVE_GETGID */
8405 #ifdef HAVE_GETGROUPS
8406 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
8407 #endif
8408 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
8409 #ifdef HAVE_GETPGRP
8410 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
8411 #endif /* HAVE_GETPGRP */
8412 #ifdef HAVE_GETPPID
8413 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
8414 #endif /* HAVE_GETPPID */
8415 #ifdef HAVE_GETUID
8416 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
8417 #endif /* HAVE_GETUID */
8418 #ifdef HAVE_GETLOGIN
8419 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
8420 #endif
8421 #ifdef HAVE_KILL
8422 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
8423 #endif /* HAVE_KILL */
8424 #ifdef HAVE_KILLPG
8425 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8426 #endif /* HAVE_KILLPG */
8427 #ifdef HAVE_PLOCK
8428 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
8429 #endif /* HAVE_PLOCK */
8430 #ifdef HAVE_POPEN
8431 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
8432 #ifdef MS_WINDOWS
8433 {"popen2", win32_popen2, METH_VARARGS},
8434 {"popen3", win32_popen3, METH_VARARGS},
8435 {"popen4", win32_popen4, METH_VARARGS},
8436 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8437 #else
8438 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8439 {"popen2", os2emx_popen2, METH_VARARGS},
8440 {"popen3", os2emx_popen3, METH_VARARGS},
8441 {"popen4", os2emx_popen4, METH_VARARGS},
8442 #endif
8443 #endif
8444 #endif /* HAVE_POPEN */
8445 #ifdef HAVE_SETUID
8446 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
8447 #endif /* HAVE_SETUID */
8448 #ifdef HAVE_SETEUID
8449 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8450 #endif /* HAVE_SETEUID */
8451 #ifdef HAVE_SETEGID
8452 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8453 #endif /* HAVE_SETEGID */
8454 #ifdef HAVE_SETREUID
8455 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8456 #endif /* HAVE_SETREUID */
8457 #ifdef HAVE_SETREGID
8458 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8459 #endif /* HAVE_SETREGID */
8460 #ifdef HAVE_SETGID
8461 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
8462 #endif /* HAVE_SETGID */
8463 #ifdef HAVE_SETGROUPS
8464 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
8465 #endif /* HAVE_SETGROUPS */
8466 #ifdef HAVE_GETPGID
8467 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8468 #endif /* HAVE_GETPGID */
8469 #ifdef HAVE_SETPGRP
8470 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
8471 #endif /* HAVE_SETPGRP */
8472 #ifdef HAVE_WAIT
8473 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
8474 #endif /* HAVE_WAIT */
8475 #ifdef HAVE_WAIT3
8476 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8477 #endif /* HAVE_WAIT3 */
8478 #ifdef HAVE_WAIT4
8479 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8480 #endif /* HAVE_WAIT4 */
8481 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
8482 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
8483 #endif /* HAVE_WAITPID */
8484 #ifdef HAVE_GETSID
8485 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8486 #endif /* HAVE_GETSID */
8487 #ifdef HAVE_SETSID
8488 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
8489 #endif /* HAVE_SETSID */
8490 #ifdef HAVE_SETPGID
8491 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
8492 #endif /* HAVE_SETPGID */
8493 #ifdef HAVE_TCGETPGRP
8494 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
8495 #endif /* HAVE_TCGETPGRP */
8496 #ifdef HAVE_TCSETPGRP
8497 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
8498 #endif /* HAVE_TCSETPGRP */
8499 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8500 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8501 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8502 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8503 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8504 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8505 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8506 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8507 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8508 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8509 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
8510 #ifdef HAVE_PIPE
8511 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
8512 #endif
8513 #ifdef HAVE_MKFIFO
8514 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
8515 #endif
8516 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8517 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8518 #endif
8519 #ifdef HAVE_DEVICE_MACROS
8520 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8521 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8522 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8523 #endif
8524 #ifdef HAVE_FTRUNCATE
8525 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
8526 #endif
8527 #ifdef HAVE_PUTENV
8528 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
8529 #endif
8530 #ifdef HAVE_UNSETENV
8531 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8532 #endif
8533 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
8534 #ifdef HAVE_FCHDIR
8535 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8536 #endif
8537 #ifdef HAVE_FSYNC
8538 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
8539 #endif
8540 #ifdef HAVE_FDATASYNC
8541 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
8542 #endif
8543 #ifdef HAVE_SYS_WAIT_H
8544 #ifdef WCOREDUMP
8545 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8546 #endif /* WCOREDUMP */
8547 #ifdef WIFCONTINUED
8548 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8549 #endif /* WIFCONTINUED */
8550 #ifdef WIFSTOPPED
8551 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
8552 #endif /* WIFSTOPPED */
8553 #ifdef WIFSIGNALED
8554 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
8555 #endif /* WIFSIGNALED */
8556 #ifdef WIFEXITED
8557 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
8558 #endif /* WIFEXITED */
8559 #ifdef WEXITSTATUS
8560 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
8561 #endif /* WEXITSTATUS */
8562 #ifdef WTERMSIG
8563 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
8564 #endif /* WTERMSIG */
8565 #ifdef WSTOPSIG
8566 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
8567 #endif /* WSTOPSIG */
8568 #endif /* HAVE_SYS_WAIT_H */
8569 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
8570 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
8571 #endif
8572 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
8573 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
8574 #endif
8575 #ifdef HAVE_TMPFILE
8576 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
8577 #endif
8578 #ifdef HAVE_TEMPNAM
8579 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8580 #endif
8581 #ifdef HAVE_TMPNAM
8582 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
8583 #endif
8584 #ifdef HAVE_CONFSTR
8585 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8586 #endif
8587 #ifdef HAVE_SYSCONF
8588 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8589 #endif
8590 #ifdef HAVE_FPATHCONF
8591 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8592 #endif
8593 #ifdef HAVE_PATHCONF
8594 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8595 #endif
8596 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
8597 #ifdef MS_WINDOWS
8598 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8599 #endif
8600 #ifdef HAVE_GETLOADAVG
8601 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
8602 #endif
8603 #ifdef MS_WINDOWS
8604 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8605 #endif
8606 #ifdef __VMS
8607 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8608 #endif
8609 {NULL, NULL} /* Sentinel */
8613 static int
8614 ins(PyObject *module, char *symbol, long value)
8616 return PyModule_AddIntConstant(module, symbol, value);
8619 #if defined(PYOS_OS2)
8620 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8621 static int insertvalues(PyObject *module)
8623 APIRET rc;
8624 ULONG values[QSV_MAX+1];
8625 PyObject *v;
8626 char *ver, tmp[50];
8628 Py_BEGIN_ALLOW_THREADS
8629 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
8630 Py_END_ALLOW_THREADS
8632 if (rc != NO_ERROR) {
8633 os2_error(rc);
8634 return -1;
8637 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8638 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8639 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8640 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8641 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8642 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8643 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
8645 switch (values[QSV_VERSION_MINOR]) {
8646 case 0: ver = "2.00"; break;
8647 case 10: ver = "2.10"; break;
8648 case 11: ver = "2.11"; break;
8649 case 30: ver = "3.00"; break;
8650 case 40: ver = "4.00"; break;
8651 case 50: ver = "5.00"; break;
8652 default:
8653 PyOS_snprintf(tmp, sizeof(tmp),
8654 "%d-%d", values[QSV_VERSION_MAJOR],
8655 values[QSV_VERSION_MINOR]);
8656 ver = &tmp[0];
8659 /* Add Indicator of the Version of the Operating System */
8660 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
8661 return -1;
8663 /* Add Indicator of Which Drive was Used to Boot the System */
8664 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8665 tmp[1] = ':';
8666 tmp[2] = '\0';
8668 return PyModule_AddStringConstant(module, "bootdrive", tmp);
8670 #endif
8672 static int
8673 all_ins(PyObject *d)
8675 #ifdef F_OK
8676 if (ins(d, "F_OK", (long)F_OK)) return -1;
8677 #endif
8678 #ifdef R_OK
8679 if (ins(d, "R_OK", (long)R_OK)) return -1;
8680 #endif
8681 #ifdef W_OK
8682 if (ins(d, "W_OK", (long)W_OK)) return -1;
8683 #endif
8684 #ifdef X_OK
8685 if (ins(d, "X_OK", (long)X_OK)) return -1;
8686 #endif
8687 #ifdef NGROUPS_MAX
8688 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8689 #endif
8690 #ifdef TMP_MAX
8691 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8692 #endif
8693 #ifdef WCONTINUED
8694 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8695 #endif
8696 #ifdef WNOHANG
8697 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
8698 #endif
8699 #ifdef WUNTRACED
8700 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8701 #endif
8702 #ifdef O_RDONLY
8703 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8704 #endif
8705 #ifdef O_WRONLY
8706 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8707 #endif
8708 #ifdef O_RDWR
8709 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8710 #endif
8711 #ifdef O_NDELAY
8712 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8713 #endif
8714 #ifdef O_NONBLOCK
8715 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8716 #endif
8717 #ifdef O_APPEND
8718 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8719 #endif
8720 #ifdef O_DSYNC
8721 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8722 #endif
8723 #ifdef O_RSYNC
8724 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8725 #endif
8726 #ifdef O_SYNC
8727 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8728 #endif
8729 #ifdef O_NOCTTY
8730 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8731 #endif
8732 #ifdef O_CREAT
8733 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8734 #endif
8735 #ifdef O_EXCL
8736 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8737 #endif
8738 #ifdef O_TRUNC
8739 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8740 #endif
8741 #ifdef O_BINARY
8742 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8743 #endif
8744 #ifdef O_TEXT
8745 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8746 #endif
8747 #ifdef O_LARGEFILE
8748 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8749 #endif
8750 #ifdef O_SHLOCK
8751 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8752 #endif
8753 #ifdef O_EXLOCK
8754 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8755 #endif
8757 /* MS Windows */
8758 #ifdef O_NOINHERIT
8759 /* Don't inherit in child processes. */
8760 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8761 #endif
8762 #ifdef _O_SHORT_LIVED
8763 /* Optimize for short life (keep in memory). */
8764 /* MS forgot to define this one with a non-underscore form too. */
8765 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8766 #endif
8767 #ifdef O_TEMPORARY
8768 /* Automatically delete when last handle is closed. */
8769 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8770 #endif
8771 #ifdef O_RANDOM
8772 /* Optimize for random access. */
8773 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8774 #endif
8775 #ifdef O_SEQUENTIAL
8776 /* Optimize for sequential access. */
8777 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8778 #endif
8780 /* GNU extensions. */
8781 #ifdef O_ASYNC
8782 /* Send a SIGIO signal whenever input or output
8783 becomes available on file descriptor */
8784 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
8785 #endif
8786 #ifdef O_DIRECT
8787 /* Direct disk access. */
8788 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8789 #endif
8790 #ifdef O_DIRECTORY
8791 /* Must be a directory. */
8792 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8793 #endif
8794 #ifdef O_NOFOLLOW
8795 /* Do not follow links. */
8796 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8797 #endif
8798 #ifdef O_NOATIME
8799 /* Do not update the access time. */
8800 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8801 #endif
8803 /* These come from sysexits.h */
8804 #ifdef EX_OK
8805 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
8806 #endif /* EX_OK */
8807 #ifdef EX_USAGE
8808 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
8809 #endif /* EX_USAGE */
8810 #ifdef EX_DATAERR
8811 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
8812 #endif /* EX_DATAERR */
8813 #ifdef EX_NOINPUT
8814 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
8815 #endif /* EX_NOINPUT */
8816 #ifdef EX_NOUSER
8817 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
8818 #endif /* EX_NOUSER */
8819 #ifdef EX_NOHOST
8820 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
8821 #endif /* EX_NOHOST */
8822 #ifdef EX_UNAVAILABLE
8823 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
8824 #endif /* EX_UNAVAILABLE */
8825 #ifdef EX_SOFTWARE
8826 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
8827 #endif /* EX_SOFTWARE */
8828 #ifdef EX_OSERR
8829 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
8830 #endif /* EX_OSERR */
8831 #ifdef EX_OSFILE
8832 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
8833 #endif /* EX_OSFILE */
8834 #ifdef EX_CANTCREAT
8835 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
8836 #endif /* EX_CANTCREAT */
8837 #ifdef EX_IOERR
8838 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
8839 #endif /* EX_IOERR */
8840 #ifdef EX_TEMPFAIL
8841 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
8842 #endif /* EX_TEMPFAIL */
8843 #ifdef EX_PROTOCOL
8844 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
8845 #endif /* EX_PROTOCOL */
8846 #ifdef EX_NOPERM
8847 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
8848 #endif /* EX_NOPERM */
8849 #ifdef EX_CONFIG
8850 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
8851 #endif /* EX_CONFIG */
8852 #ifdef EX_NOTFOUND
8853 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
8854 #endif /* EX_NOTFOUND */
8856 #ifdef HAVE_SPAWNV
8857 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8858 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8859 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8860 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8861 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8862 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8863 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8864 if (ins(d, "P_PM", (long)P_PM)) return -1;
8865 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8866 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8867 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8868 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8869 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8870 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8871 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8872 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8873 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8874 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8875 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8876 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8877 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8878 #else
8879 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8880 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8881 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8882 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8883 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
8884 #endif
8885 #endif
8887 #if defined(PYOS_OS2)
8888 if (insertvalues(d)) return -1;
8889 #endif
8890 return 0;
8894 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
8895 #define INITFUNC initnt
8896 #define MODNAME "nt"
8898 #elif defined(PYOS_OS2)
8899 #define INITFUNC initos2
8900 #define MODNAME "os2"
8902 #else
8903 #define INITFUNC initposix
8904 #define MODNAME "posix"
8905 #endif
8907 PyMODINIT_FUNC
8908 INITFUNC(void)
8910 PyObject *m, *v;
8912 m = Py_InitModule3(MODNAME,
8913 posix_methods,
8914 posix__doc__);
8915 if (m == NULL)
8916 return;
8918 /* Initialize environ dictionary */
8919 v = convertenviron();
8920 Py_XINCREF(v);
8921 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8922 return;
8923 Py_DECREF(v);
8925 if (all_ins(m))
8926 return;
8928 if (setup_confname_tables(m))
8929 return;
8931 Py_INCREF(PyExc_OSError);
8932 PyModule_AddObject(m, "error", PyExc_OSError);
8934 #ifdef HAVE_PUTENV
8935 if (posix_putenv_garbage == NULL)
8936 posix_putenv_garbage = PyDict_New();
8937 #endif
8939 if (!initialized) {
8940 stat_result_desc.name = MODNAME ".stat_result";
8941 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8942 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8943 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8944 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8945 structseq_new = StatResultType.tp_new;
8946 StatResultType.tp_new = statresult_new;
8948 statvfs_result_desc.name = MODNAME ".statvfs_result";
8949 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8951 Py_INCREF((PyObject*) &StatResultType);
8952 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8953 Py_INCREF((PyObject*) &StatVFSResultType);
8954 PyModule_AddObject(m, "statvfs_result",
8955 (PyObject*) &StatVFSResultType);
8956 initialized = 1;
8958 #ifdef __APPLE__
8960 * Step 2 of weak-linking support on Mac OS X.
8962 * The code below removes functions that are not available on the
8963 * currently active platform.
8965 * This block allow one to use a python binary that was build on
8966 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8967 * OSX 10.4.
8969 #ifdef HAVE_FSTATVFS
8970 if (fstatvfs == NULL) {
8971 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8972 return;
8975 #endif /* HAVE_FSTATVFS */
8977 #ifdef HAVE_STATVFS
8978 if (statvfs == NULL) {
8979 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8980 return;
8983 #endif /* HAVE_STATVFS */
8985 # ifdef HAVE_LCHOWN
8986 if (lchown == NULL) {
8987 if (PyObject_DelAttrString(m, "lchown") == -1) {
8988 return;
8991 #endif /* HAVE_LCHOWN */
8994 #endif /* __APPLE__ */
8998 #ifdef __cplusplus
9000 #endif