blob: 3069d34be788610988e8ae787793861486c33c3a [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
49
50
Barry Warsaw53699e91996-12-10 23:23:01 +000051#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052
Guido van Rossumb6775db1994-08-01 11:34:53 +000053#include <sys/types.h>
54#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000055#ifdef HAVE_SYS_WAIT_H
56#include <sys/wait.h> /* For WNOHANG */
57#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000058
Guido van Rossuma376cc51996-12-05 23:43:35 +000059#ifdef HAVE_SIGNAL_H
60#include <signal.h>
61#endif
62
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include "mytime.h" /* For clock_t on some systems */
64
65#ifdef HAVE_FCNTL_H
66#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000067#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000068
Guido van Rossuma4916fa1996-05-23 22:58:55 +000069/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000070/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000071#ifdef __WATCOMC__ /* Watcom compiler */
72#define HAVE_GETCWD 1
73#define HAVE_OPENDIR 1
74#define HAVE_SYSTEM 1
75#if defined(__OS2__)
76#define HAVE_EXECV 1
77#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000078#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000079#include <process.h>
80#else
81#ifdef __BORLANDC__ /* Borland compiler */
82#define HAVE_EXECV 1
83#define HAVE_GETCWD 1
84#define HAVE_GETEGID 1
85#define HAVE_GETEUID 1
86#define HAVE_GETGID 1
87#define HAVE_GETPPID 1
88#define HAVE_GETUID 1
89#define HAVE_KILL 1
90#define HAVE_OPENDIR 1
91#define HAVE_PIPE 1
92#define HAVE_POPEN 1
93#define HAVE_SYSTEM 1
94#define HAVE_WAIT 1
95#else
96#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000097#define HAVE_GETCWD 1
98#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099#define HAVE_EXECV 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
103#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000104#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#else /* all other compilers */
106/* Unix functions that the configure script doesn't check for */
107#define HAVE_EXECV 1
108#define HAVE_FORK 1
109#define HAVE_GETCWD 1
110#define HAVE_GETEGID 1
111#define HAVE_GETEUID 1
112#define HAVE_GETGID 1
113#define HAVE_GETPPID 1
114#define HAVE_GETUID 1
115#define HAVE_KILL 1
116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
118#define HAVE_POPEN 1
119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#endif /* _MSC_VER */
122#endif /* __BORLANDC__ */
123#endif /* ! __WATCOMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000124
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000126
Guido van Rossumb6775db1994-08-01 11:34:53 +0000127#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000128#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000129#endif
130
131#ifdef NeXT
132/* NeXT's <unistd.h> and <utime.h> aren't worth much */
133#undef HAVE_UNISTD_H
134#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000135#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000136/* #undef HAVE_GETCWD */
137#endif
138
139#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000140/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
141extern int rename();
142extern int pclose();
143extern int lstat();
144extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#else /* !HAVE_UNISTD_H */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000146#if defined(__WATCOMC__) || defined(_MSC_VER)
Barry Warsaw53699e91996-12-10 23:23:01 +0000147extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000148#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000149extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000151extern int chdir Py_PROTO((const char *));
152extern int rmdir Py_PROTO((const char *));
153extern int chmod Py_PROTO((const char *, mode_t));
154extern int chown Py_PROTO((const char *, uid_t, gid_t));
155extern char *getcwd Py_PROTO((char *, int));
156extern char *strerror Py_PROTO((int));
157extern int link Py_PROTO((const char *, const char *));
158extern int rename Py_PROTO((const char *, const char *));
159extern int stat Py_PROTO((const char *, struct stat *));
160extern int unlink Py_PROTO((const char *));
161extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000163extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000164#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000166extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000167#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000169
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000170#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172#ifdef HAVE_UTIME_H
173#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000174#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000176#ifdef HAVE_SYS_UTIME_H
177#include <sys/utime.h>
178#define HAVE_UTIME_H /* pretend we do for the rest of this file */
179#endif /* HAVE_SYS_UTIME_H */
180
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181#ifdef HAVE_SYS_TIMES_H
182#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000183#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184
185#ifdef HAVE_SYS_PARAM_H
186#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000187#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188
189#ifdef HAVE_SYS_UTSNAME_H
190#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000191#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192
193#ifndef MAXPATHLEN
194#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000195#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000197#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000199#define NAMLEN(dirent) strlen((dirent)->d_name)
200#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201#ifdef __WATCOMC__
202#include <direct.h>
203#define NAMLEN(dirent) strlen((dirent)->d_name)
204#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000207#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000208#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000210#endif
211#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000213#endif
214#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#endif
217#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#include <direct.h>
221#include <io.h>
222#include <process.h>
223#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000224#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000226#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000227#else /* 16-bit Windows */
228#include <dos.h>
229#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000230#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
233#ifdef OS2
234#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000235#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236
237/* Return a dictionary corresponding to the POSIX environment table */
238
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#if !defined(_MSC_VER) && !defined(__WATCOMC__)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242
Barry Warsaw53699e91996-12-10 23:23:01 +0000243static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244convertenviron()
245{
Barry Warsaw53699e91996-12-10 23:23:01 +0000246 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000248 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 if (d == NULL)
250 return NULL;
251 if (environ == NULL)
252 return d;
253 /* XXX This part ignores errors */
254 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000255 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 char *p = strchr(*e, '=');
257 if (p == NULL)
258 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000259 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 if (v == NULL)
261 continue;
262 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000263 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000265 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 }
267 return d;
268}
269
270
Barry Warsaw53699e91996-12-10 23:23:01 +0000271static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272
273/* Set a POSIX-specific error from errno, and return NULL */
274
Barry Warsaw53699e91996-12-10 23:23:01 +0000275static PyObject * posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000276{
Barry Warsaw53699e91996-12-10 23:23:01 +0000277 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278}
279
280
281/* POSIX generic methods */
282
Barry Warsaw53699e91996-12-10 23:23:01 +0000283static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000285 PyObject *args;
286 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000288 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000289 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000290 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000292 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000293 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000294 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000295 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000297 Py_INCREF(Py_None);
298 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299}
300
Barry Warsaw53699e91996-12-10 23:23:01 +0000301static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000303 PyObject *args;
304 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000306 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000307 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000308 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000310 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000311 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000312 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000313 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000315 Py_INCREF(Py_None);
316 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317}
318
Barry Warsaw53699e91996-12-10 23:23:01 +0000319static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000321 PyObject *args;
322 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000324 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000326 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000327 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000329 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000330 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000331 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000332 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000334 Py_INCREF(Py_None);
335 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
337
Barry Warsaw53699e91996-12-10 23:23:01 +0000338static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000340 PyObject *args;
341 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000342{
343 char *path;
344 int i,i2;
345 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000346 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000347 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000350 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000351 if (res < 0)
352 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000353 Py_INCREF(Py_None);
354 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000355}
356
Barry Warsaw53699e91996-12-10 23:23:01 +0000357static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000359 PyObject *self;
360 PyObject *args;
361 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362{
363 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000364 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000365 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000366 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000368 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000369 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000370 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000371 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000373 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000374 (long)st.st_mode,
375 (long)st.st_ino,
376 (long)st.st_dev,
377 (long)st.st_nlink,
378 (long)st.st_uid,
379 (long)st.st_gid,
380 (long)st.st_size,
381 (long)st.st_atime,
382 (long)st.st_mtime,
383 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384}
385
386
387/* POSIX methods */
388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000389static char posix_chdir__doc__[] =
390"chdir(path) -> None\n\
391Change the current working directory to the specified path.";
392
Barry Warsaw53699e91996-12-10 23:23:01 +0000393static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000395 PyObject *self;
396 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 return posix_1str(args, chdir);
399}
400
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000401
402static char posix_chmod__doc__[] =
403"chmod(path, mode) -> None\n\
404Change the access permissions of a file.";
405
Barry Warsaw53699e91996-12-10 23:23:01 +0000406static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000408 PyObject *self;
409 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 return posix_strint(args, chmod);
412}
413
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000414
Guido van Rossumb6775db1994-08-01 11:34:53 +0000415#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000416static char posix_chown__doc__[] =
417"chown(path, uid, gid) -> None\n\
418Change the owner and group id of path to the numeric uid and gid.";
419
Barry Warsaw53699e91996-12-10 23:23:01 +0000420static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000421posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000422 PyObject *self;
423 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000424{
425 return posix_strintint(args, chown);
426}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000427#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000429
Guido van Rossum36bc6801995-06-14 22:54:23 +0000430#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000431static char posix_getcwd__doc__[] =
432"getcwd() -> path\n\
433Return a string representing the current working directory.";
434
Barry Warsaw53699e91996-12-10 23:23:01 +0000435static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000437 PyObject *self;
438 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439{
440 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000441 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000444 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000445 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000446 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000447 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000451#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000453
Guido van Rossumb6775db1994-08-01 11:34:53 +0000454#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000455static char posix_link__doc__[] =
456"link(src, dst) -> None\n\
457Create a hard link to a file.";
458
Barry Warsaw53699e91996-12-10 23:23:01 +0000459static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *self;
462 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464 return posix_2str(args, link);
465}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000466#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000468
469static char posix_listdir__doc__[] =
470"listdir(path) -> list_of_strings\n\
471Return a list containing the names of the entries in the directory.\n\
472\n\
473 path: path of directory to list\n\
474\n\
475The list is in arbitrary order. It does not include the special\n\
476entries '.' and '..' even if they are present in the directory.";
477
Barry Warsaw53699e91996-12-10 23:23:01 +0000478static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000479posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 PyObject *self;
481 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000482{
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000483 /* XXX Should redo this putting the three versions of opendir
484 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000485#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000486
Guido van Rossumb6775db1994-08-01 11:34:53 +0000487 char *name;
488 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000490 HANDLE hFindFile;
491 WIN32_FIND_DATA FileData;
492 char namebuf[MAX_PATH+5];
493
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000495 return NULL;
496 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000497 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000498 return NULL;
499 }
500 strcpy(namebuf, name);
501 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
502 namebuf[len++] = '/';
503 strcpy(namebuf + len, "*.*");
504
Barry Warsaw53699e91996-12-10 23:23:01 +0000505 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000506 return NULL;
507
508 hFindFile = FindFirstFile(namebuf, &FileData);
509 if (hFindFile == INVALID_HANDLE_VALUE) {
510 errno = GetLastError();
511 return posix_error();
512 }
513 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000514 if (FileData.cFileName[0] == '.' &&
515 (FileData.cFileName[1] == '\0' ||
516 FileData.cFileName[1] == '.' &&
517 FileData.cFileName[2] == '\0'))
518 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000519 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000520 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000521 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000522 d = NULL;
523 break;
524 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000525 if (PyList_Append(d, v) != 0) {
526 Py_DECREF(v);
527 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000528 d = NULL;
529 break;
530 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000531 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000532 } while (FindNextFile(hFindFile, &FileData) == TRUE);
533
534 if (FindClose(hFindFile) == FALSE) {
535 errno = GetLastError();
536 return posix_error();
537 }
538
539 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000540
Guido van Rossum8d665e61996-06-26 18:22:49 +0000541#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000542#ifdef _MSC_VER /* 16-bit Windows */
543
544#ifndef MAX_PATH
545#define MAX_PATH 250
546#endif
547 char *name, *pt;
548 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000549 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000550 char namebuf[MAX_PATH+5];
551 struct _find_t ep;
552
Barry Warsaw53699e91996-12-10 23:23:01 +0000553 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000554 return NULL;
555 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000556 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000557 return NULL;
558 }
559 strcpy(namebuf, name);
560 for (pt = namebuf; *pt; pt++)
561 if (*pt == '/')
562 *pt = '\\';
563 if (namebuf[len-1] != '\\')
564 namebuf[len++] = '\\';
565 strcpy(namebuf + len, "*.*");
566
Barry Warsaw53699e91996-12-10 23:23:01 +0000567 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000568 return NULL;
569
570 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000571 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
572 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000573 errno = ENOENT;
574 return posix_error();
575 }
576 do {
577 if (ep.name[0] == '.' &&
578 (ep.name[1] == '\0' ||
579 ep.name[1] == '.' &&
580 ep.name[2] == '\0'))
581 continue;
582 strcpy(namebuf, ep.name);
583 for (pt = namebuf; *pt; pt++)
584 if (isupper(*pt))
585 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000586 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000587 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000588 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000589 d = NULL;
590 break;
591 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 if (PyList_Append(d, v) != 0) {
593 Py_DECREF(v);
594 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000595 d = NULL;
596 break;
597 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000598 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000599 } while (_dos_findnext(&ep) == 0);
600
601 return d;
602
603#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000604
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000605 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000606 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000608 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000609 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000615 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000616 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000618 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619 return NULL;
620 }
621 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000622 if (ep->d_name[0] == '.' &&
623 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000624 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000625 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000626 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000628 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629 d = NULL;
630 break;
631 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 if (PyList_Append(d, v) != 0) {
633 Py_DECREF(v);
634 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635 d = NULL;
636 break;
637 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000638 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 }
640 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000641 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000644
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000645#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000646#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647}
648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000649static char posix_mkdir__doc__[] =
650"mkdir(path [, mode=0777]) -> None\n\
651Create a directory.";
652
Barry Warsaw53699e91996-12-10 23:23:01 +0000653static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000655 PyObject *self;
656 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000657{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000658 int res;
659 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000660 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000662 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8d665e61996-06-26 18:22:49 +0000664#if defined(__WATCOMC__) || defined(_MSC_VER)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000665 res = mkdir(path);
666#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000667 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000668#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000670 if (res < 0)
671 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000672 Py_INCREF(Py_None);
673 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674}
675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000676
Guido van Rossumb6775db1994-08-01 11:34:53 +0000677#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000678static char posix_nice__doc__[] =
679"nice(inc) -> new_priority\n\
680Decrease the priority of process and return new priority.";
681
Barry Warsaw53699e91996-12-10 23:23:01 +0000682static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000683posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000684 PyObject *self;
685 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000686{
687 int increment, value;
688
Barry Warsaw53699e91996-12-10 23:23:01 +0000689 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000690 return NULL;
691 value = nice(increment);
692 if (value == -1)
693 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000694 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000695}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000696#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000698
699static char posix_rename__doc__[] =
700"rename(old, new) -> None\n\
701Rename a file or directory.";
702
Barry Warsaw53699e91996-12-10 23:23:01 +0000703static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000705 PyObject *self;
706 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708 return posix_2str(args, rename);
709}
710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000711
712static char posix_rmdir__doc__[] =
713"rmdir(path) -> None\n\
714Remove a directory.";
715
Barry Warsaw53699e91996-12-10 23:23:01 +0000716static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000718 PyObject *self;
719 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000720{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721 return posix_1str(args, rmdir);
722}
723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000724
725static char posix_stat__doc__[] =
726"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
727Perform a stat system call on the given path.";
728
Barry Warsaw53699e91996-12-10 23:23:01 +0000729static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000731 PyObject *self;
732 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734 return posix_do_stat(self, args, stat);
735}
736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000737
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000738#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000739static char posix_system__doc__[] =
740"system(command) -> exit_status\n\
741Execute the command (a string) in a subshell.";
742
Barry Warsaw53699e91996-12-10 23:23:01 +0000743static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000745 PyObject *self;
746 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000748 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000749 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000750 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000752 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000753 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000754 Py_END_ALLOW_THREADS
755 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000757#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000759
760static char posix_umask__doc__[] =
761"umask(new_mask) -> old_mask\n\
762Set the current numeric umask and return the previous umask.";
763
Barry Warsaw53699e91996-12-10 23:23:01 +0000764static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000766 PyObject *self;
767 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768{
769 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000770 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 return NULL;
772 i = umask(i);
773 if (i < 0)
774 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000775 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776}
777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000778
779static char posix_unlink__doc__[] =
780"unlink(path) -> None\n\
781Remove a file (same as remove(path)).";
782
783static char posix_remove__doc__[] =
784"remove(path) -> None\n\
785Remove a file (same as unlink(path)).";
786
Barry Warsaw53699e91996-12-10 23:23:01 +0000787static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000789 PyObject *self;
790 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792 return posix_1str(args, unlink);
793}
794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000795
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000797static char posix_uname__doc__[] =
798"uname() -> (sysname, nodename, release, version, machine)\n\
799Return a tuple identifying the current operating system.";
800
Barry Warsaw53699e91996-12-10 23:23:01 +0000801static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000802posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000803 PyObject *self;
804 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000805{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000806 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000807 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000808 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000809 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000810 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000811 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +0000812 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000813 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000814 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +0000816 u.sysname,
817 u.nodename,
818 u.release,
819 u.version,
820 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000821}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000824
825static char posix_utime__doc__[] =
826"utime(path, (atime, utime)) -> None\n\
827Set the access and modified time of the file to the given values.";
828
Barry Warsaw53699e91996-12-10 23:23:01 +0000829static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000830posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000831 PyObject *self;
832 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000834 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000835 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000836 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000837
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000838/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000840 struct utimbuf buf;
841#define ATIME buf.actime
842#define MTIME buf.modtime
843#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000844#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000845 time_t buf[2];
846#define ATIME buf[0]
847#define MTIME buf[1]
848#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000849#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000850
Barry Warsaw53699e91996-12-10 23:23:01 +0000851 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000852 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000853 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +0000854 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +0000855 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000856 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +0000857 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000858 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000860 Py_INCREF(Py_None);
861 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000862#undef UTIME_ARG
863#undef ATIME
864#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Guido van Rossum85e3b011991-06-03 12:42:10 +0000867
Guido van Rossum3b066191991-06-04 19:40:25 +0000868/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000870static char posix__exit__doc__[] =
871"_exit(status)\n\
872Exit to the system with specified status, without normal exit processing.";
873
Barry Warsaw53699e91996-12-10 23:23:01 +0000874static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000875posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000876 PyObject *self;
877 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000878{
879 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000880 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000881 return NULL;
882 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000883 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000884}
885
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000886
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000887#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000888static char posix_execv__doc__[] =
889"execv(path, args)\n\
890Execute an executable path with arguments, replacing current process.\n\
891\n\
892 path: path of executable file\n\
893 args: tuple or list of strings";
894
Barry Warsaw53699e91996-12-10 23:23:01 +0000895static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +0000896posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000897 PyObject *self;
898 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000899{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000900 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000901 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000902 char **argvlist;
903 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000904 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +0000905
Guido van Rossum89b33251993-10-22 14:26:06 +0000906 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000907 argv is a list or tuple of strings. */
908
Barry Warsaw53699e91996-12-10 23:23:01 +0000909 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000910 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000911 if (PyList_Check(argv)) {
912 argc = PyList_Size(argv);
913 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000914 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000915 else if (PyTuple_Check(argv)) {
916 argc = PyTuple_Size(argv);
917 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000918 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000919 else {
920 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +0000921 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000922 return NULL;
923 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000924
Barry Warsaw53699e91996-12-10 23:23:01 +0000925 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000926 if (argvlist == NULL)
927 return NULL;
928 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000929 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
930 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000931 goto badarg;
932 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000933 }
934 argvlist[argc] = NULL;
935
Guido van Rossumb6775db1994-08-01 11:34:53 +0000936#ifdef BAD_EXEC_PROTOTYPES
937 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000938#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000939 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000940#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000941
Guido van Rossum85e3b011991-06-03 12:42:10 +0000942 /* If we get here it's definitely an error */
943
Barry Warsaw53699e91996-12-10 23:23:01 +0000944 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000945 return posix_error();
946}
947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000948
949static char posix_execve__doc__[] =
950"execve(path, args, env)\n\
951Execute a path with arguments and environment, replacing current process.\n\
952\n\
953 path: path of executable file\n\
954 args: tuple or list of arguments\n\
955 env: dictonary of strings mapping to strings";
956
Barry Warsaw53699e91996-12-10 23:23:01 +0000957static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000958posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000959 PyObject *self;
960 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000961{
962 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000963 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000964 char **argvlist;
965 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000966 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000967 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000968 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000969
970 /* execve has three arguments: (path, argv, env), where
971 argv is a list or tuple of strings and env is a dictionary
972 like posix.environ. */
973
Barry Warsaw53699e91996-12-10 23:23:01 +0000974 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000975 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 if (PyList_Check(argv)) {
977 argc = PyList_Size(argv);
978 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000979 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000980 else if (PyTuple_Check(argv)) {
981 argc = PyTuple_Size(argv);
982 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000983 }
984 else {
Barry Warsaw53699e91996-12-10 23:23:01 +0000985 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000986 return NULL;
987 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000988 if (!PyMapping_Check(env)) {
989 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000990 return NULL;
991 }
992
Barry Warsaw53699e91996-12-10 23:23:01 +0000993 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000994 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000995 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000996 return NULL;
997 }
998 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000999 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001000 "s;argv must be list of strings",
1001 &argvlist[i]))
1002 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001003 goto fail_1;
1004 }
1005 }
1006 argvlist[argc] = NULL;
1007
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001008 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001009 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001010 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001011 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001012 goto fail_1;
1013 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001014 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001015 keys = PyMapping_Keys(env);
1016 vals = PyMapping_Values(env);
1017 if (!keys || !vals)
1018 goto fail_2;
1019
1020 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001021 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001022
1023 key = PyList_GetItem(keys, pos);
1024 val = PyList_GetItem(vals, pos);
1025 if (!key || !val)
1026 goto fail_2;
1027
Barry Warsaw53699e91996-12-10 23:23:01 +00001028 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001029 !PyArg_Parse(val, "s;non-string value in env", &v))
1030 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001031 goto fail_2;
1032 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001033 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001034 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001035 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001036 goto fail_2;
1037 }
1038 sprintf(p, "%s=%s", k, v);
1039 envlist[envc++] = p;
1040 }
1041 envlist[envc] = 0;
1042
Guido van Rossumb6775db1994-08-01 11:34:53 +00001043
1044#ifdef BAD_EXEC_PROTOTYPES
1045 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001046#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001047 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001048#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001049
1050 /* If we get here it's definitely an error */
1051
1052 (void) posix_error();
1053
1054 fail_2:
1055 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001056 PyMem_DEL(envlist[envc]);
1057 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001058 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001059 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001060 Py_XDECREF(vals);
1061 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001062 return NULL;
1063}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001064#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001066
Guido van Rossumad0ee831995-03-01 10:34:45 +00001067#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001068static char posix_fork__doc__[] =
1069"fork() -> pid\n\
1070Fork a child process.\n\
1071\n\
1072Return 0 to child process and PID of child to parent process.";
1073
Barry Warsaw53699e91996-12-10 23:23:01 +00001074static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001075posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001076 PyObject *self;
1077 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001078{
1079 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001080 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001081 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001082 pid = fork();
1083 if (pid == -1)
1084 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001085 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001086}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001087#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001088
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001089
Guido van Rossumad0ee831995-03-01 10:34:45 +00001090#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001091static char posix_getegid__doc__[] =
1092"getegid() -> egid\n\
1093Return the current process's effective group id.";
1094
Barry Warsaw53699e91996-12-10 23:23:01 +00001095static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001096posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001097 PyObject *self;
1098 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001099{
Barry Warsaw53699e91996-12-10 23:23:01 +00001100 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001101 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001102 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001103}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001104#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001105
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001106
Guido van Rossumad0ee831995-03-01 10:34:45 +00001107#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001108static char posix_geteuid__doc__[] =
1109"geteuid() -> euid\n\
1110Return the current process's effective user id.";
1111
Barry Warsaw53699e91996-12-10 23:23:01 +00001112static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001113posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 PyObject *self;
1115 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001116{
Barry Warsaw53699e91996-12-10 23:23:01 +00001117 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001118 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001119 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001120}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001121#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001123
Guido van Rossumad0ee831995-03-01 10:34:45 +00001124#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001125static char posix_getgid__doc__[] =
1126"getgid() -> gid\n\
1127Return the current process's group id.";
1128
Barry Warsaw53699e91996-12-10 23:23:01 +00001129static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001130posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001131 PyObject *self;
1132 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001133{
Barry Warsaw53699e91996-12-10 23:23:01 +00001134 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001135 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001136 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001137}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001138#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001140
1141static char posix_getpid__doc__[] =
1142"getpid() -> pid\n\
1143Return the current process id";
1144
Barry Warsaw53699e91996-12-10 23:23:01 +00001145static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001146posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001147 PyObject *self;
1148 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001149{
Barry Warsaw53699e91996-12-10 23:23:01 +00001150 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001151 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001152 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001153}
1154
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001155
Guido van Rossumb6775db1994-08-01 11:34:53 +00001156#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001157static char posix_getpgrp__doc__[] =
1158"getpgrp() -> pgrp\n\
1159Return the current process group id.";
1160
Barry Warsaw53699e91996-12-10 23:23:01 +00001161static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001162posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001163 PyObject *self;
1164 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001165{
Barry Warsaw53699e91996-12-10 23:23:01 +00001166 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001167 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001168#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001169 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001170#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001171 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001172#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001173}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001174#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001176
Guido van Rossumb6775db1994-08-01 11:34:53 +00001177#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001178static char posix_setpgrp__doc__[] =
1179"setpgrp() -> None\n\
1180Make this process a session leader.";
1181
Barry Warsaw53699e91996-12-10 23:23:01 +00001182static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001183posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001184 PyObject *self;
1185 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001186{
Barry Warsaw53699e91996-12-10 23:23:01 +00001187 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001188 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001189#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001190 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001191#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001192 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001193#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001194 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001195 Py_INCREF(Py_None);
1196 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001197}
1198
Guido van Rossumb6775db1994-08-01 11:34:53 +00001199#endif /* HAVE_SETPGRP */
1200
Guido van Rossumad0ee831995-03-01 10:34:45 +00001201#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001202static char posix_getppid__doc__[] =
1203"getppid() -> ppid\n\
1204Return the parent's process id.";
1205
Barry Warsaw53699e91996-12-10 23:23:01 +00001206static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001207posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001208 PyObject *self;
1209 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001210{
Barry Warsaw53699e91996-12-10 23:23:01 +00001211 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001212 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001213 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001215#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001216
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001217
Guido van Rossumad0ee831995-03-01 10:34:45 +00001218#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001219static char posix_getuid__doc__[] =
1220"getuid() -> uid\n\
1221Return the current process's user id.";
1222
Barry Warsaw53699e91996-12-10 23:23:01 +00001223static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001224posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001225 PyObject *self;
1226 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001227{
Barry Warsaw53699e91996-12-10 23:23:01 +00001228 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001229 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001230 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001231}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001232#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001233
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001234
Guido van Rossumad0ee831995-03-01 10:34:45 +00001235#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001236static char posix_kill__doc__[] =
1237"kill(pid, sig) -> None\n\
1238Kill a process with a signal.";
1239
Barry Warsaw53699e91996-12-10 23:23:01 +00001240static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001241posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001242 PyObject *self;
1243 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244{
1245 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001246 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001247 return NULL;
1248 if (kill(pid, sig) == -1)
1249 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 Py_INCREF(Py_None);
1251 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001252}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001253#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001254
Guido van Rossumc0125471996-06-28 18:55:32 +00001255#ifdef HAVE_PLOCK
1256
1257#ifdef HAVE_SYS_LOCK_H
1258#include <sys/lock.h>
1259#endif
1260
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001261static char posix_plock__doc__[] =
1262"plock(op) -> None\n\
1263Lock program segments into memory.";
1264
Barry Warsaw53699e91996-12-10 23:23:01 +00001265static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001266posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001267 PyObject *self;
1268 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001269{
1270 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001271 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001272 return NULL;
1273 if (plock(op) == -1)
1274 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001275 Py_INCREF(Py_None);
1276 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001277}
1278#endif
1279
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001280
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001281#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001282static char posix_popen__doc__[] =
1283"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1284Open a pipe to/from a command returning a file object.";
1285
Barry Warsaw53699e91996-12-10 23:23:01 +00001286static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001287posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001288 PyObject *self;
1289 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001290{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001291 char *name;
1292 char *mode = "r";
1293 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001294 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001295 PyObject *f;
1296 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001297 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001298 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001299 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001300 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001301 if (fp == NULL)
1302 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001303 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001304 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001306 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001307}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001308#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001310
Guido van Rossumb6775db1994-08-01 11:34:53 +00001311#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001312static char posix_setuid__doc__[] =
1313"setuid(uid) -> None\n\
1314Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001315static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001316posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 PyObject *self;
1318 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001319{
1320 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001321 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001322 return NULL;
1323 if (setuid(uid) < 0)
1324 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001325 Py_INCREF(Py_None);
1326 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001327}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001328#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001330
Guido van Rossumb6775db1994-08-01 11:34:53 +00001331#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001332static char posix_setgid__doc__[] =
1333"setgid(gid) -> None\n\
1334Set the current process's group id.";
1335
Barry Warsaw53699e91996-12-10 23:23:01 +00001336static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001337posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001338 PyObject *self;
1339 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001340{
1341 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001342 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001343 return NULL;
1344 if (setgid(gid) < 0)
1345 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001346 Py_INCREF(Py_None);
1347 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001348}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001349#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001350
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001351
Guido van Rossumb6775db1994-08-01 11:34:53 +00001352#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001353static char posix_waitpid__doc__[] =
1354"waitpid(pid, options) -> (pid, status)\n\
1355Wait for completion of a give child process.";
1356
Barry Warsaw53699e91996-12-10 23:23:01 +00001357static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001358posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001359 PyObject *self;
1360 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001361{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001362 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001363 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001364 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001365 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001366#ifdef NeXT
1367 pid = wait4(pid, (union wait *)&sts, options, NULL);
1368#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001369 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001370#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001371 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001372 if (pid == -1)
1373 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001374 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001375 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001376}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001377#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001379
Guido van Rossumad0ee831995-03-01 10:34:45 +00001380#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001381static char posix_wait__doc__[] =
1382"wait() -> (pid, status)\n\
1383Wait for completion of a child process.";
1384
Barry Warsaw53699e91996-12-10 23:23:01 +00001385static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001386posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 PyObject *self;
1388 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001389{
1390 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001391 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001392#ifdef NeXT
1393 pid = wait((union wait *)&sts);
1394#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001395 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001396#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001397 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001398 if (pid == -1)
1399 return posix_error();
1400 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001401 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001402}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001403#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001404
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001405
1406static char posix_lstat__doc__[] =
1407"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1408Like stat(path), but do not follow symbolic links.";
1409
Barry Warsaw53699e91996-12-10 23:23:01 +00001410static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001412 PyObject *self;
1413 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001414{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001415#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001416 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001417#else /* !HAVE_LSTAT */
1418 return posix_do_stat(self, args, stat);
1419#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001420}
1421
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001422
Guido van Rossumb6775db1994-08-01 11:34:53 +00001423#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001424static char posix_readlink__doc__[] =
1425"readlink(path) -> path\n\
1426Return a string representing the path to which the symbolic link points.";
1427
Barry Warsaw53699e91996-12-10 23:23:01 +00001428static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001430 PyObject *self;
1431 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001432{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001433 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001434 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001435 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001436 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001437 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001438 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001439 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001440 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001441 if (n < 0)
1442 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001443 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001444}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001445#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001446
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001447
Guido van Rossumb6775db1994-08-01 11:34:53 +00001448#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001449static char posix_symlink__doc__[] =
1450"symlink(src, dst) -> None\n\
1451Create a symbolic link.";
1452
Barry Warsaw53699e91996-12-10 23:23:01 +00001453static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001454posix_symlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001455 PyObject *self;
1456 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001458 return posix_2str(args, symlink);
1459}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001460#endif /* HAVE_SYMLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461
Guido van Rossumb6775db1994-08-01 11:34:53 +00001462#ifdef HAVE_TIMES
1463#ifndef HZ
1464#define HZ 60 /* Universal constant :-) */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001465#endif /* HZ */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001466
Barry Warsaw53699e91996-12-10 23:23:01 +00001467static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001468posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001469 PyObject *self;
1470 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001471{
1472 struct tms t;
1473 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001474 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001475 return NULL;
1476 errno = 0;
1477 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001478 if (c == (clock_t) -1)
1479 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001481 (double)t.tms_utime / HZ,
1482 (double)t.tms_stime / HZ,
1483 (double)t.tms_cutime / HZ,
1484 (double)t.tms_cstime / HZ,
1485 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001486}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001487#endif /* HAVE_TIMES */
Guido van Rossum87755a21996-09-07 00:59:43 +00001488#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001489#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001490static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001491posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001492 PyObject *self;
1493 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001494{
1495 FILETIME create, exit, kernel, user;
1496 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001497 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001498 return NULL;
1499 hProc = GetCurrentProcess();
1500 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001501 return Py_BuildValue(
1502 "ddddd",
1503 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1504 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1505 (double)0,
1506 (double)0,
1507 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001508}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001509#endif /* MS_WIN32 */
Roger E. Masse0318fd61997-06-05 22:07:58 +00001510static char posix_times__doc__[] =
1511"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1512Return a tuple of floating point numbers indicating process times.";
Guido van Rossum22db57e1992-04-05 14:25:30 +00001513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001514
Guido van Rossumb6775db1994-08-01 11:34:53 +00001515#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001516static char posix_setsid__doc__[] =
1517"setsid() -> None\n\
1518Call the system call setsid().";
1519
Barry Warsaw53699e91996-12-10 23:23:01 +00001520static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001521posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001522 PyObject *self;
1523 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001524{
Barry Warsaw53699e91996-12-10 23:23:01 +00001525 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001526 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001527 if (setsid() < 0)
1528 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001529 Py_INCREF(Py_None);
1530 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001531}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001532#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001533
Guido van Rossumb6775db1994-08-01 11:34:53 +00001534#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001535static char posix_setpgid__doc__[] =
1536"setpgid(pid, pgrp) -> None\n\
1537Call the system call setpgid().";
1538
Barry Warsaw53699e91996-12-10 23:23:01 +00001539static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001540posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001541 PyObject *self;
1542 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001543{
1544 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001545 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001546 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001547 if (setpgid(pid, pgrp) < 0)
1548 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001549 Py_INCREF(Py_None);
1550 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001551}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001552#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001553
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001554
Guido van Rossumb6775db1994-08-01 11:34:53 +00001555#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001556static char posix_tcgetpgrp__doc__[] =
1557"tcgetpgrp(fd) -> pgid\n\
1558Return the process group associated with the terminal given by a fd.";
1559
Barry Warsaw53699e91996-12-10 23:23:01 +00001560static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001561posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001562 PyObject *self;
1563 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001564{
1565 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001566 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001567 return NULL;
1568 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001569 if (pgid < 0)
1570 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001571 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001572}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001573#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001574
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001575
Guido van Rossumb6775db1994-08-01 11:34:53 +00001576#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001577static char posix_tcsetpgrp__doc__[] =
1578"tcsetpgrp(fd, pgid) -> None\n\
1579Set the process group associated with the terminal given by a fd.";
1580
Barry Warsaw53699e91996-12-10 23:23:01 +00001581static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001582posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001583 PyObject *self;
1584 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001585{
1586 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001587 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001588 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001589 if (tcsetpgrp(fd, pgid) < 0)
1590 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001591 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001592 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001593}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001594#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001595
Guido van Rossum687dd131993-05-17 08:34:16 +00001596/* Functions acting on file descriptors */
1597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001598static char posix_open__doc__[] =
1599"open(filename, flag [, mode=0777]) -> fd\n\
1600Open a file (for low level IO).";
1601
Barry Warsaw53699e91996-12-10 23:23:01 +00001602static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001603posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001604 PyObject *self;
1605 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001606{
1607 char *file;
1608 int flag;
1609 int mode = 0777;
1610 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001611 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1612 return NULL;
1613
Barry Warsaw53699e91996-12-10 23:23:01 +00001614 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001615 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001616 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001617 if (fd < 0)
1618 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001619 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001620}
1621
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001622
1623static char posix_close__doc__[] =
1624"close(fd) -> None\n\
1625Close a file descriptor (for low level IO).";
1626
Barry Warsaw53699e91996-12-10 23:23:01 +00001627static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001628posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001629 PyObject *self;
1630 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001631{
1632 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001633 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001634 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001635 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001636 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001637 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001638 if (res < 0)
1639 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 Py_INCREF(Py_None);
1641 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001642}
1643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001644
1645static char posix_dup__doc__[] =
1646"dup(fd) -> fd2\n\
1647Return a duplicate of a file descriptor.";
1648
Barry Warsaw53699e91996-12-10 23:23:01 +00001649static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001650posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001651 PyObject *self;
1652 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001653{
1654 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00001655 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001656 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001657 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001658 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001659 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001660 if (fd < 0)
1661 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001662 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001663}
1664
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001665
1666static char posix_dup2__doc__[] =
1667"dup2(fd, fd2) -> None\n\
1668Duplicate file descriptor.";
1669
Barry Warsaw53699e91996-12-10 23:23:01 +00001670static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001671posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001672 PyObject *self;
1673 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001674{
1675 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001676 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00001677 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001678 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001679 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00001680 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001681 if (res < 0)
1682 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001683 Py_INCREF(Py_None);
1684 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001685}
1686
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001687
1688static char posix_lseek__doc__[] =
1689"lseek(fd, pos, how) -> newpos\n\
1690Set the current position of a file descriptor.";
1691
Barry Warsaw53699e91996-12-10 23:23:01 +00001692static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001693posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001694 PyObject *self;
1695 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001696{
1697 int fd, how;
1698 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001699 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00001700 return NULL;
1701#ifdef SEEK_SET
1702 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1703 switch (how) {
1704 case 0: how = SEEK_SET; break;
1705 case 1: how = SEEK_CUR; break;
1706 case 2: how = SEEK_END; break;
1707 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001708#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00001709 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001710 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001712 if (res < 0)
1713 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001714 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00001715}
1716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001717
1718static char posix_read__doc__[] =
1719"read(fd, buffersize) -> string\n\
1720Read a file descriptor.";
1721
Barry Warsaw53699e91996-12-10 23:23:01 +00001722static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001723posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001724 PyObject *self;
1725 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001726{
Guido van Rossum8bac5461996-06-11 18:38:48 +00001727 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001728 PyObject *buffer;
1729 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001730 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001731 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001732 if (buffer == NULL)
1733 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 Py_BEGIN_ALLOW_THREADS
1735 n = read(fd, PyString_AsString(buffer), size);
1736 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00001737 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00001739 return posix_error();
1740 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00001741 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00001742 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00001743 return buffer;
1744}
1745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001746
1747static char posix_write__doc__[] =
1748"write(fd, string) -> byteswritten\n\
1749Write a string to a file descriptor.";
1750
Barry Warsaw53699e91996-12-10 23:23:01 +00001751static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001752posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001753 PyObject *self;
1754 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001755{
1756 int fd, size;
1757 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00001758 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001759 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001760 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001761 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00001762 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001763 if (size < 0)
1764 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001765 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001766}
1767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001768
1769static char posix_fstat__doc__[]=
1770"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1771Like stat(), but for an open file descriptor.";
1772
Barry Warsaw53699e91996-12-10 23:23:01 +00001773static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001774posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001775 PyObject *self;
1776 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001777{
1778 int fd;
1779 struct stat st;
1780 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001781 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001782 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001783 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001784 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001785 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001786 if (res != 0)
1787 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001788 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001789 (long)st.st_mode,
1790 (long)st.st_ino,
1791 (long)st.st_dev,
1792 (long)st.st_nlink,
1793 (long)st.st_uid,
1794 (long)st.st_gid,
1795 (long)st.st_size,
1796 (long)st.st_atime,
1797 (long)st.st_mtime,
1798 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00001799}
1800
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001801
1802static char posix_fdopen__doc__[] =
1803"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
1804Return an open file object connected to a file descriptor.";
1805
Barry Warsaw53699e91996-12-10 23:23:01 +00001806static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001807posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001808 PyObject *self;
1809 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001810{
Barry Warsaw53699e91996-12-10 23:23:01 +00001811 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00001812 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001813 char *mode = "r";
1814 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00001815 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 PyObject *f;
1817 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00001818 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001819
Barry Warsaw53699e91996-12-10 23:23:01 +00001820 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001821 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001822 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001823 if (fp == NULL)
1824 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001825 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001826 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001827 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001828 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00001829}
1830
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001831
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001832#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001833static char posix_pipe__doc__[] =
1834"pipe() -> (read_end, write_end)\n\
1835Create a pipe.";
1836
Barry Warsaw53699e91996-12-10 23:23:01 +00001837static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001838posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001839 PyObject *self;
1840 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001841{
Guido van Rossum8d665e61996-06-26 18:22:49 +00001842#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00001843 int fds[2];
1844 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001845 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00001846 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001847 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001848 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00001849 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001850 if (res != 0)
1851 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001852 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001853#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00001854 HANDLE read, write;
1855 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00001856 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00001857 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001858 Py_BEGIN_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001859 ok = CreatePipe( &read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00001860 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001861 if (!ok)
1862 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001863 return Py_BuildValue("(ii)", read, write);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001864#endif /* MS_WIN32 */
Guido van Rossum687dd131993-05-17 08:34:16 +00001865}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001866#endif /* HAVE_PIPE */
1867
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001868
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001869#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870static char posix_mkfifo__doc__[] =
1871"mkfifo(file, [, mode=0666]) -> None\n\
1872Create a FIFO (a POSIX named pipe).";
1873
Barry Warsaw53699e91996-12-10 23:23:01 +00001874static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001875posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001876 PyObject *self;
1877 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001878{
1879 char *file;
1880 int mode = 0666;
1881 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001882 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001883 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001884 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001885 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001886 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001887 if (res < 0)
1888 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001889 Py_INCREF(Py_None);
1890 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001891}
1892#endif
1893
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001894
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001895#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001896static char posix_ftruncate__doc__[] =
1897"ftruncate(fd, length) -> None\n\
1898Truncate a file to a specified length.";
1899
Barry Warsaw53699e91996-12-10 23:23:01 +00001900static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001901posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001902 PyObject *self; /* Not used */
1903 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001904{
1905 int fd;
1906 long length;
1907 int res;
1908
Barry Warsaw53699e91996-12-10 23:23:01 +00001909 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001910 return NULL;
1911
Barry Warsaw53699e91996-12-10 23:23:01 +00001912 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001913 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00001914 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001915 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001916 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001917 return NULL;
1918 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001919 Py_INCREF(Py_None);
1920 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001921}
1922#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001923
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001924#ifdef NeXT
1925#define HAVE_PUTENV
1926/* Steve Spicklemire got this putenv from NeXTAnswers */
1927static int
1928putenv(char *newval)
1929{
1930 extern char **environ;
1931
1932 static int firstTime = 1;
1933 char **ep;
1934 char *cp;
1935 int esiz;
1936 char *np;
1937
1938 if (!(np = strchr(newval, '=')))
1939 return 1;
1940 *np = '\0';
1941
1942 /* look it up */
1943 for (ep=environ ; *ep ; ep++)
1944 {
1945 /* this should always be true... */
1946 if (cp = strchr(*ep, '='))
1947 {
1948 *cp = '\0';
1949 if (!strcmp(*ep, newval))
1950 {
1951 /* got it! */
1952 *cp = '=';
1953 break;
1954 }
1955 *cp = '=';
1956 }
1957 else
1958 {
1959 *np = '=';
1960 return 1;
1961 }
1962 }
1963
1964 *np = '=';
1965 if (*ep)
1966 {
1967 /* the string was already there:
1968 just replace it with the new one */
1969 *ep = newval;
1970 return 0;
1971 }
1972
1973 /* expand environ by one */
1974 for (esiz=2, ep=environ ; *ep ; ep++)
1975 esiz++;
1976 if (firstTime)
1977 {
1978 char **epp;
1979 char **newenv;
1980 if (!(newenv = malloc(esiz * sizeof(char *))))
1981 return 1;
1982
1983 for (ep=environ, epp=newenv ; *ep ;)
1984 *epp++ = *ep++;
1985 *epp++ = newval;
1986 *epp = (char *) 0;
1987 environ = newenv;
1988 }
1989 else
1990 {
1991 if (!(environ = realloc(environ, esiz * sizeof(char *))))
1992 return 1;
1993 environ[esiz - 2] = newval;
1994 environ[esiz - 1] = (char *) 0;
1995 firstTime = 0;
1996 }
1997
1998 return 0;
1999}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002000#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002002
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002003#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002004static char posix_putenv__doc__[] =
2005"putenv(key, value) -> None\n\
2006Change or add an environment variable.";
2007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002009posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 PyObject *self;
2011 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002012{
2013 char *s1, *s2;
2014 char *new;
2015
Barry Warsaw53699e91996-12-10 23:23:01 +00002016 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002017 return NULL;
2018 /* XXX This leaks memory -- not easy to fix :-( */
2019 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002020 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002021 (void) sprintf(new, "%s=%s", s1, s2);
2022 if (putenv(new)) {
2023 posix_error();
2024 return NULL;
2025 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002026 Py_INCREF(Py_None);
2027 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002028}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002029#endif /* putenv */
2030
2031#ifdef HAVE_STRERROR
2032static char posix_strerror__doc__[] =
2033"strerror(code) -> string\n\
2034Translate an error code to a message string.";
2035
2036PyObject *
2037posix_strerror(self, args)
2038 PyObject *self;
2039 PyObject *args;
2040{
2041 int code;
2042 char *message;
2043 if (!PyArg_ParseTuple(args, "i", &code))
2044 return NULL;
2045 message = strerror(code);
2046 if (message == NULL) {
2047 PyErr_SetString(PyExc_ValueError,
2048 "strerror code out of range");
2049 return NULL;
2050 }
2051 return PyString_FromString(message);
2052}
2053#endif /* strerror */
2054
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002055
Barry Warsaw53699e91996-12-10 23:23:01 +00002056static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2058 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002059#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002061#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002062#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002063 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002064#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002065#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002067#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002068 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2069 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2070 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002071#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002074#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002076#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002077 {"rename", posix_rename, 0, posix_rename__doc__},
2078 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2079 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002080#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002082#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002083#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002084 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002085#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002089#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002090 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2091 {"remove", posix_unlink, 0, posix_remove__doc__},
2092 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002093#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002094 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002095#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002096 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002097#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098 {"execv", posix_execv, 0, posix_execv__doc__},
2099 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002100#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002101#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002102 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002103#endif /* HAVE_FORK */
2104#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002105 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002106#endif /* HAVE_GETEGID */
2107#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002108 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002109#endif /* HAVE_GETEUID */
2110#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002111 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002112#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002114#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002115 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002116#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002117#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002118 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002119#endif /* HAVE_GETPPID */
2120#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002121 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002122#endif /* HAVE_GETUID */
2123#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002124 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002125#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002126#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002127 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002128#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002129#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002130 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002131#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002132#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002133 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002134#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002135#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002136 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002137#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002138#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002140#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002141#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002142 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002143#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002144#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002145 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002146#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002147#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002148 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002149#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002150#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002151 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002152#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002153#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002154 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002155#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002156#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002157 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002158#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002159 {"open", posix_open, 1, posix_open__doc__},
2160 {"close", posix_close, 0, posix_close__doc__},
2161 {"dup", posix_dup, 0, posix_dup__doc__},
2162 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2163 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2164 {"read", posix_read, 0, posix_read__doc__},
2165 {"write", posix_write, 0, posix_write__doc__},
2166 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2167 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002168#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002169 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002170#endif
2171#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002172 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002173#endif
2174#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002175 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002176#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002177#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002178 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002179#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002180#ifdef HAVE_STRERROR
2181 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2182#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002183 {NULL, NULL} /* Sentinel */
2184};
2185
2186
Barry Warsaw4a342091996-12-19 23:50:02 +00002187static int
2188ins(d, symbol, value)
2189 PyObject* d;
2190 char* symbol;
2191 long value;
2192{
2193 PyObject* v = PyInt_FromLong(value);
2194 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2195 return -1; /* triggers fatal error */
2196
2197 Py_DECREF(v);
2198 return 0;
2199}
2200
2201static int
2202all_ins(d)
2203 PyObject* d;
2204{
2205#ifdef WNOHANG
2206 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2207#endif
2208#ifdef O_RDONLY
2209 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2210#endif
2211#ifdef O_WRONLY
2212 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2213#endif
2214#ifdef O_RDWR
2215 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2216#endif
2217#ifdef O_NDELAY
2218 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2219#endif
2220#ifdef O_NONBLOCK
2221 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2222#endif
2223#ifdef O_APPEND
2224 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2225#endif
2226#ifdef O_DSYNC
2227 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2228#endif
2229#ifdef O_RSYNC
2230 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2231#endif
2232#ifdef O_SYNC
2233 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2234#endif
2235#ifdef O_NOCTTY
2236 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2237#endif
2238#ifdef O_CREAT
2239 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2240#endif
2241#ifdef O_EXCL
2242 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2243#endif
2244#ifdef O_TRUNC
2245 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2246#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002247#ifdef O_BINARY
2248 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2249#endif
2250#ifdef O_TEXT
2251 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2252#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002253 return 0;
2254}
2255
2256
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002257/* XXX The following should be more unified -- only difference left is
2258 function name and module name. */
Barry Warsaw4a342091996-12-19 23:50:02 +00002259
Guido van Rossuma0e71301996-05-28 22:30:38 +00002260#if defined(_MSC_VER) || defined(__WATCOMC__)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002261void
2262initnt()
2263{
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002266 m = Py_InitModule4("nt",
2267 posix_methods,
2268 posix__doc__,
2269 (PyObject *)NULL,
2270 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272
2273 /* Initialize nt.environ dictionary */
2274 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00002276 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00002277 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002278
Barry Warsaw4a342091996-12-19 23:50:02 +00002279 if (all_ins(d))
2280 goto finally;
2281
Guido van Rossumb6775db1994-08-01 11:34:53 +00002282 /* Initialize nt.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00002283 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00002284 PyDict_SetItemString(d, "error", PosixError);
2285
2286 if (!PyErr_Occurred())
2287 return;
2288
2289 finally:
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002290 /* XXX Shouldn't */
Barry Warsaw4a342091996-12-19 23:50:02 +00002291 Py_FatalError("can't initialize NT posixmodule");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002292}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002293#else /* not a PC port */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002294void
2295initposix()
2296{
Barry Warsaw53699e91996-12-10 23:23:01 +00002297 PyObject *m, *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002298
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002299 m = Py_InitModule4("posix",
2300 posix_methods,
2301 posix__doc__,
2302 (PyObject *)NULL,
2303 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002304 d = PyModule_GetDict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002305
2306 /* Initialize posix.environ dictionary */
2307 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002308 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00002309 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00002310 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002311
Barry Warsaw4a342091996-12-19 23:50:02 +00002312 if (all_ins(d))
2313 goto finally;
2314
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002315 /* Initialize posix.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00002316 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00002317 PyDict_SetItemString(d, "error", PosixError);
2318
2319 if (!PyErr_Occurred())
2320 return;
2321
2322 finally:
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002323 /* XXX Shouldn't */
Barry Warsaw4a342091996-12-19 23:50:02 +00002324 Py_FatalError("can't initialize posix module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002325}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002326#endif /* !_MSC_VER */