blob: 89f34eb849040692e5e2b2f39999a0fe2d309084 [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 */
70#ifdef __WATCOMC__ /* Watcom compiler */
71#define HAVE_GETCWD 1
72#define HAVE_OPENDIR 1
73#define HAVE_SYSTEM 1
74#if defined(__OS2__)
75#define HAVE_EXECV 1
76#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000077#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000078#include <process.h>
79#else
80#ifdef __BORLANDC__ /* Borland compiler */
81#define HAVE_EXECV 1
82#define HAVE_GETCWD 1
83#define HAVE_GETEGID 1
84#define HAVE_GETEUID 1
85#define HAVE_GETGID 1
86#define HAVE_GETPPID 1
87#define HAVE_GETUID 1
88#define HAVE_KILL 1
89#define HAVE_OPENDIR 1
90#define HAVE_PIPE 1
91#define HAVE_POPEN 1
92#define HAVE_SYSTEM 1
93#define HAVE_WAIT 1
94#else
95#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000096#define HAVE_GETCWD 1
97#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098#define HAVE_EXECV 1
99#define HAVE_PIPE 1
100#define HAVE_POPEN 1
101#define HAVE_SYSTEM 1
102#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000103#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#else /* all other compilers */
105/* Unix functions that the configure script doesn't check for */
106#define HAVE_EXECV 1
107#define HAVE_FORK 1
108#define HAVE_GETCWD 1
109#define HAVE_GETEGID 1
110#define HAVE_GETEUID 1
111#define HAVE_GETGID 1
112#define HAVE_GETPPID 1
113#define HAVE_GETUID 1
114#define HAVE_KILL 1
115#define HAVE_OPENDIR 1
116#define HAVE_PIPE 1
117#define HAVE_POPEN 1
118#define HAVE_SYSTEM 1
119#define HAVE_WAIT 1
120#endif /* _MSC_VER */
121#endif /* __BORLANDC__ */
122#endif /* ! __WATCOMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000123
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000125
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000127#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000128#endif
129
130#ifdef NeXT
131/* NeXT's <unistd.h> and <utime.h> aren't worth much */
132#undef HAVE_UNISTD_H
133#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000134#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000135/* #undef HAVE_GETCWD */
136#endif
137
138#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000139/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
140extern int rename();
141extern int pclose();
142extern int lstat();
143extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144#else /* !HAVE_UNISTD_H */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000145#if defined(__WATCOMC__) || defined(_MSC_VER)
Barry Warsaw53699e91996-12-10 23:23:01 +0000146extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000147#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000148extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000150extern int chdir Py_PROTO((const char *));
151extern int rmdir Py_PROTO((const char *));
152extern int chmod Py_PROTO((const char *, mode_t));
153extern int chown Py_PROTO((const char *, uid_t, gid_t));
154extern char *getcwd Py_PROTO((char *, int));
155extern char *strerror Py_PROTO((int));
156extern int link Py_PROTO((const char *, const char *));
157extern int rename Py_PROTO((const char *, const char *));
158extern int stat Py_PROTO((const char *, struct stat *));
159extern int unlink Py_PROTO((const char *));
160extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000162extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000163#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000164#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000165extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000166#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000168
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000169#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171#ifdef HAVE_UTIME_H
172#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000173#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000175#ifdef HAVE_SYS_UTIME_H
176#include <sys/utime.h>
177#define HAVE_UTIME_H /* pretend we do for the rest of this file */
178#endif /* HAVE_SYS_UTIME_H */
179
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180#ifdef HAVE_SYS_TIMES_H
181#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000182#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183
184#ifdef HAVE_SYS_PARAM_H
185#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000186#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187
188#ifdef HAVE_SYS_UTSNAME_H
189#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191
192#ifndef MAXPATHLEN
193#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000194#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000196#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000198#define NAMLEN(dirent) strlen((dirent)->d_name)
199#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#ifdef __WATCOMC__
201#include <direct.h>
202#define NAMLEN(dirent) strlen((dirent)->d_name)
203#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000205#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000207#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000209#endif
210#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000212#endif
213#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000215#endif
216#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000218#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#include <direct.h>
220#include <io.h>
221#include <process.h>
222#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000223#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000224#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000225#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#else /* 16-bit Windows */
227#include <dos.h>
228#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000229#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231
232#ifdef OS2
233#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235
236/* Return a dictionary corresponding to the POSIX environment table */
237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#if !defined(_MSC_VER) && !defined(__WATCOMC__)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000240#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Barry Warsaw53699e91996-12-10 23:23:01 +0000242static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243convertenviron()
244{
Barry Warsaw53699e91996-12-10 23:23:01 +0000245 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000247 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 if (d == NULL)
249 return NULL;
250 if (environ == NULL)
251 return d;
252 /* XXX This part ignores errors */
253 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000254 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255 char *p = strchr(*e, '=');
256 if (p == NULL)
257 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000258 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 if (v == NULL)
260 continue;
261 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000262 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000264 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265 }
266 return d;
267}
268
269
Barry Warsaw53699e91996-12-10 23:23:01 +0000270static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271
272/* Set a POSIX-specific error from errno, and return NULL */
273
Barry Warsaw53699e91996-12-10 23:23:01 +0000274static PyObject * posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000275{
Barry Warsaw53699e91996-12-10 23:23:01 +0000276 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
279
280/* POSIX generic methods */
281
Barry Warsaw53699e91996-12-10 23:23:01 +0000282static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000284 PyObject *args;
285 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000287 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000288 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000289 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000291 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000292 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000293 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000294 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000296 Py_INCREF(Py_None);
297 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}
299
Barry Warsaw53699e91996-12-10 23:23:01 +0000300static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000302 PyObject *args;
303 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000305 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000306 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000307 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000309 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000310 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000311 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000312 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000314 Py_INCREF(Py_None);
315 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
Barry Warsaw53699e91996-12-10 23:23:01 +0000318static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000320 PyObject *args;
321 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000323 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000325 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000326 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000328 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000329 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000330 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000331 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000333 Py_INCREF(Py_None);
334 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
336
Barry Warsaw53699e91996-12-10 23:23:01 +0000337static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000338posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000339 PyObject *args;
340 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000341{
342 char *path;
343 int i,i2;
344 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000345 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000346 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000347 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000348 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000349 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000350 if (res < 0)
351 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000352 Py_INCREF(Py_None);
353 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000354}
355
Barry Warsaw53699e91996-12-10 23:23:01 +0000356static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000358 PyObject *self;
359 PyObject *args;
360 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
362 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000363 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000364 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000365 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000367 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000368 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000369 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000370 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000372 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000373 (long)st.st_mode,
374 (long)st.st_ino,
375 (long)st.st_dev,
376 (long)st.st_nlink,
377 (long)st.st_uid,
378 (long)st.st_gid,
379 (long)st.st_size,
380 (long)st.st_atime,
381 (long)st.st_mtime,
382 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383}
384
385
386/* POSIX methods */
387
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000388static char posix_chdir__doc__[] =
389"chdir(path) -> None\n\
390Change the current working directory to the specified path.";
391
Barry Warsaw53699e91996-12-10 23:23:01 +0000392static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000394 PyObject *self;
395 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397 return posix_1str(args, chdir);
398}
399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000400
401static char posix_chmod__doc__[] =
402"chmod(path, mode) -> None\n\
403Change the access permissions of a file.";
404
Barry Warsaw53699e91996-12-10 23:23:01 +0000405static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000407 PyObject *self;
408 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410 return posix_strint(args, chmod);
411}
412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000413
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000415static char posix_chown__doc__[] =
416"chown(path, uid, gid) -> None\n\
417Change the owner and group id of path to the numeric uid and gid.";
418
Barry Warsaw53699e91996-12-10 23:23:01 +0000419static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000420posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000421 PyObject *self;
422 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000423{
424 return posix_strintint(args, chown);
425}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000426#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000428
Guido van Rossum36bc6801995-06-14 22:54:23 +0000429#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000430static char posix_getcwd__doc__[] =
431"getcwd() -> path\n\
432Return a string representing the current working directory.";
433
Barry Warsaw53699e91996-12-10 23:23:01 +0000434static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000436 PyObject *self;
437 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438{
439 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000440 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000441 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000443 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000444 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000445 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000446 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000450#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000452
Guido van Rossumb6775db1994-08-01 11:34:53 +0000453#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000454static char posix_link__doc__[] =
455"link(src, dst) -> None\n\
456Create a hard link to a file.";
457
Barry Warsaw53699e91996-12-10 23:23:01 +0000458static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000460 PyObject *self;
461 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 return posix_2str(args, link);
464}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000465#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000467
468static char posix_listdir__doc__[] =
469"listdir(path) -> list_of_strings\n\
470Return a list containing the names of the entries in the directory.\n\
471\n\
472 path: path of directory to list\n\
473\n\
474The list is in arbitrary order. It does not include the special\n\
475entries '.' and '..' even if they are present in the directory.";
476
Barry Warsaw53699e91996-12-10 23:23:01 +0000477static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000478posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000479 PyObject *self;
480 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000481{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000482#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000483
Guido van Rossumb6775db1994-08-01 11:34:53 +0000484 char *name;
485 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000487 HANDLE hFindFile;
488 WIN32_FIND_DATA FileData;
489 char namebuf[MAX_PATH+5];
490
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000492 return NULL;
493 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000495 return NULL;
496 }
497 strcpy(namebuf, name);
498 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
499 namebuf[len++] = '/';
500 strcpy(namebuf + len, "*.*");
501
Barry Warsaw53699e91996-12-10 23:23:01 +0000502 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000503 return NULL;
504
505 hFindFile = FindFirstFile(namebuf, &FileData);
506 if (hFindFile == INVALID_HANDLE_VALUE) {
507 errno = GetLastError();
508 return posix_error();
509 }
510 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000511 if (FileData.cFileName[0] == '.' &&
512 (FileData.cFileName[1] == '\0' ||
513 FileData.cFileName[1] == '.' &&
514 FileData.cFileName[2] == '\0'))
515 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000516 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000517 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000518 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000519 d = NULL;
520 break;
521 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000522 if (PyList_Append(d, v) != 0) {
523 Py_DECREF(v);
524 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000525 d = NULL;
526 break;
527 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000528 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000529 } while (FindNextFile(hFindFile, &FileData) == TRUE);
530
531 if (FindClose(hFindFile) == FALSE) {
532 errno = GetLastError();
533 return posix_error();
534 }
535
536 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000537
Guido van Rossum8d665e61996-06-26 18:22:49 +0000538#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000539#ifdef _MSC_VER /* 16-bit Windows */
540
541#ifndef MAX_PATH
542#define MAX_PATH 250
543#endif
544 char *name, *pt;
545 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000546 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000547 char namebuf[MAX_PATH+5];
548 struct _find_t ep;
549
Barry Warsaw53699e91996-12-10 23:23:01 +0000550 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000551 return NULL;
552 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000553 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000554 return NULL;
555 }
556 strcpy(namebuf, name);
557 for (pt = namebuf; *pt; pt++)
558 if (*pt == '/')
559 *pt = '\\';
560 if (namebuf[len-1] != '\\')
561 namebuf[len++] = '\\';
562 strcpy(namebuf + len, "*.*");
563
Barry Warsaw53699e91996-12-10 23:23:01 +0000564 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000565 return NULL;
566
567 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000568 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
569 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000570 errno = ENOENT;
571 return posix_error();
572 }
573 do {
574 if (ep.name[0] == '.' &&
575 (ep.name[1] == '\0' ||
576 ep.name[1] == '.' &&
577 ep.name[2] == '\0'))
578 continue;
579 strcpy(namebuf, ep.name);
580 for (pt = namebuf; *pt; pt++)
581 if (isupper(*pt))
582 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000583 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000584 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000585 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000586 d = NULL;
587 break;
588 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000589 if (PyList_Append(d, v) != 0) {
590 Py_DECREF(v);
591 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000592 d = NULL;
593 break;
594 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000595 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000596 } while (_dos_findnext(&ep) == 0);
597
598 return d;
599
600#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000601
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000602 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000603 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000605 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000606 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000608 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000609 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000615 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616 return NULL;
617 }
618 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000619 if (ep->d_name[0] == '.' &&
620 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000621 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000622 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000623 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000625 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626 d = NULL;
627 break;
628 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000629 if (PyList_Append(d, v) != 0) {
630 Py_DECREF(v);
631 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632 d = NULL;
633 break;
634 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000635 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636 }
637 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000638 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000639
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000641
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000642#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000643#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000646static char posix_mkdir__doc__[] =
647"mkdir(path [, mode=0777]) -> None\n\
648Create a directory.";
649
Barry Warsaw53699e91996-12-10 23:23:01 +0000650static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000652 PyObject *self;
653 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000655 int res;
656 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000657 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000658 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000659 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000660 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8d665e61996-06-26 18:22:49 +0000661#if defined(__WATCOMC__) || defined(_MSC_VER)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000662 res = mkdir(path);
663#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000664 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000665#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000666 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000667 if (res < 0)
668 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_INCREF(Py_None);
670 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000673
Guido van Rossumb6775db1994-08-01 11:34:53 +0000674#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000675static char posix_nice__doc__[] =
676"nice(inc) -> new_priority\n\
677Decrease the priority of process and return new priority.";
678
Barry Warsaw53699e91996-12-10 23:23:01 +0000679static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000680posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000681 PyObject *self;
682 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000683{
684 int increment, value;
685
Barry Warsaw53699e91996-12-10 23:23:01 +0000686 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000687 return NULL;
688 value = nice(increment);
689 if (value == -1)
690 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000691 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000692}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000693#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000695
696static char posix_rename__doc__[] =
697"rename(old, new) -> None\n\
698Rename a file or directory.";
699
Barry Warsaw53699e91996-12-10 23:23:01 +0000700static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000702 PyObject *self;
703 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705 return posix_2str(args, rename);
706}
707
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000708
709static char posix_rmdir__doc__[] =
710"rmdir(path) -> None\n\
711Remove a directory.";
712
Barry Warsaw53699e91996-12-10 23:23:01 +0000713static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000715 PyObject *self;
716 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718 return posix_1str(args, rmdir);
719}
720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000721
722static char posix_stat__doc__[] =
723"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
724Perform a stat system call on the given path.";
725
Barry Warsaw53699e91996-12-10 23:23:01 +0000726static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000728 PyObject *self;
729 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731 return posix_do_stat(self, args, stat);
732}
733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000734
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000735#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000736static char posix_system__doc__[] =
737"system(command) -> exit_status\n\
738Execute the command (a string) in a subshell.";
739
Barry Warsaw53699e91996-12-10 23:23:01 +0000740static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000741posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000742 PyObject *self;
743 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000745 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000746 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000747 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000749 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000750 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000751 Py_END_ALLOW_THREADS
752 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000754#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000756
757static char posix_umask__doc__[] =
758"umask(new_mask) -> old_mask\n\
759Set the current numeric umask and return the previous umask.";
760
Barry Warsaw53699e91996-12-10 23:23:01 +0000761static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000763 PyObject *self;
764 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765{
766 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000767 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768 return NULL;
769 i = umask(i);
770 if (i < 0)
771 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000772 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773}
774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000775
776static char posix_unlink__doc__[] =
777"unlink(path) -> None\n\
778Remove a file (same as remove(path)).";
779
780static char posix_remove__doc__[] =
781"remove(path) -> None\n\
782Remove a file (same as unlink(path)).";
783
Barry Warsaw53699e91996-12-10 23:23:01 +0000784static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000786 PyObject *self;
787 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789 return posix_1str(args, unlink);
790}
791
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000792
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000794static char posix_uname__doc__[] =
795"uname() -> (sysname, nodename, release, version, machine)\n\
796Return a tuple identifying the current operating system.";
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000799posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000800 PyObject *self;
801 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000802{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000803 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000804 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000805 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000806 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000807 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000808 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +0000809 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000810 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000811 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000812 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +0000813 u.sysname,
814 u.nodename,
815 u.release,
816 u.version,
817 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000818}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000819#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000820
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000821
822static char posix_utime__doc__[] =
823"utime(path, (atime, utime)) -> None\n\
824Set the access and modified time of the file to the given values.";
825
Barry Warsaw53699e91996-12-10 23:23:01 +0000826static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000827posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000828 PyObject *self;
829 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000831 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000832 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000833 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000834
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000836 struct utimbuf buf;
837#define ATIME buf.actime
838#define MTIME buf.modtime
839#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000840#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000841 time_t buf[2];
842#define ATIME buf[0]
843#define MTIME buf[1]
844#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000845#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000846
Barry Warsaw53699e91996-12-10 23:23:01 +0000847 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000848 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +0000849 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +0000850 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +0000851 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000852 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +0000853 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000854 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000855 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000856 Py_INCREF(Py_None);
857 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000858#undef UTIME_ARG
859#undef ATIME
860#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861}
862
Guido van Rossum85e3b011991-06-03 12:42:10 +0000863
Guido van Rossum3b066191991-06-04 19:40:25 +0000864/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000865
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000866static char posix__exit__doc__[] =
867"_exit(status)\n\
868Exit to the system with specified status, without normal exit processing.";
869
Barry Warsaw53699e91996-12-10 23:23:01 +0000870static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000871posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000872 PyObject *self;
873 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000874{
875 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000876 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000877 return NULL;
878 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +0000879 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000880}
881
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000882
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000883#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000884static char posix_execv__doc__[] =
885"execv(path, args)\n\
886Execute an executable path with arguments, replacing current process.\n\
887\n\
888 path: path of executable file\n\
889 args: tuple or list of strings";
890
Barry Warsaw53699e91996-12-10 23:23:01 +0000891static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +0000892posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000893 PyObject *self;
894 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000895{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000896 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000897 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000898 char **argvlist;
899 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000900 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +0000901
Guido van Rossum89b33251993-10-22 14:26:06 +0000902 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +0000903 argv is a list or tuple of strings. */
904
Barry Warsaw53699e91996-12-10 23:23:01 +0000905 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000906 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000907 if (PyList_Check(argv)) {
908 argc = PyList_Size(argv);
909 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000910 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000911 else if (PyTuple_Check(argv)) {
912 argc = PyTuple_Size(argv);
913 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000914 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000915 else {
916 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +0000917 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000918 return NULL;
919 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000920
Barry Warsaw53699e91996-12-10 23:23:01 +0000921 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000922 if (argvlist == NULL)
923 return NULL;
924 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000925 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
926 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000927 goto badarg;
928 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000929 }
930 argvlist[argc] = NULL;
931
Guido van Rossumb6775db1994-08-01 11:34:53 +0000932#ifdef BAD_EXEC_PROTOTYPES
933 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000934#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000935 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000936#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000937
Guido van Rossum85e3b011991-06-03 12:42:10 +0000938 /* If we get here it's definitely an error */
939
Barry Warsaw53699e91996-12-10 23:23:01 +0000940 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000941 return posix_error();
942}
943
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000944
945static char posix_execve__doc__[] =
946"execve(path, args, env)\n\
947Execute a path with arguments and environment, replacing current process.\n\
948\n\
949 path: path of executable file\n\
950 args: tuple or list of arguments\n\
951 env: dictonary of strings mapping to strings";
952
Barry Warsaw53699e91996-12-10 23:23:01 +0000953static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000954posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000955 PyObject *self;
956 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000957{
958 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +0000959 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000960 char **argvlist;
961 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000962 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000963 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000965
966 /* execve has three arguments: (path, argv, env), where
967 argv is a list or tuple of strings and env is a dictionary
968 like posix.environ. */
969
Barry Warsaw53699e91996-12-10 23:23:01 +0000970 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000971 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000972 if (PyList_Check(argv)) {
973 argc = PyList_Size(argv);
974 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000975 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 else if (PyTuple_Check(argv)) {
977 argc = PyTuple_Size(argv);
978 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000979 }
980 else {
Barry Warsaw53699e91996-12-10 23:23:01 +0000981 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000982 return NULL;
983 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +0000984 if (!PyMapping_Check(env)) {
985 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000986 return NULL;
987 }
988
Barry Warsaw53699e91996-12-10 23:23:01 +0000989 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000990 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000991 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000992 return NULL;
993 }
994 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000995 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +0000996 "s;argv must be list of strings",
997 &argvlist[i]))
998 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +0000999 goto fail_1;
1000 }
1001 }
1002 argvlist[argc] = NULL;
1003
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001004 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001005 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001006 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001007 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001008 goto fail_1;
1009 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001010 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001011 keys = PyMapping_Keys(env);
1012 vals = PyMapping_Values(env);
1013 if (!keys || !vals)
1014 goto fail_2;
1015
1016 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001017 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001018
1019 key = PyList_GetItem(keys, pos);
1020 val = PyList_GetItem(vals, pos);
1021 if (!key || !val)
1022 goto fail_2;
1023
Barry Warsaw53699e91996-12-10 23:23:01 +00001024 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001025 !PyArg_Parse(val, "s;non-string value in env", &v))
1026 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001027 goto fail_2;
1028 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001029 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001030 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001031 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001032 goto fail_2;
1033 }
1034 sprintf(p, "%s=%s", k, v);
1035 envlist[envc++] = p;
1036 }
1037 envlist[envc] = 0;
1038
Guido van Rossumb6775db1994-08-01 11:34:53 +00001039
1040#ifdef BAD_EXEC_PROTOTYPES
1041 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001042#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001043 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001044#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001045
1046 /* If we get here it's definitely an error */
1047
1048 (void) posix_error();
1049
1050 fail_2:
1051 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001052 PyMem_DEL(envlist[envc]);
1053 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001054 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001055 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001056 Py_XDECREF(vals);
1057 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001058 return NULL;
1059}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001060#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001061
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001062
Guido van Rossumad0ee831995-03-01 10:34:45 +00001063#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001064static char posix_fork__doc__[] =
1065"fork() -> pid\n\
1066Fork a child process.\n\
1067\n\
1068Return 0 to child process and PID of child to parent process.";
1069
Barry Warsaw53699e91996-12-10 23:23:01 +00001070static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001071posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001072 PyObject *self;
1073 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001074{
1075 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001076 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001077 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001078 pid = fork();
1079 if (pid == -1)
1080 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001081 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001082}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001083#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001085
Guido van Rossumad0ee831995-03-01 10:34:45 +00001086#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001087static char posix_getegid__doc__[] =
1088"getegid() -> egid\n\
1089Return the current process's effective group id.";
1090
Barry Warsaw53699e91996-12-10 23:23:01 +00001091static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001092posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 PyObject *self;
1094 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001095{
Barry Warsaw53699e91996-12-10 23:23:01 +00001096 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001097 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001098 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001099}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001100#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001102
Guido van Rossumad0ee831995-03-01 10:34:45 +00001103#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001104static char posix_geteuid__doc__[] =
1105"geteuid() -> euid\n\
1106Return the current process's effective user id.";
1107
Barry Warsaw53699e91996-12-10 23:23:01 +00001108static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001109posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001110 PyObject *self;
1111 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001112{
Barry Warsaw53699e91996-12-10 23:23:01 +00001113 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001114 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001115 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001116}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001117#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001118
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001119
Guido van Rossumad0ee831995-03-01 10:34:45 +00001120#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001121static char posix_getgid__doc__[] =
1122"getgid() -> gid\n\
1123Return the current process's group id.";
1124
Barry Warsaw53699e91996-12-10 23:23:01 +00001125static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001126posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001127 PyObject *self;
1128 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001129{
Barry Warsaw53699e91996-12-10 23:23:01 +00001130 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001131 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001132 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001133}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001134#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001135
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001136
1137static char posix_getpid__doc__[] =
1138"getpid() -> pid\n\
1139Return the current process id";
1140
Barry Warsaw53699e91996-12-10 23:23:01 +00001141static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001142posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001143 PyObject *self;
1144 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001145{
Barry Warsaw53699e91996-12-10 23:23:01 +00001146 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001147 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001148 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001149}
1150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001151
Guido van Rossumb6775db1994-08-01 11:34:53 +00001152#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001153static char posix_getpgrp__doc__[] =
1154"getpgrp() -> pgrp\n\
1155Return the current process group id.";
1156
Barry Warsaw53699e91996-12-10 23:23:01 +00001157static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001158posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001159 PyObject *self;
1160 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001161{
Barry Warsaw53699e91996-12-10 23:23:01 +00001162 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001163 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001164#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001165 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001166#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001167 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001168#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001169}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001170#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001172
Guido van Rossumb6775db1994-08-01 11:34:53 +00001173#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001174static char posix_setpgrp__doc__[] =
1175"setpgrp() -> None\n\
1176Make this process a session leader.";
1177
Barry Warsaw53699e91996-12-10 23:23:01 +00001178static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001179posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 PyObject *self;
1181 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001182{
Barry Warsaw53699e91996-12-10 23:23:01 +00001183 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001184 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001185#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001186 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001187#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001188 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001189#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001190 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001191 Py_INCREF(Py_None);
1192 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001193}
1194
Guido van Rossumb6775db1994-08-01 11:34:53 +00001195#endif /* HAVE_SETPGRP */
1196
Guido van Rossumad0ee831995-03-01 10:34:45 +00001197#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001198static char posix_getppid__doc__[] =
1199"getppid() -> ppid\n\
1200Return the parent's process id.";
1201
Barry Warsaw53699e91996-12-10 23:23:01 +00001202static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001203posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001204 PyObject *self;
1205 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206{
Barry Warsaw53699e91996-12-10 23:23:01 +00001207 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001208 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001209 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001210}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001211#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001212
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001213
Guido van Rossumad0ee831995-03-01 10:34:45 +00001214#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001215static char posix_getuid__doc__[] =
1216"getuid() -> uid\n\
1217Return the current process's user id.";
1218
Barry Warsaw53699e91996-12-10 23:23:01 +00001219static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001220posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001221 PyObject *self;
1222 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001223{
Barry Warsaw53699e91996-12-10 23:23:01 +00001224 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001225 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001226 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001227}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001228#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001229
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001230
Guido van Rossumad0ee831995-03-01 10:34:45 +00001231#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001232static char posix_kill__doc__[] =
1233"kill(pid, sig) -> None\n\
1234Kill a process with a signal.";
1235
Barry Warsaw53699e91996-12-10 23:23:01 +00001236static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001237posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001238 PyObject *self;
1239 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001240{
1241 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001242 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001243 return NULL;
1244 if (kill(pid, sig) == -1)
1245 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001246 Py_INCREF(Py_None);
1247 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001248}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001249#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001250
Guido van Rossumc0125471996-06-28 18:55:32 +00001251#ifdef HAVE_PLOCK
1252
1253#ifdef HAVE_SYS_LOCK_H
1254#include <sys/lock.h>
1255#endif
1256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001257static char posix_plock__doc__[] =
1258"plock(op) -> None\n\
1259Lock program segments into memory.";
1260
Barry Warsaw53699e91996-12-10 23:23:01 +00001261static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001262posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001263 PyObject *self;
1264 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001265{
1266 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001267 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001268 return NULL;
1269 if (plock(op) == -1)
1270 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001271 Py_INCREF(Py_None);
1272 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001273}
1274#endif
1275
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001276
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001277#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001278static char posix_popen__doc__[] =
1279"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1280Open a pipe to/from a command returning a file object.";
1281
Barry Warsaw53699e91996-12-10 23:23:01 +00001282static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001283posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001284 PyObject *self;
1285 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001286{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001287 char *name;
1288 char *mode = "r";
1289 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001290 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001291 PyObject *f;
1292 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001293 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001294 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001295 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001297 if (fp == NULL)
1298 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001299 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001300 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001301 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001302 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001303}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001304#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001305
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001306
Guido van Rossumb6775db1994-08-01 11:34:53 +00001307#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001308static char posix_setuid__doc__[] =
1309"setuid(uid) -> None\n\
1310Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001311static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001312posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 PyObject *self;
1314 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001315{
1316 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001318 return NULL;
1319 if (setuid(uid) < 0)
1320 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001321 Py_INCREF(Py_None);
1322 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001323}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001324#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001325
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001326
Guido van Rossumb6775db1994-08-01 11:34:53 +00001327#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001328static char posix_setgid__doc__[] =
1329"setgid(gid) -> None\n\
1330Set the current process's group id.";
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001333posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyObject *self;
1335 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001336{
1337 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001338 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001339 return NULL;
1340 if (setgid(gid) < 0)
1341 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001342 Py_INCREF(Py_None);
1343 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001344}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001345#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001346
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001347
Guido van Rossumb6775db1994-08-01 11:34:53 +00001348#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001349static char posix_waitpid__doc__[] =
1350"waitpid(pid, options) -> (pid, status)\n\
1351Wait for completion of a give child process.";
1352
Barry Warsaw53699e91996-12-10 23:23:01 +00001353static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001354posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001355 PyObject *self;
1356 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001357{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001358 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001359 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001360 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001361 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001362#ifdef NeXT
1363 pid = wait4(pid, (union wait *)&sts, options, NULL);
1364#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001365 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001366#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001368 if (pid == -1)
1369 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001370 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001371 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001372}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001373#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001374
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001375
Guido van Rossumad0ee831995-03-01 10:34:45 +00001376#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001377static char posix_wait__doc__[] =
1378"wait() -> (pid, status)\n\
1379Wait for completion of a child process.";
1380
Barry Warsaw53699e91996-12-10 23:23:01 +00001381static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001382posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001383 PyObject *self;
1384 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001385{
1386 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001388#ifdef NeXT
1389 pid = wait((union wait *)&sts);
1390#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001391 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001392#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001393 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001394 if (pid == -1)
1395 return posix_error();
1396 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001397 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001398}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001399#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001400
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001401
1402static char posix_lstat__doc__[] =
1403"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1404Like stat(path), but do not follow symbolic links.";
1405
Barry Warsaw53699e91996-12-10 23:23:01 +00001406static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001407posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001408 PyObject *self;
1409 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001410{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001411#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001413#else /* !HAVE_LSTAT */
1414 return posix_do_stat(self, args, stat);
1415#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001416}
1417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001418
Guido van Rossumb6775db1994-08-01 11:34:53 +00001419#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001420static char posix_readlink__doc__[] =
1421"readlink(path) -> path\n\
1422Return a string representing the path to which the symbolic link points.";
1423
Barry Warsaw53699e91996-12-10 23:23:01 +00001424static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001425posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001426 PyObject *self;
1427 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001428{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001429 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001430 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001432 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001434 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001435 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001436 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001437 if (n < 0)
1438 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001439 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001440}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001441#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001443
Guido van Rossumb6775db1994-08-01 11:34:53 +00001444#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001445static char posix_symlink__doc__[] =
1446"symlink(src, dst) -> None\n\
1447Create a symbolic link.";
1448
Barry Warsaw53699e91996-12-10 23:23:01 +00001449static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450posix_symlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001451 PyObject *self;
1452 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001453{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001454 return posix_2str(args, symlink);
1455}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001456#endif /* HAVE_SYMLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457
Guido van Rossumb6775db1994-08-01 11:34:53 +00001458#ifdef HAVE_TIMES
1459#ifndef HZ
1460#define HZ 60 /* Universal constant :-) */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001461#endif /* HZ */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001462
Barry Warsaw53699e91996-12-10 23:23:01 +00001463static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001464posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001465 PyObject *self;
1466 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001467{
1468 struct tms t;
1469 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001470 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001471 return NULL;
1472 errno = 0;
1473 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001474 if (c == (clock_t) -1)
1475 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001476 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001477 (double)t.tms_utime / HZ,
1478 (double)t.tms_stime / HZ,
1479 (double)t.tms_cutime / HZ,
1480 (double)t.tms_cstime / HZ,
1481 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001482}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001483#endif /* HAVE_TIMES */
Guido van Rossum87755a21996-09-07 00:59:43 +00001484#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001485#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001486static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001487posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001488 PyObject *self;
1489 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001490{
1491 FILETIME create, exit, kernel, user;
1492 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001493 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001494 return NULL;
1495 hProc = GetCurrentProcess();
1496 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001497 return Py_BuildValue(
1498 "ddddd",
1499 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1500 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1501 (double)0,
1502 (double)0,
1503 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001504}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001505#endif /* MS_WIN32 */
Roger E. Masse0318fd61997-06-05 22:07:58 +00001506static char posix_times__doc__[] =
1507"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1508Return a tuple of floating point numbers indicating process times.";
Guido van Rossum22db57e1992-04-05 14:25:30 +00001509
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001510
Guido van Rossumb6775db1994-08-01 11:34:53 +00001511#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001512static char posix_setsid__doc__[] =
1513"setsid() -> None\n\
1514Call the system call setsid().";
1515
Barry Warsaw53699e91996-12-10 23:23:01 +00001516static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001517posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001518 PyObject *self;
1519 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001520{
Barry Warsaw53699e91996-12-10 23:23:01 +00001521 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001522 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001523 if (setsid() < 0)
1524 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001525 Py_INCREF(Py_None);
1526 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001527}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001528#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001529
Guido van Rossumb6775db1994-08-01 11:34:53 +00001530#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001531static char posix_setpgid__doc__[] =
1532"setpgid(pid, pgrp) -> None\n\
1533Call the system call setpgid().";
1534
Barry Warsaw53699e91996-12-10 23:23:01 +00001535static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001536posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001537 PyObject *self;
1538 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001539{
1540 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001541 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001542 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001543 if (setpgid(pid, pgrp) < 0)
1544 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001545 Py_INCREF(Py_None);
1546 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001547}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001548#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001549
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001550
Guido van Rossumb6775db1994-08-01 11:34:53 +00001551#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001552static char posix_tcgetpgrp__doc__[] =
1553"tcgetpgrp(fd) -> pgid\n\
1554Return the process group associated with the terminal given by a fd.";
1555
Barry Warsaw53699e91996-12-10 23:23:01 +00001556static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001557posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001558 PyObject *self;
1559 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001560{
1561 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001562 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001563 return NULL;
1564 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001565 if (pgid < 0)
1566 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001567 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001568}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001569#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001570
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001571
Guido van Rossumb6775db1994-08-01 11:34:53 +00001572#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001573static char posix_tcsetpgrp__doc__[] =
1574"tcsetpgrp(fd, pgid) -> None\n\
1575Set the process group associated with the terminal given by a fd.";
1576
Barry Warsaw53699e91996-12-10 23:23:01 +00001577static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001578posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001579 PyObject *self;
1580 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001581{
1582 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001583 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001584 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001585 if (tcsetpgrp(fd, pgid) < 0)
1586 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001587 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001588 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001589}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001590#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001591
Guido van Rossum687dd131993-05-17 08:34:16 +00001592/* Functions acting on file descriptors */
1593
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001594static char posix_open__doc__[] =
1595"open(filename, flag [, mode=0777]) -> fd\n\
1596Open a file (for low level IO).";
1597
Barry Warsaw53699e91996-12-10 23:23:01 +00001598static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001599posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 PyObject *self;
1601 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001602{
1603 char *file;
1604 int flag;
1605 int mode = 0777;
1606 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001607 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1608 return NULL;
1609
Barry Warsaw53699e91996-12-10 23:23:01 +00001610 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001611 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001613 if (fd < 0)
1614 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001615 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001616}
1617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618
1619static char posix_close__doc__[] =
1620"close(fd) -> None\n\
1621Close a file descriptor (for low level IO).";
1622
Barry Warsaw53699e91996-12-10 23:23:01 +00001623static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001624posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001625 PyObject *self;
1626 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001627{
1628 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001629 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001630 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001631 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001632 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001633 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001634 if (res < 0)
1635 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001636 Py_INCREF(Py_None);
1637 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001638}
1639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001640
1641static char posix_dup__doc__[] =
1642"dup(fd) -> fd2\n\
1643Return a duplicate of a file descriptor.";
1644
Barry Warsaw53699e91996-12-10 23:23:01 +00001645static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001646posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001647 PyObject *self;
1648 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001649{
1650 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00001651 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001652 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001653 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001654 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00001655 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001656 if (fd < 0)
1657 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001658 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001659}
1660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001661
1662static char posix_dup2__doc__[] =
1663"dup2(fd, fd2) -> None\n\
1664Duplicate file descriptor.";
1665
Barry Warsaw53699e91996-12-10 23:23:01 +00001666static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001667posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001670{
1671 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001672 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00001673 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001674 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001675 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00001676 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001677 if (res < 0)
1678 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001679 Py_INCREF(Py_None);
1680 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00001681}
1682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
1684static char posix_lseek__doc__[] =
1685"lseek(fd, pos, how) -> newpos\n\
1686Set the current position of a file descriptor.";
1687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001689posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 PyObject *self;
1691 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001692{
1693 int fd, how;
1694 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001695 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00001696 return NULL;
1697#ifdef SEEK_SET
1698 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
1699 switch (how) {
1700 case 0: how = SEEK_SET; break;
1701 case 1: how = SEEK_CUR; break;
1702 case 2: how = SEEK_END; break;
1703 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001704#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001706 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00001707 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001708 if (res < 0)
1709 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001710 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00001711}
1712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001713
1714static char posix_read__doc__[] =
1715"read(fd, buffersize) -> string\n\
1716Read a file descriptor.";
1717
Barry Warsaw53699e91996-12-10 23:23:01 +00001718static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001719posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001720 PyObject *self;
1721 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001722{
Guido van Rossum8bac5461996-06-11 18:38:48 +00001723 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001724 PyObject *buffer;
1725 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001726 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001727 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001728 if (buffer == NULL)
1729 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001730 Py_BEGIN_ALLOW_THREADS
1731 n = read(fd, PyString_AsString(buffer), size);
1732 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00001733 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00001735 return posix_error();
1736 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00001737 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00001739 return buffer;
1740}
1741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001742
1743static char posix_write__doc__[] =
1744"write(fd, string) -> byteswritten\n\
1745Write a string to a file descriptor.";
1746
Barry Warsaw53699e91996-12-10 23:23:01 +00001747static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001748posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001749 PyObject *self;
1750 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001751{
1752 int fd, size;
1753 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00001755 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001756 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001757 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00001758 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001759 if (size < 0)
1760 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001761 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00001762}
1763
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001764
1765static char posix_fstat__doc__[]=
1766"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1767Like stat(), but for an open file descriptor.";
1768
Barry Warsaw53699e91996-12-10 23:23:01 +00001769static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001770posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001771 PyObject *self;
1772 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001773{
1774 int fd;
1775 struct stat st;
1776 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001777 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001778 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001779 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001780 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001781 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001782 if (res != 0)
1783 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001784 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001785 (long)st.st_mode,
1786 (long)st.st_ino,
1787 (long)st.st_dev,
1788 (long)st.st_nlink,
1789 (long)st.st_uid,
1790 (long)st.st_gid,
1791 (long)st.st_size,
1792 (long)st.st_atime,
1793 (long)st.st_mtime,
1794 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00001795}
1796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001797
1798static char posix_fdopen__doc__[] =
1799"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
1800Return an open file object connected to a file descriptor.";
1801
Barry Warsaw53699e91996-12-10 23:23:01 +00001802static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001803posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001804 PyObject *self;
1805 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001806{
Barry Warsaw53699e91996-12-10 23:23:01 +00001807 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00001808 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001809 char *mode = "r";
1810 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00001811 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 PyObject *f;
1813 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00001814 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001815
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001817 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001818 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001819 if (fp == NULL)
1820 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001821 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001822 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001823 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001824 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00001825}
1826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001827
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001828#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001829static char posix_pipe__doc__[] =
1830"pipe() -> (read_end, write_end)\n\
1831Create a pipe.";
1832
Barry Warsaw53699e91996-12-10 23:23:01 +00001833static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001834posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001835 PyObject *self;
1836 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001837{
Guido van Rossum8d665e61996-06-26 18:22:49 +00001838#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00001839 int fds[2];
1840 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001841 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00001842 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001843 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001844 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00001845 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001846 if (res != 0)
1847 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001848 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001849#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00001850 HANDLE read, write;
1851 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00001852 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00001853 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001854 Py_BEGIN_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001855 ok = CreatePipe( &read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00001856 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00001857 if (!ok)
1858 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001859 return Py_BuildValue("(ii)", read, write);
Guido van Rossum8d665e61996-06-26 18:22:49 +00001860#endif /* MS_WIN32 */
Guido van Rossum687dd131993-05-17 08:34:16 +00001861}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001862#endif /* HAVE_PIPE */
1863
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001864
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001865#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001866static char posix_mkfifo__doc__[] =
1867"mkfifo(file, [, mode=0666]) -> None\n\
1868Create a FIFO (a POSIX named pipe).";
1869
Barry Warsaw53699e91996-12-10 23:23:01 +00001870static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001871posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001872 PyObject *self;
1873 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001874{
1875 char *file;
1876 int mode = 0666;
1877 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001878 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001879 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001880 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001881 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001882 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001883 if (res < 0)
1884 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001885 Py_INCREF(Py_None);
1886 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001887}
1888#endif
1889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001890
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001891#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001892static char posix_ftruncate__doc__[] =
1893"ftruncate(fd, length) -> None\n\
1894Truncate a file to a specified length.";
1895
Barry Warsaw53699e91996-12-10 23:23:01 +00001896static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001897posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001898 PyObject *self; /* Not used */
1899 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001900{
1901 int fd;
1902 long length;
1903 int res;
1904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001906 return NULL;
1907
Barry Warsaw53699e91996-12-10 23:23:01 +00001908 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001909 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00001910 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001911 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001912 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001913 return NULL;
1914 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001915 Py_INCREF(Py_None);
1916 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001917}
1918#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001919
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001920#ifdef NeXT
1921#define HAVE_PUTENV
1922/* Steve Spicklemire got this putenv from NeXTAnswers */
1923static int
1924putenv(char *newval)
1925{
1926 extern char **environ;
1927
1928 static int firstTime = 1;
1929 char **ep;
1930 char *cp;
1931 int esiz;
1932 char *np;
1933
1934 if (!(np = strchr(newval, '=')))
1935 return 1;
1936 *np = '\0';
1937
1938 /* look it up */
1939 for (ep=environ ; *ep ; ep++)
1940 {
1941 /* this should always be true... */
1942 if (cp = strchr(*ep, '='))
1943 {
1944 *cp = '\0';
1945 if (!strcmp(*ep, newval))
1946 {
1947 /* got it! */
1948 *cp = '=';
1949 break;
1950 }
1951 *cp = '=';
1952 }
1953 else
1954 {
1955 *np = '=';
1956 return 1;
1957 }
1958 }
1959
1960 *np = '=';
1961 if (*ep)
1962 {
1963 /* the string was already there:
1964 just replace it with the new one */
1965 *ep = newval;
1966 return 0;
1967 }
1968
1969 /* expand environ by one */
1970 for (esiz=2, ep=environ ; *ep ; ep++)
1971 esiz++;
1972 if (firstTime)
1973 {
1974 char **epp;
1975 char **newenv;
1976 if (!(newenv = malloc(esiz * sizeof(char *))))
1977 return 1;
1978
1979 for (ep=environ, epp=newenv ; *ep ;)
1980 *epp++ = *ep++;
1981 *epp++ = newval;
1982 *epp = (char *) 0;
1983 environ = newenv;
1984 }
1985 else
1986 {
1987 if (!(environ = realloc(environ, esiz * sizeof(char *))))
1988 return 1;
1989 environ[esiz - 2] = newval;
1990 environ[esiz - 1] = (char *) 0;
1991 firstTime = 0;
1992 }
1993
1994 return 0;
1995}
1996#endif NeXT
1997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00001999#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002000static char posix_putenv__doc__[] =
2001"putenv(key, value) -> None\n\
2002Change or add an environment variable.";
2003
Barry Warsaw53699e91996-12-10 23:23:01 +00002004static PyObject *
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002005posix_putenv(self,args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002006 PyObject *self;
2007 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002008{
2009 char *s1, *s2;
2010 char *new;
2011
Barry Warsaw53699e91996-12-10 23:23:01 +00002012 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002013 return NULL;
2014 /* XXX This leaks memory -- not easy to fix :-( */
2015 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002016 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002017 (void) sprintf(new, "%s=%s", s1, s2);
2018 if (putenv(new)) {
2019 posix_error();
2020 return NULL;
2021 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002022 Py_INCREF(Py_None);
2023 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002024}
2025#endif
2026
Barry Warsaw53699e91996-12-10 23:23:01 +00002027static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002028 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2029 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002030#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002031 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002032#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002033#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002034 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002035#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002036#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002037 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002038#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002039 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2040 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2041 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002042#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002043 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002044#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002045#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002046 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002047#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002048 {"rename", posix_rename, 0, posix_rename__doc__},
2049 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2050 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002051#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002052 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002053#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002054#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002055 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002056#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002058#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002060#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002061 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2062 {"remove", posix_unlink, 0, posix_remove__doc__},
2063 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002064#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002065 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002066#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002068#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002069 {"execv", posix_execv, 0, posix_execv__doc__},
2070 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002071#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002072#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002073 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002074#endif /* HAVE_FORK */
2075#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002076 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002077#endif /* HAVE_GETEGID */
2078#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002079 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002080#endif /* HAVE_GETEUID */
2081#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002082 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002083#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002084 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002087#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002088#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002089 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002090#endif /* HAVE_GETPPID */
2091#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002092 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002093#endif /* HAVE_GETUID */
2094#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002095 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002096#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002097#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002099#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002100#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002101 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002102#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002104 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002105#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002106#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002108#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002109#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002110 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002111#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002112#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002114#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002115#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002116 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002117#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002118#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002119 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002120#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002122 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002124#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002125 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002126#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002127#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002128 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002129#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002130 {"open", posix_open, 1, posix_open__doc__},
2131 {"close", posix_close, 0, posix_close__doc__},
2132 {"dup", posix_dup, 0, posix_dup__doc__},
2133 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2134 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2135 {"read", posix_read, 0, posix_read__doc__},
2136 {"write", posix_write, 0, posix_write__doc__},
2137 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2138 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002139#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002141#endif
2142#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002143 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002144#endif
2145#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002146 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002147#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002148#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002149 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002150#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002151 {NULL, NULL} /* Sentinel */
2152};
2153
2154
Barry Warsaw4a342091996-12-19 23:50:02 +00002155static int
2156ins(d, symbol, value)
2157 PyObject* d;
2158 char* symbol;
2159 long value;
2160{
2161 PyObject* v = PyInt_FromLong(value);
2162 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2163 return -1; /* triggers fatal error */
2164
2165 Py_DECREF(v);
2166 return 0;
2167}
2168
2169static int
2170all_ins(d)
2171 PyObject* d;
2172{
2173#ifdef WNOHANG
2174 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2175#endif
2176#ifdef O_RDONLY
2177 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2178#endif
2179#ifdef O_WRONLY
2180 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2181#endif
2182#ifdef O_RDWR
2183 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2184#endif
2185#ifdef O_NDELAY
2186 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2187#endif
2188#ifdef O_NONBLOCK
2189 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2190#endif
2191#ifdef O_APPEND
2192 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2193#endif
2194#ifdef O_DSYNC
2195 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2196#endif
2197#ifdef O_RSYNC
2198 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2199#endif
2200#ifdef O_SYNC
2201 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2202#endif
2203#ifdef O_NOCTTY
2204 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2205#endif
2206#ifdef O_CREAT
2207 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2208#endif
2209#ifdef O_EXCL
2210 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2211#endif
2212#ifdef O_TRUNC
2213 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2214#endif
2215 return 0;
2216}
2217
2218
2219
2220
Guido van Rossuma0e71301996-05-28 22:30:38 +00002221#if defined(_MSC_VER) || defined(__WATCOMC__)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222void
2223initnt()
2224{
Barry Warsaw53699e91996-12-10 23:23:01 +00002225 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002227 m = Py_InitModule4("nt",
2228 posix_methods,
2229 posix__doc__,
2230 (PyObject *)NULL,
2231 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233
2234 /* Initialize nt.environ dictionary */
2235 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00002237 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00002238 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239
Barry Warsaw4a342091996-12-19 23:50:02 +00002240 if (all_ins(d))
2241 goto finally;
2242
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243 /* Initialize nt.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00002244 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00002245 PyDict_SetItemString(d, "error", PosixError);
2246
2247 if (!PyErr_Occurred())
2248 return;
2249
2250 finally:
2251 Py_FatalError("can't initialize NT posixmodule");
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002253#else /* not a PC port */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002254void
2255initposix()
2256{
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 PyObject *m, *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002259 m = Py_InitModule4("posix",
2260 posix_methods,
2261 posix__doc__,
2262 (PyObject *)NULL,
2263 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 d = PyModule_GetDict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265
2266 /* Initialize posix.environ dictionary */
2267 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002268 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Barry Warsaw4a342091996-12-19 23:50:02 +00002269 goto finally;
Barry Warsaw53699e91996-12-10 23:23:01 +00002270 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002271
Barry Warsaw4a342091996-12-19 23:50:02 +00002272 if (all_ins(d))
2273 goto finally;
2274
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002275 /* Initialize posix.error exception */
Guido van Rossumba9d7c51997-04-29 15:49:54 +00002276 PosixError = PyString_FromString("os.error");
Barry Warsaw4a342091996-12-19 23:50:02 +00002277 PyDict_SetItemString(d, "error", PosixError);
2278
2279 if (!PyErr_Occurred())
2280 return;
2281
2282 finally:
2283 Py_FatalError("can't initialize posix module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002284}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002285#endif /* !_MSC_VER */