Updates of recent changes to logging.
[python.git] / Modules / posixmodule.c
blob2b62983bd22ecd4a60a7dd5b2bc5e15f32bdba4d
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 = t->ob_type->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) return NULL;
5788 if (PyUnicode_Check(v)) {
5789 arg_is_unicode = 1;
5791 Py_DECREF(v);
5792 #endif
5794 Py_BEGIN_ALLOW_THREADS
5795 n = readlink(path, buf, (int) sizeof buf);
5796 Py_END_ALLOW_THREADS
5797 if (n < 0)
5798 return posix_error_with_filename(path);
5800 v = PyString_FromStringAndSize(buf, n);
5801 #ifdef Py_USING_UNICODE
5802 if (arg_is_unicode) {
5803 PyObject *w;
5805 w = PyUnicode_FromEncodedObject(v,
5806 Py_FileSystemDefaultEncoding,
5807 "strict");
5808 if (w != NULL) {
5809 Py_DECREF(v);
5810 v = w;
5812 else {
5813 /* fall back to the original byte string, as
5814 discussed in patch #683592 */
5815 PyErr_Clear();
5818 #endif
5819 return v;
5821 #endif /* HAVE_READLINK */
5824 #ifdef HAVE_SYMLINK
5825 PyDoc_STRVAR(posix_symlink__doc__,
5826 "symlink(src, dst)\n\n\
5827 Create a symbolic link pointing to src named dst.");
5829 static PyObject *
5830 posix_symlink(PyObject *self, PyObject *args)
5832 return posix_2str(args, "etet:symlink", symlink);
5834 #endif /* HAVE_SYMLINK */
5837 #ifdef HAVE_TIMES
5838 #ifndef HZ
5839 #define HZ 60 /* Universal constant :-) */
5840 #endif /* HZ */
5842 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
5843 static long
5844 system_uptime(void)
5846 ULONG value = 0;
5848 Py_BEGIN_ALLOW_THREADS
5849 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5850 Py_END_ALLOW_THREADS
5852 return value;
5855 static PyObject *
5856 posix_times(PyObject *self, PyObject *noargs)
5858 /* Currently Only Uptime is Provided -- Others Later */
5859 return Py_BuildValue("ddddd",
5860 (double)0 /* t.tms_utime / HZ */,
5861 (double)0 /* t.tms_stime / HZ */,
5862 (double)0 /* t.tms_cutime / HZ */,
5863 (double)0 /* t.tms_cstime / HZ */,
5864 (double)system_uptime() / 1000);
5866 #else /* not OS2 */
5867 static PyObject *
5868 posix_times(PyObject *self, PyObject *noargs)
5870 struct tms t;
5871 clock_t c;
5872 errno = 0;
5873 c = times(&t);
5874 if (c == (clock_t) -1)
5875 return posix_error();
5876 return Py_BuildValue("ddddd",
5877 (double)t.tms_utime / HZ,
5878 (double)t.tms_stime / HZ,
5879 (double)t.tms_cutime / HZ,
5880 (double)t.tms_cstime / HZ,
5881 (double)c / HZ);
5883 #endif /* not OS2 */
5884 #endif /* HAVE_TIMES */
5887 #ifdef MS_WINDOWS
5888 #define HAVE_TIMES /* so the method table will pick it up */
5889 static PyObject *
5890 posix_times(PyObject *self, PyObject *noargs)
5892 FILETIME create, exit, kernel, user;
5893 HANDLE hProc;
5894 hProc = GetCurrentProcess();
5895 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5896 /* The fields of a FILETIME structure are the hi and lo part
5897 of a 64-bit value expressed in 100 nanosecond units.
5898 1e7 is one second in such units; 1e-7 the inverse.
5899 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5901 return Py_BuildValue(
5902 "ddddd",
5903 (double)(kernel.dwHighDateTime*429.4967296 +
5904 kernel.dwLowDateTime*1e-7),
5905 (double)(user.dwHighDateTime*429.4967296 +
5906 user.dwLowDateTime*1e-7),
5907 (double)0,
5908 (double)0,
5909 (double)0);
5911 #endif /* MS_WINDOWS */
5913 #ifdef HAVE_TIMES
5914 PyDoc_STRVAR(posix_times__doc__,
5915 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
5916 Return a tuple of floating point numbers indicating process times.");
5917 #endif
5920 #ifdef HAVE_GETSID
5921 PyDoc_STRVAR(posix_getsid__doc__,
5922 "getsid(pid) -> sid\n\n\
5923 Call the system call getsid().");
5925 static PyObject *
5926 posix_getsid(PyObject *self, PyObject *args)
5928 int pid, sid;
5929 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5930 return NULL;
5931 sid = getsid(pid);
5932 if (sid < 0)
5933 return posix_error();
5934 return PyInt_FromLong((long)sid);
5936 #endif /* HAVE_GETSID */
5939 #ifdef HAVE_SETSID
5940 PyDoc_STRVAR(posix_setsid__doc__,
5941 "setsid()\n\n\
5942 Call the system call setsid().");
5944 static PyObject *
5945 posix_setsid(PyObject *self, PyObject *noargs)
5947 if (setsid() < 0)
5948 return posix_error();
5949 Py_INCREF(Py_None);
5950 return Py_None;
5952 #endif /* HAVE_SETSID */
5954 #ifdef HAVE_SETPGID
5955 PyDoc_STRVAR(posix_setpgid__doc__,
5956 "setpgid(pid, pgrp)\n\n\
5957 Call the system call setpgid().");
5959 static PyObject *
5960 posix_setpgid(PyObject *self, PyObject *args)
5962 int pid, pgrp;
5963 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
5964 return NULL;
5965 if (setpgid(pid, pgrp) < 0)
5966 return posix_error();
5967 Py_INCREF(Py_None);
5968 return Py_None;
5970 #endif /* HAVE_SETPGID */
5973 #ifdef HAVE_TCGETPGRP
5974 PyDoc_STRVAR(posix_tcgetpgrp__doc__,
5975 "tcgetpgrp(fd) -> pgid\n\n\
5976 Return the process group associated with the terminal given by a fd.");
5978 static PyObject *
5979 posix_tcgetpgrp(PyObject *self, PyObject *args)
5981 int fd, pgid;
5982 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5983 return NULL;
5984 pgid = tcgetpgrp(fd);
5985 if (pgid < 0)
5986 return posix_error();
5987 return PyInt_FromLong((long)pgid);
5989 #endif /* HAVE_TCGETPGRP */
5992 #ifdef HAVE_TCSETPGRP
5993 PyDoc_STRVAR(posix_tcsetpgrp__doc__,
5994 "tcsetpgrp(fd, pgid)\n\n\
5995 Set the process group associated with the terminal given by a fd.");
5997 static PyObject *
5998 posix_tcsetpgrp(PyObject *self, PyObject *args)
6000 int fd, pgid;
6001 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
6002 return NULL;
6003 if (tcsetpgrp(fd, pgid) < 0)
6004 return posix_error();
6005 Py_INCREF(Py_None);
6006 return Py_None;
6008 #endif /* HAVE_TCSETPGRP */
6010 /* Functions acting on file descriptors */
6012 PyDoc_STRVAR(posix_open__doc__,
6013 "open(filename, flag [, mode=0777]) -> fd\n\n\
6014 Open a file (for low level IO).");
6016 static PyObject *
6017 posix_open(PyObject *self, PyObject *args)
6019 char *file = NULL;
6020 int flag;
6021 int mode = 0777;
6022 int fd;
6024 #ifdef MS_WINDOWS
6025 if (unicode_file_names()) {
6026 PyUnicodeObject *po;
6027 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6028 Py_BEGIN_ALLOW_THREADS
6029 /* PyUnicode_AS_UNICODE OK without thread
6030 lock as it is a simple dereference. */
6031 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6032 Py_END_ALLOW_THREADS
6033 if (fd < 0)
6034 return posix_error();
6035 return PyInt_FromLong((long)fd);
6037 /* Drop the argument parsing error as narrow strings
6038 are also valid. */
6039 PyErr_Clear();
6041 #endif
6043 if (!PyArg_ParseTuple(args, "eti|i",
6044 Py_FileSystemDefaultEncoding, &file,
6045 &flag, &mode))
6046 return NULL;
6048 Py_BEGIN_ALLOW_THREADS
6049 fd = open(file, flag, mode);
6050 Py_END_ALLOW_THREADS
6051 if (fd < 0)
6052 return posix_error_with_allocated_filename(file);
6053 PyMem_Free(file);
6054 return PyInt_FromLong((long)fd);
6058 PyDoc_STRVAR(posix_close__doc__,
6059 "close(fd)\n\n\
6060 Close a file descriptor (for low level IO).");
6062 static PyObject *
6063 posix_close(PyObject *self, PyObject *args)
6065 int fd, res;
6066 if (!PyArg_ParseTuple(args, "i:close", &fd))
6067 return NULL;
6068 Py_BEGIN_ALLOW_THREADS
6069 res = close(fd);
6070 Py_END_ALLOW_THREADS
6071 if (res < 0)
6072 return posix_error();
6073 Py_INCREF(Py_None);
6074 return Py_None;
6078 PyDoc_STRVAR(posix_dup__doc__,
6079 "dup(fd) -> fd2\n\n\
6080 Return a duplicate of a file descriptor.");
6082 static PyObject *
6083 posix_dup(PyObject *self, PyObject *args)
6085 int fd;
6086 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6087 return NULL;
6088 Py_BEGIN_ALLOW_THREADS
6089 fd = dup(fd);
6090 Py_END_ALLOW_THREADS
6091 if (fd < 0)
6092 return posix_error();
6093 return PyInt_FromLong((long)fd);
6097 PyDoc_STRVAR(posix_dup2__doc__,
6098 "dup2(old_fd, new_fd)\n\n\
6099 Duplicate file descriptor.");
6101 static PyObject *
6102 posix_dup2(PyObject *self, PyObject *args)
6104 int fd, fd2, res;
6105 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6106 return NULL;
6107 Py_BEGIN_ALLOW_THREADS
6108 res = dup2(fd, fd2);
6109 Py_END_ALLOW_THREADS
6110 if (res < 0)
6111 return posix_error();
6112 Py_INCREF(Py_None);
6113 return Py_None;
6117 PyDoc_STRVAR(posix_lseek__doc__,
6118 "lseek(fd, pos, how) -> newpos\n\n\
6119 Set the current position of a file descriptor.");
6121 static PyObject *
6122 posix_lseek(PyObject *self, PyObject *args)
6124 int fd, how;
6125 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6126 PY_LONG_LONG pos, res;
6127 #else
6128 off_t pos, res;
6129 #endif
6130 PyObject *posobj;
6131 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
6132 return NULL;
6133 #ifdef SEEK_SET
6134 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6135 switch (how) {
6136 case 0: how = SEEK_SET; break;
6137 case 1: how = SEEK_CUR; break;
6138 case 2: how = SEEK_END; break;
6140 #endif /* SEEK_END */
6142 #if !defined(HAVE_LARGEFILE_SUPPORT)
6143 pos = PyInt_AsLong(posobj);
6144 #else
6145 pos = PyLong_Check(posobj) ?
6146 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6147 #endif
6148 if (PyErr_Occurred())
6149 return NULL;
6151 Py_BEGIN_ALLOW_THREADS
6152 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6153 res = _lseeki64(fd, pos, how);
6154 #else
6155 res = lseek(fd, pos, how);
6156 #endif
6157 Py_END_ALLOW_THREADS
6158 if (res < 0)
6159 return posix_error();
6161 #if !defined(HAVE_LARGEFILE_SUPPORT)
6162 return PyInt_FromLong(res);
6163 #else
6164 return PyLong_FromLongLong(res);
6165 #endif
6169 PyDoc_STRVAR(posix_read__doc__,
6170 "read(fd, buffersize) -> string\n\n\
6171 Read a file descriptor.");
6173 static PyObject *
6174 posix_read(PyObject *self, PyObject *args)
6176 int fd, size, n;
6177 PyObject *buffer;
6178 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6179 return NULL;
6180 if (size < 0) {
6181 errno = EINVAL;
6182 return posix_error();
6184 buffer = PyString_FromStringAndSize((char *)NULL, size);
6185 if (buffer == NULL)
6186 return NULL;
6187 Py_BEGIN_ALLOW_THREADS
6188 n = read(fd, PyString_AsString(buffer), size);
6189 Py_END_ALLOW_THREADS
6190 if (n < 0) {
6191 Py_DECREF(buffer);
6192 return posix_error();
6194 if (n != size)
6195 _PyString_Resize(&buffer, n);
6196 return buffer;
6200 PyDoc_STRVAR(posix_write__doc__,
6201 "write(fd, string) -> byteswritten\n\n\
6202 Write a string to a file descriptor.");
6204 static PyObject *
6205 posix_write(PyObject *self, PyObject *args)
6207 int fd;
6208 Py_ssize_t size;
6209 char *buffer;
6211 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
6212 return NULL;
6213 Py_BEGIN_ALLOW_THREADS
6214 size = write(fd, buffer, (size_t)size);
6215 Py_END_ALLOW_THREADS
6216 if (size < 0)
6217 return posix_error();
6218 return PyInt_FromSsize_t(size);
6222 PyDoc_STRVAR(posix_fstat__doc__,
6223 "fstat(fd) -> stat result\n\n\
6224 Like stat(), but for an open file descriptor.");
6226 static PyObject *
6227 posix_fstat(PyObject *self, PyObject *args)
6229 int fd;
6230 STRUCT_STAT st;
6231 int res;
6232 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6233 return NULL;
6234 #ifdef __VMS
6235 /* on OpenVMS we must ensure that all bytes are written to the file */
6236 fsync(fd);
6237 #endif
6238 Py_BEGIN_ALLOW_THREADS
6239 res = FSTAT(fd, &st);
6240 Py_END_ALLOW_THREADS
6241 if (res != 0) {
6242 #ifdef MS_WINDOWS
6243 return win32_error("fstat", NULL);
6244 #else
6245 return posix_error();
6246 #endif
6249 return _pystat_fromstructstat(&st);
6253 PyDoc_STRVAR(posix_fdopen__doc__,
6254 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
6255 Return an open file object connected to a file descriptor.");
6257 static PyObject *
6258 posix_fdopen(PyObject *self, PyObject *args)
6260 int fd;
6261 char *orgmode = "r";
6262 int bufsize = -1;
6263 FILE *fp;
6264 PyObject *f;
6265 char *mode;
6266 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
6267 return NULL;
6269 /* Sanitize mode. See fileobject.c */
6270 mode = PyMem_MALLOC(strlen(orgmode)+3);
6271 if (!mode) {
6272 PyErr_NoMemory();
6273 return NULL;
6275 strcpy(mode, orgmode);
6276 if (_PyFile_SanitizeMode(mode)) {
6277 PyMem_FREE(mode);
6278 return NULL;
6280 Py_BEGIN_ALLOW_THREADS
6281 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6282 if (mode[0] == 'a') {
6283 /* try to make sure the O_APPEND flag is set */
6284 int flags;
6285 flags = fcntl(fd, F_GETFL);
6286 if (flags != -1)
6287 fcntl(fd, F_SETFL, flags | O_APPEND);
6288 fp = fdopen(fd, mode);
6289 if (fp == NULL && flags != -1)
6290 /* restore old mode if fdopen failed */
6291 fcntl(fd, F_SETFL, flags);
6292 } else {
6293 fp = fdopen(fd, mode);
6295 #else
6296 fp = fdopen(fd, mode);
6297 #endif
6298 Py_END_ALLOW_THREADS
6299 PyMem_FREE(mode);
6300 if (fp == NULL)
6301 return posix_error();
6302 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
6303 if (f != NULL)
6304 PyFile_SetBufSize(f, bufsize);
6305 return f;
6308 PyDoc_STRVAR(posix_isatty__doc__,
6309 "isatty(fd) -> bool\n\n\
6310 Return True if the file descriptor 'fd' is an open file descriptor\n\
6311 connected to the slave end of a terminal.");
6313 static PyObject *
6314 posix_isatty(PyObject *self, PyObject *args)
6316 int fd;
6317 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6318 return NULL;
6319 return PyBool_FromLong(isatty(fd));
6322 #ifdef HAVE_PIPE
6323 PyDoc_STRVAR(posix_pipe__doc__,
6324 "pipe() -> (read_end, write_end)\n\n\
6325 Create a pipe.");
6327 static PyObject *
6328 posix_pipe(PyObject *self, PyObject *noargs)
6330 #if defined(PYOS_OS2)
6331 HFILE read, write;
6332 APIRET rc;
6334 Py_BEGIN_ALLOW_THREADS
6335 rc = DosCreatePipe( &read, &write, 4096);
6336 Py_END_ALLOW_THREADS
6337 if (rc != NO_ERROR)
6338 return os2_error(rc);
6340 return Py_BuildValue("(ii)", read, write);
6341 #else
6342 #if !defined(MS_WINDOWS)
6343 int fds[2];
6344 int res;
6345 Py_BEGIN_ALLOW_THREADS
6346 res = pipe(fds);
6347 Py_END_ALLOW_THREADS
6348 if (res != 0)
6349 return posix_error();
6350 return Py_BuildValue("(ii)", fds[0], fds[1]);
6351 #else /* MS_WINDOWS */
6352 HANDLE read, write;
6353 int read_fd, write_fd;
6354 BOOL ok;
6355 Py_BEGIN_ALLOW_THREADS
6356 ok = CreatePipe(&read, &write, NULL, 0);
6357 Py_END_ALLOW_THREADS
6358 if (!ok)
6359 return win32_error("CreatePipe", NULL);
6360 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6361 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6362 return Py_BuildValue("(ii)", read_fd, write_fd);
6363 #endif /* MS_WINDOWS */
6364 #endif
6366 #endif /* HAVE_PIPE */
6369 #ifdef HAVE_MKFIFO
6370 PyDoc_STRVAR(posix_mkfifo__doc__,
6371 "mkfifo(filename [, mode=0666])\n\n\
6372 Create a FIFO (a POSIX named pipe).");
6374 static PyObject *
6375 posix_mkfifo(PyObject *self, PyObject *args)
6377 char *filename;
6378 int mode = 0666;
6379 int res;
6380 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
6381 return NULL;
6382 Py_BEGIN_ALLOW_THREADS
6383 res = mkfifo(filename, mode);
6384 Py_END_ALLOW_THREADS
6385 if (res < 0)
6386 return posix_error();
6387 Py_INCREF(Py_None);
6388 return Py_None;
6390 #endif
6393 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
6394 PyDoc_STRVAR(posix_mknod__doc__,
6395 "mknod(filename [, mode=0600, device])\n\n\
6396 Create a filesystem node (file, device special file or named pipe)\n\
6397 named filename. mode specifies both the permissions to use and the\n\
6398 type of node to be created, being combined (bitwise OR) with one of\n\
6399 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
6400 device defines the newly created device special file (probably using\n\
6401 os.makedev()), otherwise it is ignored.");
6404 static PyObject *
6405 posix_mknod(PyObject *self, PyObject *args)
6407 char *filename;
6408 int mode = 0600;
6409 int device = 0;
6410 int res;
6411 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
6412 return NULL;
6413 Py_BEGIN_ALLOW_THREADS
6414 res = mknod(filename, mode, device);
6415 Py_END_ALLOW_THREADS
6416 if (res < 0)
6417 return posix_error();
6418 Py_INCREF(Py_None);
6419 return Py_None;
6421 #endif
6423 #ifdef HAVE_DEVICE_MACROS
6424 PyDoc_STRVAR(posix_major__doc__,
6425 "major(device) -> major number\n\
6426 Extracts a device major number from a raw device number.");
6428 static PyObject *
6429 posix_major(PyObject *self, PyObject *args)
6431 int device;
6432 if (!PyArg_ParseTuple(args, "i:major", &device))
6433 return NULL;
6434 return PyInt_FromLong((long)major(device));
6437 PyDoc_STRVAR(posix_minor__doc__,
6438 "minor(device) -> minor number\n\
6439 Extracts a device minor number from a raw device number.");
6441 static PyObject *
6442 posix_minor(PyObject *self, PyObject *args)
6444 int device;
6445 if (!PyArg_ParseTuple(args, "i:minor", &device))
6446 return NULL;
6447 return PyInt_FromLong((long)minor(device));
6450 PyDoc_STRVAR(posix_makedev__doc__,
6451 "makedev(major, minor) -> device number\n\
6452 Composes a raw device number from the major and minor device numbers.");
6454 static PyObject *
6455 posix_makedev(PyObject *self, PyObject *args)
6457 int major, minor;
6458 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6459 return NULL;
6460 return PyInt_FromLong((long)makedev(major, minor));
6462 #endif /* device macros */
6465 #ifdef HAVE_FTRUNCATE
6466 PyDoc_STRVAR(posix_ftruncate__doc__,
6467 "ftruncate(fd, length)\n\n\
6468 Truncate a file to a specified length.");
6470 static PyObject *
6471 posix_ftruncate(PyObject *self, PyObject *args)
6473 int fd;
6474 off_t length;
6475 int res;
6476 PyObject *lenobj;
6478 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
6479 return NULL;
6481 #if !defined(HAVE_LARGEFILE_SUPPORT)
6482 length = PyInt_AsLong(lenobj);
6483 #else
6484 length = PyLong_Check(lenobj) ?
6485 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6486 #endif
6487 if (PyErr_Occurred())
6488 return NULL;
6490 Py_BEGIN_ALLOW_THREADS
6491 res = ftruncate(fd, length);
6492 Py_END_ALLOW_THREADS
6493 if (res < 0) {
6494 PyErr_SetFromErrno(PyExc_IOError);
6495 return NULL;
6497 Py_INCREF(Py_None);
6498 return Py_None;
6500 #endif
6502 #ifdef HAVE_PUTENV
6503 PyDoc_STRVAR(posix_putenv__doc__,
6504 "putenv(key, value)\n\n\
6505 Change or add an environment variable.");
6507 /* Save putenv() parameters as values here, so we can collect them when they
6508 * get re-set with another call for the same key. */
6509 static PyObject *posix_putenv_garbage;
6511 static PyObject *
6512 posix_putenv(PyObject *self, PyObject *args)
6514 char *s1, *s2;
6515 char *newenv;
6516 PyObject *newstr;
6517 size_t len;
6519 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
6520 return NULL;
6522 #if defined(PYOS_OS2)
6523 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6524 APIRET rc;
6526 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6527 if (rc != NO_ERROR)
6528 return os2_error(rc);
6530 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6531 APIRET rc;
6533 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6534 if (rc != NO_ERROR)
6535 return os2_error(rc);
6536 } else {
6537 #endif
6539 /* XXX This can leak memory -- not easy to fix :-( */
6540 len = strlen(s1) + strlen(s2) + 2;
6541 /* len includes space for a trailing \0; the size arg to
6542 PyString_FromStringAndSize does not count that */
6543 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
6544 if (newstr == NULL)
6545 return PyErr_NoMemory();
6546 newenv = PyString_AS_STRING(newstr);
6547 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6548 if (putenv(newenv)) {
6549 Py_DECREF(newstr);
6550 posix_error();
6551 return NULL;
6553 /* Install the first arg and newstr in posix_putenv_garbage;
6554 * this will cause previous value to be collected. This has to
6555 * happen after the real putenv() call because the old value
6556 * was still accessible until then. */
6557 if (PyDict_SetItem(posix_putenv_garbage,
6558 PyTuple_GET_ITEM(args, 0), newstr)) {
6559 /* really not much we can do; just leak */
6560 PyErr_Clear();
6562 else {
6563 Py_DECREF(newstr);
6566 #if defined(PYOS_OS2)
6568 #endif
6569 Py_INCREF(Py_None);
6570 return Py_None;
6572 #endif /* putenv */
6574 #ifdef HAVE_UNSETENV
6575 PyDoc_STRVAR(posix_unsetenv__doc__,
6576 "unsetenv(key)\n\n\
6577 Delete an environment variable.");
6579 static PyObject *
6580 posix_unsetenv(PyObject *self, PyObject *args)
6582 char *s1;
6584 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6585 return NULL;
6587 unsetenv(s1);
6589 /* Remove the key from posix_putenv_garbage;
6590 * this will cause it to be collected. This has to
6591 * happen after the real unsetenv() call because the
6592 * old value was still accessible until then.
6594 if (PyDict_DelItem(posix_putenv_garbage,
6595 PyTuple_GET_ITEM(args, 0))) {
6596 /* really not much we can do; just leak */
6597 PyErr_Clear();
6600 Py_INCREF(Py_None);
6601 return Py_None;
6603 #endif /* unsetenv */
6605 #ifdef HAVE_STRERROR
6606 PyDoc_STRVAR(posix_strerror__doc__,
6607 "strerror(code) -> string\n\n\
6608 Translate an error code to a message string.");
6610 static PyObject *
6611 posix_strerror(PyObject *self, PyObject *args)
6613 int code;
6614 char *message;
6615 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6616 return NULL;
6617 message = strerror(code);
6618 if (message == NULL) {
6619 PyErr_SetString(PyExc_ValueError,
6620 "strerror() argument out of range");
6621 return NULL;
6623 return PyString_FromString(message);
6625 #endif /* strerror */
6628 #ifdef HAVE_SYS_WAIT_H
6630 #ifdef WCOREDUMP
6631 PyDoc_STRVAR(posix_WCOREDUMP__doc__,
6632 "WCOREDUMP(status) -> bool\n\n\
6633 Return True if the process returning 'status' was dumped to a core file.");
6635 static PyObject *
6636 posix_WCOREDUMP(PyObject *self, PyObject *args)
6638 WAIT_TYPE status;
6639 WAIT_STATUS_INT(status) = 0;
6641 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6642 return NULL;
6644 return PyBool_FromLong(WCOREDUMP(status));
6646 #endif /* WCOREDUMP */
6648 #ifdef WIFCONTINUED
6649 PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
6650 "WIFCONTINUED(status) -> bool\n\n\
6651 Return True if the process returning 'status' was continued from a\n\
6652 job control stop.");
6654 static PyObject *
6655 posix_WIFCONTINUED(PyObject *self, PyObject *args)
6657 WAIT_TYPE status;
6658 WAIT_STATUS_INT(status) = 0;
6660 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6661 return NULL;
6663 return PyBool_FromLong(WIFCONTINUED(status));
6665 #endif /* WIFCONTINUED */
6667 #ifdef WIFSTOPPED
6668 PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
6669 "WIFSTOPPED(status) -> bool\n\n\
6670 Return True if the process returning 'status' was stopped.");
6672 static PyObject *
6673 posix_WIFSTOPPED(PyObject *self, PyObject *args)
6675 WAIT_TYPE status;
6676 WAIT_STATUS_INT(status) = 0;
6678 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6679 return NULL;
6681 return PyBool_FromLong(WIFSTOPPED(status));
6683 #endif /* WIFSTOPPED */
6685 #ifdef WIFSIGNALED
6686 PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
6687 "WIFSIGNALED(status) -> bool\n\n\
6688 Return True if the process returning 'status' was terminated by a signal.");
6690 static PyObject *
6691 posix_WIFSIGNALED(PyObject *self, PyObject *args)
6693 WAIT_TYPE status;
6694 WAIT_STATUS_INT(status) = 0;
6696 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6697 return NULL;
6699 return PyBool_FromLong(WIFSIGNALED(status));
6701 #endif /* WIFSIGNALED */
6703 #ifdef WIFEXITED
6704 PyDoc_STRVAR(posix_WIFEXITED__doc__,
6705 "WIFEXITED(status) -> bool\n\n\
6706 Return true if the process returning 'status' exited using the exit()\n\
6707 system call.");
6709 static PyObject *
6710 posix_WIFEXITED(PyObject *self, PyObject *args)
6712 WAIT_TYPE status;
6713 WAIT_STATUS_INT(status) = 0;
6715 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6716 return NULL;
6718 return PyBool_FromLong(WIFEXITED(status));
6720 #endif /* WIFEXITED */
6722 #ifdef WEXITSTATUS
6723 PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
6724 "WEXITSTATUS(status) -> integer\n\n\
6725 Return the process return code from 'status'.");
6727 static PyObject *
6728 posix_WEXITSTATUS(PyObject *self, PyObject *args)
6730 WAIT_TYPE status;
6731 WAIT_STATUS_INT(status) = 0;
6733 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6734 return NULL;
6736 return Py_BuildValue("i", WEXITSTATUS(status));
6738 #endif /* WEXITSTATUS */
6740 #ifdef WTERMSIG
6741 PyDoc_STRVAR(posix_WTERMSIG__doc__,
6742 "WTERMSIG(status) -> integer\n\n\
6743 Return the signal that terminated the process that provided the 'status'\n\
6744 value.");
6746 static PyObject *
6747 posix_WTERMSIG(PyObject *self, PyObject *args)
6749 WAIT_TYPE status;
6750 WAIT_STATUS_INT(status) = 0;
6752 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6753 return NULL;
6755 return Py_BuildValue("i", WTERMSIG(status));
6757 #endif /* WTERMSIG */
6759 #ifdef WSTOPSIG
6760 PyDoc_STRVAR(posix_WSTOPSIG__doc__,
6761 "WSTOPSIG(status) -> integer\n\n\
6762 Return the signal that stopped the process that provided\n\
6763 the 'status' value.");
6765 static PyObject *
6766 posix_WSTOPSIG(PyObject *self, PyObject *args)
6768 WAIT_TYPE status;
6769 WAIT_STATUS_INT(status) = 0;
6771 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6772 return NULL;
6774 return Py_BuildValue("i", WSTOPSIG(status));
6776 #endif /* WSTOPSIG */
6778 #endif /* HAVE_SYS_WAIT_H */
6781 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
6782 #ifdef _SCO_DS
6783 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6784 needed definitions in sys/statvfs.h */
6785 #define _SVID3
6786 #endif
6787 #include <sys/statvfs.h>
6789 static PyObject*
6790 _pystatvfs_fromstructstatvfs(struct statvfs st) {
6791 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6792 if (v == NULL)
6793 return NULL;
6795 #if !defined(HAVE_LARGEFILE_SUPPORT)
6796 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6797 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6798 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6799 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6800 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6801 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6802 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6803 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6804 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6805 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6806 #else
6807 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6808 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6809 PyStructSequence_SET_ITEM(v, 2,
6810 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6811 PyStructSequence_SET_ITEM(v, 3,
6812 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6813 PyStructSequence_SET_ITEM(v, 4,
6814 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6815 PyStructSequence_SET_ITEM(v, 5,
6816 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6817 PyStructSequence_SET_ITEM(v, 6,
6818 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6819 PyStructSequence_SET_ITEM(v, 7,
6820 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6821 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6822 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6823 #endif
6825 return v;
6828 PyDoc_STRVAR(posix_fstatvfs__doc__,
6829 "fstatvfs(fd) -> statvfs result\n\n\
6830 Perform an fstatvfs system call on the given fd.");
6832 static PyObject *
6833 posix_fstatvfs(PyObject *self, PyObject *args)
6835 int fd, res;
6836 struct statvfs st;
6838 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6839 return NULL;
6840 Py_BEGIN_ALLOW_THREADS
6841 res = fstatvfs(fd, &st);
6842 Py_END_ALLOW_THREADS
6843 if (res != 0)
6844 return posix_error();
6846 return _pystatvfs_fromstructstatvfs(st);
6848 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
6851 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
6852 #include <sys/statvfs.h>
6854 PyDoc_STRVAR(posix_statvfs__doc__,
6855 "statvfs(path) -> statvfs result\n\n\
6856 Perform a statvfs system call on the given path.");
6858 static PyObject *
6859 posix_statvfs(PyObject *self, PyObject *args)
6861 char *path;
6862 int res;
6863 struct statvfs st;
6864 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6865 return NULL;
6866 Py_BEGIN_ALLOW_THREADS
6867 res = statvfs(path, &st);
6868 Py_END_ALLOW_THREADS
6869 if (res != 0)
6870 return posix_error_with_filename(path);
6872 return _pystatvfs_fromstructstatvfs(st);
6874 #endif /* HAVE_STATVFS */
6877 #ifdef HAVE_TEMPNAM
6878 PyDoc_STRVAR(posix_tempnam__doc__,
6879 "tempnam([dir[, prefix]]) -> string\n\n\
6880 Return a unique name for a temporary file.\n\
6881 The directory and a prefix may be specified as strings; they may be omitted\n\
6882 or None if not needed.");
6884 static PyObject *
6885 posix_tempnam(PyObject *self, PyObject *args)
6887 PyObject *result = NULL;
6888 char *dir = NULL;
6889 char *pfx = NULL;
6890 char *name;
6892 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6893 return NULL;
6895 if (PyErr_Warn(PyExc_RuntimeWarning,
6896 "tempnam is a potential security risk to your program") < 0)
6897 return NULL;
6899 #ifdef MS_WINDOWS
6900 name = _tempnam(dir, pfx);
6901 #else
6902 name = tempnam(dir, pfx);
6903 #endif
6904 if (name == NULL)
6905 return PyErr_NoMemory();
6906 result = PyString_FromString(name);
6907 free(name);
6908 return result;
6910 #endif
6913 #ifdef HAVE_TMPFILE
6914 PyDoc_STRVAR(posix_tmpfile__doc__,
6915 "tmpfile() -> file object\n\n\
6916 Create a temporary file with no directory entries.");
6918 static PyObject *
6919 posix_tmpfile(PyObject *self, PyObject *noargs)
6921 FILE *fp;
6923 fp = tmpfile();
6924 if (fp == NULL)
6925 return posix_error();
6926 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
6928 #endif
6931 #ifdef HAVE_TMPNAM
6932 PyDoc_STRVAR(posix_tmpnam__doc__,
6933 "tmpnam() -> string\n\n\
6934 Return a unique name for a temporary file.");
6936 static PyObject *
6937 posix_tmpnam(PyObject *self, PyObject *noargs)
6939 char buffer[L_tmpnam];
6940 char *name;
6942 if (PyErr_Warn(PyExc_RuntimeWarning,
6943 "tmpnam is a potential security risk to your program") < 0)
6944 return NULL;
6946 #ifdef USE_TMPNAM_R
6947 name = tmpnam_r(buffer);
6948 #else
6949 name = tmpnam(buffer);
6950 #endif
6951 if (name == NULL) {
6952 PyObject *err = Py_BuildValue("is", 0,
6953 #ifdef USE_TMPNAM_R
6954 "unexpected NULL from tmpnam_r"
6955 #else
6956 "unexpected NULL from tmpnam"
6957 #endif
6959 PyErr_SetObject(PyExc_OSError, err);
6960 Py_XDECREF(err);
6961 return NULL;
6963 return PyString_FromString(buffer);
6965 #endif
6968 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6969 * It maps strings representing configuration variable names to
6970 * integer values, allowing those functions to be called with the
6971 * magic names instead of polluting the module's namespace with tons of
6972 * rarely-used constants. There are three separate tables that use
6973 * these definitions.
6975 * This code is always included, even if none of the interfaces that
6976 * need it are included. The #if hackery needed to avoid it would be
6977 * sufficiently pervasive that it's not worth the loss of readability.
6979 struct constdef {
6980 char *name;
6981 long value;
6984 static int
6985 conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6986 size_t tablesize)
6988 if (PyInt_Check(arg)) {
6989 *valuep = PyInt_AS_LONG(arg);
6990 return 1;
6992 if (PyString_Check(arg)) {
6993 /* look up the value in the table using a binary search */
6994 size_t lo = 0;
6995 size_t mid;
6996 size_t hi = tablesize;
6997 int cmp;
6998 char *confname = PyString_AS_STRING(arg);
6999 while (lo < hi) {
7000 mid = (lo + hi) / 2;
7001 cmp = strcmp(confname, table[mid].name);
7002 if (cmp < 0)
7003 hi = mid;
7004 else if (cmp > 0)
7005 lo = mid + 1;
7006 else {
7007 *valuep = table[mid].value;
7008 return 1;
7011 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7013 else
7014 PyErr_SetString(PyExc_TypeError,
7015 "configuration names must be strings or integers");
7016 return 0;
7020 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7021 static struct constdef posix_constants_pathconf[] = {
7022 #ifdef _PC_ABI_AIO_XFER_MAX
7023 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7024 #endif
7025 #ifdef _PC_ABI_ASYNC_IO
7026 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7027 #endif
7028 #ifdef _PC_ASYNC_IO
7029 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7030 #endif
7031 #ifdef _PC_CHOWN_RESTRICTED
7032 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7033 #endif
7034 #ifdef _PC_FILESIZEBITS
7035 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7036 #endif
7037 #ifdef _PC_LAST
7038 {"PC_LAST", _PC_LAST},
7039 #endif
7040 #ifdef _PC_LINK_MAX
7041 {"PC_LINK_MAX", _PC_LINK_MAX},
7042 #endif
7043 #ifdef _PC_MAX_CANON
7044 {"PC_MAX_CANON", _PC_MAX_CANON},
7045 #endif
7046 #ifdef _PC_MAX_INPUT
7047 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7048 #endif
7049 #ifdef _PC_NAME_MAX
7050 {"PC_NAME_MAX", _PC_NAME_MAX},
7051 #endif
7052 #ifdef _PC_NO_TRUNC
7053 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7054 #endif
7055 #ifdef _PC_PATH_MAX
7056 {"PC_PATH_MAX", _PC_PATH_MAX},
7057 #endif
7058 #ifdef _PC_PIPE_BUF
7059 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7060 #endif
7061 #ifdef _PC_PRIO_IO
7062 {"PC_PRIO_IO", _PC_PRIO_IO},
7063 #endif
7064 #ifdef _PC_SOCK_MAXBUF
7065 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7066 #endif
7067 #ifdef _PC_SYNC_IO
7068 {"PC_SYNC_IO", _PC_SYNC_IO},
7069 #endif
7070 #ifdef _PC_VDISABLE
7071 {"PC_VDISABLE", _PC_VDISABLE},
7072 #endif
7075 static int
7076 conv_path_confname(PyObject *arg, int *valuep)
7078 return conv_confname(arg, valuep, posix_constants_pathconf,
7079 sizeof(posix_constants_pathconf)
7080 / sizeof(struct constdef));
7082 #endif
7084 #ifdef HAVE_FPATHCONF
7085 PyDoc_STRVAR(posix_fpathconf__doc__,
7086 "fpathconf(fd, name) -> integer\n\n\
7087 Return the configuration limit name for the file descriptor fd.\n\
7088 If there is no limit, return -1.");
7090 static PyObject *
7091 posix_fpathconf(PyObject *self, PyObject *args)
7093 PyObject *result = NULL;
7094 int name, fd;
7096 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7097 conv_path_confname, &name)) {
7098 long limit;
7100 errno = 0;
7101 limit = fpathconf(fd, name);
7102 if (limit == -1 && errno != 0)
7103 posix_error();
7104 else
7105 result = PyInt_FromLong(limit);
7107 return result;
7109 #endif
7112 #ifdef HAVE_PATHCONF
7113 PyDoc_STRVAR(posix_pathconf__doc__,
7114 "pathconf(path, name) -> integer\n\n\
7115 Return the configuration limit name for the file or directory path.\n\
7116 If there is no limit, return -1.");
7118 static PyObject *
7119 posix_pathconf(PyObject *self, PyObject *args)
7121 PyObject *result = NULL;
7122 int name;
7123 char *path;
7125 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7126 conv_path_confname, &name)) {
7127 long limit;
7129 errno = 0;
7130 limit = pathconf(path, name);
7131 if (limit == -1 && errno != 0) {
7132 if (errno == EINVAL)
7133 /* could be a path or name problem */
7134 posix_error();
7135 else
7136 posix_error_with_filename(path);
7138 else
7139 result = PyInt_FromLong(limit);
7141 return result;
7143 #endif
7145 #ifdef HAVE_CONFSTR
7146 static struct constdef posix_constants_confstr[] = {
7147 #ifdef _CS_ARCHITECTURE
7148 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7149 #endif
7150 #ifdef _CS_HOSTNAME
7151 {"CS_HOSTNAME", _CS_HOSTNAME},
7152 #endif
7153 #ifdef _CS_HW_PROVIDER
7154 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7155 #endif
7156 #ifdef _CS_HW_SERIAL
7157 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7158 #endif
7159 #ifdef _CS_INITTAB_NAME
7160 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7161 #endif
7162 #ifdef _CS_LFS64_CFLAGS
7163 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7164 #endif
7165 #ifdef _CS_LFS64_LDFLAGS
7166 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7167 #endif
7168 #ifdef _CS_LFS64_LIBS
7169 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7170 #endif
7171 #ifdef _CS_LFS64_LINTFLAGS
7172 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7173 #endif
7174 #ifdef _CS_LFS_CFLAGS
7175 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7176 #endif
7177 #ifdef _CS_LFS_LDFLAGS
7178 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7179 #endif
7180 #ifdef _CS_LFS_LIBS
7181 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7182 #endif
7183 #ifdef _CS_LFS_LINTFLAGS
7184 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7185 #endif
7186 #ifdef _CS_MACHINE
7187 {"CS_MACHINE", _CS_MACHINE},
7188 #endif
7189 #ifdef _CS_PATH
7190 {"CS_PATH", _CS_PATH},
7191 #endif
7192 #ifdef _CS_RELEASE
7193 {"CS_RELEASE", _CS_RELEASE},
7194 #endif
7195 #ifdef _CS_SRPC_DOMAIN
7196 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7197 #endif
7198 #ifdef _CS_SYSNAME
7199 {"CS_SYSNAME", _CS_SYSNAME},
7200 #endif
7201 #ifdef _CS_VERSION
7202 {"CS_VERSION", _CS_VERSION},
7203 #endif
7204 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7205 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7206 #endif
7207 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7208 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7209 #endif
7210 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
7211 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7212 #endif
7213 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7214 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7215 #endif
7216 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7217 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7218 #endif
7219 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7220 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7221 #endif
7222 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7223 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7224 #endif
7225 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7226 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7227 #endif
7228 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7229 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7230 #endif
7231 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7232 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7233 #endif
7234 #ifdef _CS_XBS5_LP64_OFF64_LIBS
7235 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7236 #endif
7237 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7238 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7239 #endif
7240 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7241 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7242 #endif
7243 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7244 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7245 #endif
7246 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7247 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7248 #endif
7249 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7250 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7251 #endif
7252 #ifdef _MIPS_CS_AVAIL_PROCESSORS
7253 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7254 #endif
7255 #ifdef _MIPS_CS_BASE
7256 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7257 #endif
7258 #ifdef _MIPS_CS_HOSTID
7259 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7260 #endif
7261 #ifdef _MIPS_CS_HW_NAME
7262 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7263 #endif
7264 #ifdef _MIPS_CS_NUM_PROCESSORS
7265 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7266 #endif
7267 #ifdef _MIPS_CS_OSREL_MAJ
7268 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7269 #endif
7270 #ifdef _MIPS_CS_OSREL_MIN
7271 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7272 #endif
7273 #ifdef _MIPS_CS_OSREL_PATCH
7274 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7275 #endif
7276 #ifdef _MIPS_CS_OS_NAME
7277 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7278 #endif
7279 #ifdef _MIPS_CS_OS_PROVIDER
7280 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7281 #endif
7282 #ifdef _MIPS_CS_PROCESSORS
7283 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7284 #endif
7285 #ifdef _MIPS_CS_SERIAL
7286 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7287 #endif
7288 #ifdef _MIPS_CS_VENDOR
7289 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7290 #endif
7293 static int
7294 conv_confstr_confname(PyObject *arg, int *valuep)
7296 return conv_confname(arg, valuep, posix_constants_confstr,
7297 sizeof(posix_constants_confstr)
7298 / sizeof(struct constdef));
7301 PyDoc_STRVAR(posix_confstr__doc__,
7302 "confstr(name) -> string\n\n\
7303 Return a string-valued system configuration variable.");
7305 static PyObject *
7306 posix_confstr(PyObject *self, PyObject *args)
7308 PyObject *result = NULL;
7309 int name;
7310 char buffer[256];
7312 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
7313 int len;
7315 errno = 0;
7316 len = confstr(name, buffer, sizeof(buffer));
7317 if (len == 0) {
7318 if (errno) {
7319 posix_error();
7321 else {
7322 result = Py_None;
7323 Py_INCREF(Py_None);
7326 else {
7327 if ((unsigned int)len >= sizeof(buffer)) {
7328 result = PyString_FromStringAndSize(NULL, len-1);
7329 if (result != NULL)
7330 confstr(name, PyString_AS_STRING(result), len);
7332 else
7333 result = PyString_FromStringAndSize(buffer, len-1);
7336 return result;
7338 #endif
7341 #ifdef HAVE_SYSCONF
7342 static struct constdef posix_constants_sysconf[] = {
7343 #ifdef _SC_2_CHAR_TERM
7344 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7345 #endif
7346 #ifdef _SC_2_C_BIND
7347 {"SC_2_C_BIND", _SC_2_C_BIND},
7348 #endif
7349 #ifdef _SC_2_C_DEV
7350 {"SC_2_C_DEV", _SC_2_C_DEV},
7351 #endif
7352 #ifdef _SC_2_C_VERSION
7353 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7354 #endif
7355 #ifdef _SC_2_FORT_DEV
7356 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7357 #endif
7358 #ifdef _SC_2_FORT_RUN
7359 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7360 #endif
7361 #ifdef _SC_2_LOCALEDEF
7362 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7363 #endif
7364 #ifdef _SC_2_SW_DEV
7365 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7366 #endif
7367 #ifdef _SC_2_UPE
7368 {"SC_2_UPE", _SC_2_UPE},
7369 #endif
7370 #ifdef _SC_2_VERSION
7371 {"SC_2_VERSION", _SC_2_VERSION},
7372 #endif
7373 #ifdef _SC_ABI_ASYNCHRONOUS_IO
7374 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7375 #endif
7376 #ifdef _SC_ACL
7377 {"SC_ACL", _SC_ACL},
7378 #endif
7379 #ifdef _SC_AIO_LISTIO_MAX
7380 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7381 #endif
7382 #ifdef _SC_AIO_MAX
7383 {"SC_AIO_MAX", _SC_AIO_MAX},
7384 #endif
7385 #ifdef _SC_AIO_PRIO_DELTA_MAX
7386 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7387 #endif
7388 #ifdef _SC_ARG_MAX
7389 {"SC_ARG_MAX", _SC_ARG_MAX},
7390 #endif
7391 #ifdef _SC_ASYNCHRONOUS_IO
7392 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7393 #endif
7394 #ifdef _SC_ATEXIT_MAX
7395 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7396 #endif
7397 #ifdef _SC_AUDIT
7398 {"SC_AUDIT", _SC_AUDIT},
7399 #endif
7400 #ifdef _SC_AVPHYS_PAGES
7401 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7402 #endif
7403 #ifdef _SC_BC_BASE_MAX
7404 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7405 #endif
7406 #ifdef _SC_BC_DIM_MAX
7407 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7408 #endif
7409 #ifdef _SC_BC_SCALE_MAX
7410 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7411 #endif
7412 #ifdef _SC_BC_STRING_MAX
7413 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7414 #endif
7415 #ifdef _SC_CAP
7416 {"SC_CAP", _SC_CAP},
7417 #endif
7418 #ifdef _SC_CHARCLASS_NAME_MAX
7419 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7420 #endif
7421 #ifdef _SC_CHAR_BIT
7422 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7423 #endif
7424 #ifdef _SC_CHAR_MAX
7425 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7426 #endif
7427 #ifdef _SC_CHAR_MIN
7428 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7429 #endif
7430 #ifdef _SC_CHILD_MAX
7431 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7432 #endif
7433 #ifdef _SC_CLK_TCK
7434 {"SC_CLK_TCK", _SC_CLK_TCK},
7435 #endif
7436 #ifdef _SC_COHER_BLKSZ
7437 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7438 #endif
7439 #ifdef _SC_COLL_WEIGHTS_MAX
7440 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7441 #endif
7442 #ifdef _SC_DCACHE_ASSOC
7443 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7444 #endif
7445 #ifdef _SC_DCACHE_BLKSZ
7446 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7447 #endif
7448 #ifdef _SC_DCACHE_LINESZ
7449 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7450 #endif
7451 #ifdef _SC_DCACHE_SZ
7452 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7453 #endif
7454 #ifdef _SC_DCACHE_TBLKSZ
7455 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7456 #endif
7457 #ifdef _SC_DELAYTIMER_MAX
7458 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7459 #endif
7460 #ifdef _SC_EQUIV_CLASS_MAX
7461 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7462 #endif
7463 #ifdef _SC_EXPR_NEST_MAX
7464 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7465 #endif
7466 #ifdef _SC_FSYNC
7467 {"SC_FSYNC", _SC_FSYNC},
7468 #endif
7469 #ifdef _SC_GETGR_R_SIZE_MAX
7470 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7471 #endif
7472 #ifdef _SC_GETPW_R_SIZE_MAX
7473 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7474 #endif
7475 #ifdef _SC_ICACHE_ASSOC
7476 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7477 #endif
7478 #ifdef _SC_ICACHE_BLKSZ
7479 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7480 #endif
7481 #ifdef _SC_ICACHE_LINESZ
7482 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7483 #endif
7484 #ifdef _SC_ICACHE_SZ
7485 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7486 #endif
7487 #ifdef _SC_INF
7488 {"SC_INF", _SC_INF},
7489 #endif
7490 #ifdef _SC_INT_MAX
7491 {"SC_INT_MAX", _SC_INT_MAX},
7492 #endif
7493 #ifdef _SC_INT_MIN
7494 {"SC_INT_MIN", _SC_INT_MIN},
7495 #endif
7496 #ifdef _SC_IOV_MAX
7497 {"SC_IOV_MAX", _SC_IOV_MAX},
7498 #endif
7499 #ifdef _SC_IP_SECOPTS
7500 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7501 #endif
7502 #ifdef _SC_JOB_CONTROL
7503 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7504 #endif
7505 #ifdef _SC_KERN_POINTERS
7506 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7507 #endif
7508 #ifdef _SC_KERN_SIM
7509 {"SC_KERN_SIM", _SC_KERN_SIM},
7510 #endif
7511 #ifdef _SC_LINE_MAX
7512 {"SC_LINE_MAX", _SC_LINE_MAX},
7513 #endif
7514 #ifdef _SC_LOGIN_NAME_MAX
7515 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7516 #endif
7517 #ifdef _SC_LOGNAME_MAX
7518 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7519 #endif
7520 #ifdef _SC_LONG_BIT
7521 {"SC_LONG_BIT", _SC_LONG_BIT},
7522 #endif
7523 #ifdef _SC_MAC
7524 {"SC_MAC", _SC_MAC},
7525 #endif
7526 #ifdef _SC_MAPPED_FILES
7527 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7528 #endif
7529 #ifdef _SC_MAXPID
7530 {"SC_MAXPID", _SC_MAXPID},
7531 #endif
7532 #ifdef _SC_MB_LEN_MAX
7533 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7534 #endif
7535 #ifdef _SC_MEMLOCK
7536 {"SC_MEMLOCK", _SC_MEMLOCK},
7537 #endif
7538 #ifdef _SC_MEMLOCK_RANGE
7539 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7540 #endif
7541 #ifdef _SC_MEMORY_PROTECTION
7542 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7543 #endif
7544 #ifdef _SC_MESSAGE_PASSING
7545 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7546 #endif
7547 #ifdef _SC_MMAP_FIXED_ALIGNMENT
7548 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7549 #endif
7550 #ifdef _SC_MQ_OPEN_MAX
7551 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7552 #endif
7553 #ifdef _SC_MQ_PRIO_MAX
7554 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7555 #endif
7556 #ifdef _SC_NACLS_MAX
7557 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7558 #endif
7559 #ifdef _SC_NGROUPS_MAX
7560 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7561 #endif
7562 #ifdef _SC_NL_ARGMAX
7563 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7564 #endif
7565 #ifdef _SC_NL_LANGMAX
7566 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7567 #endif
7568 #ifdef _SC_NL_MSGMAX
7569 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7570 #endif
7571 #ifdef _SC_NL_NMAX
7572 {"SC_NL_NMAX", _SC_NL_NMAX},
7573 #endif
7574 #ifdef _SC_NL_SETMAX
7575 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7576 #endif
7577 #ifdef _SC_NL_TEXTMAX
7578 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7579 #endif
7580 #ifdef _SC_NPROCESSORS_CONF
7581 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7582 #endif
7583 #ifdef _SC_NPROCESSORS_ONLN
7584 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7585 #endif
7586 #ifdef _SC_NPROC_CONF
7587 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7588 #endif
7589 #ifdef _SC_NPROC_ONLN
7590 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7591 #endif
7592 #ifdef _SC_NZERO
7593 {"SC_NZERO", _SC_NZERO},
7594 #endif
7595 #ifdef _SC_OPEN_MAX
7596 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7597 #endif
7598 #ifdef _SC_PAGESIZE
7599 {"SC_PAGESIZE", _SC_PAGESIZE},
7600 #endif
7601 #ifdef _SC_PAGE_SIZE
7602 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7603 #endif
7604 #ifdef _SC_PASS_MAX
7605 {"SC_PASS_MAX", _SC_PASS_MAX},
7606 #endif
7607 #ifdef _SC_PHYS_PAGES
7608 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7609 #endif
7610 #ifdef _SC_PII
7611 {"SC_PII", _SC_PII},
7612 #endif
7613 #ifdef _SC_PII_INTERNET
7614 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7615 #endif
7616 #ifdef _SC_PII_INTERNET_DGRAM
7617 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7618 #endif
7619 #ifdef _SC_PII_INTERNET_STREAM
7620 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7621 #endif
7622 #ifdef _SC_PII_OSI
7623 {"SC_PII_OSI", _SC_PII_OSI},
7624 #endif
7625 #ifdef _SC_PII_OSI_CLTS
7626 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7627 #endif
7628 #ifdef _SC_PII_OSI_COTS
7629 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7630 #endif
7631 #ifdef _SC_PII_OSI_M
7632 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7633 #endif
7634 #ifdef _SC_PII_SOCKET
7635 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7636 #endif
7637 #ifdef _SC_PII_XTI
7638 {"SC_PII_XTI", _SC_PII_XTI},
7639 #endif
7640 #ifdef _SC_POLL
7641 {"SC_POLL", _SC_POLL},
7642 #endif
7643 #ifdef _SC_PRIORITIZED_IO
7644 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7645 #endif
7646 #ifdef _SC_PRIORITY_SCHEDULING
7647 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7648 #endif
7649 #ifdef _SC_REALTIME_SIGNALS
7650 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7651 #endif
7652 #ifdef _SC_RE_DUP_MAX
7653 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7654 #endif
7655 #ifdef _SC_RTSIG_MAX
7656 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7657 #endif
7658 #ifdef _SC_SAVED_IDS
7659 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7660 #endif
7661 #ifdef _SC_SCHAR_MAX
7662 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7663 #endif
7664 #ifdef _SC_SCHAR_MIN
7665 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7666 #endif
7667 #ifdef _SC_SELECT
7668 {"SC_SELECT", _SC_SELECT},
7669 #endif
7670 #ifdef _SC_SEMAPHORES
7671 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7672 #endif
7673 #ifdef _SC_SEM_NSEMS_MAX
7674 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7675 #endif
7676 #ifdef _SC_SEM_VALUE_MAX
7677 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7678 #endif
7679 #ifdef _SC_SHARED_MEMORY_OBJECTS
7680 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7681 #endif
7682 #ifdef _SC_SHRT_MAX
7683 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7684 #endif
7685 #ifdef _SC_SHRT_MIN
7686 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7687 #endif
7688 #ifdef _SC_SIGQUEUE_MAX
7689 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7690 #endif
7691 #ifdef _SC_SIGRT_MAX
7692 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7693 #endif
7694 #ifdef _SC_SIGRT_MIN
7695 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7696 #endif
7697 #ifdef _SC_SOFTPOWER
7698 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7699 #endif
7700 #ifdef _SC_SPLIT_CACHE
7701 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7702 #endif
7703 #ifdef _SC_SSIZE_MAX
7704 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7705 #endif
7706 #ifdef _SC_STACK_PROT
7707 {"SC_STACK_PROT", _SC_STACK_PROT},
7708 #endif
7709 #ifdef _SC_STREAM_MAX
7710 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7711 #endif
7712 #ifdef _SC_SYNCHRONIZED_IO
7713 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7714 #endif
7715 #ifdef _SC_THREADS
7716 {"SC_THREADS", _SC_THREADS},
7717 #endif
7718 #ifdef _SC_THREAD_ATTR_STACKADDR
7719 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7720 #endif
7721 #ifdef _SC_THREAD_ATTR_STACKSIZE
7722 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7723 #endif
7724 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7725 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7726 #endif
7727 #ifdef _SC_THREAD_KEYS_MAX
7728 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7729 #endif
7730 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
7731 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7732 #endif
7733 #ifdef _SC_THREAD_PRIO_INHERIT
7734 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7735 #endif
7736 #ifdef _SC_THREAD_PRIO_PROTECT
7737 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7738 #endif
7739 #ifdef _SC_THREAD_PROCESS_SHARED
7740 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7741 #endif
7742 #ifdef _SC_THREAD_SAFE_FUNCTIONS
7743 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7744 #endif
7745 #ifdef _SC_THREAD_STACK_MIN
7746 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7747 #endif
7748 #ifdef _SC_THREAD_THREADS_MAX
7749 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7750 #endif
7751 #ifdef _SC_TIMERS
7752 {"SC_TIMERS", _SC_TIMERS},
7753 #endif
7754 #ifdef _SC_TIMER_MAX
7755 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7756 #endif
7757 #ifdef _SC_TTY_NAME_MAX
7758 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7759 #endif
7760 #ifdef _SC_TZNAME_MAX
7761 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7762 #endif
7763 #ifdef _SC_T_IOV_MAX
7764 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7765 #endif
7766 #ifdef _SC_UCHAR_MAX
7767 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7768 #endif
7769 #ifdef _SC_UINT_MAX
7770 {"SC_UINT_MAX", _SC_UINT_MAX},
7771 #endif
7772 #ifdef _SC_UIO_MAXIOV
7773 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7774 #endif
7775 #ifdef _SC_ULONG_MAX
7776 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7777 #endif
7778 #ifdef _SC_USHRT_MAX
7779 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7780 #endif
7781 #ifdef _SC_VERSION
7782 {"SC_VERSION", _SC_VERSION},
7783 #endif
7784 #ifdef _SC_WORD_BIT
7785 {"SC_WORD_BIT", _SC_WORD_BIT},
7786 #endif
7787 #ifdef _SC_XBS5_ILP32_OFF32
7788 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7789 #endif
7790 #ifdef _SC_XBS5_ILP32_OFFBIG
7791 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7792 #endif
7793 #ifdef _SC_XBS5_LP64_OFF64
7794 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7795 #endif
7796 #ifdef _SC_XBS5_LPBIG_OFFBIG
7797 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7798 #endif
7799 #ifdef _SC_XOPEN_CRYPT
7800 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7801 #endif
7802 #ifdef _SC_XOPEN_ENH_I18N
7803 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7804 #endif
7805 #ifdef _SC_XOPEN_LEGACY
7806 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7807 #endif
7808 #ifdef _SC_XOPEN_REALTIME
7809 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7810 #endif
7811 #ifdef _SC_XOPEN_REALTIME_THREADS
7812 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7813 #endif
7814 #ifdef _SC_XOPEN_SHM
7815 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7816 #endif
7817 #ifdef _SC_XOPEN_UNIX
7818 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7819 #endif
7820 #ifdef _SC_XOPEN_VERSION
7821 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7822 #endif
7823 #ifdef _SC_XOPEN_XCU_VERSION
7824 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7825 #endif
7826 #ifdef _SC_XOPEN_XPG2
7827 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7828 #endif
7829 #ifdef _SC_XOPEN_XPG3
7830 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7831 #endif
7832 #ifdef _SC_XOPEN_XPG4
7833 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7834 #endif
7837 static int
7838 conv_sysconf_confname(PyObject *arg, int *valuep)
7840 return conv_confname(arg, valuep, posix_constants_sysconf,
7841 sizeof(posix_constants_sysconf)
7842 / sizeof(struct constdef));
7845 PyDoc_STRVAR(posix_sysconf__doc__,
7846 "sysconf(name) -> integer\n\n\
7847 Return an integer-valued system configuration variable.");
7849 static PyObject *
7850 posix_sysconf(PyObject *self, PyObject *args)
7852 PyObject *result = NULL;
7853 int name;
7855 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7856 int value;
7858 errno = 0;
7859 value = sysconf(name);
7860 if (value == -1 && errno != 0)
7861 posix_error();
7862 else
7863 result = PyInt_FromLong(value);
7865 return result;
7867 #endif
7870 /* This code is used to ensure that the tables of configuration value names
7871 * are in sorted order as required by conv_confname(), and also to build the
7872 * the exported dictionaries that are used to publish information about the
7873 * names available on the host platform.
7875 * Sorting the table at runtime ensures that the table is properly ordered
7876 * when used, even for platforms we're not able to test on. It also makes
7877 * it easier to add additional entries to the tables.
7880 static int
7881 cmp_constdefs(const void *v1, const void *v2)
7883 const struct constdef *c1 =
7884 (const struct constdef *) v1;
7885 const struct constdef *c2 =
7886 (const struct constdef *) v2;
7888 return strcmp(c1->name, c2->name);
7891 static int
7892 setup_confname_table(struct constdef *table, size_t tablesize,
7893 char *tablename, PyObject *module)
7895 PyObject *d = NULL;
7896 size_t i;
7898 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7899 d = PyDict_New();
7900 if (d == NULL)
7901 return -1;
7903 for (i=0; i < tablesize; ++i) {
7904 PyObject *o = PyInt_FromLong(table[i].value);
7905 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7906 Py_XDECREF(o);
7907 Py_DECREF(d);
7908 return -1;
7910 Py_DECREF(o);
7912 return PyModule_AddObject(module, tablename, d);
7915 /* Return -1 on failure, 0 on success. */
7916 static int
7917 setup_confname_tables(PyObject *module)
7919 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7920 if (setup_confname_table(posix_constants_pathconf,
7921 sizeof(posix_constants_pathconf)
7922 / sizeof(struct constdef),
7923 "pathconf_names", module))
7924 return -1;
7925 #endif
7926 #ifdef HAVE_CONFSTR
7927 if (setup_confname_table(posix_constants_confstr,
7928 sizeof(posix_constants_confstr)
7929 / sizeof(struct constdef),
7930 "confstr_names", module))
7931 return -1;
7932 #endif
7933 #ifdef HAVE_SYSCONF
7934 if (setup_confname_table(posix_constants_sysconf,
7935 sizeof(posix_constants_sysconf)
7936 / sizeof(struct constdef),
7937 "sysconf_names", module))
7938 return -1;
7939 #endif
7940 return 0;
7944 PyDoc_STRVAR(posix_abort__doc__,
7945 "abort() -> does not return!\n\n\
7946 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
7947 in the hardest way possible on the hosting operating system.");
7949 static PyObject *
7950 posix_abort(PyObject *self, PyObject *noargs)
7952 abort();
7953 /*NOTREACHED*/
7954 Py_FatalError("abort() called from Python code didn't abort!");
7955 return NULL;
7958 #ifdef MS_WINDOWS
7959 PyDoc_STRVAR(win32_startfile__doc__,
7960 "startfile(filepath [, operation]) - Start a file with its associated\n\
7961 application.\n\
7963 When \"operation\" is not specified or \"open\", this acts like\n\
7964 double-clicking the file in Explorer, or giving the file name as an\n\
7965 argument to the DOS \"start\" command: the file is opened with whatever\n\
7966 application (if any) its extension is associated.\n\
7967 When another \"operation\" is given, it specifies what should be done with\n\
7968 the file. A typical operation is \"print\".\n\
7970 startfile returns as soon as the associated application is launched.\n\
7971 There is no option to wait for the application to close, and no way\n\
7972 to retrieve the application's exit status.\n\
7974 The filepath is relative to the current directory. If you want to use\n\
7975 an absolute path, make sure the first character is not a slash (\"/\");\n\
7976 the underlying Win32 ShellExecute function doesn't work if it is.");
7978 static PyObject *
7979 win32_startfile(PyObject *self, PyObject *args)
7981 char *filepath;
7982 char *operation = NULL;
7983 HINSTANCE rc;
7984 #ifdef Py_WIN_WIDE_FILENAMES
7985 if (unicode_file_names()) {
7986 PyObject *unipath, *woperation = NULL;
7987 if (!PyArg_ParseTuple(args, "U|s:startfile",
7988 &unipath, &operation)) {
7989 PyErr_Clear();
7990 goto normal;
7994 if (operation) {
7995 woperation = PyUnicode_DecodeASCII(operation,
7996 strlen(operation), NULL);
7997 if (!woperation) {
7998 PyErr_Clear();
7999 operation = NULL;
8000 goto normal;
8004 Py_BEGIN_ALLOW_THREADS
8005 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8006 PyUnicode_AS_UNICODE(unipath),
8007 NULL, NULL, SW_SHOWNORMAL);
8008 Py_END_ALLOW_THREADS
8010 Py_XDECREF(woperation);
8011 if (rc <= (HINSTANCE)32) {
8012 PyObject *errval = win32_error_unicode("startfile",
8013 PyUnicode_AS_UNICODE(unipath));
8014 return errval;
8016 Py_INCREF(Py_None);
8017 return Py_None;
8019 #endif
8021 normal:
8022 if (!PyArg_ParseTuple(args, "et|s:startfile",
8023 Py_FileSystemDefaultEncoding, &filepath,
8024 &operation))
8025 return NULL;
8026 Py_BEGIN_ALLOW_THREADS
8027 rc = ShellExecute((HWND)0, operation, filepath,
8028 NULL, NULL, SW_SHOWNORMAL);
8029 Py_END_ALLOW_THREADS
8030 if (rc <= (HINSTANCE)32) {
8031 PyObject *errval = win32_error("startfile", filepath);
8032 PyMem_Free(filepath);
8033 return errval;
8035 PyMem_Free(filepath);
8036 Py_INCREF(Py_None);
8037 return Py_None;
8039 #endif
8041 #ifdef HAVE_GETLOADAVG
8042 PyDoc_STRVAR(posix_getloadavg__doc__,
8043 "getloadavg() -> (float, float, float)\n\n\
8044 Return the number of processes in the system run queue averaged over\n\
8045 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8046 was unobtainable");
8048 static PyObject *
8049 posix_getloadavg(PyObject *self, PyObject *noargs)
8051 double loadavg[3];
8052 if (getloadavg(loadavg, 3)!=3) {
8053 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8054 return NULL;
8055 } else
8056 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8058 #endif
8060 #ifdef MS_WINDOWS
8062 PyDoc_STRVAR(win32_urandom__doc__,
8063 "urandom(n) -> str\n\n\
8064 Return a string of n random bytes suitable for cryptographic use.");
8066 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8067 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8068 DWORD dwFlags );
8069 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8070 BYTE *pbBuffer );
8072 static CRYPTGENRANDOM pCryptGenRandom = NULL;
8073 static HCRYPTPROV hCryptProv = 0;
8075 static PyObject*
8076 win32_urandom(PyObject *self, PyObject *args)
8078 int howMany;
8079 PyObject* result;
8081 /* Read arguments */
8082 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8083 return NULL;
8084 if (howMany < 0)
8085 return PyErr_Format(PyExc_ValueError,
8086 "negative argument not allowed");
8088 if (hCryptProv == 0) {
8089 HINSTANCE hAdvAPI32 = NULL;
8090 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
8092 /* Obtain handle to the DLL containing CryptoAPI
8093 This should not fail */
8094 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8095 if(hAdvAPI32 == NULL)
8096 return win32_error("GetModuleHandle", NULL);
8098 /* Obtain pointers to the CryptoAPI functions
8099 This will fail on some early versions of Win95 */
8100 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8101 hAdvAPI32,
8102 "CryptAcquireContextA");
8103 if (pCryptAcquireContext == NULL)
8104 return PyErr_Format(PyExc_NotImplementedError,
8105 "CryptAcquireContextA not found");
8107 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8108 hAdvAPI32, "CryptGenRandom");
8109 if (pCryptGenRandom == NULL)
8110 return PyErr_Format(PyExc_NotImplementedError,
8111 "CryptGenRandom not found");
8113 /* Acquire context */
8114 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8115 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8116 return win32_error("CryptAcquireContext", NULL);
8119 /* Allocate bytes */
8120 result = PyString_FromStringAndSize(NULL, howMany);
8121 if (result != NULL) {
8122 /* Get random data */
8123 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8124 PyString_AS_STRING(result))) {
8125 Py_DECREF(result);
8126 return win32_error("CryptGenRandom", NULL);
8129 return result;
8131 #endif
8133 #ifdef __VMS
8134 /* Use openssl random routine */
8135 #include <openssl/rand.h>
8136 PyDoc_STRVAR(vms_urandom__doc__,
8137 "urandom(n) -> str\n\n\
8138 Return a string of n random bytes suitable for cryptographic use.");
8140 static PyObject*
8141 vms_urandom(PyObject *self, PyObject *args)
8143 int howMany;
8144 PyObject* result;
8146 /* Read arguments */
8147 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8148 return NULL;
8149 if (howMany < 0)
8150 return PyErr_Format(PyExc_ValueError,
8151 "negative argument not allowed");
8153 /* Allocate bytes */
8154 result = PyString_FromStringAndSize(NULL, howMany);
8155 if (result != NULL) {
8156 /* Get random data */
8157 if (RAND_pseudo_bytes((unsigned char*)
8158 PyString_AS_STRING(result),
8159 howMany) < 0) {
8160 Py_DECREF(result);
8161 return PyErr_Format(PyExc_ValueError,
8162 "RAND_pseudo_bytes");
8165 return result;
8167 #endif
8169 static PyMethodDef posix_methods[] = {
8170 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8171 #ifdef HAVE_TTYNAME
8172 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8173 #endif
8174 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8175 #ifdef HAVE_CHFLAGS
8176 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8177 #endif /* HAVE_CHFLAGS */
8178 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
8179 #ifdef HAVE_CHOWN
8180 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
8181 #endif /* HAVE_CHOWN */
8182 #ifdef HAVE_LCHFLAGS
8183 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8184 #endif /* HAVE_LCHFLAGS */
8185 #ifdef HAVE_LCHOWN
8186 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8187 #endif /* HAVE_LCHOWN */
8188 #ifdef HAVE_CHROOT
8189 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8190 #endif
8191 #ifdef HAVE_CTERMID
8192 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
8193 #endif
8194 #ifdef HAVE_GETCWD
8195 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
8196 #ifdef Py_USING_UNICODE
8197 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
8198 #endif
8199 #endif
8200 #ifdef HAVE_LINK
8201 {"link", posix_link, METH_VARARGS, posix_link__doc__},
8202 #endif /* HAVE_LINK */
8203 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8204 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8205 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
8206 #ifdef HAVE_NICE
8207 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
8208 #endif /* HAVE_NICE */
8209 #ifdef HAVE_READLINK
8210 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
8211 #endif /* HAVE_READLINK */
8212 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8213 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8214 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
8215 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
8216 #ifdef HAVE_SYMLINK
8217 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
8218 #endif /* HAVE_SYMLINK */
8219 #ifdef HAVE_SYSTEM
8220 {"system", posix_system, METH_VARARGS, posix_system__doc__},
8221 #endif
8222 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
8223 #ifdef HAVE_UNAME
8224 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
8225 #endif /* HAVE_UNAME */
8226 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8227 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8228 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
8229 #ifdef HAVE_TIMES
8230 {"times", posix_times, METH_NOARGS, posix_times__doc__},
8231 #endif /* HAVE_TIMES */
8232 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
8233 #ifdef HAVE_EXECV
8234 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8235 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
8236 #endif /* HAVE_EXECV */
8237 #ifdef HAVE_SPAWNV
8238 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8239 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
8240 #if defined(PYOS_OS2)
8241 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8242 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8243 #endif /* PYOS_OS2 */
8244 #endif /* HAVE_SPAWNV */
8245 #ifdef HAVE_FORK1
8246 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
8247 #endif /* HAVE_FORK1 */
8248 #ifdef HAVE_FORK
8249 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
8250 #endif /* HAVE_FORK */
8251 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8252 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
8253 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
8254 #ifdef HAVE_FORKPTY
8255 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
8256 #endif /* HAVE_FORKPTY */
8257 #ifdef HAVE_GETEGID
8258 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
8259 #endif /* HAVE_GETEGID */
8260 #ifdef HAVE_GETEUID
8261 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
8262 #endif /* HAVE_GETEUID */
8263 #ifdef HAVE_GETGID
8264 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
8265 #endif /* HAVE_GETGID */
8266 #ifdef HAVE_GETGROUPS
8267 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
8268 #endif
8269 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
8270 #ifdef HAVE_GETPGRP
8271 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
8272 #endif /* HAVE_GETPGRP */
8273 #ifdef HAVE_GETPPID
8274 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
8275 #endif /* HAVE_GETPPID */
8276 #ifdef HAVE_GETUID
8277 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
8278 #endif /* HAVE_GETUID */
8279 #ifdef HAVE_GETLOGIN
8280 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
8281 #endif
8282 #ifdef HAVE_KILL
8283 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
8284 #endif /* HAVE_KILL */
8285 #ifdef HAVE_KILLPG
8286 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8287 #endif /* HAVE_KILLPG */
8288 #ifdef HAVE_PLOCK
8289 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
8290 #endif /* HAVE_PLOCK */
8291 #ifdef HAVE_POPEN
8292 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
8293 #ifdef MS_WINDOWS
8294 {"popen2", win32_popen2, METH_VARARGS},
8295 {"popen3", win32_popen3, METH_VARARGS},
8296 {"popen4", win32_popen4, METH_VARARGS},
8297 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
8298 #else
8299 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8300 {"popen2", os2emx_popen2, METH_VARARGS},
8301 {"popen3", os2emx_popen3, METH_VARARGS},
8302 {"popen4", os2emx_popen4, METH_VARARGS},
8303 #endif
8304 #endif
8305 #endif /* HAVE_POPEN */
8306 #ifdef HAVE_SETUID
8307 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
8308 #endif /* HAVE_SETUID */
8309 #ifdef HAVE_SETEUID
8310 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8311 #endif /* HAVE_SETEUID */
8312 #ifdef HAVE_SETEGID
8313 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8314 #endif /* HAVE_SETEGID */
8315 #ifdef HAVE_SETREUID
8316 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8317 #endif /* HAVE_SETREUID */
8318 #ifdef HAVE_SETREGID
8319 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8320 #endif /* HAVE_SETREGID */
8321 #ifdef HAVE_SETGID
8322 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
8323 #endif /* HAVE_SETGID */
8324 #ifdef HAVE_SETGROUPS
8325 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
8326 #endif /* HAVE_SETGROUPS */
8327 #ifdef HAVE_GETPGID
8328 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8329 #endif /* HAVE_GETPGID */
8330 #ifdef HAVE_SETPGRP
8331 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
8332 #endif /* HAVE_SETPGRP */
8333 #ifdef HAVE_WAIT
8334 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
8335 #endif /* HAVE_WAIT */
8336 #ifdef HAVE_WAIT3
8337 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8338 #endif /* HAVE_WAIT3 */
8339 #ifdef HAVE_WAIT4
8340 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8341 #endif /* HAVE_WAIT4 */
8342 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
8343 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
8344 #endif /* HAVE_WAITPID */
8345 #ifdef HAVE_GETSID
8346 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8347 #endif /* HAVE_GETSID */
8348 #ifdef HAVE_SETSID
8349 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
8350 #endif /* HAVE_SETSID */
8351 #ifdef HAVE_SETPGID
8352 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
8353 #endif /* HAVE_SETPGID */
8354 #ifdef HAVE_TCGETPGRP
8355 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
8356 #endif /* HAVE_TCGETPGRP */
8357 #ifdef HAVE_TCSETPGRP
8358 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
8359 #endif /* HAVE_TCSETPGRP */
8360 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8361 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8362 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8363 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8364 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8365 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8366 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8367 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8368 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
8369 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
8370 #ifdef HAVE_PIPE
8371 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
8372 #endif
8373 #ifdef HAVE_MKFIFO
8374 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
8375 #endif
8376 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8377 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8378 #endif
8379 #ifdef HAVE_DEVICE_MACROS
8380 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8381 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8382 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8383 #endif
8384 #ifdef HAVE_FTRUNCATE
8385 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
8386 #endif
8387 #ifdef HAVE_PUTENV
8388 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
8389 #endif
8390 #ifdef HAVE_UNSETENV
8391 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8392 #endif
8393 #ifdef HAVE_STRERROR
8394 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
8395 #endif
8396 #ifdef HAVE_FCHDIR
8397 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8398 #endif
8399 #ifdef HAVE_FSYNC
8400 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
8401 #endif
8402 #ifdef HAVE_FDATASYNC
8403 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
8404 #endif
8405 #ifdef HAVE_SYS_WAIT_H
8406 #ifdef WCOREDUMP
8407 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8408 #endif /* WCOREDUMP */
8409 #ifdef WIFCONTINUED
8410 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8411 #endif /* WIFCONTINUED */
8412 #ifdef WIFSTOPPED
8413 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
8414 #endif /* WIFSTOPPED */
8415 #ifdef WIFSIGNALED
8416 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
8417 #endif /* WIFSIGNALED */
8418 #ifdef WIFEXITED
8419 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
8420 #endif /* WIFEXITED */
8421 #ifdef WEXITSTATUS
8422 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
8423 #endif /* WEXITSTATUS */
8424 #ifdef WTERMSIG
8425 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
8426 #endif /* WTERMSIG */
8427 #ifdef WSTOPSIG
8428 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
8429 #endif /* WSTOPSIG */
8430 #endif /* HAVE_SYS_WAIT_H */
8431 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
8432 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
8433 #endif
8434 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
8435 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
8436 #endif
8437 #ifdef HAVE_TMPFILE
8438 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
8439 #endif
8440 #ifdef HAVE_TEMPNAM
8441 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8442 #endif
8443 #ifdef HAVE_TMPNAM
8444 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
8445 #endif
8446 #ifdef HAVE_CONFSTR
8447 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8448 #endif
8449 #ifdef HAVE_SYSCONF
8450 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8451 #endif
8452 #ifdef HAVE_FPATHCONF
8453 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8454 #endif
8455 #ifdef HAVE_PATHCONF
8456 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8457 #endif
8458 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
8459 #ifdef MS_WINDOWS
8460 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8461 #endif
8462 #ifdef HAVE_GETLOADAVG
8463 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
8464 #endif
8465 #ifdef MS_WINDOWS
8466 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8467 #endif
8468 #ifdef __VMS
8469 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8470 #endif
8471 {NULL, NULL} /* Sentinel */
8475 static int
8476 ins(PyObject *module, char *symbol, long value)
8478 return PyModule_AddIntConstant(module, symbol, value);
8481 #if defined(PYOS_OS2)
8482 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8483 static int insertvalues(PyObject *module)
8485 APIRET rc;
8486 ULONG values[QSV_MAX+1];
8487 PyObject *v;
8488 char *ver, tmp[50];
8490 Py_BEGIN_ALLOW_THREADS
8491 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
8492 Py_END_ALLOW_THREADS
8494 if (rc != NO_ERROR) {
8495 os2_error(rc);
8496 return -1;
8499 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8500 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8501 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8502 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8503 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8504 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8505 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
8507 switch (values[QSV_VERSION_MINOR]) {
8508 case 0: ver = "2.00"; break;
8509 case 10: ver = "2.10"; break;
8510 case 11: ver = "2.11"; break;
8511 case 30: ver = "3.00"; break;
8512 case 40: ver = "4.00"; break;
8513 case 50: ver = "5.00"; break;
8514 default:
8515 PyOS_snprintf(tmp, sizeof(tmp),
8516 "%d-%d", values[QSV_VERSION_MAJOR],
8517 values[QSV_VERSION_MINOR]);
8518 ver = &tmp[0];
8521 /* Add Indicator of the Version of the Operating System */
8522 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
8523 return -1;
8525 /* Add Indicator of Which Drive was Used to Boot the System */
8526 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8527 tmp[1] = ':';
8528 tmp[2] = '\0';
8530 return PyModule_AddStringConstant(module, "bootdrive", tmp);
8532 #endif
8534 static int
8535 all_ins(PyObject *d)
8537 #ifdef F_OK
8538 if (ins(d, "F_OK", (long)F_OK)) return -1;
8539 #endif
8540 #ifdef R_OK
8541 if (ins(d, "R_OK", (long)R_OK)) return -1;
8542 #endif
8543 #ifdef W_OK
8544 if (ins(d, "W_OK", (long)W_OK)) return -1;
8545 #endif
8546 #ifdef X_OK
8547 if (ins(d, "X_OK", (long)X_OK)) return -1;
8548 #endif
8549 #ifdef NGROUPS_MAX
8550 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8551 #endif
8552 #ifdef TMP_MAX
8553 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8554 #endif
8555 #ifdef WCONTINUED
8556 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8557 #endif
8558 #ifdef WNOHANG
8559 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
8560 #endif
8561 #ifdef WUNTRACED
8562 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8563 #endif
8564 #ifdef O_RDONLY
8565 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8566 #endif
8567 #ifdef O_WRONLY
8568 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8569 #endif
8570 #ifdef O_RDWR
8571 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8572 #endif
8573 #ifdef O_NDELAY
8574 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8575 #endif
8576 #ifdef O_NONBLOCK
8577 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8578 #endif
8579 #ifdef O_APPEND
8580 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8581 #endif
8582 #ifdef O_DSYNC
8583 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8584 #endif
8585 #ifdef O_RSYNC
8586 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8587 #endif
8588 #ifdef O_SYNC
8589 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8590 #endif
8591 #ifdef O_NOCTTY
8592 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8593 #endif
8594 #ifdef O_CREAT
8595 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8596 #endif
8597 #ifdef O_EXCL
8598 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8599 #endif
8600 #ifdef O_TRUNC
8601 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8602 #endif
8603 #ifdef O_BINARY
8604 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8605 #endif
8606 #ifdef O_TEXT
8607 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8608 #endif
8609 #ifdef O_LARGEFILE
8610 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8611 #endif
8612 #ifdef O_SHLOCK
8613 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8614 #endif
8615 #ifdef O_EXLOCK
8616 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8617 #endif
8619 /* MS Windows */
8620 #ifdef O_NOINHERIT
8621 /* Don't inherit in child processes. */
8622 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8623 #endif
8624 #ifdef _O_SHORT_LIVED
8625 /* Optimize for short life (keep in memory). */
8626 /* MS forgot to define this one with a non-underscore form too. */
8627 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8628 #endif
8629 #ifdef O_TEMPORARY
8630 /* Automatically delete when last handle is closed. */
8631 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8632 #endif
8633 #ifdef O_RANDOM
8634 /* Optimize for random access. */
8635 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8636 #endif
8637 #ifdef O_SEQUENTIAL
8638 /* Optimize for sequential access. */
8639 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8640 #endif
8642 /* GNU extensions. */
8643 #ifdef O_DIRECT
8644 /* Direct disk access. */
8645 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8646 #endif
8647 #ifdef O_DIRECTORY
8648 /* Must be a directory. */
8649 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8650 #endif
8651 #ifdef O_NOFOLLOW
8652 /* Do not follow links. */
8653 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8654 #endif
8656 /* These come from sysexits.h */
8657 #ifdef EX_OK
8658 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
8659 #endif /* EX_OK */
8660 #ifdef EX_USAGE
8661 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
8662 #endif /* EX_USAGE */
8663 #ifdef EX_DATAERR
8664 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
8665 #endif /* EX_DATAERR */
8666 #ifdef EX_NOINPUT
8667 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
8668 #endif /* EX_NOINPUT */
8669 #ifdef EX_NOUSER
8670 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
8671 #endif /* EX_NOUSER */
8672 #ifdef EX_NOHOST
8673 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
8674 #endif /* EX_NOHOST */
8675 #ifdef EX_UNAVAILABLE
8676 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
8677 #endif /* EX_UNAVAILABLE */
8678 #ifdef EX_SOFTWARE
8679 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
8680 #endif /* EX_SOFTWARE */
8681 #ifdef EX_OSERR
8682 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
8683 #endif /* EX_OSERR */
8684 #ifdef EX_OSFILE
8685 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
8686 #endif /* EX_OSFILE */
8687 #ifdef EX_CANTCREAT
8688 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
8689 #endif /* EX_CANTCREAT */
8690 #ifdef EX_IOERR
8691 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
8692 #endif /* EX_IOERR */
8693 #ifdef EX_TEMPFAIL
8694 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
8695 #endif /* EX_TEMPFAIL */
8696 #ifdef EX_PROTOCOL
8697 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
8698 #endif /* EX_PROTOCOL */
8699 #ifdef EX_NOPERM
8700 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
8701 #endif /* EX_NOPERM */
8702 #ifdef EX_CONFIG
8703 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
8704 #endif /* EX_CONFIG */
8705 #ifdef EX_NOTFOUND
8706 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
8707 #endif /* EX_NOTFOUND */
8709 #ifdef HAVE_SPAWNV
8710 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8711 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8712 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8713 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8714 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8715 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8716 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8717 if (ins(d, "P_PM", (long)P_PM)) return -1;
8718 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8719 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8720 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8721 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8722 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8723 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8724 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8725 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8726 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8727 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8728 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8729 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8730 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8731 #else
8732 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8733 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8734 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8735 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8736 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
8737 #endif
8738 #endif
8740 #if defined(PYOS_OS2)
8741 if (insertvalues(d)) return -1;
8742 #endif
8743 return 0;
8747 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
8748 #define INITFUNC initnt
8749 #define MODNAME "nt"
8751 #elif defined(PYOS_OS2)
8752 #define INITFUNC initos2
8753 #define MODNAME "os2"
8755 #else
8756 #define INITFUNC initposix
8757 #define MODNAME "posix"
8758 #endif
8760 PyMODINIT_FUNC
8761 INITFUNC(void)
8763 PyObject *m, *v;
8765 m = Py_InitModule3(MODNAME,
8766 posix_methods,
8767 posix__doc__);
8768 if (m == NULL)
8769 return;
8771 /* Initialize environ dictionary */
8772 v = convertenviron();
8773 Py_XINCREF(v);
8774 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8775 return;
8776 Py_DECREF(v);
8778 if (all_ins(m))
8779 return;
8781 if (setup_confname_tables(m))
8782 return;
8784 Py_INCREF(PyExc_OSError);
8785 PyModule_AddObject(m, "error", PyExc_OSError);
8787 #ifdef HAVE_PUTENV
8788 if (posix_putenv_garbage == NULL)
8789 posix_putenv_garbage = PyDict_New();
8790 #endif
8792 if (!initialized) {
8793 stat_result_desc.name = MODNAME ".stat_result";
8794 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8795 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8796 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8797 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8798 structseq_new = StatResultType.tp_new;
8799 StatResultType.tp_new = statresult_new;
8801 statvfs_result_desc.name = MODNAME ".statvfs_result";
8802 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8804 Py_INCREF((PyObject*) &StatResultType);
8805 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8806 Py_INCREF((PyObject*) &StatVFSResultType);
8807 PyModule_AddObject(m, "statvfs_result",
8808 (PyObject*) &StatVFSResultType);
8809 initialized = 1;
8811 #ifdef __APPLE__
8813 * Step 2 of weak-linking support on Mac OS X.
8815 * The code below removes functions that are not available on the
8816 * currently active platform.
8818 * This block allow one to use a python binary that was build on
8819 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8820 * OSX 10.4.
8822 #ifdef HAVE_FSTATVFS
8823 if (fstatvfs == NULL) {
8824 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8825 return;
8828 #endif /* HAVE_FSTATVFS */
8830 #ifdef HAVE_STATVFS
8831 if (statvfs == NULL) {
8832 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8833 return;
8836 #endif /* HAVE_STATVFS */
8838 # ifdef HAVE_LCHOWN
8839 if (lchown == NULL) {
8840 if (PyObject_DelAttrString(m, "lchown") == -1) {
8841 return;
8844 #endif /* HAVE_LCHOWN */
8847 #endif /* __APPLE__ */
8851 #ifdef __cplusplus
8853 #endif