home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Modules / posixmodule.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  83KB  |  3,525 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* POSIX module implementation */
  33.  
  34. /* This file is also used for Windows NT and MS-Win.  In that case the module
  35.    actually calls itself 'nt', not 'posix', and a few functions are
  36.    either unimplemented or implemented differently.  The source
  37.    assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
  38.    of the compiler used.  Different compilers define their own feature
  39.    test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
  40.  
  41. /* See also ../Dos/dosmodule.c */
  42.  
  43. static char posix__doc__ [] =
  44. "This module provides access to operating system functionality that is\n\
  45. standardized by the C Standard and the POSIX standard (a thinly\n\
  46. disguised Unix interface).  Refer to the library manual and\n\
  47. corresponding Unix manual entries for more information on calls.";
  48.  
  49. #include "Python.h"
  50.  
  51. #if defined(PYOS_OS2)
  52. #define  INCL_DOS
  53. #define  INCL_DOSERRORS
  54. #define  INCL_DOSPROCESS
  55. #define  INCL_NOPMAPI
  56. #include <os2.h>
  57. #endif
  58.  
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. #ifdef HAVE_SYS_WAIT_H
  62. #include <sys/wait.h>        /* For WNOHANG */
  63. #endif
  64.  
  65. #ifdef HAVE_SIGNAL_H
  66. #include <signal.h>
  67. #endif
  68.  
  69. #include "mytime.h"        /* For clock_t on some systems */
  70.  
  71. #ifdef HAVE_FCNTL_H
  72. #include <fcntl.h>
  73. #endif /* HAVE_FCNTL_H */
  74.  
  75. /* Various compilers have only certain posix functions */
  76. /* XXX Gosh I wish these were all moved into config.h */
  77. #if defined(PYCC_VACPP) && defined(PYOS_OS2)
  78. #include <process.h>
  79. #else
  80. #if defined(__WATCOMC__) && !defined(__QNX__)        /* Watcom compiler */
  81. #define HAVE_GETCWD     1
  82. #define HAVE_OPENDIR    1
  83. #define HAVE_SYSTEM    1
  84. #if defined(__OS2__)
  85. #define HAVE_EXECV      1
  86. #define HAVE_WAIT       1
  87. #endif
  88. #include <process.h>
  89. #else
  90. #ifdef __BORLANDC__        /* Borland compiler */
  91. #define HAVE_EXECV      1
  92. #define HAVE_GETCWD     1
  93. #define HAVE_GETEGID    1
  94. #define HAVE_GETEUID    1
  95. #define HAVE_GETGID     1
  96. #define HAVE_GETPPID    1
  97. #define HAVE_GETUID     1
  98. #define HAVE_KILL       1
  99. #define HAVE_OPENDIR    1
  100. #define HAVE_PIPE       1
  101. #define HAVE_POPEN      1
  102. #define HAVE_SYSTEM    1
  103. #define HAVE_WAIT       1
  104. #else
  105. #ifdef _MSC_VER        /* Microsoft compiler */
  106. #define HAVE_GETCWD     1
  107. #ifdef MS_WIN32
  108. #define HAVE_SPAWNV    1
  109. #define HAVE_EXECV      1
  110. #define HAVE_PIPE       1
  111. #define HAVE_POPEN      1
  112. #define HAVE_SYSTEM    1
  113. #else /* 16-bit Windows */
  114. #endif /* !MS_WIN32 */
  115. #else            /* all other compilers */
  116. /* Unix functions that the configure script doesn't check for */
  117. #define HAVE_EXECV      1
  118. #define HAVE_FORK       1
  119. #define HAVE_GETCWD     1
  120. #define HAVE_GETEGID    1
  121. #define HAVE_GETEUID    1
  122. #define HAVE_GETGID     1
  123. #define HAVE_GETPPID    1
  124. #define HAVE_GETUID     1
  125. #define HAVE_KILL       1
  126. #define HAVE_OPENDIR    1
  127. #define HAVE_PIPE       1
  128. #define HAVE_POPEN      1
  129. #define HAVE_SYSTEM    1
  130. #define HAVE_WAIT       1
  131. #define HAVE_TTYNAME    1
  132. #endif  /* _MSC_VER */
  133. #endif  /* __BORLANDC__ */
  134. #endif  /* ! __WATCOMC__ || __QNX__ */
  135. #endif /* ! __IBMC__ */
  136.  
  137. #ifndef _MSC_VER
  138.  
  139. #ifdef HAVE_UNISTD_H
  140. #include <unistd.h>
  141. #endif
  142.  
  143. #ifdef NeXT
  144. /* NeXT's <unistd.h> and <utime.h> aren't worth much */
  145. #undef HAVE_UNISTD_H
  146. #undef HAVE_UTIME_H
  147. #define HAVE_WAITPID
  148. /* #undef HAVE_GETCWD */
  149. #define UNION_WAIT /* This should really be checked for by autoconf */
  150. #endif
  151.  
  152. #ifdef HAVE_UNISTD_H
  153. /* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
  154. extern int rename();
  155. extern int pclose();
  156. extern int lstat();
  157. extern int symlink();
  158. extern int fsync();
  159. #else /* !HAVE_UNISTD_H */
  160. #if defined(PYCC_VACPP)
  161. extern int mkdir Py_PROTO((char *));
  162. #else
  163. #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
  164. extern int mkdir Py_PROTO((const char *));
  165. #else
  166. extern int mkdir Py_PROTO((const char *, mode_t));
  167. #endif
  168. #endif
  169. #if defined(__IBMC__) || defined(__IBMCPP__)
  170. extern int chdir Py_PROTO((char *));
  171. extern int rmdir Py_PROTO((char *));
  172. #else
  173. extern int chdir Py_PROTO((const char *));
  174. extern int rmdir Py_PROTO((const char *));
  175. #endif
  176. extern int chmod Py_PROTO((const char *, mode_t));
  177. extern int chown Py_PROTO((const char *, uid_t, gid_t));
  178. extern char *getcwd Py_PROTO((char *, int));
  179. extern char *strerror Py_PROTO((int));
  180. extern int link Py_PROTO((const char *, const char *));
  181. extern int rename Py_PROTO((const char *, const char *));
  182. extern int stat Py_PROTO((const char *, struct stat *));
  183. extern int unlink Py_PROTO((const char *));
  184. extern int pclose Py_PROTO((FILE *));
  185. #ifdef HAVE_SYMLINK
  186. extern int symlink Py_PROTO((const char *, const char *));
  187. #endif /* HAVE_SYMLINK */
  188. #ifdef HAVE_LSTAT
  189. extern int lstat Py_PROTO((const char *, struct stat *));
  190. #endif /* HAVE_LSTAT */
  191. #endif /* !HAVE_UNISTD_H */
  192.  
  193. #endif /* !_MSC_VER */
  194.  
  195. #ifdef HAVE_UTIME_H
  196. #include <utime.h>
  197. #endif /* HAVE_UTIME_H */
  198.  
  199. #ifdef HAVE_SYS_UTIME_H
  200. #include <sys/utime.h>
  201. #define HAVE_UTIME_H /* pretend we do for the rest of this file */
  202. #endif /* HAVE_SYS_UTIME_H */
  203.  
  204. #ifdef HAVE_SYS_TIMES_H
  205. #include <sys/times.h>
  206. #endif /* HAVE_SYS_TIMES_H */
  207.  
  208. #ifdef HAVE_SYS_PARAM_H
  209. #include <sys/param.h>
  210. #endif /* HAVE_SYS_PARAM_H */
  211.  
  212. #ifdef HAVE_SYS_UTSNAME_H
  213. #include <sys/utsname.h>
  214. #endif /* HAVE_SYS_UTSNAME_H */
  215.  
  216. #ifndef MAXPATHLEN
  217. #define MAXPATHLEN 1024
  218. #endif /* MAXPATHLEN */
  219.  
  220. #ifdef HAVE_DIRENT_H
  221. #include <dirent.h>
  222. #define NAMLEN(dirent) strlen((dirent)->d_name)
  223. #else
  224. #if defined(__WATCOMC__) && !defined(__QNX__)
  225. #include <direct.h>
  226. #define NAMLEN(dirent) strlen((dirent)->d_name)
  227. #else
  228. #define dirent direct
  229. #define NAMLEN(dirent) (dirent)->d_namlen
  230. #endif
  231. #ifdef HAVE_SYS_NDIR_H
  232. #include <sys/ndir.h>
  233. #endif
  234. #ifdef HAVE_SYS_DIR_H
  235. #include <sys/dir.h>
  236. #endif
  237. #ifdef HAVE_NDIR_H
  238. #include <ndir.h>
  239. #endif
  240. #endif
  241.  
  242. #ifdef _MSC_VER
  243. #include <direct.h>
  244. #include <io.h>
  245. #include <process.h>
  246. #include <windows.h>
  247. #ifdef MS_WIN32
  248. #define popen    _popen
  249. #define pclose    _pclose
  250. #else /* 16-bit Windows */
  251. #include <dos.h>
  252. #include <ctype.h>
  253. #endif /* MS_WIN32 */
  254. #endif /* _MSC_VER */
  255.  
  256. #if defined(PYCC_VACPP) && defined(PYOS_OS2)
  257. #include <io.h>
  258. #endif /* OS2 */
  259.  
  260. #ifdef UNION_WAIT
  261. /* Emulate some macros on systems that have a union instead of macros */
  262.  
  263. #ifndef WIFEXITED
  264. #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
  265. #endif
  266.  
  267. #ifndef WEXITSTATUS
  268. #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
  269. #endif
  270.  
  271. #ifndef WTERMSIG
  272. #define WTERMSIG(u_wait) ((u_wait).w_termsig)
  273. #endif
  274.  
  275. #endif /* UNION_WAIT */
  276.  
  277. /* Return a dictionary corresponding to the POSIX environment table */
  278.  
  279. #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
  280. extern char **environ;
  281. #endif /* !_MSC_VER */
  282.  
  283. static PyObject *
  284. convertenviron()
  285. {
  286.     PyObject *d;
  287.     char **e;
  288.     d = PyDict_New();
  289.     if (d == NULL)
  290.         return NULL;
  291.     if (environ == NULL)
  292.         return d;
  293.     /* XXX This part ignores errors */
  294.     for (e = environ; *e != NULL; e++) {
  295.         PyObject *v;
  296.         char *p = strchr(*e, '=');
  297.         if (p == NULL)
  298.             continue;
  299.         v = PyString_FromString(p+1);
  300.         if (v == NULL)
  301.             continue;
  302.         *p = '\0';
  303.         (void) PyDict_SetItemString(d, *e, v);
  304.         *p = '=';
  305.         Py_DECREF(v);
  306.     }
  307. #if defined(PYOS_OS2)
  308.     {
  309.         APIRET rc;
  310.         char   buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
  311.  
  312.         rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
  313.         if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
  314.             PyObject *v = PyString_FromString(buffer);
  315.             PyDict_SetItemString(d, "BEGINLIBPATH", v);
  316.             Py_DECREF(v);
  317.         }
  318.         rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
  319.         if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
  320.             PyObject *v = PyString_FromString(buffer);
  321.             PyDict_SetItemString(d, "ENDLIBPATH", v);
  322.             Py_DECREF(v);
  323.         }
  324.     }
  325. #endif
  326.     return d;
  327. }
  328.  
  329.  
  330. /* Set a POSIX-specific error from errno, and return NULL */
  331.  
  332. static PyObject *
  333. posix_error()
  334. {
  335.     return PyErr_SetFromErrno(PyExc_OSError);
  336. }
  337. static PyObject *
  338. posix_error_with_filename(name)
  339.     char* name;
  340. {
  341.     return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
  342. }
  343.  
  344.  
  345. #if defined(PYOS_OS2)
  346. /**********************************************************************
  347.  *         Helper Function to Trim and Format OS/2 Messages
  348.  **********************************************************************/
  349.     static void
  350. os2_formatmsg(char *msgbuf, int msglen, char *reason)
  351. {
  352.     msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
  353.  
  354.     if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
  355.         char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
  356.  
  357.         while (lastc > msgbuf && isspace(*lastc))
  358.             *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
  359.     }
  360.  
  361.     /* Add Optional Reason Text */
  362.     if (reason) {
  363.         strcat(msgbuf, " : ");
  364.         strcat(msgbuf, reason);
  365.     }
  366. }
  367.  
  368. /**********************************************************************
  369.  *             Decode an OS/2 Operating System Error Code
  370.  *
  371.  * A convenience function to lookup an OS/2 error code and return a
  372.  * text message we can use to raise a Python exception.
  373.  *
  374.  * Notes:
  375.  *   The messages for errors returned from the OS/2 kernel reside in
  376.  *   the file OSO001.MSG in the \OS2 directory hierarchy.
  377.  *
  378.  **********************************************************************/
  379.     static char *
  380. os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
  381. {
  382.     APIRET rc;
  383.     ULONG  msglen;
  384.  
  385.     /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
  386.     Py_BEGIN_ALLOW_THREADS
  387.     rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
  388.                        errorcode, "oso001.msg", &msglen);
  389.     Py_END_ALLOW_THREADS
  390.  
  391.     if (rc == NO_ERROR)
  392.         os2_formatmsg(msgbuf, msglen, reason);
  393.     else
  394.         sprintf(msgbuf, "unknown OS error #%d", errorcode);
  395.  
  396.     return msgbuf;
  397. }
  398.  
  399. /* Set an OS/2-specific error and return NULL.  OS/2 kernel
  400.    errors are not in a global variable e.g. 'errno' nor are
  401.    they congruent with posix error numbers. */
  402.  
  403. static PyObject * os2_error(int code)
  404. {
  405.     char text[1024];
  406.     PyObject *v;
  407.  
  408.     os2_strerror(text, sizeof(text), code, "");
  409.  
  410.     v = Py_BuildValue("(is)", code, text);
  411.     if (v != NULL) {
  412.         PyErr_SetObject(PyExc_OSError, v);
  413.         Py_DECREF(v);
  414.     }
  415.     return NULL; /* Signal to Python that an Exception is Pending */
  416. }
  417.  
  418. #if defined(PYCC_VACPP)
  419. /*
  420.    Added Special OS/2 version of this to change the drive as well as the
  421.     path.  This can be added to any system that supports _chdrive()
  422. */
  423. static PyObject *
  424. os2_posix_chdir(self, args)
  425.     PyObject *self;
  426.     PyObject *args;
  427. {
  428.     char *path1;
  429.     int res;
  430.     if (!PyArg_Parse(args, "s", &path1))
  431.         return NULL;
  432.     Py_BEGIN_ALLOW_THREADS
  433.     res = chdir(path1);
  434.     
  435.     /* check for a drive letter - if there is one, do the chdrive thing */
  436.     if (res >= 0 && *path1 && path1[1] == ':') {
  437.         res = _chdrive(toupper(*path1) - '@');
  438.     }
  439.     
  440.     Py_END_ALLOW_THREADS
  441.     if (res < 0)
  442.         return posix_error();
  443.     Py_INCREF(Py_None);
  444.     return Py_None;
  445. }
  446. #endif /* VACPP */
  447. #endif /* OS2 */
  448.  
  449. /* POSIX generic methods */
  450.  
  451. static PyObject *
  452. posix_int(args, func)
  453.         PyObject *args;
  454.     int (*func) Py_FPROTO((int));
  455. {
  456.     int fd;
  457.     int res;
  458.     if (!PyArg_Parse(args,  "i", &fd))
  459.         return NULL;
  460.     Py_BEGIN_ALLOW_THREADS
  461.     res = (*func)(fd);
  462.     Py_END_ALLOW_THREADS
  463.     if (res < 0)
  464.         return posix_error();
  465.     Py_INCREF(Py_None);
  466.     return Py_None;
  467. }
  468.  
  469.  
  470. static PyObject *
  471. posix_1str(args, func)
  472.     PyObject *args;
  473.     int (*func) Py_FPROTO((const char *));
  474. {
  475.     char *path1;
  476.     int res;
  477.     if (!PyArg_Parse(args, "s", &path1))
  478.         return NULL;
  479.     Py_BEGIN_ALLOW_THREADS
  480.     res = (*func)(path1);
  481.     Py_END_ALLOW_THREADS
  482.     if (res < 0)
  483.         return posix_error_with_filename(path1);
  484.     Py_INCREF(Py_None);
  485.     return Py_None;
  486. }
  487.  
  488. static PyObject *
  489. posix_2str(args, func)
  490.     PyObject *args;
  491.     int (*func) Py_FPROTO((const char *, const char *));
  492. {
  493.     char *path1, *path2;
  494.     int res;
  495.     if (!PyArg_Parse(args, "(ss)", &path1, &path2))
  496.         return NULL;
  497.     Py_BEGIN_ALLOW_THREADS
  498.     res = (*func)(path1, path2);
  499.     Py_END_ALLOW_THREADS
  500.     if (res != 0)
  501.         /* XXX how to report both path1 and path2??? */
  502.         return posix_error();
  503.     Py_INCREF(Py_None);
  504.     return Py_None;
  505. }
  506.  
  507. static PyObject *
  508. posix_strint(args, func)
  509.     PyObject *args;
  510.     int (*func) Py_FPROTO((const char *, int));
  511. {
  512.     char *path;
  513.     int i;
  514.     int res;
  515.     if (!PyArg_Parse(args, "(si)", &path, &i))
  516.         return NULL;
  517.     Py_BEGIN_ALLOW_THREADS
  518.     res = (*func)(path, i);
  519.     Py_END_ALLOW_THREADS
  520.     if (res < 0)
  521.         return posix_error_with_filename(path);
  522.     Py_INCREF(Py_None);
  523.     return Py_None;
  524. }
  525.  
  526. static PyObject *
  527. posix_strintint(args, func)
  528.     PyObject *args;
  529.     int (*func) Py_FPROTO((const char *, int, int));
  530. {
  531.     char *path;
  532.     int i,i2;
  533.     int res;
  534.     if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
  535.         return NULL;
  536.     Py_BEGIN_ALLOW_THREADS
  537.     res = (*func)(path, i, i2);
  538.     Py_END_ALLOW_THREADS
  539.     if (res < 0)
  540.         return posix_error_with_filename(path);
  541.     Py_INCREF(Py_None);
  542.     return Py_None;
  543. }
  544.  
  545. static PyObject *
  546. posix_do_stat(self, args, statfunc)
  547.     PyObject *self;
  548.     PyObject *args;
  549.     int (*statfunc) Py_FPROTO((const char *, struct stat *));
  550. {
  551.     struct stat st;
  552.     char *path;
  553.     int res;
  554.     if (!PyArg_Parse(args, "s", &path))
  555.         return NULL;
  556.     Py_BEGIN_ALLOW_THREADS
  557.     res = (*statfunc)(path, &st);
  558.     Py_END_ALLOW_THREADS
  559.     if (res != 0)
  560.         return posix_error_with_filename(path);
  561. #if !defined(HAVE_LARGEFILE_SUPPORT)
  562.     return Py_BuildValue("(llllllllll)",
  563.                  (long)st.st_mode,
  564.                  (long)st.st_ino,
  565.                  (long)st.st_dev,
  566.                  (long)st.st_nlink,
  567.                  (long)st.st_uid,
  568.                  (long)st.st_gid,
  569.                  (long)st.st_size,
  570.                  (long)st.st_atime,
  571.                  (long)st.st_mtime,
  572.                  (long)st.st_ctime);
  573. #else
  574.     return Py_BuildValue("(lLllllLlll)",
  575.                  (long)st.st_mode,
  576.                  (LONG_LONG)st.st_ino,
  577.                  (long)st.st_dev,
  578.                  (long)st.st_nlink,
  579.                  (long)st.st_uid,
  580.                  (long)st.st_gid,
  581.                  (LONG_LONG)st.st_size,
  582.                  (long)st.st_atime,
  583.                  (long)st.st_mtime,
  584.                  (long)st.st_ctime);
  585. #endif
  586. }
  587.  
  588.  
  589. /* POSIX methods */
  590.  
  591. static char posix_access__doc__[] =
  592. "access(path, mode) -> 1 if granted, 0 otherwise\n\
  593. Test for access to a file.";
  594.  
  595. static PyObject *
  596. posix_access(self, args)
  597.     PyObject *self;
  598.     PyObject *args;
  599. {
  600.     char *path;
  601.     int mode;
  602.     int res;
  603.  
  604.     if (!PyArg_Parse(args, "(si)", &path, &mode))
  605.         return NULL;
  606.     Py_BEGIN_ALLOW_THREADS
  607.     res = access(path, mode);
  608.     Py_END_ALLOW_THREADS
  609.     return(PyInt_FromLong(res == 0 ? 1L : 0L));
  610. }
  611.  
  612. #ifndef F_OK
  613. #define F_OK 0
  614. #endif
  615. #ifndef R_OK
  616. #define R_OK 4
  617. #endif
  618. #ifndef W_OK
  619. #define W_OK 2
  620. #endif
  621. #ifndef X_OK
  622. #define X_OK 1
  623. #endif
  624.  
  625. #ifdef HAVE_TTYNAME
  626. static char posix_ttyname__doc__[] =
  627. "ttyname(fd) -> String\n\
  628. Return the name of the terminal device connected to 'fd'.";
  629.  
  630. static PyObject *
  631. posix_ttyname(self, args)
  632.     PyObject *self;
  633.     PyObject *args;
  634. {
  635.     PyObject *file;
  636.     int id;
  637.     char *ret;
  638.  
  639.     if (!PyArg_Parse(args, "i", &id))
  640.         return NULL;
  641.  
  642.     ret = ttyname(id);
  643.     if (ret == NULL)
  644.         return(posix_error());
  645.     return(PyString_FromString(ret));
  646. }
  647. #endif
  648.  
  649. static char posix_chdir__doc__[] =
  650. "chdir(path) -> None\n\
  651. Change the current working directory to the specified path.";
  652.  
  653. static PyObject *
  654. posix_chdir(self, args)
  655.     PyObject *self;
  656.     PyObject *args;
  657. {
  658. #if defined(PYOS_OS2) && defined(PYCC_VACPP)
  659.     return os2_posix_chdir(self, args);
  660. #endif
  661.     return posix_1str(args, chdir);
  662. }
  663.  
  664.  
  665. static char posix_chmod__doc__[] =
  666. "chmod(path, mode) -> None\n\
  667. Change the access permissions of a file.";
  668.  
  669. static PyObject *
  670. posix_chmod(self, args)
  671.     PyObject *self;
  672.     PyObject *args;
  673. {
  674.     return posix_strint(args, chmod);
  675. }
  676.  
  677.  
  678. #ifdef HAVE_FSYNC
  679. static char posix_fsync__doc__[] =
  680. "fsync(fildes) -> None\n\
  681. force write of file with filedescriptor to disk.";
  682.  
  683. static PyObject *
  684. posix_fsync(self, args)
  685.        PyObject *self;
  686.        PyObject *args;
  687. {
  688.        return posix_int(args, fsync);
  689. }
  690. #endif /* HAVE_FSYNC */
  691.  
  692. #ifdef HAVE_FDATASYNC
  693. static char posix_fdatasync__doc__[] =
  694. "fdatasync(fildes) -> None\n\
  695. force write of file with filedescriptor to disk.\n\
  696.  does not force update of metadata.";
  697.  
  698. extern int fdatasync(int); /* Prototype just in case */
  699.  
  700. static PyObject *
  701. posix_fdatasync(self, args)
  702.        PyObject *self;
  703.        PyObject *args;
  704. {
  705.        return posix_int(args, fdatasync);
  706. }
  707. #endif /* HAVE_FDATASYNC */
  708.  
  709.  
  710. #ifdef HAVE_CHOWN
  711. static char posix_chown__doc__[] =
  712. "chown(path, uid, gid) -> None\n\
  713. Change the owner and group id of path to the numeric uid and gid.";
  714.  
  715. static PyObject *
  716. posix_chown(self, args)
  717.     PyObject *self;
  718.     PyObject *args;
  719. {
  720.     return posix_strintint(args, chown);
  721. }
  722. #endif /* HAVE_CHOWN */
  723.  
  724.  
  725. #ifdef HAVE_GETCWD
  726. static char posix_getcwd__doc__[] =
  727. "getcwd() -> path\n\
  728. Return a string representing the current working directory.";
  729.  
  730. static PyObject *
  731. posix_getcwd(self, args)
  732.     PyObject *self;
  733.     PyObject *args;
  734. {
  735.     char buf[1026];
  736.     char *res;
  737.     if (!PyArg_NoArgs(args))
  738.         return NULL;
  739.     Py_BEGIN_ALLOW_THREADS
  740.     res = getcwd(buf, sizeof buf);
  741.     Py_END_ALLOW_THREADS
  742.     if (res == NULL)
  743.         return posix_error();
  744.     return PyString_FromString(buf);
  745. }
  746. #endif
  747.  
  748.  
  749. #ifdef HAVE_LINK
  750. static char posix_link__doc__[] =
  751. "link(src, dst) -> None\n\
  752. Create a hard link to a file.";
  753.  
  754. static PyObject *
  755. posix_link(self, args)
  756.     PyObject *self;
  757.     PyObject *args;
  758. {
  759.     return posix_2str(args, link);
  760. }
  761. #endif /* HAVE_LINK */
  762.  
  763.  
  764. static char posix_listdir__doc__[] =
  765. "listdir(path) -> list_of_strings\n\
  766. Return a list containing the names of the entries in the directory.\n\
  767. \n\
  768.     path: path of directory to list\n\
  769. \n\
  770. The list is in arbitrary order.  It does not include the special\n\
  771. entries '.' and '..' even if they are present in the directory.";
  772.  
  773. static PyObject *
  774. posix_listdir(self, args)
  775.     PyObject *self;
  776.     PyObject *args;
  777. {
  778.     /* XXX Should redo this putting the (now four) versions of opendir
  779.        in separate files instead of having them all here... */
  780. #if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
  781.  
  782.     char *name;
  783.     int len;
  784.     PyObject *d, *v;
  785.     HANDLE hFindFile;
  786.     WIN32_FIND_DATA FileData;
  787.     char namebuf[MAX_PATH+5];
  788.  
  789.     if (!PyArg_Parse(args, "t#", &name, &len))
  790.         return NULL;
  791.     if (len >= MAX_PATH) {
  792.         PyErr_SetString(PyExc_ValueError, "path too long");
  793.         return NULL;
  794.     }
  795.     strcpy(namebuf, name);
  796.     if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
  797.         namebuf[len++] = '/';
  798.     strcpy(namebuf + len, "*.*");
  799.  
  800.     if ((d = PyList_New(0)) == NULL)
  801.         return NULL;
  802.  
  803.     hFindFile = FindFirstFile(namebuf, &FileData);
  804.     if (hFindFile == INVALID_HANDLE_VALUE) {
  805.         errno = GetLastError();
  806.         if (errno == ERROR_FILE_NOT_FOUND)
  807.             return PyList_New(0);
  808.         return posix_error();
  809.     }
  810.     do {
  811.         if (FileData.cFileName[0] == '.' &&
  812.             (FileData.cFileName[1] == '\0' ||
  813.              FileData.cFileName[1] == '.' &&
  814.              FileData.cFileName[2] == '\0'))
  815.             continue;
  816.         v = PyString_FromString(FileData.cFileName);
  817.         if (v == NULL) {
  818.             Py_DECREF(d);
  819.             d = NULL;
  820.             break;
  821.         }
  822.         if (PyList_Append(d, v) != 0) {
  823.             Py_DECREF(v);
  824.             Py_DECREF(d);
  825.             d = NULL;
  826.             break;
  827.         }
  828.         Py_DECREF(v);
  829.     } while (FindNextFile(hFindFile, &FileData) == TRUE);
  830.  
  831.     if (FindClose(hFindFile) == FALSE) {
  832.         errno = GetLastError();
  833.         return posix_error();
  834.     }
  835.  
  836.     return d;
  837.  
  838. #else /* !MS_WIN32 */
  839. #ifdef _MSC_VER /* 16-bit Windows */
  840.  
  841. #ifndef MAX_PATH
  842. #define MAX_PATH    250
  843. #endif
  844.     char *name, *pt;
  845.     int len;
  846.     PyObject *d, *v;
  847.     char namebuf[MAX_PATH+5];
  848.     struct _find_t ep;
  849.  
  850.     if (!PyArg_Parse(args, "t#", &name, &len))
  851.         return NULL;
  852.     if (len >= MAX_PATH) {
  853.         PyErr_SetString(PyExc_ValueError, "path too long");
  854.         return NULL;
  855.     }
  856.     strcpy(namebuf, name);
  857.     for (pt = namebuf; *pt; pt++)
  858.         if (*pt == '/')
  859.             *pt = '\\';
  860.     if (namebuf[len-1] != '\\')
  861.         namebuf[len++] = '\\';
  862.     strcpy(namebuf + len, "*.*");
  863.  
  864.     if ((d = PyList_New(0)) == NULL)
  865.         return NULL;
  866.  
  867.     if (_dos_findfirst(namebuf, _A_RDONLY |
  868.                _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
  869.         {
  870.         errno = ENOENT;
  871.         return posix_error();
  872.     }
  873.     do {
  874.         if (ep.name[0] == '.' &&
  875.             (ep.name[1] == '\0' ||
  876.              ep.name[1] == '.' &&
  877.              ep.name[2] == '\0'))
  878.             continue;
  879.         strcpy(namebuf, ep.name);
  880.         for (pt = namebuf; *pt; pt++)
  881.             if (isupper(*pt))
  882.                 *pt = tolower(*pt);
  883.         v = PyString_FromString(namebuf);
  884.         if (v == NULL) {
  885.             Py_DECREF(d);
  886.             d = NULL;
  887.             break;
  888.         }
  889.         if (PyList_Append(d, v) != 0) {
  890.             Py_DECREF(v);
  891.             Py_DECREF(d);
  892.             d = NULL;
  893.             break;
  894.         }
  895.         Py_DECREF(v);
  896.     } while (_dos_findnext(&ep) == 0);
  897.  
  898.     return d;
  899.  
  900. #else
  901. #if defined(PYOS_OS2)
  902.  
  903. #ifndef MAX_PATH
  904. #define MAX_PATH    CCHMAXPATH
  905. #endif
  906.     char *name, *pt;
  907.     int len;
  908.     PyObject *d, *v;
  909.     char namebuf[MAX_PATH+5];
  910.     HDIR  hdir = 1;
  911.     ULONG srchcnt = 1;
  912.     FILEFINDBUF3   ep;
  913.     APIRET rc;
  914.  
  915.     if (!PyArg_Parse(args, "t#", &name, &len))
  916.         return NULL;
  917.     if (len >= MAX_PATH) {
  918.         PyErr_SetString(PyExc_ValueError, "path too long");
  919.         return NULL;
  920.     }
  921.     strcpy(namebuf, name);
  922.     for (pt = namebuf; *pt; pt++)
  923.         if (*pt == '/')
  924.             *pt = '\\';
  925.     if (namebuf[len-1] != '\\')
  926.         namebuf[len++] = '\\';
  927.     strcpy(namebuf + len, "*.*");
  928.  
  929.     if ((d = PyList_New(0)) == NULL)
  930.         return NULL;
  931.  
  932.     rc = DosFindFirst(namebuf,         /* Wildcard Pattern to Match */
  933.                       &hdir,           /* Handle to Use While Search Directory */
  934.                       FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
  935.                       &ep, sizeof(ep), /* Structure to Receive Directory Entry */
  936.                       &srchcnt,        /* Max and Actual Count of Entries Per Iteration */
  937.                       FIL_STANDARD);   /* Format of Entry (EAs or Not) */
  938.  
  939.     if (rc != NO_ERROR) {
  940.         errno = ENOENT;
  941.         return posix_error();
  942.     }
  943.  
  944.     if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
  945.         do {
  946.             if (ep.achName[0] == '.'
  947.             && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
  948.                 continue; /* Skip Over "." and ".." Names */
  949.  
  950.             strcpy(namebuf, ep.achName);
  951.  
  952.             /* Leave Case of Name Alone -- In Native Form */
  953.             /* (Removed Forced Lowercasing Code) */
  954.  
  955.             v = PyString_FromString(namebuf);
  956.             if (v == NULL) {
  957.                 Py_DECREF(d);
  958.                 d = NULL;
  959.                 break;
  960.             }
  961.             if (PyList_Append(d, v) != 0) {
  962.                 Py_DECREF(v);
  963.                 Py_DECREF(d);
  964.                 d = NULL;
  965.                 break;
  966.             }
  967.             Py_DECREF(v);
  968.         } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
  969.     }
  970.  
  971.     return d;
  972. #else
  973.  
  974.     char *name;
  975.     PyObject *d, *v;
  976.     DIR *dirp;
  977.     struct dirent *ep;
  978.     if (!PyArg_Parse(args, "s", &name))
  979.         return NULL;
  980.     Py_BEGIN_ALLOW_THREADS
  981.     if ((dirp = opendir(name)) == NULL) {
  982.         Py_BLOCK_THREADS
  983.         return posix_error();
  984.     }
  985.     if ((d = PyList_New(0)) == NULL) {
  986.         closedir(dirp);
  987.         Py_BLOCK_THREADS
  988.         return NULL;
  989.     }
  990.     while ((ep = readdir(dirp)) != NULL) {
  991.         if (ep->d_name[0] == '.' &&
  992.             (NAMLEN(ep) == 1 ||
  993.              (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
  994.             continue;
  995.         v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
  996.         if (v == NULL) {
  997.             Py_DECREF(d);
  998.             d = NULL;
  999.             break;
  1000.         }
  1001.         if (PyList_Append(d, v) != 0) {
  1002.             Py_DECREF(v);
  1003.             Py_DECREF(d);
  1004.             d = NULL;
  1005.             break;
  1006.         }
  1007.         Py_DECREF(v);
  1008.     }
  1009.     closedir(dirp);
  1010.     Py_END_ALLOW_THREADS
  1011.  
  1012.     return d;
  1013.  
  1014. #endif /* !PYOS_OS2 */
  1015. #endif /* !_MSC_VER */
  1016. #endif /* !MS_WIN32 */
  1017. }
  1018.  
  1019. static char posix_mkdir__doc__[] =
  1020. "mkdir(path [, mode=0777]) -> None\n\
  1021. Create a directory.";
  1022.  
  1023. static PyObject *
  1024. posix_mkdir(self, args)
  1025.     PyObject *self;
  1026.     PyObject *args;
  1027. {
  1028.     int res;
  1029.     char *path;
  1030.     int mode = 0777;
  1031.     if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
  1032.         return NULL;
  1033.     Py_BEGIN_ALLOW_THREADS
  1034. #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
  1035.     res = mkdir(path);
  1036. #else
  1037.     res = mkdir(path, mode);
  1038. #endif
  1039.     Py_END_ALLOW_THREADS
  1040.     if (res < 0)
  1041.         return posix_error_with_filename(path);
  1042.     Py_INCREF(Py_None);
  1043.     return Py_None;
  1044. }
  1045.  
  1046.  
  1047. #ifdef HAVE_NICE
  1048. static char posix_nice__doc__[] =
  1049. "nice(inc) -> new_priority\n\
  1050. Decrease the priority of process and return new priority.";
  1051.  
  1052. static PyObject *
  1053. posix_nice(self, args)
  1054.     PyObject *self;
  1055.     PyObject *args;
  1056. {
  1057.     int increment, value;
  1058.  
  1059.     if (!PyArg_Parse(args, "i", &increment))
  1060.         return NULL;
  1061.     value = nice(increment);
  1062.     if (value == -1)
  1063.         return posix_error();
  1064.     return PyInt_FromLong((long) value);
  1065. }
  1066. #endif /* HAVE_NICE */
  1067.  
  1068.  
  1069. static char posix_rename__doc__[] =
  1070. "rename(old, new) -> None\n\
  1071. Rename a file or directory.";
  1072.  
  1073. static PyObject *
  1074. posix_rename(self, args)
  1075.     PyObject *self;
  1076.     PyObject *args;
  1077. {
  1078.     return posix_2str(args, rename);
  1079. }
  1080.  
  1081.  
  1082. static char posix_rmdir__doc__[] =
  1083. "rmdir(path) -> None\n\
  1084. Remove a directory.";
  1085.  
  1086. static PyObject *
  1087. posix_rmdir(self, args)
  1088.     PyObject *self;
  1089.     PyObject *args;
  1090. {
  1091.     return posix_1str(args, rmdir);
  1092. }
  1093.  
  1094.  
  1095. static char posix_stat__doc__[] =
  1096. "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
  1097. Perform a stat system call on the given path.";
  1098.  
  1099. static PyObject *
  1100. posix_stat(self, args)
  1101.     PyObject *self;
  1102.     PyObject *args;
  1103. {
  1104.     return posix_do_stat(self, args, stat);
  1105. }
  1106.  
  1107.  
  1108. #ifdef HAVE_SYSTEM
  1109. static char posix_system__doc__[] =
  1110. "system(command) -> exit_status\n\
  1111. Execute the command (a string) in a subshell.";
  1112.  
  1113. static PyObject *
  1114. posix_system(self, args)
  1115.     PyObject *self;
  1116.     PyObject *args;
  1117. {
  1118.     char *command;
  1119.     long sts;
  1120.     if (!PyArg_Parse(args, "s", &command))
  1121.         return NULL;
  1122.     Py_BEGIN_ALLOW_THREADS
  1123.     sts = system(command);
  1124.     Py_END_ALLOW_THREADS
  1125.     return PyInt_FromLong(sts);
  1126. }
  1127. #endif
  1128.  
  1129.  
  1130. static char posix_umask__doc__[] =
  1131. "umask(new_mask) -> old_mask\n\
  1132. Set the current numeric umask and return the previous umask.";
  1133.  
  1134. static PyObject *
  1135. posix_umask(self, args)
  1136.     PyObject *self;
  1137.     PyObject *args;
  1138. {
  1139.     int i;
  1140.     if (!PyArg_Parse(args, "i", &i))
  1141.         return NULL;
  1142.     i = umask(i);
  1143.     if (i < 0)
  1144.         return posix_error();
  1145.     return PyInt_FromLong((long)i);
  1146. }
  1147.  
  1148.  
  1149. static char posix_unlink__doc__[] =
  1150. "unlink(path) -> None\n\
  1151. Remove a file (same as remove(path)).";
  1152.  
  1153. static char posix_remove__doc__[] =
  1154. "remove(path) -> None\n\
  1155. Remove a file (same as unlink(path)).";
  1156.  
  1157. static PyObject *
  1158. posix_unlink(self, args)
  1159.     PyObject *self;
  1160.     PyObject *args;
  1161. {
  1162.     return posix_1str(args, unlink);
  1163. }
  1164.  
  1165.  
  1166. #ifdef HAVE_UNAME
  1167. static char posix_uname__doc__[] =
  1168. "uname() -> (sysname, nodename, release, version, machine)\n\
  1169. Return a tuple identifying the current operating system.";
  1170.  
  1171. static PyObject *
  1172. posix_uname(self, args)
  1173.     PyObject *self;
  1174.     PyObject *args;
  1175. {
  1176.     struct utsname u;
  1177.     int res;
  1178.     if (!PyArg_NoArgs(args))
  1179.         return NULL;
  1180.     Py_BEGIN_ALLOW_THREADS
  1181.     res = uname(&u);
  1182.     Py_END_ALLOW_THREADS
  1183.     if (res < 0)
  1184.         return posix_error();
  1185.     return Py_BuildValue("(sssss)",
  1186.                  u.sysname,
  1187.                  u.nodename,
  1188.                  u.release,
  1189.                  u.version,
  1190.                  u.machine);
  1191. }
  1192. #endif /* HAVE_UNAME */
  1193.  
  1194.  
  1195. static char posix_utime__doc__[] =
  1196. "utime(path, (atime, utime)) -> None\n\
  1197. Set the access and modified time of the file to the given values.";
  1198.  
  1199. static PyObject *
  1200. posix_utime(self, args)
  1201.     PyObject *self;
  1202.     PyObject *args;
  1203. {
  1204.     char *path;
  1205.     long atime, mtime;
  1206.     int res;
  1207.  
  1208. /* XXX should define struct utimbuf instead, above */
  1209. #ifdef HAVE_UTIME_H
  1210.     struct utimbuf buf;
  1211. #define ATIME buf.actime
  1212. #define MTIME buf.modtime
  1213. #define UTIME_ARG &buf
  1214. #else /* HAVE_UTIME_H */
  1215.     time_t buf[2];
  1216. #define ATIME buf[0]
  1217. #define MTIME buf[1]
  1218. #define UTIME_ARG buf
  1219. #endif /* HAVE_UTIME_H */
  1220.  
  1221.     if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
  1222.         return NULL;
  1223.     ATIME = atime;
  1224.     MTIME = mtime;
  1225.     Py_BEGIN_ALLOW_THREADS
  1226.     res = utime(path, UTIME_ARG);
  1227.     Py_END_ALLOW_THREADS
  1228.     if (res < 0)
  1229.         return posix_error_with_filename(path);
  1230.     Py_INCREF(Py_None);
  1231.     return Py_None;
  1232. #undef UTIME_ARG
  1233. #undef ATIME
  1234. #undef MTIME
  1235. }
  1236.  
  1237.  
  1238. /* Process operations */
  1239.  
  1240. static char posix__exit__doc__[] =
  1241. "_exit(status)\n\
  1242. Exit to the system with specified status, without normal exit processing.";
  1243.  
  1244. static PyObject *
  1245. posix__exit(self, args)
  1246.     PyObject *self;
  1247.     PyObject *args;
  1248. {
  1249.     int sts;
  1250.     if (!PyArg_Parse(args, "i", &sts))
  1251.         return NULL;
  1252.     _exit(sts);
  1253.     return NULL; /* Make gcc -Wall happy */
  1254. }
  1255.  
  1256.  
  1257. #ifdef HAVE_EXECV
  1258. static char posix_execv__doc__[] =
  1259. "execv(path, args)\n\
  1260. Execute an executable path with arguments, replacing current process.\n\
  1261. \n\
  1262.     path: path of executable file\n\
  1263.     args: tuple or list of strings";
  1264.  
  1265. static PyObject *
  1266. posix_execv(self, args)
  1267.     PyObject *self;
  1268.     PyObject *args;
  1269. {
  1270.     char *path;
  1271.     PyObject *argv;
  1272.     char **argvlist;
  1273.     int i, argc;
  1274.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1275.  
  1276.     /* execv has two arguments: (path, argv), where
  1277.        argv is a list or tuple of strings. */
  1278.  
  1279.     if (!PyArg_Parse(args, "(sO)", &path, &argv))
  1280.         return NULL;
  1281.     if (PyList_Check(argv)) {
  1282.         argc = PyList_Size(argv);
  1283.         getitem = PyList_GetItem;
  1284.     }
  1285.     else if (PyTuple_Check(argv)) {
  1286.         argc = PyTuple_Size(argv);
  1287.         getitem = PyTuple_GetItem;
  1288.     }
  1289.     else {
  1290.  badarg:
  1291.         PyErr_BadArgument();
  1292.         return NULL;
  1293.     }
  1294.  
  1295.     argvlist = PyMem_NEW(char *, argc+1);
  1296.     if (argvlist == NULL)
  1297.         return NULL;
  1298.     for (i = 0; i < argc; i++) {
  1299.         if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
  1300.             PyMem_DEL(argvlist);
  1301.             goto badarg;
  1302.         }
  1303.     }
  1304.     argvlist[argc] = NULL;
  1305.  
  1306. #ifdef BAD_EXEC_PROTOTYPES
  1307.     execv(path, (const char **) argvlist);
  1308. #else /* BAD_EXEC_PROTOTYPES */
  1309.     execv(path, argvlist);
  1310. #endif /* BAD_EXEC_PROTOTYPES */
  1311.  
  1312.     /* If we get here it's definitely an error */
  1313.  
  1314.     PyMem_DEL(argvlist);
  1315.     return posix_error();
  1316. }
  1317.  
  1318.  
  1319. static char posix_execve__doc__[] =
  1320. "execve(path, args, env)\n\
  1321. Execute a path with arguments and environment, replacing current process.\n\
  1322. \n\
  1323.     path: path of executable file\n\
  1324.     args: tuple or list of arguments\n\
  1325.     env: dictonary of strings mapping to strings";
  1326.  
  1327. static PyObject *
  1328. posix_execve(self, args)
  1329.     PyObject *self;
  1330.     PyObject *args;
  1331. {
  1332.     char *path;
  1333.     PyObject *argv, *env;
  1334.     char **argvlist;
  1335.     char **envlist;
  1336.     PyObject *key, *val, *keys=NULL, *vals=NULL;
  1337.     int i, pos, argc, envc;
  1338.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1339.  
  1340.     /* execve has three arguments: (path, argv, env), where
  1341.        argv is a list or tuple of strings and env is a dictionary
  1342.        like posix.environ. */
  1343.  
  1344.     if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
  1345.         return NULL;
  1346.     if (PyList_Check(argv)) {
  1347.         argc = PyList_Size(argv);
  1348.         getitem = PyList_GetItem;
  1349.     }
  1350.     else if (PyTuple_Check(argv)) {
  1351.         argc = PyTuple_Size(argv);
  1352.         getitem = PyTuple_GetItem;
  1353.     }
  1354.     else {
  1355.         PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
  1356.         return NULL;
  1357.     }
  1358.     if (!PyMapping_Check(env)) {
  1359.         PyErr_SetString(PyExc_TypeError, "env must be mapping object");
  1360.         return NULL;
  1361.     }
  1362.  
  1363.     argvlist = PyMem_NEW(char *, argc+1);
  1364.     if (argvlist == NULL) {
  1365.         PyErr_NoMemory();
  1366.         return NULL;
  1367.     }
  1368.     for (i = 0; i < argc; i++) {
  1369.         if (!PyArg_Parse((*getitem)(argv, i),
  1370.                  "s;argv must be list of strings",
  1371.                  &argvlist[i]))
  1372.         {
  1373.             goto fail_1;
  1374.         }
  1375.     }
  1376.     argvlist[argc] = NULL;
  1377.  
  1378.     i = PyMapping_Length(env);
  1379.     envlist = PyMem_NEW(char *, i + 1);
  1380.     if (envlist == NULL) {
  1381.         PyErr_NoMemory();
  1382.         goto fail_1;
  1383.     }
  1384.     envc = 0;
  1385.     keys = PyMapping_Keys(env);
  1386.     vals = PyMapping_Values(env);
  1387.     if (!keys || !vals)
  1388.         goto fail_2;
  1389.     
  1390.     for (pos = 0; pos < i; pos++) {
  1391.         char *p, *k, *v;
  1392.  
  1393.         key = PyList_GetItem(keys, pos);
  1394.         val = PyList_GetItem(vals, pos);
  1395.         if (!key || !val)
  1396.             goto fail_2;
  1397.         
  1398.         if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
  1399.             !PyArg_Parse(val, "s;non-string value in env", &v))
  1400.         {
  1401.             goto fail_2;
  1402.         }
  1403.  
  1404. #if defined(PYOS_OS2)
  1405.         /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
  1406.         if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
  1407. #endif
  1408.         p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
  1409.         if (p == NULL) {
  1410.             PyErr_NoMemory();
  1411.             goto fail_2;
  1412.         }
  1413.         sprintf(p, "%s=%s", k, v);
  1414.         envlist[envc++] = p;
  1415. #if defined(PYOS_OS2)
  1416.     }
  1417. #endif
  1418.     }
  1419.     envlist[envc] = 0;
  1420.  
  1421.  
  1422. #ifdef BAD_EXEC_PROTOTYPES
  1423.     execve(path, (const char **)argvlist, envlist);
  1424. #else /* BAD_EXEC_PROTOTYPES */
  1425.     execve(path, argvlist, envlist);
  1426. #endif /* BAD_EXEC_PROTOTYPES */
  1427.     
  1428.     /* If we get here it's definitely an error */
  1429.  
  1430.     (void) posix_error();
  1431.  
  1432.  fail_2:
  1433.     while (--envc >= 0)
  1434.         PyMem_DEL(envlist[envc]);
  1435.     PyMem_DEL(envlist);
  1436.  fail_1:
  1437.     PyMem_DEL(argvlist);
  1438.     Py_XDECREF(vals);
  1439.     Py_XDECREF(keys);
  1440.     return NULL;
  1441. }
  1442. #endif /* HAVE_EXECV */
  1443.  
  1444.  
  1445. #ifdef HAVE_SPAWNV
  1446. static char posix_spawnv__doc__[] =
  1447. "spawnv(mode, path, args)\n\
  1448. Execute an executable path with arguments, replacing current process.\n\
  1449. \n\
  1450.     mode: mode of process creation\n\
  1451.     path: path of executable file\n\
  1452.     args: tuple or list of strings";
  1453.  
  1454. static PyObject *
  1455. posix_spawnv(self, args)
  1456.     PyObject *self;
  1457.     PyObject *args;
  1458. {
  1459.     char *path;
  1460.     PyObject *argv;
  1461.     char **argvlist;
  1462.     int mode, i, argc;
  1463.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1464.  
  1465.     /* spawnv has three arguments: (mode, path, argv), where
  1466.        argv is a list or tuple of strings. */
  1467.  
  1468.     if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
  1469.         return NULL;
  1470.     if (PyList_Check(argv)) {
  1471.         argc = PyList_Size(argv);
  1472.         getitem = PyList_GetItem;
  1473.     }
  1474.     else if (PyTuple_Check(argv)) {
  1475.         argc = PyTuple_Size(argv);
  1476.         getitem = PyTuple_GetItem;
  1477.     }
  1478.     else {
  1479.  badarg:
  1480.         PyErr_BadArgument();
  1481.         return NULL;
  1482.     }
  1483.  
  1484.     argvlist = PyMem_NEW(char *, argc+1);
  1485.     if (argvlist == NULL)
  1486.         return NULL;
  1487.     for (i = 0; i < argc; i++) {
  1488.         if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
  1489.             PyMem_DEL(argvlist);
  1490.             goto badarg;
  1491.         }
  1492.     }
  1493.     argvlist[argc] = NULL;
  1494.  
  1495.     if (mode == _OLD_P_OVERLAY)
  1496.         mode = _P_OVERLAY;
  1497.     i = _spawnv(mode, path, argvlist);
  1498.  
  1499.     PyMem_DEL(argvlist);
  1500.  
  1501.     if (i == -1)
  1502.         return posix_error();
  1503.     else
  1504.         return Py_BuildValue("i", i);
  1505. }
  1506.  
  1507.  
  1508. static char posix_spawnve__doc__[] =
  1509. "spawnve(mode, path, args, env)\n\
  1510. Execute a path with arguments and environment, replacing current process.\n\
  1511. \n\
  1512.     mode: mode of process creation\n\
  1513.     path: path of executable file\n\
  1514.     args: tuple or list of arguments\n\
  1515.     env: dictonary of strings mapping to strings";
  1516.  
  1517. static PyObject *
  1518. posix_spawnve(self, args)
  1519.     PyObject *self;
  1520.     PyObject *args;
  1521. {
  1522.     char *path;
  1523.     PyObject *argv, *env;
  1524.     char **argvlist;
  1525.     char **envlist;
  1526.     PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
  1527.     int mode, i, pos, argc, envc;
  1528.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1529.  
  1530.     /* spawnve has four arguments: (mode, path, argv, env), where
  1531.        argv is a list or tuple of strings and env is a dictionary
  1532.        like posix.environ. */
  1533.  
  1534.     if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
  1535.         return NULL;
  1536.     if (PyList_Check(argv)) {
  1537.         argc = PyList_Size(argv);
  1538.         getitem = PyList_GetItem;
  1539.     }
  1540.     else if (PyTuple_Check(argv)) {
  1541.         argc = PyTuple_Size(argv);
  1542.         getitem = PyTuple_GetItem;
  1543.     }
  1544.     else {
  1545.         PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
  1546.         return NULL;
  1547.     }
  1548.     if (!PyMapping_Check(env)) {
  1549.         PyErr_SetString(PyExc_TypeError, "env must be mapping object");
  1550.         return NULL;
  1551.     }
  1552.  
  1553.     argvlist = PyMem_NEW(char *, argc+1);
  1554.     if (argvlist == NULL) {
  1555.         PyErr_NoMemory();
  1556.         return NULL;
  1557.     }
  1558.     for (i = 0; i < argc; i++) {
  1559.         if (!PyArg_Parse((*getitem)(argv, i),
  1560.                  "s;argv must be list of strings",
  1561.                  &argvlist[i]))
  1562.         {
  1563.             goto fail_1;
  1564.         }
  1565.     }
  1566.     argvlist[argc] = NULL;
  1567.  
  1568.     i = PyMapping_Length(env);
  1569.     envlist = PyMem_NEW(char *, i + 1);
  1570.     if (envlist == NULL) {
  1571.         PyErr_NoMemory();
  1572.         goto fail_1;
  1573.     }
  1574.     envc = 0;
  1575.     keys = PyMapping_Keys(env);
  1576.     vals = PyMapping_Values(env);
  1577.     if (!keys || !vals)
  1578.         goto fail_2;
  1579.     
  1580.     for (pos = 0; pos < i; pos++) {
  1581.         char *p, *k, *v;
  1582.  
  1583.         key = PyList_GetItem(keys, pos);
  1584.         val = PyList_GetItem(vals, pos);
  1585.         if (!key || !val)
  1586.             goto fail_2;
  1587.         
  1588.         if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
  1589.             !PyArg_Parse(val, "s;non-string value in env", &v))
  1590.         {
  1591.             goto fail_2;
  1592.         }
  1593.         p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
  1594.         if (p == NULL) {
  1595.             PyErr_NoMemory();
  1596.             goto fail_2;
  1597.         }
  1598.         sprintf(p, "%s=%s", k, v);
  1599.         envlist[envc++] = p;
  1600.     }
  1601.     envlist[envc] = 0;
  1602.  
  1603.     if (mode == _OLD_P_OVERLAY)
  1604.         mode = _P_OVERLAY;
  1605.     i = _spawnve(mode, path, argvlist, envlist);
  1606.     if (i == -1)
  1607.         (void) posix_error();
  1608.     else
  1609.         res = Py_BuildValue("i", i);
  1610.  
  1611.  fail_2:
  1612.     while (--envc >= 0)
  1613.         PyMem_DEL(envlist[envc]);
  1614.     PyMem_DEL(envlist);
  1615.  fail_1:
  1616.     PyMem_DEL(argvlist);
  1617.     Py_XDECREF(vals);
  1618.     Py_XDECREF(keys);
  1619.     return res;
  1620. }
  1621. #endif /* HAVE_SPAWNV */
  1622.  
  1623.  
  1624. #ifdef HAVE_FORK
  1625. static char posix_fork__doc__[] =
  1626. "fork() -> pid\n\
  1627. Fork a child process.\n\
  1628. \n\
  1629. Return 0 to child process and PID of child to parent process.";
  1630.  
  1631. static PyObject *
  1632. posix_fork(self, args)
  1633.     PyObject *self;
  1634.     PyObject *args;
  1635. {
  1636.     int pid;
  1637.     if (!PyArg_NoArgs(args))
  1638.         return NULL;
  1639.     pid = fork();
  1640.     if (pid == -1)
  1641.         return posix_error();
  1642.     PyOS_AfterFork();
  1643.     return PyInt_FromLong((long)pid);
  1644. }
  1645. #endif
  1646.  
  1647.  
  1648. #ifdef HAVE_GETEGID
  1649. static char posix_getegid__doc__[] =
  1650. "getegid() -> egid\n\
  1651. Return the current process's effective group id.";
  1652.  
  1653. static PyObject *
  1654. posix_getegid(self, args)
  1655.     PyObject *self;
  1656.     PyObject *args;
  1657. {
  1658.     if (!PyArg_NoArgs(args))
  1659.         return NULL;
  1660.     return PyInt_FromLong((long)getegid());
  1661. }
  1662. #endif
  1663.  
  1664.  
  1665. #ifdef HAVE_GETEUID
  1666. static char posix_geteuid__doc__[] =
  1667. "geteuid() -> euid\n\
  1668. Return the current process's effective user id.";
  1669.  
  1670. static PyObject *
  1671. posix_geteuid(self, args)
  1672.     PyObject *self;
  1673.     PyObject *args;
  1674. {
  1675.     if (!PyArg_NoArgs(args))
  1676.         return NULL;
  1677.     return PyInt_FromLong((long)geteuid());
  1678. }
  1679. #endif
  1680.  
  1681.  
  1682. #ifdef HAVE_GETGID
  1683. static char posix_getgid__doc__[] =
  1684. "getgid() -> gid\n\
  1685. Return the current process's group id.";
  1686.  
  1687. static PyObject *
  1688. posix_getgid(self, args)
  1689.     PyObject *self;
  1690.     PyObject *args;
  1691. {
  1692.     if (!PyArg_NoArgs(args))
  1693.         return NULL;
  1694.     return PyInt_FromLong((long)getgid());
  1695. }
  1696. #endif
  1697.  
  1698.  
  1699. static char posix_getpid__doc__[] =
  1700. "getpid() -> pid\n\
  1701. Return the current process id";
  1702.  
  1703. static PyObject *
  1704. posix_getpid(self, args)
  1705.     PyObject *self;
  1706.     PyObject *args;
  1707. {
  1708.     if (!PyArg_NoArgs(args))
  1709.         return NULL;
  1710.     return PyInt_FromLong((long)getpid());
  1711. }
  1712.  
  1713.  
  1714. #ifdef HAVE_GETPGRP
  1715. static char posix_getpgrp__doc__[] =
  1716. "getpgrp() -> pgrp\n\
  1717. Return the current process group id.";
  1718.  
  1719. static PyObject *
  1720. posix_getpgrp(self, args)
  1721.     PyObject *self;
  1722.     PyObject *args;
  1723. {
  1724.     if (!PyArg_NoArgs(args))
  1725.         return NULL;
  1726. #ifdef GETPGRP_HAVE_ARG
  1727.     return PyInt_FromLong((long)getpgrp(0));
  1728. #else /* GETPGRP_HAVE_ARG */
  1729.     return PyInt_FromLong((long)getpgrp());
  1730. #endif /* GETPGRP_HAVE_ARG */
  1731. }
  1732. #endif /* HAVE_GETPGRP */
  1733.  
  1734.  
  1735. #ifdef HAVE_SETPGRP
  1736. static char posix_setpgrp__doc__[] =
  1737. "setpgrp() -> None\n\
  1738. Make this process a session leader.";
  1739.  
  1740. static PyObject *
  1741. posix_setpgrp(self, args)
  1742.     PyObject *self;
  1743.     PyObject *args;
  1744. {
  1745.     if (!PyArg_NoArgs(args))
  1746.         return NULL;
  1747. #ifdef SETPGRP_HAVE_ARG
  1748.     if (setpgrp(0, 0) < 0)
  1749. #else /* SETPGRP_HAVE_ARG */
  1750.     if (setpgrp() < 0)
  1751. #endif /* SETPGRP_HAVE_ARG */
  1752.         return posix_error();
  1753.     Py_INCREF(Py_None);
  1754.     return Py_None;
  1755. }
  1756.  
  1757. #endif /* HAVE_SETPGRP */
  1758.  
  1759. #ifdef HAVE_GETPPID
  1760. static char posix_getppid__doc__[] =
  1761. "getppid() -> ppid\n\
  1762. Return the parent's process id.";
  1763.  
  1764. static PyObject *
  1765. posix_getppid(self, args)
  1766.     PyObject *self;
  1767.     PyObject *args;
  1768. {
  1769.     if (!PyArg_NoArgs(args))
  1770.         return NULL;
  1771.     return PyInt_FromLong((long)getppid());
  1772. }
  1773. #endif
  1774.  
  1775.  
  1776. #ifdef HAVE_GETUID
  1777. static char posix_getuid__doc__[] =
  1778. "getuid() -> uid\n\
  1779. Return the current process's user id.";
  1780.  
  1781. static PyObject *
  1782. posix_getuid(self, args)
  1783.     PyObject *self;
  1784.     PyObject *args;
  1785. {
  1786.     if (!PyArg_NoArgs(args))
  1787.         return NULL;
  1788.     return PyInt_FromLong((long)getuid());
  1789. }
  1790. #endif
  1791.  
  1792.  
  1793. #ifdef HAVE_KILL
  1794. static char posix_kill__doc__[] =
  1795. "kill(pid, sig) -> None\n\
  1796. Kill a process with a signal.";
  1797.  
  1798. static PyObject *
  1799. posix_kill(self, args)
  1800.     PyObject *self;
  1801.     PyObject *args;
  1802. {
  1803.     int pid, sig;
  1804.     if (!PyArg_Parse(args, "(ii)", &pid, &sig))
  1805.         return NULL;
  1806. #if defined(PYOS_OS2)
  1807.     if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
  1808.         APIRET rc;
  1809.         if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
  1810.             return os2_error(rc);
  1811.  
  1812.     } else if (sig == XCPT_SIGNAL_KILLPROC) {
  1813.         APIRET rc;
  1814.         if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
  1815.             return os2_error(rc);
  1816.  
  1817.     } else
  1818.         return NULL; /* Unrecognized Signal Requested */
  1819. #else
  1820.     if (kill(pid, sig) == -1)
  1821.         return posix_error();
  1822. #endif
  1823.     Py_INCREF(Py_None);
  1824.     return Py_None;
  1825. }
  1826. #endif
  1827.  
  1828. #ifdef HAVE_PLOCK
  1829.  
  1830. #ifdef HAVE_SYS_LOCK_H
  1831. #include <sys/lock.h>
  1832. #endif
  1833.  
  1834. static char posix_plock__doc__[] =
  1835. "plock(op) -> None\n\
  1836. Lock program segments into memory.";
  1837.  
  1838. static PyObject *
  1839. posix_plock(self, args)
  1840.     PyObject *self;
  1841.     PyObject *args;
  1842. {
  1843.     int op;
  1844.     if (!PyArg_Parse(args, "i", &op))
  1845.         return NULL;
  1846.     if (plock(op) == -1)
  1847.         return posix_error();
  1848.     Py_INCREF(Py_None);
  1849.     return Py_None;
  1850. }
  1851. #endif
  1852.  
  1853.  
  1854. #ifdef HAVE_POPEN
  1855. static char posix_popen__doc__[] =
  1856. "popen(command [, mode='r' [, bufsize]]) -> pipe\n\
  1857. Open a pipe to/from a command returning a file object.";
  1858.  
  1859. #if defined(PYOS_OS2)
  1860. static int
  1861. async_system(const char *command)
  1862. {
  1863.     char        *p, errormsg[256], args[1024];
  1864.     RESULTCODES  rcodes;
  1865.     APIRET       rc;
  1866.     char        *shell = getenv("COMSPEC");
  1867.     if (!shell)
  1868.         shell = "cmd";
  1869.  
  1870.     strcpy(args, shell);
  1871.     p = &args[ strlen(args)+1 ];
  1872.     strcpy(p, "/c ");
  1873.     strcat(p, command);
  1874.     p += strlen(p) + 1;
  1875.     *p = '\0';
  1876.  
  1877.     rc = DosExecPgm(errormsg, sizeof(errormsg),
  1878.                     EXEC_ASYNC, /* Execute Async w/o Wait for Results */
  1879.                     args,
  1880.                     NULL,       /* Inherit Parent's Environment */
  1881.                     &rcodes, shell);
  1882.     return rc;
  1883. }
  1884.  
  1885. static FILE *
  1886. popen(const char *command, const char *mode, int pipesize, int *err)
  1887. {
  1888.     HFILE    rhan, whan;
  1889.     FILE    *retfd = NULL;
  1890.     APIRET   rc = DosCreatePipe(&rhan, &whan, pipesize);
  1891.  
  1892.     if (rc != NO_ERROR) {
  1893.     *err = rc;
  1894.         return NULL; /* ERROR - Unable to Create Anon Pipe */
  1895.     }
  1896.  
  1897.     if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
  1898.         int oldfd = dup(1);      /* Save STDOUT Handle in Another Handle */
  1899.  
  1900.         DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
  1901.         close(1);                /* Make STDOUT Available for Reallocation */
  1902.  
  1903.         if (dup2(whan, 1) == 0) {      /* Connect STDOUT to Pipe Write Side */
  1904.             DosClose(whan);            /* Close Now-Unused Pipe Write Handle */
  1905.  
  1906.             if (async_system(command) == NO_ERROR)
  1907.                 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
  1908.         }
  1909.  
  1910.         dup2(oldfd, 1);          /* Reconnect STDOUT to Original Handle */
  1911.         DosExitCritSec();        /* Now Allow Other Threads to Run */
  1912.  
  1913.         close(oldfd);            /* And Close Saved STDOUT Handle */
  1914.         return retfd;            /* Return fd of Pipe or NULL if Error */
  1915.  
  1916.     } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
  1917.         int oldfd = dup(0);      /* Save STDIN Handle in Another Handle */
  1918.  
  1919.         DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
  1920.         close(0);                /* Make STDIN Available for Reallocation */
  1921.  
  1922.         if (dup2(rhan, 0) == 0)     { /* Connect STDIN to Pipe Read Side */
  1923.             DosClose(rhan);           /* Close Now-Unused Pipe Read Handle */
  1924.  
  1925.             if (async_system(command) == NO_ERROR)
  1926.                 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
  1927.         }
  1928.  
  1929.         dup2(oldfd, 0);          /* Reconnect STDIN to Original Handle */
  1930.         DosExitCritSec();        /* Now Allow Other Threads to Run */
  1931.  
  1932.         close(oldfd);            /* And Close Saved STDIN Handle */
  1933.         return retfd;            /* Return fd of Pipe or NULL if Error */
  1934.  
  1935.     } else {
  1936.     *err = ERROR_INVALID_ACCESS;
  1937.         return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
  1938.     }
  1939. }
  1940.  
  1941. static PyObject *
  1942. posix_popen(self, args)
  1943.     PyObject *self;
  1944.     PyObject *args;
  1945. {
  1946.     char *name;
  1947.     char *mode = "r";
  1948.     int   err, bufsize = -1;
  1949.     FILE *fp;
  1950.     PyObject *f;
  1951.     if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
  1952.         return NULL;
  1953.     Py_BEGIN_ALLOW_THREADS
  1954.     fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
  1955.     Py_END_ALLOW_THREADS
  1956.     if (fp == NULL)
  1957.         return os2_error(err);
  1958.  
  1959.     f = PyFile_FromFile(fp, name, mode, fclose);
  1960.     if (f != NULL)
  1961.         PyFile_SetBufSize(f, bufsize);
  1962.     return f;
  1963. }
  1964.  
  1965. #else
  1966. static PyObject *
  1967. posix_popen(self, args)
  1968.     PyObject *self;
  1969.     PyObject *args;
  1970. {
  1971.     char *name;
  1972.     char *mode = "r";
  1973.     int bufsize = -1;
  1974.     FILE *fp;
  1975.     PyObject *f;
  1976.     if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
  1977.         return NULL;
  1978.     Py_BEGIN_ALLOW_THREADS
  1979.     fp = popen(name, mode);
  1980.     Py_END_ALLOW_THREADS
  1981.     if (fp == NULL)
  1982.         return posix_error();
  1983.     f = PyFile_FromFile(fp, name, mode, pclose);
  1984.     if (f != NULL)
  1985.         PyFile_SetBufSize(f, bufsize);
  1986.     return f;
  1987. }
  1988. #endif
  1989.  
  1990. #endif /* HAVE_POPEN */
  1991.  
  1992.  
  1993. #ifdef HAVE_SETUID
  1994. static char posix_setuid__doc__[] =
  1995. "setuid(uid) -> None\n\
  1996. Set the current process's user id.";
  1997. static PyObject *
  1998. posix_setuid(self, args)
  1999.     PyObject *self;
  2000.     PyObject *args;
  2001. {
  2002.     int uid;
  2003.     if (!PyArg_Parse(args, "i", &uid))
  2004.         return NULL;
  2005.     if (setuid(uid) < 0)
  2006.         return posix_error();
  2007.     Py_INCREF(Py_None);
  2008.     return Py_None;
  2009. }
  2010. #endif /* HAVE_SETUID */
  2011.  
  2012.  
  2013. #ifdef HAVE_SETGID
  2014. static char posix_setgid__doc__[] =
  2015. "setgid(gid) -> None\n\
  2016. Set the current process's group id.";
  2017.  
  2018. static PyObject *
  2019. posix_setgid(self, args)
  2020.     PyObject *self;
  2021.     PyObject *args;
  2022. {
  2023.     int gid;
  2024.     if (!PyArg_Parse(args, "i", &gid))
  2025.         return NULL;
  2026.     if (setgid(gid) < 0)
  2027.         return posix_error();
  2028.     Py_INCREF(Py_None);
  2029.     return Py_None;
  2030. }
  2031. #endif /* HAVE_SETGID */
  2032.  
  2033.  
  2034. #ifdef HAVE_WAITPID
  2035. static char posix_waitpid__doc__[] =
  2036. "waitpid(pid, options) -> (pid, status)\n\
  2037. Wait for completion of a give child process.";
  2038.  
  2039. static PyObject *
  2040. posix_waitpid(self, args)
  2041.     PyObject *self;
  2042.     PyObject *args;
  2043. {
  2044.     int pid, options;
  2045. #ifdef UNION_WAIT
  2046.     union wait status;
  2047. #define status_i (status.w_status)
  2048. #else
  2049.     int status;
  2050. #define status_i status
  2051. #endif
  2052.     status_i = 0;
  2053.  
  2054.     if (!PyArg_Parse(args, "(ii)", &pid, &options))
  2055.         return NULL;
  2056.     Py_BEGIN_ALLOW_THREADS
  2057. #ifdef NeXT
  2058.     pid = wait4(pid, &status, options, NULL);
  2059. #else
  2060.     pid = waitpid(pid, &status, options);
  2061. #endif
  2062.     Py_END_ALLOW_THREADS
  2063.     if (pid == -1)
  2064.         return posix_error();
  2065.     else
  2066.         return Py_BuildValue("ii", pid, status_i);
  2067. }
  2068. #endif /* HAVE_WAITPID */
  2069.  
  2070.  
  2071. #ifdef HAVE_WAIT
  2072. static char posix_wait__doc__[] =
  2073. "wait() -> (pid, status)\n\
  2074. Wait for completion of a child process.";
  2075.  
  2076. static PyObject *
  2077. posix_wait(self, args)
  2078.     PyObject *self;
  2079.     PyObject *args;
  2080. {
  2081.     int pid, sts;
  2082. #ifdef UNION_WAIT
  2083.     union wait status;
  2084. #define status_i (status.w_status)
  2085. #else
  2086.     int status;
  2087. #define status_i status
  2088. #endif
  2089.     status_i = 0;
  2090.     Py_BEGIN_ALLOW_THREADS
  2091.     pid = wait(&status);
  2092.     Py_END_ALLOW_THREADS
  2093.     if (pid == -1)
  2094.         return posix_error();
  2095.     else
  2096.         return Py_BuildValue("ii", pid, status_i);
  2097. }
  2098. #endif
  2099.  
  2100.  
  2101. static char posix_lstat__doc__[] =
  2102. "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
  2103. Like stat(path), but do not follow symbolic links.";
  2104.  
  2105. static PyObject *
  2106. posix_lstat(self, args)
  2107.     PyObject *self;
  2108.     PyObject *args;
  2109. {
  2110. #ifdef HAVE_LSTAT
  2111.     return posix_do_stat(self, args, lstat);
  2112. #else /* !HAVE_LSTAT */
  2113.     return posix_do_stat(self, args, stat);
  2114. #endif /* !HAVE_LSTAT */
  2115. }
  2116.  
  2117.  
  2118. #ifdef HAVE_READLINK
  2119. static char posix_readlink__doc__[] =
  2120. "readlink(path) -> path\n\
  2121. Return a string representing the path to which the symbolic link points.";
  2122.  
  2123. static PyObject *
  2124. posix_readlink(self, args)
  2125.     PyObject *self;
  2126.     PyObject *args;
  2127. {
  2128.     char buf[MAXPATHLEN];
  2129.     char *path;
  2130.     int n;
  2131.     if (!PyArg_Parse(args, "s", &path))
  2132.         return NULL;
  2133.     Py_BEGIN_ALLOW_THREADS
  2134.     n = readlink(path, buf, (int) sizeof buf);
  2135.     Py_END_ALLOW_THREADS
  2136.     if (n < 0)
  2137.         return posix_error_with_filename(path);
  2138.     return PyString_FromStringAndSize(buf, n);
  2139. }
  2140. #endif /* HAVE_READLINK */
  2141.  
  2142.  
  2143. #ifdef HAVE_SYMLINK
  2144. static char posix_symlink__doc__[] =
  2145. "symlink(src, dst) -> None\n\
  2146. Create a symbolic link.";
  2147.  
  2148. static PyObject *
  2149. posix_symlink(self, args)
  2150.     PyObject *self;
  2151.     PyObject *args;
  2152. {
  2153.     return posix_2str(args, symlink);
  2154. }
  2155. #endif /* HAVE_SYMLINK */
  2156.  
  2157.  
  2158. #ifdef HAVE_TIMES
  2159. #ifndef HZ
  2160. #define HZ 60 /* Universal constant :-) */
  2161. #endif /* HZ */
  2162.     
  2163. #if defined(PYCC_VACPP) && defined(PYOS_OS2)
  2164. static long
  2165. system_uptime()
  2166. {
  2167.     ULONG     value = 0;
  2168.  
  2169.     Py_BEGIN_ALLOW_THREADS
  2170.     DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
  2171.     Py_END_ALLOW_THREADS
  2172.  
  2173.     return value;
  2174. }
  2175.  
  2176. static PyObject *
  2177. posix_times(self, args)
  2178.     PyObject *self;
  2179.     PyObject *args;
  2180. {
  2181.     if (!PyArg_NoArgs(args))
  2182.         return NULL;
  2183.  
  2184.     /* Currently Only Uptime is Provided -- Others Later */
  2185.     return Py_BuildValue("ddddd",
  2186.                  (double)0 /* t.tms_utime / HZ */,
  2187.                  (double)0 /* t.tms_stime / HZ */,
  2188.                  (double)0 /* t.tms_cutime / HZ */,
  2189.                  (double)0 /* t.tms_cstime / HZ */,
  2190.                  (double)system_uptime() / 1000);
  2191. }
  2192. #else /* not OS2 */
  2193. static PyObject *
  2194. posix_times(self, args)
  2195.     PyObject *self;
  2196.     PyObject *args;
  2197. {
  2198.     struct tms t;
  2199.     clock_t c;
  2200.     if (!PyArg_NoArgs(args))
  2201.         return NULL;
  2202.     errno = 0;
  2203.     c = times(&t);
  2204.     if (c == (clock_t) -1)
  2205.         return posix_error();
  2206.     return Py_BuildValue("ddddd",
  2207.                  (double)t.tms_utime / HZ,
  2208.                  (double)t.tms_stime / HZ,
  2209.                  (double)t.tms_cutime / HZ,
  2210.                  (double)t.tms_cstime / HZ,
  2211.                  (double)c / HZ);
  2212. }
  2213. #endif /* not OS2 */
  2214. #endif /* HAVE_TIMES */
  2215.  
  2216.  
  2217. #ifdef MS_WIN32
  2218. #define HAVE_TIMES    /* so the method table will pick it up */
  2219. static PyObject *
  2220. posix_times(self, args)
  2221.     PyObject *self;
  2222.     PyObject *args;
  2223. {
  2224.     FILETIME create, exit, kernel, user;
  2225.     HANDLE hProc;
  2226.     if (!PyArg_NoArgs(args))
  2227.         return NULL;
  2228.     hProc = GetCurrentProcess();
  2229.     GetProcessTimes(hProc, &create, &exit, &kernel, &user);
  2230.     /* The fields of a FILETIME structure are the hi and lo part
  2231.        of a 64-bit value expressed in 100 nanosecond units.
  2232.        1e7 is one second in such units; 1e-7 the inverse.
  2233.        429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
  2234.     */
  2235.     return Py_BuildValue(
  2236.         "ddddd",
  2237.         (double)(kernel.dwHighDateTime*429.4967296 +
  2238.                  kernel.dwLowDateTime*1e-7),
  2239.         (double)(user.dwHighDateTime*429.4967296 +
  2240.                  user.dwLowDateTime*1e-7),
  2241.         (double)0,
  2242.         (double)0,
  2243.         (double)0);
  2244. }
  2245. #endif /* MS_WIN32 */
  2246.  
  2247. #ifdef HAVE_TIMES
  2248. static char posix_times__doc__[] =
  2249. "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
  2250. Return a tuple of floating point numbers indicating process times.";
  2251. #endif
  2252.  
  2253.  
  2254. #ifdef HAVE_SETSID
  2255. static char posix_setsid__doc__[] =
  2256. "setsid() -> None\n\
  2257. Call the system call setsid().";
  2258.  
  2259. static PyObject *
  2260. posix_setsid(self, args)
  2261.     PyObject *self;
  2262.     PyObject *args;
  2263. {
  2264.     if (!PyArg_NoArgs(args))
  2265.         return NULL;
  2266.     if (setsid() < 0)
  2267.         return posix_error();
  2268.     Py_INCREF(Py_None);
  2269.     return Py_None;
  2270. }
  2271. #endif /* HAVE_SETSID */
  2272.  
  2273. #ifdef HAVE_SETPGID
  2274. static char posix_setpgid__doc__[] =
  2275. "setpgid(pid, pgrp) -> None\n\
  2276. Call the system call setpgid().";
  2277.  
  2278. static PyObject *
  2279. posix_setpgid(self, args)
  2280.     PyObject *self;
  2281.     PyObject *args;
  2282. {
  2283.     int pid, pgrp;
  2284.     if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
  2285.         return NULL;
  2286.     if (setpgid(pid, pgrp) < 0)
  2287.         return posix_error();
  2288.     Py_INCREF(Py_None);
  2289.     return Py_None;
  2290. }
  2291. #endif /* HAVE_SETPGID */
  2292.  
  2293.  
  2294. #ifdef HAVE_TCGETPGRP
  2295. static char posix_tcgetpgrp__doc__[] =
  2296. "tcgetpgrp(fd) -> pgid\n\
  2297. Return the process group associated with the terminal given by a fd.";
  2298.  
  2299. static PyObject *
  2300. posix_tcgetpgrp(self, args)
  2301.     PyObject *self;
  2302.     PyObject *args;
  2303. {
  2304.     int fd, pgid;
  2305.     if (!PyArg_Parse(args, "i", &fd))
  2306.         return NULL;
  2307.     pgid = tcgetpgrp(fd);
  2308.     if (pgid < 0)
  2309.         return posix_error();
  2310.     return PyInt_FromLong((long)pgid);
  2311. }
  2312. #endif /* HAVE_TCGETPGRP */
  2313.  
  2314.  
  2315. #ifdef HAVE_TCSETPGRP
  2316. static char posix_tcsetpgrp__doc__[] =
  2317. "tcsetpgrp(fd, pgid) -> None\n\
  2318. Set the process group associated with the terminal given by a fd.";
  2319.  
  2320. static PyObject *
  2321. posix_tcsetpgrp(self, args)
  2322.     PyObject *self;
  2323.     PyObject *args;
  2324. {
  2325.     int fd, pgid;
  2326.     if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
  2327.         return NULL;
  2328.     if (tcsetpgrp(fd, pgid) < 0)
  2329.         return posix_error();
  2330.     Py_INCREF(Py_None);
  2331.     return Py_None;
  2332. }
  2333. #endif /* HAVE_TCSETPGRP */
  2334.  
  2335. /* Functions acting on file descriptors */
  2336.  
  2337. static char posix_open__doc__[] =
  2338. "open(filename, flag [, mode=0777]) -> fd\n\
  2339. Open a file (for low level IO).";
  2340.  
  2341. static PyObject *
  2342. posix_open(self, args)
  2343.     PyObject *self;
  2344.     PyObject *args;
  2345. {
  2346.     char *file;
  2347.     int flag;
  2348.     int mode = 0777;
  2349.     int fd;
  2350.     if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
  2351.         return NULL;
  2352.  
  2353.     Py_BEGIN_ALLOW_THREADS
  2354.     fd = open(file, flag, mode);
  2355.     Py_END_ALLOW_THREADS
  2356.     if (fd < 0)
  2357.         return posix_error_with_filename(file);
  2358.     return PyInt_FromLong((long)fd);
  2359. }
  2360.  
  2361.  
  2362. static char posix_close__doc__[] =
  2363. "close(fd) -> None\n\
  2364. Close a file descriptor (for low level IO).";
  2365.  
  2366. static PyObject *
  2367. posix_close(self, args)
  2368.     PyObject *self;
  2369.     PyObject *args;
  2370. {
  2371.     int fd, res;
  2372.     if (!PyArg_Parse(args, "i", &fd))
  2373.         return NULL;
  2374.     Py_BEGIN_ALLOW_THREADS
  2375.     res = close(fd);
  2376.     Py_END_ALLOW_THREADS
  2377.     if (res < 0)
  2378.         return posix_error();
  2379.     Py_INCREF(Py_None);
  2380.     return Py_None;
  2381. }
  2382.  
  2383.  
  2384. static char posix_dup__doc__[] =
  2385. "dup(fd) -> fd2\n\
  2386. Return a duplicate of a file descriptor.";
  2387.  
  2388. static PyObject *
  2389. posix_dup(self, args)
  2390.     PyObject *self;
  2391.     PyObject *args;
  2392. {
  2393.     int fd;
  2394.     if (!PyArg_Parse(args, "i", &fd))
  2395.         return NULL;
  2396.     Py_BEGIN_ALLOW_THREADS
  2397.     fd = dup(fd);
  2398.     Py_END_ALLOW_THREADS
  2399.     if (fd < 0)
  2400.         return posix_error();
  2401.     return PyInt_FromLong((long)fd);
  2402. }
  2403.  
  2404.  
  2405. static char posix_dup2__doc__[] =
  2406. "dup2(fd, fd2) -> None\n\
  2407. Duplicate file descriptor.";
  2408.  
  2409. static PyObject *
  2410. posix_dup2(self, args)
  2411.     PyObject *self;
  2412.     PyObject *args;
  2413. {
  2414.     int fd, fd2, res;
  2415.     if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
  2416.         return NULL;
  2417.     Py_BEGIN_ALLOW_THREADS
  2418.     res = dup2(fd, fd2);
  2419.     Py_END_ALLOW_THREADS
  2420.     if (res < 0)
  2421.         return posix_error();
  2422.     Py_INCREF(Py_None);
  2423.     return Py_None;
  2424. }
  2425.  
  2426.  
  2427. static char posix_lseek__doc__[] =
  2428. "lseek(fd, pos, how) -> newpos\n\
  2429. Set the current position of a file descriptor.";
  2430.  
  2431. static PyObject *
  2432. posix_lseek(self, args)
  2433.     PyObject *self;
  2434.     PyObject *args;
  2435. {
  2436.     int fd, how;
  2437.     off_t pos, res;
  2438.     PyObject *posobj;
  2439.     if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
  2440.         return NULL;
  2441. #ifdef SEEK_SET
  2442.     /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
  2443.     switch (how) {
  2444.     case 0: how = SEEK_SET; break;
  2445.     case 1: how = SEEK_CUR; break;
  2446.     case 2: how = SEEK_END; break;
  2447.     }
  2448. #endif /* SEEK_END */
  2449.  
  2450. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2451.     pos = PyInt_AsLong(posobj);
  2452. #else
  2453.     pos = PyLong_Check(posobj) ?
  2454.         PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
  2455. #endif
  2456.     if (PyErr_Occurred())
  2457.         return NULL;
  2458.  
  2459.     Py_BEGIN_ALLOW_THREADS
  2460.     res = lseek(fd, pos, how);
  2461.     Py_END_ALLOW_THREADS
  2462.     if (res < 0)
  2463.         return posix_error();
  2464.  
  2465. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2466.     return PyInt_FromLong(res);
  2467. #else
  2468.     return PyLong_FromLongLong(res);
  2469. #endif
  2470. }
  2471.  
  2472.  
  2473. static char posix_read__doc__[] =
  2474. "read(fd, buffersize) -> string\n\
  2475. Read a file descriptor.";
  2476.  
  2477. static PyObject *
  2478. posix_read(self, args)
  2479.     PyObject *self;
  2480.     PyObject *args;
  2481. {
  2482.     int fd, size, n;
  2483.     PyObject *buffer;
  2484.     if (!PyArg_Parse(args, "(ii)", &fd, &size))
  2485.         return NULL;
  2486.     buffer = PyString_FromStringAndSize((char *)NULL, size);
  2487.     if (buffer == NULL)
  2488.         return NULL;
  2489.     Py_BEGIN_ALLOW_THREADS
  2490.     n = read(fd, PyString_AsString(buffer), size);
  2491.     Py_END_ALLOW_THREADS
  2492.     if (n < 0) {
  2493.         Py_DECREF(buffer);
  2494.         return posix_error();
  2495.     }
  2496.     if (n != size)
  2497.         _PyString_Resize(&buffer, n);
  2498.     return buffer;
  2499. }
  2500.  
  2501.  
  2502. static char posix_write__doc__[] =
  2503. "write(fd, string) -> byteswritten\n\
  2504. Write a string to a file descriptor.";
  2505.  
  2506. static PyObject *
  2507. posix_write(self, args)
  2508.     PyObject *self;
  2509.     PyObject *args;
  2510. {
  2511.     int fd, size;
  2512.     char *buffer;
  2513.     if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
  2514.         return NULL;
  2515.     Py_BEGIN_ALLOW_THREADS
  2516.     size = write(fd, buffer, size);
  2517.     Py_END_ALLOW_THREADS
  2518.     if (size < 0)
  2519.         return posix_error();
  2520.     return PyInt_FromLong((long)size);
  2521. }
  2522.  
  2523.  
  2524. static char posix_fstat__doc__[]=
  2525. "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
  2526. Like stat(), but for an open file descriptor.";
  2527.  
  2528. static PyObject *
  2529. posix_fstat(self, args)
  2530.     PyObject *self;
  2531.     PyObject *args;
  2532. {
  2533.     int fd;
  2534.     struct stat st;
  2535.     int res;
  2536.     if (!PyArg_Parse(args, "i", &fd))
  2537.         return NULL;
  2538.     Py_BEGIN_ALLOW_THREADS
  2539.     res = fstat(fd, &st);
  2540.     Py_END_ALLOW_THREADS
  2541.     if (res != 0)
  2542.         return posix_error();
  2543. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2544.     return Py_BuildValue("(llllllllll)",
  2545.                  (long)st.st_mode,
  2546.                  (long)st.st_ino,
  2547.                  (long)st.st_dev,
  2548.                  (long)st.st_nlink,
  2549.                  (long)st.st_uid,
  2550.                  (long)st.st_gid,
  2551.                  (long)st.st_size,
  2552.                  (long)st.st_atime,
  2553.                  (long)st.st_mtime,
  2554.                  (long)st.st_ctime);
  2555. #else
  2556.     return Py_BuildValue("(lLllllLlll)",
  2557.                  (long)st.st_mode,
  2558.                  (LONG_LONG)st.st_ino,
  2559.                  (long)st.st_dev,
  2560.                  (long)st.st_nlink,
  2561.                  (long)st.st_uid,
  2562.                  (long)st.st_gid,
  2563.                  (LONG_LONG)st.st_size,
  2564.                  (long)st.st_atime,
  2565.                  (long)st.st_mtime,
  2566.                  (long)st.st_ctime);
  2567. #endif
  2568. }
  2569.  
  2570.  
  2571. static char posix_fdopen__doc__[] =
  2572. "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
  2573. Return an open file object connected to a file descriptor.";
  2574.  
  2575. static PyObject *
  2576. posix_fdopen(self, args)
  2577.     PyObject *self;
  2578.     PyObject *args;
  2579. {
  2580.     extern int fclose Py_PROTO((FILE *));
  2581.     int fd;
  2582.     char *mode = "r";
  2583.     int bufsize = -1;
  2584.     FILE *fp;
  2585.     PyObject *f;
  2586.     if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
  2587.         return NULL;
  2588.  
  2589.     Py_BEGIN_ALLOW_THREADS
  2590.     fp = fdopen(fd, mode);
  2591.     Py_END_ALLOW_THREADS
  2592.     if (fp == NULL)
  2593.         return posix_error();
  2594.     f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
  2595.     if (f != NULL)
  2596.         PyFile_SetBufSize(f, bufsize);
  2597.     return f;
  2598. }
  2599.  
  2600.  
  2601. #ifdef HAVE_PIPE
  2602. static char posix_pipe__doc__[] =
  2603. "pipe() -> (read_end, write_end)\n\
  2604. Create a pipe.";
  2605.  
  2606. static PyObject *
  2607. posix_pipe(self, args)
  2608.     PyObject *self;
  2609.     PyObject *args;
  2610. {
  2611. #if defined(PYOS_OS2)
  2612.     HFILE read, write;
  2613.     APIRET rc;
  2614.  
  2615.     if (!PyArg_Parse(args, ""))
  2616.         return NULL;
  2617.  
  2618.     Py_BEGIN_ALLOW_THREADS
  2619.     rc = DosCreatePipe( &read, &write, 4096);
  2620.     Py_END_ALLOW_THREADS
  2621.     if (rc != NO_ERROR)
  2622.         return os2_error(rc);
  2623.  
  2624.     return Py_BuildValue("(ii)", read, write);
  2625. #else
  2626. #if !defined(MS_WIN32)
  2627.     int fds[2];
  2628.     int res;
  2629.     if (!PyArg_Parse(args, ""))
  2630.         return NULL;
  2631.     Py_BEGIN_ALLOW_THREADS
  2632.     res = pipe(fds);
  2633.     Py_END_ALLOW_THREADS
  2634.     if (res != 0)
  2635.         return posix_error();
  2636.     return Py_BuildValue("(ii)", fds[0], fds[1]);
  2637. #else /* MS_WIN32 */
  2638.     HANDLE read, write;
  2639.     int read_fd, write_fd;
  2640.     BOOL ok;
  2641.     if (!PyArg_Parse(args, ""))
  2642.         return NULL;
  2643.     Py_BEGIN_ALLOW_THREADS
  2644.     ok = CreatePipe(&read, &write, NULL, 0);
  2645.     Py_END_ALLOW_THREADS
  2646.     if (!ok)
  2647.         return posix_error();
  2648.     read_fd = _open_osfhandle((long)read, 0);
  2649.     write_fd = _open_osfhandle((long)write, 1);
  2650.     return Py_BuildValue("(ii)", read_fd, write_fd);
  2651. #endif /* MS_WIN32 */
  2652. #endif
  2653. }
  2654. #endif  /* HAVE_PIPE */
  2655.  
  2656.  
  2657. #ifdef HAVE_MKFIFO
  2658. static char posix_mkfifo__doc__[] =
  2659. "mkfifo(file, [, mode=0666]) -> None\n\
  2660. Create a FIFO (a POSIX named pipe).";
  2661.  
  2662. static PyObject *
  2663. posix_mkfifo(self, args)
  2664.     PyObject *self;
  2665.     PyObject *args;
  2666. {
  2667.     char *file;
  2668.     int mode = 0666;
  2669.     int res;
  2670.     if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
  2671.         return NULL;
  2672.     Py_BEGIN_ALLOW_THREADS
  2673.     res = mkfifo(file, mode);
  2674.     Py_END_ALLOW_THREADS
  2675.     if (res < 0)
  2676.         return posix_error();
  2677.     Py_INCREF(Py_None);
  2678.     return Py_None;
  2679. }
  2680. #endif
  2681.  
  2682.  
  2683. #ifdef HAVE_FTRUNCATE
  2684. static char posix_ftruncate__doc__[] =
  2685. "ftruncate(fd, length) -> None\n\
  2686. Truncate a file to a specified length.";
  2687.  
  2688. static PyObject *
  2689. posix_ftruncate(self, args)
  2690.     PyObject *self; /* Not used */
  2691.     PyObject *args;
  2692. {
  2693.     int fd;
  2694.     off_t length;
  2695.     int res;
  2696.     PyObject *lenobj;
  2697.  
  2698.     if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
  2699.         return NULL;
  2700.  
  2701. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2702.     length = PyInt_AsLong(lenobj);
  2703. #else
  2704.     length = PyLong_Check(lenobj) ?
  2705.         PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
  2706. #endif
  2707.     if (PyErr_Occurred())
  2708.         return NULL;
  2709.  
  2710.     Py_BEGIN_ALLOW_THREADS
  2711.     res = ftruncate(fd, length);
  2712.     Py_END_ALLOW_THREADS
  2713.     if (res < 0) {
  2714.         PyErr_SetFromErrno(PyExc_IOError);
  2715.         return NULL;
  2716.     }
  2717.     Py_INCREF(Py_None);
  2718.     return Py_None;
  2719. }
  2720. #endif
  2721.  
  2722. #ifdef NeXT
  2723. #define HAVE_PUTENV
  2724. /* Steve Spicklemire got this putenv from NeXTAnswers */
  2725. static int
  2726. putenv(char *newval)
  2727. {
  2728.     extern char **environ;
  2729.  
  2730.     static int firstTime = 1;
  2731.     char **ep;
  2732.     char *cp;
  2733.     int esiz;
  2734.     char *np;
  2735.  
  2736.     if (!(np = strchr(newval, '=')))
  2737.         return 1;
  2738.     *np = '\0';
  2739.  
  2740.     /* look it up */
  2741.     for (ep=environ ; *ep ; ep++)
  2742.     {
  2743.         /* this should always be true... */
  2744.         if (cp = strchr(*ep, '='))
  2745.         {
  2746.             *cp = '\0';
  2747.             if (!strcmp(*ep, newval))
  2748.             {
  2749.                 /* got it! */
  2750.                 *cp = '=';
  2751.                 break;
  2752.             }
  2753.             *cp = '=';
  2754.         }
  2755.         else
  2756.         {
  2757.             *np = '=';
  2758.             return 1;
  2759.         }
  2760.     }
  2761.  
  2762.     *np = '=';
  2763.     if (*ep)
  2764.     {
  2765.         /* the string was already there:
  2766.            just replace it with the new one */
  2767.         *ep = newval;
  2768.         return 0;
  2769.     }
  2770.  
  2771.     /* expand environ by one */
  2772.     for (esiz=2, ep=environ ; *ep ; ep++)
  2773.         esiz++;
  2774.     if (firstTime)
  2775.     {
  2776.         char **epp;
  2777.         char **newenv;
  2778.         if (!(newenv = malloc(esiz * sizeof(char *))))
  2779.             return 1;
  2780.    
  2781.         for (ep=environ, epp=newenv ; *ep ;)
  2782.             *epp++ = *ep++;
  2783.         *epp++ = newval;
  2784.         *epp = (char *) 0;
  2785.         environ = newenv;
  2786.     }
  2787.     else
  2788.     {
  2789.         if (!(environ = realloc(environ, esiz * sizeof(char *))))
  2790.             return 1;
  2791.         environ[esiz - 2] = newval;
  2792.         environ[esiz - 1] = (char *) 0;
  2793.         firstTime = 0;
  2794.     }
  2795.  
  2796.     return 0;
  2797. }
  2798. #endif /* NeXT */
  2799.  
  2800.  
  2801. #ifdef HAVE_PUTENV
  2802. static char posix_putenv__doc__[] =
  2803. "putenv(key, value) -> None\n\
  2804. Change or add an environment variable.";
  2805.  
  2806. #ifdef __BEOS__
  2807. /* We have putenv(), but not in the headers (as of PR2). - [cjh] */
  2808. int putenv( const char *str );
  2809. #endif
  2810.  
  2811. static PyObject * 
  2812. posix_putenv(self, args)
  2813.     PyObject *self;
  2814.     PyObject *args;
  2815. {
  2816.         char *s1, *s2;
  2817.         char *new;
  2818.  
  2819.     if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
  2820.         return NULL;
  2821.  
  2822. #if defined(PYOS_OS2)
  2823.     if (stricmp(s1, "BEGINLIBPATH") == 0) {
  2824.         APIRET rc;
  2825.  
  2826.         if (strlen(s2) == 0)  /* If New Value is an Empty String */
  2827.             s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */
  2828.  
  2829.         rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
  2830.         if (rc != NO_ERROR)
  2831.             return os2_error(rc);
  2832.  
  2833.     } else if (stricmp(s1, "ENDLIBPATH") == 0) {
  2834.         APIRET rc;
  2835.  
  2836.         if (strlen(s2) == 0)  /* If New Value is an Empty String */
  2837.             s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */
  2838.  
  2839.         rc = DosSetExtLIBPATH(s2, END_LIBPATH);
  2840.         if (rc != NO_ERROR)
  2841.             return os2_error(rc);
  2842.     } else {
  2843. #endif
  2844.  
  2845.     /* XXX This leaks memory -- not easy to fix :-( */
  2846.     if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
  2847.                 return PyErr_NoMemory();
  2848.     (void) sprintf(new, "%s=%s", s1, s2);
  2849.     if (putenv(new)) {
  2850.                 posix_error();
  2851.                 return NULL;
  2852.     }
  2853.  
  2854. #if defined(PYOS_OS2)
  2855.     }
  2856. #endif
  2857.     Py_INCREF(Py_None);
  2858.         return Py_None;
  2859. }
  2860. #endif /* putenv */
  2861.  
  2862. #ifdef HAVE_STRERROR
  2863. static char posix_strerror__doc__[] =
  2864. "strerror(code) -> string\n\
  2865. Translate an error code to a message string.";
  2866.  
  2867. PyObject *
  2868. posix_strerror(self, args)
  2869.     PyObject *self;
  2870.     PyObject *args;
  2871. {
  2872.     int code;
  2873.     char *message;
  2874.     if (!PyArg_ParseTuple(args, "i", &code))
  2875.         return NULL;
  2876.     message = strerror(code);
  2877.     if (message == NULL) {
  2878.         PyErr_SetString(PyExc_ValueError,
  2879.                 "strerror code out of range");
  2880.         return NULL;
  2881.     }
  2882.     return PyString_FromString(message);
  2883. }
  2884. #endif /* strerror */
  2885.  
  2886.  
  2887. #ifdef HAVE_SYS_WAIT_H
  2888.  
  2889. #ifdef WIFSTOPPED
  2890. static char posix_WIFSTOPPED__doc__[] =
  2891. "WIFSTOPPED(status) -> Boolean\n\
  2892. Return true if the process returning 'status' was stopped.";
  2893.  
  2894. static PyObject *
  2895. posix_WIFSTOPPED(self, args)
  2896.     PyObject *self;
  2897.     PyObject *args;
  2898. {
  2899. #ifdef UNION_WAIT
  2900.     union wait status;
  2901. #define status_i (status.w_status)
  2902. #else
  2903.     int status;
  2904. #define status_i status
  2905. #endif
  2906.     status_i = 0;
  2907.    
  2908.     if (!PyArg_Parse(args, "i", &status_i))
  2909.     {
  2910.         return NULL;
  2911.     }
  2912.    
  2913.     return Py_BuildValue("i", WIFSTOPPED(status));
  2914. }
  2915. #endif /* WIFSTOPPED */
  2916.  
  2917. #ifdef WIFSIGNALED
  2918. static char posix_WIFSIGNALED__doc__[] =
  2919. "WIFSIGNALED(status) -> Boolean\n\
  2920. Return true if the process returning 'status' was terminated by a signal.";
  2921.  
  2922. static PyObject *
  2923. posix_WIFSIGNALED(self, args)
  2924.     PyObject *self;
  2925.     PyObject *args;
  2926. {
  2927. #ifdef UNION_WAIT
  2928.     union wait status;
  2929. #define status_i (status.w_status)
  2930. #else
  2931.     int status;
  2932. #define status_i status
  2933. #endif
  2934.     status_i = 0;
  2935.    
  2936.     if (!PyArg_Parse(args, "i", &status_i))
  2937.     {
  2938.         return NULL;
  2939.     }
  2940.    
  2941.     return Py_BuildValue("i", WIFSIGNALED(status));
  2942. }
  2943. #endif /* WIFSIGNALED */
  2944.  
  2945. #ifdef WIFEXITED
  2946. static char posix_WIFEXITED__doc__[] =
  2947. "WIFEXITED(status) -> Boolean\n\
  2948. Return true if the process returning 'status' exited using the exit()\n\
  2949. system call.";
  2950.  
  2951. static PyObject *
  2952. posix_WIFEXITED(self, args)
  2953.     PyObject *self;
  2954.     PyObject *args;
  2955. {
  2956. #ifdef UNION_WAIT
  2957.     union wait status;
  2958. #define status_i (status.w_status)
  2959. #else
  2960.     int status;
  2961. #define status_i status
  2962. #endif
  2963.     status_i = 0;
  2964.    
  2965.     if (!PyArg_Parse(args, "i", &status_i))
  2966.     {
  2967.         return NULL;
  2968.     }
  2969.    
  2970.     return Py_BuildValue("i", WIFEXITED(status));
  2971. }
  2972. #endif /* WIFEXITED */
  2973.  
  2974. #ifdef WEXITSTATUS
  2975. static char posix_WEXITSTATUS__doc__[] =
  2976. "WEXITSTATUS(status) -> integer\n\
  2977. Return the process return code from 'status'.";
  2978.  
  2979. static PyObject *
  2980. posix_WEXITSTATUS(self, args)
  2981.     PyObject *self;
  2982.     PyObject *args;
  2983. {
  2984. #ifdef UNION_WAIT
  2985.     union wait status;
  2986. #define status_i (status.w_status)
  2987. #else
  2988.     int status;
  2989. #define status_i status
  2990. #endif
  2991.     status_i = 0;
  2992.    
  2993.     if (!PyArg_Parse(args, "i", &status_i))
  2994.     {
  2995.         return NULL;
  2996.     }
  2997.    
  2998.     return Py_BuildValue("i", WEXITSTATUS(status));
  2999. }
  3000. #endif /* WEXITSTATUS */
  3001.  
  3002. #ifdef WTERMSIG
  3003. static char posix_WTERMSIG__doc__[] =
  3004. "WTERMSIG(status) -> integer\n\
  3005. Return the signal that terminated the process that provided the 'status'\n\
  3006. value.";
  3007.  
  3008. static PyObject *
  3009. posix_WTERMSIG(self, args)
  3010.     PyObject *self;
  3011.     PyObject *args;
  3012. {
  3013. #ifdef UNION_WAIT
  3014.     union wait status;
  3015. #define status_i (status.w_status)
  3016. #else
  3017.     int status;
  3018. #define status_i status
  3019. #endif
  3020.     status_i = 0;
  3021.    
  3022.     if (!PyArg_Parse(args, "i", &status_i))
  3023.     {
  3024.         return NULL;
  3025.     }
  3026.    
  3027.     return Py_BuildValue("i", WTERMSIG(status));
  3028. }
  3029. #endif /* WTERMSIG */
  3030.  
  3031. #ifdef WSTOPSIG
  3032. static char posix_WSTOPSIG__doc__[] =
  3033. "WSTOPSIG(status) -> integer\n\
  3034. Return the signal that stopped the process that provided the 'status' value.";
  3035.  
  3036. static PyObject *
  3037. posix_WSTOPSIG(self, args)
  3038.     PyObject *self;
  3039.     PyObject *args;
  3040. {
  3041. #ifdef UNION_WAIT
  3042.     union wait status;
  3043. #define status_i (status.w_status)
  3044. #else
  3045.     int status;
  3046. #define status_i status
  3047. #endif
  3048.     status_i = 0;
  3049.    
  3050.     if (!PyArg_Parse(args, "i", &status_i))
  3051.     {
  3052.         return NULL;
  3053.     }
  3054.    
  3055.     return Py_BuildValue("i", WSTOPSIG(status));
  3056. }
  3057. #endif /* WSTOPSIG */
  3058.  
  3059. #endif /* HAVE_SYS_WAIT_H */
  3060.  
  3061.  
  3062. #if defined(HAVE_FSTATVFS)
  3063. #include <sys/statvfs.h>
  3064.  
  3065. static char posix_fstatvfs__doc__[] =
  3066. "fstatvfs(fd) -> \n\
  3067.  (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
  3068. Perform an fstatvfs system call on the given fd.";
  3069.  
  3070. static PyObject *
  3071. posix_fstatvfs(self, args)
  3072.     PyObject *self;
  3073.     PyObject *args;
  3074. {
  3075.     int fd, res;
  3076.     struct statvfs st;
  3077.     if (!PyArg_ParseTuple(args, "i", &fd))
  3078.         return NULL;
  3079.     Py_BEGIN_ALLOW_THREADS
  3080.     res = fstatvfs(fd, &st);
  3081.     Py_END_ALLOW_THREADS
  3082.     if (res != 0)
  3083.         return posix_error();
  3084. #if !defined(HAVE_LARGEFILE_SUPPORT)
  3085.     return Py_BuildValue("(llllllllll)",
  3086.             (long) st.f_bsize,
  3087.             (long) st.f_frsize,
  3088.             (long) st.f_blocks,
  3089.             (long) st.f_bfree,
  3090.             (long) st.f_bavail,
  3091.             (long) st.f_files,
  3092.             (long) st.f_ffree,
  3093.             (long) st.f_favail,
  3094.             (long) st.f_flag,
  3095.             (long) st.f_namemax);
  3096. #else
  3097.     return Py_BuildValue("(llLLLLLLll)",
  3098.             (long) st.f_bsize,
  3099.             (long) st.f_frsize,
  3100.             (LONG_LONG) st.f_blocks,
  3101.             (LONG_LONG) st.f_bfree,
  3102.             (LONG_LONG) st.f_bavail,
  3103.             (LONG_LONG) st.f_files,
  3104.             (LONG_LONG) st.f_ffree,
  3105.             (LONG_LONG) st.f_favail,
  3106.             (long) st.f_flag,
  3107.             (long) st.f_namemax);
  3108. #endif
  3109. }
  3110. #endif /* HAVE_FSTATVFS */
  3111.  
  3112.  
  3113. #if defined(HAVE_STATVFS)
  3114. #include <sys/statvfs.h>
  3115.  
  3116. static char posix_statvfs__doc__[] =
  3117. "statvfs(path) -> \n\
  3118.  (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
  3119. Perform a statvfs system call on the given path.";
  3120.  
  3121. static PyObject *
  3122. posix_statvfs(self, args)
  3123.     PyObject *self;
  3124.     PyObject *args;
  3125. {
  3126.     char *path;
  3127.     int res;
  3128.     struct statvfs st;
  3129.     if (!PyArg_ParseTuple(args, "s", &path))
  3130.         return NULL;
  3131.     Py_BEGIN_ALLOW_THREADS
  3132.     res = statvfs(path, &st);
  3133.     Py_END_ALLOW_THREADS
  3134.     if (res != 0)
  3135.         return posix_error_with_filename(path);
  3136. #if !defined(HAVE_LARGEFILE_SUPPORT)
  3137.     return Py_BuildValue("(llllllllll)",
  3138.             (long) st.f_bsize,
  3139.             (long) st.f_frsize,
  3140.             (long) st.f_blocks,
  3141.             (long) st.f_bfree,
  3142.             (long) st.f_bavail,
  3143.             (long) st.f_files,
  3144.             (long) st.f_ffree,
  3145.             (long) st.f_favail,
  3146.             (long) st.f_flag,
  3147.             (long) st.f_namemax);
  3148. #else    /* HAVE_LARGEFILE_SUPPORT */
  3149.     return Py_BuildValue("(llLLLLLLll)",
  3150.             (long) st.f_bsize,
  3151.             (long) st.f_frsize,
  3152.             (LONG_LONG) st.f_blocks,
  3153.             (LONG_LONG) st.f_bfree,
  3154.             (LONG_LONG) st.f_bavail,
  3155.             (LONG_LONG) st.f_files,
  3156.             (LONG_LONG) st.f_ffree,
  3157.             (LONG_LONG) st.f_favail,
  3158.             (long) st.f_flag,
  3159.             (long) st.f_namemax);
  3160. #endif
  3161. }
  3162. #endif /* HAVE_STATVFS */
  3163.  
  3164.  
  3165. static PyMethodDef posix_methods[] = {
  3166.     {"access",    posix_access, 0, posix_access__doc__},
  3167. #ifdef HAVE_TTYNAME
  3168.     {"ttyname",    posix_ttyname, 0, posix_ttyname__doc__},
  3169. #endif
  3170.     {"chdir",    posix_chdir, 0, posix_chdir__doc__},
  3171.     {"chmod",    posix_chmod, 0, posix_chmod__doc__},
  3172. #ifdef HAVE_CHOWN
  3173.     {"chown",    posix_chown, 0, posix_chown__doc__},
  3174. #endif /* HAVE_CHOWN */
  3175. #ifdef HAVE_GETCWD
  3176.     {"getcwd",    posix_getcwd, 0, posix_getcwd__doc__},
  3177. #endif
  3178. #ifdef HAVE_LINK
  3179.     {"link",    posix_link, 0, posix_link__doc__},
  3180. #endif /* HAVE_LINK */
  3181.     {"listdir",    posix_listdir, 0, posix_listdir__doc__},
  3182.     {"lstat",    posix_lstat, 0, posix_lstat__doc__},
  3183.     {"mkdir",    posix_mkdir, 1, posix_mkdir__doc__},
  3184. #ifdef HAVE_NICE
  3185.     {"nice",    posix_nice, 0, posix_nice__doc__},
  3186. #endif /* HAVE_NICE */
  3187. #ifdef HAVE_READLINK
  3188.     {"readlink",    posix_readlink, 0, posix_readlink__doc__},
  3189. #endif /* HAVE_READLINK */
  3190.     {"rename",    posix_rename, 0, posix_rename__doc__},
  3191.     {"rmdir",    posix_rmdir, 0, posix_rmdir__doc__},
  3192.     {"stat",    posix_stat, 0, posix_stat__doc__},
  3193. #ifdef HAVE_SYMLINK
  3194.     {"symlink",    posix_symlink, 0, posix_symlink__doc__},
  3195. #endif /* HAVE_SYMLINK */
  3196. #ifdef HAVE_SYSTEM
  3197.     {"system",    posix_system, 0, posix_system__doc__},
  3198. #endif
  3199.     {"umask",    posix_umask, 0, posix_umask__doc__},
  3200. #ifdef HAVE_UNAME
  3201.     {"uname",    posix_uname, 0, posix_uname__doc__},
  3202. #endif /* HAVE_UNAME */
  3203.     {"unlink",    posix_unlink, 0, posix_unlink__doc__},
  3204.     {"remove",    posix_unlink, 0, posix_remove__doc__},
  3205.     {"utime",    posix_utime, 0, posix_utime__doc__},
  3206. #ifdef HAVE_TIMES
  3207.     {"times",    posix_times, 0, posix_times__doc__},
  3208. #endif /* HAVE_TIMES */
  3209.     {"_exit",    posix__exit, 0, posix__exit__doc__},
  3210. #ifdef HAVE_EXECV
  3211.     {"execv",    posix_execv, 0, posix_execv__doc__},
  3212.     {"execve",    posix_execve, 0, posix_execve__doc__},
  3213. #endif /* HAVE_EXECV */
  3214. #ifdef HAVE_SPAWNV
  3215.     {"spawnv",    posix_spawnv, 0, posix_spawnv__doc__},
  3216.     {"spawnve",    posix_spawnve, 0, posix_spawnve__doc__},
  3217. #endif /* HAVE_SPAWNV */
  3218. #ifdef HAVE_FORK
  3219.     {"fork",    posix_fork, 0, posix_fork__doc__},
  3220. #endif /* HAVE_FORK */
  3221. #ifdef HAVE_GETEGID
  3222.     {"getegid",    posix_getegid, 0, posix_getegid__doc__},
  3223. #endif /* HAVE_GETEGID */
  3224. #ifdef HAVE_GETEUID
  3225.     {"geteuid",    posix_geteuid, 0, posix_geteuid__doc__},
  3226. #endif /* HAVE_GETEUID */
  3227. #ifdef HAVE_GETGID
  3228.     {"getgid",    posix_getgid, 0, posix_getgid__doc__},
  3229. #endif /* HAVE_GETGID */
  3230.     {"getpid",    posix_getpid, 0, posix_getpid__doc__},
  3231. #ifdef HAVE_GETPGRP
  3232.     {"getpgrp",    posix_getpgrp, 0, posix_getpgrp__doc__},
  3233. #endif /* HAVE_GETPGRP */
  3234. #ifdef HAVE_GETPPID
  3235.     {"getppid",    posix_getppid, 0, posix_getppid__doc__},
  3236. #endif /* HAVE_GETPPID */
  3237. #ifdef HAVE_GETUID
  3238.     {"getuid",    posix_getuid, 0, posix_getuid__doc__},
  3239. #endif /* HAVE_GETUID */
  3240. #ifdef HAVE_KILL
  3241.     {"kill",    posix_kill, 0, posix_kill__doc__},
  3242. #endif /* HAVE_KILL */
  3243. #ifdef HAVE_PLOCK
  3244.     {"plock",    posix_plock, 0, posix_plock__doc__},
  3245. #endif /* HAVE_PLOCK */
  3246. #ifdef HAVE_POPEN
  3247.     {"popen",    posix_popen, 1, posix_popen__doc__},
  3248. #endif /* HAVE_POPEN */
  3249. #ifdef HAVE_SETUID
  3250.     {"setuid",    posix_setuid, 0, posix_setuid__doc__},
  3251. #endif /* HAVE_SETUID */
  3252. #ifdef HAVE_SETGID
  3253.     {"setgid",    posix_setgid, 0, posix_setgid__doc__},
  3254. #endif /* HAVE_SETGID */
  3255. #ifdef HAVE_SETPGRP
  3256.     {"setpgrp",    posix_setpgrp, 0, posix_setpgrp__doc__},
  3257. #endif /* HAVE_SETPGRP */
  3258. #ifdef HAVE_WAIT
  3259.     {"wait",    posix_wait, 0, posix_wait__doc__},
  3260. #endif /* HAVE_WAIT */
  3261. #ifdef HAVE_WAITPID
  3262.     {"waitpid",    posix_waitpid, 0, posix_waitpid__doc__},
  3263. #endif /* HAVE_WAITPID */
  3264. #ifdef HAVE_SETSID
  3265.     {"setsid",    posix_setsid, 0, posix_setsid__doc__},
  3266. #endif /* HAVE_SETSID */
  3267. #ifdef HAVE_SETPGID
  3268.     {"setpgid",    posix_setpgid, 0, posix_setpgid__doc__},
  3269. #endif /* HAVE_SETPGID */
  3270. #ifdef HAVE_TCGETPGRP
  3271.     {"tcgetpgrp",    posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
  3272. #endif /* HAVE_TCGETPGRP */
  3273. #ifdef HAVE_TCSETPGRP
  3274.     {"tcsetpgrp",    posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
  3275. #endif /* HAVE_TCSETPGRP */
  3276.     {"open",    posix_open, 1, posix_open__doc__},
  3277.     {"close",    posix_close, 0, posix_close__doc__},
  3278.     {"dup",        posix_dup, 0, posix_dup__doc__},
  3279.     {"dup2",    posix_dup2, 0, posix_dup2__doc__},
  3280.     {"lseek",    posix_lseek, 0, posix_lseek__doc__},
  3281.     {"read",    posix_read, 0, posix_read__doc__},
  3282.     {"write",    posix_write, 0, posix_write__doc__},
  3283.     {"fstat",    posix_fstat, 0, posix_fstat__doc__},
  3284.     {"fdopen",    posix_fdopen,    1, posix_fdopen__doc__},
  3285. #ifdef HAVE_PIPE
  3286.     {"pipe",    posix_pipe, 0, posix_pipe__doc__},
  3287. #endif
  3288. #ifdef HAVE_MKFIFO
  3289.     {"mkfifo",    posix_mkfifo, 1, posix_mkfifo__doc__},
  3290. #endif
  3291. #ifdef HAVE_FTRUNCATE
  3292.     {"ftruncate",    posix_ftruncate, 1, posix_ftruncate__doc__},
  3293. #endif
  3294. #ifdef HAVE_PUTENV
  3295.     {"putenv",    posix_putenv, 1, posix_putenv__doc__},
  3296. #endif
  3297. #ifdef HAVE_STRERROR
  3298.     {"strerror",    posix_strerror, 1, posix_strerror__doc__},
  3299. #endif
  3300. #ifdef HAVE_FSYNC
  3301.     {"fsync",       posix_fsync, 0, posix_fsync__doc__},
  3302. #endif
  3303. #ifdef HAVE_FDATASYNC
  3304.     {"fdatasync",   posix_fdatasync,  0, posix_fdatasync__doc__},
  3305. #endif
  3306. #ifdef HAVE_SYS_WAIT_H
  3307. #ifdef WIFSTOPPED
  3308.         {"WIFSTOPPED",    posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
  3309. #endif /* WIFSTOPPED */
  3310. #ifdef WIFSIGNALED
  3311.         {"WIFSIGNALED",    posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
  3312. #endif /* WIFSIGNALED */
  3313. #ifdef WIFEXITED
  3314.         {"WIFEXITED",    posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
  3315. #endif /* WIFEXITED */
  3316. #ifdef WEXITSTATUS
  3317.         {"WEXITSTATUS",    posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
  3318. #endif /* WEXITSTATUS */
  3319. #ifdef WTERMSIG
  3320.         {"WTERMSIG",    posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
  3321. #endif /* WTERMSIG */
  3322. #ifdef WSTOPSIG
  3323.         {"WSTOPSIG",    posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
  3324. #endif /* WSTOPSIG */
  3325. #endif /* HAVE_SYS_WAIT_H */
  3326. #ifdef HAVE_FSTATVFS
  3327.     {"fstatvfs",    posix_fstatvfs, 1, posix_fstatvfs__doc__},
  3328. #endif
  3329. #ifdef HAVE_STATVFS
  3330.     {"statvfs",    posix_statvfs, 1, posix_statvfs__doc__},
  3331. #endif
  3332.     {NULL,        NULL}         /* Sentinel */
  3333. };
  3334.  
  3335.  
  3336. static int
  3337. ins(d, symbol, value)
  3338.         PyObject* d;
  3339.         char* symbol;
  3340.         long value;
  3341. {
  3342.         PyObject* v = PyInt_FromLong(value);
  3343.         if (!v || PyDict_SetItemString(d, symbol, v) < 0)
  3344.                 return -1;                   /* triggers fatal error */
  3345.  
  3346.         Py_DECREF(v);
  3347.         return 0;
  3348. }
  3349.  
  3350. #if defined(PYOS_OS2)
  3351. /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
  3352. static int insertvalues(PyObject *d)
  3353. {
  3354.     APIRET    rc;
  3355.     ULONG     values[QSV_MAX+1];
  3356.     PyObject *v;
  3357.     char     *ver, tmp[10];
  3358.  
  3359.     Py_BEGIN_ALLOW_THREADS
  3360.     rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
  3361.     Py_END_ALLOW_THREADS
  3362.  
  3363.     if (rc != NO_ERROR) {
  3364.         os2_error(rc);
  3365.         return -1;
  3366.     }
  3367.  
  3368.     if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
  3369.     if (ins(d, "memkernel",    values[QSV_TOTRESMEM])) return -1;
  3370.     if (ins(d, "memvirtual",   values[QSV_TOTAVAILMEM])) return -1;
  3371.     if (ins(d, "maxpathlen",   values[QSV_MAX_PATH_LENGTH])) return -1;
  3372.     if (ins(d, "maxnamelen",   values[QSV_MAX_COMP_LENGTH])) return -1;
  3373.     if (ins(d, "revision",     values[QSV_VERSION_REVISION])) return -1;
  3374.     if (ins(d, "timeslice",    values[QSV_MIN_SLICE])) return -1;
  3375.  
  3376.     switch (values[QSV_VERSION_MINOR]) {
  3377.     case 0:  ver = "2.00"; break;
  3378.     case 10: ver = "2.10"; break;
  3379.     case 11: ver = "2.11"; break;
  3380.     case 30: ver = "3.00"; break;
  3381.     case 40: ver = "4.00"; break;
  3382.     case 50: ver = "5.00"; break;
  3383.     default:
  3384.         sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
  3385.                               values[QSV_VERSION_MINOR]);
  3386.         ver = &tmp[0];
  3387.     }
  3388.  
  3389.     /* Add Indicator of the Version of the Operating System */
  3390.     v = PyString_FromString(ver);
  3391.     if (!v || PyDict_SetItemString(d, "version", v) < 0)
  3392.         return -1;
  3393.     Py_DECREF(v);
  3394.  
  3395.     /* Add Indicator of Which Drive was Used to Boot the System */
  3396.     tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
  3397.     tmp[1] = ':';
  3398.     tmp[2] = '\0';
  3399.  
  3400.     v = PyString_FromString(tmp);
  3401.     if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
  3402.         return -1;
  3403.     Py_DECREF(v);
  3404.  
  3405.     return 0;
  3406. }
  3407. #endif
  3408.  
  3409. static int
  3410. all_ins(d)
  3411.         PyObject* d;
  3412. {
  3413. #ifdef F_OK
  3414.         if (ins(d, "F_OK", (long)F_OK)) return -1;
  3415. #endif        
  3416. #ifdef R_OK
  3417.         if (ins(d, "R_OK", (long)R_OK)) return -1;
  3418. #endif        
  3419. #ifdef W_OK
  3420.         if (ins(d, "W_OK", (long)W_OK)) return -1;
  3421. #endif        
  3422. #ifdef X_OK
  3423.         if (ins(d, "X_OK", (long)X_OK)) return -1;
  3424. #endif        
  3425. #ifdef WNOHANG
  3426.         if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
  3427. #endif        
  3428. #ifdef O_RDONLY
  3429.         if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
  3430. #endif
  3431. #ifdef O_WRONLY
  3432.         if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
  3433. #endif
  3434. #ifdef O_RDWR
  3435.         if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
  3436. #endif
  3437. #ifdef O_NDELAY
  3438.         if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
  3439. #endif
  3440. #ifdef O_NONBLOCK
  3441.         if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
  3442. #endif
  3443. #ifdef O_APPEND
  3444.         if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
  3445. #endif
  3446. #ifdef O_DSYNC
  3447.         if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
  3448. #endif
  3449. #ifdef O_RSYNC
  3450.         if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
  3451. #endif
  3452. #ifdef O_SYNC
  3453.         if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
  3454. #endif
  3455. #ifdef O_NOCTTY
  3456.         if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
  3457. #endif
  3458. #ifdef O_CREAT
  3459.         if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
  3460. #endif
  3461. #ifdef O_EXCL
  3462.         if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
  3463. #endif
  3464. #ifdef O_TRUNC
  3465.         if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
  3466. #endif
  3467. #ifdef O_BINARY
  3468.         if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
  3469. #endif
  3470. #ifdef O_TEXT
  3471.         if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
  3472. #endif
  3473.  
  3474. #ifdef HAVE_SPAWNV
  3475.         if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
  3476.         if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
  3477.         if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
  3478.         if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
  3479.         if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
  3480. #endif
  3481.  
  3482. #if defined(PYOS_OS2)
  3483.         if (insertvalues(d)) return -1;
  3484. #endif
  3485.         return 0;
  3486. }
  3487.  
  3488.  
  3489. #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
  3490. #define INITFUNC initnt
  3491. #define MODNAME "nt"
  3492. #else
  3493. #if defined(PYOS_OS2)
  3494. #define INITFUNC initos2
  3495. #define MODNAME "os2"
  3496. #else
  3497. #define INITFUNC initposix
  3498. #define MODNAME "posix"
  3499. #endif
  3500. #endif
  3501.  
  3502. DL_EXPORT(void)
  3503. INITFUNC()
  3504. {
  3505.     PyObject *m, *d, *v;
  3506.     
  3507.     m = Py_InitModule4(MODNAME,
  3508.                posix_methods,
  3509.                posix__doc__,
  3510.                (PyObject *)NULL,
  3511.                PYTHON_API_VERSION);
  3512.     d = PyModule_GetDict(m);
  3513.     
  3514.     /* Initialize environ dictionary */
  3515.     v = convertenviron();
  3516.     if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
  3517.         return;
  3518.     Py_DECREF(v);
  3519.     
  3520.         if (all_ins(d))
  3521.                 return;
  3522.  
  3523.     PyDict_SetItemString(d, "error", PyExc_OSError);
  3524. }
  3525.