blob: f7b16a05ef928e46330dd164fad253c70b8b8f76 [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 Rossuma1065681999-01-25 23:20:23 +0000108#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#define HAVE_EXECV 1
110#define HAVE_PIPE 1
111#define HAVE_POPEN 1
112#define HAVE_SYSTEM 1
113#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000114#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#else /* all other compilers */
116/* Unix functions that the configure script doesn't check for */
117#define HAVE_EXECV 1
118#define HAVE_FORK 1
119#define HAVE_GETCWD 1
120#define HAVE_GETEGID 1
121#define HAVE_GETEUID 1
122#define HAVE_GETGID 1
123#define HAVE_GETPPID 1
124#define HAVE_GETUID 1
125#define HAVE_KILL 1
126#define HAVE_OPENDIR 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
130#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000131#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000140#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000141#endif
142
143#ifdef NeXT
144/* NeXT's <unistd.h> and <utime.h> aren't worth much */
145#undef HAVE_UNISTD_H
146#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000147#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000148/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000149#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000150#endif
151
152#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000153/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
154extern int rename();
155extern int pclose();
156extern int lstat();
157extern int symlink();
Guido van Rossum8c67e4e1999-04-07 15:49:41 +0000158extern int fsync();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000160#if defined(PYCC_VACPP)
161extern int mkdir Py_PROTO((char *));
162#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000163#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000164extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000166extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000167#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#endif
169#if defined(__IBMC__) || defined(__IBMCPP__)
170extern int chdir Py_PROTO((char *));
171extern int rmdir Py_PROTO((char *));
172#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000173extern int chdir Py_PROTO((const char *));
174extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000176extern int chmod Py_PROTO((const char *, mode_t));
177extern int chown Py_PROTO((const char *, uid_t, gid_t));
178extern char *getcwd Py_PROTO((char *, int));
179extern char *strerror Py_PROTO((int));
180extern int link Py_PROTO((const char *, const char *));
181extern int rename Py_PROTO((const char *, const char *));
182extern int stat Py_PROTO((const char *, struct stat *));
183extern int unlink Py_PROTO((const char *));
184extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000186extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000189extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
216#ifndef MAXPATHLEN
217#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000218#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000224#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#include <direct.h>
226#define NAMLEN(dirent) strlen((dirent)->d_name)
227#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000231#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#endif
234#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#endif
237#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#endif
240#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000242#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#include <direct.h>
244#include <io.h>
245#include <process.h>
246#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000247#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000249#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#else /* 16-bit Windows */
251#include <dos.h>
252#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000253#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000254#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossumd48f2521997-12-05 22:19:34 +0000256#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000260#ifdef UNION_WAIT
261/* Emulate some macros on systems that have a union instead of macros */
262
263#ifndef WIFEXITED
264#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
265#endif
266
267#ifndef WEXITSTATUS
268#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
269#endif
270
271#ifndef WTERMSIG
272#define WTERMSIG(u_wait) ((u_wait).w_termsig)
273#endif
274
275#endif /* UNION_WAIT */
276
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277/* Return a dictionary corresponding to the POSIX environment table */
278
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000279#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000281#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282
Barry Warsaw53699e91996-12-10 23:23:01 +0000283static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284convertenviron()
285{
Barry Warsaw53699e91996-12-10 23:23:01 +0000286 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000288 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 if (d == NULL)
290 return NULL;
291 if (environ == NULL)
292 return d;
293 /* XXX This part ignores errors */
294 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000295 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 char *p = strchr(*e, '=');
297 if (p == NULL)
298 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000299 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300 if (v == NULL)
301 continue;
302 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000303 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000305 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000307#if defined(PYOS_OS2)
308 {
309 APIRET rc;
310 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
311
312 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
313 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
314 PyObject *v = PyString_FromString(buffer);
315 PyDict_SetItemString(d, "BEGINLIBPATH", v);
316 Py_DECREF(v);
317 }
318 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
319 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
320 PyObject *v = PyString_FromString(buffer);
321 PyDict_SetItemString(d, "ENDLIBPATH", v);
322 Py_DECREF(v);
323 }
324 }
325#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 return d;
327}
328
329
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330/* Set a POSIX-specific error from errno, and return NULL */
331
Barry Warsawd58d7641998-07-23 16:14:40 +0000332static PyObject *
333posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000334{
Barry Warsawca74da41999-02-09 19:31:45 +0000335 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
Barry Warsawd58d7641998-07-23 16:14:40 +0000337static PyObject *
338posix_error_with_filename(name)
339 char* name;
340{
Barry Warsawca74da41999-02-09 19:31:45 +0000341 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000342}
343
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344
Guido van Rossumd48f2521997-12-05 22:19:34 +0000345#if defined(PYOS_OS2)
346/**********************************************************************
347 * Helper Function to Trim and Format OS/2 Messages
348 **********************************************************************/
349 static void
350os2_formatmsg(char *msgbuf, int msglen, char *reason)
351{
352 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
353
354 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
355 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
356
357 while (lastc > msgbuf && isspace(*lastc))
358 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
359 }
360
361 /* Add Optional Reason Text */
362 if (reason) {
363 strcat(msgbuf, " : ");
364 strcat(msgbuf, reason);
365 }
366}
367
368/**********************************************************************
369 * Decode an OS/2 Operating System Error Code
370 *
371 * A convenience function to lookup an OS/2 error code and return a
372 * text message we can use to raise a Python exception.
373 *
374 * Notes:
375 * The messages for errors returned from the OS/2 kernel reside in
376 * the file OSO001.MSG in the \OS2 directory hierarchy.
377 *
378 **********************************************************************/
379 static char *
380os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
381{
382 APIRET rc;
383 ULONG msglen;
384
385 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
386 Py_BEGIN_ALLOW_THREADS
387 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
388 errorcode, "oso001.msg", &msglen);
389 Py_END_ALLOW_THREADS
390
391 if (rc == NO_ERROR)
392 os2_formatmsg(msgbuf, msglen, reason);
393 else
394 sprintf(msgbuf, "unknown OS error #%d", errorcode);
395
396 return msgbuf;
397}
398
399/* Set an OS/2-specific error and return NULL. OS/2 kernel
400 errors are not in a global variable e.g. 'errno' nor are
401 they congruent with posix error numbers. */
402
403static PyObject * os2_error(int code)
404{
405 char text[1024];
406 PyObject *v;
407
408 os2_strerror(text, sizeof(text), code, "");
409
410 v = Py_BuildValue("(is)", code, text);
411 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000412 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000413 Py_DECREF(v);
414 }
415 return NULL; /* Signal to Python that an Exception is Pending */
416}
417
418#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419
420/* POSIX generic methods */
421
Barry Warsaw53699e91996-12-10 23:23:01 +0000422static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000423posix_int(args, func)
424 PyObject *args;
425 int (*func) Py_FPROTO((int));
426{
427 int fd;
428 int res;
429 if (!PyArg_Parse(args, "i", &fd))
430 return NULL;
431 Py_BEGIN_ALLOW_THREADS
432 res = (*func)(fd);
433 Py_END_ALLOW_THREADS
434 if (res < 0)
435 return posix_error();
436 Py_INCREF(Py_None);
437 return Py_None;
438}
439
440
441static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000443 PyObject *args;
444 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000446 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000447 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 if (!PyArg_Parse(args, "s", &path1))
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);
Barry Warsaw53699e91996-12-10 23:23:01 +0000452 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000453 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000454 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000455 Py_INCREF(Py_None);
456 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457}
458
Barry Warsaw53699e91996-12-10 23:23:01 +0000459static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *args;
462 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000464 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000465 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000466 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000468 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000469 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000470 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000471 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000472 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000474 Py_INCREF(Py_None);
475 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476}
477
Barry Warsaw53699e91996-12-10 23:23:01 +0000478static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 PyObject *args;
481 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000483 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000485 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000488 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000489 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000490 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000491 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000492 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000493 Py_INCREF(Py_None);
494 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495}
496
Barry Warsaw53699e91996-12-10 23:23:01 +0000497static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000498posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000499 PyObject *args;
500 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000501{
502 char *path;
503 int i,i2;
504 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000505 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000506 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000507 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000508 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000510 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000511 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000512 Py_INCREF(Py_None);
513 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000514}
515
Barry Warsaw53699e91996-12-10 23:23:01 +0000516static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000518 PyObject *self;
519 PyObject *args;
520 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521{
522 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000523 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000524 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000525 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000527 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000528 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000529 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000530 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000531 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000532#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000533 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000534 (long)st.st_mode,
535 (long)st.st_ino,
536 (long)st.st_dev,
537 (long)st.st_nlink,
538 (long)st.st_uid,
539 (long)st.st_gid,
540 (long)st.st_size,
541 (long)st.st_atime,
542 (long)st.st_mtime,
543 (long)st.st_ctime);
544#else
545 return Py_BuildValue("(lLllllLlll)",
546 (long)st.st_mode,
547 (LONG_LONG)st.st_ino,
548 (long)st.st_dev,
549 (long)st.st_nlink,
550 (long)st.st_uid,
551 (long)st.st_gid,
552 (LONG_LONG)st.st_size,
553 (long)st.st_atime,
554 (long)st.st_mtime,
555 (long)st.st_ctime);
556#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557}
558
559
560/* POSIX methods */
561
Guido van Rossum94f6f721999-01-06 18:42:14 +0000562static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000563"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000564Test for access to a file.";
565
566static PyObject *
567posix_access(self, args)
568 PyObject *self;
569 PyObject *args;
570{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000571 char *path;
572 int mode;
573 int res;
574
575 if (!PyArg_Parse(args, "(si)", &path, &mode))
576 return NULL;
577 Py_BEGIN_ALLOW_THREADS
578 res = access(path, mode);
579 Py_END_ALLOW_THREADS
580 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000581}
582
Guido van Rossumd371ff11999-01-25 16:12:23 +0000583#ifndef F_OK
584#define F_OK 0
585#endif
586#ifndef R_OK
587#define R_OK 4
588#endif
589#ifndef W_OK
590#define W_OK 2
591#endif
592#ifndef X_OK
593#define X_OK 1
594#endif
595
596#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000597static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000598"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000599Return the name of the terminal device connected to 'fd'.";
600
601static PyObject *
602posix_ttyname(self, args)
603 PyObject *self;
604 PyObject *args;
605{
606 PyObject *file;
607 int id;
608 char *ret;
609
Guido van Rossum94f6f721999-01-06 18:42:14 +0000610 if (!PyArg_Parse(args, "i", &id))
611 return NULL;
612
Guido van Rossum94f6f721999-01-06 18:42:14 +0000613 ret = ttyname(id);
614 if (ret == NULL)
615 return(posix_error());
616 return(PyString_FromString(ret));
617}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000618#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000619
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000620static char posix_chdir__doc__[] =
621"chdir(path) -> None\n\
622Change the current working directory to the specified path.";
623
Barry Warsaw53699e91996-12-10 23:23:01 +0000624static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000626 PyObject *self;
627 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629 return posix_1str(args, chdir);
630}
631
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000632
633static char posix_chmod__doc__[] =
634"chmod(path, mode) -> None\n\
635Change the access permissions of a file.";
636
Barry Warsaw53699e91996-12-10 23:23:01 +0000637static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000639 PyObject *self;
640 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642 return posix_strint(args, chmod);
643}
644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000645
Guido van Rossum21142a01999-01-08 21:05:37 +0000646#ifdef HAVE_FSYNC
647static char posix_fsync__doc__[] =
648"fsync(fildes) -> None\n\
649force write of file with filedescriptor to disk.";
650
651static PyObject *
652posix_fsync(self, args)
653 PyObject *self;
654 PyObject *args;
655{
Guido van Rossum21142a01999-01-08 21:05:37 +0000656 return posix_int(args, fsync);
657}
658#endif /* HAVE_FSYNC */
659
660#ifdef HAVE_FDATASYNC
661static char posix_fdatasync__doc__[] =
662"fdatasync(fildes) -> None\n\
663force write of file with filedescriptor to disk.\n\
664 does not force update of metadata.";
665
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000666extern int fdatasync(int); /* Prototype just in case */
667
Guido van Rossum21142a01999-01-08 21:05:37 +0000668static PyObject *
669posix_fdatasync(self, args)
670 PyObject *self;
671 PyObject *args;
672{
Guido van Rossum21142a01999-01-08 21:05:37 +0000673 return posix_int(args, fdatasync);
674}
675#endif /* HAVE_FDATASYNC */
676
677
Guido van Rossumb6775db1994-08-01 11:34:53 +0000678#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000679static char posix_chown__doc__[] =
680"chown(path, uid, gid) -> None\n\
681Change the owner and group id of path to the numeric uid and gid.";
682
Barry Warsaw53699e91996-12-10 23:23:01 +0000683static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000684posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000685 PyObject *self;
686 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000687{
688 return posix_strintint(args, chown);
689}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000690#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000692
Guido van Rossum36bc6801995-06-14 22:54:23 +0000693#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000694static char posix_getcwd__doc__[] =
695"getcwd() -> path\n\
696Return a string representing the current working directory.";
697
Barry Warsaw53699e91996-12-10 23:23:01 +0000698static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000700 PyObject *self;
701 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
703 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000704 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000705 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000706 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000707 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000708 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000709 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000710 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000712 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000713}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000716
Guido van Rossumb6775db1994-08-01 11:34:53 +0000717#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000718static char posix_link__doc__[] =
719"link(src, dst) -> None\n\
720Create a hard link to a file.";
721
Barry Warsaw53699e91996-12-10 23:23:01 +0000722static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000724 PyObject *self;
725 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727 return posix_2str(args, link);
728}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000729#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000731
732static char posix_listdir__doc__[] =
733"listdir(path) -> list_of_strings\n\
734Return a list containing the names of the entries in the directory.\n\
735\n\
736 path: path of directory to list\n\
737\n\
738The list is in arbitrary order. It does not include the special\n\
739entries '.' and '..' even if they are present in the directory.";
740
Barry Warsaw53699e91996-12-10 23:23:01 +0000741static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000743 PyObject *self;
744 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000745{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000746 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000747 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000748#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000749
Guido van Rossumb6775db1994-08-01 11:34:53 +0000750 char *name;
751 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000752 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753 HANDLE hFindFile;
754 WIN32_FIND_DATA FileData;
755 char namebuf[MAX_PATH+5];
756
Guido van Rossum7e488981998-10-08 02:25:24 +0000757 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 return NULL;
759 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000760 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 return NULL;
762 }
763 strcpy(namebuf, name);
764 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
765 namebuf[len++] = '/';
766 strcpy(namebuf + len, "*.*");
767
Barry Warsaw53699e91996-12-10 23:23:01 +0000768 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 return NULL;
770
771 hFindFile = FindFirstFile(namebuf, &FileData);
772 if (hFindFile == INVALID_HANDLE_VALUE) {
773 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000774 if (errno == ERROR_FILE_NOT_FOUND)
775 return PyList_New(0);
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000776 return posix_error_with_filename(name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 }
778 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000779 if (FileData.cFileName[0] == '.' &&
780 (FileData.cFileName[1] == '\0' ||
781 FileData.cFileName[1] == '.' &&
782 FileData.cFileName[2] == '\0'))
783 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000784 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000785 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000786 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 d = NULL;
788 break;
789 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000790 if (PyList_Append(d, v) != 0) {
791 Py_DECREF(v);
792 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793 d = NULL;
794 break;
795 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000796 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 } while (FindNextFile(hFindFile, &FileData) == TRUE);
798
799 if (FindClose(hFindFile) == FALSE) {
800 errno = GetLastError();
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000801 return posix_error_with_filename(&name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000802 }
803
804 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000805
Guido van Rossum8d665e61996-06-26 18:22:49 +0000806#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000807#ifdef _MSC_VER /* 16-bit Windows */
808
809#ifndef MAX_PATH
810#define MAX_PATH 250
811#endif
812 char *name, *pt;
813 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000814 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000815 char namebuf[MAX_PATH+5];
816 struct _find_t ep;
817
Guido van Rossum7e488981998-10-08 02:25:24 +0000818 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000819 return NULL;
820 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000821 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000822 return NULL;
823 }
824 strcpy(namebuf, name);
825 for (pt = namebuf; *pt; pt++)
826 if (*pt == '/')
827 *pt = '\\';
828 if (namebuf[len-1] != '\\')
829 namebuf[len++] = '\\';
830 strcpy(namebuf + len, "*.*");
831
Barry Warsaw53699e91996-12-10 23:23:01 +0000832 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000833 return NULL;
834
835 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000836 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
837 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000838 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000839 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000840 }
841 do {
842 if (ep.name[0] == '.' &&
843 (ep.name[1] == '\0' ||
844 ep.name[1] == '.' &&
845 ep.name[2] == '\0'))
846 continue;
847 strcpy(namebuf, ep.name);
848 for (pt = namebuf; *pt; pt++)
849 if (isupper(*pt))
850 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000851 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000852 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000853 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000854 d = NULL;
855 break;
856 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000857 if (PyList_Append(d, v) != 0) {
858 Py_DECREF(v);
859 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000860 d = NULL;
861 break;
862 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000863 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000864 } while (_dos_findnext(&ep) == 0);
865
866 return d;
867
868#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000869#if defined(PYOS_OS2)
870
871#ifndef MAX_PATH
872#define MAX_PATH CCHMAXPATH
873#endif
874 char *name, *pt;
875 int len;
876 PyObject *d, *v;
877 char namebuf[MAX_PATH+5];
878 HDIR hdir = 1;
879 ULONG srchcnt = 1;
880 FILEFINDBUF3 ep;
881 APIRET rc;
882
Guido van Rossum7e488981998-10-08 02:25:24 +0000883 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000884 return NULL;
885 if (len >= MAX_PATH) {
886 PyErr_SetString(PyExc_ValueError, "path too long");
887 return NULL;
888 }
889 strcpy(namebuf, name);
890 for (pt = namebuf; *pt; pt++)
891 if (*pt == '/')
892 *pt = '\\';
893 if (namebuf[len-1] != '\\')
894 namebuf[len++] = '\\';
895 strcpy(namebuf + len, "*.*");
896
897 if ((d = PyList_New(0)) == NULL)
898 return NULL;
899
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000900 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
901 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000902 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000903 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
904 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
905 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000906
907 if (rc != NO_ERROR) {
908 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000909 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000910 }
911
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000912 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000913 do {
914 if (ep.achName[0] == '.'
915 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000916 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000917
918 strcpy(namebuf, ep.achName);
919
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000920 /* Leave Case of Name Alone -- In Native Form */
921 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000922
923 v = PyString_FromString(namebuf);
924 if (v == NULL) {
925 Py_DECREF(d);
926 d = NULL;
927 break;
928 }
929 if (PyList_Append(d, v) != 0) {
930 Py_DECREF(v);
931 Py_DECREF(d);
932 d = NULL;
933 break;
934 }
935 Py_DECREF(v);
936 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
937 }
938
939 return d;
940#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000941
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000942 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000943 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000945 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000946 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000947 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000948 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000949 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000950 Py_BLOCK_THREADS
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000951 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000952 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000953 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000955 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956 return NULL;
957 }
958 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000959 if (ep->d_name[0] == '.' &&
960 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000961 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000962 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000963 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000964 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000965 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000966 d = NULL;
967 break;
968 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000969 if (PyList_Append(d, v) != 0) {
970 Py_DECREF(v);
971 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000972 d = NULL;
973 break;
974 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000975 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976 }
977 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000978 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000979
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000980 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000981
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000982#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000983#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000984#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000985}
986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000987static char posix_mkdir__doc__[] =
988"mkdir(path [, mode=0777]) -> None\n\
989Create a directory.";
990
Barry Warsaw53699e91996-12-10 23:23:01 +0000991static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000992posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000993 PyObject *self;
994 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000995{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000996 int res;
997 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000998 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000999 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001000 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001001 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001002#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001003 res = mkdir(path);
1004#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001005 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001006#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001007 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001008 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001009 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001010 Py_INCREF(Py_None);
1011 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001012}
1013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001014
Guido van Rossumb6775db1994-08-01 11:34:53 +00001015#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001016static char posix_nice__doc__[] =
1017"nice(inc) -> new_priority\n\
1018Decrease the priority of process and return new priority.";
1019
Barry Warsaw53699e91996-12-10 23:23:01 +00001020static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001021posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001022 PyObject *self;
1023 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001024{
1025 int increment, value;
1026
Barry Warsaw53699e91996-12-10 23:23:01 +00001027 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001028 return NULL;
1029 value = nice(increment);
1030 if (value == -1)
1031 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001032 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001033}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001034#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001035
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001036
1037static char posix_rename__doc__[] =
1038"rename(old, new) -> None\n\
1039Rename a file or directory.";
1040
Barry Warsaw53699e91996-12-10 23:23:01 +00001041static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001042posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001043 PyObject *self;
1044 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001045{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046 return posix_2str(args, rename);
1047}
1048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001049
1050static char posix_rmdir__doc__[] =
1051"rmdir(path) -> None\n\
1052Remove a directory.";
1053
Barry Warsaw53699e91996-12-10 23:23:01 +00001054static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001055posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001056 PyObject *self;
1057 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001058{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001059 return posix_1str(args, rmdir);
1060}
1061
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001062
1063static char posix_stat__doc__[] =
1064"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1065Perform a stat system call on the given path.";
1066
Barry Warsaw53699e91996-12-10 23:23:01 +00001067static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001069 PyObject *self;
1070 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001071{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072 return posix_do_stat(self, args, stat);
1073}
1074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001075
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001076#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001077static char posix_system__doc__[] =
1078"system(command) -> exit_status\n\
1079Execute the command (a string) in a subshell.";
1080
Barry Warsaw53699e91996-12-10 23:23:01 +00001081static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001082posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001083 PyObject *self;
1084 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001085{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001086 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001087 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001088 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001089 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001090 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001091 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001092 Py_END_ALLOW_THREADS
1093 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001095#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001097
1098static char posix_umask__doc__[] =
1099"umask(new_mask) -> old_mask\n\
1100Set the current numeric umask and return the previous umask.";
1101
Barry Warsaw53699e91996-12-10 23:23:01 +00001102static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001103posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001104 PyObject *self;
1105 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001106{
1107 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001108 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109 return NULL;
1110 i = umask(i);
1111 if (i < 0)
1112 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001113 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114}
1115
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001116
1117static char posix_unlink__doc__[] =
1118"unlink(path) -> None\n\
1119Remove a file (same as remove(path)).";
1120
1121static char posix_remove__doc__[] =
1122"remove(path) -> None\n\
1123Remove a file (same as unlink(path)).";
1124
Barry Warsaw53699e91996-12-10 23:23:01 +00001125static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001126posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001127 PyObject *self;
1128 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001129{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130 return posix_1str(args, unlink);
1131}
1132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001133
Guido van Rossumb6775db1994-08-01 11:34:53 +00001134#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001135static char posix_uname__doc__[] =
1136"uname() -> (sysname, nodename, release, version, machine)\n\
1137Return a tuple identifying the current operating system.";
1138
Barry Warsaw53699e91996-12-10 23:23:01 +00001139static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001140posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001141 PyObject *self;
1142 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001143{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001144 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001145 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001146 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001147 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001148 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001149 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001150 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001151 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001152 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001153 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001154 u.sysname,
1155 u.nodename,
1156 u.release,
1157 u.version,
1158 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001159}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001160#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001161
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001162
1163static char posix_utime__doc__[] =
1164"utime(path, (atime, utime)) -> None\n\
1165Set the access and modified time of the file to the given values.";
1166
Barry Warsaw53699e91996-12-10 23:23:01 +00001167static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001168posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001169 PyObject *self;
1170 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001171{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001172 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001173 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001174 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001175
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001176/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001177#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001178 struct utimbuf buf;
1179#define ATIME buf.actime
1180#define MTIME buf.modtime
1181#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001182#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001183 time_t buf[2];
1184#define ATIME buf[0]
1185#define MTIME buf[1]
1186#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001187#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001188
Barry Warsaw53699e91996-12-10 23:23:01 +00001189 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001190 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001191 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001192 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001193 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001194 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001195 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001196 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001197 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001198 Py_INCREF(Py_None);
1199 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001200#undef UTIME_ARG
1201#undef ATIME
1202#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203}
1204
Guido van Rossum85e3b011991-06-03 12:42:10 +00001205
Guido van Rossum3b066191991-06-04 19:40:25 +00001206/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001208static char posix__exit__doc__[] =
1209"_exit(status)\n\
1210Exit to the system with specified status, without normal exit processing.";
1211
Barry Warsaw53699e91996-12-10 23:23:01 +00001212static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001213posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001214 PyObject *self;
1215 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001216{
1217 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001218 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001219 return NULL;
1220 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001221 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001222}
1223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001224
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001225#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001226static char posix_execv__doc__[] =
1227"execv(path, args)\n\
1228Execute an executable path with arguments, replacing current process.\n\
1229\n\
1230 path: path of executable file\n\
1231 args: tuple or list of strings";
1232
Barry Warsaw53699e91996-12-10 23:23:01 +00001233static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001234posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001235 PyObject *self;
1236 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001237{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001238 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001239 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001240 char **argvlist;
1241 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001242 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001243
Guido van Rossum89b33251993-10-22 14:26:06 +00001244 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001245 argv is a list or tuple of strings. */
1246
Barry Warsaw53699e91996-12-10 23:23:01 +00001247 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001248 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001249 if (PyList_Check(argv)) {
1250 argc = PyList_Size(argv);
1251 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001252 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001253 else if (PyTuple_Check(argv)) {
1254 argc = PyTuple_Size(argv);
1255 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001256 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001257 else {
1258 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001259 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001260 return NULL;
1261 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001262
Barry Warsaw53699e91996-12-10 23:23:01 +00001263 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001264 if (argvlist == NULL)
1265 return NULL;
1266 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001267 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1268 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001269 goto badarg;
1270 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001271 }
1272 argvlist[argc] = NULL;
1273
Guido van Rossumb6775db1994-08-01 11:34:53 +00001274#ifdef BAD_EXEC_PROTOTYPES
1275 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001276#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001277 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001278#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001279
Guido van Rossum85e3b011991-06-03 12:42:10 +00001280 /* If we get here it's definitely an error */
1281
Barry Warsaw53699e91996-12-10 23:23:01 +00001282 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001283 return posix_error();
1284}
1285
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001286
1287static char posix_execve__doc__[] =
1288"execve(path, args, env)\n\
1289Execute a path with arguments and environment, replacing current process.\n\
1290\n\
1291 path: path of executable file\n\
1292 args: tuple or list of arguments\n\
1293 env: dictonary of strings mapping to strings";
1294
Barry Warsaw53699e91996-12-10 23:23:01 +00001295static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001296posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001297 PyObject *self;
1298 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001299{
1300 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001301 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001302 char **argvlist;
1303 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001304 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001305 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001306 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001307
1308 /* execve has three arguments: (path, argv, env), where
1309 argv is a list or tuple of strings and env is a dictionary
1310 like posix.environ. */
1311
Barry Warsaw53699e91996-12-10 23:23:01 +00001312 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001313 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001314 if (PyList_Check(argv)) {
1315 argc = PyList_Size(argv);
1316 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001317 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001318 else if (PyTuple_Check(argv)) {
1319 argc = PyTuple_Size(argv);
1320 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001321 }
1322 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001323 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001324 return NULL;
1325 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001326 if (!PyMapping_Check(env)) {
1327 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001328 return NULL;
1329 }
1330
Barry Warsaw53699e91996-12-10 23:23:01 +00001331 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001332 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001333 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001334 return NULL;
1335 }
1336 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001338 "s;argv must be list of strings",
1339 &argvlist[i]))
1340 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001341 goto fail_1;
1342 }
1343 }
1344 argvlist[argc] = NULL;
1345
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001346 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001347 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001348 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001349 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001350 goto fail_1;
1351 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001352 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001353 keys = PyMapping_Keys(env);
1354 vals = PyMapping_Values(env);
1355 if (!keys || !vals)
1356 goto fail_2;
1357
1358 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001359 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001360
1361 key = PyList_GetItem(keys, pos);
1362 val = PyList_GetItem(vals, pos);
1363 if (!key || !val)
1364 goto fail_2;
1365
Barry Warsaw53699e91996-12-10 23:23:01 +00001366 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001367 !PyArg_Parse(val, "s;non-string value in env", &v))
1368 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001369 goto fail_2;
1370 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001371
1372#if defined(PYOS_OS2)
1373 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1374 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1375#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001376 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001377 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001379 goto fail_2;
1380 }
1381 sprintf(p, "%s=%s", k, v);
1382 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001383#if defined(PYOS_OS2)
1384 }
1385#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001386 }
1387 envlist[envc] = 0;
1388
Guido van Rossumb6775db1994-08-01 11:34:53 +00001389
1390#ifdef BAD_EXEC_PROTOTYPES
1391 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001392#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001393 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001394#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001395
1396 /* If we get here it's definitely an error */
1397
1398 (void) posix_error();
1399
1400 fail_2:
1401 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001402 PyMem_DEL(envlist[envc]);
1403 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001404 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001405 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001406 Py_XDECREF(vals);
1407 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001408 return NULL;
1409}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001410#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001411
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001412
Guido van Rossuma1065681999-01-25 23:20:23 +00001413#ifdef HAVE_SPAWNV
1414static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001415"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001416Execute an executable path with arguments, replacing current process.\n\
1417\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001418 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001419 path: path of executable file\n\
1420 args: tuple or list of strings";
1421
1422static PyObject *
1423posix_spawnv(self, args)
1424 PyObject *self;
1425 PyObject *args;
1426{
1427 char *path;
1428 PyObject *argv;
1429 char **argvlist;
1430 int mode, i, argc;
1431 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1432
1433 /* spawnv has three arguments: (mode, path, argv), where
1434 argv is a list or tuple of strings. */
1435
1436 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1437 return NULL;
1438 if (PyList_Check(argv)) {
1439 argc = PyList_Size(argv);
1440 getitem = PyList_GetItem;
1441 }
1442 else if (PyTuple_Check(argv)) {
1443 argc = PyTuple_Size(argv);
1444 getitem = PyTuple_GetItem;
1445 }
1446 else {
1447 badarg:
1448 PyErr_BadArgument();
1449 return NULL;
1450 }
1451
1452 argvlist = PyMem_NEW(char *, argc+1);
1453 if (argvlist == NULL)
1454 return NULL;
1455 for (i = 0; i < argc; i++) {
1456 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1457 PyMem_DEL(argvlist);
1458 goto badarg;
1459 }
1460 }
1461 argvlist[argc] = NULL;
1462
Guido van Rossum246bc171999-02-01 23:54:31 +00001463 if (mode == _OLD_P_OVERLAY)
1464 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001465 i = _spawnv(mode, path, argvlist);
1466
1467 PyMem_DEL(argvlist);
1468
1469 if (i == -1)
1470 return posix_error();
1471 else
1472 return Py_BuildValue("i", i);
1473}
1474
1475
1476static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001477"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001478Execute a path with arguments and environment, replacing current process.\n\
1479\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001480 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001481 path: path of executable file\n\
1482 args: tuple or list of arguments\n\
1483 env: dictonary of strings mapping to strings";
1484
1485static PyObject *
1486posix_spawnve(self, args)
1487 PyObject *self;
1488 PyObject *args;
1489{
1490 char *path;
1491 PyObject *argv, *env;
1492 char **argvlist;
1493 char **envlist;
1494 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1495 int mode, i, pos, argc, envc;
1496 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1497
1498 /* spawnve has four arguments: (mode, path, argv, env), where
1499 argv is a list or tuple of strings and env is a dictionary
1500 like posix.environ. */
1501
1502 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1503 return NULL;
1504 if (PyList_Check(argv)) {
1505 argc = PyList_Size(argv);
1506 getitem = PyList_GetItem;
1507 }
1508 else if (PyTuple_Check(argv)) {
1509 argc = PyTuple_Size(argv);
1510 getitem = PyTuple_GetItem;
1511 }
1512 else {
1513 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1514 return NULL;
1515 }
1516 if (!PyMapping_Check(env)) {
1517 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1518 return NULL;
1519 }
1520
1521 argvlist = PyMem_NEW(char *, argc+1);
1522 if (argvlist == NULL) {
1523 PyErr_NoMemory();
1524 return NULL;
1525 }
1526 for (i = 0; i < argc; i++) {
1527 if (!PyArg_Parse((*getitem)(argv, i),
1528 "s;argv must be list of strings",
1529 &argvlist[i]))
1530 {
1531 goto fail_1;
1532 }
1533 }
1534 argvlist[argc] = NULL;
1535
1536 i = PyMapping_Length(env);
1537 envlist = PyMem_NEW(char *, i + 1);
1538 if (envlist == NULL) {
1539 PyErr_NoMemory();
1540 goto fail_1;
1541 }
1542 envc = 0;
1543 keys = PyMapping_Keys(env);
1544 vals = PyMapping_Values(env);
1545 if (!keys || !vals)
1546 goto fail_2;
1547
1548 for (pos = 0; pos < i; pos++) {
1549 char *p, *k, *v;
1550
1551 key = PyList_GetItem(keys, pos);
1552 val = PyList_GetItem(vals, pos);
1553 if (!key || !val)
1554 goto fail_2;
1555
1556 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1557 !PyArg_Parse(val, "s;non-string value in env", &v))
1558 {
1559 goto fail_2;
1560 }
1561 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1562 if (p == NULL) {
1563 PyErr_NoMemory();
1564 goto fail_2;
1565 }
1566 sprintf(p, "%s=%s", k, v);
1567 envlist[envc++] = p;
1568 }
1569 envlist[envc] = 0;
1570
Guido van Rossum246bc171999-02-01 23:54:31 +00001571 if (mode == _OLD_P_OVERLAY)
1572 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001573 i = _spawnve(mode, path, argvlist, envlist);
1574 if (i == -1)
1575 (void) posix_error();
1576 else
1577 res = Py_BuildValue("i", i);
1578
1579 fail_2:
1580 while (--envc >= 0)
1581 PyMem_DEL(envlist[envc]);
1582 PyMem_DEL(envlist);
1583 fail_1:
1584 PyMem_DEL(argvlist);
1585 Py_XDECREF(vals);
1586 Py_XDECREF(keys);
1587 return res;
1588}
1589#endif /* HAVE_SPAWNV */
1590
1591
Guido van Rossumad0ee831995-03-01 10:34:45 +00001592#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001593static char posix_fork__doc__[] =
1594"fork() -> pid\n\
1595Fork a child process.\n\
1596\n\
1597Return 0 to child process and PID of child to parent process.";
1598
Barry Warsaw53699e91996-12-10 23:23:01 +00001599static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001600posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001601 PyObject *self;
1602 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001603{
1604 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001605 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001606 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001607 pid = fork();
1608 if (pid == -1)
1609 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001610 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001611 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001612}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001613#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001615
Guido van Rossumad0ee831995-03-01 10:34:45 +00001616#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001617static char posix_getegid__doc__[] =
1618"getegid() -> egid\n\
1619Return the current process's effective group id.";
1620
Barry Warsaw53699e91996-12-10 23:23:01 +00001621static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001622posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001623 PyObject *self;
1624 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001625{
Barry Warsaw53699e91996-12-10 23:23:01 +00001626 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001627 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001628 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001629}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001630#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001631
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001632
Guido van Rossumad0ee831995-03-01 10:34:45 +00001633#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001634static char posix_geteuid__doc__[] =
1635"geteuid() -> euid\n\
1636Return the current process's effective user id.";
1637
Barry Warsaw53699e91996-12-10 23:23:01 +00001638static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001639posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 PyObject *self;
1641 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001642{
Barry Warsaw53699e91996-12-10 23:23:01 +00001643 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001644 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001645 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001646}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001647#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001649
Guido van Rossumad0ee831995-03-01 10:34:45 +00001650#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001651static char posix_getgid__doc__[] =
1652"getgid() -> gid\n\
1653Return the current process's group id.";
1654
Barry Warsaw53699e91996-12-10 23:23:01 +00001655static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001656posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001657 PyObject *self;
1658 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001659{
Barry Warsaw53699e91996-12-10 23:23:01 +00001660 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001661 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001662 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001664#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001666
1667static char posix_getpid__doc__[] =
1668"getpid() -> pid\n\
1669Return the current process id";
1670
Barry Warsaw53699e91996-12-10 23:23:01 +00001671static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001672posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001673 PyObject *self;
1674 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001675{
Barry Warsaw53699e91996-12-10 23:23:01 +00001676 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001677 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001678 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001679}
1680
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001681
Guido van Rossumb6775db1994-08-01 11:34:53 +00001682#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683static char posix_getpgrp__doc__[] =
1684"getpgrp() -> pgrp\n\
1685Return the current process group id.";
1686
Barry Warsaw53699e91996-12-10 23:23:01 +00001687static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001688posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001689 PyObject *self;
1690 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001691{
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001693 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001694#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001695 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001696#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001697 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001698#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001699}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001700#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001701
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001702
Guido van Rossumb6775db1994-08-01 11:34:53 +00001703#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001704static char posix_setpgrp__doc__[] =
1705"setpgrp() -> None\n\
1706Make this process a session leader.";
1707
Barry Warsaw53699e91996-12-10 23:23:01 +00001708static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001709posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001710 PyObject *self;
1711 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001712{
Barry Warsaw53699e91996-12-10 23:23:01 +00001713 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001714 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001715#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001716 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001717#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001718 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001719#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001720 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001721 Py_INCREF(Py_None);
1722 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001723}
1724
Guido van Rossumb6775db1994-08-01 11:34:53 +00001725#endif /* HAVE_SETPGRP */
1726
Guido van Rossumad0ee831995-03-01 10:34:45 +00001727#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001728static char posix_getppid__doc__[] =
1729"getppid() -> ppid\n\
1730Return the parent's process id.";
1731
Barry Warsaw53699e91996-12-10 23:23:01 +00001732static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001733posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 PyObject *self;
1735 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001736{
Barry Warsaw53699e91996-12-10 23:23:01 +00001737 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001738 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001739 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001740}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001741#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001743
Guido van Rossumad0ee831995-03-01 10:34:45 +00001744#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001745static char posix_getuid__doc__[] =
1746"getuid() -> uid\n\
1747Return the current process's user id.";
1748
Barry Warsaw53699e91996-12-10 23:23:01 +00001749static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001750posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001751 PyObject *self;
1752 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001753{
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001755 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001756 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001757}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001758#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001759
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001760
Guido van Rossumad0ee831995-03-01 10:34:45 +00001761#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001762static char posix_kill__doc__[] =
1763"kill(pid, sig) -> None\n\
1764Kill a process with a signal.";
1765
Barry Warsaw53699e91996-12-10 23:23:01 +00001766static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001767posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001768 PyObject *self;
1769 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001770{
1771 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001772 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001773 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001774#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001775 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1776 APIRET rc;
1777 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001778 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001779
1780 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1781 APIRET rc;
1782 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001783 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001784
1785 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001786 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001787#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001788 if (kill(pid, sig) == -1)
1789 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001790#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 Py_INCREF(Py_None);
1792 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001793}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001794#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001795
Guido van Rossumc0125471996-06-28 18:55:32 +00001796#ifdef HAVE_PLOCK
1797
1798#ifdef HAVE_SYS_LOCK_H
1799#include <sys/lock.h>
1800#endif
1801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802static char posix_plock__doc__[] =
1803"plock(op) -> None\n\
1804Lock program segments into memory.";
1805
Barry Warsaw53699e91996-12-10 23:23:01 +00001806static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001807posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001808 PyObject *self;
1809 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001810{
1811 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001813 return NULL;
1814 if (plock(op) == -1)
1815 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 Py_INCREF(Py_None);
1817 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001818}
1819#endif
1820
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001821
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001822#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001823static char posix_popen__doc__[] =
1824"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1825Open a pipe to/from a command returning a file object.";
1826
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001827#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001828static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001829async_system(const char *command)
1830{
1831 char *p, errormsg[256], args[1024];
1832 RESULTCODES rcodes;
1833 APIRET rc;
1834 char *shell = getenv("COMSPEC");
1835 if (!shell)
1836 shell = "cmd";
1837
1838 strcpy(args, shell);
1839 p = &args[ strlen(args)+1 ];
1840 strcpy(p, "/c ");
1841 strcat(p, command);
1842 p += strlen(p) + 1;
1843 *p = '\0';
1844
1845 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001846 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001847 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001848 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001849 &rcodes, shell);
1850 return rc;
1851}
1852
Guido van Rossumd48f2521997-12-05 22:19:34 +00001853static FILE *
1854popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001855{
1856 HFILE rhan, whan;
1857 FILE *retfd = NULL;
1858 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1859
Guido van Rossumd48f2521997-12-05 22:19:34 +00001860 if (rc != NO_ERROR) {
1861 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001862 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001863 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001864
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001865 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1866 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001867
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001868 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1869 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001870
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001871 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1872 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001873
1874 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001875 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001876 }
1877
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001878 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1879 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001880
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001881 close(oldfd); /* And Close Saved STDOUT Handle */
1882 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001883
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001884 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1885 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001886
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001887 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1888 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001889
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001890 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1891 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001892
1893 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001894 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001895 }
1896
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001897 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1898 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001899
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001900 close(oldfd); /* And Close Saved STDIN Handle */
1901 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001902
Guido van Rossumd48f2521997-12-05 22:19:34 +00001903 } else {
1904 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001905 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001906 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001907}
1908
1909static PyObject *
1910posix_popen(self, args)
1911 PyObject *self;
1912 PyObject *args;
1913{
1914 char *name;
1915 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001916 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001917 FILE *fp;
1918 PyObject *f;
1919 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1920 return NULL;
1921 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001922 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001923 Py_END_ALLOW_THREADS
1924 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001925 return os2_error(err);
1926
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001927 f = PyFile_FromFile(fp, name, mode, fclose);
1928 if (f != NULL)
1929 PyFile_SetBufSize(f, bufsize);
1930 return f;
1931}
1932
1933#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001934static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001935posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001936 PyObject *self;
1937 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001938{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001939 char *name;
1940 char *mode = "r";
1941 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001942 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001943 PyObject *f;
1944 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001945 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001946 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001947 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001948 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001949 if (fp == NULL)
1950 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001951 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001952 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001954 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001955}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001956#endif
1957
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001958#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001959
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001960
Guido van Rossumb6775db1994-08-01 11:34:53 +00001961#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001962static char posix_setuid__doc__[] =
1963"setuid(uid) -> None\n\
1964Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001965static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001966posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001967 PyObject *self;
1968 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001969{
1970 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001971 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001972 return NULL;
1973 if (setuid(uid) < 0)
1974 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001975 Py_INCREF(Py_None);
1976 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001977}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001978#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001979
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001980
Guido van Rossumb6775db1994-08-01 11:34:53 +00001981#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001982static char posix_setgid__doc__[] =
1983"setgid(gid) -> None\n\
1984Set the current process's group id.";
1985
Barry Warsaw53699e91996-12-10 23:23:01 +00001986static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001987posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001988 PyObject *self;
1989 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001990{
1991 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001992 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001993 return NULL;
1994 if (setgid(gid) < 0)
1995 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001996 Py_INCREF(Py_None);
1997 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001998}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001999#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002001
Guido van Rossumb6775db1994-08-01 11:34:53 +00002002#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002003static char posix_waitpid__doc__[] =
2004"waitpid(pid, options) -> (pid, status)\n\
2005Wait for completion of a give child process.";
2006
Barry Warsaw53699e91996-12-10 23:23:01 +00002007static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002008posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002009 PyObject *self;
2010 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002011{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002012 int pid, options;
2013#ifdef UNION_WAIT
2014 union wait status;
2015#define status_i (status.w_status)
2016#else
2017 int status;
2018#define status_i status
2019#endif
2020 status_i = 0;
2021
Barry Warsaw53699e91996-12-10 23:23:01 +00002022 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002023 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002024 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002025#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002026 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002027#else
2028 pid = waitpid(pid, &status, options);
2029#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002030 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002031 if (pid == -1)
2032 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002033 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002034 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002035}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002036#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002037
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002038
Guido van Rossumad0ee831995-03-01 10:34:45 +00002039#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002040static char posix_wait__doc__[] =
2041"wait() -> (pid, status)\n\
2042Wait for completion of a child process.";
2043
Barry Warsaw53699e91996-12-10 23:23:01 +00002044static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002045posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002046 PyObject *self;
2047 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002048{
2049 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002050#ifdef UNION_WAIT
2051 union wait status;
2052#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002053#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002054 int status;
2055#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002056#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002057 status_i = 0;
2058 Py_BEGIN_ALLOW_THREADS
2059 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002060 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002061 if (pid == -1)
2062 return posix_error();
2063 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002064 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002065}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002066#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002067
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002068
2069static char posix_lstat__doc__[] =
2070"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2071Like stat(path), but do not follow symbolic links.";
2072
Barry Warsaw53699e91996-12-10 23:23:01 +00002073static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 PyObject *self;
2076 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002077{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002078#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002079 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002080#else /* !HAVE_LSTAT */
2081 return posix_do_stat(self, args, stat);
2082#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083}
2084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Guido van Rossumb6775db1994-08-01 11:34:53 +00002086#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002087static char posix_readlink__doc__[] =
2088"readlink(path) -> path\n\
2089Return a string representing the path to which the symbolic link points.";
2090
Barry Warsaw53699e91996-12-10 23:23:01 +00002091static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002092posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002093 PyObject *self;
2094 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002096 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002097 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002098 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002099 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002100 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002102 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002103 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002104 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002105 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002106 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002107}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002108#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002109
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002110
Guido van Rossumb6775db1994-08-01 11:34:53 +00002111#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002112static char posix_symlink__doc__[] =
2113"symlink(src, dst) -> None\n\
2114Create a symbolic link.";
2115
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002116static PyObject *
2117posix_symlink(self, args)
2118 PyObject *self;
2119 PyObject *args;
2120{
2121 return posix_2str(args, symlink);
2122}
2123#endif /* HAVE_SYMLINK */
2124
2125
2126#ifdef HAVE_TIMES
2127#ifndef HZ
2128#define HZ 60 /* Universal constant :-) */
2129#endif /* HZ */
2130
Guido van Rossumd48f2521997-12-05 22:19:34 +00002131#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2132static long
2133system_uptime()
2134{
2135 ULONG value = 0;
2136
2137 Py_BEGIN_ALLOW_THREADS
2138 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2139 Py_END_ALLOW_THREADS
2140
2141 return value;
2142}
2143
2144static PyObject *
2145posix_times(self, args)
2146 PyObject *self;
2147 PyObject *args;
2148{
2149 if (!PyArg_NoArgs(args))
2150 return NULL;
2151
2152 /* Currently Only Uptime is Provided -- Others Later */
2153 return Py_BuildValue("ddddd",
2154 (double)0 /* t.tms_utime / HZ */,
2155 (double)0 /* t.tms_stime / HZ */,
2156 (double)0 /* t.tms_cutime / HZ */,
2157 (double)0 /* t.tms_cstime / HZ */,
2158 (double)system_uptime() / 1000);
2159}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002160#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002161static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002162posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002163 PyObject *self;
2164 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002165{
2166 struct tms t;
2167 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002168 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002169 return NULL;
2170 errno = 0;
2171 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002172 if (c == (clock_t) -1)
2173 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002174 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002175 (double)t.tms_utime / HZ,
2176 (double)t.tms_stime / HZ,
2177 (double)t.tms_cutime / HZ,
2178 (double)t.tms_cstime / HZ,
2179 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002180}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002181#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002182#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002183
2184
Guido van Rossum87755a21996-09-07 00:59:43 +00002185#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002186#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002187static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002188posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 PyObject *self;
2190 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002191{
2192 FILETIME create, exit, kernel, user;
2193 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002194 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002195 return NULL;
2196 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002197 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2198 /* The fields of a FILETIME structure are the hi and lo part
2199 of a 64-bit value expressed in 100 nanosecond units.
2200 1e7 is one second in such units; 1e-7 the inverse.
2201 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2202 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 return Py_BuildValue(
2204 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002205 (double)(kernel.dwHighDateTime*429.4967296 +
2206 kernel.dwLowDateTime*1e-7),
2207 (double)(user.dwHighDateTime*429.4967296 +
2208 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002209 (double)0,
2210 (double)0,
2211 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002212}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002213#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002214
2215#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002216static char posix_times__doc__[] =
2217"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2218Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002219#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002220
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002221
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002223static char posix_setsid__doc__[] =
2224"setsid() -> None\n\
2225Call the system call setsid().";
2226
Barry Warsaw53699e91996-12-10 23:23:01 +00002227static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002228posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002229 PyObject *self;
2230 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002231{
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002233 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002234 if (setsid() < 0)
2235 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 Py_INCREF(Py_None);
2237 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002238}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002240
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002242static char posix_setpgid__doc__[] =
2243"setpgid(pid, pgrp) -> None\n\
2244Call the system call setpgid().";
2245
Barry Warsaw53699e91996-12-10 23:23:01 +00002246static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002247posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002248 PyObject *self;
2249 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002250{
2251 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002252 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002253 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002254 if (setpgid(pid, pgrp) < 0)
2255 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 Py_INCREF(Py_None);
2257 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002258}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002259#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002260
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002261
Guido van Rossumb6775db1994-08-01 11:34:53 +00002262#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002263static char posix_tcgetpgrp__doc__[] =
2264"tcgetpgrp(fd) -> pgid\n\
2265Return the process group associated with the terminal given by a fd.";
2266
Barry Warsaw53699e91996-12-10 23:23:01 +00002267static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002268posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002269 PyObject *self;
2270 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002271{
2272 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002274 return NULL;
2275 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002276 if (pgid < 0)
2277 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002278 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002279}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002280#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002282
Guido van Rossumb6775db1994-08-01 11:34:53 +00002283#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002284static char posix_tcsetpgrp__doc__[] =
2285"tcsetpgrp(fd, pgid) -> None\n\
2286Set the process group associated with the terminal given by a fd.";
2287
Barry Warsaw53699e91996-12-10 23:23:01 +00002288static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002289posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002290 PyObject *self;
2291 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002292{
2293 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002294 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002295 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002296 if (tcsetpgrp(fd, pgid) < 0)
2297 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002298 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002300}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002301#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002302
Guido van Rossum687dd131993-05-17 08:34:16 +00002303/* Functions acting on file descriptors */
2304
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002305static char posix_open__doc__[] =
2306"open(filename, flag [, mode=0777]) -> fd\n\
2307Open a file (for low level IO).";
2308
Barry Warsaw53699e91996-12-10 23:23:01 +00002309static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002310posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002311 PyObject *self;
2312 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002313{
2314 char *file;
2315 int flag;
2316 int mode = 0777;
2317 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002318 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2319 return NULL;
2320
Barry Warsaw53699e91996-12-10 23:23:01 +00002321 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002322 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002323 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002324 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002325 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002326 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002327}
2328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002329
2330static char posix_close__doc__[] =
2331"close(fd) -> None\n\
2332Close a file descriptor (for low level IO).";
2333
Barry Warsaw53699e91996-12-10 23:23:01 +00002334static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002335posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 PyObject *self;
2337 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002338{
2339 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002340 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002341 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002342 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002343 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002344 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002345 if (res < 0)
2346 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002347 Py_INCREF(Py_None);
2348 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002349}
2350
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002351
2352static char posix_dup__doc__[] =
2353"dup(fd) -> fd2\n\
2354Return a duplicate of a file descriptor.";
2355
Barry Warsaw53699e91996-12-10 23:23:01 +00002356static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002357posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002358 PyObject *self;
2359 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002360{
2361 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002362 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002363 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002364 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002365 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002366 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002367 if (fd < 0)
2368 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002370}
2371
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002372
2373static char posix_dup2__doc__[] =
2374"dup2(fd, fd2) -> None\n\
2375Duplicate file descriptor.";
2376
Barry Warsaw53699e91996-12-10 23:23:01 +00002377static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002378posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002379 PyObject *self;
2380 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002381{
2382 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002383 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002384 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002385 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002386 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002387 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002388 if (res < 0)
2389 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 Py_INCREF(Py_None);
2391 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002392}
2393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002394
2395static char posix_lseek__doc__[] =
2396"lseek(fd, pos, how) -> newpos\n\
2397Set the current position of a file descriptor.";
2398
Barry Warsaw53699e91996-12-10 23:23:01 +00002399static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002400posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002401 PyObject *self;
2402 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002403{
2404 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002405 off_t pos, res;
2406 PyObject *posobj;
2407 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002408 return NULL;
2409#ifdef SEEK_SET
2410 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2411 switch (how) {
2412 case 0: how = SEEK_SET; break;
2413 case 1: how = SEEK_CUR; break;
2414 case 2: how = SEEK_END; break;
2415 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002416#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002417
2418#if !defined(HAVE_LARGEFILE_SUPPORT)
2419 pos = PyInt_AsLong(posobj);
2420#else
2421 pos = PyLong_Check(posobj) ?
2422 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2423#endif
2424 if (PyErr_Occurred())
2425 return NULL;
2426
Barry Warsaw53699e91996-12-10 23:23:01 +00002427 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002428 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002429 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002430 if (res < 0)
2431 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002432
2433#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002434 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002435#else
2436 return PyLong_FromLongLong(res);
2437#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002438}
2439
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002440
2441static char posix_read__doc__[] =
2442"read(fd, buffersize) -> string\n\
2443Read a file descriptor.";
2444
Barry Warsaw53699e91996-12-10 23:23:01 +00002445static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002446posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002447 PyObject *self;
2448 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002449{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002450 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002451 PyObject *buffer;
2452 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002453 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002454 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002455 if (buffer == NULL)
2456 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002457 Py_BEGIN_ALLOW_THREADS
2458 n = read(fd, PyString_AsString(buffer), size);
2459 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002460 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002461 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002462 return posix_error();
2463 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002464 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002465 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002466 return buffer;
2467}
2468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002469
2470static char posix_write__doc__[] =
2471"write(fd, string) -> byteswritten\n\
2472Write a string to a file descriptor.";
2473
Barry Warsaw53699e91996-12-10 23:23:01 +00002474static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002475posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002476 PyObject *self;
2477 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002478{
2479 int fd, size;
2480 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002481 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002482 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002483 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002484 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002485 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002486 if (size < 0)
2487 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002488 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002489}
2490
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002491
2492static char posix_fstat__doc__[]=
2493"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2494Like stat(), but for an open file descriptor.";
2495
Barry Warsaw53699e91996-12-10 23:23:01 +00002496static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002497posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002498 PyObject *self;
2499 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002500{
2501 int fd;
2502 struct stat st;
2503 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002504 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002505 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002506 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002507 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002508 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002509 if (res != 0)
2510 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002511#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002512 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002513 (long)st.st_mode,
2514 (long)st.st_ino,
2515 (long)st.st_dev,
2516 (long)st.st_nlink,
2517 (long)st.st_uid,
2518 (long)st.st_gid,
2519 (long)st.st_size,
2520 (long)st.st_atime,
2521 (long)st.st_mtime,
2522 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002523#else
2524 return Py_BuildValue("(lLllllLlll)",
2525 (long)st.st_mode,
2526 (LONG_LONG)st.st_ino,
2527 (long)st.st_dev,
2528 (long)st.st_nlink,
2529 (long)st.st_uid,
2530 (long)st.st_gid,
2531 (LONG_LONG)st.st_size,
2532 (long)st.st_atime,
2533 (long)st.st_mtime,
2534 (long)st.st_ctime);
2535#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002536}
2537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
2539static char posix_fdopen__doc__[] =
2540"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2541Return an open file object connected to a file descriptor.";
2542
Barry Warsaw53699e91996-12-10 23:23:01 +00002543static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002544posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002545 PyObject *self;
2546 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002547{
Barry Warsaw53699e91996-12-10 23:23:01 +00002548 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002549 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002550 char *mode = "r";
2551 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002552 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002553 PyObject *f;
2554 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002555 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002556
Barry Warsaw53699e91996-12-10 23:23:01 +00002557 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002558 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002559 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002560 if (fp == NULL)
2561 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002562 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002563 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002565 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002566}
2567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002568
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002569#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002570static char posix_pipe__doc__[] =
2571"pipe() -> (read_end, write_end)\n\
2572Create a pipe.";
2573
Barry Warsaw53699e91996-12-10 23:23:01 +00002574static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002575posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002576 PyObject *self;
2577 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002578{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002579#if defined(PYOS_OS2)
2580 HFILE read, write;
2581 APIRET rc;
2582
2583 if (!PyArg_Parse(args, ""))
2584 return NULL;
2585
2586 Py_BEGIN_ALLOW_THREADS
2587 rc = DosCreatePipe( &read, &write, 4096);
2588 Py_END_ALLOW_THREADS
2589 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002590 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002591
2592 return Py_BuildValue("(ii)", read, write);
2593#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002594#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002595 int fds[2];
2596 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002597 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002598 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002599 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002600 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002601 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002602 if (res != 0)
2603 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002604 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002605#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002606 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002607 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002608 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002609 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002612 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002613 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002614 if (!ok)
2615 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002616 read_fd = _open_osfhandle((long)read, 0);
2617 write_fd = _open_osfhandle((long)write, 1);
2618 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002619#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002620#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002621}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002622#endif /* HAVE_PIPE */
2623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002625#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002626static char posix_mkfifo__doc__[] =
2627"mkfifo(file, [, mode=0666]) -> None\n\
2628Create a FIFO (a POSIX named pipe).";
2629
Barry Warsaw53699e91996-12-10 23:23:01 +00002630static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002631posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002632 PyObject *self;
2633 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002634{
2635 char *file;
2636 int mode = 0666;
2637 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002638 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002640 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002641 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002642 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002643 if (res < 0)
2644 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002645 Py_INCREF(Py_None);
2646 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002647}
2648#endif
2649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002650
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002651#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002652static char posix_ftruncate__doc__[] =
2653"ftruncate(fd, length) -> None\n\
2654Truncate a file to a specified length.";
2655
Barry Warsaw53699e91996-12-10 23:23:01 +00002656static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002657posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002658 PyObject *self; /* Not used */
2659 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002660{
2661 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002662 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002663 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002664 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002665
Guido van Rossum94f6f721999-01-06 18:42:14 +00002666 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2667 return NULL;
2668
2669#if !defined(HAVE_LARGEFILE_SUPPORT)
2670 length = PyInt_AsLong(lenobj);
2671#else
2672 length = PyLong_Check(lenobj) ?
2673 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2674#endif
2675 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002676 return NULL;
2677
Barry Warsaw53699e91996-12-10 23:23:01 +00002678 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002679 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002680 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002681 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002682 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002683 return NULL;
2684 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002685 Py_INCREF(Py_None);
2686 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002687}
2688#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002689
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002690#ifdef NeXT
2691#define HAVE_PUTENV
2692/* Steve Spicklemire got this putenv from NeXTAnswers */
2693static int
2694putenv(char *newval)
2695{
2696 extern char **environ;
2697
2698 static int firstTime = 1;
2699 char **ep;
2700 char *cp;
2701 int esiz;
2702 char *np;
2703
2704 if (!(np = strchr(newval, '=')))
2705 return 1;
2706 *np = '\0';
2707
2708 /* look it up */
2709 for (ep=environ ; *ep ; ep++)
2710 {
2711 /* this should always be true... */
2712 if (cp = strchr(*ep, '='))
2713 {
2714 *cp = '\0';
2715 if (!strcmp(*ep, newval))
2716 {
2717 /* got it! */
2718 *cp = '=';
2719 break;
2720 }
2721 *cp = '=';
2722 }
2723 else
2724 {
2725 *np = '=';
2726 return 1;
2727 }
2728 }
2729
2730 *np = '=';
2731 if (*ep)
2732 {
2733 /* the string was already there:
2734 just replace it with the new one */
2735 *ep = newval;
2736 return 0;
2737 }
2738
2739 /* expand environ by one */
2740 for (esiz=2, ep=environ ; *ep ; ep++)
2741 esiz++;
2742 if (firstTime)
2743 {
2744 char **epp;
2745 char **newenv;
2746 if (!(newenv = malloc(esiz * sizeof(char *))))
2747 return 1;
2748
2749 for (ep=environ, epp=newenv ; *ep ;)
2750 *epp++ = *ep++;
2751 *epp++ = newval;
2752 *epp = (char *) 0;
2753 environ = newenv;
2754 }
2755 else
2756 {
2757 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2758 return 1;
2759 environ[esiz - 2] = newval;
2760 environ[esiz - 1] = (char *) 0;
2761 firstTime = 0;
2762 }
2763
2764 return 0;
2765}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002766#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002768
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002769#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002770static char posix_putenv__doc__[] =
2771"putenv(key, value) -> None\n\
2772Change or add an environment variable.";
2773
Guido van Rossumbcc20741998-08-04 22:53:56 +00002774#ifdef __BEOS__
2775/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2776int putenv( const char *str );
2777#endif
2778
Barry Warsaw53699e91996-12-10 23:23:01 +00002779static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002780posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002781 PyObject *self;
2782 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002783{
2784 char *s1, *s2;
2785 char *new;
2786
Barry Warsaw53699e91996-12-10 23:23:01 +00002787 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002788 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002789
2790#if defined(PYOS_OS2)
2791 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2792 APIRET rc;
2793
2794 if (strlen(s2) == 0) /* If New Value is an Empty String */
2795 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2796
2797 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2798 if (rc != NO_ERROR)
2799 return os2_error(rc);
2800
2801 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2802 APIRET rc;
2803
2804 if (strlen(s2) == 0) /* If New Value is an Empty String */
2805 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2806
2807 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2808 if (rc != NO_ERROR)
2809 return os2_error(rc);
2810 } else {
2811#endif
2812
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002813 /* XXX This leaks memory -- not easy to fix :-( */
2814 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002815 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002816 (void) sprintf(new, "%s=%s", s1, s2);
2817 if (putenv(new)) {
2818 posix_error();
2819 return NULL;
2820 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002821
2822#if defined(PYOS_OS2)
2823 }
2824#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002825 Py_INCREF(Py_None);
2826 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002827}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002828#endif /* putenv */
2829
2830#ifdef HAVE_STRERROR
2831static char posix_strerror__doc__[] =
2832"strerror(code) -> string\n\
2833Translate an error code to a message string.";
2834
2835PyObject *
2836posix_strerror(self, args)
2837 PyObject *self;
2838 PyObject *args;
2839{
2840 int code;
2841 char *message;
2842 if (!PyArg_ParseTuple(args, "i", &code))
2843 return NULL;
2844 message = strerror(code);
2845 if (message == NULL) {
2846 PyErr_SetString(PyExc_ValueError,
2847 "strerror code out of range");
2848 return NULL;
2849 }
2850 return PyString_FromString(message);
2851}
2852#endif /* strerror */
2853
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002854
Guido van Rossumc9641791998-08-04 15:26:23 +00002855#ifdef HAVE_SYS_WAIT_H
2856
2857#ifdef WIFSTOPPED
2858static char posix_WIFSTOPPED__doc__[] =
2859"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002860Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002861
2862static PyObject *
2863posix_WIFSTOPPED(self, args)
2864 PyObject *self;
2865 PyObject *args;
2866{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002867#ifdef UNION_WAIT
2868 union wait status;
2869#define status_i (status.w_status)
2870#else
2871 int status;
2872#define status_i status
2873#endif
2874 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002875
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002876 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002877 {
2878 return NULL;
2879 }
2880
2881 return Py_BuildValue("i", WIFSTOPPED(status));
2882}
2883#endif /* WIFSTOPPED */
2884
2885#ifdef WIFSIGNALED
2886static char posix_WIFSIGNALED__doc__[] =
2887"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00002888Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002889
2890static PyObject *
2891posix_WIFSIGNALED(self, args)
2892 PyObject *self;
2893 PyObject *args;
2894{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002895#ifdef UNION_WAIT
2896 union wait status;
2897#define status_i (status.w_status)
2898#else
2899 int status;
2900#define status_i status
2901#endif
2902 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002903
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002904 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002905 {
2906 return NULL;
2907 }
2908
2909 return Py_BuildValue("i", WIFSIGNALED(status));
2910}
2911#endif /* WIFSIGNALED */
2912
2913#ifdef WIFEXITED
2914static char posix_WIFEXITED__doc__[] =
2915"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002916Return true if the process returning 'status' exited using the exit()\n\
2917system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002918
2919static PyObject *
2920posix_WIFEXITED(self, args)
2921 PyObject *self;
2922 PyObject *args;
2923{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002924#ifdef UNION_WAIT
2925 union wait status;
2926#define status_i (status.w_status)
2927#else
2928 int status;
2929#define status_i status
2930#endif
2931 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002932
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002933 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002934 {
2935 return NULL;
2936 }
2937
2938 return Py_BuildValue("i", WIFEXITED(status));
2939}
2940#endif /* WIFEXITED */
2941
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002942#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002943static char posix_WEXITSTATUS__doc__[] =
2944"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002945Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002946
2947static PyObject *
2948posix_WEXITSTATUS(self, args)
2949 PyObject *self;
2950 PyObject *args;
2951{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002952#ifdef UNION_WAIT
2953 union wait status;
2954#define status_i (status.w_status)
2955#else
2956 int status;
2957#define status_i status
2958#endif
2959 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002960
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002961 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002962 {
2963 return NULL;
2964 }
2965
2966 return Py_BuildValue("i", WEXITSTATUS(status));
2967}
2968#endif /* WEXITSTATUS */
2969
2970#ifdef WTERMSIG
2971static char posix_WTERMSIG__doc__[] =
2972"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002973Return the signal that terminated the process that provided the 'status'\n\
2974value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002975
2976static PyObject *
2977posix_WTERMSIG(self, args)
2978 PyObject *self;
2979 PyObject *args;
2980{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002981#ifdef UNION_WAIT
2982 union wait status;
2983#define status_i (status.w_status)
2984#else
2985 int status;
2986#define status_i status
2987#endif
2988 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002989
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002990 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002991 {
2992 return NULL;
2993 }
2994
2995 return Py_BuildValue("i", WTERMSIG(status));
2996}
2997#endif /* WTERMSIG */
2998
2999#ifdef WSTOPSIG
3000static char posix_WSTOPSIG__doc__[] =
3001"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003002Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003003
3004static PyObject *
3005posix_WSTOPSIG(self, args)
3006 PyObject *self;
3007 PyObject *args;
3008{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003009#ifdef UNION_WAIT
3010 union wait status;
3011#define status_i (status.w_status)
3012#else
3013 int status;
3014#define status_i status
3015#endif
3016 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003017
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003018 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003019 {
3020 return NULL;
3021 }
3022
3023 return Py_BuildValue("i", WSTOPSIG(status));
3024}
3025#endif /* WSTOPSIG */
3026
3027#endif /* HAVE_SYS_WAIT_H */
3028
3029
Guido van Rossum94f6f721999-01-06 18:42:14 +00003030#if defined(HAVE_FSTATVFS)
3031#include <sys/statvfs.h>
3032
3033static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003034"fstatvfs(fd) -> \n\
3035 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003036Perform an fstatvfs system call on the given fd.";
3037
3038static PyObject *
3039posix_fstatvfs(self, args)
3040 PyObject *self;
3041 PyObject *args;
3042{
3043 int fd, res;
3044 struct statvfs st;
3045 if (!PyArg_ParseTuple(args, "i", &fd))
3046 return NULL;
3047 Py_BEGIN_ALLOW_THREADS
3048 res = fstatvfs(fd, &st);
3049 Py_END_ALLOW_THREADS
3050 if (res != 0)
3051 return posix_error();
3052#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003053 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003054 (long) st.f_bsize,
3055 (long) st.f_frsize,
3056 (long) st.f_blocks,
3057 (long) st.f_bfree,
3058 (long) st.f_bavail,
3059 (long) st.f_files,
3060 (long) st.f_ffree,
3061 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003062 (long) st.f_flag,
3063 (long) st.f_namemax);
3064#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003065 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003066 (long) st.f_bsize,
3067 (long) st.f_frsize,
3068 (LONG_LONG) st.f_blocks,
3069 (LONG_LONG) st.f_bfree,
3070 (LONG_LONG) st.f_bavail,
3071 (LONG_LONG) st.f_files,
3072 (LONG_LONG) st.f_ffree,
3073 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003074 (long) st.f_flag,
3075 (long) st.f_namemax);
3076#endif
3077}
3078#endif /* HAVE_FSTATVFS */
3079
3080
3081#if defined(HAVE_STATVFS)
3082#include <sys/statvfs.h>
3083
3084static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003085"statvfs(path) -> \n\
3086 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003087Perform a statvfs system call on the given path.";
3088
3089static PyObject *
3090posix_statvfs(self, args)
3091 PyObject *self;
3092 PyObject *args;
3093{
3094 char *path;
3095 int res;
3096 struct statvfs st;
3097 if (!PyArg_ParseTuple(args, "s", &path))
3098 return NULL;
3099 Py_BEGIN_ALLOW_THREADS
3100 res = statvfs(path, &st);
3101 Py_END_ALLOW_THREADS
3102 if (res != 0)
3103 return posix_error_with_filename(path);
3104#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003105 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003106 (long) st.f_bsize,
3107 (long) st.f_frsize,
3108 (long) st.f_blocks,
3109 (long) st.f_bfree,
3110 (long) st.f_bavail,
3111 (long) st.f_files,
3112 (long) st.f_ffree,
3113 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003114 (long) st.f_flag,
3115 (long) st.f_namemax);
3116#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003117 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003118 (long) st.f_bsize,
3119 (long) st.f_frsize,
3120 (LONG_LONG) st.f_blocks,
3121 (LONG_LONG) st.f_bfree,
3122 (LONG_LONG) st.f_bavail,
3123 (LONG_LONG) st.f_files,
3124 (LONG_LONG) st.f_ffree,
3125 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003126 (long) st.f_flag,
3127 (long) st.f_namemax);
3128#endif
3129}
3130#endif /* HAVE_STATVFS */
3131
3132
Barry Warsaw53699e91996-12-10 23:23:01 +00003133static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003134 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003135#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003136 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003137#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3139 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003140#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003142#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003143#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003144 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003145#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003146#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003147 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003148#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003149 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3150 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3151 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003152#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003153 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003154#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003155#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003157#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003158 {"rename", posix_rename, 0, posix_rename__doc__},
3159 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3160 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003161#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003162 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003163#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003164#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003166#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003168#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003170#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003171 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3172 {"remove", posix_unlink, 0, posix_remove__doc__},
3173 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003174#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003176#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003177 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003178#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003179 {"execv", posix_execv, 0, posix_execv__doc__},
3180 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003181#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003182#ifdef HAVE_SPAWNV
3183 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3184 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3185#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003186#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003187 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003188#endif /* HAVE_FORK */
3189#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003190 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003191#endif /* HAVE_GETEGID */
3192#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003194#endif /* HAVE_GETEUID */
3195#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003196 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003197#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003199#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003200 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003201#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003202#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003203 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003204#endif /* HAVE_GETPPID */
3205#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003206 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003207#endif /* HAVE_GETUID */
3208#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003209 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003210#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003211#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003212 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003213#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003214#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003216#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003217#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003218 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003219#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003220#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003221 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003222#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003223#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003224 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003225#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003226#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003227 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003228#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003229#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003230 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003231#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003232#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003233 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003234#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003235#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003236 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003237#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003238#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003239 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003240#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003241#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003242 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003243#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244 {"open", posix_open, 1, posix_open__doc__},
3245 {"close", posix_close, 0, posix_close__doc__},
3246 {"dup", posix_dup, 0, posix_dup__doc__},
3247 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3248 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3249 {"read", posix_read, 0, posix_read__doc__},
3250 {"write", posix_write, 0, posix_write__doc__},
3251 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3252 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003253#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003254 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003255#endif
3256#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003258#endif
3259#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003260 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003261#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003262#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003264#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003265#ifdef HAVE_STRERROR
3266 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3267#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003268#ifdef HAVE_FSYNC
3269 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3270#endif
3271#ifdef HAVE_FDATASYNC
3272 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3273#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003274#ifdef HAVE_SYS_WAIT_H
3275#ifdef WIFSTOPPED
3276 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3277#endif /* WIFSTOPPED */
3278#ifdef WIFSIGNALED
3279 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3280#endif /* WIFSIGNALED */
3281#ifdef WIFEXITED
3282 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3283#endif /* WIFEXITED */
3284#ifdef WEXITSTATUS
3285 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3286#endif /* WEXITSTATUS */
3287#ifdef WTERMSIG
3288 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3289#endif /* WTERMSIG */
3290#ifdef WSTOPSIG
3291 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3292#endif /* WSTOPSIG */
3293#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003294#ifdef HAVE_FSTATVFS
3295 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3296#endif
3297#ifdef HAVE_STATVFS
3298 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3299#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003300 {NULL, NULL} /* Sentinel */
3301};
3302
3303
Barry Warsaw4a342091996-12-19 23:50:02 +00003304static int
3305ins(d, symbol, value)
3306 PyObject* d;
3307 char* symbol;
3308 long value;
3309{
3310 PyObject* v = PyInt_FromLong(value);
3311 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3312 return -1; /* triggers fatal error */
3313
3314 Py_DECREF(v);
3315 return 0;
3316}
3317
Guido van Rossumd48f2521997-12-05 22:19:34 +00003318#if defined(PYOS_OS2)
3319/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3320static int insertvalues(PyObject *d)
3321{
3322 APIRET rc;
3323 ULONG values[QSV_MAX+1];
3324 PyObject *v;
3325 char *ver, tmp[10];
3326
3327 Py_BEGIN_ALLOW_THREADS
3328 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3329 Py_END_ALLOW_THREADS
3330
3331 if (rc != NO_ERROR) {
3332 os2_error(rc);
3333 return -1;
3334 }
3335
3336 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3337 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3338 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3339 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3340 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3341 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3342 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3343
3344 switch (values[QSV_VERSION_MINOR]) {
3345 case 0: ver = "2.00"; break;
3346 case 10: ver = "2.10"; break;
3347 case 11: ver = "2.11"; break;
3348 case 30: ver = "3.00"; break;
3349 case 40: ver = "4.00"; break;
3350 case 50: ver = "5.00"; break;
3351 default:
3352 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3353 values[QSV_VERSION_MINOR]);
3354 ver = &tmp[0];
3355 }
3356
3357 /* Add Indicator of the Version of the Operating System */
3358 v = PyString_FromString(ver);
3359 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3360 return -1;
3361 Py_DECREF(v);
3362
3363 /* Add Indicator of Which Drive was Used to Boot the System */
3364 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3365 tmp[1] = ':';
3366 tmp[2] = '\0';
3367
3368 v = PyString_FromString(tmp);
3369 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3370 return -1;
3371 Py_DECREF(v);
3372
3373 return 0;
3374}
3375#endif
3376
Barry Warsaw4a342091996-12-19 23:50:02 +00003377static int
3378all_ins(d)
3379 PyObject* d;
3380{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003381#ifdef F_OK
3382 if (ins(d, "F_OK", (long)F_OK)) return -1;
3383#endif
3384#ifdef R_OK
3385 if (ins(d, "R_OK", (long)R_OK)) return -1;
3386#endif
3387#ifdef W_OK
3388 if (ins(d, "W_OK", (long)W_OK)) return -1;
3389#endif
3390#ifdef X_OK
3391 if (ins(d, "X_OK", (long)X_OK)) return -1;
3392#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003393#ifdef WNOHANG
3394 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3395#endif
3396#ifdef O_RDONLY
3397 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3398#endif
3399#ifdef O_WRONLY
3400 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3401#endif
3402#ifdef O_RDWR
3403 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3404#endif
3405#ifdef O_NDELAY
3406 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3407#endif
3408#ifdef O_NONBLOCK
3409 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3410#endif
3411#ifdef O_APPEND
3412 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3413#endif
3414#ifdef O_DSYNC
3415 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3416#endif
3417#ifdef O_RSYNC
3418 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3419#endif
3420#ifdef O_SYNC
3421 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3422#endif
3423#ifdef O_NOCTTY
3424 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3425#endif
3426#ifdef O_CREAT
3427 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3428#endif
3429#ifdef O_EXCL
3430 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3431#endif
3432#ifdef O_TRUNC
3433 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3434#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003435#ifdef O_BINARY
3436 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3437#endif
3438#ifdef O_TEXT
3439 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3440#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003441
Guido van Rossum246bc171999-02-01 23:54:31 +00003442#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00003443 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
3444 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
3445 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3446 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
3447 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00003448#endif
3449
Guido van Rossumd48f2521997-12-05 22:19:34 +00003450#if defined(PYOS_OS2)
3451 if (insertvalues(d)) return -1;
3452#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003453 return 0;
3454}
3455
3456
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003457#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003458#define INITFUNC initnt
3459#define MODNAME "nt"
3460#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003461#if defined(PYOS_OS2)
3462#define INITFUNC initos2
3463#define MODNAME "os2"
3464#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003465#define INITFUNC initposix
3466#define MODNAME "posix"
3467#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003468#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003469
Guido van Rossum3886bb61998-12-04 18:50:17 +00003470DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003471INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003472{
Barry Warsaw53699e91996-12-10 23:23:01 +00003473 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003474
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003475 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003476 posix_methods,
3477 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003478 (PyObject *)NULL,
3479 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003480 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003481
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003482 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003483 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003484 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003485 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003486 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003487
Barry Warsaw4a342091996-12-19 23:50:02 +00003488 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003489 return;
3490
Barry Warsawca74da41999-02-09 19:31:45 +00003491 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003492}