posixmodule.c 70.9 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1
/***********************************************************
2 3
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
Guido van Rossum's avatar
Guido van Rossum committed
4 5 6

                        All Rights Reserved

7 8
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
Guido van Rossum's avatar
Guido van Rossum committed
9
provided that the above copyright notice appear in all copies and that
10
both that copyright notice and this permission notice appear in
Guido van Rossum's avatar
Guido van Rossum committed
11
supporting documentation, and that the names of Stichting Mathematisch
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum's avatar
Guido van Rossum committed
29 30 31

******************************************************************/

Guido van Rossum's avatar
Guido van Rossum committed
32 33
/* POSIX module implementation */

34
/* This file is also used for Windows NT and MS-Win.  In that case the module
Guido van Rossum's avatar
Guido van Rossum committed
35 36
   actually calls itself 'nt', not 'posix', and a few functions are
   either unimplemented or implemented differently.  The source
37
   assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossum's avatar
Guido van Rossum committed
38
   of the compiler used.  Different compilers define their own feature
39
   test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossum's avatar
Guido van Rossum committed
40

41
/* See also ../Dos/dosmodule.c */
Guido van Rossum's avatar
Guido van Rossum committed
42

43 44 45 46 47 48
static char posix__doc__ [] =
"This module provides access to operating system functionality that is\n\
standardized by the C Standard and the POSIX standard (a thinly\n\
disguised Unix interface).  Refer to the library manual and\n\
corresponding Unix manual entries for more information on calls.";

Barry Warsaw's avatar
Barry Warsaw committed
49
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
50

Guido van Rossum's avatar
Guido van Rossum committed
51 52 53 54 55 56 57 58
#if defined(PYOS_OS2)
#define  INCL_DOS
#define  INCL_DOSERRORS
#define  INCL_DOSPROCESS
#define  INCL_NOPMAPI
#include <os2.h>
#endif

59 60
#include <sys/types.h>
#include <sys/stat.h>
61 62 63
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>		/* For WNOHANG */
#endif
64

Guido van Rossum's avatar
Guido van Rossum committed
65 66 67 68
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

69
#include "mytime.h"		/* For clock_t on some systems */
Guido van Rossum's avatar
Guido van Rossum committed
70

71 72
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
73
#endif /* HAVE_FCNTL_H */
Guido van Rossum's avatar
Guido van Rossum committed
74

75
/* Various compilers have only certain posix functions */
76
/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum's avatar
Guido van Rossum committed
77 78 79
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
#include <process.h>
#else
80
#if defined(__WATCOMC__) && !defined(__QNX__)		/* Watcom compiler */
81 82 83 84 85 86
#define HAVE_GETCWD     1
#define HAVE_OPENDIR    1
#define HAVE_SYSTEM	1
#if defined(__OS2__)
#define HAVE_EXECV      1
#define HAVE_WAIT       1
Guido van Rossum's avatar
Guido van Rossum committed
87
#endif
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include <process.h>
#else
#ifdef __BORLANDC__		/* Borland compiler */
#define HAVE_EXECV      1
#define HAVE_GETCWD     1
#define HAVE_GETEGID    1
#define HAVE_GETEUID    1
#define HAVE_GETGID     1
#define HAVE_GETPPID    1
#define HAVE_GETUID     1
#define HAVE_KILL       1
#define HAVE_OPENDIR    1
#define HAVE_PIPE       1
#define HAVE_POPEN      1
#define HAVE_SYSTEM	1
#define HAVE_WAIT       1
#else
#ifdef _MSC_VER		/* Microsoft compiler */
106 107
#define HAVE_GETCWD     1
#ifdef MS_WIN32
108 109 110 111 112
#define HAVE_EXECV      1
#define HAVE_PIPE       1
#define HAVE_POPEN      1
#define HAVE_SYSTEM	1
#else /* 16-bit Windows */
113
#endif /* !MS_WIN32 */
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#else			/* all other compilers */
/* Unix functions that the configure script doesn't check for */
#define HAVE_EXECV      1
#define HAVE_FORK       1
#define HAVE_GETCWD     1
#define HAVE_GETEGID    1
#define HAVE_GETEUID    1
#define HAVE_GETGID     1
#define HAVE_GETPPID    1
#define HAVE_GETUID     1
#define HAVE_KILL       1
#define HAVE_OPENDIR    1
#define HAVE_PIPE       1
#define HAVE_POPEN      1
#define HAVE_SYSTEM	1
#define HAVE_WAIT       1
#endif  /* _MSC_VER */
#endif  /* __BORLANDC__ */
132
#endif  /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum's avatar
Guido van Rossum committed
133
#endif /* ! __IBMC__ */
134 135

#ifndef _MSC_VER
136

137
#ifdef HAVE_UNISTD_H
138
#include <unistd.h>
139 140 141 142 143 144
#endif

#ifdef NeXT
/* NeXT's <unistd.h> and <utime.h> aren't worth much */
#undef HAVE_UNISTD_H
#undef HAVE_UTIME_H
145
#define HAVE_WAITPID
146 147 148 149
/* #undef HAVE_GETCWD */
#endif

#ifdef HAVE_UNISTD_H
Guido van Rossum's avatar
Guido van Rossum committed
150 151 152 153 154
/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
extern int rename();
extern int pclose();
extern int lstat();
extern int symlink();
155
#else /* !HAVE_UNISTD_H */
Guido van Rossum's avatar
Guido van Rossum committed
156 157 158
#if defined(PYCC_VACPP)
extern int mkdir Py_PROTO((char *));
#else
159
#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw's avatar
Barry Warsaw committed
160
extern int mkdir Py_PROTO((const char *));
161
#else
Barry Warsaw's avatar
Barry Warsaw committed
162
extern int mkdir Py_PROTO((const char *, mode_t));
163
#endif
Guido van Rossum's avatar
Guido van Rossum committed
164 165 166 167 168
#endif
#if defined(__IBMC__) || defined(__IBMCPP__)
extern int chdir Py_PROTO((char *));
extern int rmdir Py_PROTO((char *));
#else
Barry Warsaw's avatar
Barry Warsaw committed
169 170
extern int chdir Py_PROTO((const char *));
extern int rmdir Py_PROTO((const char *));
Guido van Rossum's avatar
Guido van Rossum committed
171
#endif
Barry Warsaw's avatar
Barry Warsaw committed
172 173 174 175 176 177 178 179 180
extern int chmod Py_PROTO((const char *, mode_t));
extern int chown Py_PROTO((const char *, uid_t, gid_t));
extern char *getcwd Py_PROTO((char *, int));
extern char *strerror Py_PROTO((int));
extern int link Py_PROTO((const char *, const char *));
extern int rename Py_PROTO((const char *, const char *));
extern int stat Py_PROTO((const char *, struct stat *));
extern int unlink Py_PROTO((const char *));
extern int pclose Py_PROTO((FILE *));
181
#ifdef HAVE_SYMLINK
Barry Warsaw's avatar
Barry Warsaw committed
182
extern int symlink Py_PROTO((const char *, const char *));
183
#endif /* HAVE_SYMLINK */
184
#ifdef HAVE_LSTAT
Barry Warsaw's avatar
Barry Warsaw committed
185
extern int lstat Py_PROTO((const char *, struct stat *));
186
#endif /* HAVE_LSTAT */
187
#endif /* !HAVE_UNISTD_H */
188

189
#endif /* !_MSC_VER */
190 191 192

#ifdef HAVE_UTIME_H
#include <utime.h>
193
#endif /* HAVE_UTIME_H */
194

195 196 197 198 199
#ifdef HAVE_SYS_UTIME_H
#include <sys/utime.h>
#define HAVE_UTIME_H /* pretend we do for the rest of this file */
#endif /* HAVE_SYS_UTIME_H */

200 201
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
202
#endif /* HAVE_SYS_TIMES_H */
203 204 205

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
206
#endif /* HAVE_SYS_PARAM_H */
207 208 209

#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
210
#endif /* HAVE_SYS_UTSNAME_H */
211 212 213

#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
214
#endif /* MAXPATHLEN */
215

216
#ifdef HAVE_DIRENT_H
217
#include <dirent.h>
218 219
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
220
#if defined(__WATCOMC__) && !defined(__QNX__)
221 222 223
#include <direct.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
224
#define dirent direct
225
#define NAMLEN(dirent) (dirent)->d_namlen
226
#endif
227
#ifdef HAVE_SYS_NDIR_H
228
#include <sys/ndir.h>
229 230
#endif
#ifdef HAVE_SYS_DIR_H
231
#include <sys/dir.h>
232 233
#endif
#ifdef HAVE_NDIR_H
234
#include <ndir.h>
235 236
#endif
#endif
237

238
#ifdef _MSC_VER
239 240 241 242
#include <direct.h>
#include <io.h>
#include <process.h>
#include <windows.h>
243
#ifdef MS_WIN32
244
#define popen	_popen
245
#define pclose	_pclose
246 247 248
#else /* 16-bit Windows */
#include <dos.h>
#include <ctype.h>
249
#endif /* MS_WIN32 */
250
#endif /* _MSC_VER */
251

252
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
253
#include <io.h>
254
#endif /* OS2 */
Guido van Rossum's avatar
Guido van Rossum committed
255 256 257

/* Return a dictionary corresponding to the POSIX environment table */

258
#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum's avatar
Guido van Rossum committed
259
extern char **environ;
260
#endif /* !_MSC_VER */
Guido van Rossum's avatar
Guido van Rossum committed
261

Barry Warsaw's avatar
Barry Warsaw committed
262
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
263 264
convertenviron()
{
Barry Warsaw's avatar
Barry Warsaw committed
265
	PyObject *d;
Guido van Rossum's avatar
Guido van Rossum committed
266
	char **e;
Barry Warsaw's avatar
Barry Warsaw committed
267
	d = PyDict_New();
Guido van Rossum's avatar
Guido van Rossum committed
268 269 270 271 272 273
	if (d == NULL)
		return NULL;
	if (environ == NULL)
		return d;
	/* XXX This part ignores errors */
	for (e = environ; *e != NULL; e++) {
Barry Warsaw's avatar
Barry Warsaw committed
274
		PyObject *v;
Guido van Rossum's avatar
Guido van Rossum committed
275 276 277
		char *p = strchr(*e, '=');
		if (p == NULL)
			continue;
Barry Warsaw's avatar
Barry Warsaw committed
278
		v = PyString_FromString(p+1);
Guido van Rossum's avatar
Guido van Rossum committed
279 280 281
		if (v == NULL)
			continue;
		*p = '\0';
Barry Warsaw's avatar
Barry Warsaw committed
282
		(void) PyDict_SetItemString(d, *e, v);
Guido van Rossum's avatar
Guido van Rossum committed
283
		*p = '=';
Barry Warsaw's avatar
Barry Warsaw committed
284
		Py_DECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
285
	}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#if defined(PYOS_OS2)
    {
        APIRET rc;
        char   buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */

        rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
        if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
            PyObject *v = PyString_FromString(buffer);
		    PyDict_SetItemString(d, "BEGINLIBPATH", v);
            Py_DECREF(v);
        }
        rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
        if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
            PyObject *v = PyString_FromString(buffer);
		    PyDict_SetItemString(d, "ENDLIBPATH", v);
            Py_DECREF(v);
        }
    }
#endif
Guido van Rossum's avatar
Guido van Rossum committed
305 306 307 308
	return d;
}


Barry Warsaw's avatar
Barry Warsaw committed
309
static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum's avatar
Guido van Rossum committed
310 311 312

/* Set a POSIX-specific error from errno, and return NULL */

313 314
static PyObject *
posix_error()
Guido van Rossum's avatar
Guido van Rossum committed
315
{
Barry Warsaw's avatar
Barry Warsaw committed
316
	return PyErr_SetFromErrno(PosixError);
Guido van Rossum's avatar
Guido van Rossum committed
317
}
318 319 320 321 322 323 324
static PyObject *
posix_error_with_filename(name)
	char* name;
{
	return PyErr_SetFromErrnoWithFilename(PosixError, name);
}

Guido van Rossum's avatar
Guido van Rossum committed
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
#if defined(PYOS_OS2)
/**********************************************************************
 *         Helper Function to Trim and Format OS/2 Messages
 **********************************************************************/
    static void
os2_formatmsg(char *msgbuf, int msglen, char *reason)
{
    msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */

    if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
        char *lastc = &msgbuf[ strlen(msgbuf)-1 ];

        while (lastc > msgbuf && isspace(*lastc))
            *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
    }

    /* Add Optional Reason Text */
    if (reason) {
        strcat(msgbuf, " : ");
        strcat(msgbuf, reason);
    }
}

/**********************************************************************
 *             Decode an OS/2 Operating System Error Code
 *
 * A convenience function to lookup an OS/2 error code and return a
 * text message we can use to raise a Python exception.
 *
 * Notes:
 *   The messages for errors returned from the OS/2 kernel reside in
 *   the file OSO001.MSG in the \OS2 directory hierarchy.
 *
 **********************************************************************/
    static char *
os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
{
    APIRET rc;
    ULONG  msglen;

    /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
    Py_BEGIN_ALLOW_THREADS
    rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
                       errorcode, "oso001.msg", &msglen);
    Py_END_ALLOW_THREADS

    if (rc == NO_ERROR)
        os2_formatmsg(msgbuf, msglen, reason);
    else
        sprintf(msgbuf, "unknown OS error #%d", errorcode);

    return msgbuf;
}

/* Set an OS/2-specific error and return NULL.  OS/2 kernel
   errors are not in a global variable e.g. 'errno' nor are
   they congruent with posix error numbers. */

static PyObject * os2_error(int code)
{
    char text[1024];
    PyObject *v;

    os2_strerror(text, sizeof(text), code, "");

    v = Py_BuildValue("(is)", code, text);
    if (v != NULL) {
        PyErr_SetObject(PosixError, v);
        Py_DECREF(v);
    }
    return NULL; /* Signal to Python that an Exception is Pending */
}

#endif /* OS2 */
Guido van Rossum's avatar
Guido van Rossum committed
400 401 402

/* POSIX generic methods */

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static PyObject *
posix_int(args, func)
        PyObject *args;
	int (*func) Py_FPROTO((int));
{
	int fd;
	int res;
	if (!PyArg_Parse(args,  "i", &fd))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = (*func)(fd);
	Py_END_ALLOW_THREADS
	if (res < 0)
		return posix_error();
	Py_INCREF(Py_None);
	return Py_None;
}


Barry Warsaw's avatar
Barry Warsaw committed
422
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
423
posix_1str(args, func)
Barry Warsaw's avatar
Barry Warsaw committed
424 425
	PyObject *args;
	int (*func) Py_FPROTO((const char *));
Guido van Rossum's avatar
Guido van Rossum committed
426
{
Guido van Rossum's avatar
Guido van Rossum committed
427
	char *path1;
Guido van Rossum's avatar
Guido van Rossum committed
428
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
429
	if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum's avatar
Guido van Rossum committed
430
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
431
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
432
	res = (*func)(path1);
Barry Warsaw's avatar
Barry Warsaw committed
433
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
434
	if (res < 0)
435
		return posix_error_with_filename(path1);
Barry Warsaw's avatar
Barry Warsaw committed
436 437
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
438 439
}

Barry Warsaw's avatar
Barry Warsaw committed
440
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
441
posix_2str(args, func)
Barry Warsaw's avatar
Barry Warsaw committed
442 443
	PyObject *args;
	int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum's avatar
Guido van Rossum committed
444
{
Guido van Rossum's avatar
Guido van Rossum committed
445
	char *path1, *path2;
Guido van Rossum's avatar
Guido van Rossum committed
446
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
447
	if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum's avatar
Guido van Rossum committed
448
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
449
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
450
	res = (*func)(path1, path2);
Barry Warsaw's avatar
Barry Warsaw committed
451
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
452
	if (res != 0)
453
		/* XXX how to report both path1 and path2??? */
Guido van Rossum's avatar
Guido van Rossum committed
454
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
455 456
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
457 458
}

Barry Warsaw's avatar
Barry Warsaw committed
459
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
460
posix_strint(args, func)
Barry Warsaw's avatar
Barry Warsaw committed
461 462
	PyObject *args;
	int (*func) Py_FPROTO((const char *, int));
Guido van Rossum's avatar
Guido van Rossum committed
463
{
Guido van Rossum's avatar
Guido van Rossum committed
464
	char *path;
Guido van Rossum's avatar
Guido van Rossum committed
465
	int i;
Guido van Rossum's avatar
Guido van Rossum committed
466
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
467
	if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum's avatar
Guido van Rossum committed
468
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
469
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
470
	res = (*func)(path, i);
Barry Warsaw's avatar
Barry Warsaw committed
471
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
472
	if (res < 0)
473
		return posix_error_with_filename(path);
Barry Warsaw's avatar
Barry Warsaw committed
474 475
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
476 477
}

Barry Warsaw's avatar
Barry Warsaw committed
478
static PyObject *
479
posix_strintint(args, func)
Barry Warsaw's avatar
Barry Warsaw committed
480 481
	PyObject *args;
	int (*func) Py_FPROTO((const char *, int, int));
482 483 484 485
{
	char *path;
	int i,i2;
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
486
	if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
487
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
488
	Py_BEGIN_ALLOW_THREADS
489
	res = (*func)(path, i, i2);
Barry Warsaw's avatar
Barry Warsaw committed
490
	Py_END_ALLOW_THREADS
491
	if (res < 0)
492
		return posix_error_with_filename(path);
Barry Warsaw's avatar
Barry Warsaw committed
493 494
	Py_INCREF(Py_None);
	return Py_None;
495 496
}

Barry Warsaw's avatar
Barry Warsaw committed
497
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
498
posix_do_stat(self, args, statfunc)
Barry Warsaw's avatar
Barry Warsaw committed
499 500 501
	PyObject *self;
	PyObject *args;
	int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum's avatar
Guido van Rossum committed
502 503
{
	struct stat st;
Guido van Rossum's avatar
Guido van Rossum committed
504
	char *path;
Guido van Rossum's avatar
Guido van Rossum committed
505
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
506
	if (!PyArg_Parse(args, "s", &path))
Guido van Rossum's avatar
Guido van Rossum committed
507
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
508
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
509
	res = (*statfunc)(path, &st);
Barry Warsaw's avatar
Barry Warsaw committed
510
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
511
	if (res != 0)
512
		return posix_error_with_filename(path);
513
#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw's avatar
Barry Warsaw committed
514
	return Py_BuildValue("(llllllllll)",
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
			     (long)st.st_mode,
			     (long)st.st_ino,
			     (long)st.st_dev,
			     (long)st.st_nlink,
			     (long)st.st_uid,
			     (long)st.st_gid,
			     (long)st.st_size,
			     (long)st.st_atime,
			     (long)st.st_mtime,
			     (long)st.st_ctime);
#else
	return Py_BuildValue("(lLllllLlll)",
			     (long)st.st_mode,
			     (LONG_LONG)st.st_ino,
			     (long)st.st_dev,
			     (long)st.st_nlink,
			     (long)st.st_uid,
			     (long)st.st_gid,
			     (LONG_LONG)st.st_size,
			     (long)st.st_atime,
			     (long)st.st_mtime,
			     (long)st.st_ctime);
#endif
Guido van Rossum's avatar
Guido van Rossum committed
538 539 540 541 542
}


/* POSIX methods */

543
static char posix_access__doc__[] =
544
"access(path, mode) -> 1 if granted, 0 otherwise\n\
545 546 547 548 549 550 551
Test for access to a file.";

static PyObject *
posix_access(self, args)
	PyObject *self;
	PyObject *args;
{
552 553 554 555 556 557 558 559 560 561
	char *path;
	int mode;
	int res;

	if (!PyArg_Parse(args, "(si)", &path, &mode))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = access(path, mode);
	Py_END_ALLOW_THREADS
	return(PyInt_FromLong(res == 0 ? 1L : 0L));
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
}

static char posix_ttyname__doc__[] =
"ttyname(fd, mode) -> String\n\
Return the name of the terminal device connected to 'fd'.";

static PyObject *
posix_ttyname(self, args)
	PyObject *self;
	PyObject *args;
{
	PyObject *file;
	int id;
	char *ret;

	if (!PyArg_Parse(args, "i", &id))
		return NULL;

	ret = ttyname(id);
	if (ret == NULL)
		return(posix_error());
	return(PyString_FromString(ret));
}

586 587 588 589
static char posix_chdir__doc__[] =
"chdir(path) -> None\n\
Change the current working directory to the specified path.";

Barry Warsaw's avatar
Barry Warsaw committed
590
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
591
posix_chdir(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
592 593
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
594 595 596 597
{
	return posix_1str(args, chdir);
}

598 599 600 601 602

static char posix_chmod__doc__[] =
"chmod(path, mode) -> None\n\
Change the access permissions of a file.";

Barry Warsaw's avatar
Barry Warsaw committed
603
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
604
posix_chmod(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
605 606
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
607 608 609 610
{
	return posix_strint(args, chmod);
}

611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
#ifdef HAVE_FSYNC
static char posix_fsync__doc__[] =
"fsync(fildes) -> None\n\
force write of file with filedescriptor to disk.";

static PyObject *
posix_fsync(self, args)
       PyObject *self;
       PyObject *args;
{
       return posix_int(args, fsync);
}
#endif /* HAVE_FSYNC */

#ifdef HAVE_FDATASYNC
static char posix_fdatasync__doc__[] =
"fdatasync(fildes) -> None\n\
force write of file with filedescriptor to disk.\n\
 does not force update of metadata.";

632 633
extern int fdatasync(int); /* Prototype just in case */

634 635 636 637 638 639 640 641 642 643
static PyObject *
posix_fdatasync(self, args)
       PyObject *self;
       PyObject *args;
{
       return posix_int(args, fdatasync);
}
#endif /* HAVE_FDATASYNC */


644
#ifdef HAVE_CHOWN
645 646 647 648
static char posix_chown__doc__[] =
"chown(path, uid, gid) -> None\n\
Change the owner and group id of path to the numeric uid and gid.";

Barry Warsaw's avatar
Barry Warsaw committed
649
static PyObject *
650
posix_chown(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
651 652
	PyObject *self;
	PyObject *args;
653 654 655
{
	return posix_strintint(args, chown);
}
656
#endif /* HAVE_CHOWN */
657

658

659
#ifdef HAVE_GETCWD
660 661 662 663
static char posix_getcwd__doc__[] =
"getcwd() -> path\n\
Return a string representing the current working directory.";

Barry Warsaw's avatar
Barry Warsaw committed
664
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
665
posix_getcwd(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
666 667
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
668 669
{
	char buf[1026];
Guido van Rossum's avatar
Guido van Rossum committed
670
	char *res;
Barry Warsaw's avatar
Barry Warsaw committed
671
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
672
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
673
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
674
	res = getcwd(buf, sizeof buf);
Barry Warsaw's avatar
Barry Warsaw committed
675
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
676
	if (res == NULL)
Guido van Rossum's avatar
Guido van Rossum committed
677
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
678
	return PyString_FromString(buf);
Guido van Rossum's avatar
Guido van Rossum committed
679
}
680
#endif
Guido van Rossum's avatar
Guido van Rossum committed
681

682

683
#ifdef HAVE_LINK
684 685 686 687
static char posix_link__doc__[] =
"link(src, dst) -> None\n\
Create a hard link to a file.";

Barry Warsaw's avatar
Barry Warsaw committed
688
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
689
posix_link(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
690 691
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
692 693 694
{
	return posix_2str(args, link);
}
695
#endif /* HAVE_LINK */
696

697 698 699 700 701 702 703 704 705 706

static char posix_listdir__doc__[] =
"listdir(path) -> list_of_strings\n\
Return a list containing the names of the entries in the directory.\n\
\n\
	path: path of directory to list\n\
\n\
The list is in arbitrary order.  It does not include the special\n\
entries '.' and '..' even if they are present in the directory.";

Barry Warsaw's avatar
Barry Warsaw committed
707
static PyObject *
708
posix_listdir(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
709 710
	PyObject *self;
	PyObject *args;
711
{
Guido van Rossum's avatar
Guido van Rossum committed
712
	/* XXX Should redo this putting the (now four) versions of opendir
713
	   in separate files instead of having them all here... */
714
#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
715

716 717
	char *name;
	int len;
Barry Warsaw's avatar
Barry Warsaw committed
718
	PyObject *d, *v;
719 720 721 722
	HANDLE hFindFile;
	WIN32_FIND_DATA FileData;
	char namebuf[MAX_PATH+5];

723
	if (!PyArg_Parse(args, "t#", &name, &len))
724 725
		return NULL;
	if (len >= MAX_PATH) {
Barry Warsaw's avatar
Barry Warsaw committed
726
		PyErr_SetString(PyExc_ValueError, "path too long");
727 728 729 730 731 732 733
		return NULL;
	}
	strcpy(namebuf, name);
	if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
		namebuf[len++] = '/';
	strcpy(namebuf + len, "*.*");

Barry Warsaw's avatar
Barry Warsaw committed
734
	if ((d = PyList_New(0)) == NULL)
735 736 737 738 739
		return NULL;

	hFindFile = FindFirstFile(namebuf, &FileData);
	if (hFindFile == INVALID_HANDLE_VALUE) {
		errno = GetLastError();
740 741
		if (errno == ERROR_FILE_NOT_FOUND)
			return PyList_New(0);
742 743 744
		return posix_error();
	}
	do {
745 746 747 748 749
		if (FileData.cFileName[0] == '.' &&
		    (FileData.cFileName[1] == '\0' ||
		     FileData.cFileName[1] == '.' &&
		     FileData.cFileName[2] == '\0'))
			continue;
Barry Warsaw's avatar
Barry Warsaw committed
750
		v = PyString_FromString(FileData.cFileName);
751
		if (v == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
752
			Py_DECREF(d);
753 754 755
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
756 757 758
		if (PyList_Append(d, v) != 0) {
			Py_DECREF(v);
			Py_DECREF(d);
759 760 761
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
762
		Py_DECREF(v);
763 764 765 766 767 768
	} while (FindNextFile(hFindFile, &FileData) == TRUE);

	if (FindClose(hFindFile) == FALSE) {
		errno = GetLastError();
		return posix_error();
	}
Guido van Rossum's avatar
Guido van Rossum committed
769

770
	return d;
771

772
#else /* !MS_WIN32 */
773 774 775 776 777 778 779
#ifdef _MSC_VER /* 16-bit Windows */

#ifndef MAX_PATH
#define MAX_PATH	250
#endif
	char *name, *pt;
	int len;
Barry Warsaw's avatar
Barry Warsaw committed
780
	PyObject *d, *v;
781 782 783
	char namebuf[MAX_PATH+5];
	struct _find_t ep;

784
	if (!PyArg_Parse(args, "t#", &name, &len))
785 786
		return NULL;
	if (len >= MAX_PATH) {
Barry Warsaw's avatar
Barry Warsaw committed
787
		PyErr_SetString(PyExc_ValueError, "path too long");
788 789 790 791 792 793 794 795 796 797
		return NULL;
	}
	strcpy(namebuf, name);
	for (pt = namebuf; *pt; pt++)
		if (*pt == '/')
			*pt = '\\';
	if (namebuf[len-1] != '\\')
		namebuf[len++] = '\\';
	strcpy(namebuf + len, "*.*");

Barry Warsaw's avatar
Barry Warsaw committed
798
	if ((d = PyList_New(0)) == NULL)
799 800 801
		return NULL;

	if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw's avatar
Barry Warsaw committed
802 803
			   _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
        {
804 805 806 807 808 809 810 811 812 813 814 815 816
		errno = ENOENT;
		return posix_error();
	}
	do {
		if (ep.name[0] == '.' &&
		    (ep.name[1] == '\0' ||
		     ep.name[1] == '.' &&
		     ep.name[2] == '\0'))
			continue;
		strcpy(namebuf, ep.name);
		for (pt = namebuf; *pt; pt++)
			if (isupper(*pt))
				*pt = tolower(*pt);
Barry Warsaw's avatar
Barry Warsaw committed
817
		v = PyString_FromString(namebuf);
818
		if (v == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
819
			Py_DECREF(d);
820 821 822
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
823 824 825
		if (PyList_Append(d, v) != 0) {
			Py_DECREF(v);
			Py_DECREF(d);
826 827 828
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
829
		Py_DECREF(v);
830 831 832 833
	} while (_dos_findnext(&ep) == 0);

	return d;

Guido van Rossum's avatar
Guido van Rossum committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
#else
#if defined(PYOS_OS2)

#ifndef MAX_PATH
#define MAX_PATH    CCHMAXPATH
#endif
    char *name, *pt;
    int len;
    PyObject *d, *v;
    char namebuf[MAX_PATH+5];
    HDIR  hdir = 1;
    ULONG srchcnt = 1;
    FILEFINDBUF3   ep;
    APIRET rc;

849
	if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum's avatar
Guido van Rossum committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
        return NULL;
    if (len >= MAX_PATH) {
		PyErr_SetString(PyExc_ValueError, "path too long");
        return NULL;
    }
    strcpy(namebuf, name);
    for (pt = namebuf; *pt; pt++)
        if (*pt == '/')
            *pt = '\\';
    if (namebuf[len-1] != '\\')
        namebuf[len++] = '\\';
    strcpy(namebuf + len, "*.*");

	if ((d = PyList_New(0)) == NULL)
        return NULL;

866 867
    rc = DosFindFirst(namebuf,         /* Wildcard Pattern to Match */
                      &hdir,           /* Handle to Use While Search Directory */
Guido van Rossum's avatar
Guido van Rossum committed
868
                      FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
869 870 871
                      &ep, sizeof(ep), /* Structure to Receive Directory Entry */
                      &srchcnt,        /* Max and Actual Count of Entries Per Iteration */
                      FIL_STANDARD);   /* Format of Entry (EAs or Not) */
Guido van Rossum's avatar
Guido van Rossum committed
872 873 874 875 876 877

    if (rc != NO_ERROR) {
        errno = ENOENT;
        return posix_error();
    }

878
    if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum's avatar
Guido van Rossum committed
879 880 881
        do {
            if (ep.achName[0] == '.'
            && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
882
                continue; /* Skip Over "." and ".." Names */
Guido van Rossum's avatar
Guido van Rossum committed
883 884 885

            strcpy(namebuf, ep.achName);

886 887
            /* Leave Case of Name Alone -- In Native Form */
            /* (Removed Forced Lowercasing Code) */
Guido van Rossum's avatar
Guido van Rossum committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

            v = PyString_FromString(namebuf);
            if (v == NULL) {
                Py_DECREF(d);
                d = NULL;
                break;
            }
            if (PyList_Append(d, v) != 0) {
                Py_DECREF(v);
                Py_DECREF(d);
                d = NULL;
                break;
            }
            Py_DECREF(v);
        } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
    }

    return d;
906
#else
907

Guido van Rossum's avatar
Guido van Rossum committed
908
	char *name;
Barry Warsaw's avatar
Barry Warsaw committed
909
	PyObject *d, *v;
Guido van Rossum's avatar
Guido van Rossum committed
910
	DIR *dirp;
911
	struct dirent *ep;
Barry Warsaw's avatar
Barry Warsaw committed
912
	if (!PyArg_Parse(args, "s", &name))
Guido van Rossum's avatar
Guido van Rossum committed
913
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
914
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
915
	if ((dirp = opendir(name)) == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
916
		Py_BLOCK_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
917
		return posix_error();
Guido van Rossum's avatar
Guido van Rossum committed
918
	}
Barry Warsaw's avatar
Barry Warsaw committed
919
	if ((d = PyList_New(0)) == NULL) {
Guido van Rossum's avatar
Guido van Rossum committed
920
		closedir(dirp);
Barry Warsaw's avatar
Barry Warsaw committed
921
		Py_BLOCK_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
922 923 924
		return NULL;
	}
	while ((ep = readdir(dirp)) != NULL) {
925 926
		if (ep->d_name[0] == '.' &&
		    (NAMLEN(ep) == 1 ||
Guido van Rossum's avatar
Guido van Rossum committed
927
		     (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
928
			continue;
Barry Warsaw's avatar
Barry Warsaw committed
929
		v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum's avatar
Guido van Rossum committed
930
		if (v == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
931
			Py_DECREF(d);
Guido van Rossum's avatar
Guido van Rossum committed
932 933 934
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
935 936 937
		if (PyList_Append(d, v) != 0) {
			Py_DECREF(v);
			Py_DECREF(d);
Guido van Rossum's avatar
Guido van Rossum committed
938 939 940
			d = NULL;
			break;
		}
Barry Warsaw's avatar
Barry Warsaw committed
941
		Py_DECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
942 943
	}
	closedir(dirp);
Barry Warsaw's avatar
Barry Warsaw committed
944
	Py_END_ALLOW_THREADS
945

Guido van Rossum's avatar
Guido van Rossum committed
946
	return d;
947

Guido van Rossum's avatar
Guido van Rossum committed
948
#endif /* !PYOS_OS2 */
949
#endif /* !_MSC_VER */
950
#endif /* !MS_WIN32 */
Guido van Rossum's avatar
Guido van Rossum committed
951 952
}

953 954 955 956
static char posix_mkdir__doc__[] =
"mkdir(path [, mode=0777]) -> None\n\
Create a directory.";

Barry Warsaw's avatar
Barry Warsaw committed
957
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
958
posix_mkdir(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
959 960
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
961
{
962 963
	int res;
	char *path;
964
	int mode = 0777;
Barry Warsaw's avatar
Barry Warsaw committed
965
	if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
966
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
967
	Py_BEGIN_ALLOW_THREADS
968
#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
969 970
	res = mkdir(path);
#else
971
	res = mkdir(path, mode);
972
#endif
Barry Warsaw's avatar
Barry Warsaw committed
973
	Py_END_ALLOW_THREADS
974
	if (res < 0)
975
		return posix_error_with_filename(path);
Barry Warsaw's avatar
Barry Warsaw committed
976 977
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
978 979
}

980

981
#ifdef HAVE_NICE
982 983 984 985
static char posix_nice__doc__[] =
"nice(inc) -> new_priority\n\
Decrease the priority of process and return new priority.";

Barry Warsaw's avatar
Barry Warsaw committed
986
static PyObject *
987
posix_nice(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
988 989
	PyObject *self;
	PyObject *args;
990 991 992
{
	int increment, value;

Barry Warsaw's avatar
Barry Warsaw committed
993
	if (!PyArg_Parse(args, "i", &increment))
994 995 996 997
		return NULL;
	value = nice(increment);
	if (value == -1)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
998
	return PyInt_FromLong((long) value);
999
}
1000
#endif /* HAVE_NICE */
1001

1002 1003 1004 1005 1006

static char posix_rename__doc__[] =
"rename(old, new) -> None\n\
Rename a file or directory.";

Barry Warsaw's avatar
Barry Warsaw committed
1007
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1008
posix_rename(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1009 1010
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1011 1012 1013 1014
{
	return posix_2str(args, rename);
}

1015 1016 1017 1018 1019

static char posix_rmdir__doc__[] =
"rmdir(path) -> None\n\
Remove a directory.";

Barry Warsaw's avatar
Barry Warsaw committed
1020
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1021
posix_rmdir(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1022 1023
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1024 1025 1026 1027
{
	return posix_1str(args, rmdir);
}

1028 1029 1030 1031 1032

static char posix_stat__doc__[] =
"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
Perform a stat system call on the given path.";

Barry Warsaw's avatar
Barry Warsaw committed
1033
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1034
posix_stat(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1035 1036
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1037 1038 1039 1040
{
	return posix_do_stat(self, args, stat);
}

1041

1042
#ifdef HAVE_SYSTEM
1043 1044 1045 1046
static char posix_system__doc__[] =
"system(command) -> exit_status\n\
Execute the command (a string) in a subshell.";

Barry Warsaw's avatar
Barry Warsaw committed
1047
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1048
posix_system(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1049 1050
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1051
{
Guido van Rossum's avatar
Guido van Rossum committed
1052
	char *command;
Guido van Rossum's avatar
Guido van Rossum committed
1053
	long sts;
Barry Warsaw's avatar
Barry Warsaw committed
1054
	if (!PyArg_Parse(args, "s", &command))
Guido van Rossum's avatar
Guido van Rossum committed
1055
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1056
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1057
	sts = system(command);
Barry Warsaw's avatar
Barry Warsaw committed
1058 1059
	Py_END_ALLOW_THREADS
	return PyInt_FromLong(sts);
Guido van Rossum's avatar
Guido van Rossum committed
1060
}
1061
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1062

1063 1064 1065 1066 1067

static char posix_umask__doc__[] =
"umask(new_mask) -> old_mask\n\
Set the current numeric umask and return the previous umask.";

Barry Warsaw's avatar
Barry Warsaw committed
1068
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1069
posix_umask(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1070 1071
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1072 1073
{
	int i;
Barry Warsaw's avatar
Barry Warsaw committed
1074
	if (!PyArg_Parse(args, "i", &i))
Guido van Rossum's avatar
Guido van Rossum committed
1075 1076 1077 1078
		return NULL;
	i = umask(i);
	if (i < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1079
	return PyInt_FromLong((long)i);
Guido van Rossum's avatar
Guido van Rossum committed
1080 1081
}

1082 1083 1084 1085 1086 1087 1088 1089 1090

static char posix_unlink__doc__[] =
"unlink(path) -> None\n\
Remove a file (same as remove(path)).";

static char posix_remove__doc__[] =
"remove(path) -> None\n\
Remove a file (same as unlink(path)).";

Barry Warsaw's avatar
Barry Warsaw committed
1091
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1092
posix_unlink(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1093 1094
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1095 1096 1097 1098
{
	return posix_1str(args, unlink);
}

1099

1100
#ifdef HAVE_UNAME
1101 1102 1103 1104
static char posix_uname__doc__[] =
"uname() -> (sysname, nodename, release, version, machine)\n\
Return a tuple identifying the current operating system.";

Barry Warsaw's avatar
Barry Warsaw committed
1105
static PyObject *
1106
posix_uname(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1107 1108
	PyObject *self;
	PyObject *args;
1109 1110
{
	struct utsname u;
Guido van Rossum's avatar
Guido van Rossum committed
1111
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
1112
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1113
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1114
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1115
	res = uname(&u);
Barry Warsaw's avatar
Barry Warsaw committed
1116
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1117
	if (res < 0)
1118
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1119
	return Py_BuildValue("(sssss)",
Barry Warsaw's avatar
Barry Warsaw committed
1120 1121 1122 1123 1124
			     u.sysname,
			     u.nodename,
			     u.release,
			     u.version,
			     u.machine);
1125
}
1126
#endif /* HAVE_UNAME */
1127

1128 1129 1130 1131 1132

static char posix_utime__doc__[] =
"utime(path, (atime, utime)) -> None\n\
Set the access and modified time of the file to the given values.";

Barry Warsaw's avatar
Barry Warsaw committed
1133
static PyObject *
1134
posix_utime(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1135 1136
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1137
{
Guido van Rossum's avatar
Guido van Rossum committed
1138
	char *path;
Guido van Rossum's avatar
Guido van Rossum committed
1139
	long atime, mtime;
Guido van Rossum's avatar
Guido van Rossum committed
1140
	int res;
1141

1142
/* XXX should define struct utimbuf instead, above */
1143
#ifdef HAVE_UTIME_H
1144 1145 1146 1147
	struct utimbuf buf;
#define ATIME buf.actime
#define MTIME buf.modtime
#define UTIME_ARG &buf
1148
#else /* HAVE_UTIME_H */
1149 1150 1151 1152
	time_t buf[2];
#define ATIME buf[0]
#define MTIME buf[1]
#define UTIME_ARG buf
1153
#endif /* HAVE_UTIME_H */
1154

Barry Warsaw's avatar
Barry Warsaw committed
1155
	if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum's avatar
Guido van Rossum committed
1156
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
1157
	ATIME = atime;
Guido van Rossum's avatar
Guido van Rossum committed
1158
	MTIME = mtime;
Barry Warsaw's avatar
Barry Warsaw committed
1159
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1160
	res = utime(path, UTIME_ARG);
Barry Warsaw's avatar
Barry Warsaw committed
1161
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1162
	if (res < 0)
1163
		return posix_error_with_filename(path);
Barry Warsaw's avatar
Barry Warsaw committed
1164 1165
	Py_INCREF(Py_None);
	return Py_None;
1166 1167 1168
#undef UTIME_ARG
#undef ATIME
#undef MTIME
Guido van Rossum's avatar
Guido van Rossum committed
1169 1170
}

1171

Guido van Rossum's avatar
Guido van Rossum committed
1172
/* Process operations */
1173

1174 1175 1176 1177
static char posix__exit__doc__[] =
"_exit(status)\n\
Exit to the system with specified status, without normal exit processing.";

Barry Warsaw's avatar
Barry Warsaw committed
1178
static PyObject *
1179
posix__exit(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1180 1181
	PyObject *self;
	PyObject *args;
1182 1183
{
	int sts;
Barry Warsaw's avatar
Barry Warsaw committed
1184
	if (!PyArg_Parse(args, "i", &sts))
1185 1186
		return NULL;
	_exit(sts);
Guido van Rossum's avatar
Guido van Rossum committed
1187
	return NULL; /* Make gcc -Wall happy */
1188 1189
}

1190

1191
#ifdef HAVE_EXECV
1192 1193 1194 1195 1196 1197 1198
static char posix_execv__doc__[] =
"execv(path, args)\n\
Execute an executable path with arguments, replacing current process.\n\
\n\
	path: path of executable file\n\
	args: tuple or list of strings";

Barry Warsaw's avatar
Barry Warsaw committed
1199
static PyObject *
1200
posix_execv(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1201 1202
	PyObject *self;
	PyObject *args;
1203
{
Guido van Rossum's avatar
Guido van Rossum committed
1204
	char *path;
Barry Warsaw's avatar
Barry Warsaw committed
1205
	PyObject *argv;
1206 1207
	char **argvlist;
	int i, argc;
Barry Warsaw's avatar
Barry Warsaw committed
1208
	PyObject *(*getitem) Py_PROTO((PyObject *, int));
1209

1210
	/* execv has two arguments: (path, argv), where
1211 1212
	   argv is a list or tuple of strings. */

Barry Warsaw's avatar
Barry Warsaw committed
1213
	if (!PyArg_Parse(args, "(sO)", &path, &argv))
1214
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1215 1216 1217
	if (PyList_Check(argv)) {
		argc = PyList_Size(argv);
		getitem = PyList_GetItem;
1218
	}
Barry Warsaw's avatar
Barry Warsaw committed
1219 1220 1221
	else if (PyTuple_Check(argv)) {
		argc = PyTuple_Size(argv);
		getitem = PyTuple_GetItem;
1222
	}
Guido van Rossum's avatar
Guido van Rossum committed
1223 1224
	else {
 badarg:
Barry Warsaw's avatar
Barry Warsaw committed
1225
		PyErr_BadArgument();
Guido van Rossum's avatar
Guido van Rossum committed
1226 1227
		return NULL;
	}
1228

Barry Warsaw's avatar
Barry Warsaw committed
1229
	argvlist = PyMem_NEW(char *, argc+1);
1230 1231 1232
	if (argvlist == NULL)
		return NULL;
	for (i = 0; i < argc; i++) {
Barry Warsaw's avatar
Barry Warsaw committed
1233 1234
		if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
			PyMem_DEL(argvlist);
1235 1236 1237 1238 1239
			goto badarg;
		}
	}
	argvlist[argc] = NULL;

1240 1241
#ifdef BAD_EXEC_PROTOTYPES
	execv(path, (const char **) argvlist);
1242
#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossum's avatar
Guido van Rossum committed
1243
	execv(path, argvlist);
1244
#endif /* BAD_EXEC_PROTOTYPES */
1245

1246 1247
	/* If we get here it's definitely an error */

Barry Warsaw's avatar
Barry Warsaw committed
1248
	PyMem_DEL(argvlist);
1249 1250 1251
	return posix_error();
}

1252 1253 1254 1255 1256 1257 1258 1259 1260

static char posix_execve__doc__[] =
"execve(path, args, env)\n\
Execute a path with arguments and environment, replacing current process.\n\
\n\
	path: path of executable file\n\
	args: tuple or list of arguments\n\
	env: dictonary of strings mapping to strings";

Barry Warsaw's avatar
Barry Warsaw committed
1261
static PyObject *
1262
posix_execve(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1263 1264
	PyObject *self;
	PyObject *args;
1265 1266
{
	char *path;
Barry Warsaw's avatar
Barry Warsaw committed
1267
	PyObject *argv, *env;
1268 1269
	char **argvlist;
	char **envlist;
1270
	PyObject *key, *val, *keys=NULL, *vals=NULL;
1271
	int i, pos, argc, envc;
Barry Warsaw's avatar
Barry Warsaw committed
1272
	PyObject *(*getitem) Py_PROTO((PyObject *, int));
1273 1274 1275 1276 1277

	/* execve has three arguments: (path, argv, env), where
	   argv is a list or tuple of strings and env is a dictionary
	   like posix.environ. */

Barry Warsaw's avatar
Barry Warsaw committed
1278
	if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
1279
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1280 1281 1282
	if (PyList_Check(argv)) {
		argc = PyList_Size(argv);
		getitem = PyList_GetItem;
1283
	}
Barry Warsaw's avatar
Barry Warsaw committed
1284 1285 1286
	else if (PyTuple_Check(argv)) {
		argc = PyTuple_Size(argv);
		getitem = PyTuple_GetItem;
1287 1288
	}
	else {
Barry Warsaw's avatar
Barry Warsaw committed
1289
		PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1290 1291
		return NULL;
	}
1292 1293
	if (!PyMapping_Check(env)) {
		PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1294 1295 1296
		return NULL;
	}

Barry Warsaw's avatar
Barry Warsaw committed
1297
	argvlist = PyMem_NEW(char *, argc+1);
1298
	if (argvlist == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
1299
		PyErr_NoMemory();
1300 1301 1302
		return NULL;
	}
	for (i = 0; i < argc; i++) {
Barry Warsaw's avatar
Barry Warsaw committed
1303
		if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw's avatar
Barry Warsaw committed
1304 1305 1306
				 "s;argv must be list of strings",
				 &argvlist[i]))
		{
1307 1308 1309 1310 1311
			goto fail_1;
		}
	}
	argvlist[argc] = NULL;

1312
	i = PyMapping_Length(env);
Barry Warsaw's avatar
Barry Warsaw committed
1313
	envlist = PyMem_NEW(char *, i + 1);
1314
	if (envlist == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
1315
		PyErr_NoMemory();
1316 1317 1318
		goto fail_1;
	}
	envc = 0;
1319 1320 1321 1322 1323 1324
	keys = PyMapping_Keys(env);
	vals = PyMapping_Values(env);
	if (!keys || !vals)
		goto fail_2;
	
	for (pos = 0; pos < i; pos++) {
1325
		char *p, *k, *v;
1326 1327 1328 1329 1330 1331

		key = PyList_GetItem(keys, pos);
		val = PyList_GetItem(vals, pos);
		if (!key || !val)
			goto fail_2;
		
Barry Warsaw's avatar
Barry Warsaw committed
1332
		if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw's avatar
Barry Warsaw committed
1333 1334
		    !PyArg_Parse(val, "s;non-string value in env", &v))
		{
1335 1336
			goto fail_2;
		}
1337 1338 1339 1340 1341

#if defined(PYOS_OS2)
        /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
        if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
#endif
Barry Warsaw's avatar
Barry Warsaw committed
1342
		p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1343
		if (p == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
1344
			PyErr_NoMemory();
1345 1346 1347 1348
			goto fail_2;
		}
		sprintf(p, "%s=%s", k, v);
		envlist[envc++] = p;
1349 1350 1351
#if defined(PYOS_OS2)
    }
#endif
1352 1353 1354
	}
	envlist[envc] = 0;

1355 1356 1357

#ifdef BAD_EXEC_PROTOTYPES
	execve(path, (const char **)argvlist, envlist);
1358
#else /* BAD_EXEC_PROTOTYPES */
1359
	execve(path, argvlist, envlist);
1360
#endif /* BAD_EXEC_PROTOTYPES */
1361 1362 1363 1364 1365 1366 1367
	
	/* If we get here it's definitely an error */

	(void) posix_error();

 fail_2:
	while (--envc >= 0)
Barry Warsaw's avatar
Barry Warsaw committed
1368 1369
		PyMem_DEL(envlist[envc]);
	PyMem_DEL(envlist);
1370
 fail_1:
Barry Warsaw's avatar
Barry Warsaw committed
1371
	PyMem_DEL(argvlist);
1372 1373
	Py_XDECREF(vals);
	Py_XDECREF(keys);
1374 1375
	return NULL;
}
1376
#endif /* HAVE_EXECV */
1377

1378

Guido van Rossum's avatar
Guido van Rossum committed
1379
#ifdef HAVE_FORK
1380 1381 1382 1383 1384 1385
static char posix_fork__doc__[] =
"fork() -> pid\n\
Fork a child process.\n\
\n\
Return 0 to child process and PID of child to parent process.";

Barry Warsaw's avatar
Barry Warsaw committed
1386
static PyObject *
1387
posix_fork(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1388 1389
	PyObject *self;
	PyObject *args;
1390 1391
{
	int pid;
Barry Warsaw's avatar
Barry Warsaw committed
1392
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1393
		return NULL;
1394 1395 1396
	pid = fork();
	if (pid == -1)
		return posix_error();
1397
	PyOS_AfterFork();
Barry Warsaw's avatar
Barry Warsaw committed
1398
	return PyInt_FromLong((long)pid);
1399
}
Guido van Rossum's avatar
Guido van Rossum committed
1400
#endif
1401

1402

Guido van Rossum's avatar
Guido van Rossum committed
1403
#ifdef HAVE_GETEGID
1404 1405 1406 1407
static char posix_getegid__doc__[] =
"getegid() -> egid\n\
Return the current process's effective group id.";

Barry Warsaw's avatar
Barry Warsaw committed
1408
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1409
posix_getegid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1410 1411
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1412
{
Barry Warsaw's avatar
Barry Warsaw committed
1413
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1414
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1415
	return PyInt_FromLong((long)getegid());
Guido van Rossum's avatar
Guido van Rossum committed
1416
}
Guido van Rossum's avatar
Guido van Rossum committed
1417
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1418

1419

Guido van Rossum's avatar
Guido van Rossum committed
1420
#ifdef HAVE_GETEUID
1421 1422 1423 1424
static char posix_geteuid__doc__[] =
"geteuid() -> euid\n\
Return the current process's effective user id.";

Barry Warsaw's avatar
Barry Warsaw committed
1425
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1426
posix_geteuid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1427 1428
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1429
{
Barry Warsaw's avatar
Barry Warsaw committed
1430
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1431
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1432
	return PyInt_FromLong((long)geteuid());
Guido van Rossum's avatar
Guido van Rossum committed
1433
}
Guido van Rossum's avatar
Guido van Rossum committed
1434
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1435

1436

Guido van Rossum's avatar
Guido van Rossum committed
1437
#ifdef HAVE_GETGID
1438 1439 1440 1441
static char posix_getgid__doc__[] =
"getgid() -> gid\n\
Return the current process's group id.";

Barry Warsaw's avatar
Barry Warsaw committed
1442
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1443
posix_getgid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1444 1445
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1446
{
Barry Warsaw's avatar
Barry Warsaw committed
1447
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1448
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1449
	return PyInt_FromLong((long)getgid());
Guido van Rossum's avatar
Guido van Rossum committed
1450
}
Guido van Rossum's avatar
Guido van Rossum committed
1451
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1452

1453 1454 1455 1456 1457

static char posix_getpid__doc__[] =
"getpid() -> pid\n\
Return the current process id";

Barry Warsaw's avatar
Barry Warsaw committed
1458
static PyObject *
1459
posix_getpid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1460 1461
	PyObject *self;
	PyObject *args;
1462
{
Barry Warsaw's avatar
Barry Warsaw committed
1463
	if (!PyArg_NoArgs(args))
1464
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1465
	return PyInt_FromLong((long)getpid());
1466 1467
}

1468

1469
#ifdef HAVE_GETPGRP
1470 1471 1472 1473
static char posix_getpgrp__doc__[] =
"getpgrp() -> pgrp\n\
Return the current process group id.";

Barry Warsaw's avatar
Barry Warsaw committed
1474
static PyObject *
1475
posix_getpgrp(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1476 1477
	PyObject *self;
	PyObject *args;
1478
{
Barry Warsaw's avatar
Barry Warsaw committed
1479
	if (!PyArg_NoArgs(args))
1480
		return NULL;
1481
#ifdef GETPGRP_HAVE_ARG
Barry Warsaw's avatar
Barry Warsaw committed
1482
	return PyInt_FromLong((long)getpgrp(0));
1483
#else /* GETPGRP_HAVE_ARG */
Barry Warsaw's avatar
Barry Warsaw committed
1484
	return PyInt_FromLong((long)getpgrp());
1485
#endif /* GETPGRP_HAVE_ARG */
1486
}
1487
#endif /* HAVE_GETPGRP */
1488

1489

1490
#ifdef HAVE_SETPGRP
1491 1492 1493 1494
static char posix_setpgrp__doc__[] =
"setpgrp() -> None\n\
Make this process a session leader.";

Barry Warsaw's avatar
Barry Warsaw committed
1495
static PyObject *
1496
posix_setpgrp(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1497 1498
	PyObject *self;
	PyObject *args;
1499
{
Barry Warsaw's avatar
Barry Warsaw committed
1500
	if (!PyArg_NoArgs(args))
1501
		return NULL;
1502
#ifdef SETPGRP_HAVE_ARG
1503
	if (setpgrp(0, 0) < 0)
1504
#else /* SETPGRP_HAVE_ARG */
1505
	if (setpgrp() < 0)
1506
#endif /* SETPGRP_HAVE_ARG */
1507
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1508 1509
	Py_INCREF(Py_None);
	return Py_None;
1510 1511
}

1512 1513
#endif /* HAVE_SETPGRP */

Guido van Rossum's avatar
Guido van Rossum committed
1514
#ifdef HAVE_GETPPID
1515 1516 1517 1518
static char posix_getppid__doc__[] =
"getppid() -> ppid\n\
Return the parent's process id.";

Barry Warsaw's avatar
Barry Warsaw committed
1519
static PyObject *
1520
posix_getppid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1521 1522
	PyObject *self;
	PyObject *args;
1523
{
Barry Warsaw's avatar
Barry Warsaw committed
1524
	if (!PyArg_NoArgs(args))
1525
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1526
	return PyInt_FromLong((long)getppid());
1527
}
Guido van Rossum's avatar
Guido van Rossum committed
1528
#endif
1529

1530

Guido van Rossum's avatar
Guido van Rossum committed
1531
#ifdef HAVE_GETUID
1532 1533 1534 1535
static char posix_getuid__doc__[] =
"getuid() -> uid\n\
Return the current process's user id.";

Barry Warsaw's avatar
Barry Warsaw committed
1536
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1537
posix_getuid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1538 1539
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1540
{
Barry Warsaw's avatar
Barry Warsaw committed
1541
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1542
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1543
	return PyInt_FromLong((long)getuid());
Guido van Rossum's avatar
Guido van Rossum committed
1544
}
Guido van Rossum's avatar
Guido van Rossum committed
1545
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1546

1547

Guido van Rossum's avatar
Guido van Rossum committed
1548
#ifdef HAVE_KILL
1549 1550 1551 1552
static char posix_kill__doc__[] =
"kill(pid, sig) -> None\n\
Kill a process with a signal.";

Barry Warsaw's avatar
Barry Warsaw committed
1553
static PyObject *
1554
posix_kill(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1555 1556
	PyObject *self;
	PyObject *args;
1557 1558
{
	int pid, sig;
Barry Warsaw's avatar
Barry Warsaw committed
1559
	if (!PyArg_Parse(args, "(ii)", &pid, &sig))
1560
		return NULL;
1561
#if defined(PYOS_OS2)
Guido van Rossum's avatar
Guido van Rossum committed
1562 1563 1564
    if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
        APIRET rc;
        if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
1565
            return os2_error(rc);
Guido van Rossum's avatar
Guido van Rossum committed
1566 1567 1568 1569

    } else if (sig == XCPT_SIGNAL_KILLPROC) {
        APIRET rc;
        if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
1570
            return os2_error(rc);
Guido van Rossum's avatar
Guido van Rossum committed
1571 1572

    } else
1573
        return NULL; /* Unrecognized Signal Requested */
Guido van Rossum's avatar
Guido van Rossum committed
1574
#else
1575 1576
	if (kill(pid, sig) == -1)
		return posix_error();
Guido van Rossum's avatar
Guido van Rossum committed
1577
#endif
Barry Warsaw's avatar
Barry Warsaw committed
1578 1579
	Py_INCREF(Py_None);
	return Py_None;
1580
}
Guido van Rossum's avatar
Guido van Rossum committed
1581
#endif
1582

Guido van Rossum's avatar
Guido van Rossum committed
1583 1584 1585 1586 1587 1588
#ifdef HAVE_PLOCK

#ifdef HAVE_SYS_LOCK_H
#include <sys/lock.h>
#endif

1589 1590 1591 1592
static char posix_plock__doc__[] =
"plock(op) -> None\n\
Lock program segments into memory.";

Barry Warsaw's avatar
Barry Warsaw committed
1593
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1594
posix_plock(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1595 1596
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1597 1598
{
	int op;
Barry Warsaw's avatar
Barry Warsaw committed
1599
	if (!PyArg_Parse(args, "i", &op))
Guido van Rossum's avatar
Guido van Rossum committed
1600 1601 1602
		return NULL;
	if (plock(op) == -1)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1603 1604
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
1605 1606 1607
}
#endif

1608

1609
#ifdef HAVE_POPEN
1610 1611 1612 1613
static char posix_popen__doc__[] =
"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
Open a pipe to/from a command returning a file object.";

Guido van Rossum's avatar
Guido van Rossum committed
1614
#if defined(PYOS_OS2)
1615
static int
Guido van Rossum's avatar
Guido van Rossum committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
async_system(const char *command)
{
    char        *p, errormsg[256], args[1024];
    RESULTCODES  rcodes;
    APIRET       rc;
    char        *shell = getenv("COMSPEC");
    if (!shell)
        shell = "cmd";

    strcpy(args, shell);
    p = &args[ strlen(args)+1 ];
    strcpy(p, "/c ");
    strcat(p, command);
    p += strlen(p) + 1;
    *p = '\0';

    rc = DosExecPgm(errormsg, sizeof(errormsg),
1633
                    EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum's avatar
Guido van Rossum committed
1634
                    args,
1635
                    NULL,       /* Inherit Parent's Environment */
Guido van Rossum's avatar
Guido van Rossum committed
1636 1637 1638 1639
                    &rcodes, shell);
    return rc;
}

1640 1641
static FILE *
popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum's avatar
Guido van Rossum committed
1642 1643 1644 1645 1646
{
    HFILE    rhan, whan;
    FILE    *retfd = NULL;
    APIRET   rc = DosCreatePipe(&rhan, &whan, pipesize);

1647 1648
    if (rc != NO_ERROR) {
	*err = rc;
1649
        return NULL; /* ERROR - Unable to Create Anon Pipe */
1650
    }
Guido van Rossum's avatar
Guido van Rossum committed
1651

1652 1653
    if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
        int oldfd = dup(1);      /* Save STDOUT Handle in Another Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1654

1655 1656
        DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
        close(1);                /* Make STDOUT Available for Reallocation */
Guido van Rossum's avatar
Guido van Rossum committed
1657

1658 1659
        if (dup2(whan, 1) == 0) {      /* Connect STDOUT to Pipe Write Side */
            DosClose(whan);            /* Close Now-Unused Pipe Write Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1660 1661

            if (async_system(command) == NO_ERROR)
1662
                retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1663 1664
        }

1665 1666
        dup2(oldfd, 1);          /* Reconnect STDOUT to Original Handle */
        DosExitCritSec();        /* Now Allow Other Threads to Run */
Guido van Rossum's avatar
Guido van Rossum committed
1667

1668 1669
        close(oldfd);            /* And Close Saved STDOUT Handle */
        return retfd;            /* Return fd of Pipe or NULL if Error */
Guido van Rossum's avatar
Guido van Rossum committed
1670

1671 1672
    } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
        int oldfd = dup(0);      /* Save STDIN Handle in Another Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1673

1674 1675
        DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
        close(0);                /* Make STDIN Available for Reallocation */
Guido van Rossum's avatar
Guido van Rossum committed
1676

1677 1678
        if (dup2(rhan, 0) == 0)     { /* Connect STDIN to Pipe Read Side */
            DosClose(rhan);           /* Close Now-Unused Pipe Read Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1679 1680

            if (async_system(command) == NO_ERROR)
1681
                retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum's avatar
Guido van Rossum committed
1682 1683
        }

1684 1685
        dup2(oldfd, 0);          /* Reconnect STDIN to Original Handle */
        DosExitCritSec();        /* Now Allow Other Threads to Run */
Guido van Rossum's avatar
Guido van Rossum committed
1686

1687 1688
        close(oldfd);            /* And Close Saved STDIN Handle */
        return retfd;            /* Return fd of Pipe or NULL if Error */
Guido van Rossum's avatar
Guido van Rossum committed
1689

1690 1691
    } else {
	*err = ERROR_INVALID_ACCESS;
1692
        return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
1693
    }
Guido van Rossum's avatar
Guido van Rossum committed
1694 1695 1696 1697 1698 1699 1700 1701 1702
}

static PyObject *
posix_popen(self, args)
	PyObject *self;
	PyObject *args;
{
	char *name;
	char *mode = "r";
1703
	int   err, bufsize = -1;
Guido van Rossum's avatar
Guido van Rossum committed
1704 1705 1706 1707 1708
	FILE *fp;
	PyObject *f;
	if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
1709
	fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum's avatar
Guido van Rossum committed
1710 1711
	Py_END_ALLOW_THREADS
	if (fp == NULL)
1712 1713
		return os2_error(err);

Guido van Rossum's avatar
Guido van Rossum committed
1714 1715 1716 1717 1718 1719 1720
	f = PyFile_FromFile(fp, name, mode, fclose);
	if (f != NULL)
		PyFile_SetBufSize(f, bufsize);
	return f;
}

#else
Barry Warsaw's avatar
Barry Warsaw committed
1721
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1722
posix_popen(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1723 1724
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1725
{
1726 1727 1728
	char *name;
	char *mode = "r";
	int bufsize = -1;
Guido van Rossum's avatar
Guido van Rossum committed
1729
	FILE *fp;
Barry Warsaw's avatar
Barry Warsaw committed
1730 1731
	PyObject *f;
	if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum's avatar
Guido van Rossum committed
1732
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1733
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1734
	fp = popen(name, mode);
Barry Warsaw's avatar
Barry Warsaw committed
1735
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1736 1737
	if (fp == NULL)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1738
	f = PyFile_FromFile(fp, name, mode, pclose);
1739
	if (f != NULL)
Barry Warsaw's avatar
Barry Warsaw committed
1740
		PyFile_SetBufSize(f, bufsize);
1741
	return f;
Guido van Rossum's avatar
Guido van Rossum committed
1742
}
Guido van Rossum's avatar
Guido van Rossum committed
1743 1744
#endif

1745
#endif /* HAVE_POPEN */
Guido van Rossum's avatar
Guido van Rossum committed
1746

1747

1748
#ifdef HAVE_SETUID
1749 1750 1751
static char posix_setuid__doc__[] =
"setuid(uid) -> None\n\
Set the current process's user id.";
Barry Warsaw's avatar
Barry Warsaw committed
1752
static PyObject *
1753
posix_setuid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1754 1755
	PyObject *self;
	PyObject *args;
1756 1757
{
	int uid;
Barry Warsaw's avatar
Barry Warsaw committed
1758
	if (!PyArg_Parse(args, "i", &uid))
1759 1760 1761
		return NULL;
	if (setuid(uid) < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1762 1763
	Py_INCREF(Py_None);
	return Py_None;
1764
}
1765
#endif /* HAVE_SETUID */
1766

1767

1768
#ifdef HAVE_SETGID
1769 1770 1771 1772
static char posix_setgid__doc__[] =
"setgid(gid) -> None\n\
Set the current process's group id.";

Barry Warsaw's avatar
Barry Warsaw committed
1773
static PyObject *
1774
posix_setgid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1775 1776
	PyObject *self;
	PyObject *args;
1777 1778
{
	int gid;
Barry Warsaw's avatar
Barry Warsaw committed
1779
	if (!PyArg_Parse(args, "i", &gid))
1780 1781 1782
		return NULL;
	if (setgid(gid) < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1783 1784
	Py_INCREF(Py_None);
	return Py_None;
1785
}
1786
#endif /* HAVE_SETGID */
1787

1788

1789
#ifdef HAVE_WAITPID
1790 1791 1792 1793
static char posix_waitpid__doc__[] =
"waitpid(pid, options) -> (pid, status)\n\
Wait for completion of a give child process.";

Barry Warsaw's avatar
Barry Warsaw committed
1794
static PyObject *
1795
posix_waitpid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1796 1797
	PyObject *self;
	PyObject *args;
1798
{
1799
	int pid, options, sts = 0;
Barry Warsaw's avatar
Barry Warsaw committed
1800
	if (!PyArg_Parse(args, "(ii)", &pid, &options))
1801
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1802
	Py_BEGIN_ALLOW_THREADS
1803 1804 1805
#ifdef NeXT
	pid = wait4(pid, (union wait *)&sts, options, NULL);
#else
1806
	pid = waitpid(pid, &sts, options);
1807
#endif
Barry Warsaw's avatar
Barry Warsaw committed
1808
	Py_END_ALLOW_THREADS
1809 1810 1811
	if (pid == -1)
		return posix_error();
	else
Barry Warsaw's avatar
Barry Warsaw committed
1812
		return Py_BuildValue("ii", pid, sts);
1813
}
1814
#endif /* HAVE_WAITPID */
1815

1816

Guido van Rossum's avatar
Guido van Rossum committed
1817
#ifdef HAVE_WAIT
1818 1819 1820 1821
static char posix_wait__doc__[] =
"wait() -> (pid, status)\n\
Wait for completion of a child process.";

Barry Warsaw's avatar
Barry Warsaw committed
1822
static PyObject *
1823
posix_wait(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1824 1825
	PyObject *self;
	PyObject *args;
1826 1827
{
	int pid, sts;
Barry Warsaw's avatar
Barry Warsaw committed
1828
	Py_BEGIN_ALLOW_THREADS
1829 1830 1831
#ifdef NeXT
	pid = wait((union wait *)&sts);
#else
1832
	pid = wait(&sts);
1833
#endif
Barry Warsaw's avatar
Barry Warsaw committed
1834
	Py_END_ALLOW_THREADS
1835 1836
	if (pid == -1)
		return posix_error();
1837
	else
Barry Warsaw's avatar
Barry Warsaw committed
1838
		return Py_BuildValue("ii", pid, sts);
1839
}
Guido van Rossum's avatar
Guido van Rossum committed
1840
#endif
1841

1842 1843 1844 1845 1846

static char posix_lstat__doc__[] =
"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
Like stat(path), but do not follow symbolic links.";

Barry Warsaw's avatar
Barry Warsaw committed
1847
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1848
posix_lstat(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1849 1850
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1851
{
1852
#ifdef HAVE_LSTAT
Guido van Rossum's avatar
Guido van Rossum committed
1853
	return posix_do_stat(self, args, lstat);
1854 1855 1856
#else /* !HAVE_LSTAT */
	return posix_do_stat(self, args, stat);
#endif /* !HAVE_LSTAT */
Guido van Rossum's avatar
Guido van Rossum committed
1857 1858
}

1859

1860
#ifdef HAVE_READLINK
1861 1862 1863 1864
static char posix_readlink__doc__[] =
"readlink(path) -> path\n\
Return a string representing the path to which the symbolic link points.";

Barry Warsaw's avatar
Barry Warsaw committed
1865
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1866
posix_readlink(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1867 1868
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1869
{
1870
	char buf[MAXPATHLEN];
Guido van Rossum's avatar
Guido van Rossum committed
1871
	char *path;
Guido van Rossum's avatar
Guido van Rossum committed
1872
	int n;
Barry Warsaw's avatar
Barry Warsaw committed
1873
	if (!PyArg_Parse(args, "s", &path))
Guido van Rossum's avatar
Guido van Rossum committed
1874
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
1875
	Py_BEGIN_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1876
	n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw's avatar
Barry Warsaw committed
1877
	Py_END_ALLOW_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
1878
	if (n < 0)
1879
		return posix_error_with_filename(path);
Barry Warsaw's avatar
Barry Warsaw committed
1880
	return PyString_FromStringAndSize(buf, n);
Guido van Rossum's avatar
Guido van Rossum committed
1881
}
1882
#endif /* HAVE_READLINK */
Guido van Rossum's avatar
Guido van Rossum committed
1883

1884

1885
#ifdef HAVE_SYMLINK
1886 1887 1888 1889
static char posix_symlink__doc__[] =
"symlink(src, dst) -> None\n\
Create a symbolic link.";

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
static PyObject *
posix_symlink(self, args)
	PyObject *self;
	PyObject *args;
{
	return posix_2str(args, symlink);
}
#endif /* HAVE_SYMLINK */


#ifdef HAVE_TIMES
#ifndef HZ
#define HZ 60 /* Universal constant :-) */
#endif /* HZ */
	
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
#if defined(PYCC_VACPP) && defined(PYOS_OS2)
static long
system_uptime()
{
    ULONG     value = 0;

    Py_BEGIN_ALLOW_THREADS
    DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
    Py_END_ALLOW_THREADS

    return value;
}

static PyObject *
posix_times(self, args)
	PyObject *self;
	PyObject *args;
{
	if (!PyArg_NoArgs(args))
		return NULL;

    /* Currently Only Uptime is Provided -- Others Later */
	return Py_BuildValue("ddddd",
			     (double)0 /* t.tms_utime / HZ */,
			     (double)0 /* t.tms_stime / HZ */,
			     (double)0 /* t.tms_cutime / HZ */,
			     (double)0 /* t.tms_cstime / HZ */,
			     (double)system_uptime() / 1000);
}
1934
#else /* not OS2 */
Barry Warsaw's avatar
Barry Warsaw committed
1935
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
1936
posix_times(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1937 1938
	PyObject *self;
	PyObject *args;
Guido van Rossum's avatar
Guido van Rossum committed
1939 1940 1941
{
	struct tms t;
	clock_t c;
Barry Warsaw's avatar
Barry Warsaw committed
1942
	if (!PyArg_NoArgs(args))
Guido van Rossum's avatar
Guido van Rossum committed
1943 1944 1945
		return NULL;
	errno = 0;
	c = times(&t);
1946 1947
	if (c == (clock_t) -1)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
1948
	return Py_BuildValue("ddddd",
Barry Warsaw's avatar
Barry Warsaw committed
1949 1950 1951 1952 1953
			     (double)t.tms_utime / HZ,
			     (double)t.tms_stime / HZ,
			     (double)t.tms_cutime / HZ,
			     (double)t.tms_cstime / HZ,
			     (double)c / HZ);
Guido van Rossum's avatar
Guido van Rossum committed
1954
}
1955
#endif /* not OS2 */
1956
#endif /* HAVE_TIMES */
1957 1958


1959
#ifdef MS_WIN32
1960
#define HAVE_TIMES	/* so the method table will pick it up */
Barry Warsaw's avatar
Barry Warsaw committed
1961
static PyObject *
1962
posix_times(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1963 1964
	PyObject *self;
	PyObject *args;
1965 1966 1967
{
	FILETIME create, exit, kernel, user;
	HANDLE hProc;
Barry Warsaw's avatar
Barry Warsaw committed
1968
	if (!PyArg_NoArgs(args))
1969 1970 1971
		return NULL;
	hProc = GetCurrentProcess();
	GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw's avatar
Barry Warsaw committed
1972 1973 1974 1975 1976 1977 1978
	return Py_BuildValue(
		"ddddd",
		(double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
		(double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
		(double)0,
		(double)0,
		(double)0);
1979
}
1980
#endif /* MS_WIN32 */
1981 1982

#ifdef HAVE_TIMES
1983 1984 1985
static char posix_times__doc__[] =
"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
Return a tuple of floating point numbers indicating process times.";
1986
#endif
Guido van Rossum's avatar
Guido van Rossum committed
1987

1988

1989
#ifdef HAVE_SETSID
1990 1991 1992 1993
static char posix_setsid__doc__[] =
"setsid() -> None\n\
Call the system call setsid().";

Barry Warsaw's avatar
Barry Warsaw committed
1994
static PyObject *
1995
posix_setsid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
1996 1997
	PyObject *self;
	PyObject *args;
1998
{
Barry Warsaw's avatar
Barry Warsaw committed
1999
	if (!PyArg_NoArgs(args))
2000
		return NULL;
2001 2002
	if (setsid() < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2003 2004
	Py_INCREF(Py_None);
	return Py_None;
2005
}
2006
#endif /* HAVE_SETSID */
2007

2008
#ifdef HAVE_SETPGID
2009 2010 2011 2012
static char posix_setpgid__doc__[] =
"setpgid(pid, pgrp) -> None\n\
Call the system call setpgid().";

Barry Warsaw's avatar
Barry Warsaw committed
2013
static PyObject *
2014
posix_setpgid(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2015 2016
	PyObject *self;
	PyObject *args;
2017 2018
{
	int pid, pgrp;
Barry Warsaw's avatar
Barry Warsaw committed
2019
	if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
2020
		return NULL;
2021 2022
	if (setpgid(pid, pgrp) < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2023 2024
	Py_INCREF(Py_None);
	return Py_None;
2025
}
2026
#endif /* HAVE_SETPGID */
2027

2028

2029
#ifdef HAVE_TCGETPGRP
2030 2031 2032 2033
static char posix_tcgetpgrp__doc__[] =
"tcgetpgrp(fd) -> pgid\n\
Return the process group associated with the terminal given by a fd.";

Barry Warsaw's avatar
Barry Warsaw committed
2034
static PyObject *
2035
posix_tcgetpgrp(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2036 2037
	PyObject *self;
	PyObject *args;
2038 2039
{
	int fd, pgid;
Barry Warsaw's avatar
Barry Warsaw committed
2040
	if (!PyArg_Parse(args, "i", &fd))
2041 2042
		return NULL;
	pgid = tcgetpgrp(fd);
2043 2044
	if (pgid < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2045
	return PyInt_FromLong((long)pgid);
2046
}
2047
#endif /* HAVE_TCGETPGRP */
2048

2049

2050
#ifdef HAVE_TCSETPGRP
2051 2052 2053 2054
static char posix_tcsetpgrp__doc__[] =
"tcsetpgrp(fd, pgid) -> None\n\
Set the process group associated with the terminal given by a fd.";

Barry Warsaw's avatar
Barry Warsaw committed
2055
static PyObject *
2056
posix_tcsetpgrp(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2057 2058
	PyObject *self;
	PyObject *args;
2059 2060
{
	int fd, pgid;
Barry Warsaw's avatar
Barry Warsaw committed
2061
	if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
2062
		return NULL;
2063 2064
	if (tcsetpgrp(fd, pgid) < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2065
	Py_INCREF(Py_None);
Barry Warsaw's avatar
Barry Warsaw committed
2066
	return Py_None;
2067
}
2068
#endif /* HAVE_TCSETPGRP */
Guido van Rossum's avatar
Guido van Rossum committed
2069

2070 2071
/* Functions acting on file descriptors */

2072 2073 2074 2075
static char posix_open__doc__[] =
"open(filename, flag [, mode=0777]) -> fd\n\
Open a file (for low level IO).";

Barry Warsaw's avatar
Barry Warsaw committed
2076
static PyObject *
2077
posix_open(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2078 2079
	PyObject *self;
	PyObject *args;
2080 2081 2082 2083 2084
{
	char *file;
	int flag;
	int mode = 0777;
	int fd;
Barry Warsaw's avatar
Barry Warsaw committed
2085 2086 2087
	if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
		return NULL;

Barry Warsaw's avatar
Barry Warsaw committed
2088
	Py_BEGIN_ALLOW_THREADS
2089
	fd = open(file, flag, mode);
Barry Warsaw's avatar
Barry Warsaw committed
2090
	Py_END_ALLOW_THREADS
2091
	if (fd < 0)
2092
		return posix_error_with_filename(file);
Barry Warsaw's avatar
Barry Warsaw committed
2093
	return PyInt_FromLong((long)fd);
2094 2095
}

2096 2097 2098 2099 2100

static char posix_close__doc__[] =
"close(fd) -> None\n\
Close a file descriptor (for low level IO).";

Barry Warsaw's avatar
Barry Warsaw committed
2101
static PyObject *
2102
posix_close(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2103 2104
	PyObject *self;
	PyObject *args;
2105 2106
{
	int fd, res;
Barry Warsaw's avatar
Barry Warsaw committed
2107
	if (!PyArg_Parse(args, "i", &fd))
2108
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2109
	Py_BEGIN_ALLOW_THREADS
2110
	res = close(fd);
Barry Warsaw's avatar
Barry Warsaw committed
2111
	Py_END_ALLOW_THREADS
2112 2113
	if (res < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2114 2115
	Py_INCREF(Py_None);
	return Py_None;
2116 2117
}

2118 2119 2120 2121 2122

static char posix_dup__doc__[] =
"dup(fd) -> fd2\n\
Return a duplicate of a file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2123
static PyObject *
2124
posix_dup(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2125 2126
	PyObject *self;
	PyObject *args;
2127 2128
{
	int fd;
Barry Warsaw's avatar
Barry Warsaw committed
2129
	if (!PyArg_Parse(args, "i", &fd))
2130
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2131
	Py_BEGIN_ALLOW_THREADS
2132
	fd = dup(fd);
Barry Warsaw's avatar
Barry Warsaw committed
2133
	Py_END_ALLOW_THREADS
2134 2135
	if (fd < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2136
	return PyInt_FromLong((long)fd);
2137 2138
}

2139 2140 2141 2142 2143

static char posix_dup2__doc__[] =
"dup2(fd, fd2) -> None\n\
Duplicate file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2144
static PyObject *
2145
posix_dup2(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2146 2147
	PyObject *self;
	PyObject *args;
2148 2149
{
	int fd, fd2, res;
Barry Warsaw's avatar
Barry Warsaw committed
2150
	if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
2151
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2152
	Py_BEGIN_ALLOW_THREADS
2153
	res = dup2(fd, fd2);
Barry Warsaw's avatar
Barry Warsaw committed
2154
	Py_END_ALLOW_THREADS
2155 2156
	if (res < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2157 2158
	Py_INCREF(Py_None);
	return Py_None;
2159 2160
}

2161 2162 2163 2164 2165

static char posix_lseek__doc__[] =
"lseek(fd, pos, how) -> newpos\n\
Set the current position of a file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2166
static PyObject *
2167
posix_lseek(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2168 2169
	PyObject *self;
	PyObject *args;
2170 2171
{
	int fd, how;
2172 2173 2174
	off_t pos, res;
	PyObject *posobj;
	if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
2175 2176 2177 2178 2179 2180 2181 2182
		return NULL;
#ifdef SEEK_SET
	/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
	switch (how) {
	case 0: how = SEEK_SET; break;
	case 1: how = SEEK_CUR; break;
	case 2: how = SEEK_END; break;
	}
2183
#endif /* SEEK_END */
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193

#if !defined(HAVE_LARGEFILE_SUPPORT)
	pos = PyInt_AsLong(posobj);
#else
	pos = PyLong_Check(posobj) ?
		PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
#endif
	if (PyErr_Occurred())
		return NULL;

Barry Warsaw's avatar
Barry Warsaw committed
2194
	Py_BEGIN_ALLOW_THREADS
2195
	res = lseek(fd, pos, how);
Barry Warsaw's avatar
Barry Warsaw committed
2196
	Py_END_ALLOW_THREADS
2197 2198
	if (res < 0)
		return posix_error();
2199 2200

#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw's avatar
Barry Warsaw committed
2201
	return PyInt_FromLong(res);
2202 2203 2204
#else
	return PyLong_FromLongLong(res);
#endif
2205 2206
}

2207 2208 2209 2210 2211

static char posix_read__doc__[] =
"read(fd, buffersize) -> string\n\
Read a file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2212
static PyObject *
2213
posix_read(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2214 2215
	PyObject *self;
	PyObject *args;
2216
{
2217
	int fd, size, n;
Barry Warsaw's avatar
Barry Warsaw committed
2218 2219
	PyObject *buffer;
	if (!PyArg_Parse(args, "(ii)", &fd, &size))
2220
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2221
	buffer = PyString_FromStringAndSize((char *)NULL, size);
2222 2223
	if (buffer == NULL)
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2224 2225 2226
	Py_BEGIN_ALLOW_THREADS
	n = read(fd, PyString_AsString(buffer), size);
	Py_END_ALLOW_THREADS
2227
	if (n < 0) {
Barry Warsaw's avatar
Barry Warsaw committed
2228
		Py_DECREF(buffer);
2229 2230
		return posix_error();
	}
2231
	if (n != size)
Barry Warsaw's avatar
Barry Warsaw committed
2232
		_PyString_Resize(&buffer, n);
2233 2234 2235
	return buffer;
}

2236 2237 2238 2239 2240

static char posix_write__doc__[] =
"write(fd, string) -> byteswritten\n\
Write a string to a file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2241
static PyObject *
2242
posix_write(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2243 2244
	PyObject *self;
	PyObject *args;
2245 2246 2247
{
	int fd, size;
	char *buffer;
Barry Warsaw's avatar
Barry Warsaw committed
2248
	if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
2249
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2250
	Py_BEGIN_ALLOW_THREADS
2251
	size = write(fd, buffer, size);
Barry Warsaw's avatar
Barry Warsaw committed
2252
	Py_END_ALLOW_THREADS
2253 2254
	if (size < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2255
	return PyInt_FromLong((long)size);
2256 2257
}

2258 2259 2260 2261 2262

static char posix_fstat__doc__[]=
"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Like stat(), but for an open file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2263
static PyObject *
2264
posix_fstat(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2265 2266
	PyObject *self;
	PyObject *args;
2267 2268 2269 2270
{
	int fd;
	struct stat st;
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
2271
	if (!PyArg_Parse(args, "i", &fd))
2272
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2273
	Py_BEGIN_ALLOW_THREADS
2274
	res = fstat(fd, &st);
Barry Warsaw's avatar
Barry Warsaw committed
2275
	Py_END_ALLOW_THREADS
2276 2277
	if (res != 0)
		return posix_error();
2278
#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw's avatar
Barry Warsaw committed
2279
	return Py_BuildValue("(llllllllll)",
Barry Warsaw's avatar
Barry Warsaw committed
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
			     (long)st.st_mode,
			     (long)st.st_ino,
			     (long)st.st_dev,
			     (long)st.st_nlink,
			     (long)st.st_uid,
			     (long)st.st_gid,
			     (long)st.st_size,
			     (long)st.st_atime,
			     (long)st.st_mtime,
			     (long)st.st_ctime);
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
#else
	return Py_BuildValue("(lLllllLlll)",
			     (long)st.st_mode,
			     (LONG_LONG)st.st_ino,
			     (long)st.st_dev,
			     (long)st.st_nlink,
			     (long)st.st_uid,
			     (long)st.st_gid,
			     (LONG_LONG)st.st_size,
			     (long)st.st_atime,
			     (long)st.st_mtime,
			     (long)st.st_ctime);
#endif
2303 2304
}

2305 2306 2307 2308 2309

static char posix_fdopen__doc__[] =
"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
Return an open file object connected to a file descriptor.";

Barry Warsaw's avatar
Barry Warsaw committed
2310
static PyObject *
2311
posix_fdopen(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2312 2313
	PyObject *self;
	PyObject *args;
2314
{
Barry Warsaw's avatar
Barry Warsaw committed
2315
	extern int fclose Py_PROTO((FILE *));
2316
	int fd;
2317 2318
	char *mode = "r";
	int bufsize = -1;
2319
	FILE *fp;
Barry Warsaw's avatar
Barry Warsaw committed
2320 2321
	PyObject *f;
	if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
2322
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2323

Barry Warsaw's avatar
Barry Warsaw committed
2324
	Py_BEGIN_ALLOW_THREADS
2325
	fp = fdopen(fd, mode);
Barry Warsaw's avatar
Barry Warsaw committed
2326
	Py_END_ALLOW_THREADS
2327 2328
	if (fp == NULL)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2329
	f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
2330
	if (f != NULL)
Barry Warsaw's avatar
Barry Warsaw committed
2331
		PyFile_SetBufSize(f, bufsize);
2332
	return f;
2333 2334
}

2335

2336
#ifdef HAVE_PIPE
2337 2338 2339 2340
static char posix_pipe__doc__[] =
"pipe() -> (read_end, write_end)\n\
Create a pipe.";

Barry Warsaw's avatar
Barry Warsaw committed
2341
static PyObject *
2342
posix_pipe(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2343 2344
	PyObject *self;
	PyObject *args;
2345
{
Guido van Rossum's avatar
Guido van Rossum committed
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
#if defined(PYOS_OS2)
    HFILE read, write;
    APIRET rc;

    if (!PyArg_Parse(args, ""))
        return NULL;

	Py_BEGIN_ALLOW_THREADS
    rc = DosCreatePipe( &read, &write, 4096);
	Py_END_ALLOW_THREADS
    if (rc != NO_ERROR)
2357
        return os2_error(rc);
Guido van Rossum's avatar
Guido van Rossum committed
2358 2359 2360

    return Py_BuildValue("(ii)", read, write);
#else
2361
#if !defined(MS_WIN32)
2362 2363
	int fds[2];
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
2364
	if (!PyArg_Parse(args, ""))
2365
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2366
	Py_BEGIN_ALLOW_THREADS
2367
	res = pipe(fds);
Barry Warsaw's avatar
Barry Warsaw committed
2368
	Py_END_ALLOW_THREADS
2369 2370
	if (res != 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2371
	return Py_BuildValue("(ii)", fds[0], fds[1]);
2372
#else /* MS_WIN32 */
2373
	HANDLE read, write;
2374
	int read_fd, write_fd;
2375
	BOOL ok;
Barry Warsaw's avatar
Barry Warsaw committed
2376
	if (!PyArg_Parse(args, ""))
2377
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2378
	Py_BEGIN_ALLOW_THREADS
2379
	ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw's avatar
Barry Warsaw committed
2380
	Py_END_ALLOW_THREADS
2381 2382
	if (!ok)
		return posix_error();
2383 2384 2385
	read_fd = _open_osfhandle((long)read, 0);
	write_fd = _open_osfhandle((long)write, 1);
	return Py_BuildValue("(ii)", read_fd, write_fd);
2386
#endif /* MS_WIN32 */
Guido van Rossum's avatar
Guido van Rossum committed
2387
#endif
2388
}
2389 2390
#endif  /* HAVE_PIPE */

2391

2392
#ifdef HAVE_MKFIFO
2393 2394 2395 2396
static char posix_mkfifo__doc__[] =
"mkfifo(file, [, mode=0666]) -> None\n\
Create a FIFO (a POSIX named pipe).";

Barry Warsaw's avatar
Barry Warsaw committed
2397
static PyObject *
2398
posix_mkfifo(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2399 2400
	PyObject *self;
	PyObject *args;
2401 2402 2403 2404
{
	char *file;
	int mode = 0666;
	int res;
Barry Warsaw's avatar
Barry Warsaw committed
2405
	if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
2406
		return NULL;
Barry Warsaw's avatar
Barry Warsaw committed
2407
	Py_BEGIN_ALLOW_THREADS
2408
	res = mkfifo(file, mode);
Barry Warsaw's avatar
Barry Warsaw committed
2409
	Py_END_ALLOW_THREADS
2410 2411
	if (res < 0)
		return posix_error();
Barry Warsaw's avatar
Barry Warsaw committed
2412 2413
	Py_INCREF(Py_None);
	return Py_None;
2414 2415 2416
}
#endif

2417

2418
#ifdef HAVE_FTRUNCATE
2419 2420 2421 2422
static char posix_ftruncate__doc__[] =
"ftruncate(fd, length) -> None\n\
Truncate a file to a specified length.";

Barry Warsaw's avatar
Barry Warsaw committed
2423
static PyObject *
2424
posix_ftruncate(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2425 2426
	PyObject *self; /* Not used */
	PyObject *args;
2427 2428
{
	int fd;
2429
	off_t length;
2430
	int res;
2431
	PyObject *lenobj;
2432

2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
	if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
		return NULL;

#if !defined(HAVE_LARGEFILE_SUPPORT)
	length = PyInt_AsLong(lenobj);
#else
	length = PyLong_Check(lenobj) ?
		PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
#endif
	if (PyErr_Occurred())
2443 2444
		return NULL;

Barry Warsaw's avatar
Barry Warsaw committed
2445
	Py_BEGIN_ALLOW_THREADS
2446
	res = ftruncate(fd, length);
Barry Warsaw's avatar
Barry Warsaw committed
2447
	Py_END_ALLOW_THREADS
2448
	if (res < 0) {
Barry Warsaw's avatar
Barry Warsaw committed
2449
		PyErr_SetFromErrno(PyExc_IOError);
2450 2451
		return NULL;
	}
Barry Warsaw's avatar
Barry Warsaw committed
2452 2453
	Py_INCREF(Py_None);
	return Py_None;
2454 2455
}
#endif
Guido van Rossum's avatar
Guido van Rossum committed
2456

2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
#ifdef NeXT
#define HAVE_PUTENV
/* Steve Spicklemire got this putenv from NeXTAnswers */
static int
putenv(char *newval)
{
	extern char **environ;

	static int firstTime = 1;
	char **ep;
	char *cp;
	int esiz;
	char *np;

	if (!(np = strchr(newval, '=')))
		return 1;
	*np = '\0';

	/* look it up */
	for (ep=environ ; *ep ; ep++)
	{
		/* this should always be true... */
		if (cp = strchr(*ep, '='))
		{
			*cp = '\0';
			if (!strcmp(*ep, newval))
			{
				/* got it! */
				*cp = '=';
				break;
			}
			*cp = '=';
		}
		else
		{
			*np = '=';
			return 1;
		}
	}

	*np = '=';
	if (*ep)
	{
		/* the string was already there:
		   just replace it with the new one */
		*ep = newval;
		return 0;
	}

	/* expand environ by one */
	for (esiz=2, ep=environ ; *ep ; ep++)
		esiz++;
	if (firstTime)
	{
		char **epp;
		char **newenv;
		if (!(newenv = malloc(esiz * sizeof(char *))))
			return 1;
   
		for (ep=environ, epp=newenv ; *ep ;)
			*epp++ = *ep++;
		*epp++ = newval;
		*epp = (char *) 0;
		environ = newenv;
	}
	else
	{
		if (!(environ = realloc(environ, esiz * sizeof(char *))))
			return 1;
		environ[esiz - 2] = newval;
		environ[esiz - 1] = (char *) 0;
		firstTime = 0;
	}

	return 0;
}
Guido van Rossum's avatar
Guido van Rossum committed
2533
#endif /* NeXT */
2534

2535

2536
#ifdef HAVE_PUTENV
2537 2538 2539 2540
static char posix_putenv__doc__[] =
"putenv(key, value) -> None\n\
Change or add an environment variable.";

2541 2542 2543 2544 2545
#ifdef __BEOS__
/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
int putenv( const char *str );
#endif

Barry Warsaw's avatar
Barry Warsaw committed
2546
static PyObject * 
Guido van Rossum's avatar
Guido van Rossum committed
2547
posix_putenv(self, args)
Barry Warsaw's avatar
Barry Warsaw committed
2548 2549
	PyObject *self;
	PyObject *args;
2550 2551 2552 2553
{
        char *s1, *s2;
        char *new;

Barry Warsaw's avatar
Barry Warsaw committed
2554
	if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
2555
		return NULL;
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579

#if defined(PYOS_OS2)
    if (stricmp(s1, "BEGINLIBPATH") == 0) {
        APIRET rc;

        if (strlen(s2) == 0)  /* If New Value is an Empty String */
            s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */

        rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
        if (rc != NO_ERROR)
            return os2_error(rc);

    } else if (stricmp(s1, "ENDLIBPATH") == 0) {
        APIRET rc;

        if (strlen(s2) == 0)  /* If New Value is an Empty String */
            s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */

        rc = DosSetExtLIBPATH(s2, END_LIBPATH);
        if (rc != NO_ERROR)
            return os2_error(rc);
    } else {
#endif

2580 2581
	/* XXX This leaks memory -- not easy to fix :-( */
	if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw's avatar
Barry Warsaw committed
2582
                return PyErr_NoMemory();
2583 2584 2585 2586 2587
	(void) sprintf(new, "%s=%s", s1, s2);
	if (putenv(new)) {
                posix_error();
                return NULL;
	}
2588 2589 2590 2591

#if defined(PYOS_OS2)
    }
#endif
Barry Warsaw's avatar
Barry Warsaw committed
2592 2593
	Py_INCREF(Py_None);
        return Py_None;
2594
}
Guido van Rossum's avatar
Guido van Rossum committed
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
#endif /* putenv */

#ifdef HAVE_STRERROR
static char posix_strerror__doc__[] =
"strerror(code) -> string\n\
Translate an error code to a message string.";

PyObject *
posix_strerror(self, args)
	PyObject *self;
	PyObject *args;
{
	int code;
	char *message;
	if (!PyArg_ParseTuple(args, "i", &code))
		return NULL;
	message = strerror(code);
	if (message == NULL) {
		PyErr_SetString(PyExc_ValueError,
				"strerror code out of range");
		return NULL;
	}
	return PyString_FromString(message);
}
#endif /* strerror */

2621

2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
#ifdef HAVE_SYS_WAIT_H

#ifdef WIFSTOPPED
static char posix_WIFSTOPPED__doc__[] =
"WIFSTOPPED(status) -> Boolean\n\
See Unix documentation.";

static PyObject *
posix_WIFSTOPPED(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WIFSTOPPED(status));
}
#endif /* WIFSTOPPED */

#ifdef WIFSIGNALED
static char posix_WIFSIGNALED__doc__[] =
"WIFSIGNALED(status) -> Boolean\n\
See Unix documentation.";

static PyObject *
posix_WIFSIGNALED(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WIFSIGNALED(status));
}
#endif /* WIFSIGNALED */

#ifdef WIFEXITED
static char posix_WIFEXITED__doc__[] =
"WIFEXITED(status) -> Boolean\n\
See Unix documentation.";

static PyObject *
posix_WIFEXITED(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WIFEXITED(status));
}
#endif /* WIFEXITED */

#ifdef WIFSTOPPED
static char posix_WEXITSTATUS__doc__[] =
"WEXITSTATUS(status) -> integer\n\
See Unix documentation.";

static PyObject *
posix_WEXITSTATUS(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WEXITSTATUS(status));
}
#endif /* WEXITSTATUS */

#ifdef WTERMSIG
static char posix_WTERMSIG__doc__[] =
"WTERMSIG(status) -> integer\n\
See Unix documentation.";

static PyObject *
posix_WTERMSIG(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WTERMSIG(status));
}
#endif /* WTERMSIG */

#ifdef WSTOPSIG
static char posix_WSTOPSIG__doc__[] =
"WSTOPSIG(status) -> integer\n\
See Unix documentation.";

static PyObject *
posix_WSTOPSIG(self, args)
	PyObject *self;
	PyObject *args;
{
	int status = 0;
   
	if (!PyArg_Parse(args, "i", &status))
	{
		return NULL;
	}
   
	return Py_BuildValue("i", WSTOPSIG(status));
}
#endif /* WSTOPSIG */

#endif /* HAVE_SYS_WAIT_H */


2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
#if defined(HAVE_FSTATVFS)
#include <sys/statvfs.h>

static char posix_fstatvfs__doc__[] =
"fstatvfs(fd) -> \
(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
Perform an fstatvfs system call on the given fd.";

static PyObject *
posix_fstatvfs(self, args)
	PyObject *self;
	PyObject *args;
{
	int fd, res;
	struct statvfs st;
	if (!PyArg_ParseTuple(args, "i", &fd))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = fstatvfs(fd, &st);
	Py_END_ALLOW_THREADS
	if (res != 0)
		return posix_error();
#if !defined(HAVE_LARGEFILE_SUPPORT)
	return Py_BuildValue("(lllllllllll)",
		    (long) st.f_bsize,
		    (long) st.f_frsize,
		    (long) st.f_blocks,
		    (long) st.f_bfree,
		    (long) st.f_bavail,
		    (long) st.f_files,
		    (long) st.f_ffree,
		    (long) st.f_favail,
		    (long) st.f_fsid,
		    (long) st.f_flag,
		    (long) st.f_namemax);
#else
	return Py_BuildValue("(llLLLLLLlll)",
		    (long) st.f_bsize,
		    (long) st.f_frsize,
		    (LONG_LONG) st.f_blocks,
		    (LONG_LONG) st.f_bfree,
		    (LONG_LONG) st.f_bavail,
		    (LONG_LONG) st.f_files,
		    (LONG_LONG) st.f_ffree,
		    (LONG_LONG) st.f_favail,
		    (long) st.f_fsid,
		    (long) st.f_flag,
		    (long) st.f_namemax);
#endif
}
#endif /* HAVE_FSTATVFS */


#if defined(HAVE_STATVFS)
#include <sys/statvfs.h>

static char posix_statvfs__doc__[] =
"statvfs(path) -> \
(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
Perform a statvfs system call on the given path.";

static PyObject *
posix_statvfs(self, args)
	PyObject *self;
	PyObject *args;
{
	char *path;
	int res;
	struct statvfs st;
	if (!PyArg_ParseTuple(args, "s", &path))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = statvfs(path, &st);
	Py_END_ALLOW_THREADS
	if (res != 0)
		return posix_error_with_filename(path);
#if !defined(HAVE_LARGEFILE_SUPPORT)
	return Py_BuildValue("(lllllllllll)",
		    (long) st.f_bsize,
		    (long) st.f_frsize,
		    (long) st.f_blocks,
		    (long) st.f_bfree,
		    (long) st.f_bavail,
		    (long) st.f_files,
		    (long) st.f_ffree,
		    (long) st.f_favail,
		    (long) st.f_fsid,
		    (long) st.f_flag,
		    (long) st.f_namemax);
#else	/* HAVE_LARGEFILE_SUPPORT */
	return Py_BuildValue("(llLLLLLLlll)",
		    (long) st.f_bsize,
		    (long) st.f_frsize,
		    (LONG_LONG) st.f_blocks,
		    (LONG_LONG) st.f_bfree,
		    (LONG_LONG) st.f_bavail,
		    (LONG_LONG) st.f_files,
		    (LONG_LONG) st.f_ffree,
		    (LONG_LONG) st.f_favail,
		    (long) st.f_fsid,
		    (long) st.f_flag,
		    (long) st.f_namemax);
#endif
}
#endif /* HAVE_STATVFS */


Barry Warsaw's avatar
Barry Warsaw committed
2860
static PyMethodDef posix_methods[] = {
2861 2862
	{"access",	posix_access, 0, posix_access__doc__},
	{"ttyname",	posix_ttyname, 0, posix_ttyname__doc__},
2863 2864
	{"chdir",	posix_chdir, 0, posix_chdir__doc__},
	{"chmod",	posix_chmod, 0, posix_chmod__doc__},
2865
#ifdef HAVE_CHOWN
2866
	{"chown",	posix_chown, 0, posix_chown__doc__},
2867
#endif /* HAVE_CHOWN */
2868
#ifdef HAVE_GETCWD
2869
	{"getcwd",	posix_getcwd, 0, posix_getcwd__doc__},
2870
#endif
2871
#ifdef HAVE_LINK
2872
	{"link",	posix_link, 0, posix_link__doc__},
2873
#endif /* HAVE_LINK */
2874 2875 2876
	{"listdir",	posix_listdir, 0, posix_listdir__doc__},
	{"lstat",	posix_lstat, 0, posix_lstat__doc__},
	{"mkdir",	posix_mkdir, 1, posix_mkdir__doc__},
2877
#ifdef HAVE_NICE
2878
	{"nice",	posix_nice, 0, posix_nice__doc__},
2879
#endif /* HAVE_NICE */
2880
#ifdef HAVE_READLINK
2881
	{"readlink",	posix_readlink, 0, posix_readlink__doc__},
2882
#endif /* HAVE_READLINK */
2883 2884 2885
	{"rename",	posix_rename, 0, posix_rename__doc__},
	{"rmdir",	posix_rmdir, 0, posix_rmdir__doc__},
	{"stat",	posix_stat, 0, posix_stat__doc__},
2886
#ifdef HAVE_SYMLINK
2887
	{"symlink",	posix_symlink, 0, posix_symlink__doc__},
2888
#endif /* HAVE_SYMLINK */
2889
#ifdef HAVE_SYSTEM
2890
	{"system",	posix_system, 0, posix_system__doc__},
2891
#endif
2892
	{"umask",	posix_umask, 0, posix_umask__doc__},
2893
#ifdef HAVE_UNAME
2894
	{"uname",	posix_uname, 0, posix_uname__doc__},
2895
#endif /* HAVE_UNAME */
2896 2897 2898
	{"unlink",	posix_unlink, 0, posix_unlink__doc__},
	{"remove",	posix_unlink, 0, posix_remove__doc__},
	{"utime",	posix_utime, 0, posix_utime__doc__},
2899
#ifdef HAVE_TIMES
2900
	{"times",	posix_times, 0, posix_times__doc__},
2901
#endif /* HAVE_TIMES */
2902
	{"_exit",	posix__exit, 0, posix__exit__doc__},
2903
#ifdef HAVE_EXECV
2904 2905
	{"execv",	posix_execv, 0, posix_execv__doc__},
	{"execve",	posix_execve, 0, posix_execve__doc__},
2906
#endif /* HAVE_EXECV */
Guido van Rossum's avatar
Guido van Rossum committed
2907
#ifdef HAVE_FORK
2908
	{"fork",	posix_fork, 0, posix_fork__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2909 2910
#endif /* HAVE_FORK */
#ifdef HAVE_GETEGID
2911
	{"getegid",	posix_getegid, 0, posix_getegid__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2912 2913
#endif /* HAVE_GETEGID */
#ifdef HAVE_GETEUID
2914
	{"geteuid",	posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2915 2916
#endif /* HAVE_GETEUID */
#ifdef HAVE_GETGID
2917
	{"getgid",	posix_getgid, 0, posix_getgid__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2918
#endif /* HAVE_GETGID */
2919
	{"getpid",	posix_getpid, 0, posix_getpid__doc__},
2920
#ifdef HAVE_GETPGRP
2921
	{"getpgrp",	posix_getpgrp, 0, posix_getpgrp__doc__},
2922
#endif /* HAVE_GETPGRP */
Guido van Rossum's avatar
Guido van Rossum committed
2923
#ifdef HAVE_GETPPID
2924
	{"getppid",	posix_getppid, 0, posix_getppid__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2925 2926
#endif /* HAVE_GETPPID */
#ifdef HAVE_GETUID
2927
	{"getuid",	posix_getuid, 0, posix_getuid__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2928 2929
#endif /* HAVE_GETUID */
#ifdef HAVE_KILL
2930
	{"kill",	posix_kill, 0, posix_kill__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2931
#endif /* HAVE_KILL */
Guido van Rossum's avatar
Guido van Rossum committed
2932
#ifdef HAVE_PLOCK
2933
	{"plock",	posix_plock, 0, posix_plock__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2934
#endif /* HAVE_PLOCK */
2935
#ifdef HAVE_POPEN
2936
	{"popen",	posix_popen, 1, posix_popen__doc__},
2937
#endif /* HAVE_POPEN */
2938
#ifdef HAVE_SETUID
2939
	{"setuid",	posix_setuid, 0, posix_setuid__doc__},
2940
#endif /* HAVE_SETUID */
2941
#ifdef HAVE_SETGID
2942
	{"setgid",	posix_setgid, 0, posix_setgid__doc__},
2943
#endif /* HAVE_SETGID */
2944
#ifdef HAVE_SETPGRP
2945
	{"setpgrp",	posix_setpgrp, 0, posix_setpgrp__doc__},
2946
#endif /* HAVE_SETPGRP */
Guido van Rossum's avatar
Guido van Rossum committed
2947
#ifdef HAVE_WAIT
2948
	{"wait",	posix_wait, 0, posix_wait__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2949
#endif /* HAVE_WAIT */
2950
#ifdef HAVE_WAITPID
2951
	{"waitpid",	posix_waitpid, 0, posix_waitpid__doc__},
2952
#endif /* HAVE_WAITPID */
2953
#ifdef HAVE_SETSID
2954
	{"setsid",	posix_setsid, 0, posix_setsid__doc__},
2955
#endif /* HAVE_SETSID */
2956
#ifdef HAVE_SETPGID
2957
	{"setpgid",	posix_setpgid, 0, posix_setpgid__doc__},
2958
#endif /* HAVE_SETPGID */
2959
#ifdef HAVE_TCGETPGRP
2960
	{"tcgetpgrp",	posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
2961
#endif /* HAVE_TCGETPGRP */
2962
#ifdef HAVE_TCSETPGRP
2963
	{"tcsetpgrp",	posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
2964
#endif /* HAVE_TCSETPGRP */
2965 2966 2967 2968 2969 2970 2971 2972 2973
	{"open",	posix_open, 1, posix_open__doc__},
	{"close",	posix_close, 0, posix_close__doc__},
	{"dup",		posix_dup, 0, posix_dup__doc__},
	{"dup2",	posix_dup2, 0, posix_dup2__doc__},
	{"lseek",	posix_lseek, 0, posix_lseek__doc__},
	{"read",	posix_read, 0, posix_read__doc__},
	{"write",	posix_write, 0, posix_write__doc__},
	{"fstat",	posix_fstat, 0, posix_fstat__doc__},
	{"fdopen",	posix_fdopen,	1, posix_fdopen__doc__},
2974
#ifdef HAVE_PIPE
2975
	{"pipe",	posix_pipe, 0, posix_pipe__doc__},
2976 2977
#endif
#ifdef HAVE_MKFIFO
2978
	{"mkfifo",	posix_mkfifo, 1, posix_mkfifo__doc__},
2979 2980
#endif
#ifdef HAVE_FTRUNCATE
2981
	{"ftruncate",	posix_ftruncate, 1, posix_ftruncate__doc__},
2982 2983
#endif
#ifdef HAVE_PUTENV
2984
	{"putenv",	posix_putenv, 1, posix_putenv__doc__},
Guido van Rossum's avatar
Guido van Rossum committed
2985 2986 2987
#endif
#ifdef HAVE_STRERROR
	{"strerror",	posix_strerror, 1, posix_strerror__doc__},
2988
#endif
2989 2990 2991 2992 2993 2994
#ifdef HAVE_FSYNC
	{"fsync",       posix_fsync, 0, posix_fsync__doc__},
#endif
#ifdef HAVE_FDATASYNC
	{"fdatasync",   posix_fdatasync,  0, posix_fdatasync__doc__},
#endif
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
#ifdef HAVE_SYS_WAIT_H
#ifdef WIFSTOPPED
        {"WIFSTOPPED",	posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
#endif /* WIFSTOPPED */
#ifdef WIFSIGNALED
        {"WIFSIGNALED",	posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
#endif /* WIFSIGNALED */
#ifdef WIFEXITED
        {"WIFEXITED",	posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
#endif /* WIFEXITED */
#ifdef WEXITSTATUS
        {"WEXITSTATUS",	posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
#endif /* WEXITSTATUS */
#ifdef WTERMSIG
        {"WTERMSIG",	posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
#endif /* WTERMSIG */
#ifdef WSTOPSIG
        {"WSTOPSIG",	posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
#endif /* WSTOPSIG */
#endif /* HAVE_SYS_WAIT_H */
3015 3016 3017 3018 3019 3020
#ifdef HAVE_FSTATVFS
	{"fstatvfs",	posix_fstatvfs, 1, posix_fstatvfs__doc__},
#endif
#ifdef HAVE_STATVFS
	{"statvfs",	posix_statvfs, 1, posix_statvfs__doc__},
#endif
Guido van Rossum's avatar
Guido van Rossum committed
3021 3022 3023 3024
	{NULL,		NULL}		 /* Sentinel */
};


3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
static int
ins(d, symbol, value)
        PyObject* d;
        char* symbol;
        long value;
{
        PyObject* v = PyInt_FromLong(value);
        if (!v || PyDict_SetItemString(d, symbol, v) < 0)
                return -1;                   /* triggers fatal error */

        Py_DECREF(v);
        return 0;
}

3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
#if defined(PYOS_OS2)
/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
static int insertvalues(PyObject *d)
{
    APIRET    rc;
    ULONG     values[QSV_MAX+1];
    PyObject *v;
    char     *ver, tmp[10];

    Py_BEGIN_ALLOW_THREADS
    rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
    Py_END_ALLOW_THREADS

    if (rc != NO_ERROR) {
        os2_error(rc);
        return -1;
    }

    if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
    if (ins(d, "memkernel",    values[QSV_TOTRESMEM])) return -1;
    if (ins(d, "memvirtual",   values[QSV_TOTAVAILMEM])) return -1;
    if (ins(d, "maxpathlen",   values[QSV_MAX_PATH_LENGTH])) return -1;
    if (ins(d, "maxnamelen",   values[QSV_MAX_COMP_LENGTH])) return -1;
    if (ins(d, "revision",     values[QSV_VERSION_REVISION])) return -1;
    if (ins(d, "timeslice",    values[QSV_MIN_SLICE])) return -1;

    switch (values[QSV_VERSION_MINOR]) {
    case 0:  ver = "2.00"; break;
    case 10: ver = "2.10"; break;
    case 11: ver = "2.11"; break;
    case 30: ver = "3.00"; break;
    case 40: ver = "4.00"; break;
    case 50: ver = "5.00"; break;
    default:
        sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
                              values[QSV_VERSION_MINOR]);
        ver = &tmp[0];
    }

    /* Add Indicator of the Version of the Operating System */
    v = PyString_FromString(ver);
    if (!v || PyDict_SetItemString(d, "version", v) < 0)
        return -1;
    Py_DECREF(v);

    /* Add Indicator of Which Drive was Used to Boot the System */
    tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
    tmp[1] = ':';
    tmp[2] = '\0';

    v = PyString_FromString(tmp);
    if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
        return -1;
    Py_DECREF(v);

    return 0;
}
#endif

3098 3099 3100 3101
static int
all_ins(d)
        PyObject* d;
{
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
#ifdef F_OK
        if (ins(d, "F_OK", (long)F_OK)) return -1;
#endif        
#ifdef R_OK
        if (ins(d, "R_OK", (long)R_OK)) return -1;
#endif        
#ifdef W_OK
        if (ins(d, "W_OK", (long)W_OK)) return -1;
#endif        
#ifdef X_OK
        if (ins(d, "X_OK", (long)X_OK)) return -1;
#endif        
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
#ifdef WNOHANG
        if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
#endif        
#ifdef O_RDONLY
        if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
#endif
#ifdef O_WRONLY
        if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
#endif
#ifdef O_RDWR
        if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
#endif
#ifdef O_NDELAY
        if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
#endif
#ifdef O_NONBLOCK
        if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
#endif
#ifdef O_APPEND
        if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
#endif
#ifdef O_DSYNC
        if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
#endif
#ifdef O_RSYNC
        if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
#endif
#ifdef O_SYNC
        if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
#endif
#ifdef O_NOCTTY
        if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
#endif
#ifdef O_CREAT
        if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
#endif
#ifdef O_EXCL
        if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
#endif
#ifdef O_TRUNC
        if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3155 3156 3157 3158 3159 3160
#endif
#ifdef O_BINARY
        if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
#endif
#ifdef O_TEXT
        if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3161
#endif
3162 3163 3164 3165

#if defined(PYOS_OS2)
        if (insertvalues(d)) return -1;
#endif
3166 3167 3168 3169
        return 0;
}


3170
#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
3171 3172 3173
#define INITFUNC initnt
#define MODNAME "nt"
#else
Guido van Rossum's avatar
Guido van Rossum committed
3174 3175 3176 3177
#if defined(PYOS_OS2)
#define INITFUNC initos2
#define MODNAME "os2"
#else
3178 3179 3180
#define INITFUNC initposix
#define MODNAME "posix"
#endif
Guido van Rossum's avatar
Guido van Rossum committed
3181
#endif
3182

3183
DL_EXPORT(void)
3184
INITFUNC()
Guido van Rossum's avatar
Guido van Rossum committed
3185
{
Barry Warsaw's avatar
Barry Warsaw committed
3186
	PyObject *m, *d, *v;
Guido van Rossum's avatar
Guido van Rossum committed
3187
	
3188
	m = Py_InitModule4(MODNAME,
3189 3190
			   posix_methods,
			   posix__doc__,
3191 3192
			   (PyObject *)NULL,
			   PYTHON_API_VERSION);
Barry Warsaw's avatar
Barry Warsaw committed
3193
	d = PyModule_GetDict(m);
Guido van Rossum's avatar
Guido van Rossum committed
3194
	
3195
	/* Initialize environ dictionary */
Guido van Rossum's avatar
Guido van Rossum committed
3196
	v = convertenviron();
Barry Warsaw's avatar
Barry Warsaw committed
3197
	if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
3198
		return;
Barry Warsaw's avatar
Barry Warsaw committed
3199
	Py_DECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
3200
	
3201 3202 3203
        if (all_ins(d))
                return;

3204 3205 3206
	Py_INCREF(PyExc_OSError);
	PosixError = PyExc_OSError;
	PyDict_SetItemString(d, "error", PosixError);
Guido van Rossum's avatar
Guido van Rossum committed
3207
}