Adding basic imputil documentation.
[python.git] / Modules / posixmodule.c
blob24c9e15537d8164826271efce54f7cf998776224
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 extern int chown(const char *, uid_t, gid_t);
195 extern char *getcwd(char *, int);
196 extern char *strerror(int);
197 extern int link(const char *, const char *);
198 extern int rename(const char *, const char *);
199 extern int stat(const char *, struct stat *);
200 extern int unlink(const char *);
201 extern int pclose(FILE *);
202 #ifdef HAVE_SYMLINK
203 extern int symlink(const char *, const char *);
204 #endif /* HAVE_SYMLINK */
205 #ifdef HAVE_LSTAT
206 extern int lstat(const char *, struct stat *);
207 #endif /* HAVE_LSTAT */
208 #endif /* !HAVE_UNISTD_H */
210 #endif /* !_MSC_VER */
212 #ifdef HAVE_UTIME_H
213 #include <utime.h>
214 #endif /* HAVE_UTIME_H */
216 #ifdef HAVE_SYS_UTIME_H
217 #include <sys/utime.h>
218 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
219 #endif /* HAVE_SYS_UTIME_H */
221 #ifdef HAVE_SYS_TIMES_H
222 #include <sys/times.h>
223 #endif /* HAVE_SYS_TIMES_H */
225 #ifdef HAVE_SYS_PARAM_H
226 #include <sys/param.h>
227 #endif /* HAVE_SYS_PARAM_H */
229 #ifdef HAVE_SYS_UTSNAME_H
230 #include <sys/utsname.h>
231 #endif /* HAVE_SYS_UTSNAME_H */
233 #ifdef HAVE_DIRENT_H
234 #include <dirent.h>
235 #define NAMLEN(dirent) strlen((dirent)->d_name)
236 #else
237 #if defined(__WATCOMC__) && !defined(__QNX__)
238 #include <direct.h>
239 #define NAMLEN(dirent) strlen((dirent)->d_name)
240 #else
241 #define dirent direct
242 #define NAMLEN(dirent) (dirent)->d_namlen
243 #endif
244 #ifdef HAVE_SYS_NDIR_H
245 #include <sys/ndir.h>
246 #endif
247 #ifdef HAVE_SYS_DIR_H
248 #include <sys/dir.h>
249 #endif
250 #ifdef HAVE_NDIR_H
251 #include <ndir.h>
252 #endif
253 #endif
255 #ifdef _MSC_VER
256 #ifdef HAVE_DIRECT_H
257 #include <direct.h>
258 #endif
259 #ifdef HAVE_IO_H
260 #include <io.h>
261 #endif
262 #ifdef HAVE_PROCESS_H
263 #include <process.h>
264 #endif
265 #include "osdefs.h"
266 #include <windows.h>
267 #include <shellapi.h> /* for ShellExecute() */
268 #define popen _popen
269 #define pclose _pclose
270 #endif /* _MSC_VER */
272 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
273 #include <io.h>
274 #endif /* OS2 */
276 #ifndef MAXPATHLEN
277 #if defined(PATH_MAX) && PATH_MAX > 1024
278 #define MAXPATHLEN PATH_MAX
279 #else
280 #define MAXPATHLEN 1024
281 #endif
282 #endif /* MAXPATHLEN */
284 #ifdef UNION_WAIT
285 /* Emulate some macros on systems that have a union instead of macros */
287 #ifndef WIFEXITED
288 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
289 #endif
291 #ifndef WEXITSTATUS
292 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
293 #endif
295 #ifndef WTERMSIG
296 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
297 #endif
299 #define WAIT_TYPE union wait
300 #define WAIT_STATUS_INT(s) (s.w_status)
302 #else /* !UNION_WAIT */
303 #define WAIT_TYPE int
304 #define WAIT_STATUS_INT(s) (s)
305 #endif /* UNION_WAIT */
307 /* Don't use the "_r" form if we don't need it (also, won't have a
308 prototype for it, at least on Solaris -- maybe others as well?). */
309 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
310 #define USE_CTERMID_R
311 #endif
313 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
314 #define USE_TMPNAM_R
315 #endif
317 /* choose the appropriate stat and fstat functions and return structs */
318 #undef STAT
319 #if defined(MS_WIN64) || defined(MS_WINDOWS)
320 # define STAT win32_stat
321 # define FSTAT win32_fstat
322 # define STRUCT_STAT struct win32_stat
323 #else
324 # define STAT stat
325 # define FSTAT fstat
326 # define STRUCT_STAT struct stat
327 #endif
329 #if defined(MAJOR_IN_MKDEV)
330 #include <sys/mkdev.h>
331 #else
332 #if defined(MAJOR_IN_SYSMACROS)
333 #include <sys/sysmacros.h>
334 #endif
335 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
336 #include <sys/mkdev.h>
337 #endif
338 #endif
340 /* Return a dictionary corresponding to the POSIX environment table */
341 #ifdef WITH_NEXT_FRAMEWORK
342 /* On Darwin/MacOSX a shared library or framework has no access to
343 ** environ directly, we must obtain it with _NSGetEnviron().
345 #include <crt_externs.h>
346 static char **environ;
347 #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
348 extern char **environ;
349 #endif /* !_MSC_VER */
351 static PyObject *
352 convertenviron(void)
354 PyObject *d;
355 char **e;
356 d = PyDict_New();
357 if (d == NULL)
358 return NULL;
359 #ifdef WITH_NEXT_FRAMEWORK
360 if (environ == NULL)
361 environ = *_NSGetEnviron();
362 #endif
363 if (environ == NULL)
364 return d;
365 /* This part ignores errors */
366 for (e = environ; *e != NULL; e++) {
367 PyObject *k;
368 PyObject *v;
369 char *p = strchr(*e, '=');
370 if (p == NULL)
371 continue;
372 k = PyString_FromStringAndSize(*e, (int)(p-*e));
373 if (k == NULL) {
374 PyErr_Clear();
375 continue;
377 v = PyString_FromString(p+1);
378 if (v == NULL) {
379 PyErr_Clear();
380 Py_DECREF(k);
381 continue;
383 if (PyDict_GetItem(d, k) == NULL) {
384 if (PyDict_SetItem(d, k, v) != 0)
385 PyErr_Clear();
387 Py_DECREF(k);
388 Py_DECREF(v);
390 #if defined(PYOS_OS2)
392 APIRET rc;
393 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
395 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
396 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
397 PyObject *v = PyString_FromString(buffer);
398 PyDict_SetItemString(d, "BEGINLIBPATH", v);
399 Py_DECREF(v);
401 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
402 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
403 PyObject *v = PyString_FromString(buffer);
404 PyDict_SetItemString(d, "ENDLIBPATH", v);
405 Py_DECREF(v);
408 #endif
409 return d;
413 /* Set a POSIX-specific error from errno, and return NULL */
415 static PyObject *
416 posix_error(void)
418 return PyErr_SetFromErrno(PyExc_OSError);
420 static PyObject *
421 posix_error_with_filename(char* name)
423 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
426 #ifdef Py_WIN_WIDE_FILENAMES
427 static PyObject *
428 posix_error_with_unicode_filename(Py_UNICODE* name)
430 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
432 #endif /* Py_WIN_WIDE_FILENAMES */
435 static PyObject *
436 posix_error_with_allocated_filename(char* name)
438 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
439 PyMem_Free(name);
440 return rc;
443 #ifdef MS_WINDOWS
444 static PyObject *
445 win32_error(char* function, char* filename)
447 /* XXX We should pass the function name along in the future.
448 (_winreg.c also wants to pass the function name.)
449 This would however require an additional param to the
450 Windows error object, which is non-trivial.
452 errno = GetLastError();
453 if (filename)
454 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
455 else
456 return PyErr_SetFromWindowsErr(errno);
459 #ifdef Py_WIN_WIDE_FILENAMES
460 static PyObject *
461 win32_error_unicode(char* function, Py_UNICODE* filename)
463 /* XXX - see win32_error for comments on 'function' */
464 errno = GetLastError();
465 if (filename)
466 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
467 else
468 return PyErr_SetFromWindowsErr(errno);
471 static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
475 /* Function suitable for O& conversion */
476 static int
477 convert_to_unicode(PyObject *arg, void* _param)
479 PyObject **param = (PyObject**)_param;
480 if (PyUnicode_CheckExact(arg)) {
481 Py_INCREF(arg);
482 *param = arg;
484 else if (PyUnicode_Check(arg)) {
485 /* For a Unicode subtype that's not a Unicode object,
486 return a true Unicode object with the same data. */
487 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
488 PyUnicode_GET_SIZE(arg));
489 return *param != NULL;
491 else
492 *param = PyUnicode_FromEncodedObject(arg,
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. */
1538 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
1539 #else
1540 int res;
1541 if (!PyArg_ParseTuple(args, "eti:access",
1542 Py_FileSystemDefaultEncoding, &path, &mode))
1543 return NULL;
1544 Py_BEGIN_ALLOW_THREADS
1545 res = access(path, mode);
1546 Py_END_ALLOW_THREADS
1547 PyMem_Free(path);
1548 return PyBool_FromLong(res == 0);
1549 #endif
1552 #ifndef F_OK
1553 #define F_OK 0
1554 #endif
1555 #ifndef R_OK
1556 #define R_OK 4
1557 #endif
1558 #ifndef W_OK
1559 #define W_OK 2
1560 #endif
1561 #ifndef X_OK
1562 #define X_OK 1
1563 #endif
1565 #ifdef HAVE_TTYNAME
1566 PyDoc_STRVAR(posix_ttyname__doc__,
1567 "ttyname(fd) -> string\n\n\
1568 Return the name of the terminal device connected to 'fd'.");
1570 static PyObject *
1571 posix_ttyname(PyObject *self, PyObject *args)
1573 int id;
1574 char *ret;
1576 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1577 return NULL;
1579 #if defined(__VMS)
1580 /* file descriptor 0 only, the default input device (stdin) */
1581 if (id == 0) {
1582 ret = ttyname();
1584 else {
1585 ret = NULL;
1587 #else
1588 ret = ttyname(id);
1589 #endif
1590 if (ret == NULL)
1591 return posix_error();
1592 return PyString_FromString(ret);
1594 #endif
1596 #ifdef HAVE_CTERMID
1597 PyDoc_STRVAR(posix_ctermid__doc__,
1598 "ctermid() -> string\n\n\
1599 Return the name of the controlling terminal for this process.");
1601 static PyObject *
1602 posix_ctermid(PyObject *self, PyObject *noargs)
1604 char *ret;
1605 char buffer[L_ctermid];
1607 #ifdef USE_CTERMID_R
1608 ret = ctermid_r(buffer);
1609 #else
1610 ret = ctermid(buffer);
1611 #endif
1612 if (ret == NULL)
1613 return posix_error();
1614 return PyString_FromString(buffer);
1616 #endif
1618 PyDoc_STRVAR(posix_chdir__doc__,
1619 "chdir(path)\n\n\
1620 Change the current working directory to the specified path.");
1622 static PyObject *
1623 posix_chdir(PyObject *self, PyObject *args)
1625 #ifdef MS_WINDOWS
1626 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
1627 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
1628 return posix_1str(args, "et:chdir", _chdir2);
1629 #elif defined(__VMS)
1630 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
1631 #else
1632 return posix_1str(args, "et:chdir", chdir);
1633 #endif
1636 #ifdef HAVE_FCHDIR
1637 PyDoc_STRVAR(posix_fchdir__doc__,
1638 "fchdir(fildes)\n\n\
1639 Change to the directory of the given file descriptor. fildes must be\n\
1640 opened on a directory, not a file.");
1642 static PyObject *
1643 posix_fchdir(PyObject *self, PyObject *fdobj)
1645 return posix_fildes(fdobj, fchdir);
1647 #endif /* HAVE_FCHDIR */
1650 PyDoc_STRVAR(posix_chmod__doc__,
1651 "chmod(path, mode)\n\n\
1652 Change the access permissions of a file.");
1654 static PyObject *
1655 posix_chmod(PyObject *self, PyObject *args)
1657 char *path = NULL;
1658 int i;
1659 int res;
1660 #ifdef Py_WIN_WIDE_FILENAMES
1661 DWORD attr;
1662 if (unicode_file_names()) {
1663 PyUnicodeObject *po;
1664 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1665 Py_BEGIN_ALLOW_THREADS
1666 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1667 if (attr != 0xFFFFFFFF) {
1668 if (i & _S_IWRITE)
1669 attr &= ~FILE_ATTRIBUTE_READONLY;
1670 else
1671 attr |= FILE_ATTRIBUTE_READONLY;
1672 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1674 else
1675 res = 0;
1676 Py_END_ALLOW_THREADS
1677 if (!res)
1678 return win32_error_unicode("chmod",
1679 PyUnicode_AS_UNICODE(po));
1680 Py_INCREF(Py_None);
1681 return Py_None;
1683 /* Drop the argument parsing error as narrow strings
1684 are also valid. */
1685 PyErr_Clear();
1687 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1688 &path, &i))
1689 return NULL;
1690 Py_BEGIN_ALLOW_THREADS
1691 attr = GetFileAttributesA(path);
1692 if (attr != 0xFFFFFFFF) {
1693 if (i & _S_IWRITE)
1694 attr &= ~FILE_ATTRIBUTE_READONLY;
1695 else
1696 attr |= FILE_ATTRIBUTE_READONLY;
1697 res = SetFileAttributesA(path, attr);
1699 else
1700 res = 0;
1701 Py_END_ALLOW_THREADS
1702 if (!res) {
1703 win32_error("chmod", path);
1704 PyMem_Free(path);
1705 return NULL;
1707 PyMem_Free(path);
1708 Py_INCREF(Py_None);
1709 return Py_None;
1710 #else /* Py_WIN_WIDE_FILENAMES */
1711 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1712 &path, &i))
1713 return NULL;
1714 Py_BEGIN_ALLOW_THREADS
1715 res = chmod(path, i);
1716 Py_END_ALLOW_THREADS
1717 if (res < 0)
1718 return posix_error_with_allocated_filename(path);
1719 PyMem_Free(path);
1720 Py_INCREF(Py_None);
1721 return Py_None;
1722 #endif
1726 #ifdef HAVE_CHFLAGS
1727 PyDoc_STRVAR(posix_chflags__doc__,
1728 "chflags(path, flags)\n\n\
1729 Set file flags.");
1731 static PyObject *
1732 posix_chflags(PyObject *self, PyObject *args)
1734 char *path;
1735 unsigned long flags;
1736 int res;
1737 if (!PyArg_ParseTuple(args, "etk:chflags",
1738 Py_FileSystemDefaultEncoding, &path, &flags))
1739 return NULL;
1740 Py_BEGIN_ALLOW_THREADS
1741 res = chflags(path, flags);
1742 Py_END_ALLOW_THREADS
1743 if (res < 0)
1744 return posix_error_with_allocated_filename(path);
1745 PyMem_Free(path);
1746 Py_INCREF(Py_None);
1747 return Py_None;
1749 #endif /* HAVE_CHFLAGS */
1751 #ifdef HAVE_LCHFLAGS
1752 PyDoc_STRVAR(posix_lchflags__doc__,
1753 "lchflags(path, flags)\n\n\
1754 Set file flags.\n\
1755 This function will not follow symbolic links.");
1757 static PyObject *
1758 posix_lchflags(PyObject *self, PyObject *args)
1760 char *path;
1761 unsigned long flags;
1762 int res;
1763 if (!PyArg_ParseTuple(args, "etk:lchflags",
1764 Py_FileSystemDefaultEncoding, &path, &flags))
1765 return NULL;
1766 Py_BEGIN_ALLOW_THREADS
1767 res = lchflags(path, flags);
1768 Py_END_ALLOW_THREADS
1769 if (res < 0)
1770 return posix_error_with_allocated_filename(path);
1771 PyMem_Free(path);
1772 Py_INCREF(Py_None);
1773 return Py_None;
1775 #endif /* HAVE_LCHFLAGS */
1777 #ifdef HAVE_CHROOT
1778 PyDoc_STRVAR(posix_chroot__doc__,
1779 "chroot(path)\n\n\
1780 Change root directory to path.");
1782 static PyObject *
1783 posix_chroot(PyObject *self, PyObject *args)
1785 return posix_1str(args, "et:chroot", chroot);
1787 #endif
1789 #ifdef HAVE_FSYNC
1790 PyDoc_STRVAR(posix_fsync__doc__,
1791 "fsync(fildes)\n\n\
1792 force write of file with filedescriptor to disk.");
1794 static PyObject *
1795 posix_fsync(PyObject *self, PyObject *fdobj)
1797 return posix_fildes(fdobj, fsync);
1799 #endif /* HAVE_FSYNC */
1801 #ifdef HAVE_FDATASYNC
1803 #ifdef __hpux
1804 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1805 #endif
1807 PyDoc_STRVAR(posix_fdatasync__doc__,
1808 "fdatasync(fildes)\n\n\
1809 force write of file with filedescriptor to disk.\n\
1810 does not force update of metadata.");
1812 static PyObject *
1813 posix_fdatasync(PyObject *self, PyObject *fdobj)
1815 return posix_fildes(fdobj, fdatasync);
1817 #endif /* HAVE_FDATASYNC */
1820 #ifdef HAVE_CHOWN
1821 PyDoc_STRVAR(posix_chown__doc__,
1822 "chown(path, uid, gid)\n\n\
1823 Change the owner and group id of path to the numeric uid and gid.");
1825 static PyObject *
1826 posix_chown(PyObject *self, PyObject *args)
1828 char *path = NULL;
1829 int uid, gid;
1830 int res;
1831 if (!PyArg_ParseTuple(args, "etii:chown",
1832 Py_FileSystemDefaultEncoding, &path,
1833 &uid, &gid))
1834 return NULL;
1835 Py_BEGIN_ALLOW_THREADS
1836 res = chown(path, (uid_t) uid, (gid_t) gid);
1837 Py_END_ALLOW_THREADS
1838 if (res < 0)
1839 return posix_error_with_allocated_filename(path);
1840 PyMem_Free(path);
1841 Py_INCREF(Py_None);
1842 return Py_None;
1844 #endif /* HAVE_CHOWN */
1846 #ifdef HAVE_LCHOWN
1847 PyDoc_STRVAR(posix_lchown__doc__,
1848 "lchown(path, uid, gid)\n\n\
1849 Change the owner and group id of path to the numeric uid and gid.\n\
1850 This function will not follow symbolic links.");
1852 static PyObject *
1853 posix_lchown(PyObject *self, PyObject *args)
1855 char *path = NULL;
1856 int uid, gid;
1857 int res;
1858 if (!PyArg_ParseTuple(args, "etii:lchown",
1859 Py_FileSystemDefaultEncoding, &path,
1860 &uid, &gid))
1861 return NULL;
1862 Py_BEGIN_ALLOW_THREADS
1863 res = lchown(path, (uid_t) uid, (gid_t) gid);
1864 Py_END_ALLOW_THREADS
1865 if (res < 0)
1866 return posix_error_with_allocated_filename(path);
1867 PyMem_Free(path);
1868 Py_INCREF(Py_None);
1869 return Py_None;
1871 #endif /* HAVE_LCHOWN */
1874 #ifdef HAVE_GETCWD
1875 PyDoc_STRVAR(posix_getcwd__doc__,
1876 "getcwd() -> path\n\n\
1877 Return a string representing the current working directory.");
1879 static PyObject *
1880 posix_getcwd(PyObject *self, PyObject *noargs)
1882 char buf[1026];
1883 char *res;
1885 Py_BEGIN_ALLOW_THREADS
1886 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1887 res = _getcwd2(buf, sizeof buf);
1888 #else
1889 res = getcwd(buf, sizeof buf);
1890 #endif
1891 Py_END_ALLOW_THREADS
1892 if (res == NULL)
1893 return posix_error();
1894 return PyString_FromString(buf);
1897 #ifdef Py_USING_UNICODE
1898 PyDoc_STRVAR(posix_getcwdu__doc__,
1899 "getcwdu() -> path\n\n\
1900 Return a unicode string representing the current working directory.");
1902 static PyObject *
1903 posix_getcwdu(PyObject *self, PyObject *noargs)
1905 char buf[1026];
1906 char *res;
1908 #ifdef Py_WIN_WIDE_FILENAMES
1909 DWORD len;
1910 if (unicode_file_names()) {
1911 wchar_t wbuf[1026];
1912 wchar_t *wbuf2 = wbuf;
1913 PyObject *resobj;
1914 Py_BEGIN_ALLOW_THREADS
1915 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1916 /* If the buffer is large enough, len does not include the
1917 terminating \0. If the buffer is too small, len includes
1918 the space needed for the terminator. */
1919 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1920 wbuf2 = malloc(len * sizeof(wchar_t));
1921 if (wbuf2)
1922 len = GetCurrentDirectoryW(len, wbuf2);
1924 Py_END_ALLOW_THREADS
1925 if (!wbuf2) {
1926 PyErr_NoMemory();
1927 return NULL;
1929 if (!len) {
1930 if (wbuf2 != wbuf) free(wbuf2);
1931 return win32_error("getcwdu", NULL);
1933 resobj = PyUnicode_FromWideChar(wbuf2, len);
1934 if (wbuf2 != wbuf) free(wbuf2);
1935 return resobj;
1937 #endif
1939 Py_BEGIN_ALLOW_THREADS
1940 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1941 res = _getcwd2(buf, sizeof buf);
1942 #else
1943 res = getcwd(buf, sizeof buf);
1944 #endif
1945 Py_END_ALLOW_THREADS
1946 if (res == NULL)
1947 return posix_error();
1948 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1950 #endif
1951 #endif
1954 #ifdef HAVE_LINK
1955 PyDoc_STRVAR(posix_link__doc__,
1956 "link(src, dst)\n\n\
1957 Create a hard link to a file.");
1959 static PyObject *
1960 posix_link(PyObject *self, PyObject *args)
1962 return posix_2str(args, "etet:link", link);
1964 #endif /* HAVE_LINK */
1967 PyDoc_STRVAR(posix_listdir__doc__,
1968 "listdir(path) -> list_of_strings\n\n\
1969 Return a list containing the names of the entries in the directory.\n\
1971 path: path of directory to list\n\
1973 The list is in arbitrary order. It does not include the special\n\
1974 entries '.' and '..' even if they are present in the directory.");
1976 static PyObject *
1977 posix_listdir(PyObject *self, PyObject *args)
1979 /* XXX Should redo this putting the (now four) versions of opendir
1980 in separate files instead of having them all here... */
1981 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
1983 PyObject *d, *v;
1984 HANDLE hFindFile;
1985 BOOL result;
1986 WIN32_FIND_DATA FileData;
1987 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
1988 char *bufptr = namebuf;
1989 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
1991 #ifdef Py_WIN_WIDE_FILENAMES
1992 /* If on wide-character-capable OS see if argument
1993 is Unicode and if so use wide API. */
1994 if (unicode_file_names()) {
1995 PyObject *po;
1996 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1997 WIN32_FIND_DATAW wFileData;
1998 Py_UNICODE *wnamebuf;
1999 Py_UNICODE wch;
2000 /* Overallocate for \\*.*\0 */
2001 len = PyUnicode_GET_SIZE(po);
2002 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2003 if (!wnamebuf) {
2004 PyErr_NoMemory();
2005 return NULL;
2007 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2008 wch = len > 0 ? wnamebuf[len-1] : '\0';
2009 if (wch != L'/' && wch != L'\\' && wch != L':')
2010 wnamebuf[len++] = L'\\';
2011 wcscpy(wnamebuf + len, L"*.*");
2012 if ((d = PyList_New(0)) == NULL) {
2013 free(wnamebuf);
2014 return NULL;
2016 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2017 if (hFindFile == INVALID_HANDLE_VALUE) {
2018 int error = GetLastError();
2019 if (error == ERROR_FILE_NOT_FOUND) {
2020 free(wnamebuf);
2021 return d;
2023 Py_DECREF(d);
2024 win32_error_unicode("FindFirstFileW", wnamebuf);
2025 free(wnamebuf);
2026 return NULL;
2028 do {
2029 /* Skip over . and .. */
2030 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2031 wcscmp(wFileData.cFileName, L"..") != 0) {
2032 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2033 if (v == NULL) {
2034 Py_DECREF(d);
2035 d = NULL;
2036 break;
2038 if (PyList_Append(d, v) != 0) {
2039 Py_DECREF(v);
2040 Py_DECREF(d);
2041 d = NULL;
2042 break;
2044 Py_DECREF(v);
2046 Py_BEGIN_ALLOW_THREADS
2047 result = FindNextFileW(hFindFile, &wFileData);
2048 Py_END_ALLOW_THREADS
2049 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2050 it got to the end of the directory. */
2051 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2052 Py_DECREF(d);
2053 win32_error_unicode("FindNextFileW", wnamebuf);
2054 FindClose(hFindFile);
2055 free(wnamebuf);
2056 return NULL;
2058 } while (result == TRUE);
2060 if (FindClose(hFindFile) == FALSE) {
2061 Py_DECREF(d);
2062 win32_error_unicode("FindClose", wnamebuf);
2063 free(wnamebuf);
2064 return NULL;
2066 free(wnamebuf);
2067 return d;
2069 /* Drop the argument parsing error as narrow strings
2070 are also valid. */
2071 PyErr_Clear();
2073 #endif
2075 if (!PyArg_ParseTuple(args, "et#:listdir",
2076 Py_FileSystemDefaultEncoding, &bufptr, &len))
2077 return NULL;
2078 if (len > 0) {
2079 char ch = namebuf[len-1];
2080 if (ch != SEP && ch != ALTSEP && ch != ':')
2081 namebuf[len++] = '/';
2083 strcpy(namebuf + len, "*.*");
2085 if ((d = PyList_New(0)) == NULL)
2086 return NULL;
2088 hFindFile = FindFirstFile(namebuf, &FileData);
2089 if (hFindFile == INVALID_HANDLE_VALUE) {
2090 int error = GetLastError();
2091 if (error == ERROR_FILE_NOT_FOUND)
2092 return d;
2093 Py_DECREF(d);
2094 return win32_error("FindFirstFile", namebuf);
2096 do {
2097 /* Skip over . and .. */
2098 if (strcmp(FileData.cFileName, ".") != 0 &&
2099 strcmp(FileData.cFileName, "..") != 0) {
2100 v = PyString_FromString(FileData.cFileName);
2101 if (v == NULL) {
2102 Py_DECREF(d);
2103 d = NULL;
2104 break;
2106 if (PyList_Append(d, v) != 0) {
2107 Py_DECREF(v);
2108 Py_DECREF(d);
2109 d = NULL;
2110 break;
2112 Py_DECREF(v);
2114 Py_BEGIN_ALLOW_THREADS
2115 result = FindNextFile(hFindFile, &FileData);
2116 Py_END_ALLOW_THREADS
2117 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2118 it got to the end of the directory. */
2119 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2120 Py_DECREF(d);
2121 win32_error("FindNextFile", namebuf);
2122 FindClose(hFindFile);
2123 return NULL;
2125 } while (result == TRUE);
2127 if (FindClose(hFindFile) == FALSE) {
2128 Py_DECREF(d);
2129 return win32_error("FindClose", namebuf);
2132 return d;
2134 #elif defined(PYOS_OS2)
2136 #ifndef MAX_PATH
2137 #define MAX_PATH CCHMAXPATH
2138 #endif
2139 char *name, *pt;
2140 Py_ssize_t len;
2141 PyObject *d, *v;
2142 char namebuf[MAX_PATH+5];
2143 HDIR hdir = 1;
2144 ULONG srchcnt = 1;
2145 FILEFINDBUF3 ep;
2146 APIRET rc;
2148 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
2149 return NULL;
2150 if (len >= MAX_PATH) {
2151 PyErr_SetString(PyExc_ValueError, "path too long");
2152 return NULL;
2154 strcpy(namebuf, name);
2155 for (pt = namebuf; *pt; pt++)
2156 if (*pt == ALTSEP)
2157 *pt = SEP;
2158 if (namebuf[len-1] != SEP)
2159 namebuf[len++] = SEP;
2160 strcpy(namebuf + len, "*.*");
2162 if ((d = PyList_New(0)) == NULL)
2163 return NULL;
2165 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2166 &hdir, /* Handle to Use While Search Directory */
2167 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2168 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2169 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2170 FIL_STANDARD); /* Format of Entry (EAs or Not) */
2172 if (rc != NO_ERROR) {
2173 errno = ENOENT;
2174 return posix_error_with_filename(name);
2177 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
2178 do {
2179 if (ep.achName[0] == '.'
2180 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
2181 continue; /* Skip Over "." and ".." Names */
2183 strcpy(namebuf, ep.achName);
2185 /* Leave Case of Name Alone -- In Native Form */
2186 /* (Removed Forced Lowercasing Code) */
2188 v = PyString_FromString(namebuf);
2189 if (v == NULL) {
2190 Py_DECREF(d);
2191 d = NULL;
2192 break;
2194 if (PyList_Append(d, v) != 0) {
2195 Py_DECREF(v);
2196 Py_DECREF(d);
2197 d = NULL;
2198 break;
2200 Py_DECREF(v);
2201 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2204 return d;
2205 #else
2207 char *name = NULL;
2208 PyObject *d, *v;
2209 DIR *dirp;
2210 struct dirent *ep;
2211 int arg_is_unicode = 1;
2213 errno = 0;
2214 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2215 arg_is_unicode = 0;
2216 PyErr_Clear();
2218 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2219 return NULL;
2220 if ((dirp = opendir(name)) == NULL) {
2221 return posix_error_with_allocated_filename(name);
2223 if ((d = PyList_New(0)) == NULL) {
2224 closedir(dirp);
2225 PyMem_Free(name);
2226 return NULL;
2228 for (;;) {
2229 Py_BEGIN_ALLOW_THREADS
2230 ep = readdir(dirp);
2231 Py_END_ALLOW_THREADS
2232 if (ep == NULL)
2233 break;
2234 if (ep->d_name[0] == '.' &&
2235 (NAMLEN(ep) == 1 ||
2236 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2237 continue;
2238 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
2239 if (v == NULL) {
2240 Py_DECREF(d);
2241 d = NULL;
2242 break;
2244 #ifdef Py_USING_UNICODE
2245 if (arg_is_unicode) {
2246 PyObject *w;
2248 w = PyUnicode_FromEncodedObject(v,
2249 Py_FileSystemDefaultEncoding,
2250 "strict");
2251 if (w != NULL) {
2252 Py_DECREF(v);
2253 v = w;
2255 else {
2256 /* fall back to the original byte string, as
2257 discussed in patch #683592 */
2258 PyErr_Clear();
2261 #endif
2262 if (PyList_Append(d, v) != 0) {
2263 Py_DECREF(v);
2264 Py_DECREF(d);
2265 d = NULL;
2266 break;
2268 Py_DECREF(v);
2270 if (errno != 0 && d != NULL) {
2271 /* readdir() returned NULL and set errno */
2272 closedir(dirp);
2273 Py_DECREF(d);
2274 return posix_error_with_allocated_filename(name);
2276 closedir(dirp);
2277 PyMem_Free(name);
2279 return d;
2281 #endif /* which OS */
2282 } /* end of posix_listdir */
2284 #ifdef MS_WINDOWS
2285 /* A helper function for abspath on win32 */
2286 static PyObject *
2287 posix__getfullpathname(PyObject *self, PyObject *args)
2289 /* assume encoded strings wont more than double no of chars */
2290 char inbuf[MAX_PATH*2];
2291 char *inbufp = inbuf;
2292 Py_ssize_t insize = sizeof(inbuf);
2293 char outbuf[MAX_PATH*2];
2294 char *temp;
2295 #ifdef Py_WIN_WIDE_FILENAMES
2296 if (unicode_file_names()) {
2297 PyUnicodeObject *po;
2298 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2299 Py_UNICODE woutbuf[MAX_PATH*2];
2300 Py_UNICODE *wtemp;
2301 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
2302 sizeof(woutbuf)/sizeof(woutbuf[0]),
2303 woutbuf, &wtemp))
2304 return win32_error("GetFullPathName", "");
2305 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2307 /* Drop the argument parsing error as narrow strings
2308 are also valid. */
2309 PyErr_Clear();
2311 #endif
2312 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2313 Py_FileSystemDefaultEncoding, &inbufp,
2314 &insize))
2315 return NULL;
2316 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2317 outbuf, &temp))
2318 return win32_error("GetFullPathName", inbuf);
2319 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2320 return PyUnicode_Decode(outbuf, strlen(outbuf),
2321 Py_FileSystemDefaultEncoding, NULL);
2323 return PyString_FromString(outbuf);
2324 } /* end of posix__getfullpathname */
2325 #endif /* MS_WINDOWS */
2327 PyDoc_STRVAR(posix_mkdir__doc__,
2328 "mkdir(path [, mode=0777])\n\n\
2329 Create a directory.");
2331 static PyObject *
2332 posix_mkdir(PyObject *self, PyObject *args)
2334 int res;
2335 char *path = NULL;
2336 int mode = 0777;
2338 #ifdef Py_WIN_WIDE_FILENAMES
2339 if (unicode_file_names()) {
2340 PyUnicodeObject *po;
2341 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2342 Py_BEGIN_ALLOW_THREADS
2343 /* PyUnicode_AS_UNICODE OK without thread lock as
2344 it is a simple dereference. */
2345 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2346 Py_END_ALLOW_THREADS
2347 if (!res)
2348 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2349 Py_INCREF(Py_None);
2350 return Py_None;
2352 /* Drop the argument parsing error as narrow strings
2353 are also valid. */
2354 PyErr_Clear();
2356 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2357 Py_FileSystemDefaultEncoding, &path, &mode))
2358 return NULL;
2359 Py_BEGIN_ALLOW_THREADS
2360 /* PyUnicode_AS_UNICODE OK without thread lock as
2361 it is a simple dereference. */
2362 res = CreateDirectoryA(path, NULL);
2363 Py_END_ALLOW_THREADS
2364 if (!res) {
2365 win32_error("mkdir", path);
2366 PyMem_Free(path);
2367 return NULL;
2369 PyMem_Free(path);
2370 Py_INCREF(Py_None);
2371 return Py_None;
2372 #else
2374 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2375 Py_FileSystemDefaultEncoding, &path, &mode))
2376 return NULL;
2377 Py_BEGIN_ALLOW_THREADS
2378 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2379 res = mkdir(path);
2380 #else
2381 res = mkdir(path, mode);
2382 #endif
2383 Py_END_ALLOW_THREADS
2384 if (res < 0)
2385 return posix_error_with_allocated_filename(path);
2386 PyMem_Free(path);
2387 Py_INCREF(Py_None);
2388 return Py_None;
2389 #endif
2393 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2394 #if defined(HAVE_SYS_RESOURCE_H)
2395 #include <sys/resource.h>
2396 #endif
2399 #ifdef HAVE_NICE
2400 PyDoc_STRVAR(posix_nice__doc__,
2401 "nice(inc) -> new_priority\n\n\
2402 Decrease the priority of process by inc and return the new priority.");
2404 static PyObject *
2405 posix_nice(PyObject *self, PyObject *args)
2407 int increment, value;
2409 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2410 return NULL;
2412 /* There are two flavours of 'nice': one that returns the new
2413 priority (as required by almost all standards out there) and the
2414 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2415 the use of getpriority() to get the new priority.
2417 If we are of the nice family that returns the new priority, we
2418 need to clear errno before the call, and check if errno is filled
2419 before calling posix_error() on a returnvalue of -1, because the
2420 -1 may be the actual new priority! */
2422 errno = 0;
2423 value = nice(increment);
2424 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2425 if (value == 0)
2426 value = getpriority(PRIO_PROCESS, 0);
2427 #endif
2428 if (value == -1 && errno != 0)
2429 /* either nice() or getpriority() returned an error */
2430 return posix_error();
2431 return PyInt_FromLong((long) value);
2433 #endif /* HAVE_NICE */
2435 PyDoc_STRVAR(posix_rename__doc__,
2436 "rename(old, new)\n\n\
2437 Rename a file or directory.");
2439 static PyObject *
2440 posix_rename(PyObject *self, PyObject *args)
2442 #ifdef MS_WINDOWS
2443 PyObject *o1, *o2;
2444 char *p1, *p2;
2445 BOOL result;
2446 if (unicode_file_names()) {
2447 if (!PyArg_ParseTuple(args, "O&O&:rename",
2448 convert_to_unicode, &o1,
2449 convert_to_unicode, &o2))
2450 PyErr_Clear();
2451 else {
2452 Py_BEGIN_ALLOW_THREADS
2453 result = MoveFileW(PyUnicode_AsUnicode(o1),
2454 PyUnicode_AsUnicode(o2));
2455 Py_END_ALLOW_THREADS
2456 Py_DECREF(o1);
2457 Py_DECREF(o2);
2458 if (!result)
2459 return win32_error("rename", NULL);
2460 Py_INCREF(Py_None);
2461 return Py_None;
2464 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2465 return NULL;
2466 Py_BEGIN_ALLOW_THREADS
2467 result = MoveFileA(p1, p2);
2468 Py_END_ALLOW_THREADS
2469 if (!result)
2470 return win32_error("rename", NULL);
2471 Py_INCREF(Py_None);
2472 return Py_None;
2473 #else
2474 return posix_2str(args, "etet:rename", rename);
2475 #endif
2479 PyDoc_STRVAR(posix_rmdir__doc__,
2480 "rmdir(path)\n\n\
2481 Remove a directory.");
2483 static PyObject *
2484 posix_rmdir(PyObject *self, PyObject *args)
2486 #ifdef MS_WINDOWS
2487 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
2488 #else
2489 return posix_1str(args, "et:rmdir", rmdir);
2490 #endif
2494 PyDoc_STRVAR(posix_stat__doc__,
2495 "stat(path) -> stat result\n\n\
2496 Perform a stat system call on the given path.");
2498 static PyObject *
2499 posix_stat(PyObject *self, PyObject *args)
2501 #ifdef MS_WINDOWS
2502 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
2503 #else
2504 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2505 #endif
2509 #ifdef HAVE_SYSTEM
2510 PyDoc_STRVAR(posix_system__doc__,
2511 "system(command) -> exit_status\n\n\
2512 Execute the command (a string) in a subshell.");
2514 static PyObject *
2515 posix_system(PyObject *self, PyObject *args)
2517 char *command;
2518 long sts;
2519 if (!PyArg_ParseTuple(args, "s:system", &command))
2520 return NULL;
2521 Py_BEGIN_ALLOW_THREADS
2522 sts = system(command);
2523 Py_END_ALLOW_THREADS
2524 return PyInt_FromLong(sts);
2526 #endif
2529 PyDoc_STRVAR(posix_umask__doc__,
2530 "umask(new_mask) -> old_mask\n\n\
2531 Set the current numeric umask and return the previous umask.");
2533 static PyObject *
2534 posix_umask(PyObject *self, PyObject *args)
2536 int i;
2537 if (!PyArg_ParseTuple(args, "i:umask", &i))
2538 return NULL;
2539 i = (int)umask(i);
2540 if (i < 0)
2541 return posix_error();
2542 return PyInt_FromLong((long)i);
2546 PyDoc_STRVAR(posix_unlink__doc__,
2547 "unlink(path)\n\n\
2548 Remove a file (same as remove(path)).");
2550 PyDoc_STRVAR(posix_remove__doc__,
2551 "remove(path)\n\n\
2552 Remove a file (same as unlink(path)).");
2554 static PyObject *
2555 posix_unlink(PyObject *self, PyObject *args)
2557 #ifdef MS_WINDOWS
2558 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
2559 #else
2560 return posix_1str(args, "et:remove", unlink);
2561 #endif
2565 #ifdef HAVE_UNAME
2566 PyDoc_STRVAR(posix_uname__doc__,
2567 "uname() -> (sysname, nodename, release, version, machine)\n\n\
2568 Return a tuple identifying the current operating system.");
2570 static PyObject *
2571 posix_uname(PyObject *self, PyObject *noargs)
2573 struct utsname u;
2574 int res;
2576 Py_BEGIN_ALLOW_THREADS
2577 res = uname(&u);
2578 Py_END_ALLOW_THREADS
2579 if (res < 0)
2580 return posix_error();
2581 return Py_BuildValue("(sssss)",
2582 u.sysname,
2583 u.nodename,
2584 u.release,
2585 u.version,
2586 u.machine);
2588 #endif /* HAVE_UNAME */
2590 static int
2591 extract_time(PyObject *t, long* sec, long* usec)
2593 long intval;
2594 if (PyFloat_Check(t)) {
2595 double tval = PyFloat_AsDouble(t);
2596 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
2597 if (!intobj)
2598 return -1;
2599 intval = PyInt_AsLong(intobj);
2600 Py_DECREF(intobj);
2601 if (intval == -1 && PyErr_Occurred())
2602 return -1;
2603 *sec = intval;
2604 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2605 if (*usec < 0)
2606 /* If rounding gave us a negative number,
2607 truncate. */
2608 *usec = 0;
2609 return 0;
2611 intval = PyInt_AsLong(t);
2612 if (intval == -1 && PyErr_Occurred())
2613 return -1;
2614 *sec = intval;
2615 *usec = 0;
2616 return 0;
2619 PyDoc_STRVAR(posix_utime__doc__,
2620 "utime(path, (atime, mtime))\n\
2621 utime(path, None)\n\n\
2622 Set the access and modified time of the file to the given values. If the\n\
2623 second form is used, set the access and modified times to the current time.");
2625 static PyObject *
2626 posix_utime(PyObject *self, PyObject *args)
2628 #ifdef Py_WIN_WIDE_FILENAMES
2629 PyObject *arg;
2630 PyUnicodeObject *obwpath;
2631 wchar_t *wpath = NULL;
2632 char *apath = NULL;
2633 HANDLE hFile;
2634 long atimesec, mtimesec, ausec, musec;
2635 FILETIME atime, mtime;
2636 PyObject *result = NULL;
2638 if (unicode_file_names()) {
2639 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2640 wpath = PyUnicode_AS_UNICODE(obwpath);
2641 Py_BEGIN_ALLOW_THREADS
2642 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2643 NULL, OPEN_EXISTING,
2644 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2645 Py_END_ALLOW_THREADS
2646 if (hFile == INVALID_HANDLE_VALUE)
2647 return win32_error_unicode("utime", wpath);
2648 } else
2649 /* Drop the argument parsing error as narrow strings
2650 are also valid. */
2651 PyErr_Clear();
2653 if (!wpath) {
2654 if (!PyArg_ParseTuple(args, "etO:utime",
2655 Py_FileSystemDefaultEncoding, &apath, &arg))
2656 return NULL;
2657 Py_BEGIN_ALLOW_THREADS
2658 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2659 NULL, OPEN_EXISTING,
2660 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2661 Py_END_ALLOW_THREADS
2662 if (hFile == INVALID_HANDLE_VALUE) {
2663 win32_error("utime", apath);
2664 PyMem_Free(apath);
2665 return NULL;
2667 PyMem_Free(apath);
2670 if (arg == Py_None) {
2671 SYSTEMTIME now;
2672 GetSystemTime(&now);
2673 if (!SystemTimeToFileTime(&now, &mtime) ||
2674 !SystemTimeToFileTime(&now, &atime)) {
2675 win32_error("utime", NULL);
2676 goto done;
2679 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2680 PyErr_SetString(PyExc_TypeError,
2681 "utime() arg 2 must be a tuple (atime, mtime)");
2682 goto done;
2684 else {
2685 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2686 &atimesec, &ausec) == -1)
2687 goto done;
2688 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2689 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2690 &mtimesec, &musec) == -1)
2691 goto done;
2692 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2694 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2695 /* Avoid putting the file name into the error here,
2696 as that may confuse the user into believing that
2697 something is wrong with the file, when it also
2698 could be the time stamp that gives a problem. */
2699 win32_error("utime", NULL);
2701 Py_INCREF(Py_None);
2702 result = Py_None;
2703 done:
2704 CloseHandle(hFile);
2705 return result;
2706 #else /* Py_WIN_WIDE_FILENAMES */
2708 char *path = NULL;
2709 long atime, mtime, ausec, musec;
2710 int res;
2711 PyObject* arg;
2713 #if defined(HAVE_UTIMES)
2714 struct timeval buf[2];
2715 #define ATIME buf[0].tv_sec
2716 #define MTIME buf[1].tv_sec
2717 #elif defined(HAVE_UTIME_H)
2718 /* XXX should define struct utimbuf instead, above */
2719 struct utimbuf buf;
2720 #define ATIME buf.actime
2721 #define MTIME buf.modtime
2722 #define UTIME_ARG &buf
2723 #else /* HAVE_UTIMES */
2724 time_t buf[2];
2725 #define ATIME buf[0]
2726 #define MTIME buf[1]
2727 #define UTIME_ARG buf
2728 #endif /* HAVE_UTIMES */
2731 if (!PyArg_ParseTuple(args, "etO:utime",
2732 Py_FileSystemDefaultEncoding, &path, &arg))
2733 return NULL;
2734 if (arg == Py_None) {
2735 /* optional time values not given */
2736 Py_BEGIN_ALLOW_THREADS
2737 res = utime(path, NULL);
2738 Py_END_ALLOW_THREADS
2740 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2741 PyErr_SetString(PyExc_TypeError,
2742 "utime() arg 2 must be a tuple (atime, mtime)");
2743 PyMem_Free(path);
2744 return NULL;
2746 else {
2747 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2748 &atime, &ausec) == -1) {
2749 PyMem_Free(path);
2750 return NULL;
2752 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2753 &mtime, &musec) == -1) {
2754 PyMem_Free(path);
2755 return NULL;
2757 ATIME = atime;
2758 MTIME = mtime;
2759 #ifdef HAVE_UTIMES
2760 buf[0].tv_usec = ausec;
2761 buf[1].tv_usec = musec;
2762 Py_BEGIN_ALLOW_THREADS
2763 res = utimes(path, buf);
2764 Py_END_ALLOW_THREADS
2765 #else
2766 Py_BEGIN_ALLOW_THREADS
2767 res = utime(path, UTIME_ARG);
2768 Py_END_ALLOW_THREADS
2769 #endif /* HAVE_UTIMES */
2771 if (res < 0) {
2772 return posix_error_with_allocated_filename(path);
2774 PyMem_Free(path);
2775 Py_INCREF(Py_None);
2776 return Py_None;
2777 #undef UTIME_ARG
2778 #undef ATIME
2779 #undef MTIME
2780 #endif /* Py_WIN_WIDE_FILENAMES */
2784 /* Process operations */
2786 PyDoc_STRVAR(posix__exit__doc__,
2787 "_exit(status)\n\n\
2788 Exit to the system with specified status, without normal exit processing.");
2790 static PyObject *
2791 posix__exit(PyObject *self, PyObject *args)
2793 int sts;
2794 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2795 return NULL;
2796 _exit(sts);
2797 return NULL; /* Make gcc -Wall happy */
2800 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2801 static void
2802 free_string_array(char **array, Py_ssize_t count)
2804 Py_ssize_t i;
2805 for (i = 0; i < count; i++)
2806 PyMem_Free(array[i]);
2807 PyMem_DEL(array);
2809 #endif
2812 #ifdef HAVE_EXECV
2813 PyDoc_STRVAR(posix_execv__doc__,
2814 "execv(path, args)\n\n\
2815 Execute an executable path with arguments, replacing current process.\n\
2817 path: path of executable file\n\
2818 args: tuple or list of strings");
2820 static PyObject *
2821 posix_execv(PyObject *self, PyObject *args)
2823 char *path;
2824 PyObject *argv;
2825 char **argvlist;
2826 Py_ssize_t i, argc;
2827 PyObject *(*getitem)(PyObject *, Py_ssize_t);
2829 /* execv has two arguments: (path, argv), where
2830 argv is a list or tuple of strings. */
2832 if (!PyArg_ParseTuple(args, "etO:execv",
2833 Py_FileSystemDefaultEncoding,
2834 &path, &argv))
2835 return NULL;
2836 if (PyList_Check(argv)) {
2837 argc = PyList_Size(argv);
2838 getitem = PyList_GetItem;
2840 else if (PyTuple_Check(argv)) {
2841 argc = PyTuple_Size(argv);
2842 getitem = PyTuple_GetItem;
2844 else {
2845 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
2846 PyMem_Free(path);
2847 return NULL;
2850 argvlist = PyMem_NEW(char *, argc+1);
2851 if (argvlist == NULL) {
2852 PyMem_Free(path);
2853 return PyErr_NoMemory();
2855 for (i = 0; i < argc; i++) {
2856 if (!PyArg_Parse((*getitem)(argv, i), "et",
2857 Py_FileSystemDefaultEncoding,
2858 &argvlist[i])) {
2859 free_string_array(argvlist, i);
2860 PyErr_SetString(PyExc_TypeError,
2861 "execv() arg 2 must contain only strings");
2862 PyMem_Free(path);
2863 return NULL;
2867 argvlist[argc] = NULL;
2869 execv(path, argvlist);
2871 /* If we get here it's definitely an error */
2873 free_string_array(argvlist, argc);
2874 PyMem_Free(path);
2875 return posix_error();
2879 PyDoc_STRVAR(posix_execve__doc__,
2880 "execve(path, args, env)\n\n\
2881 Execute a path with arguments and environment, replacing current process.\n\
2883 path: path of executable file\n\
2884 args: tuple or list of arguments\n\
2885 env: dictionary of strings mapping to strings");
2887 static PyObject *
2888 posix_execve(PyObject *self, PyObject *args)
2890 char *path;
2891 PyObject *argv, *env;
2892 char **argvlist;
2893 char **envlist;
2894 PyObject *key, *val, *keys=NULL, *vals=NULL;
2895 Py_ssize_t i, pos, argc, envc;
2896 PyObject *(*getitem)(PyObject *, Py_ssize_t);
2897 Py_ssize_t lastarg = 0;
2899 /* execve has three arguments: (path, argv, env), where
2900 argv is a list or tuple of strings and env is a dictionary
2901 like posix.environ. */
2903 if (!PyArg_ParseTuple(args, "etOO:execve",
2904 Py_FileSystemDefaultEncoding,
2905 &path, &argv, &env))
2906 return NULL;
2907 if (PyList_Check(argv)) {
2908 argc = PyList_Size(argv);
2909 getitem = PyList_GetItem;
2911 else if (PyTuple_Check(argv)) {
2912 argc = PyTuple_Size(argv);
2913 getitem = PyTuple_GetItem;
2915 else {
2916 PyErr_SetString(PyExc_TypeError,
2917 "execve() arg 2 must be a tuple or list");
2918 goto fail_0;
2920 if (!PyMapping_Check(env)) {
2921 PyErr_SetString(PyExc_TypeError,
2922 "execve() arg 3 must be a mapping object");
2923 goto fail_0;
2926 argvlist = PyMem_NEW(char *, argc+1);
2927 if (argvlist == NULL) {
2928 PyErr_NoMemory();
2929 goto fail_0;
2931 for (i = 0; i < argc; i++) {
2932 if (!PyArg_Parse((*getitem)(argv, i),
2933 "et;execve() arg 2 must contain only strings",
2934 Py_FileSystemDefaultEncoding,
2935 &argvlist[i]))
2937 lastarg = i;
2938 goto fail_1;
2941 lastarg = argc;
2942 argvlist[argc] = NULL;
2944 i = PyMapping_Size(env);
2945 if (i < 0)
2946 goto fail_1;
2947 envlist = PyMem_NEW(char *, i + 1);
2948 if (envlist == NULL) {
2949 PyErr_NoMemory();
2950 goto fail_1;
2952 envc = 0;
2953 keys = PyMapping_Keys(env);
2954 vals = PyMapping_Values(env);
2955 if (!keys || !vals)
2956 goto fail_2;
2957 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2958 PyErr_SetString(PyExc_TypeError,
2959 "execve(): env.keys() or env.values() is not a list");
2960 goto fail_2;
2963 for (pos = 0; pos < i; pos++) {
2964 char *p, *k, *v;
2965 size_t len;
2967 key = PyList_GetItem(keys, pos);
2968 val = PyList_GetItem(vals, pos);
2969 if (!key || !val)
2970 goto fail_2;
2972 if (!PyArg_Parse(
2973 key,
2974 "s;execve() arg 3 contains a non-string key",
2975 &k) ||
2976 !PyArg_Parse(
2977 val,
2978 "s;execve() arg 3 contains a non-string value",
2979 &v))
2981 goto fail_2;
2984 #if defined(PYOS_OS2)
2985 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2986 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2987 #endif
2988 len = PyString_Size(key) + PyString_Size(val) + 2;
2989 p = PyMem_NEW(char, len);
2990 if (p == NULL) {
2991 PyErr_NoMemory();
2992 goto fail_2;
2994 PyOS_snprintf(p, len, "%s=%s", k, v);
2995 envlist[envc++] = p;
2996 #if defined(PYOS_OS2)
2998 #endif
3000 envlist[envc] = 0;
3002 execve(path, argvlist, envlist);
3004 /* If we get here it's definitely an error */
3006 (void) posix_error();
3008 fail_2:
3009 while (--envc >= 0)
3010 PyMem_DEL(envlist[envc]);
3011 PyMem_DEL(envlist);
3012 fail_1:
3013 free_string_array(argvlist, lastarg);
3014 Py_XDECREF(vals);
3015 Py_XDECREF(keys);
3016 fail_0:
3017 PyMem_Free(path);
3018 return NULL;
3020 #endif /* HAVE_EXECV */
3023 #ifdef HAVE_SPAWNV
3024 PyDoc_STRVAR(posix_spawnv__doc__,
3025 "spawnv(mode, path, args)\n\n\
3026 Execute the program 'path' in a new process.\n\
3028 mode: mode of process creation\n\
3029 path: path of executable file\n\
3030 args: tuple or list of strings");
3032 static PyObject *
3033 posix_spawnv(PyObject *self, PyObject *args)
3035 char *path;
3036 PyObject *argv;
3037 char **argvlist;
3038 int mode, i;
3039 Py_ssize_t argc;
3040 Py_intptr_t spawnval;
3041 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3043 /* spawnv has three arguments: (mode, path, argv), where
3044 argv is a list or tuple of strings. */
3046 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3047 Py_FileSystemDefaultEncoding,
3048 &path, &argv))
3049 return NULL;
3050 if (PyList_Check(argv)) {
3051 argc = PyList_Size(argv);
3052 getitem = PyList_GetItem;
3054 else if (PyTuple_Check(argv)) {
3055 argc = PyTuple_Size(argv);
3056 getitem = PyTuple_GetItem;
3058 else {
3059 PyErr_SetString(PyExc_TypeError,
3060 "spawnv() arg 2 must be a tuple or list");
3061 PyMem_Free(path);
3062 return NULL;
3065 argvlist = PyMem_NEW(char *, argc+1);
3066 if (argvlist == NULL) {
3067 PyMem_Free(path);
3068 return PyErr_NoMemory();
3070 for (i = 0; i < argc; i++) {
3071 if (!PyArg_Parse((*getitem)(argv, i), "et",
3072 Py_FileSystemDefaultEncoding,
3073 &argvlist[i])) {
3074 free_string_array(argvlist, i);
3075 PyErr_SetString(
3076 PyExc_TypeError,
3077 "spawnv() arg 2 must contain only strings");
3078 PyMem_Free(path);
3079 return NULL;
3082 argvlist[argc] = NULL;
3084 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3085 Py_BEGIN_ALLOW_THREADS
3086 spawnval = spawnv(mode, path, argvlist);
3087 Py_END_ALLOW_THREADS
3088 #else
3089 if (mode == _OLD_P_OVERLAY)
3090 mode = _P_OVERLAY;
3092 Py_BEGIN_ALLOW_THREADS
3093 spawnval = _spawnv(mode, path, argvlist);
3094 Py_END_ALLOW_THREADS
3095 #endif
3097 free_string_array(argvlist, argc);
3098 PyMem_Free(path);
3100 if (spawnval == -1)
3101 return posix_error();
3102 else
3103 #if SIZEOF_LONG == SIZEOF_VOID_P
3104 return Py_BuildValue("l", (long) spawnval);
3105 #else
3106 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3107 #endif
3111 PyDoc_STRVAR(posix_spawnve__doc__,
3112 "spawnve(mode, path, args, env)\n\n\
3113 Execute the program 'path' in a new process.\n\
3115 mode: mode of process creation\n\
3116 path: path of executable file\n\
3117 args: tuple or list of arguments\n\
3118 env: dictionary of strings mapping to strings");
3120 static PyObject *
3121 posix_spawnve(PyObject *self, PyObject *args)
3123 char *path;
3124 PyObject *argv, *env;
3125 char **argvlist;
3126 char **envlist;
3127 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3128 int mode, pos, envc;
3129 Py_ssize_t argc, i;
3130 Py_intptr_t spawnval;
3131 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3132 Py_ssize_t lastarg = 0;
3134 /* spawnve has four arguments: (mode, path, argv, env), where
3135 argv is a list or tuple of strings and env is a dictionary
3136 like posix.environ. */
3138 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3139 Py_FileSystemDefaultEncoding,
3140 &path, &argv, &env))
3141 return NULL;
3142 if (PyList_Check(argv)) {
3143 argc = PyList_Size(argv);
3144 getitem = PyList_GetItem;
3146 else if (PyTuple_Check(argv)) {
3147 argc = PyTuple_Size(argv);
3148 getitem = PyTuple_GetItem;
3150 else {
3151 PyErr_SetString(PyExc_TypeError,
3152 "spawnve() arg 2 must be a tuple or list");
3153 goto fail_0;
3155 if (!PyMapping_Check(env)) {
3156 PyErr_SetString(PyExc_TypeError,
3157 "spawnve() arg 3 must be a mapping object");
3158 goto fail_0;
3161 argvlist = PyMem_NEW(char *, argc+1);
3162 if (argvlist == NULL) {
3163 PyErr_NoMemory();
3164 goto fail_0;
3166 for (i = 0; i < argc; i++) {
3167 if (!PyArg_Parse((*getitem)(argv, i),
3168 "et;spawnve() arg 2 must contain only strings",
3169 Py_FileSystemDefaultEncoding,
3170 &argvlist[i]))
3172 lastarg = i;
3173 goto fail_1;
3176 lastarg = argc;
3177 argvlist[argc] = NULL;
3179 i = PyMapping_Size(env);
3180 if (i < 0)
3181 goto fail_1;
3182 envlist = PyMem_NEW(char *, i + 1);
3183 if (envlist == NULL) {
3184 PyErr_NoMemory();
3185 goto fail_1;
3187 envc = 0;
3188 keys = PyMapping_Keys(env);
3189 vals = PyMapping_Values(env);
3190 if (!keys || !vals)
3191 goto fail_2;
3192 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3193 PyErr_SetString(PyExc_TypeError,
3194 "spawnve(): env.keys() or env.values() is not a list");
3195 goto fail_2;
3198 for (pos = 0; pos < i; pos++) {
3199 char *p, *k, *v;
3200 size_t len;
3202 key = PyList_GetItem(keys, pos);
3203 val = PyList_GetItem(vals, pos);
3204 if (!key || !val)
3205 goto fail_2;
3207 if (!PyArg_Parse(
3208 key,
3209 "s;spawnve() arg 3 contains a non-string key",
3210 &k) ||
3211 !PyArg_Parse(
3212 val,
3213 "s;spawnve() arg 3 contains a non-string value",
3214 &v))
3216 goto fail_2;
3218 len = PyString_Size(key) + PyString_Size(val) + 2;
3219 p = PyMem_NEW(char, len);
3220 if (p == NULL) {
3221 PyErr_NoMemory();
3222 goto fail_2;
3224 PyOS_snprintf(p, len, "%s=%s", k, v);
3225 envlist[envc++] = p;
3227 envlist[envc] = 0;
3229 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3230 Py_BEGIN_ALLOW_THREADS
3231 spawnval = spawnve(mode, path, argvlist, envlist);
3232 Py_END_ALLOW_THREADS
3233 #else
3234 if (mode == _OLD_P_OVERLAY)
3235 mode = _P_OVERLAY;
3237 Py_BEGIN_ALLOW_THREADS
3238 spawnval = _spawnve(mode, path, argvlist, envlist);
3239 Py_END_ALLOW_THREADS
3240 #endif
3242 if (spawnval == -1)
3243 (void) posix_error();
3244 else
3245 #if SIZEOF_LONG == SIZEOF_VOID_P
3246 res = Py_BuildValue("l", (long) spawnval);
3247 #else
3248 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3249 #endif
3251 fail_2:
3252 while (--envc >= 0)
3253 PyMem_DEL(envlist[envc]);
3254 PyMem_DEL(envlist);
3255 fail_1:
3256 free_string_array(argvlist, lastarg);
3257 Py_XDECREF(vals);
3258 Py_XDECREF(keys);
3259 fail_0:
3260 PyMem_Free(path);
3261 return res;
3264 /* OS/2 supports spawnvp & spawnvpe natively */
3265 #if defined(PYOS_OS2)
3266 PyDoc_STRVAR(posix_spawnvp__doc__,
3267 "spawnvp(mode, file, args)\n\n\
3268 Execute the program 'file' in a new process, using the environment\n\
3269 search path to find the file.\n\
3271 mode: mode of process creation\n\
3272 file: executable file name\n\
3273 args: tuple or list of strings");
3275 static PyObject *
3276 posix_spawnvp(PyObject *self, PyObject *args)
3278 char *path;
3279 PyObject *argv;
3280 char **argvlist;
3281 int mode, i, argc;
3282 Py_intptr_t spawnval;
3283 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3285 /* spawnvp has three arguments: (mode, path, argv), where
3286 argv is a list or tuple of strings. */
3288 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3289 Py_FileSystemDefaultEncoding,
3290 &path, &argv))
3291 return NULL;
3292 if (PyList_Check(argv)) {
3293 argc = PyList_Size(argv);
3294 getitem = PyList_GetItem;
3296 else if (PyTuple_Check(argv)) {
3297 argc = PyTuple_Size(argv);
3298 getitem = PyTuple_GetItem;
3300 else {
3301 PyErr_SetString(PyExc_TypeError,
3302 "spawnvp() arg 2 must be a tuple or list");
3303 PyMem_Free(path);
3304 return NULL;
3307 argvlist = PyMem_NEW(char *, argc+1);
3308 if (argvlist == NULL) {
3309 PyMem_Free(path);
3310 return PyErr_NoMemory();
3312 for (i = 0; i < argc; i++) {
3313 if (!PyArg_Parse((*getitem)(argv, i), "et",
3314 Py_FileSystemDefaultEncoding,
3315 &argvlist[i])) {
3316 free_string_array(argvlist, i);
3317 PyErr_SetString(
3318 PyExc_TypeError,
3319 "spawnvp() arg 2 must contain only strings");
3320 PyMem_Free(path);
3321 return NULL;
3324 argvlist[argc] = NULL;
3326 Py_BEGIN_ALLOW_THREADS
3327 #if defined(PYCC_GCC)
3328 spawnval = spawnvp(mode, path, argvlist);
3329 #else
3330 spawnval = _spawnvp(mode, path, argvlist);
3331 #endif
3332 Py_END_ALLOW_THREADS
3334 free_string_array(argvlist, argc);
3335 PyMem_Free(path);
3337 if (spawnval == -1)
3338 return posix_error();
3339 else
3340 return Py_BuildValue("l", (long) spawnval);
3344 PyDoc_STRVAR(posix_spawnvpe__doc__,
3345 "spawnvpe(mode, file, args, env)\n\n\
3346 Execute the program 'file' in a new process, using the environment\n\
3347 search path to find the file.\n\
3349 mode: mode of process creation\n\
3350 file: executable file name\n\
3351 args: tuple or list of arguments\n\
3352 env: dictionary of strings mapping to strings");
3354 static PyObject *
3355 posix_spawnvpe(PyObject *self, PyObject *args)
3357 char *path;
3358 PyObject *argv, *env;
3359 char **argvlist;
3360 char **envlist;
3361 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3362 int mode, i, pos, argc, envc;
3363 Py_intptr_t spawnval;
3364 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3365 int lastarg = 0;
3367 /* spawnvpe has four arguments: (mode, path, argv, env), where
3368 argv is a list or tuple of strings and env is a dictionary
3369 like posix.environ. */
3371 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3372 Py_FileSystemDefaultEncoding,
3373 &path, &argv, &env))
3374 return NULL;
3375 if (PyList_Check(argv)) {
3376 argc = PyList_Size(argv);
3377 getitem = PyList_GetItem;
3379 else if (PyTuple_Check(argv)) {
3380 argc = PyTuple_Size(argv);
3381 getitem = PyTuple_GetItem;
3383 else {
3384 PyErr_SetString(PyExc_TypeError,
3385 "spawnvpe() arg 2 must be a tuple or list");
3386 goto fail_0;
3388 if (!PyMapping_Check(env)) {
3389 PyErr_SetString(PyExc_TypeError,
3390 "spawnvpe() arg 3 must be a mapping object");
3391 goto fail_0;
3394 argvlist = PyMem_NEW(char *, argc+1);
3395 if (argvlist == NULL) {
3396 PyErr_NoMemory();
3397 goto fail_0;
3399 for (i = 0; i < argc; i++) {
3400 if (!PyArg_Parse((*getitem)(argv, i),
3401 "et;spawnvpe() arg 2 must contain only strings",
3402 Py_FileSystemDefaultEncoding,
3403 &argvlist[i]))
3405 lastarg = i;
3406 goto fail_1;
3409 lastarg = argc;
3410 argvlist[argc] = NULL;
3412 i = PyMapping_Size(env);
3413 if (i < 0)
3414 goto fail_1;
3415 envlist = PyMem_NEW(char *, i + 1);
3416 if (envlist == NULL) {
3417 PyErr_NoMemory();
3418 goto fail_1;
3420 envc = 0;
3421 keys = PyMapping_Keys(env);
3422 vals = PyMapping_Values(env);
3423 if (!keys || !vals)
3424 goto fail_2;
3425 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3426 PyErr_SetString(PyExc_TypeError,
3427 "spawnvpe(): env.keys() or env.values() is not a list");
3428 goto fail_2;
3431 for (pos = 0; pos < i; pos++) {
3432 char *p, *k, *v;
3433 size_t len;
3435 key = PyList_GetItem(keys, pos);
3436 val = PyList_GetItem(vals, pos);
3437 if (!key || !val)
3438 goto fail_2;
3440 if (!PyArg_Parse(
3441 key,
3442 "s;spawnvpe() arg 3 contains a non-string key",
3443 &k) ||
3444 !PyArg_Parse(
3445 val,
3446 "s;spawnvpe() arg 3 contains a non-string value",
3447 &v))
3449 goto fail_2;
3451 len = PyString_Size(key) + PyString_Size(val) + 2;
3452 p = PyMem_NEW(char, len);
3453 if (p == NULL) {
3454 PyErr_NoMemory();
3455 goto fail_2;
3457 PyOS_snprintf(p, len, "%s=%s", k, v);
3458 envlist[envc++] = p;
3460 envlist[envc] = 0;
3462 Py_BEGIN_ALLOW_THREADS
3463 #if defined(PYCC_GCC)
3464 spawnval = spawnve(mode, path, argvlist, envlist);
3465 #else
3466 spawnval = _spawnve(mode, path, argvlist, envlist);
3467 #endif
3468 Py_END_ALLOW_THREADS
3470 if (spawnval == -1)
3471 (void) posix_error();
3472 else
3473 res = Py_BuildValue("l", (long) spawnval);
3475 fail_2:
3476 while (--envc >= 0)
3477 PyMem_DEL(envlist[envc]);
3478 PyMem_DEL(envlist);
3479 fail_1:
3480 free_string_array(argvlist, lastarg);
3481 Py_XDECREF(vals);
3482 Py_XDECREF(keys);
3483 fail_0:
3484 PyMem_Free(path);
3485 return res;
3487 #endif /* PYOS_OS2 */
3488 #endif /* HAVE_SPAWNV */
3491 #ifdef HAVE_FORK1
3492 PyDoc_STRVAR(posix_fork1__doc__,
3493 "fork1() -> pid\n\n\
3494 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3496 Return 0 to child process and PID of child to parent process.");
3498 static PyObject *
3499 posix_fork1(PyObject *self, PyObject *noargs)
3501 int pid = fork1();
3502 if (pid == -1)
3503 return posix_error();
3504 PyOS_AfterFork();
3505 return PyInt_FromLong((long)pid);
3507 #endif
3510 #ifdef HAVE_FORK
3511 PyDoc_STRVAR(posix_fork__doc__,
3512 "fork() -> pid\n\n\
3513 Fork a child process.\n\
3514 Return 0 to child process and PID of child to parent process.");
3516 static PyObject *
3517 posix_fork(PyObject *self, PyObject *noargs)
3519 int pid = fork();
3520 if (pid == -1)
3521 return posix_error();
3522 if (pid == 0)
3523 PyOS_AfterFork();
3524 return PyInt_FromLong((long)pid);
3526 #endif
3528 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3529 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3530 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3531 #define DEV_PTY_FILE "/dev/ptc"
3532 #define HAVE_DEV_PTMX
3533 #else
3534 #define DEV_PTY_FILE "/dev/ptmx"
3535 #endif
3537 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3538 #ifdef HAVE_PTY_H
3539 #include <pty.h>
3540 #else
3541 #ifdef HAVE_LIBUTIL_H
3542 #include <libutil.h>
3543 #endif /* HAVE_LIBUTIL_H */
3544 #endif /* HAVE_PTY_H */
3545 #ifdef HAVE_STROPTS_H
3546 #include <stropts.h>
3547 #endif
3548 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3550 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3551 PyDoc_STRVAR(posix_openpty__doc__,
3552 "openpty() -> (master_fd, slave_fd)\n\n\
3553 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3555 static PyObject *
3556 posix_openpty(PyObject *self, PyObject *noargs)
3558 int master_fd, slave_fd;
3559 #ifndef HAVE_OPENPTY
3560 char * slave_name;
3561 #endif
3562 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3563 PyOS_sighandler_t sig_saved;
3564 #ifdef sun
3565 extern char *ptsname(int fildes);
3566 #endif
3567 #endif
3569 #ifdef HAVE_OPENPTY
3570 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3571 return posix_error();
3572 #elif defined(HAVE__GETPTY)
3573 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3574 if (slave_name == NULL)
3575 return posix_error();
3577 slave_fd = open(slave_name, O_RDWR);
3578 if (slave_fd < 0)
3579 return posix_error();
3580 #else
3581 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3582 if (master_fd < 0)
3583 return posix_error();
3584 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3585 /* change permission of slave */
3586 if (grantpt(master_fd) < 0) {
3587 PyOS_setsig(SIGCHLD, sig_saved);
3588 return posix_error();
3590 /* unlock slave */
3591 if (unlockpt(master_fd) < 0) {
3592 PyOS_setsig(SIGCHLD, sig_saved);
3593 return posix_error();
3595 PyOS_setsig(SIGCHLD, sig_saved);
3596 slave_name = ptsname(master_fd); /* get name of slave */
3597 if (slave_name == NULL)
3598 return posix_error();
3599 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3600 if (slave_fd < 0)
3601 return posix_error();
3602 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3603 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3604 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
3605 #ifndef __hpux
3606 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
3607 #endif /* __hpux */
3608 #endif /* HAVE_CYGWIN */
3609 #endif /* HAVE_OPENPTY */
3611 return Py_BuildValue("(ii)", master_fd, slave_fd);
3614 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3616 #ifdef HAVE_FORKPTY
3617 PyDoc_STRVAR(posix_forkpty__doc__,
3618 "forkpty() -> (pid, master_fd)\n\n\
3619 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3620 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3621 To both, return fd of newly opened pseudo-terminal.\n");
3623 static PyObject *
3624 posix_forkpty(PyObject *self, PyObject *noargs)
3626 int master_fd = -1, pid;
3628 pid = forkpty(&master_fd, NULL, NULL, NULL);
3629 if (pid == -1)
3630 return posix_error();
3631 if (pid == 0)
3632 PyOS_AfterFork();
3633 return Py_BuildValue("(ii)", pid, master_fd);
3635 #endif
3637 #ifdef HAVE_GETEGID
3638 PyDoc_STRVAR(posix_getegid__doc__,
3639 "getegid() -> egid\n\n\
3640 Return the current process's effective group id.");
3642 static PyObject *
3643 posix_getegid(PyObject *self, PyObject *noargs)
3645 return PyInt_FromLong((long)getegid());
3647 #endif
3650 #ifdef HAVE_GETEUID
3651 PyDoc_STRVAR(posix_geteuid__doc__,
3652 "geteuid() -> euid\n\n\
3653 Return the current process's effective user id.");
3655 static PyObject *
3656 posix_geteuid(PyObject *self, PyObject *noargs)
3658 return PyInt_FromLong((long)geteuid());
3660 #endif
3663 #ifdef HAVE_GETGID
3664 PyDoc_STRVAR(posix_getgid__doc__,
3665 "getgid() -> gid\n\n\
3666 Return the current process's group id.");
3668 static PyObject *
3669 posix_getgid(PyObject *self, PyObject *noargs)
3671 return PyInt_FromLong((long)getgid());
3673 #endif
3676 PyDoc_STRVAR(posix_getpid__doc__,
3677 "getpid() -> pid\n\n\
3678 Return the current process id");
3680 static PyObject *
3681 posix_getpid(PyObject *self, PyObject *noargs)
3683 return PyInt_FromLong((long)getpid());
3687 #ifdef HAVE_GETGROUPS
3688 PyDoc_STRVAR(posix_getgroups__doc__,
3689 "getgroups() -> list of group IDs\n\n\
3690 Return list of supplemental group IDs for the process.");
3692 static PyObject *
3693 posix_getgroups(PyObject *self, PyObject *noargs)
3695 PyObject *result = NULL;
3697 #ifdef NGROUPS_MAX
3698 #define MAX_GROUPS NGROUPS_MAX
3699 #else
3700 /* defined to be 16 on Solaris7, so this should be a small number */
3701 #define MAX_GROUPS 64
3702 #endif
3703 gid_t grouplist[MAX_GROUPS];
3704 int n;
3706 n = getgroups(MAX_GROUPS, grouplist);
3707 if (n < 0)
3708 posix_error();
3709 else {
3710 result = PyList_New(n);
3711 if (result != NULL) {
3712 int i;
3713 for (i = 0; i < n; ++i) {
3714 PyObject *o = PyInt_FromLong((long)grouplist[i]);
3715 if (o == NULL) {
3716 Py_DECREF(result);
3717 result = NULL;
3718 break;
3720 PyList_SET_ITEM(result, i, o);
3725 return result;
3727 #endif
3729 #ifdef HAVE_GETPGID
3730 PyDoc_STRVAR(posix_getpgid__doc__,
3731 "getpgid(pid) -> pgid\n\n\
3732 Call the system call getpgid().");
3734 static PyObject *
3735 posix_getpgid(PyObject *self, PyObject *args)
3737 int pid, pgid;
3738 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3739 return NULL;
3740 pgid = getpgid(pid);
3741 if (pgid < 0)
3742 return posix_error();
3743 return PyInt_FromLong((long)pgid);
3745 #endif /* HAVE_GETPGID */
3748 #ifdef HAVE_GETPGRP
3749 PyDoc_STRVAR(posix_getpgrp__doc__,
3750 "getpgrp() -> pgrp\n\n\
3751 Return the current process group id.");
3753 static PyObject *
3754 posix_getpgrp(PyObject *self, PyObject *noargs)
3756 #ifdef GETPGRP_HAVE_ARG
3757 return PyInt_FromLong((long)getpgrp(0));
3758 #else /* GETPGRP_HAVE_ARG */
3759 return PyInt_FromLong((long)getpgrp());
3760 #endif /* GETPGRP_HAVE_ARG */
3762 #endif /* HAVE_GETPGRP */
3765 #ifdef HAVE_SETPGRP
3766 PyDoc_STRVAR(posix_setpgrp__doc__,
3767 "setpgrp()\n\n\
3768 Make this process a session leader.");
3770 static PyObject *
3771 posix_setpgrp(PyObject *self, PyObject *noargs)
3773 #ifdef SETPGRP_HAVE_ARG
3774 if (setpgrp(0, 0) < 0)
3775 #else /* SETPGRP_HAVE_ARG */
3776 if (setpgrp() < 0)
3777 #endif /* SETPGRP_HAVE_ARG */
3778 return posix_error();
3779 Py_INCREF(Py_None);
3780 return Py_None;
3783 #endif /* HAVE_SETPGRP */
3785 #ifdef HAVE_GETPPID
3786 PyDoc_STRVAR(posix_getppid__doc__,
3787 "getppid() -> ppid\n\n\
3788 Return the parent's process id.");
3790 static PyObject *
3791 posix_getppid(PyObject *self, PyObject *noargs)
3793 return PyInt_FromLong((long)getppid());
3795 #endif
3798 #ifdef HAVE_GETLOGIN
3799 PyDoc_STRVAR(posix_getlogin__doc__,
3800 "getlogin() -> string\n\n\
3801 Return the actual login name.");
3803 static PyObject *
3804 posix_getlogin(PyObject *self, PyObject *noargs)
3806 PyObject *result = NULL;
3807 char *name;
3808 int old_errno = errno;
3810 errno = 0;
3811 name = getlogin();
3812 if (name == NULL) {
3813 if (errno)
3814 posix_error();
3815 else
3816 PyErr_SetString(PyExc_OSError,
3817 "unable to determine login name");
3819 else
3820 result = PyString_FromString(name);
3821 errno = old_errno;
3823 return result;
3825 #endif
3827 #ifdef HAVE_GETUID
3828 PyDoc_STRVAR(posix_getuid__doc__,
3829 "getuid() -> uid\n\n\
3830 Return the current process's user id.");
3832 static PyObject *
3833 posix_getuid(PyObject *self, PyObject *noargs)
3835 return PyInt_FromLong((long)getuid());
3837 #endif
3840 #ifdef HAVE_KILL
3841 PyDoc_STRVAR(posix_kill__doc__,
3842 "kill(pid, sig)\n\n\
3843 Kill a process with a signal.");
3845 static PyObject *
3846 posix_kill(PyObject *self, PyObject *args)
3848 int pid, sig;
3849 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
3850 return NULL;
3851 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
3852 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3853 APIRET rc;
3854 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
3855 return os2_error(rc);
3857 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3858 APIRET rc;
3859 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
3860 return os2_error(rc);
3862 } else
3863 return NULL; /* Unrecognized Signal Requested */
3864 #else
3865 if (kill(pid, sig) == -1)
3866 return posix_error();
3867 #endif
3868 Py_INCREF(Py_None);
3869 return Py_None;
3871 #endif
3873 #ifdef HAVE_KILLPG
3874 PyDoc_STRVAR(posix_killpg__doc__,
3875 "killpg(pgid, sig)\n\n\
3876 Kill a process group with a signal.");
3878 static PyObject *
3879 posix_killpg(PyObject *self, PyObject *args)
3881 int pgid, sig;
3882 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3883 return NULL;
3884 if (killpg(pgid, sig) == -1)
3885 return posix_error();
3886 Py_INCREF(Py_None);
3887 return Py_None;
3889 #endif
3891 #ifdef HAVE_PLOCK
3893 #ifdef HAVE_SYS_LOCK_H
3894 #include <sys/lock.h>
3895 #endif
3897 PyDoc_STRVAR(posix_plock__doc__,
3898 "plock(op)\n\n\
3899 Lock program segments into memory.");
3901 static PyObject *
3902 posix_plock(PyObject *self, PyObject *args)
3904 int op;
3905 if (!PyArg_ParseTuple(args, "i:plock", &op))
3906 return NULL;
3907 if (plock(op) == -1)
3908 return posix_error();
3909 Py_INCREF(Py_None);
3910 return Py_None;
3912 #endif
3915 #ifdef HAVE_POPEN
3916 PyDoc_STRVAR(posix_popen__doc__,
3917 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
3918 Open a pipe to/from a command returning a file object.");
3920 #if defined(PYOS_OS2)
3921 #if defined(PYCC_VACPP)
3922 static int
3923 async_system(const char *command)
3925 char errormsg[256], args[1024];
3926 RESULTCODES rcodes;
3927 APIRET rc;
3929 char *shell = getenv("COMSPEC");
3930 if (!shell)
3931 shell = "cmd";
3933 /* avoid overflowing the argument buffer */
3934 if (strlen(shell) + 3 + strlen(command) >= 1024)
3935 return ERROR_NOT_ENOUGH_MEMORY
3937 args[0] = '\0';
3938 strcat(args, shell);
3939 strcat(args, "/c ");
3940 strcat(args, command);
3942 /* execute asynchronously, inheriting the environment */
3943 rc = DosExecPgm(errormsg,
3944 sizeof(errormsg),
3945 EXEC_ASYNC,
3946 args,
3947 NULL,
3948 &rcodes,
3949 shell);
3950 return rc;
3953 static FILE *
3954 popen(const char *command, const char *mode, int pipesize, int *err)
3956 int oldfd, tgtfd;
3957 HFILE pipeh[2];
3958 APIRET rc;
3960 /* mode determines which of stdin or stdout is reconnected to
3961 * the pipe to the child
3963 if (strchr(mode, 'r') != NULL) {
3964 tgt_fd = 1; /* stdout */
3965 } else if (strchr(mode, 'w')) {
3966 tgt_fd = 0; /* stdin */
3967 } else {
3968 *err = ERROR_INVALID_ACCESS;
3969 return NULL;
3972 /* setup the pipe */
3973 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3974 *err = rc;
3975 return NULL;
3978 /* prevent other threads accessing stdio */
3979 DosEnterCritSec();
3981 /* reconnect stdio and execute child */
3982 oldfd = dup(tgtfd);
3983 close(tgtfd);
3984 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3985 DosClose(pipeh[tgtfd]);
3986 rc = async_system(command);
3989 /* restore stdio */
3990 dup2(oldfd, tgtfd);
3991 close(oldfd);
3993 /* allow other threads access to stdio */
3994 DosExitCritSec();
3996 /* if execution of child was successful return file stream */
3997 if (rc == NO_ERROR)
3998 return fdopen(pipeh[1 - tgtfd], mode);
3999 else {
4000 DosClose(pipeh[1 - tgtfd]);
4001 *err = rc;
4002 return NULL;
4006 static PyObject *
4007 posix_popen(PyObject *self, PyObject *args)
4009 char *name;
4010 char *mode = "r";
4011 int err, bufsize = -1;
4012 FILE *fp;
4013 PyObject *f;
4014 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4015 return NULL;
4016 Py_BEGIN_ALLOW_THREADS
4017 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
4018 Py_END_ALLOW_THREADS
4019 if (fp == NULL)
4020 return os2_error(err);
4022 f = PyFile_FromFile(fp, name, mode, fclose);
4023 if (f != NULL)
4024 PyFile_SetBufSize(f, bufsize);
4025 return f;
4028 #elif defined(PYCC_GCC)
4030 /* standard posix version of popen() support */
4031 static PyObject *
4032 posix_popen(PyObject *self, PyObject *args)
4034 char *name;
4035 char *mode = "r";
4036 int bufsize = -1;
4037 FILE *fp;
4038 PyObject *f;
4039 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4040 return NULL;
4041 Py_BEGIN_ALLOW_THREADS
4042 fp = popen(name, mode);
4043 Py_END_ALLOW_THREADS
4044 if (fp == NULL)
4045 return posix_error();
4046 f = PyFile_FromFile(fp, name, mode, pclose);
4047 if (f != NULL)
4048 PyFile_SetBufSize(f, bufsize);
4049 return f;
4052 /* fork() under OS/2 has lots'o'warts
4053 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4054 * most of this code is a ripoff of the win32 code, but using the
4055 * capabilities of EMX's C library routines
4058 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4059 #define POPEN_1 1
4060 #define POPEN_2 2
4061 #define POPEN_3 3
4062 #define POPEN_4 4
4064 static PyObject *_PyPopen(char *, int, int, int);
4065 static int _PyPclose(FILE *file);
4068 * Internal dictionary mapping popen* file pointers to process handles,
4069 * for use when retrieving the process exit code. See _PyPclose() below
4070 * for more information on this dictionary's use.
4072 static PyObject *_PyPopenProcs = NULL;
4074 /* os2emx version of popen2()
4076 * The result of this function is a pipe (file) connected to the
4077 * process's stdin, and a pipe connected to the process's stdout.
4080 static PyObject *
4081 os2emx_popen2(PyObject *self, PyObject *args)
4083 PyObject *f;
4084 int tm=0;
4086 char *cmdstring;
4087 char *mode = "t";
4088 int bufsize = -1;
4089 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4090 return NULL;
4092 if (*mode == 't')
4093 tm = O_TEXT;
4094 else if (*mode != 'b') {
4095 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4096 return NULL;
4097 } else
4098 tm = O_BINARY;
4100 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4102 return f;
4106 * Variation on os2emx.popen2
4108 * The result of this function is 3 pipes - the process's stdin,
4109 * stdout and stderr
4112 static PyObject *
4113 os2emx_popen3(PyObject *self, PyObject *args)
4115 PyObject *f;
4116 int tm = 0;
4118 char *cmdstring;
4119 char *mode = "t";
4120 int bufsize = -1;
4121 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4122 return NULL;
4124 if (*mode == 't')
4125 tm = O_TEXT;
4126 else if (*mode != 'b') {
4127 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4128 return NULL;
4129 } else
4130 tm = O_BINARY;
4132 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4134 return f;
4138 * Variation on os2emx.popen2
4140 * The result of this function is 2 pipes - the processes stdin,
4141 * and stdout+stderr combined as a single pipe.
4144 static PyObject *
4145 os2emx_popen4(PyObject *self, PyObject *args)
4147 PyObject *f;
4148 int tm = 0;
4150 char *cmdstring;
4151 char *mode = "t";
4152 int bufsize = -1;
4153 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4154 return NULL;
4156 if (*mode == 't')
4157 tm = O_TEXT;
4158 else if (*mode != 'b') {
4159 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4160 return NULL;
4161 } else
4162 tm = O_BINARY;
4164 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4166 return f;
4169 /* a couple of structures for convenient handling of multiple
4170 * file handles and pipes
4172 struct file_ref
4174 int handle;
4175 int flags;
4178 struct pipe_ref
4180 int rd;
4181 int wr;
4184 /* The following code is derived from the win32 code */
4186 static PyObject *
4187 _PyPopen(char *cmdstring, int mode, int n, int bufsize)
4189 struct file_ref stdio[3];
4190 struct pipe_ref p_fd[3];
4191 FILE *p_s[3];
4192 int file_count, i, pipe_err, pipe_pid;
4193 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4194 PyObject *f, *p_f[3];
4196 /* file modes for subsequent fdopen's on pipe handles */
4197 if (mode == O_TEXT)
4199 rd_mode = "rt";
4200 wr_mode = "wt";
4202 else
4204 rd_mode = "rb";
4205 wr_mode = "wb";
4208 /* prepare shell references */
4209 if ((shell = getenv("EMXSHELL")) == NULL)
4210 if ((shell = getenv("COMSPEC")) == NULL)
4212 errno = ENOENT;
4213 return posix_error();
4216 sh_name = _getname(shell);
4217 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4218 opt = "/c";
4219 else
4220 opt = "-c";
4222 /* save current stdio fds + their flags, and set not inheritable */
4223 i = pipe_err = 0;
4224 while (pipe_err >= 0 && i < 3)
4226 pipe_err = stdio[i].handle = dup(i);
4227 stdio[i].flags = fcntl(i, F_GETFD, 0);
4228 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4229 i++;
4231 if (pipe_err < 0)
4233 /* didn't get them all saved - clean up and bail out */
4234 int saved_err = errno;
4235 while (i-- > 0)
4237 close(stdio[i].handle);
4239 errno = saved_err;
4240 return posix_error();
4243 /* create pipe ends */
4244 file_count = 2;
4245 if (n == POPEN_3)
4246 file_count = 3;
4247 i = pipe_err = 0;
4248 while ((pipe_err == 0) && (i < file_count))
4249 pipe_err = pipe((int *)&p_fd[i++]);
4250 if (pipe_err < 0)
4252 /* didn't get them all made - clean up and bail out */
4253 while (i-- > 0)
4255 close(p_fd[i].wr);
4256 close(p_fd[i].rd);
4258 errno = EPIPE;
4259 return posix_error();
4262 /* change the actual standard IO streams over temporarily,
4263 * making the retained pipe ends non-inheritable
4265 pipe_err = 0;
4267 /* - stdin */
4268 if (dup2(p_fd[0].rd, 0) == 0)
4270 close(p_fd[0].rd);
4271 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4272 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4273 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4275 close(p_fd[0].wr);
4276 pipe_err = -1;
4279 else
4281 pipe_err = -1;
4284 /* - stdout */
4285 if (pipe_err == 0)
4287 if (dup2(p_fd[1].wr, 1) == 1)
4289 close(p_fd[1].wr);
4290 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4291 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4292 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4294 close(p_fd[1].rd);
4295 pipe_err = -1;
4298 else
4300 pipe_err = -1;
4304 /* - stderr, as required */
4305 if (pipe_err == 0)
4306 switch (n)
4308 case POPEN_3:
4310 if (dup2(p_fd[2].wr, 2) == 2)
4312 close(p_fd[2].wr);
4313 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4314 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4315 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4317 close(p_fd[2].rd);
4318 pipe_err = -1;
4321 else
4323 pipe_err = -1;
4325 break;
4328 case POPEN_4:
4330 if (dup2(1, 2) != 2)
4332 pipe_err = -1;
4334 break;
4338 /* spawn the child process */
4339 if (pipe_err == 0)
4341 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4342 if (pipe_pid == -1)
4344 pipe_err = -1;
4346 else
4348 /* save the PID into the FILE structure
4349 * NOTE: this implementation doesn't actually
4350 * take advantage of this, but do it for
4351 * completeness - AIM Apr01
4353 for (i = 0; i < file_count; i++)
4354 p_s[i]->_pid = pipe_pid;
4358 /* reset standard IO to normal */
4359 for (i = 0; i < 3; i++)
4361 dup2(stdio[i].handle, i);
4362 fcntl(i, F_SETFD, stdio[i].flags);
4363 close(stdio[i].handle);
4366 /* if any remnant problems, clean up and bail out */
4367 if (pipe_err < 0)
4369 for (i = 0; i < 3; i++)
4371 close(p_fd[i].rd);
4372 close(p_fd[i].wr);
4374 errno = EPIPE;
4375 return posix_error_with_filename(cmdstring);
4378 /* build tuple of file objects to return */
4379 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4380 PyFile_SetBufSize(p_f[0], bufsize);
4381 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4382 PyFile_SetBufSize(p_f[1], bufsize);
4383 if (n == POPEN_3)
4385 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4386 PyFile_SetBufSize(p_f[0], bufsize);
4387 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
4389 else
4390 f = PyTuple_Pack(2, p_f[0], p_f[1]);
4393 * Insert the files we've created into the process dictionary
4394 * all referencing the list with the process handle and the
4395 * initial number of files (see description below in _PyPclose).
4396 * Since if _PyPclose later tried to wait on a process when all
4397 * handles weren't closed, it could create a deadlock with the
4398 * child, we spend some energy here to try to ensure that we
4399 * either insert all file handles into the dictionary or none
4400 * at all. It's a little clumsy with the various popen modes
4401 * and variable number of files involved.
4403 if (!_PyPopenProcs)
4405 _PyPopenProcs = PyDict_New();
4408 if (_PyPopenProcs)
4410 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4411 int ins_rc[3];
4413 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4414 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4416 procObj = PyList_New(2);
4417 pidObj = PyInt_FromLong((long) pipe_pid);
4418 intObj = PyInt_FromLong((long) file_count);
4420 if (procObj && pidObj && intObj)
4422 PyList_SetItem(procObj, 0, pidObj);
4423 PyList_SetItem(procObj, 1, intObj);
4425 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4426 if (fileObj[0])
4428 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4429 fileObj[0],
4430 procObj);
4432 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4433 if (fileObj[1])
4435 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4436 fileObj[1],
4437 procObj);
4439 if (file_count >= 3)
4441 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4442 if (fileObj[2])
4444 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4445 fileObj[2],
4446 procObj);
4450 if (ins_rc[0] < 0 || !fileObj[0] ||
4451 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4452 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4454 /* Something failed - remove any dictionary
4455 * entries that did make it.
4457 if (!ins_rc[0] && fileObj[0])
4459 PyDict_DelItem(_PyPopenProcs,
4460 fileObj[0]);
4462 if (!ins_rc[1] && fileObj[1])
4464 PyDict_DelItem(_PyPopenProcs,
4465 fileObj[1]);
4467 if (!ins_rc[2] && fileObj[2])
4469 PyDict_DelItem(_PyPopenProcs,
4470 fileObj[2]);
4476 * Clean up our localized references for the dictionary keys
4477 * and value since PyDict_SetItem will Py_INCREF any copies
4478 * that got placed in the dictionary.
4480 Py_XDECREF(procObj);
4481 Py_XDECREF(fileObj[0]);
4482 Py_XDECREF(fileObj[1]);
4483 Py_XDECREF(fileObj[2]);
4486 /* Child is launched. */
4487 return f;
4491 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4492 * exit code for the child process and return as a result of the close.
4494 * This function uses the _PyPopenProcs dictionary in order to map the
4495 * input file pointer to information about the process that was
4496 * originally created by the popen* call that created the file pointer.
4497 * The dictionary uses the file pointer as a key (with one entry
4498 * inserted for each file returned by the original popen* call) and a
4499 * single list object as the value for all files from a single call.
4500 * The list object contains the Win32 process handle at [0], and a file
4501 * count at [1], which is initialized to the total number of file
4502 * handles using that list.
4504 * This function closes whichever handle it is passed, and decrements
4505 * the file count in the dictionary for the process handle pointed to
4506 * by this file. On the last close (when the file count reaches zero),
4507 * this function will wait for the child process and then return its
4508 * exit code as the result of the close() operation. This permits the
4509 * files to be closed in any order - it is always the close() of the
4510 * final handle that will return the exit code.
4512 * NOTE: This function is currently called with the GIL released.
4513 * hence we use the GILState API to manage our state.
4516 static int _PyPclose(FILE *file)
4518 int result;
4519 int exit_code;
4520 int pipe_pid;
4521 PyObject *procObj, *pidObj, *intObj, *fileObj;
4522 int file_count;
4523 #ifdef WITH_THREAD
4524 PyGILState_STATE state;
4525 #endif
4527 /* Close the file handle first, to ensure it can't block the
4528 * child from exiting if it's the last handle.
4530 result = fclose(file);
4532 #ifdef WITH_THREAD
4533 state = PyGILState_Ensure();
4534 #endif
4535 if (_PyPopenProcs)
4537 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4538 (procObj = PyDict_GetItem(_PyPopenProcs,
4539 fileObj)) != NULL &&
4540 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4541 (intObj = PyList_GetItem(procObj,1)) != NULL)
4543 pipe_pid = (int) PyInt_AsLong(pidObj);
4544 file_count = (int) PyInt_AsLong(intObj);
4546 if (file_count > 1)
4548 /* Still other files referencing process */
4549 file_count--;
4550 PyList_SetItem(procObj,1,
4551 PyInt_FromLong((long) file_count));
4553 else
4555 /* Last file for this process */
4556 if (result != EOF &&
4557 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4559 /* extract exit status */
4560 if (WIFEXITED(exit_code))
4562 result = WEXITSTATUS(exit_code);
4564 else
4566 errno = EPIPE;
4567 result = -1;
4570 else
4572 /* Indicate failure - this will cause the file object
4573 * to raise an I/O error and translate the last
4574 * error code from errno. We do have a problem with
4575 * last errors that overlap the normal errno table,
4576 * but that's a consistent problem with the file object.
4578 result = -1;
4582 /* Remove this file pointer from dictionary */
4583 PyDict_DelItem(_PyPopenProcs, fileObj);
4585 if (PyDict_Size(_PyPopenProcs) == 0)
4587 Py_DECREF(_PyPopenProcs);
4588 _PyPopenProcs = NULL;
4591 } /* if object retrieval ok */
4593 Py_XDECREF(fileObj);
4594 } /* if _PyPopenProcs */
4596 #ifdef WITH_THREAD
4597 PyGILState_Release(state);
4598 #endif
4599 return result;
4602 #endif /* PYCC_??? */
4604 #elif defined(MS_WINDOWS)
4607 * Portable 'popen' replacement for Win32.
4609 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4610 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
4611 * Return code handling by David Bolen <db3l@fitlinxx.com>.
4614 #include <malloc.h>
4615 #include <io.h>
4616 #include <fcntl.h>
4618 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4619 #define POPEN_1 1
4620 #define POPEN_2 2
4621 #define POPEN_3 3
4622 #define POPEN_4 4
4624 static PyObject *_PyPopen(char *, int, int);
4625 static int _PyPclose(FILE *file);
4628 * Internal dictionary mapping popen* file pointers to process handles,
4629 * for use when retrieving the process exit code. See _PyPclose() below
4630 * for more information on this dictionary's use.
4632 static PyObject *_PyPopenProcs = NULL;
4635 /* popen that works from a GUI.
4637 * The result of this function is a pipe (file) connected to the
4638 * processes stdin or stdout, depending on the requested mode.
4641 static PyObject *
4642 posix_popen(PyObject *self, PyObject *args)
4644 PyObject *f;
4645 int tm = 0;
4647 char *cmdstring;
4648 char *mode = "r";
4649 int bufsize = -1;
4650 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
4651 return NULL;
4653 if (*mode == 'r')
4654 tm = _O_RDONLY;
4655 else if (*mode != 'w') {
4656 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
4657 return NULL;
4658 } else
4659 tm = _O_WRONLY;
4661 if (bufsize != -1) {
4662 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
4663 return NULL;
4666 if (*(mode+1) == 't')
4667 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4668 else if (*(mode+1) == 'b')
4669 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
4670 else
4671 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4673 return f;
4676 /* Variation on win32pipe.popen
4678 * The result of this function is a pipe (file) connected to the
4679 * process's stdin, and a pipe connected to the process's stdout.
4682 static PyObject *
4683 win32_popen2(PyObject *self, PyObject *args)
4685 PyObject *f;
4686 int tm=0;
4688 char *cmdstring;
4689 char *mode = "t";
4690 int bufsize = -1;
4691 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4692 return NULL;
4694 if (*mode == 't')
4695 tm = _O_TEXT;
4696 else if (*mode != 'b') {
4697 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
4698 return NULL;
4699 } else
4700 tm = _O_BINARY;
4702 if (bufsize != -1) {
4703 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
4704 return NULL;
4707 f = _PyPopen(cmdstring, tm, POPEN_2);
4709 return f;
4713 * Variation on <om win32pipe.popen>
4715 * The result of this function is 3 pipes - the process's stdin,
4716 * stdout and stderr
4719 static PyObject *
4720 win32_popen3(PyObject *self, PyObject *args)
4722 PyObject *f;
4723 int tm = 0;
4725 char *cmdstring;
4726 char *mode = "t";
4727 int bufsize = -1;
4728 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4729 return NULL;
4731 if (*mode == 't')
4732 tm = _O_TEXT;
4733 else if (*mode != 'b') {
4734 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
4735 return NULL;
4736 } else
4737 tm = _O_BINARY;
4739 if (bufsize != -1) {
4740 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
4741 return NULL;
4744 f = _PyPopen(cmdstring, tm, POPEN_3);
4746 return f;
4750 * Variation on win32pipe.popen
4752 * The result of this function is 2 pipes - the processes stdin,
4753 * and stdout+stderr combined as a single pipe.
4756 static PyObject *
4757 win32_popen4(PyObject *self, PyObject *args)
4759 PyObject *f;
4760 int tm = 0;
4762 char *cmdstring;
4763 char *mode = "t";
4764 int bufsize = -1;
4765 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4766 return NULL;
4768 if (*mode == 't')
4769 tm = _O_TEXT;
4770 else if (*mode != 'b') {
4771 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
4772 return NULL;
4773 } else
4774 tm = _O_BINARY;
4776 if (bufsize != -1) {
4777 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
4778 return NULL;
4781 f = _PyPopen(cmdstring, tm, POPEN_4);
4783 return f;
4786 static BOOL
4787 _PyPopenCreateProcess(char *cmdstring,
4788 HANDLE hStdin,
4789 HANDLE hStdout,
4790 HANDLE hStderr,
4791 HANDLE *hProcess)
4793 PROCESS_INFORMATION piProcInfo;
4794 STARTUPINFO siStartInfo;
4795 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
4796 char *s1,*s2, *s3 = " /c ";
4797 const char *szConsoleSpawn = "w9xpopen.exe";
4798 int i;
4799 Py_ssize_t x;
4801 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
4802 char *comshell;
4804 s1 = (char *)alloca(i);
4805 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4806 /* x < i, so x fits into an integer */
4807 return (int)x;
4809 /* Explicitly check if we are using COMMAND.COM. If we are
4810 * then use the w9xpopen hack.
4812 comshell = s1 + x;
4813 while (comshell >= s1 && *comshell != '\\')
4814 --comshell;
4815 ++comshell;
4817 if (GetVersion() < 0x80000000 &&
4818 _stricmp(comshell, "command.com") != 0) {
4819 /* NT/2000 and not using command.com. */
4820 x = i + strlen(s3) + strlen(cmdstring) + 1;
4821 s2 = (char *)alloca(x);
4822 ZeroMemory(s2, x);
4823 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
4825 else {
4827 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4828 * the workaround listed in KB: Q150956
4830 char modulepath[_MAX_PATH];
4831 struct stat statinfo;
4832 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4833 for (x = i = 0; modulepath[i]; i++)
4834 if (modulepath[i] == SEP)
4835 x = i+1;
4836 modulepath[x] = '\0';
4837 /* Create the full-name to w9xpopen, so we can test it exists */
4838 strncat(modulepath,
4839 szConsoleSpawn,
4840 (sizeof(modulepath)/sizeof(modulepath[0]))
4841 -strlen(modulepath));
4842 if (stat(modulepath, &statinfo) != 0) {
4843 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
4844 /* Eeek - file-not-found - possibly an embedding
4845 situation - see if we can locate it in sys.prefix
4847 strncpy(modulepath,
4848 Py_GetExecPrefix(),
4849 mplen);
4850 modulepath[mplen-1] = '\0';
4851 if (modulepath[strlen(modulepath)-1] != '\\')
4852 strcat(modulepath, "\\");
4853 strncat(modulepath,
4854 szConsoleSpawn,
4855 mplen-strlen(modulepath));
4856 /* No where else to look - raise an easily identifiable
4857 error, rather than leaving Windows to report
4858 "file not found" - as the user is probably blissfully
4859 unaware this shim EXE is used, and it will confuse them.
4860 (well, it confused me for a while ;-)
4862 if (stat(modulepath, &statinfo) != 0) {
4863 PyErr_Format(PyExc_RuntimeError,
4864 "Can not locate '%s' which is needed "
4865 "for popen to work with your shell "
4866 "or platform.",
4867 szConsoleSpawn);
4868 return FALSE;
4871 x = i + strlen(s3) + strlen(cmdstring) + 1 +
4872 strlen(modulepath) +
4873 strlen(szConsoleSpawn) + 1;
4875 s2 = (char *)alloca(x);
4876 ZeroMemory(s2, x);
4877 /* To maintain correct argument passing semantics,
4878 we pass the command-line as it stands, and allow
4879 quoting to be applied. w9xpopen.exe will then
4880 use its argv vector, and re-quote the necessary
4881 args for the ultimate child process.
4883 PyOS_snprintf(
4884 s2, x,
4885 "\"%s\" %s%s%s",
4886 modulepath,
4889 cmdstring);
4890 /* Not passing CREATE_NEW_CONSOLE has been known to
4891 cause random failures on win9x. Specifically a
4892 dialog:
4893 "Your program accessed mem currently in use at xxx"
4894 and a hopeful warning about the stability of your
4895 system.
4896 Cost is Ctrl+C wont kill children, but anyone
4897 who cares can have a go!
4899 dwProcessFlags |= CREATE_NEW_CONSOLE;
4903 /* Could be an else here to try cmd.exe / command.com in the path
4904 Now we'll just error out.. */
4905 else {
4906 PyErr_SetString(PyExc_RuntimeError,
4907 "Cannot locate a COMSPEC environment variable to "
4908 "use as the shell");
4909 return FALSE;
4912 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4913 siStartInfo.cb = sizeof(STARTUPINFO);
4914 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4915 siStartInfo.hStdInput = hStdin;
4916 siStartInfo.hStdOutput = hStdout;
4917 siStartInfo.hStdError = hStderr;
4918 siStartInfo.wShowWindow = SW_HIDE;
4920 if (CreateProcess(NULL,
4922 NULL,
4923 NULL,
4924 TRUE,
4925 dwProcessFlags,
4926 NULL,
4927 NULL,
4928 &siStartInfo,
4929 &piProcInfo) ) {
4930 /* Close the handles now so anyone waiting is woken. */
4931 CloseHandle(piProcInfo.hThread);
4933 /* Return process handle */
4934 *hProcess = piProcInfo.hProcess;
4935 return TRUE;
4937 win32_error("CreateProcess", s2);
4938 return FALSE;
4941 /* The following code is based off of KB: Q190351 */
4943 static PyObject *
4944 _PyPopen(char *cmdstring, int mode, int n)
4946 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4947 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
4948 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
4950 SECURITY_ATTRIBUTES saAttr;
4951 BOOL fSuccess;
4952 int fd1, fd2, fd3;
4953 FILE *f1, *f2, *f3;
4954 long file_count;
4955 PyObject *f;
4957 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4958 saAttr.bInheritHandle = TRUE;
4959 saAttr.lpSecurityDescriptor = NULL;
4961 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4962 return win32_error("CreatePipe", NULL);
4964 /* Create new output read handle and the input write handle. Set
4965 * the inheritance properties to FALSE. Otherwise, the child inherits
4966 * these handles; resulting in non-closeable handles to the pipes
4967 * being created. */
4968 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
4969 GetCurrentProcess(), &hChildStdinWrDup, 0,
4970 FALSE,
4971 DUPLICATE_SAME_ACCESS);
4972 if (!fSuccess)
4973 return win32_error("DuplicateHandle", NULL);
4975 /* Close the inheritable version of ChildStdin
4976 that we're using. */
4977 CloseHandle(hChildStdinWr);
4979 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4980 return win32_error("CreatePipe", NULL);
4982 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
4983 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4984 FALSE, DUPLICATE_SAME_ACCESS);
4985 if (!fSuccess)
4986 return win32_error("DuplicateHandle", NULL);
4988 /* Close the inheritable version of ChildStdout
4989 that we're using. */
4990 CloseHandle(hChildStdoutRd);
4992 if (n != POPEN_4) {
4993 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4994 return win32_error("CreatePipe", NULL);
4995 fSuccess = DuplicateHandle(GetCurrentProcess(),
4996 hChildStderrRd,
4997 GetCurrentProcess(),
4998 &hChildStderrRdDup, 0,
4999 FALSE, DUPLICATE_SAME_ACCESS);
5000 if (!fSuccess)
5001 return win32_error("DuplicateHandle", NULL);
5002 /* Close the inheritable version of ChildStdErr that we're using. */
5003 CloseHandle(hChildStderrRd);
5006 switch (n) {
5007 case POPEN_1:
5008 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5009 case _O_WRONLY | _O_TEXT:
5010 /* Case for writing to child Stdin in text mode. */
5011 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5012 f1 = _fdopen(fd1, "w");
5013 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
5014 PyFile_SetBufSize(f, 0);
5015 /* We don't care about these pipes anymore, so close them. */
5016 CloseHandle(hChildStdoutRdDup);
5017 CloseHandle(hChildStderrRdDup);
5018 break;
5020 case _O_RDONLY | _O_TEXT:
5021 /* Case for reading from child Stdout in text mode. */
5022 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5023 f1 = _fdopen(fd1, "r");
5024 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
5025 PyFile_SetBufSize(f, 0);
5026 /* We don't care about these pipes anymore, so close them. */
5027 CloseHandle(hChildStdinWrDup);
5028 CloseHandle(hChildStderrRdDup);
5029 break;
5031 case _O_RDONLY | _O_BINARY:
5032 /* Case for readinig from child Stdout in binary mode. */
5033 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5034 f1 = _fdopen(fd1, "rb");
5035 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
5036 PyFile_SetBufSize(f, 0);
5037 /* We don't care about these pipes anymore, so close them. */
5038 CloseHandle(hChildStdinWrDup);
5039 CloseHandle(hChildStderrRdDup);
5040 break;
5042 case _O_WRONLY | _O_BINARY:
5043 /* Case for writing to child Stdin in binary mode. */
5044 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5045 f1 = _fdopen(fd1, "wb");
5046 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
5047 PyFile_SetBufSize(f, 0);
5048 /* We don't care about these pipes anymore, so close them. */
5049 CloseHandle(hChildStdoutRdDup);
5050 CloseHandle(hChildStderrRdDup);
5051 break;
5053 file_count = 1;
5054 break;
5056 case POPEN_2:
5057 case POPEN_4:
5059 char *m1, *m2;
5060 PyObject *p1, *p2;
5062 if (mode & _O_TEXT) {
5063 m1 = "r";
5064 m2 = "w";
5065 } else {
5066 m1 = "rb";
5067 m2 = "wb";
5070 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5071 f1 = _fdopen(fd1, m2);
5072 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5073 f2 = _fdopen(fd2, m1);
5074 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5075 PyFile_SetBufSize(p1, 0);
5076 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5077 PyFile_SetBufSize(p2, 0);
5079 if (n != 4)
5080 CloseHandle(hChildStderrRdDup);
5082 f = PyTuple_Pack(2,p1,p2);
5083 Py_XDECREF(p1);
5084 Py_XDECREF(p2);
5085 file_count = 2;
5086 break;
5089 case POPEN_3:
5091 char *m1, *m2;
5092 PyObject *p1, *p2, *p3;
5094 if (mode & _O_TEXT) {
5095 m1 = "r";
5096 m2 = "w";
5097 } else {
5098 m1 = "rb";
5099 m2 = "wb";
5102 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
5103 f1 = _fdopen(fd1, m2);
5104 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
5105 f2 = _fdopen(fd2, m1);
5106 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
5107 f3 = _fdopen(fd3, m1);
5108 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
5109 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5110 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
5111 PyFile_SetBufSize(p1, 0);
5112 PyFile_SetBufSize(p2, 0);
5113 PyFile_SetBufSize(p3, 0);
5114 f = PyTuple_Pack(3,p1,p2,p3);
5115 Py_XDECREF(p1);
5116 Py_XDECREF(p2);
5117 Py_XDECREF(p3);
5118 file_count = 3;
5119 break;
5123 if (n == POPEN_4) {
5124 if (!_PyPopenCreateProcess(cmdstring,
5125 hChildStdinRd,
5126 hChildStdoutWr,
5127 hChildStdoutWr,
5128 &hProcess))
5129 return NULL;
5131 else {
5132 if (!_PyPopenCreateProcess(cmdstring,
5133 hChildStdinRd,
5134 hChildStdoutWr,
5135 hChildStderrWr,
5136 &hProcess))
5137 return NULL;
5141 * Insert the files we've created into the process dictionary
5142 * all referencing the list with the process handle and the
5143 * initial number of files (see description below in _PyPclose).
5144 * Since if _PyPclose later tried to wait on a process when all
5145 * handles weren't closed, it could create a deadlock with the
5146 * child, we spend some energy here to try to ensure that we
5147 * either insert all file handles into the dictionary or none
5148 * at all. It's a little clumsy with the various popen modes
5149 * and variable number of files involved.
5151 if (!_PyPopenProcs) {
5152 _PyPopenProcs = PyDict_New();
5155 if (_PyPopenProcs) {
5156 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5157 int ins_rc[3];
5159 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5160 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5162 procObj = PyList_New(2);
5163 hProcessObj = PyLong_FromVoidPtr(hProcess);
5164 intObj = PyInt_FromLong(file_count);
5166 if (procObj && hProcessObj && intObj) {
5167 PyList_SetItem(procObj,0,hProcessObj);
5168 PyList_SetItem(procObj,1,intObj);
5170 fileObj[0] = PyLong_FromVoidPtr(f1);
5171 if (fileObj[0]) {
5172 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5173 fileObj[0],
5174 procObj);
5176 if (file_count >= 2) {
5177 fileObj[1] = PyLong_FromVoidPtr(f2);
5178 if (fileObj[1]) {
5179 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5180 fileObj[1],
5181 procObj);
5184 if (file_count >= 3) {
5185 fileObj[2] = PyLong_FromVoidPtr(f3);
5186 if (fileObj[2]) {
5187 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5188 fileObj[2],
5189 procObj);
5193 if (ins_rc[0] < 0 || !fileObj[0] ||
5194 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5195 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5196 /* Something failed - remove any dictionary
5197 * entries that did make it.
5199 if (!ins_rc[0] && fileObj[0]) {
5200 PyDict_DelItem(_PyPopenProcs,
5201 fileObj[0]);
5203 if (!ins_rc[1] && fileObj[1]) {
5204 PyDict_DelItem(_PyPopenProcs,
5205 fileObj[1]);
5207 if (!ins_rc[2] && fileObj[2]) {
5208 PyDict_DelItem(_PyPopenProcs,
5209 fileObj[2]);
5215 * Clean up our localized references for the dictionary keys
5216 * and value since PyDict_SetItem will Py_INCREF any copies
5217 * that got placed in the dictionary.
5219 Py_XDECREF(procObj);
5220 Py_XDECREF(fileObj[0]);
5221 Py_XDECREF(fileObj[1]);
5222 Py_XDECREF(fileObj[2]);
5225 /* Child is launched. Close the parents copy of those pipe
5226 * handles that only the child should have open. You need to
5227 * make sure that no handles to the write end of the output pipe
5228 * are maintained in this process or else the pipe will not close
5229 * when the child process exits and the ReadFile will hang. */
5231 if (!CloseHandle(hChildStdinRd))
5232 return win32_error("CloseHandle", NULL);
5234 if (!CloseHandle(hChildStdoutWr))
5235 return win32_error("CloseHandle", NULL);
5237 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5238 return win32_error("CloseHandle", NULL);
5240 return f;
5244 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5245 * exit code for the child process and return as a result of the close.
5247 * This function uses the _PyPopenProcs dictionary in order to map the
5248 * input file pointer to information about the process that was
5249 * originally created by the popen* call that created the file pointer.
5250 * The dictionary uses the file pointer as a key (with one entry
5251 * inserted for each file returned by the original popen* call) and a
5252 * single list object as the value for all files from a single call.
5253 * The list object contains the Win32 process handle at [0], and a file
5254 * count at [1], which is initialized to the total number of file
5255 * handles using that list.
5257 * This function closes whichever handle it is passed, and decrements
5258 * the file count in the dictionary for the process handle pointed to
5259 * by this file. On the last close (when the file count reaches zero),
5260 * this function will wait for the child process and then return its
5261 * exit code as the result of the close() operation. This permits the
5262 * files to be closed in any order - it is always the close() of the
5263 * final handle that will return the exit code.
5265 * NOTE: This function is currently called with the GIL released.
5266 * hence we use the GILState API to manage our state.
5269 static int _PyPclose(FILE *file)
5271 int result;
5272 DWORD exit_code;
5273 HANDLE hProcess;
5274 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5275 long file_count;
5276 #ifdef WITH_THREAD
5277 PyGILState_STATE state;
5278 #endif
5280 /* Close the file handle first, to ensure it can't block the
5281 * child from exiting if it's the last handle.
5283 result = fclose(file);
5284 #ifdef WITH_THREAD
5285 state = PyGILState_Ensure();
5286 #endif
5287 if (_PyPopenProcs) {
5288 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5289 (procObj = PyDict_GetItem(_PyPopenProcs,
5290 fileObj)) != NULL &&
5291 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5292 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5294 hProcess = PyLong_AsVoidPtr(hProcessObj);
5295 file_count = PyInt_AsLong(intObj);
5297 if (file_count > 1) {
5298 /* Still other files referencing process */
5299 file_count--;
5300 PyList_SetItem(procObj,1,
5301 PyInt_FromLong(file_count));
5302 } else {
5303 /* Last file for this process */
5304 if (result != EOF &&
5305 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5306 GetExitCodeProcess(hProcess, &exit_code)) {
5307 /* Possible truncation here in 16-bit environments, but
5308 * real exit codes are just the lower byte in any event.
5310 result = exit_code;
5311 } else {
5312 /* Indicate failure - this will cause the file object
5313 * to raise an I/O error and translate the last Win32
5314 * error code from errno. We do have a problem with
5315 * last errors that overlap the normal errno table,
5316 * but that's a consistent problem with the file object.
5318 if (result != EOF) {
5319 /* If the error wasn't from the fclose(), then
5320 * set errno for the file object error handling.
5322 errno = GetLastError();
5324 result = -1;
5327 /* Free up the native handle at this point */
5328 CloseHandle(hProcess);
5331 /* Remove this file pointer from dictionary */
5332 PyDict_DelItem(_PyPopenProcs, fileObj);
5334 if (PyDict_Size(_PyPopenProcs) == 0) {
5335 Py_DECREF(_PyPopenProcs);
5336 _PyPopenProcs = NULL;
5339 } /* if object retrieval ok */
5341 Py_XDECREF(fileObj);
5342 } /* if _PyPopenProcs */
5344 #ifdef WITH_THREAD
5345 PyGILState_Release(state);
5346 #endif
5347 return result;
5350 #else /* which OS? */
5351 static PyObject *
5352 posix_popen(PyObject *self, PyObject *args)
5354 char *name;
5355 char *mode = "r";
5356 int bufsize = -1;
5357 FILE *fp;
5358 PyObject *f;
5359 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
5360 return NULL;
5361 /* Strip mode of binary or text modifiers */
5362 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5363 mode = "r";
5364 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5365 mode = "w";
5366 Py_BEGIN_ALLOW_THREADS
5367 fp = popen(name, mode);
5368 Py_END_ALLOW_THREADS
5369 if (fp == NULL)
5370 return posix_error();
5371 f = PyFile_FromFile(fp, name, mode, pclose);
5372 if (f != NULL)
5373 PyFile_SetBufSize(f, bufsize);
5374 return f;
5377 #endif /* PYOS_??? */
5378 #endif /* HAVE_POPEN */
5381 #ifdef HAVE_SETUID
5382 PyDoc_STRVAR(posix_setuid__doc__,
5383 "setuid(uid)\n\n\
5384 Set the current process's user id.");
5386 static PyObject *
5387 posix_setuid(PyObject *self, PyObject *args)
5389 int uid;
5390 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
5391 return NULL;
5392 if (setuid(uid) < 0)
5393 return posix_error();
5394 Py_INCREF(Py_None);
5395 return Py_None;
5397 #endif /* HAVE_SETUID */
5400 #ifdef HAVE_SETEUID
5401 PyDoc_STRVAR(posix_seteuid__doc__,
5402 "seteuid(uid)\n\n\
5403 Set the current process's effective user id.");
5405 static PyObject *
5406 posix_seteuid (PyObject *self, PyObject *args)
5408 int euid;
5409 if (!PyArg_ParseTuple(args, "i", &euid)) {
5410 return NULL;
5411 } else if (seteuid(euid) < 0) {
5412 return posix_error();
5413 } else {
5414 Py_INCREF(Py_None);
5415 return Py_None;
5418 #endif /* HAVE_SETEUID */
5420 #ifdef HAVE_SETEGID
5421 PyDoc_STRVAR(posix_setegid__doc__,
5422 "setegid(gid)\n\n\
5423 Set the current process's effective group id.");
5425 static PyObject *
5426 posix_setegid (PyObject *self, PyObject *args)
5428 int egid;
5429 if (!PyArg_ParseTuple(args, "i", &egid)) {
5430 return NULL;
5431 } else if (setegid(egid) < 0) {
5432 return posix_error();
5433 } else {
5434 Py_INCREF(Py_None);
5435 return Py_None;
5438 #endif /* HAVE_SETEGID */
5440 #ifdef HAVE_SETREUID
5441 PyDoc_STRVAR(posix_setreuid__doc__,
5442 "setreuid(ruid, euid)\n\n\
5443 Set the current process's real and effective user ids.");
5445 static PyObject *
5446 posix_setreuid (PyObject *self, PyObject *args)
5448 int ruid, euid;
5449 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5450 return NULL;
5451 } else if (setreuid(ruid, euid) < 0) {
5452 return posix_error();
5453 } else {
5454 Py_INCREF(Py_None);
5455 return Py_None;
5458 #endif /* HAVE_SETREUID */
5460 #ifdef HAVE_SETREGID
5461 PyDoc_STRVAR(posix_setregid__doc__,
5462 "setregid(rgid, egid)\n\n\
5463 Set the current process's real and effective group ids.");
5465 static PyObject *
5466 posix_setregid (PyObject *self, PyObject *args)
5468 int rgid, egid;
5469 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5470 return NULL;
5471 } else if (setregid(rgid, egid) < 0) {
5472 return posix_error();
5473 } else {
5474 Py_INCREF(Py_None);
5475 return Py_None;
5478 #endif /* HAVE_SETREGID */
5480 #ifdef HAVE_SETGID
5481 PyDoc_STRVAR(posix_setgid__doc__,
5482 "setgid(gid)\n\n\
5483 Set the current process's group id.");
5485 static PyObject *
5486 posix_setgid(PyObject *self, PyObject *args)
5488 int gid;
5489 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
5490 return NULL;
5491 if (setgid(gid) < 0)
5492 return posix_error();
5493 Py_INCREF(Py_None);
5494 return Py_None;
5496 #endif /* HAVE_SETGID */
5498 #ifdef HAVE_SETGROUPS
5499 PyDoc_STRVAR(posix_setgroups__doc__,
5500 "setgroups(list)\n\n\
5501 Set the groups of the current process to list.");
5503 static PyObject *
5504 posix_setgroups(PyObject *self, PyObject *groups)
5506 int i, len;
5507 gid_t grouplist[MAX_GROUPS];
5509 if (!PySequence_Check(groups)) {
5510 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5511 return NULL;
5513 len = PySequence_Size(groups);
5514 if (len > MAX_GROUPS) {
5515 PyErr_SetString(PyExc_ValueError, "too many groups");
5516 return NULL;
5518 for(i = 0; i < len; i++) {
5519 PyObject *elem;
5520 elem = PySequence_GetItem(groups, i);
5521 if (!elem)
5522 return NULL;
5523 if (!PyInt_Check(elem)) {
5524 if (!PyLong_Check(elem)) {
5525 PyErr_SetString(PyExc_TypeError,
5526 "groups must be integers");
5527 Py_DECREF(elem);
5528 return NULL;
5529 } else {
5530 unsigned long x = PyLong_AsUnsignedLong(elem);
5531 if (PyErr_Occurred()) {
5532 PyErr_SetString(PyExc_TypeError,
5533 "group id too big");
5534 Py_DECREF(elem);
5535 return NULL;
5537 grouplist[i] = x;
5538 /* read back the value to see if it fitted in gid_t */
5539 if (grouplist[i] != x) {
5540 PyErr_SetString(PyExc_TypeError,
5541 "group id too big");
5542 Py_DECREF(elem);
5543 return NULL;
5546 } else {
5547 long x = PyInt_AsLong(elem);
5548 grouplist[i] = x;
5549 if (grouplist[i] != x) {
5550 PyErr_SetString(PyExc_TypeError,
5551 "group id too big");
5552 Py_DECREF(elem);
5553 return NULL;
5556 Py_DECREF(elem);
5559 if (setgroups(len, grouplist) < 0)
5560 return posix_error();
5561 Py_INCREF(Py_None);
5562 return Py_None;
5564 #endif /* HAVE_SETGROUPS */
5566 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5567 static PyObject *
5568 wait_helper(int pid, int status, struct rusage *ru)
5570 PyObject *result;
5571 static PyObject *struct_rusage;
5573 if (pid == -1)
5574 return posix_error();
5576 if (struct_rusage == NULL) {
5577 PyObject *m = PyImport_ImportModule("resource");
5578 if (m == NULL)
5579 return NULL;
5580 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5581 Py_DECREF(m);
5582 if (struct_rusage == NULL)
5583 return NULL;
5586 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5587 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5588 if (!result)
5589 return NULL;
5591 #ifndef doubletime
5592 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5593 #endif
5595 PyStructSequence_SET_ITEM(result, 0,
5596 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5597 PyStructSequence_SET_ITEM(result, 1,
5598 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5599 #define SET_INT(result, index, value)\
5600 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5601 SET_INT(result, 2, ru->ru_maxrss);
5602 SET_INT(result, 3, ru->ru_ixrss);
5603 SET_INT(result, 4, ru->ru_idrss);
5604 SET_INT(result, 5, ru->ru_isrss);
5605 SET_INT(result, 6, ru->ru_minflt);
5606 SET_INT(result, 7, ru->ru_majflt);
5607 SET_INT(result, 8, ru->ru_nswap);
5608 SET_INT(result, 9, ru->ru_inblock);
5609 SET_INT(result, 10, ru->ru_oublock);
5610 SET_INT(result, 11, ru->ru_msgsnd);
5611 SET_INT(result, 12, ru->ru_msgrcv);
5612 SET_INT(result, 13, ru->ru_nsignals);
5613 SET_INT(result, 14, ru->ru_nvcsw);
5614 SET_INT(result, 15, ru->ru_nivcsw);
5615 #undef SET_INT
5617 if (PyErr_Occurred()) {
5618 Py_DECREF(result);
5619 return NULL;
5622 return Py_BuildValue("iiN", pid, status, result);
5624 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5626 #ifdef HAVE_WAIT3
5627 PyDoc_STRVAR(posix_wait3__doc__,
5628 "wait3(options) -> (pid, status, rusage)\n\n\
5629 Wait for completion of a child process.");
5631 static PyObject *
5632 posix_wait3(PyObject *self, PyObject *args)
5634 int pid, options;
5635 struct rusage ru;
5636 WAIT_TYPE status;
5637 WAIT_STATUS_INT(status) = 0;
5639 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5640 return NULL;
5642 Py_BEGIN_ALLOW_THREADS
5643 pid = wait3(&status, options, &ru);
5644 Py_END_ALLOW_THREADS
5646 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5648 #endif /* HAVE_WAIT3 */
5650 #ifdef HAVE_WAIT4
5651 PyDoc_STRVAR(posix_wait4__doc__,
5652 "wait4(pid, options) -> (pid, status, rusage)\n\n\
5653 Wait for completion of a given child process.");
5655 static PyObject *
5656 posix_wait4(PyObject *self, PyObject *args)
5658 int pid, options;
5659 struct rusage ru;
5660 WAIT_TYPE status;
5661 WAIT_STATUS_INT(status) = 0;
5663 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5664 return NULL;
5666 Py_BEGIN_ALLOW_THREADS
5667 pid = wait4(pid, &status, options, &ru);
5668 Py_END_ALLOW_THREADS
5670 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
5672 #endif /* HAVE_WAIT4 */
5674 #ifdef HAVE_WAITPID
5675 PyDoc_STRVAR(posix_waitpid__doc__,
5676 "waitpid(pid, options) -> (pid, status)\n\n\
5677 Wait for completion of a given child process.");
5679 static PyObject *
5680 posix_waitpid(PyObject *self, PyObject *args)
5682 int pid, options;
5683 WAIT_TYPE status;
5684 WAIT_STATUS_INT(status) = 0;
5686 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5687 return NULL;
5688 Py_BEGIN_ALLOW_THREADS
5689 pid = waitpid(pid, &status, options);
5690 Py_END_ALLOW_THREADS
5691 if (pid == -1)
5692 return posix_error();
5694 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
5697 #elif defined(HAVE_CWAIT)
5699 /* MS C has a variant of waitpid() that's usable for most purposes. */
5700 PyDoc_STRVAR(posix_waitpid__doc__,
5701 "waitpid(pid, options) -> (pid, status << 8)\n\n"
5702 "Wait for completion of a given process. options is ignored on Windows.");
5704 static PyObject *
5705 posix_waitpid(PyObject *self, PyObject *args)
5707 Py_intptr_t pid;
5708 int status, options;
5710 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5711 return NULL;
5712 Py_BEGIN_ALLOW_THREADS
5713 pid = _cwait(&status, pid, options);
5714 Py_END_ALLOW_THREADS
5715 if (pid == -1)
5716 return posix_error();
5718 /* shift the status left a byte so this is more like the POSIX waitpid */
5719 return Py_BuildValue("ii", pid, status << 8);
5721 #endif /* HAVE_WAITPID || HAVE_CWAIT */
5723 #ifdef HAVE_WAIT
5724 PyDoc_STRVAR(posix_wait__doc__,
5725 "wait() -> (pid, status)\n\n\
5726 Wait for completion of a child process.");
5728 static PyObject *
5729 posix_wait(PyObject *self, PyObject *noargs)
5731 int pid;
5732 WAIT_TYPE status;
5733 WAIT_STATUS_INT(status) = 0;
5735 Py_BEGIN_ALLOW_THREADS
5736 pid = wait(&status);
5737 Py_END_ALLOW_THREADS
5738 if (pid == -1)
5739 return posix_error();
5741 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
5743 #endif
5746 PyDoc_STRVAR(posix_lstat__doc__,
5747 "lstat(path) -> stat result\n\n\
5748 Like stat(path), but do not follow symbolic links.");
5750 static PyObject *
5751 posix_lstat(PyObject *self, PyObject *args)
5753 #ifdef HAVE_LSTAT
5754 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
5755 #else /* !HAVE_LSTAT */
5756 #ifdef MS_WINDOWS
5757 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
5758 #else
5759 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5760 #endif
5761 #endif /* !HAVE_LSTAT */
5765 #ifdef HAVE_READLINK
5766 PyDoc_STRVAR(posix_readlink__doc__,
5767 "readlink(path) -> path\n\n\
5768 Return a string representing the path to which the symbolic link points.");
5770 static PyObject *
5771 posix_readlink(PyObject *self, PyObject *args)
5773 PyObject* v;
5774 char buf[MAXPATHLEN];
5775 char *path;
5776 int n;
5777 #ifdef Py_USING_UNICODE
5778 int arg_is_unicode = 0;
5779 #endif
5781 if (!PyArg_ParseTuple(args, "et:readlink",
5782 Py_FileSystemDefaultEncoding, &path))
5783 return NULL;
5784 #ifdef Py_USING_UNICODE
5785 v = PySequence_GetItem(args, 0);
5786 if (v == NULL) {
5787 PyMem_Free(path);
5788 return NULL;
5791 if (PyUnicode_Check(v)) {
5792 arg_is_unicode = 1;
5794 Py_DECREF(v);
5795 #endif
5797 Py_BEGIN_ALLOW_THREADS
5798 n = readlink(path, buf, (int) sizeof buf);
5799 Py_END_ALLOW_THREADS
5800 if (n < 0)
5801 return posix_error_with_allocated_filename(path);
5803 PyMem_Free(path);
5804 v = PyString_FromStringAndSize(buf, n);
5805 #ifdef Py_USING_UNICODE
5806 if (arg_is_unicode) {
5807 PyObject *w;
5809 w = PyUnicode_FromEncodedObject(v,
5810 Py_FileSystemDefaultEncoding,
5811 "strict");
5812 if (w != NULL) {
5813 Py_DECREF(v);
5814 v = w;
5816 else {
5817 /* fall back to the original byte string, as
5818 discussed in patch #683592 */
5819 PyErr_Clear();
5822 #endif
5823 return v;
5825 #endif /* HAVE_READLINK */
5828 #ifdef HAVE_SYMLINK
5829 PyDoc_STRVAR(posix_symlink__doc__,
5830 "symlink(src, dst)\n\n\
5831 Create a symbolic link pointing to src named dst.");
5833 static PyObject *
5834 posix_symlink(PyObject *self, PyObject *args)
5836 return posix_2str(args, "etet:symlink", symlink);
5838 #endif /* HAVE_SYMLINK */
5841 #ifdef HAVE_TIMES
5842 #ifndef HZ
5843 #define HZ 60 /* Universal constant :-) */
5844 #endif /* HZ */
5846 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
5847 static long
5848 system_uptime(void)
5850 ULONG value = 0;
5852 Py_BEGIN_ALLOW_THREADS
5853 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5854 Py_END_ALLOW_THREADS
5856 return value;
5859 static PyObject *
5860 posix_times(PyObject *self, PyObject *noargs)
5862 /* Currently Only Uptime is Provided -- Others Later */
5863 return Py_BuildValue("ddddd",
5864 (double)0 /* t.tms_utime / HZ */,
5865 (double)0 /* t.tms_stime / HZ */,
5866 (double)0 /* t.tms_cutime / HZ */,
5867 (double)0 /* t.tms_cstime / HZ */,
5868 (double)system_uptime() / 1000);
5870 #else /* not OS2 */
5871 static PyObject *
5872 posix_times(PyObject *self, PyObject *noargs)
5874 struct tms t;
5875 clock_t c;
5876 errno = 0;
5877 c = times(&t);
5878 if (c == (clock_t) -1)
5879 return posix_error();
5880 return Py_BuildValue("ddddd",
5881 (double)t.tms_utime / HZ,
5882 (double)t.tms_stime / HZ,
5883 (double)t.tms_cutime / HZ,
5884 (double)t.tms_cstime / HZ,
5885 (double)c / HZ);
5887 #endif /* not OS2 */
5888 #endif /* HAVE_TIMES */
5891 #ifdef MS_WINDOWS
5892 #define HAVE_TIMES /* so the method table will pick it up */
5893 static PyObject *
5894 posix_times(PyObject *self, PyObject *noargs)
5896 FILETIME create, exit, kernel, user;
5897 HANDLE hProc;
5898 hProc = GetCurrentProcess();
5899 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5900 /* The fields of a FILETIME structure are the hi and lo part
5901 of a 64-bit value expressed in 100 nanosecond units.
5902 1e7 is one second in such units; 1e-7 the inverse.
5903 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5905 return Py_BuildValue(
5906 "ddddd",
5907 (double)(kernel.dwHighDateTime*429.4967296 +
5908 kernel.dwLowDateTime*1e-7),
5909 (double)(user.dwHighDateTime*429.4967296 +
5910 user.dwLowDateTime*1e-7),
5911 (double)0,
5912 (double)0,
5913 (double)0);
5915 #endif /* MS_WINDOWS */
5917 #ifdef HAVE_TIMES
5918 PyDoc_STRVAR(posix_times__doc__,
5919 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
5920 Return a tuple of floating point numbers indicating process times.");
5921 #endif
5924 #ifdef HAVE_GETSID
5925 PyDoc_STRVAR(posix_getsid__doc__,
5926 "getsid(pid) -> sid\n\n\
5927 Call the system call getsid().");
5929 static PyObject *
5930 posix_getsid(PyObject *self, PyObject *args)
5932 int pid, sid;
5933 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5934 return NULL;
5935 sid = getsid(pid);
5936 if (sid < 0)
5937 return posix_error();
5938 return PyInt_FromLong((long)sid);
5940 #endif /* HAVE_GETSID */
5943 #ifdef HAVE_SETSID
5944 PyDoc_STRVAR(posix_setsid__doc__,
5945 "setsid()\n\n\
5946 Call the system call setsid().");
5948 static PyObject *
5949 posix_setsid(PyObject *self, PyObject *noargs)
5951 if (setsid() < 0)
5952 return posix_error();
5953 Py_INCREF(Py_None);
5954 return Py_None;
5956 #endif /* HAVE_SETSID */
5958 #ifdef HAVE_SETPGID
5959 PyDoc_STRVAR(posix_setpgid__doc__,
5960 "setpgid(pid, pgrp)\n\n\
5961 Call the system call setpgid().");
5963 static PyObject *
5964 posix_setpgid(PyObject *self, PyObject *args)
5966 int pid, pgrp;
5967 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
5968 return NULL;
5969 if (setpgid(pid, pgrp) < 0)
5970 return posix_error();
5971 Py_INCREF(Py_None);
5972 return Py_None;
5974 #endif /* HAVE_SETPGID */
5977 #ifdef HAVE_TCGETPGRP
5978 PyDoc_STRVAR(posix_tcgetpgrp__doc__,
5979 "tcgetpgrp(fd) -> pgid\n\n\
5980 Return the process group associated with the terminal given by a fd.");
5982 static PyObject *
5983 posix_tcgetpgrp(PyObject *self, PyObject *args)
5985 int fd, pgid;
5986 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5987 return NULL;
5988 pgid = tcgetpgrp(fd);
5989 if (pgid < 0)
5990 return posix_error();
5991 return PyInt_FromLong((long)pgid);
5993 #endif /* HAVE_TCGETPGRP */
5996 #ifdef HAVE_TCSETPGRP
5997 PyDoc_STRVAR(posix_tcsetpgrp__doc__,
5998 "tcsetpgrp(fd, pgid)\n\n\
5999 Set the process group associated with the terminal given by a fd.");
6001 static PyObject *
6002 posix_tcsetpgrp(PyObject *self, PyObject *args)
6004 int fd, pgid;
6005 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
6006 return NULL;
6007 if (tcsetpgrp(fd, pgid) < 0)
6008 return posix_error();
6009 Py_INCREF(Py_None);
6010 return Py_None;
6012 #endif /* HAVE_TCSETPGRP */
6014 /* Functions acting on file descriptors */
6016 PyDoc_STRVAR(posix_open__doc__,
6017 "open(filename, flag [, mode=0777]) -> fd\n\n\
6018 Open a file (for low level IO).");
6020 static PyObject *
6021 posix_open(PyObject *self, PyObject *args)
6023 char *file = NULL;
6024 int flag;
6025 int mode = 0777;
6026 int fd;
6028 #ifdef MS_WINDOWS
6029 if (unicode_file_names()) {
6030 PyUnicodeObject *po;
6031 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6032 Py_BEGIN_ALLOW_THREADS
6033 /* PyUnicode_AS_UNICODE OK without thread
6034 lock as it is a simple dereference. */
6035 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6036 Py_END_ALLOW_THREADS
6037 if (fd < 0)
6038 return posix_error();
6039 return PyInt_FromLong((long)fd);
6041 /* Drop the argument parsing error as narrow strings
6042 are also valid. */
6043 PyErr_Clear();
6045 #endif
6047 if (!PyArg_ParseTuple(args, "eti|i",
6048 Py_FileSystemDefaultEncoding, &file,
6049 &flag, &mode))
6050 return NULL;
6052 Py_BEGIN_ALLOW_THREADS
6053 fd = open(file, flag, mode);
6054 Py_END_ALLOW_THREADS
6055 if (fd < 0)
6056 return posix_error_with_allocated_filename(file);
6057 PyMem_Free(file);
6058 return PyInt_FromLong((long)fd);
6062 PyDoc_STRVAR(posix_close__doc__,
6063 "close(fd)\n\n\
6064 Close a file descriptor (for low level IO).");
6066 static PyObject *
6067 posix_close(PyObject *self, PyObject *args)
6069 int fd, res;
6070 if (!PyArg_ParseTuple(args, "i:close", &fd))
6071 return NULL;
6072 Py_BEGIN_ALLOW_THREADS
6073 res = close(fd);
6074 Py_END_ALLOW_THREADS
6075 if (res < 0)
6076 return posix_error();
6077 Py_INCREF(Py_None);
6078 return Py_None;
6082 PyDoc_STRVAR(posix_dup__doc__,
6083 "dup(fd) -> fd2\n\n\
6084 Return a duplicate of a file descriptor.");
6086 static PyObject *
6087 posix_dup(PyObject *self, PyObject *args)
6089 int fd;
6090 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6091 return NULL;
6092 Py_BEGIN_ALLOW_THREADS
6093 fd = dup(fd);
6094 Py_END_ALLOW_THREADS
6095 if (fd < 0)
6096 return posix_error();
6097 return PyInt_FromLong((long)fd);
6101 PyDoc_STRVAR(posix_dup2__doc__,
6102 "dup2(old_fd, new_fd)\n\n\
6103 Duplicate file descriptor.");
6105 static PyObject *
6106 posix_dup2(PyObject *self, PyObject *args)
6108 int fd, fd2, res;
6109 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6110 return NULL;
6111 Py_BEGIN_ALLOW_THREADS
6112 res = dup2(fd, fd2);
6113 Py_END_ALLOW_THREADS
6114 if (res < 0)
6115 return posix_error();
6116 Py_INCREF(Py_None);
6117 return Py_None;
6121 PyDoc_STRVAR(posix_lseek__doc__,
6122 "lseek(fd, pos, how) -> newpos\n\n\
6123 Set the current position of a file descriptor.");
6125 static PyObject *
6126 posix_lseek(PyObject *self, PyObject *args)
6128 int fd, how;
6129 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6130 PY_LONG_LONG pos, res;
6131 #else
6132 off_t pos, res;
6133 #endif
6134 PyObject *posobj;
6135 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6136 return NULL;
6137 #ifdef SEEK_SET
6138 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6139 switch (how) {
6140 case 0: how = SEEK_SET; break;
6141 case 1: how = SEEK_CUR; break;
6142 case 2: how = SEEK_END; break;
6144 #endif /* SEEK_END */
6146 #if !defined(HAVE_LARGEFILE_SUPPORT)
6147 pos = PyInt_AsLong(posobj);
6148 #else
6149 pos = PyLong_Check(posobj) ?
6150 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6151 #endif
6152 if (PyErr_Occurred())
6153 return NULL;
6155 Py_BEGIN_ALLOW_THREADS
6156 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6157 res = _lseeki64(fd, pos, how);
6158 #else
6159 res = lseek(fd, pos, how);
6160 #endif
6161 Py_END_ALLOW_THREADS
6162 if (res < 0)
6163 return posix_error();
6165 #if !defined(HAVE_LARGEFILE_SUPPORT)
6166 return PyInt_FromLong(res);
6167 #else
6168 return PyLong_FromLongLong(res);
6169 #endif
6173 PyDoc_STRVAR(posix_read__doc__,
6174 "read(fd, buffersize) -> string\n\n\
6175 Read a file descriptor.");
6177 static PyObject *
6178 posix_read(PyObject *self, PyObject *args)
6180 int fd, size, n;
6181 PyObject *buffer;
6182 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6183 return NULL;
6184 if (size < 0) {
6185 errno = EINVAL;
6186 return posix_error();
6188 buffer = PyString_FromStringAndSize((char *)NULL, size);
6189 if (buffer == NULL)
6190 return NULL;
6191 Py_BEGIN_ALLOW_THREADS
6192 n = read(fd, PyString_AsString(buffer), size);
6193 Py_END_ALLOW_THREADS
6194 if (n < 0) {
6195 Py_DECREF(buffer);
6196 return posix_error();
6198 if (n != size)
6199 _PyString_Resize(&buffer, n);
6200 return buffer;
6204 PyDoc_STRVAR(posix_write__doc__,
6205 "write(fd, string) -> byteswritten\n\n\
6206 Write a string to a file descriptor.");
6208 static PyObject *
6209 posix_write(PyObject *self, PyObject *args)
6211 int fd;
6212 Py_ssize_t size;
6213 char *buffer;
6215 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
6216 return NULL;
6217 Py_BEGIN_ALLOW_THREADS
6218 size = write(fd, buffer, (size_t)size);
6219 Py_END_ALLOW_THREADS
6220 if (size < 0)
6221 return posix_error();
6222 return PyInt_FromSsize_t(size);
6226 PyDoc_STRVAR(posix_fstat__doc__,
6227 "fstat(fd) -> stat result\n\n\
6228 Like stat(), but for an open file descriptor.");
6230 static PyObject *
6231 posix_fstat(PyObject *self, PyObject *args)
6233 int fd;
6234 STRUCT_STAT st;
6235 int res;
6236 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6237 return NULL;
6238 #ifdef __VMS
6239 /* on OpenVMS we must ensure that all bytes are written to the file */
6240 fsync(fd);
6241 #endif
6242 Py_BEGIN_ALLOW_THREADS
6243 res = FSTAT(fd, &st);
6244 Py_END_ALLOW_THREADS
6245 if (res != 0) {
6246 #ifdef MS_WINDOWS
6247 return win32_error("fstat", NULL);
6248 #else
6249 return posix_error();
6250 #endif
6253 return _pystat_fromstructstat(&st);
6257 PyDoc_STRVAR(posix_fdopen__doc__,
6258 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
6259 Return an open file object connected to a file descriptor.");
6261 static PyObject *
6262 posix_fdopen(PyObject *self, PyObject *args)
6264 int fd;
6265 char *orgmode = "r";
6266 int bufsize = -1;
6267 FILE *fp;
6268 PyObject *f;
6269 char *mode;
6270 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6271 return NULL;
6273 /* Sanitize mode. See fileobject.c */
6274 mode = PyMem_MALLOC(strlen(orgmode)+3);
6275 if (!mode) {
6276 PyErr_NoMemory();
6277 return NULL;
6279 strcpy(mode, orgmode);
6280 if (_PyFile_SanitizeMode(mode)) {
6281 PyMem_FREE(mode);
6282 return NULL;
6284 Py_BEGIN_ALLOW_THREADS
6285 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6286 if (mode[0] == 'a') {
6287 /* try to make sure the O_APPEND flag is set */
6288 int flags;
6289 flags = fcntl(fd, F_GETFL);
6290 if (flags != -1)
6291 fcntl(fd, F_SETFL, flags | O_APPEND);
6292 fp = fdopen(fd, mode);
6293 if (fp == NULL && flags != -1)
6294 /* restore old mode if fdopen failed */
6295 fcntl(fd, F_SETFL, flags);
6296 } else {
6297 fp = fdopen(fd, mode);
6299 #else
6300 fp = fdopen(fd, mode);
6301 #endif
6302 Py_END_ALLOW_THREADS
6303 PyMem_FREE(mode);
6304 if (fp == NULL)
6305 return posix_error();
6306 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6307 if (f != NULL)
6308 PyFile_SetBufSize(f, bufsize);
6309 return f;
6312 PyDoc_STRVAR(posix_isatty__doc__,
6313 "isatty(fd) -> bool\n\n\
6314 Return True if the file descriptor 'fd' is an open file descriptor\n\
6315 connected to the slave end of a terminal.");
6317 static PyObject *
6318 posix_isatty(PyObject *self, PyObject *args)
6320 int fd;
6321 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6322 return NULL;
6323 return PyBool_FromLong(isatty(fd));
6326 #ifdef HAVE_PIPE
6327 PyDoc_STRVAR(posix_pipe__doc__,
6328 "pipe() -> (read_end, write_end)\n\n\
6329 Create a pipe.");
6331 static PyObject *
6332 posix_pipe(PyObject *self, PyObject *noargs)
6334 #if defined(PYOS_OS2)
6335 HFILE read, write;
6336 APIRET rc;
6338 Py_BEGIN_ALLOW_THREADS
6339 rc = DosCreatePipe( &read, &write, 4096);
6340 Py_END_ALLOW_THREADS
6341 if (rc != NO_ERROR)
6342 return os2_error(rc);
6344 return Py_BuildValue("(ii)", read, write);
6345 #else
6346 #if !defined(MS_WINDOWS)
6347 int fds[2];
6348 int res;
6349 Py_BEGIN_ALLOW_THREADS
6350 res = pipe(fds);
6351 Py_END_ALLOW_THREADS
6352 if (res != 0)
6353 return posix_error();
6354 return Py_BuildValue("(ii)", fds[0], fds[1]);
6355 #else /* MS_WINDOWS */
6356 HANDLE read, write;
6357 int read_fd, write_fd;
6358 BOOL ok;
6359 Py_BEGIN_ALLOW_THREADS
6360 ok = CreatePipe(&read, &write, NULL, 0);
6361 Py_END_ALLOW_THREADS
6362 if (!ok)
6363 return win32_error("CreatePipe", NULL);
6364 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6365 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6366 return Py_BuildValue("(ii)", read_fd, write_fd);
6367 #endif /* MS_WINDOWS */
6368 #endif
6370 #endif /* HAVE_PIPE */
6373 #ifdef HAVE_MKFIFO
6374 PyDoc_STRVAR(posix_mkfifo__doc__,
6375 "mkfifo(filename [, mode=0666])\n\n\
6376 Create a FIFO (a POSIX named pipe).");
6378 static PyObject *
6379 posix_mkfifo(PyObject *self, PyObject *args)
6381 char *filename;
6382 int mode = 0666;
6383 int res;
6384 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6385 return NULL;
6386 Py_BEGIN_ALLOW_THREADS
6387 res = mkfifo(filename, mode);
6388 Py_END_ALLOW_THREADS
6389 if (res < 0)
6390 return posix_error();
6391 Py_INCREF(Py_None);
6392 return Py_None;
6394 #endif
6397 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
6398 PyDoc_STRVAR(posix_mknod__doc__,
6399 "mknod(filename [, mode=0600, device])\n\n\
6400 Create a filesystem node (file, device special file or named pipe)\n\
6401 named filename. mode specifies both the permissions to use and the\n\
6402 type of node to be created, being combined (bitwise OR) with one of\n\
6403 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
6404 device defines the newly created device special file (probably using\n\
6405 os.makedev()), otherwise it is ignored.");
6408 static PyObject *
6409 posix_mknod(PyObject *self, PyObject *args)
6411 char *filename;
6412 int mode = 0600;
6413 int device = 0;
6414 int res;
6415 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6416 return NULL;
6417 Py_BEGIN_ALLOW_THREADS
6418 res = mknod(filename, mode, device);
6419 Py_END_ALLOW_THREADS
6420 if (res < 0)
6421 return posix_error();
6422 Py_INCREF(Py_None);
6423 return Py_None;
6425 #endif
6427 #ifdef HAVE_DEVICE_MACROS
6428 PyDoc_STRVAR(posix_major__doc__,
6429 "major(device) -> major number\n\
6430 Extracts a device major number from a raw device number.");
6432 static PyObject *
6433 posix_major(PyObject *self, PyObject *args)
6435 int device;
6436 if (!PyArg_ParseTuple(args, "i:major", &device))
6437 return NULL;
6438 return PyInt_FromLong((long)major(device));
6441 PyDoc_STRVAR(posix_minor__doc__,
6442 "minor(device) -> minor number\n\
6443 Extracts a device minor number from a raw device number.");
6445 static PyObject *
6446 posix_minor(PyObject *self, PyObject *args)
6448 int device;
6449 if (!PyArg_ParseTuple(args, "i:minor", &device))
6450 return NULL;
6451 return PyInt_FromLong((long)minor(device));
6454 PyDoc_STRVAR(posix_makedev__doc__,
6455 "makedev(major, minor) -> device number\n\
6456 Composes a raw device number from the major and minor device numbers.");
6458 static PyObject *
6459 posix_makedev(PyObject *self, PyObject *args)
6461 int major, minor;
6462 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6463 return NULL;
6464 return PyInt_FromLong((long)makedev(major, minor));
6466 #endif /* device macros */
6469 #ifdef HAVE_FTRUNCATE
6470 PyDoc_STRVAR(posix_ftruncate__doc__,
6471 "ftruncate(fd, length)\n\n\
6472 Truncate a file to a specified length.");
6474 static PyObject *
6475 posix_ftruncate(PyObject *self, PyObject *args)
6477 int fd;
6478 off_t length;
6479 int res;
6480 PyObject *lenobj;
6482 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6483 return NULL;
6485 #if !defined(HAVE_LARGEFILE_SUPPORT)
6486 length = PyInt_AsLong(lenobj);
6487 #else
6488 length = PyLong_Check(lenobj) ?
6489 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6490 #endif
6491 if (PyErr_Occurred())
6492 return NULL;
6494 Py_BEGIN_ALLOW_THREADS
6495 res = ftruncate(fd, length);
6496 Py_END_ALLOW_THREADS
6497 if (res < 0) {
6498 PyErr_SetFromErrno(PyExc_IOError);
6499 return NULL;
6501 Py_INCREF(Py_None);
6502 return Py_None;
6504 #endif
6506 #ifdef HAVE_PUTENV
6507 PyDoc_STRVAR(posix_putenv__doc__,
6508 "putenv(key, value)\n\n\
6509 Change or add an environment variable.");
6511 /* Save putenv() parameters as values here, so we can collect them when they
6512 * get re-set with another call for the same key. */
6513 static PyObject *posix_putenv_garbage;
6515 static PyObject *
6516 posix_putenv(PyObject *self, PyObject *args)
6518 char *s1, *s2;
6519 char *newenv;
6520 PyObject *newstr;
6521 size_t len;
6523 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6524 return NULL;
6526 #if defined(PYOS_OS2)
6527 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6528 APIRET rc;
6530 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6531 if (rc != NO_ERROR)
6532 return os2_error(rc);
6534 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6535 APIRET rc;
6537 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6538 if (rc != NO_ERROR)
6539 return os2_error(rc);
6540 } else {
6541 #endif
6543 /* XXX This can leak memory -- not easy to fix :-( */
6544 len = strlen(s1) + strlen(s2) + 2;
6545 /* len includes space for a trailing \0; the size arg to
6546 PyString_FromStringAndSize does not count that */
6547 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6548 if (newstr == NULL)
6549 return PyErr_NoMemory();
6550 newenv = PyString_AS_STRING(newstr);
6551 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6552 if (putenv(newenv)) {
6553 Py_DECREF(newstr);
6554 posix_error();
6555 return NULL;
6557 /* Install the first arg and newstr in posix_putenv_garbage;
6558 * this will cause previous value to be collected. This has to
6559 * happen after the real putenv() call because the old value
6560 * was still accessible until then. */
6561 if (PyDict_SetItem(posix_putenv_garbage,
6562 PyTuple_GET_ITEM(args, 0), newstr)) {
6563 /* really not much we can do; just leak */
6564 PyErr_Clear();
6566 else {
6567 Py_DECREF(newstr);
6570 #if defined(PYOS_OS2)
6572 #endif
6573 Py_INCREF(Py_None);
6574 return Py_None;
6576 #endif /* putenv */
6578 #ifdef HAVE_UNSETENV
6579 PyDoc_STRVAR(posix_unsetenv__doc__,
6580 "unsetenv(key)\n\n\
6581 Delete an environment variable.");
6583 static PyObject *
6584 posix_unsetenv(PyObject *self, PyObject *args)
6586 char *s1;
6588 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6589 return NULL;
6591 unsetenv(s1);
6593 /* Remove the key from posix_putenv_garbage;
6594 * this will cause it to be collected. This has to
6595 * happen after the real unsetenv() call because the
6596 * old value was still accessible until then.
6598 if (PyDict_DelItem(posix_putenv_garbage,
6599 PyTuple_GET_ITEM(args, 0))) {
6600 /* really not much we can do; just leak */
6601 PyErr_Clear();
6604 Py_INCREF(Py_None);
6605 return Py_None;
6607 #endif /* unsetenv */
6609 #ifdef HAVE_STRERROR
6610 PyDoc_STRVAR(posix_strerror__doc__,
6611 "strerror(code) -> string\n\n\
6612 Translate an error code to a message string.");
6614 static PyObject *
6615 posix_strerror(PyObject *self, PyObject *args)
6617 int code;
6618 char *message;
6619 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6620 return NULL;
6621 message = strerror(code);
6622 if (message == NULL) {
6623 PyErr_SetString(PyExc_ValueError,
6624 "strerror() argument out of range");
6625 return NULL;
6627 return PyString_FromString(message);
6629 #endif /* strerror */
6632 #ifdef HAVE_SYS_WAIT_H
6634 #ifdef WCOREDUMP
6635 PyDoc_STRVAR(posix_WCOREDUMP__doc__,
6636 "WCOREDUMP(status) -> bool\n\n\
6637 Return True if the process returning 'status' was dumped to a core file.");
6639 static PyObject *
6640 posix_WCOREDUMP(PyObject *self, PyObject *args)
6642 WAIT_TYPE status;
6643 WAIT_STATUS_INT(status) = 0;
6645 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6646 return NULL;
6648 return PyBool_FromLong(WCOREDUMP(status));
6650 #endif /* WCOREDUMP */
6652 #ifdef WIFCONTINUED
6653 PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
6654 "WIFCONTINUED(status) -> bool\n\n\
6655 Return True if the process returning 'status' was continued from a\n\
6656 job control stop.");
6658 static PyObject *
6659 posix_WIFCONTINUED(PyObject *self, PyObject *args)
6661 WAIT_TYPE status;
6662 WAIT_STATUS_INT(status) = 0;
6664 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6665 return NULL;
6667 return PyBool_FromLong(WIFCONTINUED(status));
6669 #endif /* WIFCONTINUED */
6671 #ifdef WIFSTOPPED
6672 PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
6673 "WIFSTOPPED(status) -> bool\n\n\
6674 Return True if the process returning 'status' was stopped.");
6676 static PyObject *
6677 posix_WIFSTOPPED(PyObject *self, PyObject *args)
6679 WAIT_TYPE status;
6680 WAIT_STATUS_INT(status) = 0;
6682 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6683 return NULL;
6685 return PyBool_FromLong(WIFSTOPPED(status));
6687 #endif /* WIFSTOPPED */
6689 #ifdef WIFSIGNALED
6690 PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
6691 "WIFSIGNALED(status) -> bool\n\n\
6692 Return True if the process returning 'status' was terminated by a signal.");
6694 static PyObject *
6695 posix_WIFSIGNALED(PyObject *self, PyObject *args)
6697 WAIT_TYPE status;
6698 WAIT_STATUS_INT(status) = 0;
6700 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6701 return NULL;
6703 return PyBool_FromLong(WIFSIGNALED(status));
6705 #endif /* WIFSIGNALED */
6707 #ifdef WIFEXITED
6708 PyDoc_STRVAR(posix_WIFEXITED__doc__,
6709 "WIFEXITED(status) -> bool\n\n\
6710 Return true if the process returning 'status' exited using the exit()\n\
6711 system call.");
6713 static PyObject *
6714 posix_WIFEXITED(PyObject *self, PyObject *args)
6716 WAIT_TYPE status;
6717 WAIT_STATUS_INT(status) = 0;
6719 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6720 return NULL;
6722 return PyBool_FromLong(WIFEXITED(status));
6724 #endif /* WIFEXITED */
6726 #ifdef WEXITSTATUS
6727 PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
6728 "WEXITSTATUS(status) -> integer\n\n\
6729 Return the process return code from 'status'.");
6731 static PyObject *
6732 posix_WEXITSTATUS(PyObject *self, PyObject *args)
6734 WAIT_TYPE status;
6735 WAIT_STATUS_INT(status) = 0;
6737 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6738 return NULL;
6740 return Py_BuildValue("i", WEXITSTATUS(status));
6742 #endif /* WEXITSTATUS */
6744 #ifdef WTERMSIG
6745 PyDoc_STRVAR(posix_WTERMSIG__doc__,
6746 "WTERMSIG(status) -> integer\n\n\
6747 Return the signal that terminated the process that provided the 'status'\n\
6748 value.");
6750 static PyObject *
6751 posix_WTERMSIG(PyObject *self, PyObject *args)
6753 WAIT_TYPE status;
6754 WAIT_STATUS_INT(status) = 0;
6756 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6757 return NULL;
6759 return Py_BuildValue("i", WTERMSIG(status));
6761 #endif /* WTERMSIG */
6763 #ifdef WSTOPSIG
6764 PyDoc_STRVAR(posix_WSTOPSIG__doc__,
6765 "WSTOPSIG(status) -> integer\n\n\
6766 Return the signal that stopped the process that provided\n\
6767 the 'status' value.");
6769 static PyObject *
6770 posix_WSTOPSIG(PyObject *self, PyObject *args)
6772 WAIT_TYPE status;
6773 WAIT_STATUS_INT(status) = 0;
6775 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6776 return NULL;
6778 return Py_BuildValue("i", WSTOPSIG(status));
6780 #endif /* WSTOPSIG */
6782 #endif /* HAVE_SYS_WAIT_H */
6785 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
6786 #ifdef _SCO_DS
6787 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6788 needed definitions in sys/statvfs.h */
6789 #define _SVID3
6790 #endif
6791 #include <sys/statvfs.h>
6793 static PyObject*
6794 _pystatvfs_fromstructstatvfs(struct statvfs st) {
6795 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6796 if (v == NULL)
6797 return NULL;
6799 #if !defined(HAVE_LARGEFILE_SUPPORT)
6800 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6801 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6802 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6803 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6804 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6805 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6806 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6807 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6808 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6809 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6810 #else
6811 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6812 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6813 PyStructSequence_SET_ITEM(v, 2,
6814 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6815 PyStructSequence_SET_ITEM(v, 3,
6816 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6817 PyStructSequence_SET_ITEM(v, 4,
6818 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6819 PyStructSequence_SET_ITEM(v, 5,
6820 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6821 PyStructSequence_SET_ITEM(v, 6,
6822 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6823 PyStructSequence_SET_ITEM(v, 7,
6824 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6825 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6826 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6827 #endif
6829 return v;
6832 PyDoc_STRVAR(posix_fstatvfs__doc__,
6833 "fstatvfs(fd) -> statvfs result\n\n\
6834 Perform an fstatvfs system call on the given fd.");
6836 static PyObject *
6837 posix_fstatvfs(PyObject *self, PyObject *args)
6839 int fd, res;
6840 struct statvfs st;
6842 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6843 return NULL;
6844 Py_BEGIN_ALLOW_THREADS
6845 res = fstatvfs(fd, &st);
6846 Py_END_ALLOW_THREADS
6847 if (res != 0)
6848 return posix_error();
6850 return _pystatvfs_fromstructstatvfs(st);
6852 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
6855 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
6856 #include <sys/statvfs.h>
6858 PyDoc_STRVAR(posix_statvfs__doc__,
6859 "statvfs(path) -> statvfs result\n\n\
6860 Perform a statvfs system call on the given path.");
6862 static PyObject *
6863 posix_statvfs(PyObject *self, PyObject *args)
6865 char *path;
6866 int res;
6867 struct statvfs st;
6868 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6869 return NULL;
6870 Py_BEGIN_ALLOW_THREADS
6871 res = statvfs(path, &st);
6872 Py_END_ALLOW_THREADS
6873 if (res != 0)
6874 return posix_error_with_filename(path);
6876 return _pystatvfs_fromstructstatvfs(st);
6878 #endif /* HAVE_STATVFS */
6881 #ifdef HAVE_TEMPNAM
6882 PyDoc_STRVAR(posix_tempnam__doc__,
6883 "tempnam([dir[, prefix]]) -> string\n\n\
6884 Return a unique name for a temporary file.\n\
6885 The directory and a prefix may be specified as strings; they may be omitted\n\
6886 or None if not needed.");
6888 static PyObject *
6889 posix_tempnam(PyObject *self, PyObject *args)
6891 PyObject *result = NULL;
6892 char *dir = NULL;
6893 char *pfx = NULL;
6894 char *name;
6896 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6897 return NULL;
6899 if (PyErr_Warn(PyExc_RuntimeWarning,
6900 "tempnam is a potential security risk to your program") < 0)
6901 return NULL;
6903 #ifdef MS_WINDOWS
6904 name = _tempnam(dir, pfx);
6905 #else
6906 name = tempnam(dir, pfx);
6907 #endif
6908 if (name == NULL)
6909 return PyErr_NoMemory();
6910 result = PyString_FromString(name);
6911 free(name);
6912 return result;
6914 #endif
6917 #ifdef HAVE_TMPFILE
6918 PyDoc_STRVAR(posix_tmpfile__doc__,
6919 "tmpfile() -> file object\n\n\
6920 Create a temporary file with no directory entries.");
6922 static PyObject *
6923 posix_tmpfile(PyObject *self, PyObject *noargs)
6925 FILE *fp;
6927 fp = tmpfile();
6928 if (fp == NULL)
6929 return posix_error();
6930 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
6932 #endif
6935 #ifdef HAVE_TMPNAM
6936 PyDoc_STRVAR(posix_tmpnam__doc__,
6937 "tmpnam() -> string\n\n\
6938 Return a unique name for a temporary file.");
6940 static PyObject *
6941 posix_tmpnam(PyObject *self, PyObject *noargs)
6943 char buffer[L_tmpnam];
6944 char *name;
6946 if (PyErr_Warn(PyExc_RuntimeWarning,
6947 "tmpnam is a potential security risk to your program") < 0)
6948 return NULL;
6950 #ifdef USE_TMPNAM_R
6951 name = tmpnam_r(buffer);
6952 #else
6953 name = tmpnam(buffer);
6954 #endif
6955 if (name == NULL) {
6956 PyObject *err = Py_BuildValue("is", 0,
6957 #ifdef USE_TMPNAM_R
6958 "unexpected NULL from tmpnam_r"
6959 #else
6960 "unexpected NULL from tmpnam"
6961 #endif
6963 PyErr_SetObject(PyExc_OSError, err);
6964 Py_XDECREF(err);
6965 return NULL;
6967 return PyString_FromString(buffer);
6969 #endif
6972 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6973 * It maps strings representing configuration variable names to
6974 * integer values, allowing those functions to be called with the
6975 * magic names instead of polluting the module's namespace with tons of
6976 * rarely-used constants. There are three separate tables that use
6977 * these definitions.
6979 * This code is always included, even if none of the interfaces that
6980 * need it are included. The #if hackery needed to avoid it would be
6981 * sufficiently pervasive that it's not worth the loss of readability.
6983 struct constdef {
6984 char *name;
6985 long value;
6988 static int
6989 conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6990 size_t tablesize)
6992 if (PyInt_Check(arg)) {
6993 *valuep = PyInt_AS_LONG(arg);
6994 return 1;
6996 if (PyString_Check(arg)) {
6997 /* look up the value in the table using a binary search */
6998 size_t lo = 0;
6999 size_t mid;
7000 size_t hi = tablesize;
7001 int cmp;
7002 char *confname = PyString_AS_STRING(arg);
7003 while (lo < hi) {
7004 mid = (lo + hi) / 2;
7005 cmp = strcmp(confname, table[mid].name);
7006 if (cmp < 0)
7007 hi = mid;
7008 else if (cmp > 0)
7009 lo = mid + 1;
7010 else {
7011 *valuep = table[mid].value;
7012 return 1;
7015 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7017 else
7018 PyErr_SetString(PyExc_TypeError,
7019 "configuration names must be strings or integers");
7020 return 0;
7024 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7025 static struct constdef posix_constants_pathconf[] = {
7026 #ifdef _PC_ABI_AIO_XFER_MAX
7027 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7028 #endif
7029 #ifdef _PC_ABI_ASYNC_IO
7030 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7031 #endif
7032 #ifdef _PC_ASYNC_IO
7033 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7034 #endif
7035 #ifdef _PC_CHOWN_RESTRICTED
7036 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7037 #endif
7038 #ifdef _PC_FILESIZEBITS
7039 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7040 #endif
7041 #ifdef _PC_LAST
7042 {"PC_LAST", _PC_LAST},
7043 #endif
7044 #ifdef _PC_LINK_MAX
7045 {"PC_LINK_MAX", _PC_LINK_MAX},
7046 #endif
7047 #ifdef _PC_MAX_CANON
7048 {"PC_MAX_CANON", _PC_MAX_CANON},
7049 #endif
7050 #ifdef _PC_MAX_INPUT
7051 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7052 #endif
7053 #ifdef _PC_NAME_MAX
7054 {"PC_NAME_MAX", _PC_NAME_MAX},
7055 #endif
7056 #ifdef _PC_NO_TRUNC
7057 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7058 #endif
7059 #ifdef _PC_PATH_MAX
7060 {"PC_PATH_MAX", _PC_PATH_MAX},
7061 #endif
7062 #ifdef _PC_PIPE_BUF
7063 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7064 #endif
7065 #ifdef _PC_PRIO_IO
7066 {"PC_PRIO_IO", _PC_PRIO_IO},
7067 #endif
7068 #ifdef _PC_SOCK_MAXBUF
7069 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7070 #endif
7071 #ifdef _PC_SYNC_IO
7072 {"PC_SYNC_IO", _PC_SYNC_IO},
7073 #endif
7074 #ifdef _PC_VDISABLE
7075 {"PC_VDISABLE", _PC_VDISABLE},
7076 #endif
7079 static int
7080 conv_path_confname(PyObject *arg, int *valuep)
7082 return conv_confname(arg, valuep, posix_constants_pathconf,
7083 sizeof(posix_constants_pathconf)
7084 / sizeof(struct constdef));
7086 #endif
7088 #ifdef HAVE_FPATHCONF
7089 PyDoc_STRVAR(posix_fpathconf__doc__,
7090 "fpathconf(fd, name) -> integer\n\n\
7091 Return the configuration limit name for the file descriptor fd.\n\
7092 If there is no limit, return -1.");
7094 static PyObject *
7095 posix_fpathconf(PyObject *self, PyObject *args)
7097 PyObject *result = NULL;
7098 int name, fd;
7100 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7101 conv_path_confname, &name)) {
7102 long limit;
7104 errno = 0;
7105 limit = fpathconf(fd, name);
7106 if (limit == -1 && errno != 0)
7107 posix_error();
7108 else
7109 result = PyInt_FromLong(limit);
7111 return result;
7113 #endif
7116 #ifdef HAVE_PATHCONF
7117 PyDoc_STRVAR(posix_pathconf__doc__,
7118 "pathconf(path, name) -> integer\n\n\
7119 Return the configuration limit name for the file or directory path.\n\
7120 If there is no limit, return -1.");
7122 static PyObject *
7123 posix_pathconf(PyObject *self, PyObject *args)
7125 PyObject *result = NULL;
7126 int name;
7127 char *path;
7129 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7130 conv_path_confname, &name)) {
7131 long limit;
7133 errno = 0;
7134 limit = pathconf(path, name);
7135 if (limit == -1 && errno != 0) {
7136 if (errno == EINVAL)
7137 /* could be a path or name problem */
7138 posix_error();
7139 else
7140 posix_error_with_filename(path);
7142 else
7143 result = PyInt_FromLong(limit);
7145 return result;
7147 #endif
7149 #ifdef HAVE_CONFSTR
7150 static struct constdef posix_constants_confstr[] = {
7151 #ifdef _CS_ARCHITECTURE
7152 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7153 #endif
7154 #ifdef _CS_HOSTNAME
7155 {"CS_HOSTNAME", _CS_HOSTNAME},
7156 #endif
7157 #ifdef _CS_HW_PROVIDER
7158 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7159 #endif
7160 #ifdef _CS_HW_SERIAL
7161 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7162 #endif
7163 #ifdef _CS_INITTAB_NAME
7164 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7165 #endif
7166 #ifdef _CS_LFS64_CFLAGS
7167 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7168 #endif
7169 #ifdef _CS_LFS64_LDFLAGS
7170 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7171 #endif
7172 #ifdef _CS_LFS64_LIBS
7173 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7174 #endif
7175 #ifdef _CS_LFS64_LINTFLAGS
7176 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7177 #endif
7178 #ifdef _CS_LFS_CFLAGS
7179 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7180 #endif
7181 #ifdef _CS_LFS_LDFLAGS
7182 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7183 #endif
7184 #ifdef _CS_LFS_LIBS
7185 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7186 #endif
7187 #ifdef _CS_LFS_LINTFLAGS
7188 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7189 #endif
7190 #ifdef _CS_MACHINE
7191 {"CS_MACHINE", _CS_MACHINE},
7192 #endif
7193 #ifdef _CS_PATH
7194 {"CS_PATH", _CS_PATH},
7195 #endif
7196 #ifdef _CS_RELEASE
7197 {"CS_RELEASE", _CS_RELEASE},
7198 #endif
7199 #ifdef _CS_SRPC_DOMAIN
7200 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7201 #endif
7202 #ifdef _CS_SYSNAME
7203 {"CS_SYSNAME", _CS_SYSNAME},
7204 #endif
7205 #ifdef _CS_VERSION
7206 {"CS_VERSION", _CS_VERSION},
7207 #endif
7208 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7209 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7210 #endif
7211 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7212 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7213 #endif
7214 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
7215 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7216 #endif
7217 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7218 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7219 #endif
7220 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7221 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7222 #endif
7223 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7224 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7225 #endif
7226 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7227 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7228 #endif
7229 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7230 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7231 #endif
7232 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7233 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7234 #endif
7235 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7236 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7237 #endif
7238 #ifdef _CS_XBS5_LP64_OFF64_LIBS
7239 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7240 #endif
7241 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7242 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7243 #endif
7244 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7245 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7246 #endif
7247 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7248 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7249 #endif
7250 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7251 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7252 #endif
7253 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7254 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7255 #endif
7256 #ifdef _MIPS_CS_AVAIL_PROCESSORS
7257 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7258 #endif
7259 #ifdef _MIPS_CS_BASE
7260 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7261 #endif
7262 #ifdef _MIPS_CS_HOSTID
7263 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7264 #endif
7265 #ifdef _MIPS_CS_HW_NAME
7266 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7267 #endif
7268 #ifdef _MIPS_CS_NUM_PROCESSORS
7269 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7270 #endif
7271 #ifdef _MIPS_CS_OSREL_MAJ
7272 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7273 #endif
7274 #ifdef _MIPS_CS_OSREL_MIN
7275 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7276 #endif
7277 #ifdef _MIPS_CS_OSREL_PATCH
7278 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7279 #endif
7280 #ifdef _MIPS_CS_OS_NAME
7281 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7282 #endif
7283 #ifdef _MIPS_CS_OS_PROVIDER
7284 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7285 #endif
7286 #ifdef _MIPS_CS_PROCESSORS
7287 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7288 #endif
7289 #ifdef _MIPS_CS_SERIAL
7290 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7291 #endif
7292 #ifdef _MIPS_CS_VENDOR
7293 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7294 #endif
7297 static int
7298 conv_confstr_confname(PyObject *arg, int *valuep)
7300 return conv_confname(arg, valuep, posix_constants_confstr,
7301 sizeof(posix_constants_confstr)
7302 / sizeof(struct constdef));
7305 PyDoc_STRVAR(posix_confstr__doc__,
7306 "confstr(name) -> string\n\n\
7307 Return a string-valued system configuration variable.");
7309 static PyObject *
7310 posix_confstr(PyObject *self, PyObject *args)
7312 PyObject *result = NULL;
7313 int name;
7314 char buffer[256];
7316 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
7317 int len;
7319 errno = 0;
7320 len = confstr(name, buffer, sizeof(buffer));
7321 if (len == 0) {
7322 if (errno) {
7323 posix_error();
7325 else {
7326 result = Py_None;
7327 Py_INCREF(Py_None);
7330 else {
7331 if ((unsigned int)len >= sizeof(buffer)) {
7332 result = PyString_FromStringAndSize(NULL, len-1);
7333 if (result != NULL)
7334 confstr(name, PyString_AS_STRING(result), len);
7336 else
7337 result = PyString_FromStringAndSize(buffer, len-1);
7340 return result;
7342 #endif
7345 #ifdef HAVE_SYSCONF
7346 static struct constdef posix_constants_sysconf[] = {
7347 #ifdef _SC_2_CHAR_TERM
7348 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7349 #endif
7350 #ifdef _SC_2_C_BIND
7351 {"SC_2_C_BIND", _SC_2_C_BIND},
7352 #endif
7353 #ifdef _SC_2_C_DEV
7354 {"SC_2_C_DEV", _SC_2_C_DEV},
7355 #endif
7356 #ifdef _SC_2_C_VERSION
7357 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7358 #endif
7359 #ifdef _SC_2_FORT_DEV
7360 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7361 #endif
7362 #ifdef _SC_2_FORT_RUN
7363 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7364 #endif
7365 #ifdef _SC_2_LOCALEDEF
7366 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7367 #endif
7368 #ifdef _SC_2_SW_DEV
7369 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7370 #endif
7371 #ifdef _SC_2_UPE
7372 {"SC_2_UPE", _SC_2_UPE},
7373 #endif
7374 #ifdef _SC_2_VERSION
7375 {"SC_2_VERSION", _SC_2_VERSION},
7376 #endif
7377 #ifdef _SC_ABI_ASYNCHRONOUS_IO
7378 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7379 #endif
7380 #ifdef _SC_ACL
7381 {"SC_ACL", _SC_ACL},
7382 #endif
7383 #ifdef _SC_AIO_LISTIO_MAX
7384 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7385 #endif
7386 #ifdef _SC_AIO_MAX
7387 {"SC_AIO_MAX", _SC_AIO_MAX},
7388 #endif
7389 #ifdef _SC_AIO_PRIO_DELTA_MAX
7390 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7391 #endif
7392 #ifdef _SC_ARG_MAX
7393 {"SC_ARG_MAX", _SC_ARG_MAX},
7394 #endif
7395 #ifdef _SC_ASYNCHRONOUS_IO
7396 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7397 #endif
7398 #ifdef _SC_ATEXIT_MAX
7399 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7400 #endif
7401 #ifdef _SC_AUDIT
7402 {"SC_AUDIT", _SC_AUDIT},
7403 #endif
7404 #ifdef _SC_AVPHYS_PAGES
7405 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7406 #endif
7407 #ifdef _SC_BC_BASE_MAX
7408 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7409 #endif
7410 #ifdef _SC_BC_DIM_MAX
7411 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7412 #endif
7413 #ifdef _SC_BC_SCALE_MAX
7414 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7415 #endif
7416 #ifdef _SC_BC_STRING_MAX
7417 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7418 #endif
7419 #ifdef _SC_CAP
7420 {"SC_CAP", _SC_CAP},
7421 #endif
7422 #ifdef _SC_CHARCLASS_NAME_MAX
7423 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7424 #endif
7425 #ifdef _SC_CHAR_BIT
7426 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7427 #endif
7428 #ifdef _SC_CHAR_MAX
7429 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7430 #endif
7431 #ifdef _SC_CHAR_MIN
7432 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7433 #endif
7434 #ifdef _SC_CHILD_MAX
7435 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7436 #endif
7437 #ifdef _SC_CLK_TCK
7438 {"SC_CLK_TCK", _SC_CLK_TCK},
7439 #endif
7440 #ifdef _SC_COHER_BLKSZ
7441 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7442 #endif
7443 #ifdef _SC_COLL_WEIGHTS_MAX
7444 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7445 #endif
7446 #ifdef _SC_DCACHE_ASSOC
7447 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7448 #endif
7449 #ifdef _SC_DCACHE_BLKSZ
7450 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7451 #endif
7452 #ifdef _SC_DCACHE_LINESZ
7453 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7454 #endif
7455 #ifdef _SC_DCACHE_SZ
7456 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7457 #endif
7458 #ifdef _SC_DCACHE_TBLKSZ
7459 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7460 #endif
7461 #ifdef _SC_DELAYTIMER_MAX
7462 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7463 #endif
7464 #ifdef _SC_EQUIV_CLASS_MAX
7465 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7466 #endif
7467 #ifdef _SC_EXPR_NEST_MAX
7468 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7469 #endif
7470 #ifdef _SC_FSYNC
7471 {"SC_FSYNC", _SC_FSYNC},
7472 #endif
7473 #ifdef _SC_GETGR_R_SIZE_MAX
7474 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7475 #endif
7476 #ifdef _SC_GETPW_R_SIZE_MAX
7477 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7478 #endif
7479 #ifdef _SC_ICACHE_ASSOC
7480 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7481 #endif
7482 #ifdef _SC_ICACHE_BLKSZ
7483 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7484 #endif
7485 #ifdef _SC_ICACHE_LINESZ
7486 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7487 #endif
7488 #ifdef _SC_ICACHE_SZ
7489 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7490 #endif
7491 #ifdef _SC_INF
7492 {"SC_INF", _SC_INF},
7493 #endif
7494 #ifdef _SC_INT_MAX
7495 {"SC_INT_MAX", _SC_INT_MAX},
7496 #endif
7497 #ifdef _SC_INT_MIN
7498 {"SC_INT_MIN", _SC_INT_MIN},
7499 #endif
7500 #ifdef _SC_IOV_MAX
7501 {"SC_IOV_MAX", _SC_IOV_MAX},
7502 #endif
7503 #ifdef _SC_IP_SECOPTS
7504 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7505 #endif
7506 #ifdef _SC_JOB_CONTROL
7507 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7508 #endif
7509 #ifdef _SC_KERN_POINTERS
7510 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7511 #endif
7512 #ifdef _SC_KERN_SIM
7513 {"SC_KERN_SIM", _SC_KERN_SIM},
7514 #endif
7515 #ifdef _SC_LINE_MAX
7516 {"SC_LINE_MAX", _SC_LINE_MAX},
7517 #endif
7518 #ifdef _SC_LOGIN_NAME_MAX
7519 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7520 #endif
7521 #ifdef _SC_LOGNAME_MAX
7522 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7523 #endif
7524 #ifdef _SC_LONG_BIT
7525 {"SC_LONG_BIT", _SC_LONG_BIT},
7526 #endif
7527 #ifdef _SC_MAC
7528 {"SC_MAC", _SC_MAC},
7529 #endif
7530 #ifdef _SC_MAPPED_FILES
7531 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7532 #endif
7533 #ifdef _SC_MAXPID
7534 {"SC_MAXPID", _SC_MAXPID},
7535 #endif
7536 #ifdef _SC_MB_LEN_MAX
7537 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7538 #endif
7539 #ifdef _SC_MEMLOCK
7540 {"SC_MEMLOCK", _SC_MEMLOCK},
7541 #endif
7542 #ifdef _SC_MEMLOCK_RANGE
7543 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7544 #endif
7545 #ifdef _SC_MEMORY_PROTECTION
7546 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7547 #endif
7548 #ifdef _SC_MESSAGE_PASSING
7549 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7550 #endif
7551 #ifdef _SC_MMAP_FIXED_ALIGNMENT
7552 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7553 #endif
7554 #ifdef _SC_MQ_OPEN_MAX
7555 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7556 #endif
7557 #ifdef _SC_MQ_PRIO_MAX
7558 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7559 #endif
7560 #ifdef _SC_NACLS_MAX
7561 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7562 #endif
7563 #ifdef _SC_NGROUPS_MAX
7564 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7565 #endif
7566 #ifdef _SC_NL_ARGMAX
7567 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7568 #endif
7569 #ifdef _SC_NL_LANGMAX
7570 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7571 #endif
7572 #ifdef _SC_NL_MSGMAX
7573 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7574 #endif
7575 #ifdef _SC_NL_NMAX
7576 {"SC_NL_NMAX", _SC_NL_NMAX},
7577 #endif
7578 #ifdef _SC_NL_SETMAX
7579 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7580 #endif
7581 #ifdef _SC_NL_TEXTMAX
7582 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7583 #endif
7584 #ifdef _SC_NPROCESSORS_CONF
7585 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7586 #endif
7587 #ifdef _SC_NPROCESSORS_ONLN
7588 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7589 #endif
7590 #ifdef _SC_NPROC_CONF
7591 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7592 #endif
7593 #ifdef _SC_NPROC_ONLN
7594 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7595 #endif
7596 #ifdef _SC_NZERO
7597 {"SC_NZERO", _SC_NZERO},
7598 #endif
7599 #ifdef _SC_OPEN_MAX
7600 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7601 #endif
7602 #ifdef _SC_PAGESIZE
7603 {"SC_PAGESIZE", _SC_PAGESIZE},
7604 #endif
7605 #ifdef _SC_PAGE_SIZE
7606 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7607 #endif
7608 #ifdef _SC_PASS_MAX
7609 {"SC_PASS_MAX", _SC_PASS_MAX},
7610 #endif
7611 #ifdef _SC_PHYS_PAGES
7612 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7613 #endif
7614 #ifdef _SC_PII
7615 {"SC_PII", _SC_PII},
7616 #endif
7617 #ifdef _SC_PII_INTERNET
7618 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7619 #endif
7620 #ifdef _SC_PII_INTERNET_DGRAM
7621 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7622 #endif
7623 #ifdef _SC_PII_INTERNET_STREAM
7624 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7625 #endif
7626 #ifdef _SC_PII_OSI
7627 {"SC_PII_OSI", _SC_PII_OSI},
7628 #endif
7629 #ifdef _SC_PII_OSI_CLTS
7630 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7631 #endif
7632 #ifdef _SC_PII_OSI_COTS
7633 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7634 #endif
7635 #ifdef _SC_PII_OSI_M
7636 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7637 #endif
7638 #ifdef _SC_PII_SOCKET
7639 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7640 #endif
7641 #ifdef _SC_PII_XTI
7642 {"SC_PII_XTI", _SC_PII_XTI},
7643 #endif
7644 #ifdef _SC_POLL
7645 {"SC_POLL", _SC_POLL},
7646 #endif
7647 #ifdef _SC_PRIORITIZED_IO
7648 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7649 #endif
7650 #ifdef _SC_PRIORITY_SCHEDULING
7651 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7652 #endif
7653 #ifdef _SC_REALTIME_SIGNALS
7654 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7655 #endif
7656 #ifdef _SC_RE_DUP_MAX
7657 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7658 #endif
7659 #ifdef _SC_RTSIG_MAX
7660 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7661 #endif
7662 #ifdef _SC_SAVED_IDS
7663 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7664 #endif
7665 #ifdef _SC_SCHAR_MAX
7666 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7667 #endif
7668 #ifdef _SC_SCHAR_MIN
7669 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7670 #endif
7671 #ifdef _SC_SELECT
7672 {"SC_SELECT", _SC_SELECT},
7673 #endif
7674 #ifdef _SC_SEMAPHORES
7675 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7676 #endif
7677 #ifdef _SC_SEM_NSEMS_MAX
7678 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7679 #endif
7680 #ifdef _SC_SEM_VALUE_MAX
7681 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7682 #endif
7683 #ifdef _SC_SHARED_MEMORY_OBJECTS
7684 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7685 #endif
7686 #ifdef _SC_SHRT_MAX
7687 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7688 #endif
7689 #ifdef _SC_SHRT_MIN
7690 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7691 #endif
7692 #ifdef _SC_SIGQUEUE_MAX
7693 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7694 #endif
7695 #ifdef _SC_SIGRT_MAX
7696 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7697 #endif
7698 #ifdef _SC_SIGRT_MIN
7699 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7700 #endif
7701 #ifdef _SC_SOFTPOWER
7702 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7703 #endif
7704 #ifdef _SC_SPLIT_CACHE
7705 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7706 #endif
7707 #ifdef _SC_SSIZE_MAX
7708 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7709 #endif
7710 #ifdef _SC_STACK_PROT
7711 {"SC_STACK_PROT", _SC_STACK_PROT},
7712 #endif
7713 #ifdef _SC_STREAM_MAX
7714 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7715 #endif
7716 #ifdef _SC_SYNCHRONIZED_IO
7717 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7718 #endif
7719 #ifdef _SC_THREADS
7720 {"SC_THREADS", _SC_THREADS},
7721 #endif
7722 #ifdef _SC_THREAD_ATTR_STACKADDR
7723 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7724 #endif
7725 #ifdef _SC_THREAD_ATTR_STACKSIZE
7726 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7727 #endif
7728 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7729 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7730 #endif
7731 #ifdef _SC_THREAD_KEYS_MAX
7732 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7733 #endif
7734 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
7735 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7736 #endif
7737 #ifdef _SC_THREAD_PRIO_INHERIT
7738 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7739 #endif
7740 #ifdef _SC_THREAD_PRIO_PROTECT
7741 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7742 #endif
7743 #ifdef _SC_THREAD_PROCESS_SHARED
7744 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7745 #endif
7746 #ifdef _SC_THREAD_SAFE_FUNCTIONS
7747 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7748 #endif
7749 #ifdef _SC_THREAD_STACK_MIN
7750 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7751 #endif
7752 #ifdef _SC_THREAD_THREADS_MAX
7753 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7754 #endif
7755 #ifdef _SC_TIMERS
7756 {"SC_TIMERS", _SC_TIMERS},
7757 #endif
7758 #ifdef _SC_TIMER_MAX
7759 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7760 #endif
7761 #ifdef _SC_TTY_NAME_MAX
7762 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7763 #endif
7764 #ifdef _SC_TZNAME_MAX
7765 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7766 #endif
7767 #ifdef _SC_T_IOV_MAX
7768 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7769 #endif
7770 #ifdef _SC_UCHAR_MAX
7771 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7772 #endif
7773 #ifdef _SC_UINT_MAX
7774 {"SC_UINT_MAX", _SC_UINT_MAX},
7775 #endif
7776 #ifdef _SC_UIO_MAXIOV
7777 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7778 #endif
7779 #ifdef _SC_ULONG_MAX
7780 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7781 #endif
7782 #ifdef _SC_USHRT_MAX
7783 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7784 #endif
7785 #ifdef _SC_VERSION
7786 {"SC_VERSION", _SC_VERSION},
7787 #endif
7788 #ifdef _SC_WORD_BIT
7789 {"SC_WORD_BIT", _SC_WORD_BIT},
7790 #endif
7791 #ifdef _SC_XBS5_ILP32_OFF32
7792 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7793 #endif
7794 #ifdef _SC_XBS5_ILP32_OFFBIG
7795 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7796 #endif
7797 #ifdef _SC_XBS5_LP64_OFF64
7798 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7799 #endif
7800 #ifdef _SC_XBS5_LPBIG_OFFBIG
7801 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7802 #endif
7803 #ifdef _SC_XOPEN_CRYPT
7804 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7805 #endif
7806 #ifdef _SC_XOPEN_ENH_I18N
7807 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7808 #endif
7809 #ifdef _SC_XOPEN_LEGACY
7810 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7811 #endif
7812 #ifdef _SC_XOPEN_REALTIME
7813 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7814 #endif
7815 #ifdef _SC_XOPEN_REALTIME_THREADS
7816 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7817 #endif
7818 #ifdef _SC_XOPEN_SHM
7819 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7820 #endif
7821 #ifdef _SC_XOPEN_UNIX
7822 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7823 #endif
7824 #ifdef _SC_XOPEN_VERSION
7825 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7826 #endif
7827 #ifdef _SC_XOPEN_XCU_VERSION
7828 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7829 #endif
7830 #ifdef _SC_XOPEN_XPG2
7831 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7832 #endif
7833 #ifdef _SC_XOPEN_XPG3
7834 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7835 #endif
7836 #ifdef _SC_XOPEN_XPG4
7837 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7838 #endif
7841 static int
7842 conv_sysconf_confname(PyObject *arg, int *valuep)
7844 return conv_confname(arg, valuep, posix_constants_sysconf,
7845 sizeof(posix_constants_sysconf)
7846 / sizeof(struct constdef));
7849 PyDoc_STRVAR(posix_sysconf__doc__,
7850 "sysconf(name) -> integer\n\n\
7851 Return an integer-valued system configuration variable.");
7853 static PyObject *
7854 posix_sysconf(PyObject *self, PyObject *args)
7856 PyObject *result = NULL;
7857 int name;
7859 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7860 int value;
7862 errno = 0;
7863 value = sysconf(name);
7864 if (value == -1 && errno != 0)
7865 posix_error();
7866 else
7867 result = PyInt_FromLong(value);
7869 return result;
7871 #endif
7874 /* This code is used to ensure that the tables of configuration value names
7875 * are in sorted order as required by conv_confname(), and also to build the
7876 * the exported dictionaries that are used to publish information about the
7877 * names available on the host platform.
7879 * Sorting the table at runtime ensures that the table is properly ordered
7880 * when used, even for platforms we're not able to test on. It also makes
7881 * it easier to add additional entries to the tables.
7884 static int
7885 cmp_constdefs(const void *v1, const void *v2)
7887 const struct constdef *c1 =
7888 (const struct constdef *) v1;
7889 const struct constdef *c2 =
7890 (const struct constdef *) v2;
7892 return strcmp(c1->name, c2->name);
7895 static int
7896 setup_confname_table(struct constdef *table, size_t tablesize,
7897 char *tablename, PyObject *module)
7899 PyObject *d = NULL;
7900 size_t i;
7902 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7903 d = PyDict_New();
7904 if (d == NULL)
7905 return -1;
7907 for (i=0; i < tablesize; ++i) {
7908 PyObject *o = PyInt_FromLong(table[i].value);
7909 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7910 Py_XDECREF(o);
7911 Py_DECREF(d);
7912 return -1;
7914 Py_DECREF(o);
7916 return PyModule_AddObject(module, tablename, d);
7919 /* Return -1 on failure, 0 on success. */
7920 static int
7921 setup_confname_tables(PyObject *module)
7923 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7924 if (setup_confname_table(posix_constants_pathconf,
7925 sizeof(posix_constants_pathconf)
7926 / sizeof(struct constdef),
7927 "pathconf_names", module))
7928 return -1;
7929 #endif
7930 #ifdef HAVE_CONFSTR
7931 if (setup_confname_table(posix_constants_confstr,
7932 sizeof(posix_constants_confstr)
7933 / sizeof(struct constdef),
7934 "confstr_names", module))
7935 return -1;
7936 #endif
7937 #ifdef HAVE_SYSCONF
7938 if (setup_confname_table(posix_constants_sysconf,
7939 sizeof(posix_constants_sysconf)
7940 / sizeof(struct constdef),
7941 "sysconf_names", module))
7942 return -1;
7943 #endif
7944 return 0;
7948 PyDoc_STRVAR(posix_abort__doc__,
7949 "abort() -> does not return!\n\n\
7950 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
7951 in the hardest way possible on the hosting operating system.");
7953 static PyObject *
7954 posix_abort(PyObject *self, PyObject *noargs)
7956 abort();
7957 /*NOTREACHED*/
7958 Py_FatalError("abort() called from Python code didn't abort!");
7959 return NULL;
7962 #ifdef MS_WINDOWS
7963 PyDoc_STRVAR(win32_startfile__doc__,
7964 "startfile(filepath [, operation]) - Start a file with its associated\n\
7965 application.\n\
7967 When \"operation\" is not specified or \"open\", this acts like\n\
7968 double-clicking the file in Explorer, or giving the file name as an\n\
7969 argument to the DOS \"start\" command: the file is opened with whatever\n\
7970 application (if any) its extension is associated.\n\
7971 When another \"operation\" is given, it specifies what should be done with\n\
7972 the file. A typical operation is \"print\".\n\
7974 startfile returns as soon as the associated application is launched.\n\
7975 There is no option to wait for the application to close, and no way\n\
7976 to retrieve the application's exit status.\n\
7978 The filepath is relative to the current directory. If you want to use\n\
7979 an absolute path, make sure the first character is not a slash (\"/\");\n\
7980 the underlying Win32 ShellExecute function doesn't work if it is.");
7982 static PyObject *
7983 win32_startfile(PyObject *self, PyObject *args)
7985 char *filepath;
7986 char *operation = NULL;
7987 HINSTANCE rc;
7988 #ifdef Py_WIN_WIDE_FILENAMES
7989 if (unicode_file_names()) {
7990 PyObject *unipath, *woperation = NULL;
7991 if (!PyArg_ParseTuple(args, "U|s:startfile",
7992 &unipath, &operation)) {
7993 PyErr_Clear();
7994 goto normal;
7998 if (operation) {
7999 woperation = PyUnicode_DecodeASCII(operation,
8000 strlen(operation), NULL);
8001 if (!woperation) {
8002 PyErr_Clear();
8003 operation = NULL;
8004 goto normal;
8008 Py_BEGIN_ALLOW_THREADS
8009 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8010 PyUnicode_AS_UNICODE(unipath),
8011 NULL, NULL, SW_SHOWNORMAL);
8012 Py_END_ALLOW_THREADS
8014 Py_XDECREF(woperation);
8015 if (rc <= (HINSTANCE)32) {
8016 PyObject *errval = win32_error_unicode("startfile",
8017 PyUnicode_AS_UNICODE(unipath));
8018 return errval;
8020 Py_INCREF(Py_None);
8021 return Py_None;
8023 #endif
8025 normal:
8026 if (!PyArg_ParseTuple(args, "et|s:startfile",
8027 Py_FileSystemDefaultEncoding, &filepath,
8028 &operation))
8029 return NULL;
8030 Py_BEGIN_ALLOW_THREADS
8031 rc = ShellExecute((HWND)0, operation, filepath,
8032 NULL, NULL, SW_SHOWNORMAL);
8033 Py_END_ALLOW_THREADS
8034 if (rc <= (HINSTANCE)32) {
8035 PyObject *errval = win32_error("startfile", filepath);
8036 PyMem_Free(filepath);
8037 return errval;
8039 PyMem_Free(filepath);
8040 Py_INCREF(Py_None);
8041 return Py_None;
8043 #endif
8045 #ifdef HAVE_GETLOADAVG
8046 PyDoc_STRVAR(posix_getloadavg__doc__,
8047 "getloadavg() -> (float, float, float)\n\n\
8048 Return the number of processes in the system run queue averaged over\n\
8049 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8050 was unobtainable");
8052 static PyObject *
8053 posix_getloadavg(PyObject *self, PyObject *noargs)
8055 double loadavg[3];
8056 if (getloadavg(loadavg, 3)!=3) {
8057 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8058 return NULL;
8059 } else
8060 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8062 #endif
8064 #ifdef MS_WINDOWS
8066 PyDoc_STRVAR(win32_urandom__doc__,
8067 "urandom(n) -> str\n\n\
8068 Return a string of n random bytes suitable for cryptographic use.");
8070 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8071 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8072 DWORD dwFlags );
8073 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8074 BYTE *pbBuffer );
8076 static CRYPTGENRANDOM pCryptGenRandom = NULL;
8077 static HCRYPTPROV hCryptProv = 0;
8079 static PyObject*
8080 win32_urandom(PyObject *self, PyObject *args)
8082 int howMany;
8083 PyObject* result;
8085 /* Read arguments */
8086 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8087 return NULL;
8088 if (howMany < 0)
8089 return PyErr_Format(PyExc_ValueError,
8090 "negative argument not allowed");
8092 if (hCryptProv == 0) {
8093 HINSTANCE hAdvAPI32 = NULL;
8094 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
8096 /* Obtain handle to the DLL containing CryptoAPI
8097 This should not fail */
8098 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8099 if(hAdvAPI32 == NULL)
8100 return win32_error("GetModuleHandle", NULL);
8102 /* Obtain pointers to the CryptoAPI functions
8103 This will fail on some early versions of Win95 */
8104 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8105 hAdvAPI32,
8106 "CryptAcquireContextA");
8107 if (pCryptAcquireContext == NULL)
8108 return PyErr_Format(PyExc_NotImplementedError,
8109 "CryptAcquireContextA not found");
8111 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8112 hAdvAPI32, "CryptGenRandom");
8113 if (pCryptGenRandom == NULL)
8114 return PyErr_Format(PyExc_NotImplementedError,
8115 "CryptGenRandom not found");
8117 /* Acquire context */
8118 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8119 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8120 return win32_error("CryptAcquireContext", NULL);
8123 /* Allocate bytes */
8124 result = PyString_FromStringAndSize(NULL, howMany);
8125 if (result != NULL) {
8126 /* Get random data */
8127 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8128 PyString_AS_STRING(result))) {
8129 Py_DECREF(result);
8130 return win32_error("CryptGenRandom", NULL);
8133 return result;
8135 #endif
8137 #ifdef __VMS
8138 /* Use openssl random routine */
8139 #include <openssl/rand.h>
8140 PyDoc_STRVAR(vms_urandom__doc__,
8141 "urandom(n) -> str\n\n\
8142 Return a string of n random bytes suitable for cryptographic use.");
8144 static PyObject*
8145 vms_urandom(PyObject *self, PyObject *args)
8147 int howMany;
8148 PyObject* result;
8150 /* Read arguments */
8151 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8152 return NULL;
8153 if (howMany < 0)
8154 return PyErr_Format(PyExc_ValueError,
8155 "negative argument not allowed");
8157 /* Allocate bytes */
8158 result = PyString_FromStringAndSize(NULL, howMany);
8159 if (result != NULL) {
8160 /* Get random data */
8161 if (RAND_pseudo_bytes((unsigned char*)
8162 PyString_AS_STRING(result),
8163 howMany) < 0) {
8164 Py_DECREF(result);
8165 return PyErr_Format(PyExc_ValueError,
8166 "RAND_pseudo_bytes");
8169 return result;
8171 #endif
8173 static PyMethodDef posix_methods[] = {
8174 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8175 #ifdef HAVE_TTYNAME
8176 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8177 #endif
8178 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8179 #ifdef HAVE_CHFLAGS
8180 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8181 #endif /* HAVE_CHFLAGS */
8182 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
8183 #ifdef HAVE_CHOWN
8184 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
8185 #endif /* HAVE_CHOWN */
8186 #ifdef HAVE_LCHFLAGS
8187 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8188 #endif /* HAVE_LCHFLAGS */
8189 #ifdef HAVE_LCHOWN
8190 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8191 #endif /* HAVE_LCHOWN */
8192 #ifdef HAVE_CHROOT
8193 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8194 #endif
8195 #ifdef HAVE_CTERMID
8196 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
8197 #endif
8198 #ifdef HAVE_GETCWD
8199 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
8200 #ifdef Py_USING_UNICODE
8201 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
8202 #endif
8203 #endif
8204 #ifdef HAVE_LINK
8205 {"link", posix_link, METH_VARARGS, posix_link__doc__},
8206 #endif /* HAVE_LINK */
8207 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8208 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8209 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
8210 #ifdef HAVE_NICE
8211 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
8212 #endif /* HAVE_NICE */
8213 #ifdef HAVE_READLINK
8214 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
8215 #endif /* HAVE_READLINK */
8216 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8217 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8218 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8219 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
8220 #ifdef HAVE_SYMLINK
8221 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
8222 #endif /* HAVE_SYMLINK */
8223 #ifdef HAVE_SYSTEM
8224 {"system", posix_system, METH_VARARGS, posix_system__doc__},
8225 #endif
8226 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
8227 #ifdef HAVE_UNAME
8228 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
8229 #endif /* HAVE_UNAME */
8230 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8231 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8232 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
8233 #ifdef HAVE_TIMES
8234 {"times", posix_times, METH_NOARGS, posix_times__doc__},
8235 #endif /* HAVE_TIMES */
8236 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
8237 #ifdef HAVE_EXECV
8238 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8239 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
8240 #endif /* HAVE_EXECV */
8241 #ifdef HAVE_SPAWNV
8242 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8243 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
8244 #if defined(PYOS_OS2)
8245 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8246 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8247 #endif /* PYOS_OS2 */
8248 #endif /* HAVE_SPAWNV */
8249 #ifdef HAVE_FORK1
8250 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
8251 #endif /* HAVE_FORK1 */
8252 #ifdef HAVE_FORK
8253 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
8254 #endif /* HAVE_FORK */
8255 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8256 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
8257 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
8258 #ifdef HAVE_FORKPTY
8259 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
8260 #endif /* HAVE_FORKPTY */
8261 #ifdef HAVE_GETEGID
8262 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
8263 #endif /* HAVE_GETEGID */
8264 #ifdef HAVE_GETEUID
8265 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
8266 #endif /* HAVE_GETEUID */
8267 #ifdef HAVE_GETGID
8268 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
8269 #endif /* HAVE_GETGID */
8270 #ifdef HAVE_GETGROUPS
8271 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
8272 #endif
8273 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
8274 #ifdef HAVE_GETPGRP
8275 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
8276 #endif /* HAVE_GETPGRP */
8277 #ifdef HAVE_GETPPID
8278 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
8279 #endif /* HAVE_GETPPID */
8280 #ifdef HAVE_GETUID
8281 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
8282 #endif /* HAVE_GETUID */
8283 #ifdef HAVE_GETLOGIN
8284 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
8285 #endif
8286 #ifdef HAVE_KILL
8287 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
8288 #endif /* HAVE_KILL */
8289 #ifdef HAVE_KILLPG
8290 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8291 #endif /* HAVE_KILLPG */
8292 #ifdef HAVE_PLOCK
8293 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
8294 #endif /* HAVE_PLOCK */
8295 #ifdef HAVE_POPEN
8296 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
8297 #ifdef MS_WINDOWS
8298 {"popen2", win32_popen2, METH_VARARGS},
8299 {"popen3", win32_popen3, METH_VARARGS},
8300 {"popen4", win32_popen4, METH_VARARGS},
8301 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8302 #else
8303 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8304 {"popen2", os2emx_popen2, METH_VARARGS},
8305 {"popen3", os2emx_popen3, METH_VARARGS},
8306 {"popen4", os2emx_popen4, METH_VARARGS},
8307 #endif
8308 #endif
8309 #endif /* HAVE_POPEN */
8310 #ifdef HAVE_SETUID
8311 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
8312 #endif /* HAVE_SETUID */
8313 #ifdef HAVE_SETEUID
8314 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8315 #endif /* HAVE_SETEUID */
8316 #ifdef HAVE_SETEGID
8317 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8318 #endif /* HAVE_SETEGID */
8319 #ifdef HAVE_SETREUID
8320 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8321 #endif /* HAVE_SETREUID */
8322 #ifdef HAVE_SETREGID
8323 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8324 #endif /* HAVE_SETREGID */
8325 #ifdef HAVE_SETGID
8326 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
8327 #endif /* HAVE_SETGID */
8328 #ifdef HAVE_SETGROUPS
8329 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
8330 #endif /* HAVE_SETGROUPS */
8331 #ifdef HAVE_GETPGID
8332 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8333 #endif /* HAVE_GETPGID */
8334 #ifdef HAVE_SETPGRP
8335 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
8336 #endif /* HAVE_SETPGRP */
8337 #ifdef HAVE_WAIT
8338 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
8339 #endif /* HAVE_WAIT */
8340 #ifdef HAVE_WAIT3
8341 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8342 #endif /* HAVE_WAIT3 */
8343 #ifdef HAVE_WAIT4
8344 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8345 #endif /* HAVE_WAIT4 */
8346 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
8347 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
8348 #endif /* HAVE_WAITPID */
8349 #ifdef HAVE_GETSID
8350 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8351 #endif /* HAVE_GETSID */
8352 #ifdef HAVE_SETSID
8353 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
8354 #endif /* HAVE_SETSID */
8355 #ifdef HAVE_SETPGID
8356 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
8357 #endif /* HAVE_SETPGID */
8358 #ifdef HAVE_TCGETPGRP
8359 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
8360 #endif /* HAVE_TCGETPGRP */
8361 #ifdef HAVE_TCSETPGRP
8362 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
8363 #endif /* HAVE_TCSETPGRP */
8364 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8365 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8366 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8367 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8368 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8369 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8370 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8371 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8372 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8373 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
8374 #ifdef HAVE_PIPE
8375 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
8376 #endif
8377 #ifdef HAVE_MKFIFO
8378 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
8379 #endif
8380 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8381 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8382 #endif
8383 #ifdef HAVE_DEVICE_MACROS
8384 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8385 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8386 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8387 #endif
8388 #ifdef HAVE_FTRUNCATE
8389 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
8390 #endif
8391 #ifdef HAVE_PUTENV
8392 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
8393 #endif
8394 #ifdef HAVE_UNSETENV
8395 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8396 #endif
8397 #ifdef HAVE_STRERROR
8398 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
8399 #endif
8400 #ifdef HAVE_FCHDIR
8401 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8402 #endif
8403 #ifdef HAVE_FSYNC
8404 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
8405 #endif
8406 #ifdef HAVE_FDATASYNC
8407 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
8408 #endif
8409 #ifdef HAVE_SYS_WAIT_H
8410 #ifdef WCOREDUMP
8411 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8412 #endif /* WCOREDUMP */
8413 #ifdef WIFCONTINUED
8414 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8415 #endif /* WIFCONTINUED */
8416 #ifdef WIFSTOPPED
8417 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
8418 #endif /* WIFSTOPPED */
8419 #ifdef WIFSIGNALED
8420 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
8421 #endif /* WIFSIGNALED */
8422 #ifdef WIFEXITED
8423 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
8424 #endif /* WIFEXITED */
8425 #ifdef WEXITSTATUS
8426 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
8427 #endif /* WEXITSTATUS */
8428 #ifdef WTERMSIG
8429 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
8430 #endif /* WTERMSIG */
8431 #ifdef WSTOPSIG
8432 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
8433 #endif /* WSTOPSIG */
8434 #endif /* HAVE_SYS_WAIT_H */
8435 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
8436 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
8437 #endif
8438 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
8439 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
8440 #endif
8441 #ifdef HAVE_TMPFILE
8442 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
8443 #endif
8444 #ifdef HAVE_TEMPNAM
8445 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8446 #endif
8447 #ifdef HAVE_TMPNAM
8448 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
8449 #endif
8450 #ifdef HAVE_CONFSTR
8451 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8452 #endif
8453 #ifdef HAVE_SYSCONF
8454 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8455 #endif
8456 #ifdef HAVE_FPATHCONF
8457 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8458 #endif
8459 #ifdef HAVE_PATHCONF
8460 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8461 #endif
8462 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
8463 #ifdef MS_WINDOWS
8464 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8465 #endif
8466 #ifdef HAVE_GETLOADAVG
8467 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
8468 #endif
8469 #ifdef MS_WINDOWS
8470 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8471 #endif
8472 #ifdef __VMS
8473 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8474 #endif
8475 {NULL, NULL} /* Sentinel */
8479 static int
8480 ins(PyObject *module, char *symbol, long value)
8482 return PyModule_AddIntConstant(module, symbol, value);
8485 #if defined(PYOS_OS2)
8486 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8487 static int insertvalues(PyObject *module)
8489 APIRET rc;
8490 ULONG values[QSV_MAX+1];
8491 PyObject *v;
8492 char *ver, tmp[50];
8494 Py_BEGIN_ALLOW_THREADS
8495 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
8496 Py_END_ALLOW_THREADS
8498 if (rc != NO_ERROR) {
8499 os2_error(rc);
8500 return -1;
8503 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8504 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8505 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8506 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8507 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8508 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8509 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
8511 switch (values[QSV_VERSION_MINOR]) {
8512 case 0: ver = "2.00"; break;
8513 case 10: ver = "2.10"; break;
8514 case 11: ver = "2.11"; break;
8515 case 30: ver = "3.00"; break;
8516 case 40: ver = "4.00"; break;
8517 case 50: ver = "5.00"; break;
8518 default:
8519 PyOS_snprintf(tmp, sizeof(tmp),
8520 "%d-%d", values[QSV_VERSION_MAJOR],
8521 values[QSV_VERSION_MINOR]);
8522 ver = &tmp[0];
8525 /* Add Indicator of the Version of the Operating System */
8526 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
8527 return -1;
8529 /* Add Indicator of Which Drive was Used to Boot the System */
8530 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8531 tmp[1] = ':';
8532 tmp[2] = '\0';
8534 return PyModule_AddStringConstant(module, "bootdrive", tmp);
8536 #endif
8538 static int
8539 all_ins(PyObject *d)
8541 #ifdef F_OK
8542 if (ins(d, "F_OK", (long)F_OK)) return -1;
8543 #endif
8544 #ifdef R_OK
8545 if (ins(d, "R_OK", (long)R_OK)) return -1;
8546 #endif
8547 #ifdef W_OK
8548 if (ins(d, "W_OK", (long)W_OK)) return -1;
8549 #endif
8550 #ifdef X_OK
8551 if (ins(d, "X_OK", (long)X_OK)) return -1;
8552 #endif
8553 #ifdef NGROUPS_MAX
8554 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8555 #endif
8556 #ifdef TMP_MAX
8557 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8558 #endif
8559 #ifdef WCONTINUED
8560 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8561 #endif
8562 #ifdef WNOHANG
8563 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
8564 #endif
8565 #ifdef WUNTRACED
8566 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8567 #endif
8568 #ifdef O_RDONLY
8569 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8570 #endif
8571 #ifdef O_WRONLY
8572 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8573 #endif
8574 #ifdef O_RDWR
8575 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8576 #endif
8577 #ifdef O_NDELAY
8578 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8579 #endif
8580 #ifdef O_NONBLOCK
8581 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8582 #endif
8583 #ifdef O_APPEND
8584 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8585 #endif
8586 #ifdef O_DSYNC
8587 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8588 #endif
8589 #ifdef O_RSYNC
8590 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8591 #endif
8592 #ifdef O_SYNC
8593 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8594 #endif
8595 #ifdef O_NOCTTY
8596 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8597 #endif
8598 #ifdef O_CREAT
8599 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8600 #endif
8601 #ifdef O_EXCL
8602 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8603 #endif
8604 #ifdef O_TRUNC
8605 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8606 #endif
8607 #ifdef O_BINARY
8608 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8609 #endif
8610 #ifdef O_TEXT
8611 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8612 #endif
8613 #ifdef O_LARGEFILE
8614 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8615 #endif
8616 #ifdef O_SHLOCK
8617 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8618 #endif
8619 #ifdef O_EXLOCK
8620 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8621 #endif
8623 /* MS Windows */
8624 #ifdef O_NOINHERIT
8625 /* Don't inherit in child processes. */
8626 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8627 #endif
8628 #ifdef _O_SHORT_LIVED
8629 /* Optimize for short life (keep in memory). */
8630 /* MS forgot to define this one with a non-underscore form too. */
8631 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8632 #endif
8633 #ifdef O_TEMPORARY
8634 /* Automatically delete when last handle is closed. */
8635 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8636 #endif
8637 #ifdef O_RANDOM
8638 /* Optimize for random access. */
8639 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8640 #endif
8641 #ifdef O_SEQUENTIAL
8642 /* Optimize for sequential access. */
8643 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8644 #endif
8646 /* GNU extensions. */
8647 #ifdef O_DIRECT
8648 /* Direct disk access. */
8649 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8650 #endif
8651 #ifdef O_DIRECTORY
8652 /* Must be a directory. */
8653 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8654 #endif
8655 #ifdef O_NOFOLLOW
8656 /* Do not follow links. */
8657 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8658 #endif
8660 /* These come from sysexits.h */
8661 #ifdef EX_OK
8662 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
8663 #endif /* EX_OK */
8664 #ifdef EX_USAGE
8665 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
8666 #endif /* EX_USAGE */
8667 #ifdef EX_DATAERR
8668 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
8669 #endif /* EX_DATAERR */
8670 #ifdef EX_NOINPUT
8671 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
8672 #endif /* EX_NOINPUT */
8673 #ifdef EX_NOUSER
8674 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
8675 #endif /* EX_NOUSER */
8676 #ifdef EX_NOHOST
8677 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
8678 #endif /* EX_NOHOST */
8679 #ifdef EX_UNAVAILABLE
8680 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
8681 #endif /* EX_UNAVAILABLE */
8682 #ifdef EX_SOFTWARE
8683 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
8684 #endif /* EX_SOFTWARE */
8685 #ifdef EX_OSERR
8686 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
8687 #endif /* EX_OSERR */
8688 #ifdef EX_OSFILE
8689 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
8690 #endif /* EX_OSFILE */
8691 #ifdef EX_CANTCREAT
8692 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
8693 #endif /* EX_CANTCREAT */
8694 #ifdef EX_IOERR
8695 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
8696 #endif /* EX_IOERR */
8697 #ifdef EX_TEMPFAIL
8698 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
8699 #endif /* EX_TEMPFAIL */
8700 #ifdef EX_PROTOCOL
8701 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
8702 #endif /* EX_PROTOCOL */
8703 #ifdef EX_NOPERM
8704 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
8705 #endif /* EX_NOPERM */
8706 #ifdef EX_CONFIG
8707 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
8708 #endif /* EX_CONFIG */
8709 #ifdef EX_NOTFOUND
8710 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
8711 #endif /* EX_NOTFOUND */
8713 #ifdef HAVE_SPAWNV
8714 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8715 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8716 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8717 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8718 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8719 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8720 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8721 if (ins(d, "P_PM", (long)P_PM)) return -1;
8722 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8723 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8724 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8725 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8726 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8727 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8728 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8729 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8730 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8731 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8732 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8733 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8734 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8735 #else
8736 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8737 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8738 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8739 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8740 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
8741 #endif
8742 #endif
8744 #if defined(PYOS_OS2)
8745 if (insertvalues(d)) return -1;
8746 #endif
8747 return 0;
8751 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
8752 #define INITFUNC initnt
8753 #define MODNAME "nt"
8755 #elif defined(PYOS_OS2)
8756 #define INITFUNC initos2
8757 #define MODNAME "os2"
8759 #else
8760 #define INITFUNC initposix
8761 #define MODNAME "posix"
8762 #endif
8764 PyMODINIT_FUNC
8765 INITFUNC(void)
8767 PyObject *m, *v;
8769 m = Py_InitModule3(MODNAME,
8770 posix_methods,
8771 posix__doc__);
8772 if (m == NULL)
8773 return;
8775 /* Initialize environ dictionary */
8776 v = convertenviron();
8777 Py_XINCREF(v);
8778 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8779 return;
8780 Py_DECREF(v);
8782 if (all_ins(m))
8783 return;
8785 if (setup_confname_tables(m))
8786 return;
8788 Py_INCREF(PyExc_OSError);
8789 PyModule_AddObject(m, "error", PyExc_OSError);
8791 #ifdef HAVE_PUTENV
8792 if (posix_putenv_garbage == NULL)
8793 posix_putenv_garbage = PyDict_New();
8794 #endif
8796 if (!initialized) {
8797 stat_result_desc.name = MODNAME ".stat_result";
8798 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8799 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8800 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8801 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8802 structseq_new = StatResultType.tp_new;
8803 StatResultType.tp_new = statresult_new;
8805 statvfs_result_desc.name = MODNAME ".statvfs_result";
8806 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8808 Py_INCREF((PyObject*) &StatResultType);
8809 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8810 Py_INCREF((PyObject*) &StatVFSResultType);
8811 PyModule_AddObject(m, "statvfs_result",
8812 (PyObject*) &StatVFSResultType);
8813 initialized = 1;
8815 #ifdef __APPLE__
8817 * Step 2 of weak-linking support on Mac OS X.
8819 * The code below removes functions that are not available on the
8820 * currently active platform.
8822 * This block allow one to use a python binary that was build on
8823 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8824 * OSX 10.4.
8826 #ifdef HAVE_FSTATVFS
8827 if (fstatvfs == NULL) {
8828 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8829 return;
8832 #endif /* HAVE_FSTATVFS */
8834 #ifdef HAVE_STATVFS
8835 if (statvfs == NULL) {
8836 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8837 return;
8840 #endif /* HAVE_STATVFS */
8842 # ifdef HAVE_LCHOWN
8843 if (lchown == NULL) {
8844 if (PyObject_DelAttrString(m, "lchown") == -1) {
8845 return;
8848 #endif /* HAVE_LCHOWN */
8851 #endif /* __APPLE__ */
8855 #ifdef __cplusplus
8857 #endif