blob: 2f7ca54b7dc90e2763dcb2e889e5008e62b59ef0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* POSIX module implementation */
33
Guido van Rossuma4916fa1996-05-23 22:58:55 +000034/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000035 actually calls itself 'nt', not 'posix', and a few functions are
36 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000037 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000038 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000039 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000040
Guido van Rossuma4916fa1996-05-23 22:58:55 +000041/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000043static char posix__doc__ [] =
44"This module provides access to operating system functionality that is\n\
45standardized by the C Standard and the POSIX standard (a thinly\n\
46disguised Unix interface). Refer to the library manual and\n\
47corresponding Unix manual entries for more information on calls.";
48
Barry Warsaw53699e91996-12-10 23:23:01 +000049#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000051#if defined(PYOS_OS2)
52#define INCL_DOS
53#define INCL_DOSERRORS
54#define INCL_DOSPROCESS
55#define INCL_NOPMAPI
56#include <os2.h>
57#endif
58
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#include <sys/types.h>
60#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000061#ifdef HAVE_SYS_WAIT_H
62#include <sys/wait.h> /* For WNOHANG */
63#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000064
Guido van Rossuma376cc51996-12-05 23:43:35 +000065#ifdef HAVE_SIGNAL_H
66#include <signal.h>
67#endif
68
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include "mytime.h" /* For clock_t on some systems */
70
71#ifdef HAVE_FCNTL_H
72#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000073#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Guido van Rossuma4916fa1996-05-23 22:58:55 +000075/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000076/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000077#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000078#include <process.h>
79#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000080#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000081#define HAVE_GETCWD 1
82#define HAVE_OPENDIR 1
83#define HAVE_SYSTEM 1
84#if defined(__OS2__)
85#define HAVE_EXECV 1
86#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000087#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#include <process.h>
89#else
90#ifdef __BORLANDC__ /* Borland compiler */
91#define HAVE_EXECV 1
92#define HAVE_GETCWD 1
93#define HAVE_GETEGID 1
94#define HAVE_GETEUID 1
95#define HAVE_GETGID 1
96#define HAVE_GETPPID 1
97#define HAVE_GETUID 1
98#define HAVE_KILL 1
99#define HAVE_OPENDIR 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
103#define HAVE_WAIT 1
104#else
105#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000106#define HAVE_GETCWD 1
107#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#define HAVE_EXECV 1
109#define HAVE_PIPE 1
110#define HAVE_POPEN 1
111#define HAVE_SYSTEM 1
112#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000113#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#else /* all other compilers */
115/* Unix functions that the configure script doesn't check for */
116#define HAVE_EXECV 1
117#define HAVE_FORK 1
118#define HAVE_GETCWD 1
119#define HAVE_GETEGID 1
120#define HAVE_GETEUID 1
121#define HAVE_GETGID 1
122#define HAVE_GETPPID 1
123#define HAVE_GETUID 1
124#define HAVE_KILL 1
125#define HAVE_OPENDIR 1
126#define HAVE_PIPE 1
127#define HAVE_POPEN 1
128#define HAVE_SYSTEM 1
129#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000130#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000131#endif /* _MSC_VER */
132#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000133#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000134#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000135
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000137
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000139#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000140#endif
141
142#ifdef NeXT
143/* NeXT's <unistd.h> and <utime.h> aren't worth much */
144#undef HAVE_UNISTD_H
145#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000146#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000147/* #undef HAVE_GETCWD */
148#endif
149
150#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000151/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
152extern int rename();
153extern int pclose();
154extern int lstat();
155extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#if defined(PYCC_VACPP)
158extern int mkdir Py_PROTO((char *));
159#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000160#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000161extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000163extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000165#endif
166#if defined(__IBMC__) || defined(__IBMCPP__)
167extern int chdir Py_PROTO((char *));
168extern int rmdir Py_PROTO((char *));
169#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000170extern int chdir Py_PROTO((const char *));
171extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000173extern int chmod Py_PROTO((const char *, mode_t));
174extern int chown Py_PROTO((const char *, uid_t, gid_t));
175extern char *getcwd Py_PROTO((char *, int));
176extern char *strerror Py_PROTO((int));
177extern int link Py_PROTO((const char *, const char *));
178extern int rename Py_PROTO((const char *, const char *));
179extern int stat Py_PROTO((const char *, struct stat *));
180extern int unlink Py_PROTO((const char *));
181extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000183extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000184#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000186extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000187#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000189
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#ifdef HAVE_UTIME_H
193#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000194#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000196#ifdef HAVE_SYS_UTIME_H
197#include <sys/utime.h>
198#define HAVE_UTIME_H /* pretend we do for the rest of this file */
199#endif /* HAVE_SYS_UTIME_H */
200
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#ifdef HAVE_SYS_TIMES_H
202#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000203#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204
205#ifdef HAVE_SYS_PARAM_H
206#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208
209#ifdef HAVE_SYS_UTSNAME_H
210#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000211#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212
213#ifndef MAXPATHLEN
214#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000219#define NAMLEN(dirent) strlen((dirent)->d_name)
220#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000221#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000222#include <direct.h>
223#define NAMLEN(dirent) strlen((dirent)->d_name)
224#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000226#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000227#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000228#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000230#endif
231#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#endif
234#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#endif
237#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <direct.h>
241#include <io.h>
242#include <process.h>
243#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000244#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000245#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000246#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000247#else /* 16-bit Windows */
248#include <dos.h>
249#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000250#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000251#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252
Guido van Rossumd48f2521997-12-05 22:19:34 +0000253#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000255#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256
257/* Return a dictionary corresponding to the POSIX environment table */
258
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000259#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262
Barry Warsaw53699e91996-12-10 23:23:01 +0000263static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264convertenviron()
265{
Barry Warsaw53699e91996-12-10 23:23:01 +0000266 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000268 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269 if (d == NULL)
270 return NULL;
271 if (environ == NULL)
272 return d;
273 /* XXX This part ignores errors */
274 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000275 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 char *p = strchr(*e, '=');
277 if (p == NULL)
278 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000279 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280 if (v == NULL)
281 continue;
282 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000283 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000285 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000287#if defined(PYOS_OS2)
288 {
289 APIRET rc;
290 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
291
292 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
293 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
294 PyObject *v = PyString_FromString(buffer);
295 PyDict_SetItemString(d, "BEGINLIBPATH", v);
296 Py_DECREF(v);
297 }
298 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
299 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
300 PyObject *v = PyString_FromString(buffer);
301 PyDict_SetItemString(d, "ENDLIBPATH", v);
302 Py_DECREF(v);
303 }
304 }
305#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 return d;
307}
308
309
Barry Warsaw53699e91996-12-10 23:23:01 +0000310static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311
312/* Set a POSIX-specific error from errno, and return NULL */
313
Barry Warsawd58d7641998-07-23 16:14:40 +0000314static PyObject *
315posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000316{
Barry Warsaw53699e91996-12-10 23:23:01 +0000317 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
Barry Warsawd58d7641998-07-23 16:14:40 +0000319static PyObject *
320posix_error_with_filename(name)
321 char* name;
322{
323 return PyErr_SetFromErrnoWithFilename(PosixError, name);
324}
325
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326
Guido van Rossumd48f2521997-12-05 22:19:34 +0000327#if defined(PYOS_OS2)
328/**********************************************************************
329 * Helper Function to Trim and Format OS/2 Messages
330 **********************************************************************/
331 static void
332os2_formatmsg(char *msgbuf, int msglen, char *reason)
333{
334 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
335
336 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
337 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
338
339 while (lastc > msgbuf && isspace(*lastc))
340 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
341 }
342
343 /* Add Optional Reason Text */
344 if (reason) {
345 strcat(msgbuf, " : ");
346 strcat(msgbuf, reason);
347 }
348}
349
350/**********************************************************************
351 * Decode an OS/2 Operating System Error Code
352 *
353 * A convenience function to lookup an OS/2 error code and return a
354 * text message we can use to raise a Python exception.
355 *
356 * Notes:
357 * The messages for errors returned from the OS/2 kernel reside in
358 * the file OSO001.MSG in the \OS2 directory hierarchy.
359 *
360 **********************************************************************/
361 static char *
362os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
363{
364 APIRET rc;
365 ULONG msglen;
366
367 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
368 Py_BEGIN_ALLOW_THREADS
369 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
370 errorcode, "oso001.msg", &msglen);
371 Py_END_ALLOW_THREADS
372
373 if (rc == NO_ERROR)
374 os2_formatmsg(msgbuf, msglen, reason);
375 else
376 sprintf(msgbuf, "unknown OS error #%d", errorcode);
377
378 return msgbuf;
379}
380
381/* Set an OS/2-specific error and return NULL. OS/2 kernel
382 errors are not in a global variable e.g. 'errno' nor are
383 they congruent with posix error numbers. */
384
385static PyObject * os2_error(int code)
386{
387 char text[1024];
388 PyObject *v;
389
390 os2_strerror(text, sizeof(text), code, "");
391
392 v = Py_BuildValue("(is)", code, text);
393 if (v != NULL) {
394 PyErr_SetObject(PosixError, v);
395 Py_DECREF(v);
396 }
397 return NULL; /* Signal to Python that an Exception is Pending */
398}
399
400#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401
402/* POSIX generic methods */
403
Barry Warsaw53699e91996-12-10 23:23:01 +0000404static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000405posix_int(args, func)
406 PyObject *args;
407 int (*func) Py_FPROTO((int));
408{
409 int fd;
410 int res;
411 if (!PyArg_Parse(args, "i", &fd))
412 return NULL;
413 Py_BEGIN_ALLOW_THREADS
414 res = (*func)(fd);
415 Py_END_ALLOW_THREADS
416 if (res < 0)
417 return posix_error();
418 Py_INCREF(Py_None);
419 return Py_None;
420}
421
422
423static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000425 PyObject *args;
426 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000428 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000429 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000430 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000432 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000433 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000434 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000435 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000436 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000437 Py_INCREF(Py_None);
438 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439}
440
Barry Warsaw53699e91996-12-10 23:23:01 +0000441static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000443 PyObject *args;
444 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000446 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000447 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000450 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000451 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000452 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000453 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000454 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000456 Py_INCREF(Py_None);
457 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458}
459
Barry Warsaw53699e91996-12-10 23:23:01 +0000460static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000462 PyObject *args;
463 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000465 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000468 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000470 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000471 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000472 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000473 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000474 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_INCREF(Py_None);
476 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Barry Warsaw53699e91996-12-10 23:23:01 +0000479static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000480posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 PyObject *args;
482 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000483{
484 char *path;
485 int i,i2;
486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000488 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000490 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000492 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000493 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 Py_INCREF(Py_None);
495 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000496}
497
Barry Warsaw53699e91996-12-10 23:23:01 +0000498static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000500 PyObject *self;
501 PyObject *args;
502 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503{
504 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000505 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000506 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000507 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000510 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000511 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000512 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000513 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000514#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000515 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000516 (long)st.st_mode,
517 (long)st.st_ino,
518 (long)st.st_dev,
519 (long)st.st_nlink,
520 (long)st.st_uid,
521 (long)st.st_gid,
522 (long)st.st_size,
523 (long)st.st_atime,
524 (long)st.st_mtime,
525 (long)st.st_ctime);
526#else
527 return Py_BuildValue("(lLllllLlll)",
528 (long)st.st_mode,
529 (LONG_LONG)st.st_ino,
530 (long)st.st_dev,
531 (long)st.st_nlink,
532 (long)st.st_uid,
533 (long)st.st_gid,
534 (LONG_LONG)st.st_size,
535 (long)st.st_atime,
536 (long)st.st_mtime,
537 (long)st.st_ctime);
538#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539}
540
541
542/* POSIX methods */
543
Guido van Rossum94f6f721999-01-06 18:42:14 +0000544static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000545"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000546Test for access to a file.";
547
548static PyObject *
549posix_access(self, args)
550 PyObject *self;
551 PyObject *args;
552{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000553 char *path;
554 int mode;
555 int res;
556
557 if (!PyArg_Parse(args, "(si)", &path, &mode))
558 return NULL;
559 Py_BEGIN_ALLOW_THREADS
560 res = access(path, mode);
561 Py_END_ALLOW_THREADS
562 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000563}
564
Guido van Rossumd371ff11999-01-25 16:12:23 +0000565#ifndef F_OK
566#define F_OK 0
567#endif
568#ifndef R_OK
569#define R_OK 4
570#endif
571#ifndef W_OK
572#define W_OK 2
573#endif
574#ifndef X_OK
575#define X_OK 1
576#endif
577
578#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000579static char posix_ttyname__doc__[] =
580"ttyname(fd, mode) -> String\n\
581Return the name of the terminal device connected to 'fd'.";
582
583static PyObject *
584posix_ttyname(self, args)
585 PyObject *self;
586 PyObject *args;
587{
588 PyObject *file;
589 int id;
590 char *ret;
591
Guido van Rossum94f6f721999-01-06 18:42:14 +0000592 if (!PyArg_Parse(args, "i", &id))
593 return NULL;
594
Guido van Rossum94f6f721999-01-06 18:42:14 +0000595 ret = ttyname(id);
596 if (ret == NULL)
597 return(posix_error());
598 return(PyString_FromString(ret));
599}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000600#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000602static char posix_chdir__doc__[] =
603"chdir(path) -> None\n\
604Change the current working directory to the specified path.";
605
Barry Warsaw53699e91996-12-10 23:23:01 +0000606static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000608 PyObject *self;
609 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611 return posix_1str(args, chdir);
612}
613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000614
615static char posix_chmod__doc__[] =
616"chmod(path, mode) -> None\n\
617Change the access permissions of a file.";
618
Barry Warsaw53699e91996-12-10 23:23:01 +0000619static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000621 PyObject *self;
622 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624 return posix_strint(args, chmod);
625}
626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000627
Guido van Rossum21142a01999-01-08 21:05:37 +0000628#ifdef HAVE_FSYNC
629static char posix_fsync__doc__[] =
630"fsync(fildes) -> None\n\
631force write of file with filedescriptor to disk.";
632
633static PyObject *
634posix_fsync(self, args)
635 PyObject *self;
636 PyObject *args;
637{
Guido van Rossum21142a01999-01-08 21:05:37 +0000638 return posix_int(args, fsync);
639}
640#endif /* HAVE_FSYNC */
641
642#ifdef HAVE_FDATASYNC
643static char posix_fdatasync__doc__[] =
644"fdatasync(fildes) -> None\n\
645force write of file with filedescriptor to disk.\n\
646 does not force update of metadata.";
647
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000648extern int fdatasync(int); /* Prototype just in case */
649
Guido van Rossum21142a01999-01-08 21:05:37 +0000650static PyObject *
651posix_fdatasync(self, args)
652 PyObject *self;
653 PyObject *args;
654{
Guido van Rossum21142a01999-01-08 21:05:37 +0000655 return posix_int(args, fdatasync);
656}
657#endif /* HAVE_FDATASYNC */
658
659
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000661static char posix_chown__doc__[] =
662"chown(path, uid, gid) -> None\n\
663Change the owner and group id of path to the numeric uid and gid.";
664
Barry Warsaw53699e91996-12-10 23:23:01 +0000665static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000666posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 PyObject *self;
668 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000669{
670 return posix_strintint(args, chown);
671}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000672#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000674
Guido van Rossum36bc6801995-06-14 22:54:23 +0000675#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000676static char posix_getcwd__doc__[] =
677"getcwd() -> path\n\
678Return a string representing the current working directory.";
679
Barry Warsaw53699e91996-12-10 23:23:01 +0000680static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000682 PyObject *self;
683 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684{
685 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000686 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000687 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000689 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000690 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000691 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000692 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000693 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000694 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000696#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000698
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000700static char posix_link__doc__[] =
701"link(src, dst) -> None\n\
702Create a hard link to a file.";
703
Barry Warsaw53699e91996-12-10 23:23:01 +0000704static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000706 PyObject *self;
707 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709 return posix_2str(args, link);
710}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000711#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000713
714static char posix_listdir__doc__[] =
715"listdir(path) -> list_of_strings\n\
716Return a list containing the names of the entries in the directory.\n\
717\n\
718 path: path of directory to list\n\
719\n\
720The list is in arbitrary order. It does not include the special\n\
721entries '.' and '..' even if they are present in the directory.";
722
Barry Warsaw53699e91996-12-10 23:23:01 +0000723static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000724posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 PyObject *self;
726 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000728 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000729 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000730#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000731
Guido van Rossumb6775db1994-08-01 11:34:53 +0000732 char *name;
733 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000734 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735 HANDLE hFindFile;
736 WIN32_FIND_DATA FileData;
737 char namebuf[MAX_PATH+5];
738
Guido van Rossum7e488981998-10-08 02:25:24 +0000739 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740 return NULL;
741 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000742 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743 return NULL;
744 }
745 strcpy(namebuf, name);
746 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
747 namebuf[len++] = '/';
748 strcpy(namebuf + len, "*.*");
749
Barry Warsaw53699e91996-12-10 23:23:01 +0000750 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 return NULL;
752
753 hFindFile = FindFirstFile(namebuf, &FileData);
754 if (hFindFile == INVALID_HANDLE_VALUE) {
755 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000756 if (errno == ERROR_FILE_NOT_FOUND)
757 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 return posix_error();
759 }
760 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000761 if (FileData.cFileName[0] == '.' &&
762 (FileData.cFileName[1] == '\0' ||
763 FileData.cFileName[1] == '.' &&
764 FileData.cFileName[2] == '\0'))
765 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000766 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000768 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 d = NULL;
770 break;
771 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000772 if (PyList_Append(d, v) != 0) {
773 Py_DECREF(v);
774 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 d = NULL;
776 break;
777 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000778 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000779 } while (FindNextFile(hFindFile, &FileData) == TRUE);
780
781 if (FindClose(hFindFile) == FALSE) {
782 errno = GetLastError();
783 return posix_error();
784 }
785
786 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000787
Guido van Rossum8d665e61996-06-26 18:22:49 +0000788#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000789#ifdef _MSC_VER /* 16-bit Windows */
790
791#ifndef MAX_PATH
792#define MAX_PATH 250
793#endif
794 char *name, *pt;
795 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000796 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000797 char namebuf[MAX_PATH+5];
798 struct _find_t ep;
799
Guido van Rossum7e488981998-10-08 02:25:24 +0000800 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000801 return NULL;
802 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000803 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000804 return NULL;
805 }
806 strcpy(namebuf, name);
807 for (pt = namebuf; *pt; pt++)
808 if (*pt == '/')
809 *pt = '\\';
810 if (namebuf[len-1] != '\\')
811 namebuf[len++] = '\\';
812 strcpy(namebuf + len, "*.*");
813
Barry Warsaw53699e91996-12-10 23:23:01 +0000814 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000815 return NULL;
816
817 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000818 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
819 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000820 errno = ENOENT;
821 return posix_error();
822 }
823 do {
824 if (ep.name[0] == '.' &&
825 (ep.name[1] == '\0' ||
826 ep.name[1] == '.' &&
827 ep.name[2] == '\0'))
828 continue;
829 strcpy(namebuf, ep.name);
830 for (pt = namebuf; *pt; pt++)
831 if (isupper(*pt))
832 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000834 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000835 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000836 d = NULL;
837 break;
838 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000839 if (PyList_Append(d, v) != 0) {
840 Py_DECREF(v);
841 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000842 d = NULL;
843 break;
844 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000845 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000846 } while (_dos_findnext(&ep) == 0);
847
848 return d;
849
850#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000851#if defined(PYOS_OS2)
852
853#ifndef MAX_PATH
854#define MAX_PATH CCHMAXPATH
855#endif
856 char *name, *pt;
857 int len;
858 PyObject *d, *v;
859 char namebuf[MAX_PATH+5];
860 HDIR hdir = 1;
861 ULONG srchcnt = 1;
862 FILEFINDBUF3 ep;
863 APIRET rc;
864
Guido van Rossum7e488981998-10-08 02:25:24 +0000865 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000866 return NULL;
867 if (len >= MAX_PATH) {
868 PyErr_SetString(PyExc_ValueError, "path too long");
869 return NULL;
870 }
871 strcpy(namebuf, name);
872 for (pt = namebuf; *pt; pt++)
873 if (*pt == '/')
874 *pt = '\\';
875 if (namebuf[len-1] != '\\')
876 namebuf[len++] = '\\';
877 strcpy(namebuf + len, "*.*");
878
879 if ((d = PyList_New(0)) == NULL)
880 return NULL;
881
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000882 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
883 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000884 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000885 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
886 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
887 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000888
889 if (rc != NO_ERROR) {
890 errno = ENOENT;
891 return posix_error();
892 }
893
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000894 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000895 do {
896 if (ep.achName[0] == '.'
897 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000898 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000899
900 strcpy(namebuf, ep.achName);
901
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000902 /* Leave Case of Name Alone -- In Native Form */
903 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000904
905 v = PyString_FromString(namebuf);
906 if (v == NULL) {
907 Py_DECREF(d);
908 d = NULL;
909 break;
910 }
911 if (PyList_Append(d, v) != 0) {
912 Py_DECREF(v);
913 Py_DECREF(d);
914 d = NULL;
915 break;
916 }
917 Py_DECREF(v);
918 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
919 }
920
921 return d;
922#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000923
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000924 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000925 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000927 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000928 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000930 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000931 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000932 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000933 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000934 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000935 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000937 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 return NULL;
939 }
940 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000941 if (ep->d_name[0] == '.' &&
942 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000943 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000944 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000945 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948 d = NULL;
949 break;
950 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000951 if (PyList_Append(d, v) != 0) {
952 Py_DECREF(v);
953 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954 d = NULL;
955 break;
956 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000957 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958 }
959 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000960 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000961
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000962 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000963
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000964#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000965#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000966#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967}
968
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000969static char posix_mkdir__doc__[] =
970"mkdir(path [, mode=0777]) -> None\n\
971Create a directory.";
972
Barry Warsaw53699e91996-12-10 23:23:01 +0000973static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000975 PyObject *self;
976 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000978 int res;
979 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000980 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000981 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000982 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000983 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000984#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000985 res = mkdir(path);
986#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000987 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000988#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000989 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000990 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000991 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000992 Py_INCREF(Py_None);
993 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994}
995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000996
Guido van Rossumb6775db1994-08-01 11:34:53 +0000997#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000998static char posix_nice__doc__[] =
999"nice(inc) -> new_priority\n\
1000Decrease the priority of process and return new priority.";
1001
Barry Warsaw53699e91996-12-10 23:23:01 +00001002static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001003posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 PyObject *self;
1005 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001006{
1007 int increment, value;
1008
Barry Warsaw53699e91996-12-10 23:23:01 +00001009 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001010 return NULL;
1011 value = nice(increment);
1012 if (value == -1)
1013 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001014 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001015}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001016#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001018
1019static char posix_rename__doc__[] =
1020"rename(old, new) -> None\n\
1021Rename a file or directory.";
1022
Barry Warsaw53699e91996-12-10 23:23:01 +00001023static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001024posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001025 PyObject *self;
1026 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001027{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001028 return posix_2str(args, rename);
1029}
1030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001031
1032static char posix_rmdir__doc__[] =
1033"rmdir(path) -> None\n\
1034Remove a directory.";
1035
Barry Warsaw53699e91996-12-10 23:23:01 +00001036static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001038 PyObject *self;
1039 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001041 return posix_1str(args, rmdir);
1042}
1043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001044
1045static char posix_stat__doc__[] =
1046"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1047Perform a stat system call on the given path.";
1048
Barry Warsaw53699e91996-12-10 23:23:01 +00001049static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001050posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001051 PyObject *self;
1052 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001053{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001054 return posix_do_stat(self, args, stat);
1055}
1056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001057
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001058#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001059static char posix_system__doc__[] =
1060"system(command) -> exit_status\n\
1061Execute the command (a string) in a subshell.";
1062
Barry Warsaw53699e91996-12-10 23:23:01 +00001063static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001064posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001065 PyObject *self;
1066 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001067{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001068 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001069 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001071 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001072 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001073 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001074 Py_END_ALLOW_THREADS
1075 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001077#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001079
1080static char posix_umask__doc__[] =
1081"umask(new_mask) -> old_mask\n\
1082Set the current numeric umask and return the previous umask.";
1083
Barry Warsaw53699e91996-12-10 23:23:01 +00001084static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001085posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001086 PyObject *self;
1087 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001088{
1089 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001090 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001091 return NULL;
1092 i = umask(i);
1093 if (i < 0)
1094 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001095 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096}
1097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001098
1099static char posix_unlink__doc__[] =
1100"unlink(path) -> None\n\
1101Remove a file (same as remove(path)).";
1102
1103static char posix_remove__doc__[] =
1104"remove(path) -> None\n\
1105Remove a file (same as unlink(path)).";
1106
Barry Warsaw53699e91996-12-10 23:23:01 +00001107static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001108posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001109 PyObject *self;
1110 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001111{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001112 return posix_1str(args, unlink);
1113}
1114
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001115
Guido van Rossumb6775db1994-08-01 11:34:53 +00001116#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001117static char posix_uname__doc__[] =
1118"uname() -> (sysname, nodename, release, version, machine)\n\
1119Return a tuple identifying the current operating system.";
1120
Barry Warsaw53699e91996-12-10 23:23:01 +00001121static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001122posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001123 PyObject *self;
1124 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001125{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001126 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001127 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001128 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001129 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001130 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001131 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001132 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001133 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001134 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001135 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001136 u.sysname,
1137 u.nodename,
1138 u.release,
1139 u.version,
1140 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001141}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001142#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001143
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001144
1145static char posix_utime__doc__[] =
1146"utime(path, (atime, utime)) -> None\n\
1147Set the access and modified time of the file to the given values.";
1148
Barry Warsaw53699e91996-12-10 23:23:01 +00001149static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001150posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001151 PyObject *self;
1152 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001153{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001154 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001155 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001156 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001157
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001158/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001159#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001160 struct utimbuf buf;
1161#define ATIME buf.actime
1162#define MTIME buf.modtime
1163#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001164#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001165 time_t buf[2];
1166#define ATIME buf[0]
1167#define MTIME buf[1]
1168#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001169#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001170
Barry Warsaw53699e91996-12-10 23:23:01 +00001171 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001173 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001174 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001175 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001176 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001177 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001178 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001179 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 Py_INCREF(Py_None);
1181 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001182#undef UTIME_ARG
1183#undef ATIME
1184#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001185}
1186
Guido van Rossum85e3b011991-06-03 12:42:10 +00001187
Guido van Rossum3b066191991-06-04 19:40:25 +00001188/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001189
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001190static char posix__exit__doc__[] =
1191"_exit(status)\n\
1192Exit to the system with specified status, without normal exit processing.";
1193
Barry Warsaw53699e91996-12-10 23:23:01 +00001194static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001195posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001196 PyObject *self;
1197 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001198{
1199 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001200 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001201 return NULL;
1202 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001203 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001204}
1205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001206
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001207#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001208static char posix_execv__doc__[] =
1209"execv(path, args)\n\
1210Execute an executable path with arguments, replacing current process.\n\
1211\n\
1212 path: path of executable file\n\
1213 args: tuple or list of strings";
1214
Barry Warsaw53699e91996-12-10 23:23:01 +00001215static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001216posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001217 PyObject *self;
1218 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001219{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001220 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001221 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001222 char **argvlist;
1223 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001224 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001225
Guido van Rossum89b33251993-10-22 14:26:06 +00001226 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001227 argv is a list or tuple of strings. */
1228
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001230 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001231 if (PyList_Check(argv)) {
1232 argc = PyList_Size(argv);
1233 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001234 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001235 else if (PyTuple_Check(argv)) {
1236 argc = PyTuple_Size(argv);
1237 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001238 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001239 else {
1240 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001241 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001242 return NULL;
1243 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244
Barry Warsaw53699e91996-12-10 23:23:01 +00001245 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001246 if (argvlist == NULL)
1247 return NULL;
1248 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001249 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1250 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001251 goto badarg;
1252 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001253 }
1254 argvlist[argc] = NULL;
1255
Guido van Rossumb6775db1994-08-01 11:34:53 +00001256#ifdef BAD_EXEC_PROTOTYPES
1257 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001258#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001259 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001260#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001261
Guido van Rossum85e3b011991-06-03 12:42:10 +00001262 /* If we get here it's definitely an error */
1263
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001265 return posix_error();
1266}
1267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001268
1269static char posix_execve__doc__[] =
1270"execve(path, args, env)\n\
1271Execute a path with arguments and environment, replacing current process.\n\
1272\n\
1273 path: path of executable file\n\
1274 args: tuple or list of arguments\n\
1275 env: dictonary of strings mapping to strings";
1276
Barry Warsaw53699e91996-12-10 23:23:01 +00001277static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001278posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001279 PyObject *self;
1280 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001281{
1282 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001283 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001284 char **argvlist;
1285 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001286 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001287 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001288 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001289
1290 /* execve has three arguments: (path, argv, env), where
1291 argv is a list or tuple of strings and env is a dictionary
1292 like posix.environ. */
1293
Barry Warsaw53699e91996-12-10 23:23:01 +00001294 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001295 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 if (PyList_Check(argv)) {
1297 argc = PyList_Size(argv);
1298 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001299 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001300 else if (PyTuple_Check(argv)) {
1301 argc = PyTuple_Size(argv);
1302 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001303 }
1304 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001306 return NULL;
1307 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001308 if (!PyMapping_Check(env)) {
1309 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001310 return NULL;
1311 }
1312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001314 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001316 return NULL;
1317 }
1318 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001320 "s;argv must be list of strings",
1321 &argvlist[i]))
1322 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001323 goto fail_1;
1324 }
1325 }
1326 argvlist[argc] = NULL;
1327
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001328 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001329 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001330 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001331 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001332 goto fail_1;
1333 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001334 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001335 keys = PyMapping_Keys(env);
1336 vals = PyMapping_Values(env);
1337 if (!keys || !vals)
1338 goto fail_2;
1339
1340 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001341 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001342
1343 key = PyList_GetItem(keys, pos);
1344 val = PyList_GetItem(vals, pos);
1345 if (!key || !val)
1346 goto fail_2;
1347
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001349 !PyArg_Parse(val, "s;non-string value in env", &v))
1350 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 goto fail_2;
1352 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001353
1354#if defined(PYOS_OS2)
1355 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1356 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1357#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001358 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001359 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001360 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001361 goto fail_2;
1362 }
1363 sprintf(p, "%s=%s", k, v);
1364 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001365#if defined(PYOS_OS2)
1366 }
1367#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001368 }
1369 envlist[envc] = 0;
1370
Guido van Rossumb6775db1994-08-01 11:34:53 +00001371
1372#ifdef BAD_EXEC_PROTOTYPES
1373 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001374#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001375 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001376#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001377
1378 /* If we get here it's definitely an error */
1379
1380 (void) posix_error();
1381
1382 fail_2:
1383 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001384 PyMem_DEL(envlist[envc]);
1385 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001386 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001388 Py_XDECREF(vals);
1389 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001390 return NULL;
1391}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001392#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001394
Guido van Rossumad0ee831995-03-01 10:34:45 +00001395#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001396static char posix_fork__doc__[] =
1397"fork() -> pid\n\
1398Fork a child process.\n\
1399\n\
1400Return 0 to child process and PID of child to parent process.";
1401
Barry Warsaw53699e91996-12-10 23:23:01 +00001402static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001403posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001404 PyObject *self;
1405 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001406{
1407 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001408 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001409 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001410 pid = fork();
1411 if (pid == -1)
1412 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001413 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001414 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001415}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001416#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001418
Guido van Rossumad0ee831995-03-01 10:34:45 +00001419#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001420static char posix_getegid__doc__[] =
1421"getegid() -> egid\n\
1422Return the current process's effective group id.";
1423
Barry Warsaw53699e91996-12-10 23:23:01 +00001424static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001425posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001426 PyObject *self;
1427 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001428{
Barry Warsaw53699e91996-12-10 23:23:01 +00001429 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001430 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001431 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001432}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001433#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001434
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001435
Guido van Rossumad0ee831995-03-01 10:34:45 +00001436#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001437static char posix_geteuid__doc__[] =
1438"geteuid() -> euid\n\
1439Return the current process's effective user id.";
1440
Barry Warsaw53699e91996-12-10 23:23:01 +00001441static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001442posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001443 PyObject *self;
1444 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001445{
Barry Warsaw53699e91996-12-10 23:23:01 +00001446 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001447 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001448 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001449}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001450#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001452
Guido van Rossumad0ee831995-03-01 10:34:45 +00001453#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001454static char posix_getgid__doc__[] =
1455"getgid() -> gid\n\
1456Return the current process's group id.";
1457
Barry Warsaw53699e91996-12-10 23:23:01 +00001458static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001459posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001460 PyObject *self;
1461 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001462{
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001464 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001465 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001466}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001467#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001469
1470static char posix_getpid__doc__[] =
1471"getpid() -> pid\n\
1472Return the current process id";
1473
Barry Warsaw53699e91996-12-10 23:23:01 +00001474static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001475posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001476 PyObject *self;
1477 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001478{
Barry Warsaw53699e91996-12-10 23:23:01 +00001479 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001480 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001481 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001482}
1483
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001484
Guido van Rossumb6775db1994-08-01 11:34:53 +00001485#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001486static char posix_getpgrp__doc__[] =
1487"getpgrp() -> pgrp\n\
1488Return the current process group id.";
1489
Barry Warsaw53699e91996-12-10 23:23:01 +00001490static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001491posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001492 PyObject *self;
1493 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001494{
Barry Warsaw53699e91996-12-10 23:23:01 +00001495 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001496 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001497#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001498 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001499#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001500 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001501#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001502}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001503#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001504
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001505
Guido van Rossumb6775db1994-08-01 11:34:53 +00001506#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001507static char posix_setpgrp__doc__[] =
1508"setpgrp() -> None\n\
1509Make this process a session leader.";
1510
Barry Warsaw53699e91996-12-10 23:23:01 +00001511static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001512posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001513 PyObject *self;
1514 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001515{
Barry Warsaw53699e91996-12-10 23:23:01 +00001516 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001517 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001518#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001519 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001520#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001521 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001522#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001523 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001524 Py_INCREF(Py_None);
1525 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001526}
1527
Guido van Rossumb6775db1994-08-01 11:34:53 +00001528#endif /* HAVE_SETPGRP */
1529
Guido van Rossumad0ee831995-03-01 10:34:45 +00001530#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001531static char posix_getppid__doc__[] =
1532"getppid() -> ppid\n\
1533Return the parent's process id.";
1534
Barry Warsaw53699e91996-12-10 23:23:01 +00001535static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001536posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001537 PyObject *self;
1538 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001539{
Barry Warsaw53699e91996-12-10 23:23:01 +00001540 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001541 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001542 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001543}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001544#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001545
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001546
Guido van Rossumad0ee831995-03-01 10:34:45 +00001547#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001548static char posix_getuid__doc__[] =
1549"getuid() -> uid\n\
1550Return the current process's user id.";
1551
Barry Warsaw53699e91996-12-10 23:23:01 +00001552static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001553posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001554 PyObject *self;
1555 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001556{
Barry Warsaw53699e91996-12-10 23:23:01 +00001557 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001558 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001559 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001560}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001561#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001563
Guido van Rossumad0ee831995-03-01 10:34:45 +00001564#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001565static char posix_kill__doc__[] =
1566"kill(pid, sig) -> None\n\
1567Kill a process with a signal.";
1568
Barry Warsaw53699e91996-12-10 23:23:01 +00001569static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001570posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001571 PyObject *self;
1572 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001573{
1574 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001575 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001576 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001577#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001578 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1579 APIRET rc;
1580 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001581 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001582
1583 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1584 APIRET rc;
1585 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001586 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001587
1588 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001589 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001590#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001591 if (kill(pid, sig) == -1)
1592 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001593#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001594 Py_INCREF(Py_None);
1595 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001596}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001597#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001598
Guido van Rossumc0125471996-06-28 18:55:32 +00001599#ifdef HAVE_PLOCK
1600
1601#ifdef HAVE_SYS_LOCK_H
1602#include <sys/lock.h>
1603#endif
1604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001605static char posix_plock__doc__[] =
1606"plock(op) -> None\n\
1607Lock program segments into memory.";
1608
Barry Warsaw53699e91996-12-10 23:23:01 +00001609static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001610posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001611 PyObject *self;
1612 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001613{
1614 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001615 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001616 return NULL;
1617 if (plock(op) == -1)
1618 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001619 Py_INCREF(Py_None);
1620 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001621}
1622#endif
1623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001624
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001625#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001626static char posix_popen__doc__[] =
1627"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1628Open a pipe to/from a command returning a file object.";
1629
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001630#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001631static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001632async_system(const char *command)
1633{
1634 char *p, errormsg[256], args[1024];
1635 RESULTCODES rcodes;
1636 APIRET rc;
1637 char *shell = getenv("COMSPEC");
1638 if (!shell)
1639 shell = "cmd";
1640
1641 strcpy(args, shell);
1642 p = &args[ strlen(args)+1 ];
1643 strcpy(p, "/c ");
1644 strcat(p, command);
1645 p += strlen(p) + 1;
1646 *p = '\0';
1647
1648 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001649 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001650 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001651 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001652 &rcodes, shell);
1653 return rc;
1654}
1655
Guido van Rossumd48f2521997-12-05 22:19:34 +00001656static FILE *
1657popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001658{
1659 HFILE rhan, whan;
1660 FILE *retfd = NULL;
1661 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1662
Guido van Rossumd48f2521997-12-05 22:19:34 +00001663 if (rc != NO_ERROR) {
1664 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001665 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001666 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001667
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001668 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1669 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001670
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001671 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1672 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001673
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001674 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1675 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001676
1677 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001678 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001679 }
1680
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001681 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1682 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001683
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001684 close(oldfd); /* And Close Saved STDOUT Handle */
1685 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001686
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001687 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1688 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001689
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001690 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1691 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001692
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001693 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1694 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001695
1696 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001697 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001698 }
1699
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001700 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1701 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001702
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001703 close(oldfd); /* And Close Saved STDIN Handle */
1704 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001705
Guido van Rossumd48f2521997-12-05 22:19:34 +00001706 } else {
1707 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001708 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001709 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001710}
1711
1712static PyObject *
1713posix_popen(self, args)
1714 PyObject *self;
1715 PyObject *args;
1716{
1717 char *name;
1718 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001719 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001720 FILE *fp;
1721 PyObject *f;
1722 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1723 return NULL;
1724 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001725 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001726 Py_END_ALLOW_THREADS
1727 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001728 return os2_error(err);
1729
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001730 f = PyFile_FromFile(fp, name, mode, fclose);
1731 if (f != NULL)
1732 PyFile_SetBufSize(f, bufsize);
1733 return f;
1734}
1735
1736#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001737static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001738posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001739 PyObject *self;
1740 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001741{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001742 char *name;
1743 char *mode = "r";
1744 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001745 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001746 PyObject *f;
1747 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001748 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001749 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001750 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001751 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001752 if (fp == NULL)
1753 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001755 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001756 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001757 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001758}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001759#endif
1760
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001761#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001763
Guido van Rossumb6775db1994-08-01 11:34:53 +00001764#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001765static char posix_setuid__doc__[] =
1766"setuid(uid) -> None\n\
1767Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001768static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001769posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001770 PyObject *self;
1771 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001772{
1773 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001774 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001775 return NULL;
1776 if (setuid(uid) < 0)
1777 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001778 Py_INCREF(Py_None);
1779 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001780}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001781#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001783
Guido van Rossumb6775db1994-08-01 11:34:53 +00001784#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001785static char posix_setgid__doc__[] =
1786"setgid(gid) -> None\n\
1787Set the current process's group id.";
1788
Barry Warsaw53699e91996-12-10 23:23:01 +00001789static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001790posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 PyObject *self;
1792 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001793{
1794 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001795 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001796 return NULL;
1797 if (setgid(gid) < 0)
1798 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001799 Py_INCREF(Py_None);
1800 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001801}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001802#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001804
Guido van Rossumb6775db1994-08-01 11:34:53 +00001805#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001806static char posix_waitpid__doc__[] =
1807"waitpid(pid, options) -> (pid, status)\n\
1808Wait for completion of a give child process.";
1809
Barry Warsaw53699e91996-12-10 23:23:01 +00001810static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001811posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 PyObject *self;
1813 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001814{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001815 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001817 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001818 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001819#ifdef NeXT
1820 pid = wait4(pid, (union wait *)&sts, options, NULL);
1821#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001822 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001823#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001824 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001825 if (pid == -1)
1826 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001827 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001828 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001829}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001830#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001832
Guido van Rossumad0ee831995-03-01 10:34:45 +00001833#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834static char posix_wait__doc__[] =
1835"wait() -> (pid, status)\n\
1836Wait for completion of a child process.";
1837
Barry Warsaw53699e91996-12-10 23:23:01 +00001838static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001839posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001840 PyObject *self;
1841 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001842{
1843 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001844 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001845#ifdef NeXT
1846 pid = wait((union wait *)&sts);
1847#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001848 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001849#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001850 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001851 if (pid == -1)
1852 return posix_error();
1853 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001854 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001855}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001856#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001857
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001858
1859static char posix_lstat__doc__[] =
1860"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1861Like stat(path), but do not follow symbolic links.";
1862
Barry Warsaw53699e91996-12-10 23:23:01 +00001863static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001864posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001865 PyObject *self;
1866 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001867{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001868#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001870#else /* !HAVE_LSTAT */
1871 return posix_do_stat(self, args, stat);
1872#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873}
1874
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001875
Guido van Rossumb6775db1994-08-01 11:34:53 +00001876#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001877static char posix_readlink__doc__[] =
1878"readlink(path) -> path\n\
1879Return a string representing the path to which the symbolic link points.";
1880
Barry Warsaw53699e91996-12-10 23:23:01 +00001881static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001882posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 PyObject *self;
1884 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001886 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001887 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001888 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001889 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001890 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001892 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001893 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001895 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001896 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001897}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001900
Guido van Rossumb6775db1994-08-01 11:34:53 +00001901#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001902static char posix_symlink__doc__[] =
1903"symlink(src, dst) -> None\n\
1904Create a symbolic link.";
1905
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001906static PyObject *
1907posix_symlink(self, args)
1908 PyObject *self;
1909 PyObject *args;
1910{
1911 return posix_2str(args, symlink);
1912}
1913#endif /* HAVE_SYMLINK */
1914
1915
1916#ifdef HAVE_TIMES
1917#ifndef HZ
1918#define HZ 60 /* Universal constant :-) */
1919#endif /* HZ */
1920
Guido van Rossumd48f2521997-12-05 22:19:34 +00001921#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1922static long
1923system_uptime()
1924{
1925 ULONG value = 0;
1926
1927 Py_BEGIN_ALLOW_THREADS
1928 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1929 Py_END_ALLOW_THREADS
1930
1931 return value;
1932}
1933
1934static PyObject *
1935posix_times(self, args)
1936 PyObject *self;
1937 PyObject *args;
1938{
1939 if (!PyArg_NoArgs(args))
1940 return NULL;
1941
1942 /* Currently Only Uptime is Provided -- Others Later */
1943 return Py_BuildValue("ddddd",
1944 (double)0 /* t.tms_utime / HZ */,
1945 (double)0 /* t.tms_stime / HZ */,
1946 (double)0 /* t.tms_cutime / HZ */,
1947 (double)0 /* t.tms_cstime / HZ */,
1948 (double)system_uptime() / 1000);
1949}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001950#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001951static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001952posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 PyObject *self;
1954 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001955{
1956 struct tms t;
1957 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001958 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001959 return NULL;
1960 errno = 0;
1961 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001962 if (c == (clock_t) -1)
1963 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001964 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001965 (double)t.tms_utime / HZ,
1966 (double)t.tms_stime / HZ,
1967 (double)t.tms_cutime / HZ,
1968 (double)t.tms_cstime / HZ,
1969 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001970}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001971#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001972#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001973
1974
Guido van Rossum87755a21996-09-07 00:59:43 +00001975#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001976#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001977static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001978posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001979 PyObject *self;
1980 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001981{
1982 FILETIME create, exit, kernel, user;
1983 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001984 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001985 return NULL;
1986 hProc = GetCurrentProcess();
1987 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001988 return Py_BuildValue(
1989 "ddddd",
1990 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1991 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1992 (double)0,
1993 (double)0,
1994 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001995}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001996#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001997
1998#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001999static char posix_times__doc__[] =
2000"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2001Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002002#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002004
Guido van Rossumb6775db1994-08-01 11:34:53 +00002005#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002006static char posix_setsid__doc__[] =
2007"setsid() -> None\n\
2008Call the system call setsid().";
2009
Barry Warsaw53699e91996-12-10 23:23:01 +00002010static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002011posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002012 PyObject *self;
2013 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002014{
Barry Warsaw53699e91996-12-10 23:23:01 +00002015 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002016 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002017 if (setsid() < 0)
2018 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 Py_INCREF(Py_None);
2020 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002021}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002022#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002023
Guido van Rossumb6775db1994-08-01 11:34:53 +00002024#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002025static char posix_setpgid__doc__[] =
2026"setpgid(pid, pgrp) -> None\n\
2027Call the system call setpgid().";
2028
Barry Warsaw53699e91996-12-10 23:23:01 +00002029static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002030posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002031 PyObject *self;
2032 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002033{
2034 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002035 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002036 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002037 if (setpgid(pid, pgrp) < 0)
2038 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002039 Py_INCREF(Py_None);
2040 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002041}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002042#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002044
Guido van Rossumb6775db1994-08-01 11:34:53 +00002045#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002046static char posix_tcgetpgrp__doc__[] =
2047"tcgetpgrp(fd) -> pgid\n\
2048Return the process group associated with the terminal given by a fd.";
2049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002051posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002052 PyObject *self;
2053 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002054{
2055 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002056 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002057 return NULL;
2058 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002059 if (pgid < 0)
2060 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002061 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002062}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002063#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002064
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002065
Guido van Rossumb6775db1994-08-01 11:34:53 +00002066#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067static char posix_tcsetpgrp__doc__[] =
2068"tcsetpgrp(fd, pgid) -> None\n\
2069Set the process group associated with the terminal given by a fd.";
2070
Barry Warsaw53699e91996-12-10 23:23:01 +00002071static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002072posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002073 PyObject *self;
2074 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002075{
2076 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002077 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002078 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002079 if (tcsetpgrp(fd, pgid) < 0)
2080 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002081 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002082 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002083}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002084#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002085
Guido van Rossum687dd131993-05-17 08:34:16 +00002086/* Functions acting on file descriptors */
2087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088static char posix_open__doc__[] =
2089"open(filename, flag [, mode=0777]) -> fd\n\
2090Open a file (for low level IO).";
2091
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002093posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *self;
2095 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002096{
2097 char *file;
2098 int flag;
2099 int mode = 0777;
2100 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002101 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2102 return NULL;
2103
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002105 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002106 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002107 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002108 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002109 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002110}
2111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002112
2113static char posix_close__doc__[] =
2114"close(fd) -> None\n\
2115Close a file descriptor (for low level IO).";
2116
Barry Warsaw53699e91996-12-10 23:23:01 +00002117static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002118posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002119 PyObject *self;
2120 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002121{
2122 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002123 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002124 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002125 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002126 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002127 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002128 if (res < 0)
2129 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002130 Py_INCREF(Py_None);
2131 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002132}
2133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002134
2135static char posix_dup__doc__[] =
2136"dup(fd) -> fd2\n\
2137Return a duplicate of a file descriptor.";
2138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002140posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 PyObject *self;
2142 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002143{
2144 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002146 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002147 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002148 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002149 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002150 if (fd < 0)
2151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002152 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002153}
2154
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002155
2156static char posix_dup2__doc__[] =
2157"dup2(fd, fd2) -> None\n\
2158Duplicate file descriptor.";
2159
Barry Warsaw53699e91996-12-10 23:23:01 +00002160static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002161posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 PyObject *self;
2163 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002164{
2165 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002166 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002167 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002168 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002169 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002170 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002171 if (res < 0)
2172 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002173 Py_INCREF(Py_None);
2174 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002175}
2176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002177
2178static char posix_lseek__doc__[] =
2179"lseek(fd, pos, how) -> newpos\n\
2180Set the current position of a file descriptor.";
2181
Barry Warsaw53699e91996-12-10 23:23:01 +00002182static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002183posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002184 PyObject *self;
2185 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002186{
2187 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002188 off_t pos, res;
2189 PyObject *posobj;
2190 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002191 return NULL;
2192#ifdef SEEK_SET
2193 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2194 switch (how) {
2195 case 0: how = SEEK_SET; break;
2196 case 1: how = SEEK_CUR; break;
2197 case 2: how = SEEK_END; break;
2198 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002199#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002200
2201#if !defined(HAVE_LARGEFILE_SUPPORT)
2202 pos = PyInt_AsLong(posobj);
2203#else
2204 pos = PyLong_Check(posobj) ?
2205 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2206#endif
2207 if (PyErr_Occurred())
2208 return NULL;
2209
Barry Warsaw53699e91996-12-10 23:23:01 +00002210 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002211 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002212 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002213 if (res < 0)
2214 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002215
2216#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002217 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002218#else
2219 return PyLong_FromLongLong(res);
2220#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002221}
2222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002223
2224static char posix_read__doc__[] =
2225"read(fd, buffersize) -> string\n\
2226Read a file descriptor.";
2227
Barry Warsaw53699e91996-12-10 23:23:01 +00002228static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002229posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 PyObject *self;
2231 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002232{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002233 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002234 PyObject *buffer;
2235 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002236 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002238 if (buffer == NULL)
2239 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 Py_BEGIN_ALLOW_THREADS
2241 n = read(fd, PyString_AsString(buffer), size);
2242 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002243 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002244 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002245 return posix_error();
2246 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002247 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002248 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002249 return buffer;
2250}
2251
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002252
2253static char posix_write__doc__[] =
2254"write(fd, string) -> byteswritten\n\
2255Write a string to a file descriptor.";
2256
Barry Warsaw53699e91996-12-10 23:23:01 +00002257static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002258posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002259 PyObject *self;
2260 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002261{
2262 int fd, size;
2263 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002265 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002266 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002267 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002268 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002269 if (size < 0)
2270 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002272}
2273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002274
2275static char posix_fstat__doc__[]=
2276"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2277Like stat(), but for an open file descriptor.";
2278
Barry Warsaw53699e91996-12-10 23:23:01 +00002279static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002280posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002281 PyObject *self;
2282 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002283{
2284 int fd;
2285 struct stat st;
2286 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002287 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002288 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002289 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002290 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002291 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002292 if (res != 0)
2293 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002294#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002295 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002296 (long)st.st_mode,
2297 (long)st.st_ino,
2298 (long)st.st_dev,
2299 (long)st.st_nlink,
2300 (long)st.st_uid,
2301 (long)st.st_gid,
2302 (long)st.st_size,
2303 (long)st.st_atime,
2304 (long)st.st_mtime,
2305 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002306#else
2307 return Py_BuildValue("(lLllllLlll)",
2308 (long)st.st_mode,
2309 (LONG_LONG)st.st_ino,
2310 (long)st.st_dev,
2311 (long)st.st_nlink,
2312 (long)st.st_uid,
2313 (long)st.st_gid,
2314 (LONG_LONG)st.st_size,
2315 (long)st.st_atime,
2316 (long)st.st_mtime,
2317 (long)st.st_ctime);
2318#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002319}
2320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002321
2322static char posix_fdopen__doc__[] =
2323"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2324Return an open file object connected to a file descriptor.";
2325
Barry Warsaw53699e91996-12-10 23:23:01 +00002326static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002327posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002328 PyObject *self;
2329 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002330{
Barry Warsaw53699e91996-12-10 23:23:01 +00002331 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002332 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002333 char *mode = "r";
2334 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002335 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 PyObject *f;
2337 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002338 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002339
Barry Warsaw53699e91996-12-10 23:23:01 +00002340 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002341 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002342 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002343 if (fp == NULL)
2344 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002345 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002346 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002347 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002348 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002349}
2350
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002351
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002352#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002353static char posix_pipe__doc__[] =
2354"pipe() -> (read_end, write_end)\n\
2355Create a pipe.";
2356
Barry Warsaw53699e91996-12-10 23:23:01 +00002357static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002358posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002359 PyObject *self;
2360 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002361{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002362#if defined(PYOS_OS2)
2363 HFILE read, write;
2364 APIRET rc;
2365
2366 if (!PyArg_Parse(args, ""))
2367 return NULL;
2368
2369 Py_BEGIN_ALLOW_THREADS
2370 rc = DosCreatePipe( &read, &write, 4096);
2371 Py_END_ALLOW_THREADS
2372 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002373 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002374
2375 return Py_BuildValue("(ii)", read, write);
2376#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002377#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002378 int fds[2];
2379 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002381 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002382 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002383 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002385 if (res != 0)
2386 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002387 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002388#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002389 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002390 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002391 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002392 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002393 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002394 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002395 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002396 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002397 if (!ok)
2398 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002399 read_fd = _open_osfhandle((long)read, 0);
2400 write_fd = _open_osfhandle((long)write, 1);
2401 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002402#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002403#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002404}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002405#endif /* HAVE_PIPE */
2406
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002407
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002408#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002409static char posix_mkfifo__doc__[] =
2410"mkfifo(file, [, mode=0666]) -> None\n\
2411Create a FIFO (a POSIX named pipe).";
2412
Barry Warsaw53699e91996-12-10 23:23:01 +00002413static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002414posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002415 PyObject *self;
2416 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002417{
2418 char *file;
2419 int mode = 0666;
2420 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002421 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002422 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002423 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002424 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002425 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002426 if (res < 0)
2427 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002428 Py_INCREF(Py_None);
2429 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002430}
2431#endif
2432
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002433
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002434#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002435static char posix_ftruncate__doc__[] =
2436"ftruncate(fd, length) -> None\n\
2437Truncate a file to a specified length.";
2438
Barry Warsaw53699e91996-12-10 23:23:01 +00002439static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002440posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 PyObject *self; /* Not used */
2442 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002443{
2444 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002445 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002446 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002447 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002448
Guido van Rossum94f6f721999-01-06 18:42:14 +00002449 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2450 return NULL;
2451
2452#if !defined(HAVE_LARGEFILE_SUPPORT)
2453 length = PyInt_AsLong(lenobj);
2454#else
2455 length = PyLong_Check(lenobj) ?
2456 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2457#endif
2458 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002459 return NULL;
2460
Barry Warsaw53699e91996-12-10 23:23:01 +00002461 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002462 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002463 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002464 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002465 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002466 return NULL;
2467 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002468 Py_INCREF(Py_None);
2469 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002470}
2471#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002472
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002473#ifdef NeXT
2474#define HAVE_PUTENV
2475/* Steve Spicklemire got this putenv from NeXTAnswers */
2476static int
2477putenv(char *newval)
2478{
2479 extern char **environ;
2480
2481 static int firstTime = 1;
2482 char **ep;
2483 char *cp;
2484 int esiz;
2485 char *np;
2486
2487 if (!(np = strchr(newval, '=')))
2488 return 1;
2489 *np = '\0';
2490
2491 /* look it up */
2492 for (ep=environ ; *ep ; ep++)
2493 {
2494 /* this should always be true... */
2495 if (cp = strchr(*ep, '='))
2496 {
2497 *cp = '\0';
2498 if (!strcmp(*ep, newval))
2499 {
2500 /* got it! */
2501 *cp = '=';
2502 break;
2503 }
2504 *cp = '=';
2505 }
2506 else
2507 {
2508 *np = '=';
2509 return 1;
2510 }
2511 }
2512
2513 *np = '=';
2514 if (*ep)
2515 {
2516 /* the string was already there:
2517 just replace it with the new one */
2518 *ep = newval;
2519 return 0;
2520 }
2521
2522 /* expand environ by one */
2523 for (esiz=2, ep=environ ; *ep ; ep++)
2524 esiz++;
2525 if (firstTime)
2526 {
2527 char **epp;
2528 char **newenv;
2529 if (!(newenv = malloc(esiz * sizeof(char *))))
2530 return 1;
2531
2532 for (ep=environ, epp=newenv ; *ep ;)
2533 *epp++ = *ep++;
2534 *epp++ = newval;
2535 *epp = (char *) 0;
2536 environ = newenv;
2537 }
2538 else
2539 {
2540 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2541 return 1;
2542 environ[esiz - 2] = newval;
2543 environ[esiz - 1] = (char *) 0;
2544 firstTime = 0;
2545 }
2546
2547 return 0;
2548}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002549#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002550
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002552#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553static char posix_putenv__doc__[] =
2554"putenv(key, value) -> None\n\
2555Change or add an environment variable.";
2556
Guido van Rossumbcc20741998-08-04 22:53:56 +00002557#ifdef __BEOS__
2558/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2559int putenv( const char *str );
2560#endif
2561
Barry Warsaw53699e91996-12-10 23:23:01 +00002562static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002563posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 PyObject *self;
2565 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002566{
2567 char *s1, *s2;
2568 char *new;
2569
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002571 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002572
2573#if defined(PYOS_OS2)
2574 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2575 APIRET rc;
2576
2577 if (strlen(s2) == 0) /* If New Value is an Empty String */
2578 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2579
2580 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2581 if (rc != NO_ERROR)
2582 return os2_error(rc);
2583
2584 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2585 APIRET rc;
2586
2587 if (strlen(s2) == 0) /* If New Value is an Empty String */
2588 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2589
2590 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2591 if (rc != NO_ERROR)
2592 return os2_error(rc);
2593 } else {
2594#endif
2595
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002596 /* XXX This leaks memory -- not easy to fix :-( */
2597 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002598 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002599 (void) sprintf(new, "%s=%s", s1, s2);
2600 if (putenv(new)) {
2601 posix_error();
2602 return NULL;
2603 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002604
2605#if defined(PYOS_OS2)
2606 }
2607#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002608 Py_INCREF(Py_None);
2609 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002610}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002611#endif /* putenv */
2612
2613#ifdef HAVE_STRERROR
2614static char posix_strerror__doc__[] =
2615"strerror(code) -> string\n\
2616Translate an error code to a message string.";
2617
2618PyObject *
2619posix_strerror(self, args)
2620 PyObject *self;
2621 PyObject *args;
2622{
2623 int code;
2624 char *message;
2625 if (!PyArg_ParseTuple(args, "i", &code))
2626 return NULL;
2627 message = strerror(code);
2628 if (message == NULL) {
2629 PyErr_SetString(PyExc_ValueError,
2630 "strerror code out of range");
2631 return NULL;
2632 }
2633 return PyString_FromString(message);
2634}
2635#endif /* strerror */
2636
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002637
Guido van Rossumc9641791998-08-04 15:26:23 +00002638#ifdef HAVE_SYS_WAIT_H
2639
2640#ifdef WIFSTOPPED
2641static char posix_WIFSTOPPED__doc__[] =
2642"WIFSTOPPED(status) -> Boolean\n\
2643See Unix documentation.";
2644
2645static PyObject *
2646posix_WIFSTOPPED(self, args)
2647 PyObject *self;
2648 PyObject *args;
2649{
2650 int status = 0;
2651
2652 if (!PyArg_Parse(args, "i", &status))
2653 {
2654 return NULL;
2655 }
2656
2657 return Py_BuildValue("i", WIFSTOPPED(status));
2658}
2659#endif /* WIFSTOPPED */
2660
2661#ifdef WIFSIGNALED
2662static char posix_WIFSIGNALED__doc__[] =
2663"WIFSIGNALED(status) -> Boolean\n\
2664See Unix documentation.";
2665
2666static PyObject *
2667posix_WIFSIGNALED(self, args)
2668 PyObject *self;
2669 PyObject *args;
2670{
2671 int status = 0;
2672
2673 if (!PyArg_Parse(args, "i", &status))
2674 {
2675 return NULL;
2676 }
2677
2678 return Py_BuildValue("i", WIFSIGNALED(status));
2679}
2680#endif /* WIFSIGNALED */
2681
2682#ifdef WIFEXITED
2683static char posix_WIFEXITED__doc__[] =
2684"WIFEXITED(status) -> Boolean\n\
2685See Unix documentation.";
2686
2687static PyObject *
2688posix_WIFEXITED(self, args)
2689 PyObject *self;
2690 PyObject *args;
2691{
2692 int status = 0;
2693
2694 if (!PyArg_Parse(args, "i", &status))
2695 {
2696 return NULL;
2697 }
2698
2699 return Py_BuildValue("i", WIFEXITED(status));
2700}
2701#endif /* WIFEXITED */
2702
2703#ifdef WIFSTOPPED
2704static char posix_WEXITSTATUS__doc__[] =
2705"WEXITSTATUS(status) -> integer\n\
2706See Unix documentation.";
2707
2708static PyObject *
2709posix_WEXITSTATUS(self, args)
2710 PyObject *self;
2711 PyObject *args;
2712{
2713 int status = 0;
2714
2715 if (!PyArg_Parse(args, "i", &status))
2716 {
2717 return NULL;
2718 }
2719
2720 return Py_BuildValue("i", WEXITSTATUS(status));
2721}
2722#endif /* WEXITSTATUS */
2723
2724#ifdef WTERMSIG
2725static char posix_WTERMSIG__doc__[] =
2726"WTERMSIG(status) -> integer\n\
2727See Unix documentation.";
2728
2729static PyObject *
2730posix_WTERMSIG(self, args)
2731 PyObject *self;
2732 PyObject *args;
2733{
2734 int status = 0;
2735
2736 if (!PyArg_Parse(args, "i", &status))
2737 {
2738 return NULL;
2739 }
2740
2741 return Py_BuildValue("i", WTERMSIG(status));
2742}
2743#endif /* WTERMSIG */
2744
2745#ifdef WSTOPSIG
2746static char posix_WSTOPSIG__doc__[] =
2747"WSTOPSIG(status) -> integer\n\
2748See Unix documentation.";
2749
2750static PyObject *
2751posix_WSTOPSIG(self, args)
2752 PyObject *self;
2753 PyObject *args;
2754{
2755 int status = 0;
2756
2757 if (!PyArg_Parse(args, "i", &status))
2758 {
2759 return NULL;
2760 }
2761
2762 return Py_BuildValue("i", WSTOPSIG(status));
2763}
2764#endif /* WSTOPSIG */
2765
2766#endif /* HAVE_SYS_WAIT_H */
2767
2768
Guido van Rossum94f6f721999-01-06 18:42:14 +00002769#if defined(HAVE_FSTATVFS)
2770#include <sys/statvfs.h>
2771
2772static char posix_fstatvfs__doc__[] =
2773"fstatvfs(fd) -> \
2774(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2775Perform an fstatvfs system call on the given fd.";
2776
2777static PyObject *
2778posix_fstatvfs(self, args)
2779 PyObject *self;
2780 PyObject *args;
2781{
2782 int fd, res;
2783 struct statvfs st;
2784 if (!PyArg_ParseTuple(args, "i", &fd))
2785 return NULL;
2786 Py_BEGIN_ALLOW_THREADS
2787 res = fstatvfs(fd, &st);
2788 Py_END_ALLOW_THREADS
2789 if (res != 0)
2790 return posix_error();
2791#if !defined(HAVE_LARGEFILE_SUPPORT)
2792 return Py_BuildValue("(lllllllllll)",
2793 (long) st.f_bsize,
2794 (long) st.f_frsize,
2795 (long) st.f_blocks,
2796 (long) st.f_bfree,
2797 (long) st.f_bavail,
2798 (long) st.f_files,
2799 (long) st.f_ffree,
2800 (long) st.f_favail,
2801 (long) st.f_fsid,
2802 (long) st.f_flag,
2803 (long) st.f_namemax);
2804#else
2805 return Py_BuildValue("(llLLLLLLlll)",
2806 (long) st.f_bsize,
2807 (long) st.f_frsize,
2808 (LONG_LONG) st.f_blocks,
2809 (LONG_LONG) st.f_bfree,
2810 (LONG_LONG) st.f_bavail,
2811 (LONG_LONG) st.f_files,
2812 (LONG_LONG) st.f_ffree,
2813 (LONG_LONG) st.f_favail,
2814 (long) st.f_fsid,
2815 (long) st.f_flag,
2816 (long) st.f_namemax);
2817#endif
2818}
2819#endif /* HAVE_FSTATVFS */
2820
2821
2822#if defined(HAVE_STATVFS)
2823#include <sys/statvfs.h>
2824
2825static char posix_statvfs__doc__[] =
2826"statvfs(path) -> \
2827(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2828Perform a statvfs system call on the given path.";
2829
2830static PyObject *
2831posix_statvfs(self, args)
2832 PyObject *self;
2833 PyObject *args;
2834{
2835 char *path;
2836 int res;
2837 struct statvfs st;
2838 if (!PyArg_ParseTuple(args, "s", &path))
2839 return NULL;
2840 Py_BEGIN_ALLOW_THREADS
2841 res = statvfs(path, &st);
2842 Py_END_ALLOW_THREADS
2843 if (res != 0)
2844 return posix_error_with_filename(path);
2845#if !defined(HAVE_LARGEFILE_SUPPORT)
2846 return Py_BuildValue("(lllllllllll)",
2847 (long) st.f_bsize,
2848 (long) st.f_frsize,
2849 (long) st.f_blocks,
2850 (long) st.f_bfree,
2851 (long) st.f_bavail,
2852 (long) st.f_files,
2853 (long) st.f_ffree,
2854 (long) st.f_favail,
2855 (long) st.f_fsid,
2856 (long) st.f_flag,
2857 (long) st.f_namemax);
2858#else /* HAVE_LARGEFILE_SUPPORT */
2859 return Py_BuildValue("(llLLLLLLlll)",
2860 (long) st.f_bsize,
2861 (long) st.f_frsize,
2862 (LONG_LONG) st.f_blocks,
2863 (LONG_LONG) st.f_bfree,
2864 (LONG_LONG) st.f_bavail,
2865 (LONG_LONG) st.f_files,
2866 (LONG_LONG) st.f_ffree,
2867 (LONG_LONG) st.f_favail,
2868 (long) st.f_fsid,
2869 (long) st.f_flag,
2870 (long) st.f_namemax);
2871#endif
2872}
2873#endif /* HAVE_STATVFS */
2874
2875
Barry Warsaw53699e91996-12-10 23:23:01 +00002876static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00002877 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00002878#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00002879 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00002880#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002881 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2882 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002883#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002884 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002885#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002886#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002887 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002888#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002889#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002890 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002891#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002892 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2893 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2894 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002895#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002896 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002897#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002898#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002899 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002900#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002901 {"rename", posix_rename, 0, posix_rename__doc__},
2902 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2903 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002904#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002905 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002906#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002907#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002908 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002909#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002911#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002912 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002913#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002914 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2915 {"remove", posix_unlink, 0, posix_remove__doc__},
2916 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002917#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002918 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002919#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002920 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002921#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002922 {"execv", posix_execv, 0, posix_execv__doc__},
2923 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002924#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002925#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002926 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002927#endif /* HAVE_FORK */
2928#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002929 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002930#endif /* HAVE_GETEGID */
2931#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002932 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002933#endif /* HAVE_GETEUID */
2934#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002935 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002936#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002937 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002938#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002939 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002940#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002941#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002942 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002943#endif /* HAVE_GETPPID */
2944#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002945 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002946#endif /* HAVE_GETUID */
2947#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002949#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002950#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002951 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002952#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002953#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002954 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002955#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002956#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002957 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002958#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002959#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002960 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002961#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002962#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002963 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002964#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002965#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002966 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002967#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002968#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002969 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002970#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002971#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002972 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002973#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002974#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002975 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002976#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002977#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002978 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002979#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002980#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002982#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002983 {"open", posix_open, 1, posix_open__doc__},
2984 {"close", posix_close, 0, posix_close__doc__},
2985 {"dup", posix_dup, 0, posix_dup__doc__},
2986 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2987 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2988 {"read", posix_read, 0, posix_read__doc__},
2989 {"write", posix_write, 0, posix_write__doc__},
2990 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2991 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002992#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002993 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002994#endif
2995#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002996 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002997#endif
2998#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002999 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003000#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003001#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003002 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003003#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003004#ifdef HAVE_STRERROR
3005 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3006#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003007#ifdef HAVE_FSYNC
3008 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3009#endif
3010#ifdef HAVE_FDATASYNC
3011 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3012#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003013#ifdef HAVE_SYS_WAIT_H
3014#ifdef WIFSTOPPED
3015 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3016#endif /* WIFSTOPPED */
3017#ifdef WIFSIGNALED
3018 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3019#endif /* WIFSIGNALED */
3020#ifdef WIFEXITED
3021 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3022#endif /* WIFEXITED */
3023#ifdef WEXITSTATUS
3024 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3025#endif /* WEXITSTATUS */
3026#ifdef WTERMSIG
3027 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3028#endif /* WTERMSIG */
3029#ifdef WSTOPSIG
3030 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3031#endif /* WSTOPSIG */
3032#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003033#ifdef HAVE_FSTATVFS
3034 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3035#endif
3036#ifdef HAVE_STATVFS
3037 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3038#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003039 {NULL, NULL} /* Sentinel */
3040};
3041
3042
Barry Warsaw4a342091996-12-19 23:50:02 +00003043static int
3044ins(d, symbol, value)
3045 PyObject* d;
3046 char* symbol;
3047 long value;
3048{
3049 PyObject* v = PyInt_FromLong(value);
3050 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3051 return -1; /* triggers fatal error */
3052
3053 Py_DECREF(v);
3054 return 0;
3055}
3056
Guido van Rossumd48f2521997-12-05 22:19:34 +00003057#if defined(PYOS_OS2)
3058/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3059static int insertvalues(PyObject *d)
3060{
3061 APIRET rc;
3062 ULONG values[QSV_MAX+1];
3063 PyObject *v;
3064 char *ver, tmp[10];
3065
3066 Py_BEGIN_ALLOW_THREADS
3067 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3068 Py_END_ALLOW_THREADS
3069
3070 if (rc != NO_ERROR) {
3071 os2_error(rc);
3072 return -1;
3073 }
3074
3075 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3076 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3077 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3078 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3079 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3080 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3081 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3082
3083 switch (values[QSV_VERSION_MINOR]) {
3084 case 0: ver = "2.00"; break;
3085 case 10: ver = "2.10"; break;
3086 case 11: ver = "2.11"; break;
3087 case 30: ver = "3.00"; break;
3088 case 40: ver = "4.00"; break;
3089 case 50: ver = "5.00"; break;
3090 default:
3091 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3092 values[QSV_VERSION_MINOR]);
3093 ver = &tmp[0];
3094 }
3095
3096 /* Add Indicator of the Version of the Operating System */
3097 v = PyString_FromString(ver);
3098 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3099 return -1;
3100 Py_DECREF(v);
3101
3102 /* Add Indicator of Which Drive was Used to Boot the System */
3103 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3104 tmp[1] = ':';
3105 tmp[2] = '\0';
3106
3107 v = PyString_FromString(tmp);
3108 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3109 return -1;
3110 Py_DECREF(v);
3111
3112 return 0;
3113}
3114#endif
3115
Barry Warsaw4a342091996-12-19 23:50:02 +00003116static int
3117all_ins(d)
3118 PyObject* d;
3119{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003120#ifdef F_OK
3121 if (ins(d, "F_OK", (long)F_OK)) return -1;
3122#endif
3123#ifdef R_OK
3124 if (ins(d, "R_OK", (long)R_OK)) return -1;
3125#endif
3126#ifdef W_OK
3127 if (ins(d, "W_OK", (long)W_OK)) return -1;
3128#endif
3129#ifdef X_OK
3130 if (ins(d, "X_OK", (long)X_OK)) return -1;
3131#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003132#ifdef WNOHANG
3133 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3134#endif
3135#ifdef O_RDONLY
3136 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3137#endif
3138#ifdef O_WRONLY
3139 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3140#endif
3141#ifdef O_RDWR
3142 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3143#endif
3144#ifdef O_NDELAY
3145 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3146#endif
3147#ifdef O_NONBLOCK
3148 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3149#endif
3150#ifdef O_APPEND
3151 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3152#endif
3153#ifdef O_DSYNC
3154 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3155#endif
3156#ifdef O_RSYNC
3157 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3158#endif
3159#ifdef O_SYNC
3160 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3161#endif
3162#ifdef O_NOCTTY
3163 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3164#endif
3165#ifdef O_CREAT
3166 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3167#endif
3168#ifdef O_EXCL
3169 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3170#endif
3171#ifdef O_TRUNC
3172 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3173#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003174#ifdef O_BINARY
3175 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3176#endif
3177#ifdef O_TEXT
3178 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3179#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003180
3181#if defined(PYOS_OS2)
3182 if (insertvalues(d)) return -1;
3183#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003184 return 0;
3185}
3186
3187
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003188#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003189#define INITFUNC initnt
3190#define MODNAME "nt"
3191#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003192#if defined(PYOS_OS2)
3193#define INITFUNC initos2
3194#define MODNAME "os2"
3195#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003196#define INITFUNC initposix
3197#define MODNAME "posix"
3198#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003199#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003200
Guido van Rossum3886bb61998-12-04 18:50:17 +00003201DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003202INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003203{
Barry Warsaw53699e91996-12-10 23:23:01 +00003204 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003205
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003206 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 posix_methods,
3208 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003209 (PyObject *)NULL,
3210 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003211 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003212
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003213 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003214 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003215 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003216 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003217 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003218
Barry Warsaw4a342091996-12-19 23:50:02 +00003219 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003220 return;
3221
Barry Warsawd58d7641998-07-23 16:14:40 +00003222 Py_INCREF(PyExc_OSError);
3223 PosixError = PyExc_OSError;
3224 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003225}