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 */
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.
24
# pragma weak fstatvfs
26
#endif /* __APPLE__ */
28
#define PY_SSIZE_T_CLEAN
31
#include "structseq.h"
35
#endif /* defined(__VMS) */
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.");
50
#define INCL_DOSERRORS
51
#define INCL_DOSPROCESS
63
#ifdef HAVE_SYS_TYPES_H
64
#include <sys/types.h>
65
#endif /* HAVE_SYS_TYPES_H */
67
#ifdef HAVE_SYS_STAT_H
69
#endif /* HAVE_SYS_STAT_H */
71
#ifdef HAVE_SYS_WAIT_H
72
#include <sys/wait.h> /* For WNOHANG */
81
#endif /* HAVE_FCNTL_H */
87
#ifdef HAVE_SYSEXITS_H
89
#endif /* HAVE_SYSEXITS_H */
91
#ifdef HAVE_SYS_LOADAVG_H
92
#include <sys/loadavg.h>
95
#ifdef HAVE_LANGINFO_H
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)
104
#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
105
#define HAVE_GETCWD 1
106
#define HAVE_OPENDIR 1
107
#define HAVE_SYSTEM 1
114
#ifdef __BORLANDC__ /* Borland compiler */
116
#define HAVE_GETCWD 1
117
#define HAVE_OPENDIR 1
119
#define HAVE_SYSTEM 1
122
#ifdef _MSC_VER /* Microsoft compiler */
123
#define HAVE_GETCWD 1
124
#define HAVE_SPAWNV 1
127
#define HAVE_SYSTEM 1
130
#define fsync _commit
132
#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133
/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
134
#else /* all other compilers */
135
/* Unix functions that the configure script doesn't check for */
138
#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141
#define HAVE_GETCWD 1
142
#define HAVE_GETEGID 1
143
#define HAVE_GETEUID 1
144
#define HAVE_GETGID 1
145
#define HAVE_GETPPID 1
146
#define HAVE_GETUID 1
148
#define HAVE_OPENDIR 1
150
#define HAVE_SYSTEM 1
152
#define HAVE_TTYNAME 1
153
#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
154
#endif /* _MSC_VER */
155
#endif /* __BORLANDC__ */
156
#endif /* ! __WATCOMC__ || __QNX__ */
157
#endif /* ! __IBMC__ */
161
#if defined(__sgi)&&_COMPILER_VERSION>=700
162
/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164
extern char *ctermid_r(char *);
167
#ifndef HAVE_UNISTD_H
168
#if defined(PYCC_VACPP)
169
extern int mkdir(char *);
171
#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
172
extern int mkdir(const char *);
174
extern int mkdir(const char *, mode_t);
177
#if defined(__IBMC__) || defined(__IBMCPP__)
178
extern int chdir(char *);
179
extern int rmdir(char *);
181
extern int chdir(const char *);
182
extern int rmdir(const char *);
185
extern int chmod(const char *, int);
187
extern int chmod(const char *, mode_t);
190
extern int fchmod(int, mode_t);
193
extern int lchmod(const char *, mode_t);
195
extern int chown(const char *, uid_t, gid_t);
196
extern char *getcwd(char *, int);
197
extern char *strerror(int);
198
extern int link(const char *, const char *);
199
extern int rename(const char *, const char *);
200
extern int stat(const char *, struct stat *);
201
extern int unlink(const char *);
203
extern int symlink(const char *, const char *);
204
#endif /* HAVE_SYMLINK */
206
extern int lstat(const char *, struct stat *);
207
#endif /* HAVE_LSTAT */
208
#endif /* !HAVE_UNISTD_H */
210
#endif /* !_MSC_VER */
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 */
235
#define NAMLEN(dirent) strlen((dirent)->d_name)
237
#if defined(__WATCOMC__) && !defined(__QNX__)
239
#define NAMLEN(dirent) strlen((dirent)->d_name)
241
#define dirent direct
242
#define NAMLEN(dirent) (dirent)->d_namlen
244
#ifdef HAVE_SYS_NDIR_H
245
#include <sys/ndir.h>
247
#ifdef HAVE_SYS_DIR_H
262
#ifdef HAVE_PROCESS_H
267
#include <shellapi.h> /* for ShellExecute() */
268
#endif /* _MSC_VER */
270
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
275
#if defined(PATH_MAX) && PATH_MAX > 1024
276
#define MAXPATHLEN PATH_MAX
278
#define MAXPATHLEN 1024
280
#endif /* MAXPATHLEN */
283
/* Emulate some macros on systems that have a union instead of macros */
286
#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
290
#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
294
#define WTERMSIG(u_wait) ((u_wait).w_termsig)
297
#define WAIT_TYPE union wait
298
#define WAIT_STATUS_INT(s) (s.w_status)
300
#else /* !UNION_WAIT */
301
#define WAIT_TYPE int
302
#define WAIT_STATUS_INT(s) (s)
303
#endif /* UNION_WAIT */
305
/* Don't use the "_r" form if we don't need it (also, won't have a
306
prototype for it, at least on Solaris -- maybe others as well?). */
307
#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
308
#define USE_CTERMID_R
311
/* choose the appropriate stat and fstat functions and return structs */
313
#if defined(MS_WIN64) || defined(MS_WINDOWS)
314
# define STAT win32_stat
315
# define FSTAT win32_fstat
316
# define STRUCT_STAT struct win32_stat
320
# define STRUCT_STAT struct stat
323
#if defined(MAJOR_IN_MKDEV)
324
#include <sys/mkdev.h>
326
#if defined(MAJOR_IN_SYSMACROS)
327
#include <sys/sysmacros.h>
329
#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
330
#include <sys/mkdev.h>
334
#if defined _MSC_VER && _MSC_VER >= 1400
335
/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
336
* valid and throw an assertion if it isn't.
337
* Normally, an invalid fd is likely to be a C program error and therefore
338
* an assertion can be useful, but it does contradict the POSIX standard
339
* which for write(2) states:
340
* "Otherwise, -1 shall be returned and errno set to indicate the error."
341
* "[EBADF] The fildes argument is not a valid file descriptor open for
343
* Furthermore, python allows the user to enter any old integer
344
* as a fd and should merely raise a python exception on error.
345
* The Microsoft CRT doesn't provide an official way to check for the
346
* validity of a file descriptor, but we can emulate its internal behaviour
347
* by using the exported __pinfo data member and knowledge of the
348
* internal structures involved.
349
* The structures below must be updated for each version of visual studio
350
* according to the file internal.h in the CRT source, until MS comes
351
* up with a less hacky way to do this.
352
* (all of this is to avoid globally modifying the CRT behaviour using
353
* _set_invalid_parameter_handler() and _CrtSetReportMode())
355
#if _MSC_VER >= 1500 /* VS 2008 */
361
CRITICAL_SECTION lock;
362
#ifndef _SAFECRT_IMPL
367
BOOL utf8translations;
370
#endif /* _SAFECRT_IMPL */
372
#elif _MSC_VER >= 1400 /* VS 2005 */
378
CRITICAL_SECTION lock;
379
#ifndef _SAFECRT_IMPL
384
BOOL utf8translations;
386
/* padding hack. 8 byte extra length observed at
387
* runtime, for 32 and 64 bits when not in _DEBUG
391
#endif /* _SAFECRT_IMPL */
395
extern __declspec(dllimport) ioinfo * __pioinfo[];
397
#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
398
#define IOINFO_ARRAYS 64
399
#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
401
#define _NO_CONSOLE_FILENO (intptr_t)-2
403
/* This function emulates what the windows CRT does to validate file handles */
407
const int i1 = fd >> IOINFO_L2E;
408
const int i2 = fd & ((1 << IOINFO_L2E) - 1);
410
/* See that it isn't a special CLEAR fileno */
411
if (fd != _NO_CONSOLE_FILENO) {
412
/* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
413
* we check pointer validity and other info
415
if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
416
/* finally, check that the file is open */
417
if (__pioinfo[i1][i2].osfile & FOPEN)
425
/* the special case of checking dup2. The target fd must be in a sensible range */
427
_PyVerify_fd_dup2(int fd1, int fd2)
429
if (!_PyVerify_fd(fd1))
431
if (fd2 == _NO_CONSOLE_FILENO)
433
if ((unsigned)fd2 < _NHANDLE_)
439
/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
440
#define _PyVerify_fd_dup2(A, B) (1)
443
/* Return a dictionary corresponding to the POSIX environment table */
444
#ifdef WITH_NEXT_FRAMEWORK
445
/* On Darwin/MacOSX a shared library or framework has no access to
446
** environ directly, we must obtain it with _NSGetEnviron().
448
#include <crt_externs.h>
449
static char **environ;
450
#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
451
extern char **environ;
452
#endif /* !_MSC_VER */
466
#ifdef WITH_NEXT_FRAMEWORK
468
environ = *_NSGetEnviron();
471
/* _wenviron must be initialized in this way if the program is started
472
through main() instead of wmain(). */
474
if (_wenviron == NULL)
476
/* This part ignores errors */
477
for (e = _wenviron; *e != NULL; e++) {
480
wchar_t *p = wcschr(*e, L'=');
483
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
488
v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
494
if (PyDict_GetItem(d, k) == NULL) {
495
if (PyDict_SetItem(d, k, v) != 0)
504
/* This part ignores errors */
505
for (e = environ; *e != NULL; e++) {
508
char *p = strchr(*e, '=');
511
k = PyUnicode_FromStringAndSize(*e, (int)(p-*e));
516
v = PyUnicode_FromString(p+1);
522
if (PyDict_GetItem(d, k) == NULL) {
523
if (PyDict_SetItem(d, k, v) != 0)
530
#if defined(PYOS_OS2)
533
char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
535
rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
536
if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
537
PyObject *v = PyBytes_FromString(buffer);
538
PyDict_SetItemString(d, "BEGINLIBPATH", v);
541
rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
542
if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
543
PyObject *v = PyBytes_FromString(buffer);
544
PyDict_SetItemString(d, "ENDLIBPATH", v);
553
/* Set a POSIX-specific error from errno, and return NULL */
558
return PyErr_SetFromErrno(PyExc_OSError);
561
posix_error_with_filename(char* name)
563
return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
566
#ifdef Py_WIN_WIDE_FILENAMES
568
posix_error_with_unicode_filename(Py_UNICODE* name)
570
return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
572
#endif /* Py_WIN_WIDE_FILENAMES */
576
posix_error_with_allocated_filename(char* name)
578
PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
585
win32_error(char* function, char* filename)
587
/* XXX We should pass the function name along in the future.
588
(winreg.c also wants to pass the function name.)
589
This would however require an additional param to the
590
Windows error object, which is non-trivial.
592
errno = GetLastError();
594
return PyErr_SetFromWindowsErrWithFilename(errno, filename);
596
return PyErr_SetFromWindowsErr(errno);
599
#ifdef Py_WIN_WIDE_FILENAMES
601
win32_error_unicode(char* function, Py_UNICODE* filename)
603
/* XXX - see win32_error for comments on 'function' */
604
errno = GetLastError();
606
return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
608
return PyErr_SetFromWindowsErr(errno);
612
convert_to_unicode(PyObject **param)
614
if (PyUnicode_CheckExact(*param))
616
else if (PyUnicode_Check(*param))
617
/* For a Unicode subtype that's not a Unicode object,
618
return a true Unicode object with the same data. */
619
*param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
620
PyUnicode_GET_SIZE(*param));
622
*param = PyUnicode_FromEncodedObject(*param,
623
Py_FileSystemDefaultEncoding,
625
return (*param) != NULL;
628
#endif /* Py_WIN_WIDE_FILENAMES */
632
#if defined(PYOS_OS2)
633
/**********************************************************************
634
* Helper Function to Trim and Format OS/2 Messages
635
**********************************************************************/
637
os2_formatmsg(char *msgbuf, int msglen, char *reason)
639
msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
641
if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
642
char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
644
while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
645
*lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
648
/* Add Optional Reason Text */
650
strcat(msgbuf, " : ");
651
strcat(msgbuf, reason);
655
/**********************************************************************
656
* Decode an OS/2 Operating System Error Code
658
* A convenience function to lookup an OS/2 error code and return a
659
* text message we can use to raise a Python exception.
662
* The messages for errors returned from the OS/2 kernel reside in
663
* the file OSO001.MSG in the \OS2 directory hierarchy.
665
**********************************************************************/
667
os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
672
/* Retrieve Kernel-Related Error Message from OSO001.MSG File */
673
Py_BEGIN_ALLOW_THREADS
674
rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
675
errorcode, "oso001.msg", &msglen);
679
os2_formatmsg(msgbuf, msglen, reason);
681
PyOS_snprintf(msgbuf, msgbuflen,
682
"unknown OS error #%d", errorcode);
687
/* Set an OS/2-specific error and return NULL. OS/2 kernel
688
errors are not in a global variable e.g. 'errno' nor are
689
they congruent with posix error numbers. */
691
static PyObject * os2_error(int code)
696
os2_strerror(text, sizeof(text), code, "");
698
v = Py_BuildValue("(is)", code, text);
700
PyErr_SetObject(PyExc_OSError, v);
703
return NULL; /* Signal to Python that an Exception is Pending */
708
/* POSIX generic methods */
711
posix_fildes(PyObject *fdobj, int (*func)(int))
715
fd = PyObject_AsFileDescriptor(fdobj);
718
if (!_PyVerify_fd(fd))
719
return posix_error();
720
Py_BEGIN_ALLOW_THREADS
724
return posix_error();
729
#ifdef Py_WIN_WIDE_FILENAMES
731
unicode_file_names(void)
733
static int canusewide = -1;
734
if (canusewide == -1) {
735
/* As per doc for ::GetVersion(), this is the correct test for
736
the Windows NT family. */
737
canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
744
posix_1str(PyObject *args, char *format, int (*func)(const char*))
748
if (!PyArg_ParseTuple(args, format,
749
Py_FileSystemDefaultEncoding, &path1))
751
Py_BEGIN_ALLOW_THREADS
752
res = (*func)(path1);
755
return posix_error_with_allocated_filename(path1);
762
posix_2str(PyObject *args,
764
int (*func)(const char *, const char *))
766
char *path1 = NULL, *path2 = NULL;
768
if (!PyArg_ParseTuple(args, format,
769
Py_FileSystemDefaultEncoding, &path1,
770
Py_FileSystemDefaultEncoding, &path2))
772
Py_BEGIN_ALLOW_THREADS
773
res = (*func)(path1, path2);
778
/* XXX how to report both path1 and path2??? */
779
return posix_error();
784
#ifdef Py_WIN_WIDE_FILENAMES
786
win32_1str(PyObject* args, char* func,
787
char* format, BOOL (__stdcall *funcA)(LPCSTR),
788
char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
793
if (unicode_file_names()) {
794
if (!PyArg_ParseTuple(args, wformat, &uni))
797
Py_BEGIN_ALLOW_THREADS
798
result = funcW(PyUnicode_AsUnicode(uni));
801
return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
806
if (!PyArg_ParseTuple(args, format, &ansi))
808
Py_BEGIN_ALLOW_THREADS
809
result = funcA(ansi);
812
return win32_error(func, ansi);
818
/* This is a reimplementation of the C library's chdir function,
819
but one that produces Win32 errors instead of DOS error codes.
820
chdir is essentially a wrapper around SetCurrentDirectory; however,
821
it also needs to set "magic" environment variables indicating
822
the per-drive current directory, which are of the form =<drive>: */
823
static BOOL __stdcall
824
win32_chdir(LPCSTR path)
826
char new_path[MAX_PATH+1];
830
if(!SetCurrentDirectoryA(path))
832
result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
835
/* In the ANSI API, there should not be any paths longer
837
assert(result <= MAX_PATH+1);
838
if (strncmp(new_path, "\\\\", 2) == 0 ||
839
strncmp(new_path, "//", 2) == 0)
840
/* UNC path, nothing to do. */
842
env[1] = new_path[0];
843
return SetEnvironmentVariableA(env, new_path);
846
/* The Unicode version differs from the ANSI version
847
since the current directory might exceed MAX_PATH characters */
848
static BOOL __stdcall
849
win32_wchdir(LPCWSTR path)
851
wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
853
wchar_t env[4] = L"=x:";
855
if(!SetCurrentDirectoryW(path))
857
result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
860
if (result > MAX_PATH+1) {
861
new_path = malloc(result * sizeof(wchar_t));
863
SetLastError(ERROR_OUTOFMEMORY);
866
result = GetCurrentDirectoryW(result, new_path);
872
if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
873
wcsncmp(new_path, L"//", 2) == 0)
874
/* UNC path, nothing to do. */
876
env[1] = new_path[0];
877
result = SetEnvironmentVariableW(env, new_path);
878
if (new_path != _new_path)
885
/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
886
- time stamps are restricted to second resolution
887
- file modification times suffer from forth-and-back conversions between
889
Therefore, we implement our own stat, based on the Win32 API directly.
891
#define HAVE_STAT_NSEC 1
896
unsigned short st_mode;
910
static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
913
FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
915
/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
916
/* Cannot simply cast and dereference in_ptr,
917
since it might not be aligned properly */
919
memcpy(&in, in_ptr, sizeof(in));
920
*nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
921
/* XXX Win32 supports time stamps past 2038; we currently don't */
922
*time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
926
time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
930
out = time_in + secs_between_epochs;
931
out = out * 10000000 + nsec_in / 100;
932
memcpy(out_ptr, &out, sizeof(out));
935
/* Below, we *know* that ugo+r is 0444 */
937
#error Unsupported C library
940
attributes_to_mode(DWORD attr)
943
if (attr & FILE_ATTRIBUTE_DIRECTORY)
944
m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
947
if (attr & FILE_ATTRIBUTE_READONLY)
955
attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
957
memset(result, 0, sizeof(*result));
958
result->st_mode = attributes_to_mode(info->dwFileAttributes);
959
result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
960
FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
961
FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
962
FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
967
/* Emulate GetFileAttributesEx[AW] on Windows 95 */
968
static int checked = 0;
969
static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
970
static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
978
hKernel32 = GetModuleHandle("KERNEL32");
979
*(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
980
*(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
984
attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
987
WIN32_FIND_DATAA FileData;
988
hFindFile = FindFirstFileA(pszFile, &FileData);
989
if (hFindFile == INVALID_HANDLE_VALUE)
991
FindClose(hFindFile);
992
pfad->dwFileAttributes = FileData.dwFileAttributes;
993
pfad->ftCreationTime = FileData.ftCreationTime;
994
pfad->ftLastAccessTime = FileData.ftLastAccessTime;
995
pfad->ftLastWriteTime = FileData.ftLastWriteTime;
996
pfad->nFileSizeHigh = FileData.nFileSizeHigh;
997
pfad->nFileSizeLow = FileData.nFileSizeLow;
1002
attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
1005
WIN32_FIND_DATAW FileData;
1006
hFindFile = FindFirstFileW(pszFile, &FileData);
1007
if (hFindFile == INVALID_HANDLE_VALUE)
1009
FindClose(hFindFile);
1010
pfad->dwFileAttributes = FileData.dwFileAttributes;
1011
pfad->ftCreationTime = FileData.ftCreationTime;
1012
pfad->ftLastAccessTime = FileData.ftLastAccessTime;
1013
pfad->ftLastWriteTime = FileData.ftLastWriteTime;
1014
pfad->nFileSizeHigh = FileData.nFileSizeHigh;
1015
pfad->nFileSizeLow = FileData.nFileSizeLow;
1020
Py_GetFileAttributesExA(LPCSTR pszFile,
1021
GET_FILEEX_INFO_LEVELS level,
1025
LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1026
/* First try to use the system's implementation, if that is
1027
available and either succeeds to gives an error other than
1028
that it isn't implemented. */
1031
result = gfaxa(pszFile, level, pv);
1032
if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1035
/* It's either not present, or not implemented.
1036
Emulate using FindFirstFile. */
1037
if (level != GetFileExInfoStandard) {
1038
SetLastError(ERROR_INVALID_PARAMETER);
1041
/* Use GetFileAttributes to validate that the file name
1042
does not contain wildcards (which FindFirstFile would
1044
if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
1046
return attributes_from_dir(pszFile, pfad);
1050
Py_GetFileAttributesExW(LPCWSTR pszFile,
1051
GET_FILEEX_INFO_LEVELS level,
1055
LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
1056
/* First try to use the system's implementation, if that is
1057
available and either succeeds to gives an error other than
1058
that it isn't implemented. */
1061
result = gfaxw(pszFile, level, pv);
1062
if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
1065
/* It's either not present, or not implemented.
1066
Emulate using FindFirstFile. */
1067
if (level != GetFileExInfoStandard) {
1068
SetLastError(ERROR_INVALID_PARAMETER);
1071
/* Use GetFileAttributes to validate that the file name
1072
does not contain wildcards (which FindFirstFile would
1074
if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
1076
return attributes_from_dir_w(pszFile, pfad);
1080
win32_stat(const char* path, struct win32_stat *result)
1082
WIN32_FILE_ATTRIBUTE_DATA info;
1085
/* XXX not supported on Win95 and NT 3.x */
1086
if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1087
if (GetLastError() != ERROR_SHARING_VIOLATION) {
1088
/* Protocol violation: we explicitly clear errno, instead of
1089
setting it to a POSIX error. Callers should use GetLastError. */
1093
/* Could not get attributes on open file. Fall back to
1094
reading the directory. */
1095
if (!attributes_from_dir(path, &info)) {
1096
/* Very strange. This should not fail now */
1102
code = attribute_data_to_stat(&info, result);
1105
/* Set S_IFEXEC if it is an .exe, .bat, ... */
1106
dot = strrchr(path, '.');
1108
if (stricmp(dot, ".bat") == 0 ||
1109
stricmp(dot, ".cmd") == 0 ||
1110
stricmp(dot, ".exe") == 0 ||
1111
stricmp(dot, ".com") == 0)
1112
result->st_mode |= 0111;
1118
win32_wstat(const wchar_t* path, struct win32_stat *result)
1122
WIN32_FILE_ATTRIBUTE_DATA info;
1123
/* XXX not supported on Win95 and NT 3.x */
1124
if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1125
if (GetLastError() != ERROR_SHARING_VIOLATION) {
1126
/* Protocol violation: we explicitly clear errno, instead of
1127
setting it to a POSIX error. Callers should use GetLastError. */
1131
/* Could not get attributes on open file. Fall back to
1132
reading the directory. */
1133
if (!attributes_from_dir_w(path, &info)) {
1134
/* Very strange. This should not fail now */
1140
code = attribute_data_to_stat(&info, result);
1143
/* Set IFEXEC if it is an .exe, .bat, ... */
1144
dot = wcsrchr(path, '.');
1146
if (_wcsicmp(dot, L".bat") == 0 ||
1147
_wcsicmp(dot, L".cmd") == 0 ||
1148
_wcsicmp(dot, L".exe") == 0 ||
1149
_wcsicmp(dot, L".com") == 0)
1150
result->st_mode |= 0111;
1156
win32_fstat(int file_number, struct win32_stat *result)
1158
BY_HANDLE_FILE_INFORMATION info;
1162
h = (HANDLE)_get_osfhandle(file_number);
1164
/* Protocol violation: we explicitly clear errno, instead of
1165
setting it to a POSIX error. Callers should use GetLastError. */
1168
if (h == INVALID_HANDLE_VALUE) {
1169
/* This is really a C library error (invalid file handle).
1170
We set the Win32 error to the closes one matching. */
1171
SetLastError(ERROR_INVALID_HANDLE);
1174
memset(result, 0, sizeof(*result));
1176
type = GetFileType(h);
1177
if (type == FILE_TYPE_UNKNOWN) {
1178
DWORD error = GetLastError();
1182
/* else: valid but unknown file */
1185
if (type != FILE_TYPE_DISK) {
1186
if (type == FILE_TYPE_CHAR)
1187
result->st_mode = _S_IFCHR;
1188
else if (type == FILE_TYPE_PIPE)
1189
result->st_mode = _S_IFIFO;
1193
if (!GetFileInformationByHandle(h, &info)) {
1197
/* similar to stat() */
1198
result->st_mode = attributes_to_mode(info.dwFileAttributes);
1199
result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1200
FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1201
FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1202
FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1203
/* specific to fstat() */
1204
result->st_nlink = info.nNumberOfLinks;
1205
result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1209
#endif /* MS_WINDOWS */
1211
PyDoc_STRVAR(stat_result__doc__,
1212
"stat_result: Result from stat or lstat.\n\n\
1213
This object may be accessed either as a tuple of\n\
1214
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1215
or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1217
Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1218
or st_flags, they are available as attributes only.\n\
1220
See os.stat for more information.");
1222
static PyStructSequence_Field stat_result_fields[] = {
1223
{"st_mode", "protection bits"},
1224
{"st_ino", "inode"},
1225
{"st_dev", "device"},
1226
{"st_nlink", "number of hard links"},
1227
{"st_uid", "user ID of owner"},
1228
{"st_gid", "group ID of owner"},
1229
{"st_size", "total size, in bytes"},
1230
/* The NULL is replaced with PyStructSequence_UnnamedField later. */
1231
{NULL, "integer time of last access"},
1232
{NULL, "integer time of last modification"},
1233
{NULL, "integer time of last change"},
1234
{"st_atime", "time of last access"},
1235
{"st_mtime", "time of last modification"},
1236
{"st_ctime", "time of last change"},
1237
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1238
{"st_blksize", "blocksize for filesystem I/O"},
1240
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1241
{"st_blocks", "number of blocks allocated"},
1243
#ifdef HAVE_STRUCT_STAT_ST_RDEV
1244
{"st_rdev", "device type (if inode device)"},
1246
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1247
{"st_flags", "user defined flags for file"},
1249
#ifdef HAVE_STRUCT_STAT_ST_GEN
1250
{"st_gen", "generation number"},
1252
#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1253
{"st_birthtime", "time of creation"},
1258
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1259
#define ST_BLKSIZE_IDX 13
1261
#define ST_BLKSIZE_IDX 12
1264
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1265
#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1267
#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1270
#ifdef HAVE_STRUCT_STAT_ST_RDEV
1271
#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1273
#define ST_RDEV_IDX ST_BLOCKS_IDX
1276
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1277
#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1279
#define ST_FLAGS_IDX ST_RDEV_IDX
1282
#ifdef HAVE_STRUCT_STAT_ST_GEN
1283
#define ST_GEN_IDX (ST_FLAGS_IDX+1)
1285
#define ST_GEN_IDX ST_FLAGS_IDX
1288
#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1289
#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1291
#define ST_BIRTHTIME_IDX ST_GEN_IDX
1294
static PyStructSequence_Desc stat_result_desc = {
1295
"stat_result", /* name */
1296
stat_result__doc__, /* doc */
1301
PyDoc_STRVAR(statvfs_result__doc__,
1302
"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1303
This object may be accessed either as a tuple of\n\
1304
(bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1305
or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
1307
See os.statvfs for more information.");
1309
static PyStructSequence_Field statvfs_result_fields[] = {
1323
static PyStructSequence_Desc statvfs_result_desc = {
1324
"statvfs_result", /* name */
1325
statvfs_result__doc__, /* doc */
1326
statvfs_result_fields,
1330
static int initialized;
1331
static PyTypeObject StatResultType;
1332
static PyTypeObject StatVFSResultType;
1333
static newfunc structseq_new;
1336
statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1338
PyStructSequence *result;
1341
result = (PyStructSequence*)structseq_new(type, args, kwds);
1344
/* If we have been initialized from a tuple,
1345
st_?time might be set to None. Initialize it
1346
from the int slots. */
1347
for (i = 7; i <= 9; i++) {
1348
if (result->ob_item[i+3] == Py_None) {
1350
Py_INCREF(result->ob_item[i]);
1351
result->ob_item[i+3] = result->ob_item[i];
1354
return (PyObject*)result;
1359
/* If true, st_?time is float. */
1360
static int _stat_float_times = 1;
1362
PyDoc_STRVAR(stat_float_times__doc__,
1363
"stat_float_times([newval]) -> oldval\n\n\
1364
Determine whether os.[lf]stat represents time stamps as float objects.\n\
1365
If newval is True, future calls to stat() return floats, if it is False,\n\
1366
future calls return ints. \n\
1367
If newval is omitted, return the current setting.\n");
1370
stat_float_times(PyObject* self, PyObject *args)
1373
if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1376
/* Return old value */
1377
return PyBool_FromLong(_stat_float_times);
1378
_stat_float_times = newval;
1384
fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1386
PyObject *fval,*ival;
1387
#if SIZEOF_TIME_T > SIZEOF_LONG
1388
ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
1390
ival = PyLong_FromLong((long)sec);
1394
if (_stat_float_times) {
1395
fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1400
PyStructSequence_SET_ITEM(v, index, ival);
1401
PyStructSequence_SET_ITEM(v, index+3, fval);
1404
/* pack a system stat C structure into the Python stat tuple
1405
(used by posix_stat() and posix_fstat()) */
1407
_pystat_fromstructstat(STRUCT_STAT *st)
1409
unsigned long ansec, mnsec, cnsec;
1410
PyObject *v = PyStructSequence_New(&StatResultType);
1414
PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
1415
#ifdef HAVE_LARGEFILE_SUPPORT
1416
PyStructSequence_SET_ITEM(v, 1,
1417
PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
1419
PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
1421
#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
1422
PyStructSequence_SET_ITEM(v, 2,
1423
PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
1425
PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
1427
PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1428
PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1429
PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
1430
#ifdef HAVE_LARGEFILE_SUPPORT
1431
PyStructSequence_SET_ITEM(v, 6,
1432
PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
1434
PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
1437
#if defined(HAVE_STAT_TV_NSEC)
1438
ansec = st->st_atim.tv_nsec;
1439
mnsec = st->st_mtim.tv_nsec;
1440
cnsec = st->st_ctim.tv_nsec;
1441
#elif defined(HAVE_STAT_TV_NSEC2)
1442
ansec = st->st_atimespec.tv_nsec;
1443
mnsec = st->st_mtimespec.tv_nsec;
1444
cnsec = st->st_ctimespec.tv_nsec;
1445
#elif defined(HAVE_STAT_NSEC)
1446
ansec = st->st_atime_nsec;
1447
mnsec = st->st_mtime_nsec;
1448
cnsec = st->st_ctime_nsec;
1450
ansec = mnsec = cnsec = 0;
1452
fill_time(v, 7, st->st_atime, ansec);
1453
fill_time(v, 8, st->st_mtime, mnsec);
1454
fill_time(v, 9, st->st_ctime, cnsec);
1456
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1457
PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1458
PyLong_FromLong((long)st->st_blksize));
1460
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1461
PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1462
PyLong_FromLong((long)st->st_blocks));
1464
#ifdef HAVE_STRUCT_STAT_ST_RDEV
1465
PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1466
PyLong_FromLong((long)st->st_rdev));
1468
#ifdef HAVE_STRUCT_STAT_ST_GEN
1469
PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1470
PyLong_FromLong((long)st->st_gen));
1472
#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1475
unsigned long bsec,bnsec;
1476
bsec = (long)st->st_birthtime;
1477
#ifdef HAVE_STAT_TV_NSEC2
1478
bnsec = st->st_birthtimespec.tv_nsec;
1482
if (_stat_float_times) {
1483
val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1485
val = PyLong_FromLong((long)bsec);
1487
PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1491
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1492
PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1493
PyLong_FromLong((long)st->st_flags));
1496
if (PyErr_Occurred()) {
1506
/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1507
where / can be used in place of \ and the trailing slash is optional.
1508
Both SERVER and SHARE must have at least one character.
1511
#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1512
#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1514
#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1518
IsUNCRootA(char *path, int pathlen)
1520
#define ISSLASH ISSLASHA
1524
if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1525
/* minimum UNCRoot is \\x\y */
1527
for (i = 2; i < pathlen ; i++)
1528
if (ISSLASH(path[i])) break;
1529
if (i == 2 || i == pathlen)
1530
/* do not allow \\\SHARE or \\SERVER */
1533
for (i = share; i < pathlen; i++)
1534
if (ISSLASH(path[i])) break;
1535
return (i != share && (i == pathlen || i == pathlen-1));
1540
#ifdef Py_WIN_WIDE_FILENAMES
1542
IsUNCRootW(Py_UNICODE *path, int pathlen)
1544
#define ISSLASH ISSLASHW
1548
if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1549
/* minimum UNCRoot is \\x\y */
1551
for (i = 2; i < pathlen ; i++)
1552
if (ISSLASH(path[i])) break;
1553
if (i == 2 || i == pathlen)
1554
/* do not allow \\\SHARE or \\SERVER */
1557
for (i = share; i < pathlen; i++)
1558
if (ISSLASH(path[i])) break;
1559
return (i != share && (i == pathlen || i == pathlen-1));
1563
#endif /* Py_WIN_WIDE_FILENAMES */
1564
#endif /* MS_WINDOWS */
1567
posix_do_stat(PyObject *self, PyObject *args,
1570
int (*statfunc)(const char *, STRUCT_STAT *, ...),
1572
int (*statfunc)(const char *, STRUCT_STAT *),
1575
int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
1578
char *path = NULL; /* pass this to stat; do not free() it */
1579
char *pathfree = NULL; /* this memory must be free'd */
1583
#ifdef Py_WIN_WIDE_FILENAMES
1584
/* If on wide-character-capable OS see if argument
1585
is Unicode and if so use wide API. */
1586
if (unicode_file_names()) {
1587
PyUnicodeObject *po;
1588
if (PyArg_ParseTuple(args, wformat, &po)) {
1589
Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1591
Py_BEGIN_ALLOW_THREADS
1592
/* PyUnicode_AS_UNICODE result OK without
1593
thread lock as it is a simple dereference. */
1594
res = wstatfunc(wpath, &st);
1595
Py_END_ALLOW_THREADS
1598
return win32_error_unicode("stat", wpath);
1599
return _pystat_fromstructstat(&st);
1601
/* Drop the argument parsing error as narrow strings
1607
if (!PyArg_ParseTuple(args, format,
1608
Py_FileSystemDefaultEncoding, &path))
1612
Py_BEGIN_ALLOW_THREADS
1613
res = (*statfunc)(path, &st);
1614
Py_END_ALLOW_THREADS
1618
result = win32_error("stat", pathfree);
1620
result = posix_error_with_filename(pathfree);
1624
result = _pystat_fromstructstat(&st);
1626
PyMem_Free(pathfree);
1632
PyDoc_STRVAR(posix_access__doc__,
1633
"access(path, mode) -> True if granted, False otherwise\n\n\
1634
Use the real uid/gid to test for access to a path. Note that most\n\
1635
operations will use the effective uid/gid, therefore this routine can\n\
1636
be used in a suid/sgid environment to test if the invoking user has the\n\
1637
specified access to the path. The mode argument can be F_OK to test\n\
1638
existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
1641
posix_access(PyObject *self, PyObject *args)
1646
#ifdef Py_WIN_WIDE_FILENAMES
1648
if (unicode_file_names()) {
1649
PyUnicodeObject *po;
1650
if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1651
Py_BEGIN_ALLOW_THREADS
1652
/* PyUnicode_AS_UNICODE OK without thread lock as
1653
it is a simple dereference. */
1654
attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1655
Py_END_ALLOW_THREADS
1658
/* Drop the argument parsing error as narrow strings
1662
if (!PyArg_ParseTuple(args, "eti:access",
1663
Py_FileSystemDefaultEncoding, &path, &mode))
1665
Py_BEGIN_ALLOW_THREADS
1666
attr = GetFileAttributesA(path);
1667
Py_END_ALLOW_THREADS
1670
if (attr == 0xFFFFFFFF)
1671
/* File does not exist, or cannot read attributes */
1672
return PyBool_FromLong(0);
1673
/* Access is possible if either write access wasn't requested, or
1674
the file isn't read-only, or if it's a directory, as there are
1675
no read-only directories on Windows. */
1676
return PyBool_FromLong(!(mode & 2)
1677
|| !(attr & FILE_ATTRIBUTE_READONLY)
1678
|| (attr & FILE_ATTRIBUTE_DIRECTORY));
1681
if (!PyArg_ParseTuple(args, "eti:access",
1682
Py_FileSystemDefaultEncoding, &path, &mode))
1684
Py_BEGIN_ALLOW_THREADS
1685
res = access(path, mode);
1686
Py_END_ALLOW_THREADS
1688
return PyBool_FromLong(res == 0);
1706
PyDoc_STRVAR(posix_ttyname__doc__,
1707
"ttyname(fd) -> string\n\n\
1708
Return the name of the terminal device connected to 'fd'.");
1711
posix_ttyname(PyObject *self, PyObject *args)
1716
if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1720
/* file descriptor 0 only, the default input device (stdin) */
1731
return posix_error();
1732
return PyUnicode_FromString(ret);
1737
PyDoc_STRVAR(posix_ctermid__doc__,
1738
"ctermid() -> string\n\n\
1739
Return the name of the controlling terminal for this process.");
1742
posix_ctermid(PyObject *self, PyObject *noargs)
1745
char buffer[L_ctermid];
1747
#ifdef USE_CTERMID_R
1748
ret = ctermid_r(buffer);
1750
ret = ctermid(buffer);
1753
return posix_error();
1754
return PyUnicode_FromString(buffer);
1758
PyDoc_STRVAR(posix_chdir__doc__,
1760
Change the current working directory to the specified path.");
1763
posix_chdir(PyObject *self, PyObject *args)
1766
return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
1767
#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1768
return posix_1str(args, "et:chdir", _chdir2);
1769
#elif defined(__VMS)
1770
return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
1772
return posix_1str(args, "et:chdir", chdir);
1777
PyDoc_STRVAR(posix_fchdir__doc__,
1778
"fchdir(fildes)\n\n\
1779
Change to the directory of the given file descriptor. fildes must be\n\
1780
opened on a directory, not a file.");
1783
posix_fchdir(PyObject *self, PyObject *fdobj)
1785
return posix_fildes(fdobj, fchdir);
1787
#endif /* HAVE_FCHDIR */
1790
PyDoc_STRVAR(posix_chmod__doc__,
1791
"chmod(path, mode)\n\n\
1792
Change the access permissions of a file.");
1795
posix_chmod(PyObject *self, PyObject *args)
1800
#ifdef Py_WIN_WIDE_FILENAMES
1802
if (unicode_file_names()) {
1803
PyUnicodeObject *po;
1804
if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1805
Py_BEGIN_ALLOW_THREADS
1806
attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1807
if (attr != 0xFFFFFFFF) {
1809
attr &= ~FILE_ATTRIBUTE_READONLY;
1811
attr |= FILE_ATTRIBUTE_READONLY;
1812
res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1816
Py_END_ALLOW_THREADS
1818
return win32_error_unicode("chmod",
1819
PyUnicode_AS_UNICODE(po));
1823
/* Drop the argument parsing error as narrow strings
1827
if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1830
Py_BEGIN_ALLOW_THREADS
1831
attr = GetFileAttributesA(path);
1832
if (attr != 0xFFFFFFFF) {
1834
attr &= ~FILE_ATTRIBUTE_READONLY;
1836
attr |= FILE_ATTRIBUTE_READONLY;
1837
res = SetFileAttributesA(path, attr);
1841
Py_END_ALLOW_THREADS
1843
win32_error("chmod", path);
1850
#else /* Py_WIN_WIDE_FILENAMES */
1851
if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1854
Py_BEGIN_ALLOW_THREADS
1855
res = chmod(path, i);
1856
Py_END_ALLOW_THREADS
1858
return posix_error_with_allocated_filename(path);
1866
PyDoc_STRVAR(posix_fchmod__doc__,
1867
"fchmod(fd, mode)\n\n\
1868
Change the access permissions of the file given by file\n\
1872
posix_fchmod(PyObject *self, PyObject *args)
1875
if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1877
Py_BEGIN_ALLOW_THREADS
1878
res = fchmod(fd, mode);
1879
Py_END_ALLOW_THREADS
1881
return posix_error();
1884
#endif /* HAVE_FCHMOD */
1887
PyDoc_STRVAR(posix_lchmod__doc__,
1888
"lchmod(path, mode)\n\n\
1889
Change the access permissions of a file. If path is a symlink, this\n\
1890
affects the link itself rather than the target.");
1893
posix_lchmod(PyObject *self, PyObject *args)
1898
if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1901
Py_BEGIN_ALLOW_THREADS
1902
res = lchmod(path, i);
1903
Py_END_ALLOW_THREADS
1905
return posix_error_with_allocated_filename(path);
1909
#endif /* HAVE_LCHMOD */
1913
PyDoc_STRVAR(posix_chflags__doc__,
1914
"chflags(path, flags)\n\n\
1918
posix_chflags(PyObject *self, PyObject *args)
1921
unsigned long flags;
1923
if (!PyArg_ParseTuple(args, "etk:chflags",
1924
Py_FileSystemDefaultEncoding, &path, &flags))
1926
Py_BEGIN_ALLOW_THREADS
1927
res = chflags(path, flags);
1928
Py_END_ALLOW_THREADS
1930
return posix_error_with_allocated_filename(path);
1935
#endif /* HAVE_CHFLAGS */
1937
#ifdef HAVE_LCHFLAGS
1938
PyDoc_STRVAR(posix_lchflags__doc__,
1939
"lchflags(path, flags)\n\n\
1941
This function will not follow symbolic links.");
1944
posix_lchflags(PyObject *self, PyObject *args)
1947
unsigned long flags;
1949
if (!PyArg_ParseTuple(args, "etk:lchflags",
1950
Py_FileSystemDefaultEncoding, &path, &flags))
1952
Py_BEGIN_ALLOW_THREADS
1953
res = lchflags(path, flags);
1954
Py_END_ALLOW_THREADS
1956
return posix_error_with_allocated_filename(path);
1961
#endif /* HAVE_LCHFLAGS */
1964
PyDoc_STRVAR(posix_chroot__doc__,
1966
Change root directory to path.");
1969
posix_chroot(PyObject *self, PyObject *args)
1971
return posix_1str(args, "et:chroot", chroot);
1976
PyDoc_STRVAR(posix_fsync__doc__,
1978
force write of file with filedescriptor to disk.");
1981
posix_fsync(PyObject *self, PyObject *fdobj)
1983
return posix_fildes(fdobj, fsync);
1985
#endif /* HAVE_FSYNC */
1987
#ifdef HAVE_FDATASYNC
1990
extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1993
PyDoc_STRVAR(posix_fdatasync__doc__,
1994
"fdatasync(fildes)\n\n\
1995
force write of file with filedescriptor to disk.\n\
1996
does not force update of metadata.");
1999
posix_fdatasync(PyObject *self, PyObject *fdobj)
2001
return posix_fildes(fdobj, fdatasync);
2003
#endif /* HAVE_FDATASYNC */
2007
PyDoc_STRVAR(posix_chown__doc__,
2008
"chown(path, uid, gid)\n\n\
2009
Change the owner and group id of path to the numeric uid and gid.");
2012
posix_chown(PyObject *self, PyObject *args)
2017
if (!PyArg_ParseTuple(args, "etll:chown",
2018
Py_FileSystemDefaultEncoding, &path,
2021
Py_BEGIN_ALLOW_THREADS
2022
res = chown(path, (uid_t) uid, (gid_t) gid);
2023
Py_END_ALLOW_THREADS
2025
return posix_error_with_allocated_filename(path);
2030
#endif /* HAVE_CHOWN */
2033
PyDoc_STRVAR(posix_fchown__doc__,
2034
"fchown(fd, uid, gid)\n\n\
2035
Change the owner and group id of the file given by file descriptor\n\
2036
fd to the numeric uid and gid.");
2039
posix_fchown(PyObject *self, PyObject *args)
2043
if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
2045
Py_BEGIN_ALLOW_THREADS
2046
res = fchown(fd, (uid_t) uid, (gid_t) gid);
2047
Py_END_ALLOW_THREADS
2049
return posix_error();
2052
#endif /* HAVE_FCHOWN */
2055
PyDoc_STRVAR(posix_lchown__doc__,
2056
"lchown(path, uid, gid)\n\n\
2057
Change the owner and group id of path to the numeric uid and gid.\n\
2058
This function will not follow symbolic links.");
2061
posix_lchown(PyObject *self, PyObject *args)
2066
if (!PyArg_ParseTuple(args, "etii:lchown",
2067
Py_FileSystemDefaultEncoding, &path,
2070
Py_BEGIN_ALLOW_THREADS
2071
res = lchown(path, (uid_t) uid, (gid_t) gid);
2072
Py_END_ALLOW_THREADS
2074
return posix_error_with_allocated_filename(path);
2079
#endif /* HAVE_LCHOWN */
2084
posix_getcwd(int use_bytes)
2089
#ifdef Py_WIN_WIDE_FILENAMES
2090
if (!use_bytes && unicode_file_names()) {
2092
wchar_t *wbuf2 = wbuf;
2095
Py_BEGIN_ALLOW_THREADS
2096
len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2097
/* If the buffer is large enough, len does not include the
2098
terminating \0. If the buffer is too small, len includes
2099
the space needed for the terminator. */
2100
if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2101
wbuf2 = malloc(len * sizeof(wchar_t));
2103
len = GetCurrentDirectoryW(len, wbuf2);
2105
Py_END_ALLOW_THREADS
2111
if (wbuf2 != wbuf) free(wbuf2);
2112
return win32_error("getcwdu", NULL);
2114
resobj = PyUnicode_FromWideChar(wbuf2, len);
2115
if (wbuf2 != wbuf) free(wbuf2);
2120
Py_BEGIN_ALLOW_THREADS
2121
#if defined(PYOS_OS2) && defined(PYCC_GCC)
2122
res = _getcwd2(buf, sizeof buf);
2124
res = getcwd(buf, sizeof buf);
2126
Py_END_ALLOW_THREADS
2128
return posix_error();
2130
return PyBytes_FromStringAndSize(buf, strlen(buf));
2131
return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2134
PyDoc_STRVAR(posix_getcwd__doc__,
2135
"getcwd() -> path\n\n\
2136
Return a unicode string representing the current working directory.");
2139
posix_getcwd_unicode(PyObject *self)
2141
return posix_getcwd(0);
2144
PyDoc_STRVAR(posix_getcwdb__doc__,
2145
"getcwdb() -> path\n\n\
2146
Return a bytes string representing the current working directory.");
2149
posix_getcwd_bytes(PyObject *self)
2151
return posix_getcwd(1);
2157
PyDoc_STRVAR(posix_link__doc__,
2158
"link(src, dst)\n\n\
2159
Create a hard link to a file.");
2162
posix_link(PyObject *self, PyObject *args)
2164
return posix_2str(args, "etet:link", link);
2166
#endif /* HAVE_LINK */
2169
PyDoc_STRVAR(posix_listdir__doc__,
2170
"listdir(path) -> list_of_strings\n\n\
2171
Return a list containing the names of the entries in the directory.\n\
2173
path: path of directory to list\n\
2175
The list is in arbitrary order. It does not include the special\n\
2176
entries '.' and '..' even if they are present in the directory.");
2179
posix_listdir(PyObject *self, PyObject *args)
2181
/* XXX Should redo this putting the (now four) versions of opendir
2182
in separate files instead of having them all here... */
2183
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
2188
WIN32_FIND_DATA FileData;
2189
char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2190
char *bufptr = namebuf;
2191
Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
2193
#ifdef Py_WIN_WIDE_FILENAMES
2194
/* If on wide-character-capable OS see if argument
2195
is Unicode and if so use wide API. */
2196
if (unicode_file_names()) {
2198
if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2199
WIN32_FIND_DATAW wFileData;
2200
Py_UNICODE *wnamebuf;
2202
/* Overallocate for \\*.*\0 */
2203
len = PyUnicode_GET_SIZE(po);
2204
wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2209
wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2210
wch = len > 0 ? wnamebuf[len-1] : '\0';
2211
if (wch != L'/' && wch != L'\\' && wch != L':')
2212
wnamebuf[len++] = L'\\';
2213
wcscpy(wnamebuf + len, L"*.*");
2214
if ((d = PyList_New(0)) == NULL) {
2218
hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2219
if (hFindFile == INVALID_HANDLE_VALUE) {
2220
int error = GetLastError();
2221
if (error == ERROR_FILE_NOT_FOUND) {
2226
win32_error_unicode("FindFirstFileW", wnamebuf);
2231
/* Skip over . and .. */
2232
if (wcscmp(wFileData.cFileName, L".") != 0 &&
2233
wcscmp(wFileData.cFileName, L"..") != 0) {
2234
v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2240
if (PyList_Append(d, v) != 0) {
2248
Py_BEGIN_ALLOW_THREADS
2249
result = FindNextFileW(hFindFile, &wFileData);
2250
Py_END_ALLOW_THREADS
2251
/* FindNextFile sets error to ERROR_NO_MORE_FILES if
2252
it got to the end of the directory. */
2253
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2255
win32_error_unicode("FindNextFileW", wnamebuf);
2256
FindClose(hFindFile);
2260
} while (result == TRUE);
2262
if (FindClose(hFindFile) == FALSE) {
2264
win32_error_unicode("FindClose", wnamebuf);
2271
/* Drop the argument parsing error as narrow strings
2277
if (!PyArg_ParseTuple(args, "et#:listdir",
2278
Py_FileSystemDefaultEncoding, &bufptr, &len))
2281
char ch = namebuf[len-1];
2282
if (ch != SEP && ch != ALTSEP && ch != ':')
2283
namebuf[len++] = '/';
2285
strcpy(namebuf + len, "*.*");
2287
if ((d = PyList_New(0)) == NULL)
2290
hFindFile = FindFirstFile(namebuf, &FileData);
2291
if (hFindFile == INVALID_HANDLE_VALUE) {
2292
int error = GetLastError();
2293
if (error == ERROR_FILE_NOT_FOUND)
2296
return win32_error("FindFirstFile", namebuf);
2299
/* Skip over . and .. */
2300
if (strcmp(FileData.cFileName, ".") != 0 &&
2301
strcmp(FileData.cFileName, "..") != 0) {
2302
v = PyBytes_FromString(FileData.cFileName);
2308
if (PyList_Append(d, v) != 0) {
2316
Py_BEGIN_ALLOW_THREADS
2317
result = FindNextFile(hFindFile, &FileData);
2318
Py_END_ALLOW_THREADS
2319
/* FindNextFile sets error to ERROR_NO_MORE_FILES if
2320
it got to the end of the directory. */
2321
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2323
win32_error("FindNextFile", namebuf);
2324
FindClose(hFindFile);
2327
} while (result == TRUE);
2329
if (FindClose(hFindFile) == FALSE) {
2331
return win32_error("FindClose", namebuf);
2336
#elif defined(PYOS_OS2)
2339
#define MAX_PATH CCHMAXPATH
2344
char namebuf[MAX_PATH+5];
2350
if (!PyArg_ParseTuple(args, "et#:listdir",
2351
Py_FileSystemDefaultEncoding, &name, &len))
2353
if (len >= MAX_PATH) {
2355
PyErr_SetString(PyExc_ValueError, "path too long");
2358
strcpy(namebuf, name);
2359
for (pt = namebuf; *pt; pt++)
2362
if (namebuf[len-1] != SEP)
2363
namebuf[len++] = SEP;
2364
strcpy(namebuf + len, "*.*");
2366
if ((d = PyList_New(0)) == NULL) {
2371
rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2372
&hdir, /* Handle to Use While Search Directory */
2373
FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2374
&ep, sizeof(ep), /* Structure to Receive Directory Entry */
2375
&srchcnt, /* Max and Actual Count of Entries Per Iteration */
2376
FIL_STANDARD); /* Format of Entry (EAs or Not) */
2378
if (rc != NO_ERROR) {
2380
return posix_error_with_allocated_filename(name);
2383
if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
2385
if (ep.achName[0] == '.'
2386
&& (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
2387
continue; /* Skip Over "." and ".." Names */
2389
strcpy(namebuf, ep.achName);
2391
/* Leave Case of Name Alone -- In Native Form */
2392
/* (Removed Forced Lowercasing Code) */
2394
v = PyBytes_FromString(namebuf);
2400
if (PyList_Append(d, v) != 0) {
2407
} while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2418
int arg_is_unicode = 1;
2421
if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2425
if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
2427
if ((dirp = opendir(name)) == NULL) {
2428
return posix_error_with_allocated_filename(name);
2430
if ((d = PyList_New(0)) == NULL) {
2437
Py_BEGIN_ALLOW_THREADS
2439
Py_END_ALLOW_THREADS
2446
return posix_error_with_allocated_filename(name);
2449
if (ep->d_name[0] == '.' &&
2451
(ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2453
v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
2459
if (arg_is_unicode) {
2462
w = PyUnicode_FromEncodedObject(v,
2463
Py_FileSystemDefaultEncoding,
2470
/* Ignore undecodable filenames, as discussed
2471
* in issue 3187. To include these,
2478
if (PyList_Append(d, v) != 0) {
2491
#endif /* which OS */
2492
} /* end of posix_listdir */
2495
/* A helper function for abspath on win32 */
2497
posix__getfullpathname(PyObject *self, PyObject *args)
2499
/* assume encoded strings won't more than double no of chars */
2500
char inbuf[MAX_PATH*2];
2501
char *inbufp = inbuf;
2502
Py_ssize_t insize = sizeof(inbuf);
2503
char outbuf[MAX_PATH*2];
2505
#ifdef Py_WIN_WIDE_FILENAMES
2506
if (unicode_file_names()) {
2507
PyUnicodeObject *po;
2508
if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2509
Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2510
Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2514
result = GetFullPathNameW(wpath,
2515
sizeof(woutbuf)/sizeof(woutbuf[0]),
2517
if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2518
woutbufp = malloc(result * sizeof(Py_UNICODE));
2520
return PyErr_NoMemory();
2521
result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2524
v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2526
v = win32_error_unicode("GetFullPathNameW", wpath);
2527
if (woutbufp != woutbuf)
2531
/* Drop the argument parsing error as narrow strings
2536
if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2537
Py_FileSystemDefaultEncoding, &inbufp,
2540
if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2542
return win32_error("GetFullPathName", inbuf);
2543
if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2544
return PyUnicode_Decode(outbuf, strlen(outbuf),
2545
Py_FileSystemDefaultEncoding, NULL);
2547
return PyBytes_FromString(outbuf);
2548
} /* end of posix__getfullpathname */
2549
#endif /* MS_WINDOWS */
2551
PyDoc_STRVAR(posix_mkdir__doc__,
2552
"mkdir(path [, mode=0777])\n\n\
2553
Create a directory.");
2556
posix_mkdir(PyObject *self, PyObject *args)
2562
#ifdef Py_WIN_WIDE_FILENAMES
2563
if (unicode_file_names()) {
2564
PyUnicodeObject *po;
2565
if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2566
Py_BEGIN_ALLOW_THREADS
2567
/* PyUnicode_AS_UNICODE OK without thread lock as
2568
it is a simple dereference. */
2569
res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2570
Py_END_ALLOW_THREADS
2572
return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2576
/* Drop the argument parsing error as narrow strings
2580
if (!PyArg_ParseTuple(args, "et|i:mkdir",
2581
Py_FileSystemDefaultEncoding, &path, &mode))
2583
Py_BEGIN_ALLOW_THREADS
2584
/* PyUnicode_AS_UNICODE OK without thread lock as
2585
it is a simple dereference. */
2586
res = CreateDirectoryA(path, NULL);
2587
Py_END_ALLOW_THREADS
2589
win32_error("mkdir", path);
2598
if (!PyArg_ParseTuple(args, "et|i:mkdir",
2599
Py_FileSystemDefaultEncoding, &path, &mode))
2601
Py_BEGIN_ALLOW_THREADS
2602
#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2605
res = mkdir(path, mode);
2607
Py_END_ALLOW_THREADS
2609
return posix_error_with_allocated_filename(path);
2617
/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2618
#if defined(HAVE_SYS_RESOURCE_H)
2619
#include <sys/resource.h>
2624
PyDoc_STRVAR(posix_nice__doc__,
2625
"nice(inc) -> new_priority\n\n\
2626
Decrease the priority of process by inc and return the new priority.");
2629
posix_nice(PyObject *self, PyObject *args)
2631
int increment, value;
2633
if (!PyArg_ParseTuple(args, "i:nice", &increment))
2636
/* There are two flavours of 'nice': one that returns the new
2637
priority (as required by almost all standards out there) and the
2638
Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2639
the use of getpriority() to get the new priority.
2641
If we are of the nice family that returns the new priority, we
2642
need to clear errno before the call, and check if errno is filled
2643
before calling posix_error() on a returnvalue of -1, because the
2644
-1 may be the actual new priority! */
2647
value = nice(increment);
2648
#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2650
value = getpriority(PRIO_PROCESS, 0);
2652
if (value == -1 && errno != 0)
2653
/* either nice() or getpriority() returned an error */
2654
return posix_error();
2655
return PyLong_FromLong((long) value);
2657
#endif /* HAVE_NICE */
2659
PyDoc_STRVAR(posix_rename__doc__,
2660
"rename(old, new)\n\n\
2661
Rename a file or directory.");
2664
posix_rename(PyObject *self, PyObject *args)
2670
if (unicode_file_names()) {
2671
if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2673
if (!convert_to_unicode(&o1))
2675
if (!convert_to_unicode(&o2)) {
2679
Py_BEGIN_ALLOW_THREADS
2680
result = MoveFileW(PyUnicode_AsUnicode(o1),
2681
PyUnicode_AsUnicode(o2));
2682
Py_END_ALLOW_THREADS
2686
return win32_error("rename", NULL);
2692
if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2694
Py_BEGIN_ALLOW_THREADS
2695
result = MoveFileA(p1, p2);
2696
Py_END_ALLOW_THREADS
2698
return win32_error("rename", NULL);
2702
return posix_2str(args, "etet:rename", rename);
2707
PyDoc_STRVAR(posix_rmdir__doc__,
2709
Remove a directory.");
2712
posix_rmdir(PyObject *self, PyObject *args)
2715
return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
2717
return posix_1str(args, "et:rmdir", rmdir);
2722
PyDoc_STRVAR(posix_stat__doc__,
2723
"stat(path) -> stat result\n\n\
2724
Perform a stat system call on the given path.");
2727
posix_stat(PyObject *self, PyObject *args)
2730
return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
2732
return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2738
PyDoc_STRVAR(posix_system__doc__,
2739
"system(command) -> exit_status\n\n\
2740
Execute the command (a string) in a subshell.");
2743
posix_system(PyObject *self, PyObject *args)
2748
if (!PyArg_ParseTuple(args, "u:system", &command))
2752
if (!PyArg_ParseTuple(args, "s:system", &command))
2755
Py_BEGIN_ALLOW_THREADS
2757
sts = _wsystem(command);
2759
sts = system(command);
2761
Py_END_ALLOW_THREADS
2762
return PyLong_FromLong(sts);
2767
PyDoc_STRVAR(posix_umask__doc__,
2768
"umask(new_mask) -> old_mask\n\n\
2769
Set the current numeric umask and return the previous umask.");
2772
posix_umask(PyObject *self, PyObject *args)
2775
if (!PyArg_ParseTuple(args, "i:umask", &i))
2779
return posix_error();
2780
return PyLong_FromLong((long)i);
2784
PyDoc_STRVAR(posix_unlink__doc__,
2786
Remove a file (same as remove(path)).");
2788
PyDoc_STRVAR(posix_remove__doc__,
2790
Remove a file (same as unlink(path)).");
2793
posix_unlink(PyObject *self, PyObject *args)
2796
return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
2798
return posix_1str(args, "et:remove", unlink);
2804
PyDoc_STRVAR(posix_uname__doc__,
2805
"uname() -> (sysname, nodename, release, version, machine)\n\n\
2806
Return a tuple identifying the current operating system.");
2809
posix_uname(PyObject *self, PyObject *noargs)
2814
Py_BEGIN_ALLOW_THREADS
2816
Py_END_ALLOW_THREADS
2818
return posix_error();
2819
return Py_BuildValue("(sssss)",
2826
#endif /* HAVE_UNAME */
2829
extract_time(PyObject *t, long* sec, long* usec)
2832
if (PyFloat_Check(t)) {
2833
double tval = PyFloat_AsDouble(t);
2834
PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2837
intval = PyLong_AsLong(intobj);
2839
if (intval == -1 && PyErr_Occurred())
2842
*usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2844
/* If rounding gave us a negative number,
2849
intval = PyLong_AsLong(t);
2850
if (intval == -1 && PyErr_Occurred())
2857
PyDoc_STRVAR(posix_utime__doc__,
2858
"utime(path, (atime, mtime))\n\
2859
utime(path, None)\n\n\
2860
Set the access and modified time of the file to the given values. If the\n\
2861
second form is used, set the access and modified times to the current time.");
2864
posix_utime(PyObject *self, PyObject *args)
2866
#ifdef Py_WIN_WIDE_FILENAMES
2868
PyUnicodeObject *obwpath;
2869
wchar_t *wpath = NULL;
2872
long atimesec, mtimesec, ausec, musec;
2873
FILETIME atime, mtime;
2874
PyObject *result = NULL;
2876
if (unicode_file_names()) {
2877
if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2878
wpath = PyUnicode_AS_UNICODE(obwpath);
2879
Py_BEGIN_ALLOW_THREADS
2880
hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2881
NULL, OPEN_EXISTING,
2882
FILE_FLAG_BACKUP_SEMANTICS, NULL);
2883
Py_END_ALLOW_THREADS
2884
if (hFile == INVALID_HANDLE_VALUE)
2885
return win32_error_unicode("utime", wpath);
2887
/* Drop the argument parsing error as narrow strings
2892
if (!PyArg_ParseTuple(args, "etO:utime",
2893
Py_FileSystemDefaultEncoding, &apath, &arg))
2895
Py_BEGIN_ALLOW_THREADS
2896
hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2897
NULL, OPEN_EXISTING,
2898
FILE_FLAG_BACKUP_SEMANTICS, NULL);
2899
Py_END_ALLOW_THREADS
2900
if (hFile == INVALID_HANDLE_VALUE) {
2901
win32_error("utime", apath);
2908
if (arg == Py_None) {
2910
GetSystemTime(&now);
2911
if (!SystemTimeToFileTime(&now, &mtime) ||
2912
!SystemTimeToFileTime(&now, &atime)) {
2913
win32_error("utime", NULL);
2917
else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2918
PyErr_SetString(PyExc_TypeError,
2919
"utime() arg 2 must be a tuple (atime, mtime)");
2923
if (extract_time(PyTuple_GET_ITEM(arg, 0),
2924
&atimesec, &ausec) == -1)
2926
time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2927
if (extract_time(PyTuple_GET_ITEM(arg, 1),
2928
&mtimesec, &musec) == -1)
2930
time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2932
if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2933
/* Avoid putting the file name into the error here,
2934
as that may confuse the user into believing that
2935
something is wrong with the file, when it also
2936
could be the time stamp that gives a problem. */
2937
win32_error("utime", NULL);
2944
#else /* Py_WIN_WIDE_FILENAMES */
2947
long atime, mtime, ausec, musec;
2951
#if defined(HAVE_UTIMES)
2952
struct timeval buf[2];
2953
#define ATIME buf[0].tv_sec
2954
#define MTIME buf[1].tv_sec
2955
#elif defined(HAVE_UTIME_H)
2956
/* XXX should define struct utimbuf instead, above */
2958
#define ATIME buf.actime
2959
#define MTIME buf.modtime
2960
#define UTIME_ARG &buf
2961
#else /* HAVE_UTIMES */
2963
#define ATIME buf[0]
2964
#define MTIME buf[1]
2965
#define UTIME_ARG buf
2966
#endif /* HAVE_UTIMES */
2969
if (!PyArg_ParseTuple(args, "etO:utime",
2970
Py_FileSystemDefaultEncoding, &path, &arg))
2972
if (arg == Py_None) {
2973
/* optional time values not given */
2974
Py_BEGIN_ALLOW_THREADS
2975
res = utime(path, NULL);
2976
Py_END_ALLOW_THREADS
2978
else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2979
PyErr_SetString(PyExc_TypeError,
2980
"utime() arg 2 must be a tuple (atime, mtime)");
2985
if (extract_time(PyTuple_GET_ITEM(arg, 0),
2986
&atime, &ausec) == -1) {
2990
if (extract_time(PyTuple_GET_ITEM(arg, 1),
2991
&mtime, &musec) == -1) {
2998
buf[0].tv_usec = ausec;
2999
buf[1].tv_usec = musec;
3000
Py_BEGIN_ALLOW_THREADS
3001
res = utimes(path, buf);
3002
Py_END_ALLOW_THREADS
3004
Py_BEGIN_ALLOW_THREADS
3005
res = utime(path, UTIME_ARG);
3006
Py_END_ALLOW_THREADS
3007
#endif /* HAVE_UTIMES */
3010
return posix_error_with_allocated_filename(path);
3018
#endif /* Py_WIN_WIDE_FILENAMES */
3022
/* Process operations */
3024
PyDoc_STRVAR(posix__exit__doc__,
3026
Exit to the system with specified status, without normal exit processing.");
3029
posix__exit(PyObject *self, PyObject *args)
3032
if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3035
return NULL; /* Make gcc -Wall happy */
3038
#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3040
free_string_array(char **array, Py_ssize_t count)
3043
for (i = 0; i < count; i++)
3044
PyMem_Free(array[i]);
3051
PyDoc_STRVAR(posix_execv__doc__,
3052
"execv(path, args)\n\n\
3053
Execute an executable path with arguments, replacing current process.\n\
3055
path: path of executable file\n\
3056
args: tuple or list of strings");
3059
posix_execv(PyObject *self, PyObject *args)
3065
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3067
/* execv has two arguments: (path, argv), where
3068
argv is a list or tuple of strings. */
3070
if (!PyArg_ParseTuple(args, "etO:execv",
3071
Py_FileSystemDefaultEncoding,
3074
if (PyList_Check(argv)) {
3075
argc = PyList_Size(argv);
3076
getitem = PyList_GetItem;
3078
else if (PyTuple_Check(argv)) {
3079
argc = PyTuple_Size(argv);
3080
getitem = PyTuple_GetItem;
3083
PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3088
PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3093
argvlist = PyMem_NEW(char *, argc+1);
3094
if (argvlist == NULL) {
3096
return PyErr_NoMemory();
3098
for (i = 0; i < argc; i++) {
3099
if (!PyArg_Parse((*getitem)(argv, i), "et",
3100
Py_FileSystemDefaultEncoding,
3102
free_string_array(argvlist, i);
3103
PyErr_SetString(PyExc_TypeError,
3104
"execv() arg 2 must contain only strings");
3110
argvlist[argc] = NULL;
3112
execv(path, argvlist);
3114
/* If we get here it's definitely an error */
3116
free_string_array(argvlist, argc);
3118
return posix_error();
3122
PyDoc_STRVAR(posix_execve__doc__,
3123
"execve(path, args, env)\n\n\
3124
Execute a path with arguments and environment, replacing current process.\n\
3126
path: path of executable file\n\
3127
args: tuple or list of arguments\n\
3128
env: dictionary of strings mapping to strings");
3131
posix_execve(PyObject *self, PyObject *args)
3134
PyObject *argv, *env;
3137
PyObject *key, *val, *keys=NULL, *vals=NULL;
3138
Py_ssize_t i, pos, argc, envc;
3139
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3140
Py_ssize_t lastarg = 0;
3142
/* execve has three arguments: (path, argv, env), where
3143
argv is a list or tuple of strings and env is a dictionary
3144
like posix.environ. */
3146
if (!PyArg_ParseTuple(args, "etOO:execve",
3147
Py_FileSystemDefaultEncoding,
3148
&path, &argv, &env))
3150
if (PyList_Check(argv)) {
3151
argc = PyList_Size(argv);
3152
getitem = PyList_GetItem;
3154
else if (PyTuple_Check(argv)) {
3155
argc = PyTuple_Size(argv);
3156
getitem = PyTuple_GetItem;
3159
PyErr_SetString(PyExc_TypeError,
3160
"execve() arg 2 must be a tuple or list");
3163
if (!PyMapping_Check(env)) {
3164
PyErr_SetString(PyExc_TypeError,
3165
"execve() arg 3 must be a mapping object");
3169
argvlist = PyMem_NEW(char *, argc+1);
3170
if (argvlist == NULL) {
3174
for (i = 0; i < argc; i++) {
3175
if (!PyArg_Parse((*getitem)(argv, i),
3176
"et;execve() arg 2 must contain only strings",
3177
Py_FileSystemDefaultEncoding,
3185
argvlist[argc] = NULL;
3187
i = PyMapping_Size(env);
3190
envlist = PyMem_NEW(char *, i + 1);
3191
if (envlist == NULL) {
3196
keys = PyMapping_Keys(env);
3197
vals = PyMapping_Values(env);
3200
if (!PyList_Check(keys) || !PyList_Check(vals)) {
3201
PyErr_SetString(PyExc_TypeError,
3202
"execve(): env.keys() or env.values() is not a list");
3206
for (pos = 0; pos < i; pos++) {
3210
key = PyList_GetItem(keys, pos);
3211
val = PyList_GetItem(vals, pos);
3217
"s;execve() arg 3 contains a non-string key",
3221
"s;execve() arg 3 contains a non-string value",
3227
#if defined(PYOS_OS2)
3228
/* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3229
if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3231
len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
3232
p = PyMem_NEW(char, len);
3237
PyOS_snprintf(p, len, "%s=%s", k, v);
3238
envlist[envc++] = p;
3239
#if defined(PYOS_OS2)
3245
execve(path, argvlist, envlist);
3247
/* If we get here it's definitely an error */
3249
(void) posix_error();
3253
PyMem_DEL(envlist[envc]);
3256
free_string_array(argvlist, lastarg);
3263
#endif /* HAVE_EXECV */
3267
PyDoc_STRVAR(posix_spawnv__doc__,
3268
"spawnv(mode, path, args)\n\n\
3269
Execute the program 'path' in a new process.\n\
3271
mode: mode of process creation\n\
3272
path: path of executable file\n\
3273
args: tuple or list of strings");
3276
posix_spawnv(PyObject *self, PyObject *args)
3283
Py_intptr_t spawnval;
3284
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3286
/* spawnv has three arguments: (mode, path, argv), where
3287
argv is a list or tuple of strings. */
3289
if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3290
Py_FileSystemDefaultEncoding,
3293
if (PyList_Check(argv)) {
3294
argc = PyList_Size(argv);
3295
getitem = PyList_GetItem;
3297
else if (PyTuple_Check(argv)) {
3298
argc = PyTuple_Size(argv);
3299
getitem = PyTuple_GetItem;
3302
PyErr_SetString(PyExc_TypeError,
3303
"spawnv() arg 2 must be a tuple or list");
3308
argvlist = PyMem_NEW(char *, argc+1);
3309
if (argvlist == NULL) {
3311
return PyErr_NoMemory();
3313
for (i = 0; i < argc; i++) {
3314
if (!PyArg_Parse((*getitem)(argv, i), "et",
3315
Py_FileSystemDefaultEncoding,
3317
free_string_array(argvlist, i);
3320
"spawnv() arg 2 must contain only strings");
3325
argvlist[argc] = NULL;
3327
#if defined(PYOS_OS2) && defined(PYCC_GCC)
3328
Py_BEGIN_ALLOW_THREADS
3329
spawnval = spawnv(mode, path, argvlist);
3330
Py_END_ALLOW_THREADS
3332
if (mode == _OLD_P_OVERLAY)
3335
Py_BEGIN_ALLOW_THREADS
3336
spawnval = _spawnv(mode, path, argvlist);
3337
Py_END_ALLOW_THREADS
3340
free_string_array(argvlist, argc);
3344
return posix_error();
3346
#if SIZEOF_LONG == SIZEOF_VOID_P
3347
return Py_BuildValue("l", (long) spawnval);
3349
return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3354
PyDoc_STRVAR(posix_spawnve__doc__,
3355
"spawnve(mode, path, args, env)\n\n\
3356
Execute the program 'path' in a new process.\n\
3358
mode: mode of process creation\n\
3359
path: path of executable file\n\
3360
args: tuple or list of arguments\n\
3361
env: dictionary of strings mapping to strings");
3364
posix_spawnve(PyObject *self, PyObject *args)
3367
PyObject *argv, *env;
3370
PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3371
int mode, pos, envc;
3373
Py_intptr_t spawnval;
3374
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3375
Py_ssize_t lastarg = 0;
3377
/* spawnve has four arguments: (mode, path, argv, env), where
3378
argv is a list or tuple of strings and env is a dictionary
3379
like posix.environ. */
3381
if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3382
Py_FileSystemDefaultEncoding,
3383
&path, &argv, &env))
3385
if (PyList_Check(argv)) {
3386
argc = PyList_Size(argv);
3387
getitem = PyList_GetItem;
3389
else if (PyTuple_Check(argv)) {
3390
argc = PyTuple_Size(argv);
3391
getitem = PyTuple_GetItem;
3394
PyErr_SetString(PyExc_TypeError,
3395
"spawnve() arg 2 must be a tuple or list");
3398
if (!PyMapping_Check(env)) {
3399
PyErr_SetString(PyExc_TypeError,
3400
"spawnve() arg 3 must be a mapping object");
3404
argvlist = PyMem_NEW(char *, argc+1);
3405
if (argvlist == NULL) {
3409
for (i = 0; i < argc; i++) {
3410
if (!PyArg_Parse((*getitem)(argv, i),
3411
"et;spawnve() arg 2 must contain only strings",
3412
Py_FileSystemDefaultEncoding,
3420
argvlist[argc] = NULL;
3422
i = PyMapping_Size(env);
3425
envlist = PyMem_NEW(char *, i + 1);
3426
if (envlist == NULL) {
3431
keys = PyMapping_Keys(env);
3432
vals = PyMapping_Values(env);
3435
if (!PyList_Check(keys) || !PyList_Check(vals)) {
3436
PyErr_SetString(PyExc_TypeError,
3437
"spawnve(): env.keys() or env.values() is not a list");
3441
for (pos = 0; pos < i; pos++) {
3445
key = PyList_GetItem(keys, pos);
3446
val = PyList_GetItem(vals, pos);
3452
"s;spawnve() arg 3 contains a non-string key",
3456
"s;spawnve() arg 3 contains a non-string value",
3461
len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
3462
p = PyMem_NEW(char, len);
3467
PyOS_snprintf(p, len, "%s=%s", k, v);
3468
envlist[envc++] = p;
3472
#if defined(PYOS_OS2) && defined(PYCC_GCC)
3473
Py_BEGIN_ALLOW_THREADS
3474
spawnval = spawnve(mode, path, argvlist, envlist);
3475
Py_END_ALLOW_THREADS
3477
if (mode == _OLD_P_OVERLAY)
3480
Py_BEGIN_ALLOW_THREADS
3481
spawnval = _spawnve(mode, path, argvlist, envlist);
3482
Py_END_ALLOW_THREADS
3486
(void) posix_error();
3488
#if SIZEOF_LONG == SIZEOF_VOID_P
3489
res = Py_BuildValue("l", (long) spawnval);
3491
res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
3496
PyMem_DEL(envlist[envc]);
3499
free_string_array(argvlist, lastarg);
3507
/* OS/2 supports spawnvp & spawnvpe natively */
3508
#if defined(PYOS_OS2)
3509
PyDoc_STRVAR(posix_spawnvp__doc__,
3510
"spawnvp(mode, file, args)\n\n\
3511
Execute the program 'file' in a new process, using the environment\n\
3512
search path to find the file.\n\
3514
mode: mode of process creation\n\
3515
file: executable file name\n\
3516
args: tuple or list of strings");
3519
posix_spawnvp(PyObject *self, PyObject *args)
3525
Py_intptr_t spawnval;
3526
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3528
/* spawnvp has three arguments: (mode, path, argv), where
3529
argv is a list or tuple of strings. */
3531
if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3532
Py_FileSystemDefaultEncoding,
3535
if (PyList_Check(argv)) {
3536
argc = PyList_Size(argv);
3537
getitem = PyList_GetItem;
3539
else if (PyTuple_Check(argv)) {
3540
argc = PyTuple_Size(argv);
3541
getitem = PyTuple_GetItem;
3544
PyErr_SetString(PyExc_TypeError,
3545
"spawnvp() arg 2 must be a tuple or list");
3550
argvlist = PyMem_NEW(char *, argc+1);
3551
if (argvlist == NULL) {
3553
return PyErr_NoMemory();
3555
for (i = 0; i < argc; i++) {
3556
if (!PyArg_Parse((*getitem)(argv, i), "et",
3557
Py_FileSystemDefaultEncoding,
3559
free_string_array(argvlist, i);
3562
"spawnvp() arg 2 must contain only strings");
3567
argvlist[argc] = NULL;
3569
Py_BEGIN_ALLOW_THREADS
3570
#if defined(PYCC_GCC)
3571
spawnval = spawnvp(mode, path, argvlist);
3573
spawnval = _spawnvp(mode, path, argvlist);
3575
Py_END_ALLOW_THREADS
3577
free_string_array(argvlist, argc);
3581
return posix_error();
3583
return Py_BuildValue("l", (long) spawnval);
3587
PyDoc_STRVAR(posix_spawnvpe__doc__,
3588
"spawnvpe(mode, file, args, env)\n\n\
3589
Execute the program 'file' in a new process, using the environment\n\
3590
search path to find the file.\n\
3592
mode: mode of process creation\n\
3593
file: executable file name\n\
3594
args: tuple or list of arguments\n\
3595
env: dictionary of strings mapping to strings");
3598
posix_spawnvpe(PyObject *self, PyObject *args)
3601
PyObject *argv, *env;
3604
PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3605
int mode, i, pos, argc, envc;
3606
Py_intptr_t spawnval;
3607
PyObject *(*getitem)(PyObject *, Py_ssize_t);
3610
/* spawnvpe has four arguments: (mode, path, argv, env), where
3611
argv is a list or tuple of strings and env is a dictionary
3612
like posix.environ. */
3614
if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3615
Py_FileSystemDefaultEncoding,
3616
&path, &argv, &env))
3618
if (PyList_Check(argv)) {
3619
argc = PyList_Size(argv);
3620
getitem = PyList_GetItem;
3622
else if (PyTuple_Check(argv)) {
3623
argc = PyTuple_Size(argv);
3624
getitem = PyTuple_GetItem;
3627
PyErr_SetString(PyExc_TypeError,
3628
"spawnvpe() arg 2 must be a tuple or list");
3631
if (!PyMapping_Check(env)) {
3632
PyErr_SetString(PyExc_TypeError,
3633
"spawnvpe() arg 3 must be a mapping object");
3637
argvlist = PyMem_NEW(char *, argc+1);
3638
if (argvlist == NULL) {
3642
for (i = 0; i < argc; i++) {
3643
if (!PyArg_Parse((*getitem)(argv, i),
3644
"et;spawnvpe() arg 2 must contain only strings",
3645
Py_FileSystemDefaultEncoding,
3653
argvlist[argc] = NULL;
3655
i = PyMapping_Size(env);
3658
envlist = PyMem_NEW(char *, i + 1);
3659
if (envlist == NULL) {
3664
keys = PyMapping_Keys(env);
3665
vals = PyMapping_Values(env);
3668
if (!PyList_Check(keys) || !PyList_Check(vals)) {
3669
PyErr_SetString(PyExc_TypeError,
3670
"spawnvpe(): env.keys() or env.values() is not a list");
3674
for (pos = 0; pos < i; pos++) {
3678
key = PyList_GetItem(keys, pos);
3679
val = PyList_GetItem(vals, pos);
3685
"s;spawnvpe() arg 3 contains a non-string key",
3689
"s;spawnvpe() arg 3 contains a non-string value",
3694
len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
3695
p = PyMem_NEW(char, len);
3700
PyOS_snprintf(p, len, "%s=%s", k, v);
3701
envlist[envc++] = p;
3705
Py_BEGIN_ALLOW_THREADS
3706
#if defined(PYCC_GCC)
3707
spawnval = spawnvpe(mode, path, argvlist, envlist);
3709
spawnval = _spawnvpe(mode, path, argvlist, envlist);
3711
Py_END_ALLOW_THREADS
3714
(void) posix_error();
3716
res = Py_BuildValue("l", (long) spawnval);
3720
PyMem_DEL(envlist[envc]);
3723
free_string_array(argvlist, lastarg);
3730
#endif /* PYOS_OS2 */
3731
#endif /* HAVE_SPAWNV */
3735
PyDoc_STRVAR(posix_fork1__doc__,
3736
"fork1() -> pid\n\n\
3737
Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3739
Return 0 to child process and PID of child to parent process.");
3742
posix_fork1(PyObject *self, PyObject *noargs)
3744
pid_t pid = fork1();
3746
return posix_error();
3749
return PyLong_FromLong(pid);
3755
PyDoc_STRVAR(posix_fork__doc__,
3757
Fork a child process.\n\
3758
Return 0 to child process and PID of child to parent process.");
3761
posix_fork(PyObject *self, PyObject *noargs)
3765
return posix_error();
3768
return PyLong_FromLong(pid);
3772
/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3773
/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3774
#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3775
#define DEV_PTY_FILE "/dev/ptc"
3776
#define HAVE_DEV_PTMX
3778
#define DEV_PTY_FILE "/dev/ptmx"
3781
#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3785
#ifdef HAVE_LIBUTIL_H
3786
#include <libutil.h>
3787
#endif /* HAVE_LIBUTIL_H */
3788
#endif /* HAVE_PTY_H */
3789
#ifdef HAVE_STROPTS_H
3790
#include <stropts.h>
3792
#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3794
#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3795
PyDoc_STRVAR(posix_openpty__doc__,
3796
"openpty() -> (master_fd, slave_fd)\n\n\
3797
Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3800
posix_openpty(PyObject *self, PyObject *noargs)
3802
int master_fd, slave_fd;
3803
#ifndef HAVE_OPENPTY
3806
#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3807
PyOS_sighandler_t sig_saved;
3809
extern char *ptsname(int fildes);
3814
if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3815
return posix_error();
3816
#elif defined(HAVE__GETPTY)
3817
slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3818
if (slave_name == NULL)
3819
return posix_error();
3821
slave_fd = open(slave_name, O_RDWR);
3823
return posix_error();
3825
master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3827
return posix_error();
3828
sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3829
/* change permission of slave */
3830
if (grantpt(master_fd) < 0) {
3831
PyOS_setsig(SIGCHLD, sig_saved);
3832
return posix_error();
3835
if (unlockpt(master_fd) < 0) {
3836
PyOS_setsig(SIGCHLD, sig_saved);
3837
return posix_error();
3839
PyOS_setsig(SIGCHLD, sig_saved);
3840
slave_name = ptsname(master_fd); /* get name of slave */
3841
if (slave_name == NULL)
3842
return posix_error();
3843
slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3845
return posix_error();
3846
#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3847
ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3848
ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
3850
ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
3852
#endif /* HAVE_CYGWIN */
3853
#endif /* HAVE_OPENPTY */
3855
return Py_BuildValue("(ii)", master_fd, slave_fd);
3858
#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3861
PyDoc_STRVAR(posix_forkpty__doc__,
3862
"forkpty() -> (pid, master_fd)\n\n\
3863
Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3864
Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3865
To both, return fd of newly opened pseudo-terminal.\n");
3868
posix_forkpty(PyObject *self, PyObject *noargs)
3873
pid = forkpty(&master_fd, NULL, NULL, NULL);
3875
return posix_error();
3878
return Py_BuildValue("(li)", pid, master_fd);
3883
PyDoc_STRVAR(posix_getegid__doc__,
3884
"getegid() -> egid\n\n\
3885
Return the current process's effective group id.");
3888
posix_getegid(PyObject *self, PyObject *noargs)
3890
return PyLong_FromLong((long)getegid());
3896
PyDoc_STRVAR(posix_geteuid__doc__,
3897
"geteuid() -> euid\n\n\
3898
Return the current process's effective user id.");
3901
posix_geteuid(PyObject *self, PyObject *noargs)
3903
return PyLong_FromLong((long)geteuid());
3909
PyDoc_STRVAR(posix_getgid__doc__,
3910
"getgid() -> gid\n\n\
3911
Return the current process's group id.");
3914
posix_getgid(PyObject *self, PyObject *noargs)
3916
return PyLong_FromLong((long)getgid());
3921
PyDoc_STRVAR(posix_getpid__doc__,
3922
"getpid() -> pid\n\n\
3923
Return the current process id");
3926
posix_getpid(PyObject *self, PyObject *noargs)
3928
return PyLong_FromLong((long)getpid());
3932
#ifdef HAVE_GETGROUPS
3933
PyDoc_STRVAR(posix_getgroups__doc__,
3934
"getgroups() -> list of group IDs\n\n\
3935
Return list of supplemental group IDs for the process.");
3938
posix_getgroups(PyObject *self, PyObject *noargs)
3940
PyObject *result = NULL;
3943
#define MAX_GROUPS NGROUPS_MAX
3945
/* defined to be 16 on Solaris7, so this should be a small number */
3946
#define MAX_GROUPS 64
3948
gid_t grouplist[MAX_GROUPS];
3951
n = getgroups(MAX_GROUPS, grouplist);
3955
result = PyList_New(n);
3956
if (result != NULL) {
3958
for (i = 0; i < n; ++i) {
3959
PyObject *o = PyLong_FromLong((long)grouplist[i]);
3965
PyList_SET_ITEM(result, i, o);
3975
PyDoc_STRVAR(posix_getpgid__doc__,
3976
"getpgid(pid) -> pgid\n\n\
3977
Call the system call getpgid().");
3980
posix_getpgid(PyObject *self, PyObject *args)
3983
if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3985
pgid = getpgid(pid);
3987
return posix_error();
3988
return PyLong_FromLong((long)pgid);
3990
#endif /* HAVE_GETPGID */
3994
PyDoc_STRVAR(posix_getpgrp__doc__,
3995
"getpgrp() -> pgrp\n\n\
3996
Return the current process group id.");
3999
posix_getpgrp(PyObject *self, PyObject *noargs)
4001
#ifdef GETPGRP_HAVE_ARG
4002
return PyLong_FromLong((long)getpgrp(0));
4003
#else /* GETPGRP_HAVE_ARG */
4004
return PyLong_FromLong((long)getpgrp());
4005
#endif /* GETPGRP_HAVE_ARG */
4007
#endif /* HAVE_GETPGRP */
4011
PyDoc_STRVAR(posix_setpgrp__doc__,
4013
Make this process a session leader.");
4016
posix_setpgrp(PyObject *self, PyObject *noargs)
4018
#ifdef SETPGRP_HAVE_ARG
4019
if (setpgrp(0, 0) < 0)
4020
#else /* SETPGRP_HAVE_ARG */
4022
#endif /* SETPGRP_HAVE_ARG */
4023
return posix_error();
4028
#endif /* HAVE_SETPGRP */
4031
PyDoc_STRVAR(posix_getppid__doc__,
4032
"getppid() -> ppid\n\n\
4033
Return the parent's process id.");
4036
posix_getppid(PyObject *self, PyObject *noargs)
4038
return PyLong_FromLong((long)getppid());
4043
#ifdef HAVE_GETLOGIN
4044
PyDoc_STRVAR(posix_getlogin__doc__,
4045
"getlogin() -> string\n\n\
4046
Return the actual login name.");
4049
posix_getlogin(PyObject *self, PyObject *noargs)
4051
PyObject *result = NULL;
4053
int old_errno = errno;
4061
PyErr_SetString(PyExc_OSError,
4062
"unable to determine login name");
4065
result = PyUnicode_FromString(name);
4073
PyDoc_STRVAR(posix_getuid__doc__,
4074
"getuid() -> uid\n\n\
4075
Return the current process's user id.");
4078
posix_getuid(PyObject *self, PyObject *noargs)
4080
return PyLong_FromLong((long)getuid());
4086
PyDoc_STRVAR(posix_kill__doc__,
4087
"kill(pid, sig)\n\n\
4088
Kill a process with a signal.");
4091
posix_kill(PyObject *self, PyObject *args)
4095
if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
4097
#if defined(PYOS_OS2) && !defined(PYCC_GCC)
4098
if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4100
if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
4101
return os2_error(rc);
4103
} else if (sig == XCPT_SIGNAL_KILLPROC) {
4105
if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
4106
return os2_error(rc);
4109
return NULL; /* Unrecognized Signal Requested */
4111
if (kill(pid, sig) == -1)
4112
return posix_error();
4120
PyDoc_STRVAR(posix_killpg__doc__,
4121
"killpg(pgid, sig)\n\n\
4122
Kill a process group with a signal.");
4125
posix_killpg(PyObject *self, PyObject *args)
4128
if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4130
if (killpg(pgid, sig) == -1)
4131
return posix_error();
4139
#ifdef HAVE_SYS_LOCK_H
4140
#include <sys/lock.h>
4143
PyDoc_STRVAR(posix_plock__doc__,
4145
Lock program segments into memory.");
4148
posix_plock(PyObject *self, PyObject *args)
4151
if (!PyArg_ParseTuple(args, "i:plock", &op))
4153
if (plock(op) == -1)
4154
return posix_error();
4164
PyDoc_STRVAR(posix_setuid__doc__,
4166
Set the current process's user id.");
4169
posix_setuid(PyObject *self, PyObject *args)
4172
if (!PyArg_ParseTuple(args, "i:setuid", &uid))
4174
if (setuid(uid) < 0)
4175
return posix_error();
4179
#endif /* HAVE_SETUID */
4183
PyDoc_STRVAR(posix_seteuid__doc__,
4185
Set the current process's effective user id.");
4188
posix_seteuid (PyObject *self, PyObject *args)
4191
if (!PyArg_ParseTuple(args, "i", &euid)) {
4193
} else if (seteuid(euid) < 0) {
4194
return posix_error();
4200
#endif /* HAVE_SETEUID */
4203
PyDoc_STRVAR(posix_setegid__doc__,
4205
Set the current process's effective group id.");
4208
posix_setegid (PyObject *self, PyObject *args)
4211
if (!PyArg_ParseTuple(args, "i", &egid)) {
4213
} else if (setegid(egid) < 0) {
4214
return posix_error();
4220
#endif /* HAVE_SETEGID */
4222
#ifdef HAVE_SETREUID
4223
PyDoc_STRVAR(posix_setreuid__doc__,
4224
"setreuid(ruid, euid)\n\n\
4225
Set the current process's real and effective user ids.");
4228
posix_setreuid (PyObject *self, PyObject *args)
4231
if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4233
} else if (setreuid(ruid, euid) < 0) {
4234
return posix_error();
4240
#endif /* HAVE_SETREUID */
4242
#ifdef HAVE_SETREGID
4243
PyDoc_STRVAR(posix_setregid__doc__,
4244
"setregid(rgid, egid)\n\n\
4245
Set the current process's real and effective group ids.");
4248
posix_setregid (PyObject *self, PyObject *args)
4251
if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4253
} else if (setregid(rgid, egid) < 0) {
4254
return posix_error();
4260
#endif /* HAVE_SETREGID */
4263
PyDoc_STRVAR(posix_setgid__doc__,
4265
Set the current process's group id.");
4268
posix_setgid(PyObject *self, PyObject *args)
4271
if (!PyArg_ParseTuple(args, "i:setgid", &gid))
4273
if (setgid(gid) < 0)
4274
return posix_error();
4278
#endif /* HAVE_SETGID */
4280
#ifdef HAVE_SETGROUPS
4281
PyDoc_STRVAR(posix_setgroups__doc__,
4282
"setgroups(list)\n\n\
4283
Set the groups of the current process to list.");
4286
posix_setgroups(PyObject *self, PyObject *groups)
4289
gid_t grouplist[MAX_GROUPS];
4291
if (!PySequence_Check(groups)) {
4292
PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4295
len = PySequence_Size(groups);
4296
if (len > MAX_GROUPS) {
4297
PyErr_SetString(PyExc_ValueError, "too many groups");
4300
for(i = 0; i < len; i++) {
4302
elem = PySequence_GetItem(groups, i);
4305
if (!PyLong_Check(elem)) {
4306
PyErr_SetString(PyExc_TypeError,
4307
"groups must be integers");
4311
unsigned long x = PyLong_AsUnsignedLong(elem);
4312
if (PyErr_Occurred()) {
4313
PyErr_SetString(PyExc_TypeError,
4314
"group id too big");
4319
/* read back the value to see if it fitted in gid_t */
4320
if (grouplist[i] != x) {
4321
PyErr_SetString(PyExc_TypeError,
4322
"group id too big");
4330
if (setgroups(len, grouplist) < 0)
4331
return posix_error();
4335
#endif /* HAVE_SETGROUPS */
4337
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4339
wait_helper(pid_t pid, int status, struct rusage *ru)
4342
static PyObject *struct_rusage;
4345
return posix_error();
4347
if (struct_rusage == NULL) {
4348
PyObject *m = PyImport_ImportModuleNoBlock("resource");
4351
struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4353
if (struct_rusage == NULL)
4357
/* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4358
result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4363
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4366
PyStructSequence_SET_ITEM(result, 0,
4367
PyFloat_FromDouble(doubletime(ru->ru_utime)));
4368
PyStructSequence_SET_ITEM(result, 1,
4369
PyFloat_FromDouble(doubletime(ru->ru_stime)));
4370
#define SET_INT(result, index, value)\
4371
PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4372
SET_INT(result, 2, ru->ru_maxrss);
4373
SET_INT(result, 3, ru->ru_ixrss);
4374
SET_INT(result, 4, ru->ru_idrss);
4375
SET_INT(result, 5, ru->ru_isrss);
4376
SET_INT(result, 6, ru->ru_minflt);
4377
SET_INT(result, 7, ru->ru_majflt);
4378
SET_INT(result, 8, ru->ru_nswap);
4379
SET_INT(result, 9, ru->ru_inblock);
4380
SET_INT(result, 10, ru->ru_oublock);
4381
SET_INT(result, 11, ru->ru_msgsnd);
4382
SET_INT(result, 12, ru->ru_msgrcv);
4383
SET_INT(result, 13, ru->ru_nsignals);
4384
SET_INT(result, 14, ru->ru_nvcsw);
4385
SET_INT(result, 15, ru->ru_nivcsw);
4388
if (PyErr_Occurred()) {
4393
return Py_BuildValue("iiN", pid, status, result);
4395
#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4398
PyDoc_STRVAR(posix_wait3__doc__,
4399
"wait3(options) -> (pid, status, rusage)\n\n\
4400
Wait for completion of a child process.");
4403
posix_wait3(PyObject *self, PyObject *args)
4409
WAIT_STATUS_INT(status) = 0;
4411
if (!PyArg_ParseTuple(args, "i:wait3", &options))
4414
Py_BEGIN_ALLOW_THREADS
4415
pid = wait3(&status, options, &ru);
4416
Py_END_ALLOW_THREADS
4418
return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4420
#endif /* HAVE_WAIT3 */
4423
PyDoc_STRVAR(posix_wait4__doc__,
4424
"wait4(pid, options) -> (pid, status, rusage)\n\n\
4425
Wait for completion of a given child process.");
4428
posix_wait4(PyObject *self, PyObject *args)
4434
WAIT_STATUS_INT(status) = 0;
4436
if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4439
Py_BEGIN_ALLOW_THREADS
4440
pid = wait4(pid, &status, options, &ru);
4441
Py_END_ALLOW_THREADS
4443
return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4445
#endif /* HAVE_WAIT4 */
4448
PyDoc_STRVAR(posix_waitpid__doc__,
4449
"waitpid(pid, options) -> (pid, status)\n\n\
4450
Wait for completion of a given child process.");
4453
posix_waitpid(PyObject *self, PyObject *args)
4458
WAIT_STATUS_INT(status) = 0;
4460
if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4462
Py_BEGIN_ALLOW_THREADS
4463
pid = waitpid(pid, &status, options);
4464
Py_END_ALLOW_THREADS
4466
return posix_error();
4468
return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
4471
#elif defined(HAVE_CWAIT)
4473
/* MS C has a variant of waitpid() that's usable for most purposes. */
4474
PyDoc_STRVAR(posix_waitpid__doc__,
4475
"waitpid(pid, options) -> (pid, status << 8)\n\n"
4476
"Wait for completion of a given process. options is ignored on Windows.");
4479
posix_waitpid(PyObject *self, PyObject *args)
4482
int status, options;
4484
if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4486
Py_BEGIN_ALLOW_THREADS
4487
pid = _cwait(&status, pid, options);
4488
Py_END_ALLOW_THREADS
4490
return posix_error();
4492
/* shift the status left a byte so this is more like the POSIX waitpid */
4493
return Py_BuildValue("ii", pid, status << 8);
4495
#endif /* HAVE_WAITPID || HAVE_CWAIT */
4498
PyDoc_STRVAR(posix_wait__doc__,
4499
"wait() -> (pid, status)\n\n\
4500
Wait for completion of a child process.");
4503
posix_wait(PyObject *self, PyObject *noargs)
4507
WAIT_STATUS_INT(status) = 0;
4509
Py_BEGIN_ALLOW_THREADS
4510
pid = wait(&status);
4511
Py_END_ALLOW_THREADS
4513
return posix_error();
4515
return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
4520
PyDoc_STRVAR(posix_lstat__doc__,
4521
"lstat(path) -> stat result\n\n\
4522
Like stat(path), but do not follow symbolic links.");
4525
posix_lstat(PyObject *self, PyObject *args)
4528
return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
4529
#else /* !HAVE_LSTAT */
4531
return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
4533
return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4535
#endif /* !HAVE_LSTAT */
4539
#ifdef HAVE_READLINK
4540
PyDoc_STRVAR(posix_readlink__doc__,
4541
"readlink(path) -> path\n\n\
4542
Return a string representing the path to which the symbolic link points.");
4545
posix_readlink(PyObject *self, PyObject *args)
4548
char buf[MAXPATHLEN];
4551
int arg_is_unicode = 0;
4553
if (!PyArg_ParseTuple(args, "et:readlink",
4554
Py_FileSystemDefaultEncoding, &path))
4556
v = PySequence_GetItem(args, 0);
4562
if (PyUnicode_Check(v)) {
4567
Py_BEGIN_ALLOW_THREADS
4568
n = readlink(path, buf, (int) sizeof buf);
4569
Py_END_ALLOW_THREADS
4571
return posix_error_with_allocated_filename(path);
4574
v = PyBytes_FromStringAndSize(buf, n);
4575
if (arg_is_unicode) {
4578
w = PyUnicode_FromEncodedObject(v,
4579
Py_FileSystemDefaultEncoding,
4591
#endif /* HAVE_READLINK */
4595
PyDoc_STRVAR(posix_symlink__doc__,
4596
"symlink(src, dst)\n\n\
4597
Create a symbolic link pointing to src named dst.");
4600
posix_symlink(PyObject *self, PyObject *args)
4602
return posix_2str(args, "etet:symlink", symlink);
4604
#endif /* HAVE_SYMLINK */
4608
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4614
Py_BEGIN_ALLOW_THREADS
4615
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4616
Py_END_ALLOW_THREADS
4622
posix_times(PyObject *self, PyObject *noargs)
4624
/* Currently Only Uptime is Provided -- Others Later */
4625
return Py_BuildValue("ddddd",
4626
(double)0 /* t.tms_utime / HZ */,
4627
(double)0 /* t.tms_stime / HZ */,
4628
(double)0 /* t.tms_cutime / HZ */,
4629
(double)0 /* t.tms_cstime / HZ */,
4630
(double)system_uptime() / 1000);
4633
#define NEED_TICKS_PER_SECOND
4634
static long ticks_per_second = -1;
4636
posix_times(PyObject *self, PyObject *noargs)
4642
if (c == (clock_t) -1)
4643
return posix_error();
4644
return Py_BuildValue("ddddd",
4645
(double)t.tms_utime / ticks_per_second,
4646
(double)t.tms_stime / ticks_per_second,
4647
(double)t.tms_cutime / ticks_per_second,
4648
(double)t.tms_cstime / ticks_per_second,
4649
(double)c / ticks_per_second);
4651
#endif /* not OS2 */
4652
#endif /* HAVE_TIMES */
4656
#define HAVE_TIMES /* so the method table will pick it up */
4658
posix_times(PyObject *self, PyObject *noargs)
4660
FILETIME create, exit, kernel, user;
4662
hProc = GetCurrentProcess();
4663
GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4664
/* The fields of a FILETIME structure are the hi and lo part
4665
of a 64-bit value expressed in 100 nanosecond units.
4666
1e7 is one second in such units; 1e-7 the inverse.
4667
429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4669
return Py_BuildValue(
4671
(double)(user.dwHighDateTime*429.4967296 +
4672
user.dwLowDateTime*1e-7),
4673
(double)(kernel.dwHighDateTime*429.4967296 +
4674
kernel.dwLowDateTime*1e-7),
4679
#endif /* MS_WINDOWS */
4682
PyDoc_STRVAR(posix_times__doc__,
4683
"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
4684
Return a tuple of floating point numbers indicating process times.");
4689
PyDoc_STRVAR(posix_getsid__doc__,
4690
"getsid(pid) -> sid\n\n\
4691
Call the system call getsid().");
4694
posix_getsid(PyObject *self, PyObject *args)
4698
if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4702
return posix_error();
4703
return PyLong_FromLong((long)sid);
4705
#endif /* HAVE_GETSID */
4709
PyDoc_STRVAR(posix_setsid__doc__,
4711
Call the system call setsid().");
4714
posix_setsid(PyObject *self, PyObject *noargs)
4717
return posix_error();
4721
#endif /* HAVE_SETSID */
4724
PyDoc_STRVAR(posix_setpgid__doc__,
4725
"setpgid(pid, pgrp)\n\n\
4726
Call the system call setpgid().");
4729
posix_setpgid(PyObject *self, PyObject *args)
4733
if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
4735
if (setpgid(pid, pgrp) < 0)
4736
return posix_error();
4740
#endif /* HAVE_SETPGID */
4743
#ifdef HAVE_TCGETPGRP
4744
PyDoc_STRVAR(posix_tcgetpgrp__doc__,
4745
"tcgetpgrp(fd) -> pgid\n\n\
4746
Return the process group associated with the terminal given by a fd.");
4749
posix_tcgetpgrp(PyObject *self, PyObject *args)
4753
if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4755
pgid = tcgetpgrp(fd);
4757
return posix_error();
4758
return PyLong_FromLong((long)pgid);
4760
#endif /* HAVE_TCGETPGRP */
4763
#ifdef HAVE_TCSETPGRP
4764
PyDoc_STRVAR(posix_tcsetpgrp__doc__,
4765
"tcsetpgrp(fd, pgid)\n\n\
4766
Set the process group associated with the terminal given by a fd.");
4769
posix_tcsetpgrp(PyObject *self, PyObject *args)
4772
if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
4774
if (tcsetpgrp(fd, pgid) < 0)
4775
return posix_error();
4779
#endif /* HAVE_TCSETPGRP */
4781
/* Functions acting on file descriptors */
4783
PyDoc_STRVAR(posix_open__doc__,
4784
"open(filename, flag [, mode=0777]) -> fd\n\n\
4785
Open a file (for low level IO).");
4788
posix_open(PyObject *self, PyObject *args)
4796
if (unicode_file_names()) {
4797
PyUnicodeObject *po;
4798
if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4799
Py_BEGIN_ALLOW_THREADS
4800
/* PyUnicode_AS_UNICODE OK without thread
4801
lock as it is a simple dereference. */
4802
fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4803
Py_END_ALLOW_THREADS
4805
return posix_error();
4806
return PyLong_FromLong((long)fd);
4808
/* Drop the argument parsing error as narrow strings
4814
if (!PyArg_ParseTuple(args, "eti|i",
4815
Py_FileSystemDefaultEncoding, &file,
4819
Py_BEGIN_ALLOW_THREADS
4820
fd = open(file, flag, mode);
4821
Py_END_ALLOW_THREADS
4823
return posix_error_with_allocated_filename(file);
4825
return PyLong_FromLong((long)fd);
4829
PyDoc_STRVAR(posix_close__doc__,
4831
Close a file descriptor (for low level IO).");
4834
posix_close(PyObject *self, PyObject *args)
4837
if (!PyArg_ParseTuple(args, "i:close", &fd))
4839
if (!_PyVerify_fd(fd))
4840
return posix_error();
4841
Py_BEGIN_ALLOW_THREADS
4843
Py_END_ALLOW_THREADS
4845
return posix_error();
4851
PyDoc_STRVAR(posix_closerange__doc__,
4852
"closerange(fd_low, fd_high)\n\n\
4853
Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4856
posix_closerange(PyObject *self, PyObject *args)
4858
int fd_from, fd_to, i;
4859
if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4861
Py_BEGIN_ALLOW_THREADS
4862
for (i = fd_from; i < fd_to; i++)
4863
if (_PyVerify_fd(i))
4865
Py_END_ALLOW_THREADS
4870
PyDoc_STRVAR(posix_dup__doc__,
4871
"dup(fd) -> fd2\n\n\
4872
Return a duplicate of a file descriptor.");
4875
posix_dup(PyObject *self, PyObject *args)
4878
if (!PyArg_ParseTuple(args, "i:dup", &fd))
4880
if (!_PyVerify_fd(fd))
4881
return posix_error();
4882
Py_BEGIN_ALLOW_THREADS
4884
Py_END_ALLOW_THREADS
4886
return posix_error();
4887
return PyLong_FromLong((long)fd);
4891
PyDoc_STRVAR(posix_dup2__doc__,
4892
"dup2(old_fd, new_fd)\n\n\
4893
Duplicate file descriptor.");
4896
posix_dup2(PyObject *self, PyObject *args)
4899
if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
4901
if (!_PyVerify_fd_dup2(fd, fd2))
4902
return posix_error();
4903
Py_BEGIN_ALLOW_THREADS
4904
res = dup2(fd, fd2);
4905
Py_END_ALLOW_THREADS
4907
return posix_error();
4913
PyDoc_STRVAR(posix_lseek__doc__,
4914
"lseek(fd, pos, how) -> newpos\n\n\
4915
Set the current position of a file descriptor.");
4918
posix_lseek(PyObject *self, PyObject *args)
4921
#if defined(MS_WIN64) || defined(MS_WINDOWS)
4922
PY_LONG_LONG pos, res;
4927
if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
4930
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4932
case 0: how = SEEK_SET; break;
4933
case 1: how = SEEK_CUR; break;
4934
case 2: how = SEEK_END; break;
4936
#endif /* SEEK_END */
4938
#if !defined(HAVE_LARGEFILE_SUPPORT)
4939
pos = PyLong_AsLong(posobj);
4941
pos = PyLong_Check(posobj) ?
4942
PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
4944
if (PyErr_Occurred())
4947
if (!_PyVerify_fd(fd))
4948
return posix_error();
4949
Py_BEGIN_ALLOW_THREADS
4950
#if defined(MS_WIN64) || defined(MS_WINDOWS)
4951
res = _lseeki64(fd, pos, how);
4953
res = lseek(fd, pos, how);
4955
Py_END_ALLOW_THREADS
4957
return posix_error();
4959
#if !defined(HAVE_LARGEFILE_SUPPORT)
4960
return PyLong_FromLong(res);
4962
return PyLong_FromLongLong(res);
4967
PyDoc_STRVAR(posix_read__doc__,
4968
"read(fd, buffersize) -> string\n\n\
4969
Read a file descriptor.");
4972
posix_read(PyObject *self, PyObject *args)
4977
if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
4981
return posix_error();
4983
buffer = PyBytes_FromStringAndSize((char *)NULL, size);
4986
if (!_PyVerify_fd(fd))
4987
return posix_error();
4988
Py_BEGIN_ALLOW_THREADS
4989
n = read(fd, PyBytes_AS_STRING(buffer), size);
4990
Py_END_ALLOW_THREADS
4993
return posix_error();
4996
_PyBytes_Resize(&buffer, n);
5001
PyDoc_STRVAR(posix_write__doc__,
5002
"write(fd, string) -> byteswritten\n\n\
5003
Write a string to a file descriptor.");
5006
posix_write(PyObject *self, PyObject *args)
5012
if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5014
if (!_PyVerify_fd(fd))
5015
return posix_error();
5016
Py_BEGIN_ALLOW_THREADS
5017
size = write(fd, pbuf.buf, (size_t)pbuf.len);
5018
Py_END_ALLOW_THREADS
5019
PyBuffer_Release(&pbuf);
5021
return posix_error();
5022
return PyLong_FromSsize_t(size);
5026
PyDoc_STRVAR(posix_fstat__doc__,
5027
"fstat(fd) -> stat result\n\n\
5028
Like stat(), but for an open file descriptor.");
5031
posix_fstat(PyObject *self, PyObject *args)
5036
if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5039
/* on OpenVMS we must ensure that all bytes are written to the file */
5042
if (!_PyVerify_fd(fd))
5043
return posix_error();
5044
Py_BEGIN_ALLOW_THREADS
5045
res = FSTAT(fd, &st);
5046
Py_END_ALLOW_THREADS
5049
return win32_error("fstat", NULL);
5051
return posix_error();
5055
return _pystat_fromstructstat(&st);
5058
PyDoc_STRVAR(posix_isatty__doc__,
5059
"isatty(fd) -> bool\n\n\
5060
Return True if the file descriptor 'fd' is an open file descriptor\n\
5061
connected to the slave end of a terminal.");
5064
posix_isatty(PyObject *self, PyObject *args)
5067
if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5069
if (!_PyVerify_fd(fd))
5070
return PyBool_FromLong(0);
5071
return PyBool_FromLong(isatty(fd));
5075
PyDoc_STRVAR(posix_pipe__doc__,
5076
"pipe() -> (read_end, write_end)\n\n\
5080
posix_pipe(PyObject *self, PyObject *noargs)
5082
#if defined(PYOS_OS2)
5086
Py_BEGIN_ALLOW_THREADS
5087
rc = DosCreatePipe( &read, &write, 4096);
5088
Py_END_ALLOW_THREADS
5090
return os2_error(rc);
5092
return Py_BuildValue("(ii)", read, write);
5094
#if !defined(MS_WINDOWS)
5097
Py_BEGIN_ALLOW_THREADS
5099
Py_END_ALLOW_THREADS
5101
return posix_error();
5102
return Py_BuildValue("(ii)", fds[0], fds[1]);
5103
#else /* MS_WINDOWS */
5105
int read_fd, write_fd;
5107
Py_BEGIN_ALLOW_THREADS
5108
ok = CreatePipe(&read, &write, NULL, 0);
5109
Py_END_ALLOW_THREADS
5111
return win32_error("CreatePipe", NULL);
5112
read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5113
write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5114
return Py_BuildValue("(ii)", read_fd, write_fd);
5115
#endif /* MS_WINDOWS */
5118
#endif /* HAVE_PIPE */
5122
PyDoc_STRVAR(posix_mkfifo__doc__,
5123
"mkfifo(filename [, mode=0666])\n\n\
5124
Create a FIFO (a POSIX named pipe).");
5127
posix_mkfifo(PyObject *self, PyObject *args)
5132
if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5134
Py_BEGIN_ALLOW_THREADS
5135
res = mkfifo(filename, mode);
5136
Py_END_ALLOW_THREADS
5138
return posix_error();
5145
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
5146
PyDoc_STRVAR(posix_mknod__doc__,
5147
"mknod(filename [, mode=0600, device])\n\n\
5148
Create a filesystem node (file, device special file or named pipe)\n\
5149
named filename. mode specifies both the permissions to use and the\n\
5150
type of node to be created, being combined (bitwise OR) with one of\n\
5151
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5152
device defines the newly created device special file (probably using\n\
5153
os.makedev()), otherwise it is ignored.");
5157
posix_mknod(PyObject *self, PyObject *args)
5163
if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5165
Py_BEGIN_ALLOW_THREADS
5166
res = mknod(filename, mode, device);
5167
Py_END_ALLOW_THREADS
5169
return posix_error();
5175
#ifdef HAVE_DEVICE_MACROS
5176
PyDoc_STRVAR(posix_major__doc__,
5177
"major(device) -> major number\n\
5178
Extracts a device major number from a raw device number.");
5181
posix_major(PyObject *self, PyObject *args)
5184
if (!PyArg_ParseTuple(args, "i:major", &device))
5186
return PyLong_FromLong((long)major(device));
5189
PyDoc_STRVAR(posix_minor__doc__,
5190
"minor(device) -> minor number\n\
5191
Extracts a device minor number from a raw device number.");
5194
posix_minor(PyObject *self, PyObject *args)
5197
if (!PyArg_ParseTuple(args, "i:minor", &device))
5199
return PyLong_FromLong((long)minor(device));
5202
PyDoc_STRVAR(posix_makedev__doc__,
5203
"makedev(major, minor) -> device number\n\
5204
Composes a raw device number from the major and minor device numbers.");
5207
posix_makedev(PyObject *self, PyObject *args)
5210
if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5212
return PyLong_FromLong((long)makedev(major, minor));
5214
#endif /* device macros */
5217
#ifdef HAVE_FTRUNCATE
5218
PyDoc_STRVAR(posix_ftruncate__doc__,
5219
"ftruncate(fd, length)\n\n\
5220
Truncate a file to a specified length.");
5223
posix_ftruncate(PyObject *self, PyObject *args)
5230
if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5233
#if !defined(HAVE_LARGEFILE_SUPPORT)
5234
length = PyLong_AsLong(lenobj);
5236
length = PyLong_Check(lenobj) ?
5237
PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
5239
if (PyErr_Occurred())
5242
Py_BEGIN_ALLOW_THREADS
5243
res = ftruncate(fd, length);
5244
Py_END_ALLOW_THREADS
5246
return posix_error();
5253
PyDoc_STRVAR(posix_putenv__doc__,
5254
"putenv(key, value)\n\n\
5255
Change or add an environment variable.");
5257
/* Save putenv() parameters as values here, so we can collect them when they
5258
* get re-set with another call for the same key. */
5259
static PyObject *posix_putenv_garbage;
5262
posix_putenv(PyObject *self, PyObject *args)
5274
if (!PyArg_ParseTuple(args,
5283
#if defined(PYOS_OS2)
5284
if (stricmp(s1, "BEGINLIBPATH") == 0) {
5287
rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5289
return os2_error(rc);
5291
} else if (stricmp(s1, "ENDLIBPATH") == 0) {
5294
rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5296
return os2_error(rc);
5299
/* XXX This can leak memory -- not easy to fix :-( */
5300
/* len includes space for a trailing \0; the size arg to
5301
PyBytes_FromStringAndSize does not count that */
5303
len = wcslen(s1) + wcslen(s2) + 2;
5304
newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5306
len = strlen(s1) + strlen(s2) + 2;
5307
newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
5310
return PyErr_NoMemory();
5312
newenv = PyUnicode_AsUnicode(newstr);
5313
_snwprintf(newenv, len, L"%s=%s", s1, s2);
5314
if (_wputenv(newenv)) {
5320
newenv = PyBytes_AS_STRING(newstr);
5321
PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5322
if (putenv(newenv)) {
5328
/* Install the first arg and newstr in posix_putenv_garbage;
5329
* this will cause previous value to be collected. This has to
5330
* happen after the real putenv() call because the old value
5331
* was still accessible until then. */
5332
if (PyDict_SetItem(posix_putenv_garbage,
5333
PyTuple_GET_ITEM(args, 0), newstr)) {
5334
/* really not much we can do; just leak */
5341
#if defined(PYOS_OS2)
5349
#ifdef HAVE_UNSETENV
5350
PyDoc_STRVAR(posix_unsetenv__doc__,
5352
Delete an environment variable.");
5355
posix_unsetenv(PyObject *self, PyObject *args)
5359
if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5364
/* Remove the key from posix_putenv_garbage;
5365
* this will cause it to be collected. This has to
5366
* happen after the real unsetenv() call because the
5367
* old value was still accessible until then.
5369
if (PyDict_DelItem(posix_putenv_garbage,
5370
PyTuple_GET_ITEM(args, 0))) {
5371
/* really not much we can do; just leak */
5378
#endif /* unsetenv */
5380
PyDoc_STRVAR(posix_strerror__doc__,
5381
"strerror(code) -> string\n\n\
5382
Translate an error code to a message string.");
5385
posix_strerror(PyObject *self, PyObject *args)
5389
if (!PyArg_ParseTuple(args, "i:strerror", &code))
5391
message = strerror(code);
5392
if (message == NULL) {
5393
PyErr_SetString(PyExc_ValueError,
5394
"strerror() argument out of range");
5397
return PyUnicode_FromString(message);
5401
#ifdef HAVE_SYS_WAIT_H
5404
PyDoc_STRVAR(posix_WCOREDUMP__doc__,
5405
"WCOREDUMP(status) -> bool\n\n\
5406
Return True if the process returning 'status' was dumped to a core file.");
5409
posix_WCOREDUMP(PyObject *self, PyObject *args)
5412
WAIT_STATUS_INT(status) = 0;
5414
if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5417
return PyBool_FromLong(WCOREDUMP(status));
5419
#endif /* WCOREDUMP */
5422
PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
5423
"WIFCONTINUED(status) -> bool\n\n\
5424
Return True if the process returning 'status' was continued from a\n\
5425
job control stop.");
5428
posix_WIFCONTINUED(PyObject *self, PyObject *args)
5431
WAIT_STATUS_INT(status) = 0;
5433
if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5436
return PyBool_FromLong(WIFCONTINUED(status));
5438
#endif /* WIFCONTINUED */
5441
PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
5442
"WIFSTOPPED(status) -> bool\n\n\
5443
Return True if the process returning 'status' was stopped.");
5446
posix_WIFSTOPPED(PyObject *self, PyObject *args)
5449
WAIT_STATUS_INT(status) = 0;
5451
if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5454
return PyBool_FromLong(WIFSTOPPED(status));
5456
#endif /* WIFSTOPPED */
5459
PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
5460
"WIFSIGNALED(status) -> bool\n\n\
5461
Return True if the process returning 'status' was terminated by a signal.");
5464
posix_WIFSIGNALED(PyObject *self, PyObject *args)
5467
WAIT_STATUS_INT(status) = 0;
5469
if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5472
return PyBool_FromLong(WIFSIGNALED(status));
5474
#endif /* WIFSIGNALED */
5477
PyDoc_STRVAR(posix_WIFEXITED__doc__,
5478
"WIFEXITED(status) -> bool\n\n\
5479
Return true if the process returning 'status' exited using the exit()\n\
5483
posix_WIFEXITED(PyObject *self, PyObject *args)
5486
WAIT_STATUS_INT(status) = 0;
5488
if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5491
return PyBool_FromLong(WIFEXITED(status));
5493
#endif /* WIFEXITED */
5496
PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
5497
"WEXITSTATUS(status) -> integer\n\n\
5498
Return the process return code from 'status'.");
5501
posix_WEXITSTATUS(PyObject *self, PyObject *args)
5504
WAIT_STATUS_INT(status) = 0;
5506
if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5509
return Py_BuildValue("i", WEXITSTATUS(status));
5511
#endif /* WEXITSTATUS */
5514
PyDoc_STRVAR(posix_WTERMSIG__doc__,
5515
"WTERMSIG(status) -> integer\n\n\
5516
Return the signal that terminated the process that provided the 'status'\n\
5520
posix_WTERMSIG(PyObject *self, PyObject *args)
5523
WAIT_STATUS_INT(status) = 0;
5525
if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5528
return Py_BuildValue("i", WTERMSIG(status));
5530
#endif /* WTERMSIG */
5533
PyDoc_STRVAR(posix_WSTOPSIG__doc__,
5534
"WSTOPSIG(status) -> integer\n\n\
5535
Return the signal that stopped the process that provided\n\
5536
the 'status' value.");
5539
posix_WSTOPSIG(PyObject *self, PyObject *args)
5542
WAIT_STATUS_INT(status) = 0;
5544
if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5547
return Py_BuildValue("i", WSTOPSIG(status));
5549
#endif /* WSTOPSIG */
5551
#endif /* HAVE_SYS_WAIT_H */
5554
#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
5556
/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5557
needed definitions in sys/statvfs.h */
5560
#include <sys/statvfs.h>
5563
_pystatvfs_fromstructstatvfs(struct statvfs st) {
5564
PyObject *v = PyStructSequence_New(&StatVFSResultType);
5568
#if !defined(HAVE_LARGEFILE_SUPPORT)
5569
PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5570
PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5571
PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5572
PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5573
PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5574
PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5575
PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5576
PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5577
PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5578
PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
5580
PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5581
PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5582
PyStructSequence_SET_ITEM(v, 2,
5583
PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5584
PyStructSequence_SET_ITEM(v, 3,
5585
PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5586
PyStructSequence_SET_ITEM(v, 4,
5587
PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5588
PyStructSequence_SET_ITEM(v, 5,
5589
PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5590
PyStructSequence_SET_ITEM(v, 6,
5591
PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5592
PyStructSequence_SET_ITEM(v, 7,
5593
PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5594
PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5595
PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
5601
PyDoc_STRVAR(posix_fstatvfs__doc__,
5602
"fstatvfs(fd) -> statvfs result\n\n\
5603
Perform an fstatvfs system call on the given fd.");
5606
posix_fstatvfs(PyObject *self, PyObject *args)
5611
if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5613
Py_BEGIN_ALLOW_THREADS
5614
res = fstatvfs(fd, &st);
5615
Py_END_ALLOW_THREADS
5617
return posix_error();
5619
return _pystatvfs_fromstructstatvfs(st);
5621
#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
5624
#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
5625
#include <sys/statvfs.h>
5627
PyDoc_STRVAR(posix_statvfs__doc__,
5628
"statvfs(path) -> statvfs result\n\n\
5629
Perform a statvfs system call on the given path.");
5632
posix_statvfs(PyObject *self, PyObject *args)
5637
if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5639
Py_BEGIN_ALLOW_THREADS
5640
res = statvfs(path, &st);
5641
Py_END_ALLOW_THREADS
5643
return posix_error_with_filename(path);
5645
return _pystatvfs_fromstructstatvfs(st);
5647
#endif /* HAVE_STATVFS */
5649
/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5650
* It maps strings representing configuration variable names to
5651
* integer values, allowing those functions to be called with the
5652
* magic names instead of polluting the module's namespace with tons of
5653
* rarely-used constants. There are three separate tables that use
5654
* these definitions.
5656
* This code is always included, even if none of the interfaces that
5657
* need it are included. The #if hackery needed to avoid it would be
5658
* sufficiently pervasive that it's not worth the loss of readability.
5666
conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5669
if (PyLong_Check(arg)) {
5670
*valuep = PyLong_AS_LONG(arg);
5674
/* look up the value in the table using a binary search */
5677
size_t hi = tablesize;
5679
const char *confname;
5680
if (!PyUnicode_Check(arg)) {
5681
PyErr_SetString(PyExc_TypeError,
5682
"configuration names must be strings or integers");
5685
confname = _PyUnicode_AsString(arg);
5686
if (confname == NULL)
5689
mid = (lo + hi) / 2;
5690
cmp = strcmp(confname, table[mid].name);
5696
*valuep = table[mid].value;
5700
PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5706
#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5707
static struct constdef posix_constants_pathconf[] = {
5708
#ifdef _PC_ABI_AIO_XFER_MAX
5709
{"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5711
#ifdef _PC_ABI_ASYNC_IO
5712
{"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5715
{"PC_ASYNC_IO", _PC_ASYNC_IO},
5717
#ifdef _PC_CHOWN_RESTRICTED
5718
{"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5720
#ifdef _PC_FILESIZEBITS
5721
{"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5724
{"PC_LAST", _PC_LAST},
5727
{"PC_LINK_MAX", _PC_LINK_MAX},
5729
#ifdef _PC_MAX_CANON
5730
{"PC_MAX_CANON", _PC_MAX_CANON},
5732
#ifdef _PC_MAX_INPUT
5733
{"PC_MAX_INPUT", _PC_MAX_INPUT},
5736
{"PC_NAME_MAX", _PC_NAME_MAX},
5739
{"PC_NO_TRUNC", _PC_NO_TRUNC},
5742
{"PC_PATH_MAX", _PC_PATH_MAX},
5745
{"PC_PIPE_BUF", _PC_PIPE_BUF},
5748
{"PC_PRIO_IO", _PC_PRIO_IO},
5750
#ifdef _PC_SOCK_MAXBUF
5751
{"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5754
{"PC_SYNC_IO", _PC_SYNC_IO},
5757
{"PC_VDISABLE", _PC_VDISABLE},
5762
conv_path_confname(PyObject *arg, int *valuep)
5764
return conv_confname(arg, valuep, posix_constants_pathconf,
5765
sizeof(posix_constants_pathconf)
5766
/ sizeof(struct constdef));
5770
#ifdef HAVE_FPATHCONF
5771
PyDoc_STRVAR(posix_fpathconf__doc__,
5772
"fpathconf(fd, name) -> integer\n\n\
5773
Return the configuration limit name for the file descriptor fd.\n\
5774
If there is no limit, return -1.");
5777
posix_fpathconf(PyObject *self, PyObject *args)
5779
PyObject *result = NULL;
5782
if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5783
conv_path_confname, &name)) {
5787
limit = fpathconf(fd, name);
5788
if (limit == -1 && errno != 0)
5791
result = PyLong_FromLong(limit);
5798
#ifdef HAVE_PATHCONF
5799
PyDoc_STRVAR(posix_pathconf__doc__,
5800
"pathconf(path, name) -> integer\n\n\
5801
Return the configuration limit name for the file or directory path.\n\
5802
If there is no limit, return -1.");
5805
posix_pathconf(PyObject *self, PyObject *args)
5807
PyObject *result = NULL;
5811
if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5812
conv_path_confname, &name)) {
5816
limit = pathconf(path, name);
5817
if (limit == -1 && errno != 0) {
5818
if (errno == EINVAL)
5819
/* could be a path or name problem */
5822
posix_error_with_filename(path);
5825
result = PyLong_FromLong(limit);
5832
static struct constdef posix_constants_confstr[] = {
5833
#ifdef _CS_ARCHITECTURE
5834
{"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5837
{"CS_HOSTNAME", _CS_HOSTNAME},
5839
#ifdef _CS_HW_PROVIDER
5840
{"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5842
#ifdef _CS_HW_SERIAL
5843
{"CS_HW_SERIAL", _CS_HW_SERIAL},
5845
#ifdef _CS_INITTAB_NAME
5846
{"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5848
#ifdef _CS_LFS64_CFLAGS
5849
{"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5851
#ifdef _CS_LFS64_LDFLAGS
5852
{"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5854
#ifdef _CS_LFS64_LIBS
5855
{"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5857
#ifdef _CS_LFS64_LINTFLAGS
5858
{"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5860
#ifdef _CS_LFS_CFLAGS
5861
{"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5863
#ifdef _CS_LFS_LDFLAGS
5864
{"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5867
{"CS_LFS_LIBS", _CS_LFS_LIBS},
5869
#ifdef _CS_LFS_LINTFLAGS
5870
{"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5873
{"CS_MACHINE", _CS_MACHINE},
5876
{"CS_PATH", _CS_PATH},
5879
{"CS_RELEASE", _CS_RELEASE},
5881
#ifdef _CS_SRPC_DOMAIN
5882
{"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5885
{"CS_SYSNAME", _CS_SYSNAME},
5888
{"CS_VERSION", _CS_VERSION},
5890
#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5891
{"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5893
#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5894
{"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5896
#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5897
{"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5899
#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5900
{"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5902
#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5903
{"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5905
#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5906
{"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5908
#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5909
{"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5911
#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5912
{"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5914
#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5915
{"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5917
#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5918
{"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5920
#ifdef _CS_XBS5_LP64_OFF64_LIBS
5921
{"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5923
#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5924
{"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5926
#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5927
{"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5929
#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5930
{"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5932
#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5933
{"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5935
#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5936
{"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5938
#ifdef _MIPS_CS_AVAIL_PROCESSORS
5939
{"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5941
#ifdef _MIPS_CS_BASE
5942
{"MIPS_CS_BASE", _MIPS_CS_BASE},
5944
#ifdef _MIPS_CS_HOSTID
5945
{"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5947
#ifdef _MIPS_CS_HW_NAME
5948
{"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5950
#ifdef _MIPS_CS_NUM_PROCESSORS
5951
{"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5953
#ifdef _MIPS_CS_OSREL_MAJ
5954
{"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5956
#ifdef _MIPS_CS_OSREL_MIN
5957
{"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5959
#ifdef _MIPS_CS_OSREL_PATCH
5960
{"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5962
#ifdef _MIPS_CS_OS_NAME
5963
{"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5965
#ifdef _MIPS_CS_OS_PROVIDER
5966
{"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5968
#ifdef _MIPS_CS_PROCESSORS
5969
{"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5971
#ifdef _MIPS_CS_SERIAL
5972
{"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5974
#ifdef _MIPS_CS_VENDOR
5975
{"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5980
conv_confstr_confname(PyObject *arg, int *valuep)
5982
return conv_confname(arg, valuep, posix_constants_confstr,
5983
sizeof(posix_constants_confstr)
5984
/ sizeof(struct constdef));
5987
PyDoc_STRVAR(posix_confstr__doc__,
5988
"confstr(name) -> string\n\n\
5989
Return a string-valued system configuration variable.");
5992
posix_confstr(PyObject *self, PyObject *args)
5994
PyObject *result = NULL;
5998
if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6002
len = confstr(name, buffer, sizeof(buffer));
6013
if ((unsigned int)len >= sizeof(buffer)) {
6014
result = PyUnicode_FromStringAndSize(NULL, len-1);
6016
confstr(name, _PyUnicode_AsString(result), len);
6019
result = PyUnicode_FromStringAndSize(buffer, len-1);
6028
static struct constdef posix_constants_sysconf[] = {
6029
#ifdef _SC_2_CHAR_TERM
6030
{"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6033
{"SC_2_C_BIND", _SC_2_C_BIND},
6036
{"SC_2_C_DEV", _SC_2_C_DEV},
6038
#ifdef _SC_2_C_VERSION
6039
{"SC_2_C_VERSION", _SC_2_C_VERSION},
6041
#ifdef _SC_2_FORT_DEV
6042
{"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6044
#ifdef _SC_2_FORT_RUN
6045
{"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6047
#ifdef _SC_2_LOCALEDEF
6048
{"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6051
{"SC_2_SW_DEV", _SC_2_SW_DEV},
6054
{"SC_2_UPE", _SC_2_UPE},
6056
#ifdef _SC_2_VERSION
6057
{"SC_2_VERSION", _SC_2_VERSION},
6059
#ifdef _SC_ABI_ASYNCHRONOUS_IO
6060
{"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6063
{"SC_ACL", _SC_ACL},
6065
#ifdef _SC_AIO_LISTIO_MAX
6066
{"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6069
{"SC_AIO_MAX", _SC_AIO_MAX},
6071
#ifdef _SC_AIO_PRIO_DELTA_MAX
6072
{"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6075
{"SC_ARG_MAX", _SC_ARG_MAX},
6077
#ifdef _SC_ASYNCHRONOUS_IO
6078
{"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6080
#ifdef _SC_ATEXIT_MAX
6081
{"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6084
{"SC_AUDIT", _SC_AUDIT},
6086
#ifdef _SC_AVPHYS_PAGES
6087
{"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6089
#ifdef _SC_BC_BASE_MAX
6090
{"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6092
#ifdef _SC_BC_DIM_MAX
6093
{"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6095
#ifdef _SC_BC_SCALE_MAX
6096
{"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6098
#ifdef _SC_BC_STRING_MAX
6099
{"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6102
{"SC_CAP", _SC_CAP},
6104
#ifdef _SC_CHARCLASS_NAME_MAX
6105
{"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6108
{"SC_CHAR_BIT", _SC_CHAR_BIT},
6111
{"SC_CHAR_MAX", _SC_CHAR_MAX},
6114
{"SC_CHAR_MIN", _SC_CHAR_MIN},
6116
#ifdef _SC_CHILD_MAX
6117
{"SC_CHILD_MAX", _SC_CHILD_MAX},
6120
{"SC_CLK_TCK", _SC_CLK_TCK},
6122
#ifdef _SC_COHER_BLKSZ
6123
{"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6125
#ifdef _SC_COLL_WEIGHTS_MAX
6126
{"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6128
#ifdef _SC_DCACHE_ASSOC
6129
{"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6131
#ifdef _SC_DCACHE_BLKSZ
6132
{"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6134
#ifdef _SC_DCACHE_LINESZ
6135
{"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6137
#ifdef _SC_DCACHE_SZ
6138
{"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6140
#ifdef _SC_DCACHE_TBLKSZ
6141
{"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6143
#ifdef _SC_DELAYTIMER_MAX
6144
{"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6146
#ifdef _SC_EQUIV_CLASS_MAX
6147
{"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6149
#ifdef _SC_EXPR_NEST_MAX
6150
{"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6153
{"SC_FSYNC", _SC_FSYNC},
6155
#ifdef _SC_GETGR_R_SIZE_MAX
6156
{"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6158
#ifdef _SC_GETPW_R_SIZE_MAX
6159
{"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6161
#ifdef _SC_ICACHE_ASSOC
6162
{"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6164
#ifdef _SC_ICACHE_BLKSZ
6165
{"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6167
#ifdef _SC_ICACHE_LINESZ
6168
{"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6170
#ifdef _SC_ICACHE_SZ
6171
{"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6174
{"SC_INF", _SC_INF},
6177
{"SC_INT_MAX", _SC_INT_MAX},
6180
{"SC_INT_MIN", _SC_INT_MIN},
6183
{"SC_IOV_MAX", _SC_IOV_MAX},
6185
#ifdef _SC_IP_SECOPTS
6186
{"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6188
#ifdef _SC_JOB_CONTROL
6189
{"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6191
#ifdef _SC_KERN_POINTERS
6192
{"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6195
{"SC_KERN_SIM", _SC_KERN_SIM},
6198
{"SC_LINE_MAX", _SC_LINE_MAX},
6200
#ifdef _SC_LOGIN_NAME_MAX
6201
{"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6203
#ifdef _SC_LOGNAME_MAX
6204
{"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6207
{"SC_LONG_BIT", _SC_LONG_BIT},
6210
{"SC_MAC", _SC_MAC},
6212
#ifdef _SC_MAPPED_FILES
6213
{"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6216
{"SC_MAXPID", _SC_MAXPID},
6218
#ifdef _SC_MB_LEN_MAX
6219
{"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6222
{"SC_MEMLOCK", _SC_MEMLOCK},
6224
#ifdef _SC_MEMLOCK_RANGE
6225
{"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6227
#ifdef _SC_MEMORY_PROTECTION
6228
{"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6230
#ifdef _SC_MESSAGE_PASSING
6231
{"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6233
#ifdef _SC_MMAP_FIXED_ALIGNMENT
6234
{"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6236
#ifdef _SC_MQ_OPEN_MAX
6237
{"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6239
#ifdef _SC_MQ_PRIO_MAX
6240
{"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6242
#ifdef _SC_NACLS_MAX
6243
{"SC_NACLS_MAX", _SC_NACLS_MAX},
6245
#ifdef _SC_NGROUPS_MAX
6246
{"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6248
#ifdef _SC_NL_ARGMAX
6249
{"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6251
#ifdef _SC_NL_LANGMAX
6252
{"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6254
#ifdef _SC_NL_MSGMAX
6255
{"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6258
{"SC_NL_NMAX", _SC_NL_NMAX},
6260
#ifdef _SC_NL_SETMAX
6261
{"SC_NL_SETMAX", _SC_NL_SETMAX},
6263
#ifdef _SC_NL_TEXTMAX
6264
{"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6266
#ifdef _SC_NPROCESSORS_CONF
6267
{"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6269
#ifdef _SC_NPROCESSORS_ONLN
6270
{"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6272
#ifdef _SC_NPROC_CONF
6273
{"SC_NPROC_CONF", _SC_NPROC_CONF},
6275
#ifdef _SC_NPROC_ONLN
6276
{"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6279
{"SC_NZERO", _SC_NZERO},
6282
{"SC_OPEN_MAX", _SC_OPEN_MAX},
6285
{"SC_PAGESIZE", _SC_PAGESIZE},
6287
#ifdef _SC_PAGE_SIZE
6288
{"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6291
{"SC_PASS_MAX", _SC_PASS_MAX},
6293
#ifdef _SC_PHYS_PAGES
6294
{"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6297
{"SC_PII", _SC_PII},
6299
#ifdef _SC_PII_INTERNET
6300
{"SC_PII_INTERNET", _SC_PII_INTERNET},
6302
#ifdef _SC_PII_INTERNET_DGRAM
6303
{"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6305
#ifdef _SC_PII_INTERNET_STREAM
6306
{"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6309
{"SC_PII_OSI", _SC_PII_OSI},
6311
#ifdef _SC_PII_OSI_CLTS
6312
{"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6314
#ifdef _SC_PII_OSI_COTS
6315
{"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6317
#ifdef _SC_PII_OSI_M
6318
{"SC_PII_OSI_M", _SC_PII_OSI_M},
6320
#ifdef _SC_PII_SOCKET
6321
{"SC_PII_SOCKET", _SC_PII_SOCKET},
6324
{"SC_PII_XTI", _SC_PII_XTI},
6327
{"SC_POLL", _SC_POLL},
6329
#ifdef _SC_PRIORITIZED_IO
6330
{"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6332
#ifdef _SC_PRIORITY_SCHEDULING
6333
{"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6335
#ifdef _SC_REALTIME_SIGNALS
6336
{"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6338
#ifdef _SC_RE_DUP_MAX
6339
{"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6341
#ifdef _SC_RTSIG_MAX
6342
{"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6344
#ifdef _SC_SAVED_IDS
6345
{"SC_SAVED_IDS", _SC_SAVED_IDS},
6347
#ifdef _SC_SCHAR_MAX
6348
{"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6350
#ifdef _SC_SCHAR_MIN
6351
{"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6354
{"SC_SELECT", _SC_SELECT},
6356
#ifdef _SC_SEMAPHORES
6357
{"SC_SEMAPHORES", _SC_SEMAPHORES},
6359
#ifdef _SC_SEM_NSEMS_MAX
6360
{"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6362
#ifdef _SC_SEM_VALUE_MAX
6363
{"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6365
#ifdef _SC_SHARED_MEMORY_OBJECTS
6366
{"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6369
{"SC_SHRT_MAX", _SC_SHRT_MAX},
6372
{"SC_SHRT_MIN", _SC_SHRT_MIN},
6374
#ifdef _SC_SIGQUEUE_MAX
6375
{"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6377
#ifdef _SC_SIGRT_MAX
6378
{"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6380
#ifdef _SC_SIGRT_MIN
6381
{"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6383
#ifdef _SC_SOFTPOWER
6384
{"SC_SOFTPOWER", _SC_SOFTPOWER},
6386
#ifdef _SC_SPLIT_CACHE
6387
{"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6389
#ifdef _SC_SSIZE_MAX
6390
{"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6392
#ifdef _SC_STACK_PROT
6393
{"SC_STACK_PROT", _SC_STACK_PROT},
6395
#ifdef _SC_STREAM_MAX
6396
{"SC_STREAM_MAX", _SC_STREAM_MAX},
6398
#ifdef _SC_SYNCHRONIZED_IO
6399
{"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6402
{"SC_THREADS", _SC_THREADS},
6404
#ifdef _SC_THREAD_ATTR_STACKADDR
6405
{"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6407
#ifdef _SC_THREAD_ATTR_STACKSIZE
6408
{"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6410
#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6411
{"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6413
#ifdef _SC_THREAD_KEYS_MAX
6414
{"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6416
#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6417
{"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6419
#ifdef _SC_THREAD_PRIO_INHERIT
6420
{"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6422
#ifdef _SC_THREAD_PRIO_PROTECT
6423
{"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6425
#ifdef _SC_THREAD_PROCESS_SHARED
6426
{"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6428
#ifdef _SC_THREAD_SAFE_FUNCTIONS
6429
{"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6431
#ifdef _SC_THREAD_STACK_MIN
6432
{"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6434
#ifdef _SC_THREAD_THREADS_MAX
6435
{"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6438
{"SC_TIMERS", _SC_TIMERS},
6440
#ifdef _SC_TIMER_MAX
6441
{"SC_TIMER_MAX", _SC_TIMER_MAX},
6443
#ifdef _SC_TTY_NAME_MAX
6444
{"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6446
#ifdef _SC_TZNAME_MAX
6447
{"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6449
#ifdef _SC_T_IOV_MAX
6450
{"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6452
#ifdef _SC_UCHAR_MAX
6453
{"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6456
{"SC_UINT_MAX", _SC_UINT_MAX},
6458
#ifdef _SC_UIO_MAXIOV
6459
{"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6461
#ifdef _SC_ULONG_MAX
6462
{"SC_ULONG_MAX", _SC_ULONG_MAX},
6464
#ifdef _SC_USHRT_MAX
6465
{"SC_USHRT_MAX", _SC_USHRT_MAX},
6468
{"SC_VERSION", _SC_VERSION},
6471
{"SC_WORD_BIT", _SC_WORD_BIT},
6473
#ifdef _SC_XBS5_ILP32_OFF32
6474
{"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6476
#ifdef _SC_XBS5_ILP32_OFFBIG
6477
{"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6479
#ifdef _SC_XBS5_LP64_OFF64
6480
{"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6482
#ifdef _SC_XBS5_LPBIG_OFFBIG
6483
{"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6485
#ifdef _SC_XOPEN_CRYPT
6486
{"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6488
#ifdef _SC_XOPEN_ENH_I18N
6489
{"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6491
#ifdef _SC_XOPEN_LEGACY
6492
{"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6494
#ifdef _SC_XOPEN_REALTIME
6495
{"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6497
#ifdef _SC_XOPEN_REALTIME_THREADS
6498
{"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6500
#ifdef _SC_XOPEN_SHM
6501
{"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6503
#ifdef _SC_XOPEN_UNIX
6504
{"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6506
#ifdef _SC_XOPEN_VERSION
6507
{"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6509
#ifdef _SC_XOPEN_XCU_VERSION
6510
{"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6512
#ifdef _SC_XOPEN_XPG2
6513
{"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6515
#ifdef _SC_XOPEN_XPG3
6516
{"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6518
#ifdef _SC_XOPEN_XPG4
6519
{"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6524
conv_sysconf_confname(PyObject *arg, int *valuep)
6526
return conv_confname(arg, valuep, posix_constants_sysconf,
6527
sizeof(posix_constants_sysconf)
6528
/ sizeof(struct constdef));
6531
PyDoc_STRVAR(posix_sysconf__doc__,
6532
"sysconf(name) -> integer\n\n\
6533
Return an integer-valued system configuration variable.");
6536
posix_sysconf(PyObject *self, PyObject *args)
6538
PyObject *result = NULL;
6541
if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6545
value = sysconf(name);
6546
if (value == -1 && errno != 0)
6549
result = PyLong_FromLong(value);
6556
/* This code is used to ensure that the tables of configuration value names
6557
* are in sorted order as required by conv_confname(), and also to build the
6558
* the exported dictionaries that are used to publish information about the
6559
* names available on the host platform.
6561
* Sorting the table at runtime ensures that the table is properly ordered
6562
* when used, even for platforms we're not able to test on. It also makes
6563
* it easier to add additional entries to the tables.
6567
cmp_constdefs(const void *v1, const void *v2)
6569
const struct constdef *c1 =
6570
(const struct constdef *) v1;
6571
const struct constdef *c2 =
6572
(const struct constdef *) v2;
6574
return strcmp(c1->name, c2->name);
6578
setup_confname_table(struct constdef *table, size_t tablesize,
6579
char *tablename, PyObject *module)
6584
qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6589
for (i=0; i < tablesize; ++i) {
6590
PyObject *o = PyLong_FromLong(table[i].value);
6591
if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6598
return PyModule_AddObject(module, tablename, d);
6601
/* Return -1 on failure, 0 on success. */
6603
setup_confname_tables(PyObject *module)
6605
#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6606
if (setup_confname_table(posix_constants_pathconf,
6607
sizeof(posix_constants_pathconf)
6608
/ sizeof(struct constdef),
6609
"pathconf_names", module))
6613
if (setup_confname_table(posix_constants_confstr,
6614
sizeof(posix_constants_confstr)
6615
/ sizeof(struct constdef),
6616
"confstr_names", module))
6620
if (setup_confname_table(posix_constants_sysconf,
6621
sizeof(posix_constants_sysconf)
6622
/ sizeof(struct constdef),
6623
"sysconf_names", module))
6630
PyDoc_STRVAR(posix_abort__doc__,
6631
"abort() -> does not return!\n\n\
6632
Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
6633
in the hardest way possible on the hosting operating system.");
6636
posix_abort(PyObject *self, PyObject *noargs)
6640
Py_FatalError("abort() called from Python code didn't abort!");
6645
PyDoc_STRVAR(win32_startfile__doc__,
6646
"startfile(filepath [, operation]) - Start a file with its associated\n\
6649
When \"operation\" is not specified or \"open\", this acts like\n\
6650
double-clicking the file in Explorer, or giving the file name as an\n\
6651
argument to the DOS \"start\" command: the file is opened with whatever\n\
6652
application (if any) its extension is associated.\n\
6653
When another \"operation\" is given, it specifies what should be done with\n\
6654
the file. A typical operation is \"print\".\n\
6656
startfile returns as soon as the associated application is launched.\n\
6657
There is no option to wait for the application to close, and no way\n\
6658
to retrieve the application's exit status.\n\
6660
The filepath is relative to the current directory. If you want to use\n\
6661
an absolute path, make sure the first character is not a slash (\"/\");\n\
6662
the underlying Win32 ShellExecute function doesn't work if it is.");
6665
win32_startfile(PyObject *self, PyObject *args)
6668
char *operation = NULL;
6670
#ifdef Py_WIN_WIDE_FILENAMES
6671
if (unicode_file_names()) {
6672
PyObject *unipath, *woperation = NULL;
6673
if (!PyArg_ParseTuple(args, "U|s:startfile",
6674
&unipath, &operation)) {
6681
woperation = PyUnicode_DecodeASCII(operation,
6682
strlen(operation), NULL);
6690
Py_BEGIN_ALLOW_THREADS
6691
rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6692
PyUnicode_AS_UNICODE(unipath),
6693
NULL, NULL, SW_SHOWNORMAL);
6694
Py_END_ALLOW_THREADS
6696
Py_XDECREF(woperation);
6697
if (rc <= (HINSTANCE)32) {
6698
PyObject *errval = win32_error_unicode("startfile",
6699
PyUnicode_AS_UNICODE(unipath));
6708
if (!PyArg_ParseTuple(args, "et|s:startfile",
6709
Py_FileSystemDefaultEncoding, &filepath,
6712
Py_BEGIN_ALLOW_THREADS
6713
rc = ShellExecute((HWND)0, operation, filepath,
6714
NULL, NULL, SW_SHOWNORMAL);
6715
Py_END_ALLOW_THREADS
6716
if (rc <= (HINSTANCE)32) {
6717
PyObject *errval = win32_error("startfile", filepath);
6718
PyMem_Free(filepath);
6721
PyMem_Free(filepath);
6727
#ifdef HAVE_GETLOADAVG
6728
PyDoc_STRVAR(posix_getloadavg__doc__,
6729
"getloadavg() -> (float, float, float)\n\n\
6730
Return the number of processes in the system run queue averaged over\n\
6731
the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6735
posix_getloadavg(PyObject *self, PyObject *noargs)
6738
if (getloadavg(loadavg, 3)!=3) {
6739
PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6742
return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6748
PyDoc_STRVAR(win32_urandom__doc__,
6749
"urandom(n) -> str\n\n\
6750
Return n random bytes suitable for cryptographic use.");
6752
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6753
LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6755
typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6758
static CRYPTGENRANDOM pCryptGenRandom = NULL;
6759
/* This handle is never explicitly released. Instead, the operating
6760
system will release it when the process terminates. */
6761
static HCRYPTPROV hCryptProv = 0;
6764
win32_urandom(PyObject *self, PyObject *args)
6769
/* Read arguments */
6770
if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6773
return PyErr_Format(PyExc_ValueError,
6774
"negative argument not allowed");
6776
if (hCryptProv == 0) {
6777
HINSTANCE hAdvAPI32 = NULL;
6778
CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
6780
/* Obtain handle to the DLL containing CryptoAPI
6781
This should not fail */
6782
hAdvAPI32 = GetModuleHandle("advapi32.dll");
6783
if(hAdvAPI32 == NULL)
6784
return win32_error("GetModuleHandle", NULL);
6786
/* Obtain pointers to the CryptoAPI functions
6787
This will fail on some early versions of Win95 */
6788
pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6790
"CryptAcquireContextA");
6791
if (pCryptAcquireContext == NULL)
6792
return PyErr_Format(PyExc_NotImplementedError,
6793
"CryptAcquireContextA not found");
6795
pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6796
hAdvAPI32, "CryptGenRandom");
6797
if (pCryptGenRandom == NULL)
6798
return PyErr_Format(PyExc_NotImplementedError,
6799
"CryptGenRandom not found");
6801
/* Acquire context */
6802
if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6803
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6804
return win32_error("CryptAcquireContext", NULL);
6807
/* Allocate bytes */
6808
result = PyBytes_FromStringAndSize(NULL, howMany);
6809
if (result != NULL) {
6810
/* Get random data */
6811
memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6812
if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6813
PyBytes_AS_STRING(result))) {
6815
return win32_error("CryptGenRandom", NULL);
6822
PyDoc_STRVAR(device_encoding__doc__,
6823
"device_encoding(fd) -> str\n\n\
6824
Return a string describing the encoding of the device\n\
6825
if the output is a terminal; else return None.");
6828
device_encoding(PyObject *self, PyObject *args)
6831
if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6833
if (!_PyVerify_fd(fd))
6834
return posix_error();
6839
#if defined(MS_WINDOWS) || defined(MS_WIN64)
6842
sprintf(buf, "cp%d", GetConsoleCP());
6843
return PyUnicode_FromString(buf);
6845
if (fd == 1 || fd == 2) {
6847
sprintf(buf, "cp%d", GetConsoleOutputCP());
6848
return PyUnicode_FromString(buf);
6850
#elif defined(CODESET)
6852
char *codeset = nl_langinfo(CODESET);
6853
if (codeset != NULL && codeset[0] != 0)
6854
return PyUnicode_FromString(codeset);
6862
/* Use openssl random routine */
6863
#include <openssl/rand.h>
6864
PyDoc_STRVAR(vms_urandom__doc__,
6865
"urandom(n) -> str\n\n\
6866
Return n random bytes suitable for cryptographic use.");
6869
vms_urandom(PyObject *self, PyObject *args)
6874
/* Read arguments */
6875
if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6878
return PyErr_Format(PyExc_ValueError,
6879
"negative argument not allowed");
6881
/* Allocate bytes */
6882
result = PyBytes_FromStringAndSize(NULL, howMany);
6883
if (result != NULL) {
6884
/* Get random data */
6885
if (RAND_pseudo_bytes((unsigned char*)
6886
PyBytes_AS_STRING(result),
6889
return PyErr_Format(PyExc_ValueError,
6890
"RAND_pseudo_bytes");
6897
static PyMethodDef posix_methods[] = {
6898
{"access", posix_access, METH_VARARGS, posix_access__doc__},
6900
{"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6902
{"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6904
{"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6905
#endif /* HAVE_CHFLAGS */
6906
{"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
6908
{"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6909
#endif /* HAVE_FCHMOD */
6911
{"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
6912
#endif /* HAVE_CHOWN */
6914
{"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6915
#endif /* HAVE_LCHMOD */
6917
{"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6918
#endif /* HAVE_FCHOWN */
6919
#ifdef HAVE_LCHFLAGS
6920
{"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6921
#endif /* HAVE_LCHFLAGS */
6923
{"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6924
#endif /* HAVE_LCHOWN */
6926
{"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6929
{"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
6932
{"getcwd", (PyCFunction)posix_getcwd_unicode,
6933
METH_NOARGS, posix_getcwd__doc__},
6934
{"getcwdb", (PyCFunction)posix_getcwd_bytes,
6935
METH_NOARGS, posix_getcwdb__doc__},
6938
{"link", posix_link, METH_VARARGS, posix_link__doc__},
6939
#endif /* HAVE_LINK */
6940
{"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6941
{"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6942
{"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
6944
{"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
6945
#endif /* HAVE_NICE */
6946
#ifdef HAVE_READLINK
6947
{"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
6948
#endif /* HAVE_READLINK */
6949
{"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6950
{"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6951
{"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
6952
{"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
6954
{"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
6955
#endif /* HAVE_SYMLINK */
6957
{"system", posix_system, METH_VARARGS, posix_system__doc__},
6959
{"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
6961
{"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
6962
#endif /* HAVE_UNAME */
6963
{"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6964
{"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6965
{"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
6967
{"times", posix_times, METH_NOARGS, posix_times__doc__},
6968
#endif /* HAVE_TIMES */
6969
{"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
6971
{"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6972
{"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
6973
#endif /* HAVE_EXECV */
6975
{"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6976
{"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
6977
#if defined(PYOS_OS2)
6978
{"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6979
{"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6980
#endif /* PYOS_OS2 */
6981
#endif /* HAVE_SPAWNV */
6983
{"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
6984
#endif /* HAVE_FORK1 */
6986
{"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
6987
#endif /* HAVE_FORK */
6988
#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
6989
{"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
6990
#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
6992
{"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
6993
#endif /* HAVE_FORKPTY */
6995
{"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
6996
#endif /* HAVE_GETEGID */
6998
{"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
6999
#endif /* HAVE_GETEUID */
7001
{"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
7002
#endif /* HAVE_GETGID */
7003
#ifdef HAVE_GETGROUPS
7004
{"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
7006
{"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
7008
{"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
7009
#endif /* HAVE_GETPGRP */
7011
{"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
7012
#endif /* HAVE_GETPPID */
7014
{"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
7015
#endif /* HAVE_GETUID */
7016
#ifdef HAVE_GETLOGIN
7017
{"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
7020
{"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
7021
#endif /* HAVE_KILL */
7023
{"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7024
#endif /* HAVE_KILLPG */
7026
{"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
7027
#endif /* HAVE_PLOCK */
7029
{"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7032
{"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
7033
#endif /* HAVE_SETUID */
7035
{"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7036
#endif /* HAVE_SETEUID */
7038
{"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7039
#endif /* HAVE_SETEGID */
7040
#ifdef HAVE_SETREUID
7041
{"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7042
#endif /* HAVE_SETREUID */
7043
#ifdef HAVE_SETREGID
7044
{"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7045
#endif /* HAVE_SETREGID */
7047
{"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
7048
#endif /* HAVE_SETGID */
7049
#ifdef HAVE_SETGROUPS
7050
{"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
7051
#endif /* HAVE_SETGROUPS */
7053
{"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7054
#endif /* HAVE_GETPGID */
7056
{"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
7057
#endif /* HAVE_SETPGRP */
7059
{"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
7060
#endif /* HAVE_WAIT */
7062
{"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7063
#endif /* HAVE_WAIT3 */
7065
{"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7066
#endif /* HAVE_WAIT4 */
7067
#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
7068
{"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
7069
#endif /* HAVE_WAITPID */
7071
{"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7072
#endif /* HAVE_GETSID */
7074
{"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
7075
#endif /* HAVE_SETSID */
7077
{"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
7078
#endif /* HAVE_SETPGID */
7079
#ifdef HAVE_TCGETPGRP
7080
{"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
7081
#endif /* HAVE_TCGETPGRP */
7082
#ifdef HAVE_TCSETPGRP
7083
{"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
7084
#endif /* HAVE_TCSETPGRP */
7085
{"open", posix_open, METH_VARARGS, posix_open__doc__},
7086
{"close", posix_close, METH_VARARGS, posix_close__doc__},
7087
{"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7088
{"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7089
{"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7090
{"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7091
{"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7092
{"read", posix_read, METH_VARARGS, posix_read__doc__},
7093
{"write", posix_write, METH_VARARGS, posix_write__doc__},
7094
{"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7095
{"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
7097
{"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
7100
{"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
7102
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
7103
{"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7105
#ifdef HAVE_DEVICE_MACROS
7106
{"major", posix_major, METH_VARARGS, posix_major__doc__},
7107
{"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7108
{"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7110
#ifdef HAVE_FTRUNCATE
7111
{"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
7114
{"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
7116
#ifdef HAVE_UNSETENV
7117
{"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7119
{"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
7121
{"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7124
{"fsync", posix_fsync, METH_O, posix_fsync__doc__},
7126
#ifdef HAVE_FDATASYNC
7127
{"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
7129
#ifdef HAVE_SYS_WAIT_H
7131
{"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7132
#endif /* WCOREDUMP */
7134
{"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7135
#endif /* WIFCONTINUED */
7137
{"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
7138
#endif /* WIFSTOPPED */
7140
{"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
7141
#endif /* WIFSIGNALED */
7143
{"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
7144
#endif /* WIFEXITED */
7146
{"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
7147
#endif /* WEXITSTATUS */
7149
{"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
7150
#endif /* WTERMSIG */
7152
{"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
7153
#endif /* WSTOPSIG */
7154
#endif /* HAVE_SYS_WAIT_H */
7155
#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7156
{"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
7158
#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7159
{"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
7162
{"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7165
{"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7167
#ifdef HAVE_FPATHCONF
7168
{"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7170
#ifdef HAVE_PATHCONF
7171
{"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7173
{"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
7175
{"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7177
#ifdef HAVE_GETLOADAVG
7178
{"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
7181
{"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7184
{"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7186
{NULL, NULL} /* Sentinel */
7191
ins(PyObject *module, char *symbol, long value)
7193
return PyModule_AddIntConstant(module, symbol, value);
7196
#if defined(PYOS_OS2)
7197
/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
7198
static int insertvalues(PyObject *module)
7201
ULONG values[QSV_MAX+1];
7205
Py_BEGIN_ALLOW_THREADS
7206
rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
7207
Py_END_ALLOW_THREADS
7209
if (rc != NO_ERROR) {
7214
if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7215
if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7216
if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7217
if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7218
if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7219
if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7220
if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
7222
switch (values[QSV_VERSION_MINOR]) {
7223
case 0: ver = "2.00"; break;
7224
case 10: ver = "2.10"; break;
7225
case 11: ver = "2.11"; break;
7226
case 30: ver = "3.00"; break;
7227
case 40: ver = "4.00"; break;
7228
case 50: ver = "5.00"; break;
7230
PyOS_snprintf(tmp, sizeof(tmp),
7231
"%d-%d", values[QSV_VERSION_MAJOR],
7232
values[QSV_VERSION_MINOR]);
7236
/* Add Indicator of the Version of the Operating System */
7237
if (PyModule_AddStringConstant(module, "version", tmp) < 0)
7240
/* Add Indicator of Which Drive was Used to Boot the System */
7241
tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7245
return PyModule_AddStringConstant(module, "bootdrive", tmp);
7250
all_ins(PyObject *d)
7253
if (ins(d, "F_OK", (long)F_OK)) return -1;
7256
if (ins(d, "R_OK", (long)R_OK)) return -1;
7259
if (ins(d, "W_OK", (long)W_OK)) return -1;
7262
if (ins(d, "X_OK", (long)X_OK)) return -1;
7265
if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7268
if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7271
if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7274
if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
7277
if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7280
if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7283
if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7286
if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7289
if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7292
if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7295
if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7298
if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7301
if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7304
if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7307
if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7310
if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7313
if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7316
if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7319
if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7322
if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7325
if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7328
if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7331
if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7336
/* Don't inherit in child processes. */
7337
if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7339
#ifdef _O_SHORT_LIVED
7340
/* Optimize for short life (keep in memory). */
7341
/* MS forgot to define this one with a non-underscore form too. */
7342
if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7345
/* Automatically delete when last handle is closed. */
7346
if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7349
/* Optimize for random access. */
7350
if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7353
/* Optimize for sequential access. */
7354
if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7357
/* GNU extensions. */
7359
/* Send a SIGIO signal whenever input or output
7360
becomes available on file descriptor */
7361
if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7364
/* Direct disk access. */
7365
if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7368
/* Must be a directory. */
7369
if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7372
/* Do not follow links. */
7373
if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7376
/* Do not update the access time. */
7377
if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7380
/* These come from sysexits.h */
7382
if (ins(d, "EX_OK", (long)EX_OK)) return -1;
7385
if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
7386
#endif /* EX_USAGE */
7388
if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
7389
#endif /* EX_DATAERR */
7391
if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
7392
#endif /* EX_NOINPUT */
7394
if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
7395
#endif /* EX_NOUSER */
7397
if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
7398
#endif /* EX_NOHOST */
7399
#ifdef EX_UNAVAILABLE
7400
if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
7401
#endif /* EX_UNAVAILABLE */
7403
if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
7404
#endif /* EX_SOFTWARE */
7406
if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
7407
#endif /* EX_OSERR */
7409
if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
7410
#endif /* EX_OSFILE */
7412
if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
7413
#endif /* EX_CANTCREAT */
7415
if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
7416
#endif /* EX_IOERR */
7418
if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
7419
#endif /* EX_TEMPFAIL */
7421
if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
7422
#endif /* EX_PROTOCOL */
7424
if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
7425
#endif /* EX_NOPERM */
7427
if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
7428
#endif /* EX_CONFIG */
7430
if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
7431
#endif /* EX_NOTFOUND */
7434
#if defined(PYOS_OS2) && defined(PYCC_GCC)
7435
if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7436
if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7437
if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7438
if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7439
if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7440
if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7441
if (ins(d, "P_PM", (long)P_PM)) return -1;
7442
if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7443
if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7444
if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7445
if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7446
if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7447
if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7448
if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7449
if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7450
if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7451
if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7452
if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7453
if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7454
if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7456
if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7457
if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7458
if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7459
if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7460
if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
7464
#if defined(PYOS_OS2)
7465
if (insertvalues(d)) return -1;
7471
#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
7472
#define INITFUNC PyInit_nt
7473
#define MODNAME "nt"
7475
#elif defined(PYOS_OS2)
7476
#define INITFUNC PyInit_os2
7477
#define MODNAME "os2"
7480
#define INITFUNC PyInit_posix
7481
#define MODNAME "posix"
7484
static struct PyModuleDef posixmodule = {
7485
PyModuleDef_HEAD_INIT,
7502
m = PyModule_Create(&posixmodule);
7506
/* Initialize environ dictionary */
7507
v = convertenviron();
7509
if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7516
if (setup_confname_tables(m))
7519
Py_INCREF(PyExc_OSError);
7520
PyModule_AddObject(m, "error", PyExc_OSError);
7523
if (posix_putenv_garbage == NULL)
7524
posix_putenv_garbage = PyDict_New();
7528
stat_result_desc.name = MODNAME ".stat_result";
7529
stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7530
stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7531
stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7532
PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7533
structseq_new = StatResultType.tp_new;
7534
StatResultType.tp_new = statresult_new;
7536
statvfs_result_desc.name = MODNAME ".statvfs_result";
7537
PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7538
#ifdef NEED_TICKS_PER_SECOND
7539
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7540
ticks_per_second = sysconf(_SC_CLK_TCK);
7542
ticks_per_second = HZ;
7544
ticks_per_second = 60; /* magic fallback value; may be bogus */
7548
Py_INCREF((PyObject*) &StatResultType);
7549
PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7550
Py_INCREF((PyObject*) &StatVFSResultType);
7551
PyModule_AddObject(m, "statvfs_result",
7552
(PyObject*) &StatVFSResultType);
7557
* Step 2 of weak-linking support on Mac OS X.
7559
* The code below removes functions that are not available on the
7560
* currently active platform.
7562
* This block allow one to use a python binary that was build on
7563
* OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7566
#ifdef HAVE_FSTATVFS
7567
if (fstatvfs == NULL) {
7568
if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7572
#endif /* HAVE_FSTATVFS */
7575
if (statvfs == NULL) {
7576
if (PyObject_DelAttrString(m, "statvfs") == -1) {
7580
#endif /* HAVE_STATVFS */
7583
if (lchown == NULL) {
7584
if (PyObject_DelAttrString(m, "lchown") == -1) {
7588
#endif /* HAVE_LCHOWN */
7591
#endif /* __APPLE__ */