home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / posixmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  114KB  |  4,797 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. /* Don't use the "_r" form if we don't need it (also, won't have a
  278.    prototype for it, at least on Solaris -- maybe others as well?). */
  279. #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
  280. #define USE_CTERMID_R
  281. #endif
  282.  
  283. #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
  284. #define USE_TMPNAM_R
  285. #endif
  286.  
  287. /* Return a dictionary corresponding to the POSIX environment table */
  288.  
  289. #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
  290. extern char **environ;
  291. #endif /* !_MSC_VER */
  292.  
  293. static PyObject *
  294. convertenviron()
  295. {
  296.     PyObject *d;
  297.     char **e;
  298.     d = PyDict_New();
  299.     if (d == NULL)
  300.         return NULL;
  301.     if (environ == NULL)
  302.         return d;
  303.     /* This part ignores errors */
  304.     for (e = environ; *e != NULL; e++) {
  305.         PyObject *k;
  306.         PyObject *v;
  307.         char *p = strchr(*e, '=');
  308.         if (p == NULL)
  309.             continue;
  310.         k = PyString_FromStringAndSize(*e, (int)(p-*e));
  311.         if (k == NULL) {
  312.             PyErr_Clear();
  313.             continue;
  314.         }
  315.         v = PyString_FromString(p+1);
  316.         if (v == NULL) {
  317.             PyErr_Clear();
  318.             Py_DECREF(k);
  319.             continue;
  320.         }
  321.         if (PyDict_GetItem(d, k) == NULL) {
  322.             if (PyDict_SetItem(d, k, v) != 0)
  323.                 PyErr_Clear();
  324.         }
  325.         Py_DECREF(k);
  326.         Py_DECREF(v);
  327.     }
  328. #if defined(PYOS_OS2)
  329.     {
  330.         APIRET rc;
  331.         char   buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
  332.  
  333.         rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
  334.         if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
  335.             PyObject *v = PyString_FromString(buffer);
  336.             PyDict_SetItemString(d, "BEGINLIBPATH", v);
  337.             Py_DECREF(v);
  338.         }
  339.         rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
  340.         if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
  341.             PyObject *v = PyString_FromString(buffer);
  342.             PyDict_SetItemString(d, "ENDLIBPATH", v);
  343.             Py_DECREF(v);
  344.         }
  345.     }
  346. #endif
  347.     return d;
  348. }
  349.  
  350.  
  351. /* Set a POSIX-specific error from errno, and return NULL */
  352.  
  353. static PyObject *
  354. posix_error()
  355. {
  356.     return PyErr_SetFromErrno(PyExc_OSError);
  357. }
  358. static PyObject *
  359. posix_error_with_filename(name)
  360.     char* name;
  361. {
  362.     return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
  363. }
  364.  
  365.  
  366. #if defined(PYOS_OS2)
  367. /**********************************************************************
  368.  *         Helper Function to Trim and Format OS/2 Messages
  369.  **********************************************************************/
  370.     static void
  371. os2_formatmsg(char *msgbuf, int msglen, char *reason)
  372. {
  373.     msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
  374.  
  375.     if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
  376.         char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
  377.  
  378.         while (lastc > msgbuf && isspace(*lastc))
  379.             *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
  380.     }
  381.  
  382.     /* Add Optional Reason Text */
  383.     if (reason) {
  384.         strcat(msgbuf, " : ");
  385.         strcat(msgbuf, reason);
  386.     }
  387. }
  388.  
  389. /**********************************************************************
  390.  *             Decode an OS/2 Operating System Error Code
  391.  *
  392.  * A convenience function to lookup an OS/2 error code and return a
  393.  * text message we can use to raise a Python exception.
  394.  *
  395.  * Notes:
  396.  *   The messages for errors returned from the OS/2 kernel reside in
  397.  *   the file OSO001.MSG in the \OS2 directory hierarchy.
  398.  *
  399.  **********************************************************************/
  400.     static char *
  401. os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
  402. {
  403.     APIRET rc;
  404.     ULONG  msglen;
  405.  
  406.     /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
  407.     Py_BEGIN_ALLOW_THREADS
  408.     rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
  409.                        errorcode, "oso001.msg", &msglen);
  410.     Py_END_ALLOW_THREADS
  411.  
  412.     if (rc == NO_ERROR)
  413.         os2_formatmsg(msgbuf, msglen, reason);
  414.     else
  415.         sprintf(msgbuf, "unknown OS error #%d", errorcode);
  416.  
  417.     return msgbuf;
  418. }
  419.  
  420. /* Set an OS/2-specific error and return NULL.  OS/2 kernel
  421.    errors are not in a global variable e.g. 'errno' nor are
  422.    they congruent with posix error numbers. */
  423.  
  424. static PyObject * os2_error(int code)
  425. {
  426.     char text[1024];
  427.     PyObject *v;
  428.  
  429.     os2_strerror(text, sizeof(text), code, "");
  430.  
  431.     v = Py_BuildValue("(is)", code, text);
  432.     if (v != NULL) {
  433.         PyErr_SetObject(PyExc_OSError, v);
  434.         Py_DECREF(v);
  435.     }
  436.     return NULL; /* Signal to Python that an Exception is Pending */
  437. }
  438.  
  439. #endif /* OS2 */
  440.  
  441. /* POSIX generic methods */
  442.  
  443. static PyObject *
  444. posix_int(args, format, func)
  445.         PyObject *args;
  446.         char *format;
  447.     int (*func) Py_FPROTO((int));
  448. {
  449.     int fd;
  450.     int res;
  451.     if (!PyArg_ParseTuple(args,  format, &fd))
  452.         return NULL;
  453.     Py_BEGIN_ALLOW_THREADS
  454.     res = (*func)(fd);
  455.     Py_END_ALLOW_THREADS
  456.     if (res < 0)
  457.         return posix_error();
  458.     Py_INCREF(Py_None);
  459.     return Py_None;
  460. }
  461.  
  462.  
  463. static PyObject *
  464. posix_1str(args, format, func)
  465.     PyObject *args;
  466.         char *format;
  467.     int (*func) Py_FPROTO((const char *));
  468. {
  469.     char *path1;
  470.     int res;
  471.     if (!PyArg_ParseTuple(args, format, &path1))
  472.         return NULL;
  473.     Py_BEGIN_ALLOW_THREADS
  474.     res = (*func)(path1);
  475.     Py_END_ALLOW_THREADS
  476.     if (res < 0)
  477.         return posix_error_with_filename(path1);
  478.     Py_INCREF(Py_None);
  479.     return Py_None;
  480. }
  481.  
  482. static PyObject *
  483. posix_2str(args, format, func)
  484.     PyObject *args;
  485.         char *format;
  486.     int (*func) Py_FPROTO((const char *, const char *));
  487. {
  488.     char *path1, *path2;
  489.     int res;
  490.     if (!PyArg_ParseTuple(args, format, &path1, &path2))
  491.         return NULL;
  492.     Py_BEGIN_ALLOW_THREADS
  493.     res = (*func)(path1, path2);
  494.     Py_END_ALLOW_THREADS
  495.     if (res != 0)
  496.         /* XXX how to report both path1 and path2??? */
  497.         return posix_error();
  498.     Py_INCREF(Py_None);
  499.     return Py_None;
  500. }
  501.  
  502. static PyObject *
  503. posix_strint(args, format, func)
  504.     PyObject *args;
  505.         char *format;
  506.     int (*func) Py_FPROTO((const char *, int));
  507. {
  508.     char *path;
  509.     int i;
  510.     int res;
  511.     if (!PyArg_ParseTuple(args, format, &path, &i))
  512.         return NULL;
  513.     Py_BEGIN_ALLOW_THREADS
  514.     res = (*func)(path, i);
  515.     Py_END_ALLOW_THREADS
  516.     if (res < 0)
  517.         return posix_error_with_filename(path);
  518.     Py_INCREF(Py_None);
  519.     return Py_None;
  520. }
  521.  
  522. static PyObject *
  523. posix_strintint(args, format, func)
  524.     PyObject *args;
  525.         char *format;
  526.     int (*func) Py_FPROTO((const char *, int, int));
  527. {
  528.     char *path;
  529.     int i,i2;
  530.     int res;
  531.     if (!PyArg_ParseTuple(args, format, &path, &i, &i2))
  532.         return NULL;
  533.     Py_BEGIN_ALLOW_THREADS
  534.     res = (*func)(path, i, i2);
  535.     Py_END_ALLOW_THREADS
  536.     if (res < 0)
  537.         return posix_error_with_filename(path);
  538.     Py_INCREF(Py_None);
  539.     return Py_None;
  540. }
  541.  
  542. static PyObject *
  543. posix_do_stat(self, args, format, statfunc)
  544.     PyObject *self;
  545.     PyObject *args;
  546.         char *format;
  547.     int (*statfunc) Py_FPROTO((const char *, struct stat *));
  548. {
  549.     struct stat st;
  550.     char *path;
  551.     int res;
  552.     if (!PyArg_ParseTuple(args, format, &path))
  553.         return NULL;
  554.     Py_BEGIN_ALLOW_THREADS
  555.     res = (*statfunc)(path, &st);
  556.     Py_END_ALLOW_THREADS
  557.     if (res != 0)
  558.         return posix_error_with_filename(path);
  559. #if !defined(HAVE_LARGEFILE_SUPPORT)
  560.     return Py_BuildValue("(llllllllll)",
  561.                  (long)st.st_mode,
  562.                  (long)st.st_ino,
  563.                  (long)st.st_dev,
  564.                  (long)st.st_nlink,
  565.                  (long)st.st_uid,
  566.                  (long)st.st_gid,
  567.                  (long)st.st_size,
  568.                  (long)st.st_atime,
  569.                  (long)st.st_mtime,
  570.                  (long)st.st_ctime);
  571. #else
  572.     return Py_BuildValue("(lLllllLlll)",
  573.                  (long)st.st_mode,
  574.                  (LONG_LONG)st.st_ino,
  575.                  (long)st.st_dev,
  576.                  (long)st.st_nlink,
  577.                  (long)st.st_uid,
  578.                  (long)st.st_gid,
  579.                  (LONG_LONG)st.st_size,
  580.                  (long)st.st_atime,
  581.                  (long)st.st_mtime,
  582.                  (long)st.st_ctime);
  583. #endif
  584. }
  585.  
  586.  
  587. /* POSIX methods */
  588.  
  589. static char posix_access__doc__[] =
  590. "access(path, mode) -> 1 if granted, 0 otherwise\n\
  591. Test for access to a file.";
  592.  
  593. static PyObject *
  594. posix_access(self, args)
  595.     PyObject *self;
  596.     PyObject *args;
  597. {
  598.     char *path;
  599.     int mode;
  600.     int res;
  601.  
  602.     if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
  603.         return NULL;
  604.     Py_BEGIN_ALLOW_THREADS
  605.     res = access(path, mode);
  606.     Py_END_ALLOW_THREADS
  607.     return(PyInt_FromLong(res == 0 ? 1L : 0L));
  608. }
  609.  
  610. #ifndef F_OK
  611. #define F_OK 0
  612. #endif
  613. #ifndef R_OK
  614. #define R_OK 4
  615. #endif
  616. #ifndef W_OK
  617. #define W_OK 2
  618. #endif
  619. #ifndef X_OK
  620. #define X_OK 1
  621. #endif
  622.  
  623. #ifdef HAVE_TTYNAME
  624. static char posix_ttyname__doc__[] =
  625. "ttyname(fd) -> String\n\
  626. Return the name of the terminal device connected to 'fd'.";
  627.  
  628. static PyObject *
  629. posix_ttyname(self, args)
  630.     PyObject *self;
  631.     PyObject *args;
  632. {
  633.     int id;
  634.     char *ret;
  635.  
  636.     if (!PyArg_ParseTuple(args, "i:ttyname", &id))
  637.         return NULL;
  638.  
  639.     ret = ttyname(id);
  640.     if (ret == NULL)
  641.         return(posix_error());
  642.     return(PyString_FromString(ret));
  643. }
  644. #endif
  645.  
  646. #ifdef HAVE_CTERMID
  647. static char posix_ctermid__doc__[] =
  648. "ctermid() -> String\n\
  649. Return the name of the controlling terminal for this process.";
  650.  
  651. static PyObject *
  652. posix_ctermid(self, args)
  653.     PyObject *self;
  654.     PyObject *args;
  655. {
  656.         char *ret;
  657.         char buffer[L_ctermid];
  658.  
  659.     if (!PyArg_ParseTuple(args, ":ctermid"))
  660.         return NULL;
  661.  
  662. #ifdef USE_CTERMID_R
  663.     ret = ctermid_r(buffer);
  664. #else
  665.         ret = ctermid(buffer);
  666. #endif
  667.     if (ret == NULL)
  668.         return(posix_error());
  669.     return(PyString_FromString(buffer));
  670. }
  671. #endif
  672.  
  673. static char posix_chdir__doc__[] =
  674. "chdir(path) -> None\n\
  675. Change the current working directory to the specified path.";
  676.  
  677. static PyObject *
  678. posix_chdir(self, args)
  679.     PyObject *self;
  680.     PyObject *args;
  681. {
  682.     return posix_1str(args, "s:chdir", chdir);
  683. }
  684.  
  685.  
  686. static char posix_chmod__doc__[] =
  687. "chmod(path, mode) -> None\n\
  688. Change the access permissions of a file.";
  689.  
  690. static PyObject *
  691. posix_chmod(self, args)
  692.     PyObject *self;
  693.     PyObject *args;
  694. {
  695.     return posix_strint(args, "si:chmod", chmod);
  696. }
  697.  
  698.  
  699. #ifdef HAVE_FSYNC
  700. static char posix_fsync__doc__[] =
  701. "fsync(fildes) -> None\n\
  702. force write of file with filedescriptor to disk.";
  703.  
  704. static PyObject *
  705. posix_fsync(self, args)
  706.        PyObject *self;
  707.        PyObject *args;
  708. {
  709.        return posix_int(args, "i:fsync", fsync);
  710. }
  711. #endif /* HAVE_FSYNC */
  712.  
  713. #ifdef HAVE_FDATASYNC
  714. static char posix_fdatasync__doc__[] =
  715. "fdatasync(fildes) -> None\n\
  716. force write of file with filedescriptor to disk.\n\
  717.  does not force update of metadata.";
  718.  
  719. extern int fdatasync(int); /* Prototype just in case */
  720.  
  721. static PyObject *
  722. posix_fdatasync(self, args)
  723.        PyObject *self;
  724.        PyObject *args;
  725. {
  726.        return posix_int(args, "i:fdatasync", fdatasync);
  727. }
  728. #endif /* HAVE_FDATASYNC */
  729.  
  730.  
  731. #ifdef HAVE_CHOWN
  732. static char posix_chown__doc__[] =
  733. "chown(path, uid, gid) -> None\n\
  734. Change the owner and group id of path to the numeric uid and gid.";
  735.  
  736. static PyObject *
  737. posix_chown(self, args)
  738.     PyObject *self;
  739.     PyObject *args;
  740. {
  741.     return posix_strintint(args, "sii:chown", chown);
  742. }
  743. #endif /* HAVE_CHOWN */
  744.  
  745.  
  746. #ifdef HAVE_GETCWD
  747. static char posix_getcwd__doc__[] =
  748. "getcwd() -> path\n\
  749. Return a string representing the current working directory.";
  750.  
  751. static PyObject *
  752. posix_getcwd(self, args)
  753.     PyObject *self;
  754.     PyObject *args;
  755. {
  756.     char buf[1026];
  757.     char *res;
  758.     if (!PyArg_ParseTuple(args, ":getcwd"))
  759.         return NULL;
  760.     Py_BEGIN_ALLOW_THREADS
  761.     res = getcwd(buf, sizeof buf);
  762.     Py_END_ALLOW_THREADS
  763.     if (res == NULL)
  764.         return posix_error();
  765.     return PyString_FromString(buf);
  766. }
  767. #endif
  768.  
  769.  
  770. #ifdef HAVE_LINK
  771. static char posix_link__doc__[] =
  772. "link(src, dst) -> None\n\
  773. Create a hard link to a file.";
  774.  
  775. static PyObject *
  776. posix_link(self, args)
  777.     PyObject *self;
  778.     PyObject *args;
  779. {
  780.     return posix_2str(args, "ss:link", link);
  781. }
  782. #endif /* HAVE_LINK */
  783.  
  784.  
  785. static char posix_listdir__doc__[] =
  786. "listdir(path) -> list_of_strings\n\
  787. Return a list containing the names of the entries in the directory.\n\
  788. \n\
  789.     path: path of directory to list\n\
  790. \n\
  791. The list is in arbitrary order.  It does not include the special\n\
  792. entries '.' and '..' even if they are present in the directory.";
  793.  
  794. static PyObject *
  795. posix_listdir(self, args)
  796.     PyObject *self;
  797.     PyObject *args;
  798. {
  799.     /* XXX Should redo this putting the (now four) versions of opendir
  800.        in separate files instead of having them all here... */
  801. #if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
  802.  
  803.     char *name;
  804.     int len;
  805.     PyObject *d, *v;
  806.     HANDLE hFindFile;
  807.     WIN32_FIND_DATA FileData;
  808.     char namebuf[MAX_PATH+5];
  809.  
  810.     if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
  811.         return NULL;
  812.     if (len >= MAX_PATH) {
  813.         PyErr_SetString(PyExc_ValueError, "path too long");
  814.         return NULL;
  815.     }
  816.     strcpy(namebuf, name);
  817.     if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
  818.         namebuf[len++] = '/';
  819.     strcpy(namebuf + len, "*.*");
  820.  
  821.     if ((d = PyList_New(0)) == NULL)
  822.         return NULL;
  823.  
  824.     hFindFile = FindFirstFile(namebuf, &FileData);
  825.     if (hFindFile == INVALID_HANDLE_VALUE) {
  826.         errno = GetLastError();
  827.         if (errno == ERROR_FILE_NOT_FOUND)
  828.             return PyList_New(0);
  829.         return posix_error_with_filename(name);
  830.     }
  831.     do {
  832.         if (FileData.cFileName[0] == '.' &&
  833.             (FileData.cFileName[1] == '\0' ||
  834.              FileData.cFileName[1] == '.' &&
  835.              FileData.cFileName[2] == '\0'))
  836.             continue;
  837.         v = PyString_FromString(FileData.cFileName);
  838.         if (v == NULL) {
  839.             Py_DECREF(d);
  840.             d = NULL;
  841.             break;
  842.         }
  843.         if (PyList_Append(d, v) != 0) {
  844.             Py_DECREF(v);
  845.             Py_DECREF(d);
  846.             d = NULL;
  847.             break;
  848.         }
  849.         Py_DECREF(v);
  850.     } while (FindNextFile(hFindFile, &FileData) == TRUE);
  851.  
  852.     if (FindClose(hFindFile) == FALSE) {
  853.         errno = GetLastError();
  854.         return posix_error_with_filename(&name);
  855.     }
  856.  
  857.     return d;
  858.  
  859. #else /* !MS_WIN32 */
  860. #ifdef _MSC_VER /* 16-bit Windows */
  861.  
  862. #ifndef MAX_PATH
  863. #define MAX_PATH    250
  864. #endif
  865.     char *name, *pt;
  866.     int len;
  867.     PyObject *d, *v;
  868.     char namebuf[MAX_PATH+5];
  869.     struct _find_t ep;
  870.  
  871.     if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
  872.         return NULL;
  873.     if (len >= MAX_PATH) {
  874.         PyErr_SetString(PyExc_ValueError, "path too long");
  875.         return NULL;
  876.     }
  877.     strcpy(namebuf, name);
  878.     for (pt = namebuf; *pt; pt++)
  879.         if (*pt == '/')
  880.             *pt = '\\';
  881.     if (namebuf[len-1] != '\\')
  882.         namebuf[len++] = '\\';
  883.     strcpy(namebuf + len, "*.*");
  884.  
  885.     if ((d = PyList_New(0)) == NULL)
  886.         return NULL;
  887.  
  888.     if (_dos_findfirst(namebuf, _A_RDONLY |
  889.                _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
  890.         {
  891.         errno = ENOENT;
  892.         return posix_error_with_filename(name);
  893.     }
  894.     do {
  895.         if (ep.name[0] == '.' &&
  896.             (ep.name[1] == '\0' ||
  897.              ep.name[1] == '.' &&
  898.              ep.name[2] == '\0'))
  899.             continue;
  900.         strcpy(namebuf, ep.name);
  901.         for (pt = namebuf; *pt; pt++)
  902.             if (isupper(*pt))
  903.                 *pt = tolower(*pt);
  904.         v = PyString_FromString(namebuf);
  905.         if (v == NULL) {
  906.             Py_DECREF(d);
  907.             d = NULL;
  908.             break;
  909.         }
  910.         if (PyList_Append(d, v) != 0) {
  911.             Py_DECREF(v);
  912.             Py_DECREF(d);
  913.             d = NULL;
  914.             break;
  915.         }
  916.         Py_DECREF(v);
  917.     } while (_dos_findnext(&ep) == 0);
  918.  
  919.     return d;
  920.  
  921. #else
  922. #if defined(PYOS_OS2)
  923.  
  924. #ifndef MAX_PATH
  925. #define MAX_PATH    CCHMAXPATH
  926. #endif
  927.     char *name, *pt;
  928.     int len;
  929.     PyObject *d, *v;
  930.     char namebuf[MAX_PATH+5];
  931.     HDIR  hdir = 1;
  932.     ULONG srchcnt = 1;
  933.     FILEFINDBUF3   ep;
  934.     APIRET rc;
  935.  
  936.     if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
  937.         return NULL;
  938.     if (len >= MAX_PATH) {
  939.         PyErr_SetString(PyExc_ValueError, "path too long");
  940.         return NULL;
  941.     }
  942.     strcpy(namebuf, name);
  943.     for (pt = namebuf; *pt; pt++)
  944.         if (*pt == '/')
  945.             *pt = '\\';
  946.     if (namebuf[len-1] != '\\')
  947.         namebuf[len++] = '\\';
  948.     strcpy(namebuf + len, "*.*");
  949.  
  950.     if ((d = PyList_New(0)) == NULL)
  951.         return NULL;
  952.  
  953.     rc = DosFindFirst(namebuf,         /* Wildcard Pattern to Match */
  954.                       &hdir,           /* Handle to Use While Search Directory */
  955.                       FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
  956.                       &ep, sizeof(ep), /* Structure to Receive Directory Entry */
  957.                       &srchcnt,        /* Max and Actual Count of Entries Per Iteration */
  958.                       FIL_STANDARD);   /* Format of Entry (EAs or Not) */
  959.  
  960.     if (rc != NO_ERROR) {
  961.         errno = ENOENT;
  962.         return posix_error_with_filename(name);
  963.     }
  964.  
  965.     if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
  966.         do {
  967.             if (ep.achName[0] == '.'
  968.             && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
  969.                 continue; /* Skip Over "." and ".." Names */
  970.  
  971.             strcpy(namebuf, ep.achName);
  972.  
  973.             /* Leave Case of Name Alone -- In Native Form */
  974.             /* (Removed Forced Lowercasing Code) */
  975.  
  976.             v = PyString_FromString(namebuf);
  977.             if (v == NULL) {
  978.                 Py_DECREF(d);
  979.                 d = NULL;
  980.                 break;
  981.             }
  982.             if (PyList_Append(d, v) != 0) {
  983.                 Py_DECREF(v);
  984.                 Py_DECREF(d);
  985.                 d = NULL;
  986.                 break;
  987.             }
  988.             Py_DECREF(v);
  989.         } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
  990.     }
  991.  
  992.     return d;
  993. #else
  994.  
  995.     char *name;
  996.     PyObject *d, *v;
  997.     DIR *dirp;
  998.     struct dirent *ep;
  999.     if (!PyArg_ParseTuple(args, "s:listdir", &name))
  1000.         return NULL;
  1001.     Py_BEGIN_ALLOW_THREADS
  1002.     if ((dirp = opendir(name)) == NULL) {
  1003.         Py_BLOCK_THREADS
  1004.         return posix_error_with_filename(name);
  1005.     }
  1006.     if ((d = PyList_New(0)) == NULL) {
  1007.         closedir(dirp);
  1008.         Py_BLOCK_THREADS
  1009.         return NULL;
  1010.     }
  1011.     while ((ep = readdir(dirp)) != NULL) {
  1012.         if (ep->d_name[0] == '.' &&
  1013.             (NAMLEN(ep) == 1 ||
  1014.              (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
  1015.             continue;
  1016.         v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
  1017.         if (v == NULL) {
  1018.             Py_DECREF(d);
  1019.             d = NULL;
  1020.             break;
  1021.         }
  1022.         if (PyList_Append(d, v) != 0) {
  1023.             Py_DECREF(v);
  1024.             Py_DECREF(d);
  1025.             d = NULL;
  1026.             break;
  1027.         }
  1028.         Py_DECREF(v);
  1029.     }
  1030.     closedir(dirp);
  1031.     Py_END_ALLOW_THREADS
  1032.  
  1033.     return d;
  1034.  
  1035. #endif /* !PYOS_OS2 */
  1036. #endif /* !_MSC_VER */
  1037. #endif /* !MS_WIN32 */
  1038. }
  1039.  
  1040. static char posix_mkdir__doc__[] =
  1041. "mkdir(path [, mode=0777]) -> None\n\
  1042. Create a directory.";
  1043.  
  1044. static PyObject *
  1045. posix_mkdir(self, args)
  1046.     PyObject *self;
  1047.     PyObject *args;
  1048. {
  1049.     int res;
  1050.     char *path;
  1051.     int mode = 0777;
  1052.     if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
  1053.         return NULL;
  1054.     Py_BEGIN_ALLOW_THREADS
  1055. #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
  1056.     res = mkdir(path);
  1057. #else
  1058.     res = mkdir(path, mode);
  1059. #endif
  1060.     Py_END_ALLOW_THREADS
  1061.     if (res < 0)
  1062.         return posix_error_with_filename(path);
  1063.     Py_INCREF(Py_None);
  1064.     return Py_None;
  1065. }
  1066.  
  1067.  
  1068. #ifdef HAVE_NICE
  1069. static char posix_nice__doc__[] =
  1070. "nice(inc) -> new_priority\n\
  1071. Decrease the priority of process and return new priority.";
  1072.  
  1073. static PyObject *
  1074. posix_nice(self, args)
  1075.     PyObject *self;
  1076.     PyObject *args;
  1077. {
  1078.     int increment, value;
  1079.  
  1080.     if (!PyArg_ParseTuple(args, "i:nice", &increment))
  1081.         return NULL;
  1082.     value = nice(increment);
  1083.     if (value == -1)
  1084.         return posix_error();
  1085.     return PyInt_FromLong((long) value);
  1086. }
  1087. #endif /* HAVE_NICE */
  1088.  
  1089.  
  1090. static char posix_rename__doc__[] =
  1091. "rename(old, new) -> None\n\
  1092. Rename a file or directory.";
  1093.  
  1094. static PyObject *
  1095. posix_rename(self, args)
  1096.     PyObject *self;
  1097.     PyObject *args;
  1098. {
  1099.     return posix_2str(args, "ss:rename", rename);
  1100. }
  1101.  
  1102.  
  1103. static char posix_rmdir__doc__[] =
  1104. "rmdir(path) -> None\n\
  1105. Remove a directory.";
  1106.  
  1107. static PyObject *
  1108. posix_rmdir(self, args)
  1109.     PyObject *self;
  1110.     PyObject *args;
  1111. {
  1112.     return posix_1str(args, "s:rmdir", rmdir);
  1113. }
  1114.  
  1115.  
  1116. static char posix_stat__doc__[] =
  1117. "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
  1118. Perform a stat system call on the given path.";
  1119.  
  1120. static PyObject *
  1121. posix_stat(self, args)
  1122.     PyObject *self;
  1123.     PyObject *args;
  1124. {
  1125.     return posix_do_stat(self, args, "s:stat", stat);
  1126. }
  1127.  
  1128.  
  1129. #ifdef HAVE_SYSTEM
  1130. static char posix_system__doc__[] =
  1131. "system(command) -> exit_status\n\
  1132. Execute the command (a string) in a subshell.";
  1133.  
  1134. static PyObject *
  1135. posix_system(self, args)
  1136.     PyObject *self;
  1137.     PyObject *args;
  1138. {
  1139.     char *command;
  1140.     long sts;
  1141.     if (!PyArg_ParseTuple(args, "s:system", &command))
  1142.         return NULL;
  1143.     Py_BEGIN_ALLOW_THREADS
  1144.     sts = system(command);
  1145.     Py_END_ALLOW_THREADS
  1146.     return PyInt_FromLong(sts);
  1147. }
  1148. #endif
  1149.  
  1150.  
  1151. static char posix_umask__doc__[] =
  1152. "umask(new_mask) -> old_mask\n\
  1153. Set the current numeric umask and return the previous umask.";
  1154.  
  1155. static PyObject *
  1156. posix_umask(self, args)
  1157.     PyObject *self;
  1158.     PyObject *args;
  1159. {
  1160.     int i;
  1161.     if (!PyArg_ParseTuple(args, "i:umask", &i))
  1162.         return NULL;
  1163.     i = umask(i);
  1164.     if (i < 0)
  1165.         return posix_error();
  1166.     return PyInt_FromLong((long)i);
  1167. }
  1168.  
  1169.  
  1170. static char posix_unlink__doc__[] =
  1171. "unlink(path) -> None\n\
  1172. Remove a file (same as remove(path)).";
  1173.  
  1174. static char posix_remove__doc__[] =
  1175. "remove(path) -> None\n\
  1176. Remove a file (same as unlink(path)).";
  1177.  
  1178. static PyObject *
  1179. posix_unlink(self, args)
  1180.     PyObject *self;
  1181.     PyObject *args;
  1182. {
  1183.     return posix_1str(args, "s:remove", unlink);
  1184. }
  1185.  
  1186.  
  1187. #ifdef HAVE_UNAME
  1188. static char posix_uname__doc__[] =
  1189. "uname() -> (sysname, nodename, release, version, machine)\n\
  1190. Return a tuple identifying the current operating system.";
  1191.  
  1192. static PyObject *
  1193. posix_uname(self, args)
  1194.     PyObject *self;
  1195.     PyObject *args;
  1196. {
  1197.     struct utsname u;
  1198.     int res;
  1199.     if (!PyArg_ParseTuple(args, ":uname"))
  1200.         return NULL;
  1201.     Py_BEGIN_ALLOW_THREADS
  1202.     res = uname(&u);
  1203.     Py_END_ALLOW_THREADS
  1204.     if (res < 0)
  1205.         return posix_error();
  1206.     return Py_BuildValue("(sssss)",
  1207.                  u.sysname,
  1208.                  u.nodename,
  1209.                  u.release,
  1210.                  u.version,
  1211.                  u.machine);
  1212. }
  1213. #endif /* HAVE_UNAME */
  1214.  
  1215.  
  1216. static char posix_utime__doc__[] =
  1217. "utime(path, (atime, utime)) -> None\n\
  1218. Set the access and modified time of the file to the given values.";
  1219.  
  1220. static PyObject *
  1221. posix_utime(self, args)
  1222.     PyObject *self;
  1223.     PyObject *args;
  1224. {
  1225.     char *path;
  1226.     long atime, mtime;
  1227.     int res;
  1228.  
  1229. /* XXX should define struct utimbuf instead, above */
  1230. #ifdef HAVE_UTIME_H
  1231.     struct utimbuf buf;
  1232. #define ATIME buf.actime
  1233. #define MTIME buf.modtime
  1234. #define UTIME_ARG &buf
  1235. #else /* HAVE_UTIME_H */
  1236.     time_t buf[2];
  1237. #define ATIME buf[0]
  1238. #define MTIME buf[1]
  1239. #define UTIME_ARG buf
  1240. #endif /* HAVE_UTIME_H */
  1241.  
  1242.     if (!PyArg_ParseTuple(args, "s(ll):utime", &path, &atime, &mtime))
  1243.         return NULL;
  1244.     ATIME = atime;
  1245.     MTIME = mtime;
  1246.     Py_BEGIN_ALLOW_THREADS
  1247.     res = utime(path, UTIME_ARG);
  1248.     Py_END_ALLOW_THREADS
  1249.     if (res < 0)
  1250.         return posix_error_with_filename(path);
  1251.     Py_INCREF(Py_None);
  1252.     return Py_None;
  1253. #undef UTIME_ARG
  1254. #undef ATIME
  1255. #undef MTIME
  1256. }
  1257.  
  1258.  
  1259. /* Process operations */
  1260.  
  1261. static char posix__exit__doc__[] =
  1262. "_exit(status)\n\
  1263. Exit to the system with specified status, without normal exit processing.";
  1264.  
  1265. static PyObject *
  1266. posix__exit(self, args)
  1267.     PyObject *self;
  1268.     PyObject *args;
  1269. {
  1270.     int sts;
  1271.     if (!PyArg_ParseTuple(args, "i:_exit", &sts))
  1272.         return NULL;
  1273.     _exit(sts);
  1274.     return NULL; /* Make gcc -Wall happy */
  1275. }
  1276.  
  1277.  
  1278. #ifdef HAVE_EXECV
  1279. static char posix_execv__doc__[] =
  1280. "execv(path, args)\n\
  1281. Execute an executable path with arguments, replacing current process.\n\
  1282. \n\
  1283.     path: path of executable file\n\
  1284.     args: tuple or list of strings";
  1285.  
  1286. static PyObject *
  1287. posix_execv(self, args)
  1288.     PyObject *self;
  1289.     PyObject *args;
  1290. {
  1291.     char *path;
  1292.     PyObject *argv;
  1293.     char **argvlist;
  1294.     int i, argc;
  1295.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1296.  
  1297.     /* execv has two arguments: (path, argv), where
  1298.        argv is a list or tuple of strings. */
  1299.  
  1300.     if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
  1301.         return NULL;
  1302.     if (PyList_Check(argv)) {
  1303.         argc = PyList_Size(argv);
  1304.         getitem = PyList_GetItem;
  1305.     }
  1306.     else if (PyTuple_Check(argv)) {
  1307.         argc = PyTuple_Size(argv);
  1308.         getitem = PyTuple_GetItem;
  1309.     }
  1310.     else {
  1311.  badarg:
  1312.         PyErr_BadArgument();
  1313.         return NULL;
  1314.     }
  1315.  
  1316.     argvlist = PyMem_NEW(char *, argc+1);
  1317.     if (argvlist == NULL)
  1318.         return NULL;
  1319.     for (i = 0; i < argc; i++) {
  1320.         if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
  1321.             PyMem_DEL(argvlist);
  1322.             goto badarg;
  1323.         }
  1324.     }
  1325.     argvlist[argc] = NULL;
  1326.  
  1327. #ifdef BAD_EXEC_PROTOTYPES
  1328.     execv(path, (const char **) argvlist);
  1329. #else /* BAD_EXEC_PROTOTYPES */
  1330.     execv(path, argvlist);
  1331. #endif /* BAD_EXEC_PROTOTYPES */
  1332.  
  1333.     /* If we get here it's definitely an error */
  1334.  
  1335.     PyMem_DEL(argvlist);
  1336.     return posix_error();
  1337. }
  1338.  
  1339.  
  1340. static char posix_execve__doc__[] =
  1341. "execve(path, args, env)\n\
  1342. Execute a path with arguments and environment, replacing current process.\n\
  1343. \n\
  1344.     path: path of executable file\n\
  1345.     args: tuple or list of arguments\n\
  1346.     env: dictonary of strings mapping to strings";
  1347.  
  1348. static PyObject *
  1349. posix_execve(self, args)
  1350.     PyObject *self;
  1351.     PyObject *args;
  1352. {
  1353.     char *path;
  1354.     PyObject *argv, *env;
  1355.     char **argvlist;
  1356.     char **envlist;
  1357.     PyObject *key, *val, *keys=NULL, *vals=NULL;
  1358.     int i, pos, argc, envc;
  1359.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1360.  
  1361.     /* execve has three arguments: (path, argv, env), where
  1362.        argv is a list or tuple of strings and env is a dictionary
  1363.        like posix.environ. */
  1364.  
  1365.     if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
  1366.         return NULL;
  1367.     if (PyList_Check(argv)) {
  1368.         argc = PyList_Size(argv);
  1369.         getitem = PyList_GetItem;
  1370.     }
  1371.     else if (PyTuple_Check(argv)) {
  1372.         argc = PyTuple_Size(argv);
  1373.         getitem = PyTuple_GetItem;
  1374.     }
  1375.     else {
  1376.         PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
  1377.         return NULL;
  1378.     }
  1379.     if (!PyMapping_Check(env)) {
  1380.         PyErr_SetString(PyExc_TypeError, "env must be mapping object");
  1381.         return NULL;
  1382.     }
  1383.  
  1384.     argvlist = PyMem_NEW(char *, argc+1);
  1385.     if (argvlist == NULL) {
  1386.         PyErr_NoMemory();
  1387.         return NULL;
  1388.     }
  1389.     for (i = 0; i < argc; i++) {
  1390.         if (!PyArg_Parse((*getitem)(argv, i),
  1391.                  "s;argv must be list of strings",
  1392.                  &argvlist[i]))
  1393.         {
  1394.             goto fail_1;
  1395.         }
  1396.     }
  1397.     argvlist[argc] = NULL;
  1398.  
  1399.     i = PyMapping_Length(env);
  1400.     envlist = PyMem_NEW(char *, i + 1);
  1401.     if (envlist == NULL) {
  1402.         PyErr_NoMemory();
  1403.         goto fail_1;
  1404.     }
  1405.     envc = 0;
  1406.     keys = PyMapping_Keys(env);
  1407.     vals = PyMapping_Values(env);
  1408.     if (!keys || !vals)
  1409.         goto fail_2;
  1410.     
  1411.     for (pos = 0; pos < i; pos++) {
  1412.         char *p, *k, *v;
  1413.  
  1414.         key = PyList_GetItem(keys, pos);
  1415.         val = PyList_GetItem(vals, pos);
  1416.         if (!key || !val)
  1417.             goto fail_2;
  1418.         
  1419.         if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
  1420.             !PyArg_Parse(val, "s;non-string value in env", &v))
  1421.         {
  1422.             goto fail_2;
  1423.         }
  1424.  
  1425. #if defined(PYOS_OS2)
  1426.         /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
  1427.         if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
  1428. #endif
  1429.         p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
  1430.         if (p == NULL) {
  1431.             PyErr_NoMemory();
  1432.             goto fail_2;
  1433.         }
  1434.         sprintf(p, "%s=%s", k, v);
  1435.         envlist[envc++] = p;
  1436. #if defined(PYOS_OS2)
  1437.     }
  1438. #endif
  1439.     }
  1440.     envlist[envc] = 0;
  1441.  
  1442.  
  1443. #ifdef BAD_EXEC_PROTOTYPES
  1444.     execve(path, (const char **)argvlist, envlist);
  1445. #else /* BAD_EXEC_PROTOTYPES */
  1446.     execve(path, argvlist, envlist);
  1447. #endif /* BAD_EXEC_PROTOTYPES */
  1448.     
  1449.     /* If we get here it's definitely an error */
  1450.  
  1451.     (void) posix_error();
  1452.  
  1453.  fail_2:
  1454.     while (--envc >= 0)
  1455.         PyMem_DEL(envlist[envc]);
  1456.     PyMem_DEL(envlist);
  1457.  fail_1:
  1458.     PyMem_DEL(argvlist);
  1459.     Py_XDECREF(vals);
  1460.     Py_XDECREF(keys);
  1461.     return NULL;
  1462. }
  1463. #endif /* HAVE_EXECV */
  1464.  
  1465.  
  1466. #ifdef HAVE_SPAWNV
  1467. static char posix_spawnv__doc__[] =
  1468. "spawnv(mode, path, args)\n\
  1469. Execute an executable path with arguments, replacing current process.\n\
  1470. \n\
  1471.     mode: mode of process creation\n\
  1472.     path: path of executable file\n\
  1473.     args: tuple or list of strings";
  1474.  
  1475. static PyObject *
  1476. posix_spawnv(self, args)
  1477.     PyObject *self;
  1478.     PyObject *args;
  1479. {
  1480.     char *path;
  1481.     PyObject *argv;
  1482.     char **argvlist;
  1483.     int mode, i, argc;
  1484.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1485.  
  1486.     /* spawnv has three arguments: (mode, path, argv), where
  1487.        argv is a list or tuple of strings. */
  1488.  
  1489.     if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
  1490.         return NULL;
  1491.     if (PyList_Check(argv)) {
  1492.         argc = PyList_Size(argv);
  1493.         getitem = PyList_GetItem;
  1494.     }
  1495.     else if (PyTuple_Check(argv)) {
  1496.         argc = PyTuple_Size(argv);
  1497.         getitem = PyTuple_GetItem;
  1498.     }
  1499.     else {
  1500.  badarg:
  1501.         PyErr_BadArgument();
  1502.         return NULL;
  1503.     }
  1504.  
  1505.     argvlist = PyMem_NEW(char *, argc+1);
  1506.     if (argvlist == NULL)
  1507.         return NULL;
  1508.     for (i = 0; i < argc; i++) {
  1509.         if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
  1510.             PyMem_DEL(argvlist);
  1511.             goto badarg;
  1512.         }
  1513.     }
  1514.     argvlist[argc] = NULL;
  1515.  
  1516.     if (mode == _OLD_P_OVERLAY)
  1517.         mode = _P_OVERLAY;
  1518.     i = _spawnv(mode, path, argvlist);
  1519.  
  1520.     PyMem_DEL(argvlist);
  1521.  
  1522.     if (i == -1)
  1523.         return posix_error();
  1524.     else
  1525.         return Py_BuildValue("i", i);
  1526. }
  1527.  
  1528.  
  1529. static char posix_spawnve__doc__[] =
  1530. "spawnve(mode, path, args, env)\n\
  1531. Execute a path with arguments and environment, replacing current process.\n\
  1532. \n\
  1533.     mode: mode of process creation\n\
  1534.     path: path of executable file\n\
  1535.     args: tuple or list of arguments\n\
  1536.     env: dictonary of strings mapping to strings";
  1537.  
  1538. static PyObject *
  1539. posix_spawnve(self, args)
  1540.     PyObject *self;
  1541.     PyObject *args;
  1542. {
  1543.     char *path;
  1544.     PyObject *argv, *env;
  1545.     char **argvlist;
  1546.     char **envlist;
  1547.     PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
  1548.     int mode, i, pos, argc, envc;
  1549.     PyObject *(*getitem) Py_PROTO((PyObject *, int));
  1550.  
  1551.     /* spawnve has four arguments: (mode, path, argv, env), where
  1552.        argv is a list or tuple of strings and env is a dictionary
  1553.        like posix.environ. */
  1554.  
  1555.     if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
  1556.         return NULL;
  1557.     if (PyList_Check(argv)) {
  1558.         argc = PyList_Size(argv);
  1559.         getitem = PyList_GetItem;
  1560.     }
  1561.     else if (PyTuple_Check(argv)) {
  1562.         argc = PyTuple_Size(argv);
  1563.         getitem = PyTuple_GetItem;
  1564.     }
  1565.     else {
  1566.         PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
  1567.         return NULL;
  1568.     }
  1569.     if (!PyMapping_Check(env)) {
  1570.         PyErr_SetString(PyExc_TypeError, "env must be mapping object");
  1571.         return NULL;
  1572.     }
  1573.  
  1574.     argvlist = PyMem_NEW(char *, argc+1);
  1575.     if (argvlist == NULL) {
  1576.         PyErr_NoMemory();
  1577.         return NULL;
  1578.     }
  1579.     for (i = 0; i < argc; i++) {
  1580.         if (!PyArg_Parse((*getitem)(argv, i),
  1581.                  "s;argv must be list of strings",
  1582.                  &argvlist[i]))
  1583.         {
  1584.             goto fail_1;
  1585.         }
  1586.     }
  1587.     argvlist[argc] = NULL;
  1588.  
  1589.     i = PyMapping_Length(env);
  1590.     envlist = PyMem_NEW(char *, i + 1);
  1591.     if (envlist == NULL) {
  1592.         PyErr_NoMemory();
  1593.         goto fail_1;
  1594.     }
  1595.     envc = 0;
  1596.     keys = PyMapping_Keys(env);
  1597.     vals = PyMapping_Values(env);
  1598.     if (!keys || !vals)
  1599.         goto fail_2;
  1600.     
  1601.     for (pos = 0; pos < i; pos++) {
  1602.         char *p, *k, *v;
  1603.  
  1604.         key = PyList_GetItem(keys, pos);
  1605.         val = PyList_GetItem(vals, pos);
  1606.         if (!key || !val)
  1607.             goto fail_2;
  1608.         
  1609.         if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
  1610.             !PyArg_Parse(val, "s;non-string value in env", &v))
  1611.         {
  1612.             goto fail_2;
  1613.         }
  1614.         p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
  1615.         if (p == NULL) {
  1616.             PyErr_NoMemory();
  1617.             goto fail_2;
  1618.         }
  1619.         sprintf(p, "%s=%s", k, v);
  1620.         envlist[envc++] = p;
  1621.     }
  1622.     envlist[envc] = 0;
  1623.  
  1624.     if (mode == _OLD_P_OVERLAY)
  1625.         mode = _P_OVERLAY;
  1626.     i = _spawnve(mode, path, argvlist, envlist);
  1627.     if (i == -1)
  1628.         (void) posix_error();
  1629.     else
  1630.         res = Py_BuildValue("i", i);
  1631.  
  1632.  fail_2:
  1633.     while (--envc >= 0)
  1634.         PyMem_DEL(envlist[envc]);
  1635.     PyMem_DEL(envlist);
  1636.  fail_1:
  1637.     PyMem_DEL(argvlist);
  1638.     Py_XDECREF(vals);
  1639.     Py_XDECREF(keys);
  1640.     return res;
  1641. }
  1642. #endif /* HAVE_SPAWNV */
  1643.  
  1644.  
  1645. #ifdef HAVE_FORK
  1646. static char posix_fork__doc__[] =
  1647. "fork() -> pid\n\
  1648. Fork a child process.\n\
  1649. \n\
  1650. Return 0 to child process and PID of child to parent process.";
  1651.  
  1652. static PyObject *
  1653. posix_fork(self, args)
  1654.     PyObject *self;
  1655.     PyObject *args;
  1656. {
  1657.     int pid;
  1658.     if (!PyArg_ParseTuple(args, ":fork"))
  1659.         return NULL;
  1660.     pid = fork();
  1661.     if (pid == -1)
  1662.         return posix_error();
  1663.     PyOS_AfterFork();
  1664.     return PyInt_FromLong((long)pid);
  1665. }
  1666. #endif
  1667.  
  1668.  
  1669. #ifdef HAVE_GETEGID
  1670. static char posix_getegid__doc__[] =
  1671. "getegid() -> egid\n\
  1672. Return the current process's effective group id.";
  1673.  
  1674. static PyObject *
  1675. posix_getegid(self, args)
  1676.     PyObject *self;
  1677.     PyObject *args;
  1678. {
  1679.     if (!PyArg_ParseTuple(args, ":getegid"))
  1680.         return NULL;
  1681.     return PyInt_FromLong((long)getegid());
  1682. }
  1683. #endif
  1684.  
  1685.  
  1686. #ifdef HAVE_GETEUID
  1687. static char posix_geteuid__doc__[] =
  1688. "geteuid() -> euid\n\
  1689. Return the current process's effective user id.";
  1690.  
  1691. static PyObject *
  1692. posix_geteuid(self, args)
  1693.     PyObject *self;
  1694.     PyObject *args;
  1695. {
  1696.     if (!PyArg_ParseTuple(args, ":geteuid"))
  1697.         return NULL;
  1698.     return PyInt_FromLong((long)geteuid());
  1699. }
  1700. #endif
  1701.  
  1702.  
  1703. #ifdef HAVE_GETGID
  1704. static char posix_getgid__doc__[] =
  1705. "getgid() -> gid\n\
  1706. Return the current process's group id.";
  1707.  
  1708. static PyObject *
  1709. posix_getgid(self, args)
  1710.     PyObject *self;
  1711.     PyObject *args;
  1712. {
  1713.     if (!PyArg_ParseTuple(args, ":getgid"))
  1714.         return NULL;
  1715.     return PyInt_FromLong((long)getgid());
  1716. }
  1717. #endif
  1718.  
  1719.  
  1720. static char posix_getpid__doc__[] =
  1721. "getpid() -> pid\n\
  1722. Return the current process id";
  1723.  
  1724. static PyObject *
  1725. posix_getpid(self, args)
  1726.     PyObject *self;
  1727.     PyObject *args;
  1728. {
  1729.     if (!PyArg_ParseTuple(args, ":getpid"))
  1730.         return NULL;
  1731.     return PyInt_FromLong((long)getpid());
  1732. }
  1733.  
  1734.  
  1735. #ifdef HAVE_GETGROUPS
  1736. static char posix_getgroups__doc__[] = "\
  1737. getgroups() -> list of group IDs\n\
  1738. Return list of supplemental group IDs for the process.";
  1739.  
  1740. static PyObject *
  1741. posix_getgroups(self, args)
  1742.      PyObject *self;
  1743.      PyObject *args;
  1744. {
  1745.     PyObject *result = NULL;
  1746.  
  1747.     if (PyArg_ParseTuple(args, ":getgroups")) {
  1748. #ifdef NGROUPS_MAX
  1749. #define MAX_GROUPS NGROUPS_MAX
  1750. #else
  1751.         /* defined to be 16 on Solaris7, so this should be a small number */
  1752. #define MAX_GROUPS 64
  1753. #endif
  1754.         gid_t grouplist[MAX_GROUPS];
  1755.         int n;
  1756.  
  1757.         n = getgroups(MAX_GROUPS, grouplist);
  1758.         if (n < 0)
  1759.             posix_error();
  1760.         else {
  1761.             result = PyList_New(n);
  1762.             if (result != NULL) {
  1763.                 PyObject *o;
  1764.                 int i;
  1765.                 for (i = 0; i < n; ++i) {
  1766.                     o = PyInt_FromLong((long)grouplist[i]);
  1767.                     if (o == NULL) {
  1768.                         Py_DECREF(result);
  1769.                         result = NULL;
  1770.                         break;
  1771.                     }
  1772.                     PyList_SET_ITEM(result, i, o);
  1773.                 }
  1774.             }
  1775.         }
  1776.     }
  1777.     return result;
  1778. }
  1779. #endif
  1780.  
  1781. #ifdef HAVE_GETPGRP
  1782. static char posix_getpgrp__doc__[] =
  1783. "getpgrp() -> pgrp\n\
  1784. Return the current process group id.";
  1785.  
  1786. static PyObject *
  1787. posix_getpgrp(self, args)
  1788.     PyObject *self;
  1789.     PyObject *args;
  1790. {
  1791.     if (!PyArg_ParseTuple(args, ":getpgrp"))
  1792.         return NULL;
  1793. #ifdef GETPGRP_HAVE_ARG
  1794.     return PyInt_FromLong((long)getpgrp(0));
  1795. #else /* GETPGRP_HAVE_ARG */
  1796.     return PyInt_FromLong((long)getpgrp());
  1797. #endif /* GETPGRP_HAVE_ARG */
  1798. }
  1799. #endif /* HAVE_GETPGRP */
  1800.  
  1801.  
  1802. #ifdef HAVE_SETPGRP
  1803. static char posix_setpgrp__doc__[] =
  1804. "setpgrp() -> None\n\
  1805. Make this process a session leader.";
  1806.  
  1807. static PyObject *
  1808. posix_setpgrp(self, args)
  1809.     PyObject *self;
  1810.     PyObject *args;
  1811. {
  1812.     if (!PyArg_ParseTuple(args, ":setpgrp"))
  1813.         return NULL;
  1814. #ifdef SETPGRP_HAVE_ARG
  1815.     if (setpgrp(0, 0) < 0)
  1816. #else /* SETPGRP_HAVE_ARG */
  1817.     if (setpgrp() < 0)
  1818. #endif /* SETPGRP_HAVE_ARG */
  1819.         return posix_error();
  1820.     Py_INCREF(Py_None);
  1821.     return Py_None;
  1822. }
  1823.  
  1824. #endif /* HAVE_SETPGRP */
  1825.  
  1826. #ifdef HAVE_GETPPID
  1827. static char posix_getppid__doc__[] =
  1828. "getppid() -> ppid\n\
  1829. Return the parent's process id.";
  1830.  
  1831. static PyObject *
  1832. posix_getppid(self, args)
  1833.     PyObject *self;
  1834.     PyObject *args;
  1835. {
  1836.     if (!PyArg_ParseTuple(args, ":getppid"))
  1837.         return NULL;
  1838.     return PyInt_FromLong((long)getppid());
  1839. }
  1840. #endif
  1841.  
  1842.  
  1843. #ifdef HAVE_GETLOGIN
  1844. static char posix_getlogin__doc__[] = "\
  1845. getlogin() -> string\n\
  1846. Return the actual login name.";
  1847.  
  1848. static PyObject *
  1849. posix_getlogin(self, args)
  1850.     PyObject *self;
  1851.     PyObject *args;
  1852. {
  1853.     PyObject *result = NULL;
  1854.  
  1855.     if (PyArg_ParseTuple(args, ":getlogin")) {
  1856.         char *name = getlogin();
  1857.  
  1858.         if (name == NULL)
  1859.             posix_error();
  1860.         else
  1861.             result = PyString_FromString(name);
  1862.     }
  1863.     return result;
  1864. }
  1865. #endif
  1866.  
  1867. #ifdef HAVE_GETUID
  1868. static char posix_getuid__doc__[] =
  1869. "getuid() -> uid\n\
  1870. Return the current process's user id.";
  1871.  
  1872. static PyObject *
  1873. posix_getuid(self, args)
  1874.     PyObject *self;
  1875.     PyObject *args;
  1876. {
  1877.     if (!PyArg_ParseTuple(args, ":getuid"))
  1878.         return NULL;
  1879.     return PyInt_FromLong((long)getuid());
  1880. }
  1881. #endif
  1882.  
  1883.  
  1884. #ifdef HAVE_KILL
  1885. static char posix_kill__doc__[] =
  1886. "kill(pid, sig) -> None\n\
  1887. Kill a process with a signal.";
  1888.  
  1889. static PyObject *
  1890. posix_kill(self, args)
  1891.     PyObject *self;
  1892.     PyObject *args;
  1893. {
  1894.     int pid, sig;
  1895.     if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
  1896.         return NULL;
  1897. #if defined(PYOS_OS2)
  1898.     if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
  1899.         APIRET rc;
  1900.         if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
  1901.             return os2_error(rc);
  1902.  
  1903.     } else if (sig == XCPT_SIGNAL_KILLPROC) {
  1904.         APIRET rc;
  1905.         if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
  1906.             return os2_error(rc);
  1907.  
  1908.     } else
  1909.         return NULL; /* Unrecognized Signal Requested */
  1910. #else
  1911.     if (kill(pid, sig) == -1)
  1912.         return posix_error();
  1913. #endif
  1914.     Py_INCREF(Py_None);
  1915.     return Py_None;
  1916. }
  1917. #endif
  1918.  
  1919. #ifdef HAVE_PLOCK
  1920.  
  1921. #ifdef HAVE_SYS_LOCK_H
  1922. #include <sys/lock.h>
  1923. #endif
  1924.  
  1925. static char posix_plock__doc__[] =
  1926. "plock(op) -> None\n\
  1927. Lock program segments into memory.";
  1928.  
  1929. static PyObject *
  1930. posix_plock(self, args)
  1931.     PyObject *self;
  1932.     PyObject *args;
  1933. {
  1934.     int op;
  1935.     if (!PyArg_ParseTuple(args, "i:plock", &op))
  1936.         return NULL;
  1937.     if (plock(op) == -1)
  1938.         return posix_error();
  1939.     Py_INCREF(Py_None);
  1940.     return Py_None;
  1941. }
  1942. #endif
  1943.  
  1944.  
  1945. #ifdef HAVE_POPEN
  1946. static char posix_popen__doc__[] =
  1947. "popen(command [, mode='r' [, bufsize]]) -> pipe\n\
  1948. Open a pipe to/from a command returning a file object.";
  1949.  
  1950. #if defined(PYOS_OS2)
  1951. static int
  1952. async_system(const char *command)
  1953. {
  1954.     char        *p, errormsg[256], args[1024];
  1955.     RESULTCODES  rcodes;
  1956.     APIRET       rc;
  1957.     char        *shell = getenv("COMSPEC");
  1958.     if (!shell)
  1959.         shell = "cmd";
  1960.  
  1961.     strcpy(args, shell);
  1962.     p = &args[ strlen(args)+1 ];
  1963.     strcpy(p, "/c ");
  1964.     strcat(p, command);
  1965.     p += strlen(p) + 1;
  1966.     *p = '\0';
  1967.  
  1968.     rc = DosExecPgm(errormsg, sizeof(errormsg),
  1969.                     EXEC_ASYNC, /* Execute Async w/o Wait for Results */
  1970.                     args,
  1971.                     NULL,       /* Inherit Parent's Environment */
  1972.                     &rcodes, shell);
  1973.     return rc;
  1974. }
  1975.  
  1976. static FILE *
  1977. popen(const char *command, const char *mode, int pipesize, int *err)
  1978. {
  1979.     HFILE    rhan, whan;
  1980.     FILE    *retfd = NULL;
  1981.     APIRET   rc = DosCreatePipe(&rhan, &whan, pipesize);
  1982.  
  1983.     if (rc != NO_ERROR) {
  1984.     *err = rc;
  1985.         return NULL; /* ERROR - Unable to Create Anon Pipe */
  1986.     }
  1987.  
  1988.     if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
  1989.         int oldfd = dup(1);      /* Save STDOUT Handle in Another Handle */
  1990.  
  1991.         DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
  1992.         close(1);                /* Make STDOUT Available for Reallocation */
  1993.  
  1994.         if (dup2(whan, 1) == 0) {      /* Connect STDOUT to Pipe Write Side */
  1995.             DosClose(whan);            /* Close Now-Unused Pipe Write Handle */
  1996.  
  1997.             if (async_system(command) == NO_ERROR)
  1998.                 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
  1999.         }
  2000.  
  2001.         dup2(oldfd, 1);          /* Reconnect STDOUT to Original Handle */
  2002.         DosExitCritSec();        /* Now Allow Other Threads to Run */
  2003.  
  2004.         close(oldfd);            /* And Close Saved STDOUT Handle */
  2005.         return retfd;            /* Return fd of Pipe or NULL if Error */
  2006.  
  2007.     } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
  2008.         int oldfd = dup(0);      /* Save STDIN Handle in Another Handle */
  2009.  
  2010.         DosEnterCritSec();      /* Stop Other Threads While Changing Handles */
  2011.         close(0);                /* Make STDIN Available for Reallocation */
  2012.  
  2013.         if (dup2(rhan, 0) == 0)     { /* Connect STDIN to Pipe Read Side */
  2014.             DosClose(rhan);           /* Close Now-Unused Pipe Read Handle */
  2015.  
  2016.             if (async_system(command) == NO_ERROR)
  2017.                 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
  2018.         }
  2019.  
  2020.         dup2(oldfd, 0);          /* Reconnect STDIN to Original Handle */
  2021.         DosExitCritSec();        /* Now Allow Other Threads to Run */
  2022.  
  2023.         close(oldfd);            /* And Close Saved STDIN Handle */
  2024.         return retfd;            /* Return fd of Pipe or NULL if Error */
  2025.  
  2026.     } else {
  2027.     *err = ERROR_INVALID_ACCESS;
  2028.         return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
  2029.     }
  2030. }
  2031.  
  2032. static PyObject *
  2033. posix_popen(self, args)
  2034.     PyObject *self;
  2035.     PyObject *args;
  2036. {
  2037.     char *name;
  2038.     char *mode = "r";
  2039.     int   err, bufsize = -1;
  2040.     FILE *fp;
  2041.     PyObject *f;
  2042.     if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
  2043.         return NULL;
  2044.     Py_BEGIN_ALLOW_THREADS
  2045.     fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
  2046.     Py_END_ALLOW_THREADS
  2047.     if (fp == NULL)
  2048.         return os2_error(err);
  2049.  
  2050.     f = PyFile_FromFile(fp, name, mode, fclose);
  2051.     if (f != NULL)
  2052.         PyFile_SetBufSize(f, bufsize);
  2053.     return f;
  2054. }
  2055.  
  2056. #else
  2057. static PyObject *
  2058. posix_popen(self, args)
  2059.     PyObject *self;
  2060.     PyObject *args;
  2061. {
  2062.     char *name;
  2063.     char *mode = "r";
  2064.     int bufsize = -1;
  2065.     FILE *fp;
  2066.     PyObject *f;
  2067.     if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
  2068.         return NULL;
  2069.     Py_BEGIN_ALLOW_THREADS
  2070.     fp = popen(name, mode);
  2071.     Py_END_ALLOW_THREADS
  2072.     if (fp == NULL)
  2073.         return posix_error();
  2074.     f = PyFile_FromFile(fp, name, mode, pclose);
  2075.     if (f != NULL)
  2076.         PyFile_SetBufSize(f, bufsize);
  2077.     return f;
  2078. }
  2079. #endif
  2080.  
  2081. #endif /* HAVE_POPEN */
  2082.  
  2083.  
  2084. #ifdef HAVE_SETUID
  2085. static char posix_setuid__doc__[] =
  2086. "setuid(uid) -> None\n\
  2087. Set the current process's user id.";
  2088. static PyObject *
  2089. posix_setuid(self, args)
  2090.     PyObject *self;
  2091.     PyObject *args;
  2092. {
  2093.     int uid;
  2094.     if (!PyArg_ParseTuple(args, "i:setuid", &uid))
  2095.         return NULL;
  2096.     if (setuid(uid) < 0)
  2097.         return posix_error();
  2098.     Py_INCREF(Py_None);
  2099.     return Py_None;
  2100. }
  2101. #endif /* HAVE_SETUID */
  2102.  
  2103.  
  2104. #ifdef HAVE_SETGID
  2105. static char posix_setgid__doc__[] =
  2106. "setgid(gid) -> None\n\
  2107. Set the current process's group id.";
  2108.  
  2109. static PyObject *
  2110. posix_setgid(self, args)
  2111.     PyObject *self;
  2112.     PyObject *args;
  2113. {
  2114.     int gid;
  2115.     if (!PyArg_ParseTuple(args, "i:setgid", &gid))
  2116.         return NULL;
  2117.     if (setgid(gid) < 0)
  2118.         return posix_error();
  2119.     Py_INCREF(Py_None);
  2120.     return Py_None;
  2121. }
  2122. #endif /* HAVE_SETGID */
  2123.  
  2124.  
  2125. #ifdef HAVE_WAITPID
  2126. static char posix_waitpid__doc__[] =
  2127. "waitpid(pid, options) -> (pid, status)\n\
  2128. Wait for completion of a give child process.";
  2129.  
  2130. static PyObject *
  2131. posix_waitpid(self, args)
  2132.     PyObject *self;
  2133.     PyObject *args;
  2134. {
  2135.     int pid, options;
  2136. #ifdef UNION_WAIT
  2137.     union wait status;
  2138. #define status_i (status.w_status)
  2139. #else
  2140.     int status;
  2141. #define status_i status
  2142. #endif
  2143.     status_i = 0;
  2144.  
  2145.     if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
  2146.         return NULL;
  2147.     Py_BEGIN_ALLOW_THREADS
  2148. #ifdef NeXT
  2149.     pid = wait4(pid, &status, options, NULL);
  2150. #else
  2151.     pid = waitpid(pid, &status, options);
  2152. #endif
  2153.     Py_END_ALLOW_THREADS
  2154.     if (pid == -1)
  2155.         return posix_error();
  2156.     else
  2157.         return Py_BuildValue("ii", pid, status_i);
  2158. }
  2159. #endif /* HAVE_WAITPID */
  2160.  
  2161.  
  2162. #ifdef HAVE_WAIT
  2163. static char posix_wait__doc__[] =
  2164. "wait() -> (pid, status)\n\
  2165. Wait for completion of a child process.";
  2166.  
  2167. static PyObject *
  2168. posix_wait(self, args)
  2169.     PyObject *self;
  2170.     PyObject *args;
  2171. {
  2172.     int pid;
  2173. #ifdef UNION_WAIT
  2174.     union wait status;
  2175. #define status_i (status.w_status)
  2176. #else
  2177.     int status;
  2178. #define status_i status
  2179. #endif
  2180.         if (!PyArg_ParseTuple(args, ":wait"))
  2181.                 return NULL;
  2182.     status_i = 0;
  2183.     Py_BEGIN_ALLOW_THREADS
  2184.     pid = wait(&status);
  2185.     Py_END_ALLOW_THREADS
  2186.     if (pid == -1)
  2187.         return posix_error();
  2188.     else
  2189.         return Py_BuildValue("ii", pid, status_i);
  2190. #undef status_i
  2191. }
  2192. #endif
  2193.  
  2194.  
  2195. static char posix_lstat__doc__[] =
  2196. "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
  2197. Like stat(path), but do not follow symbolic links.";
  2198.  
  2199. static PyObject *
  2200. posix_lstat(self, args)
  2201.     PyObject *self;
  2202.     PyObject *args;
  2203. {
  2204. #ifdef HAVE_LSTAT
  2205.     return posix_do_stat(self, args, "s:lstat", lstat);
  2206. #else /* !HAVE_LSTAT */
  2207.     return posix_do_stat(self, args, "s:lstat", stat);
  2208. #endif /* !HAVE_LSTAT */
  2209. }
  2210.  
  2211.  
  2212. #ifdef HAVE_READLINK
  2213. static char posix_readlink__doc__[] =
  2214. "readlink(path) -> path\n\
  2215. Return a string representing the path to which the symbolic link points.";
  2216.  
  2217. static PyObject *
  2218. posix_readlink(self, args)
  2219.     PyObject *self;
  2220.     PyObject *args;
  2221. {
  2222.     char buf[MAXPATHLEN];
  2223.     char *path;
  2224.     int n;
  2225.     if (!PyArg_ParseTuple(args, "s:readlink", &path))
  2226.         return NULL;
  2227.     Py_BEGIN_ALLOW_THREADS
  2228.     n = readlink(path, buf, (int) sizeof buf);
  2229.     Py_END_ALLOW_THREADS
  2230.     if (n < 0)
  2231.         return posix_error_with_filename(path);
  2232.     return PyString_FromStringAndSize(buf, n);
  2233. }
  2234. #endif /* HAVE_READLINK */
  2235.  
  2236.  
  2237. #ifdef HAVE_SYMLINK
  2238. static char posix_symlink__doc__[] =
  2239. "symlink(src, dst) -> None\n\
  2240. Create a symbolic link.";
  2241.  
  2242. static PyObject *
  2243. posix_symlink(self, args)
  2244.     PyObject *self;
  2245.     PyObject *args;
  2246. {
  2247.     return posix_2str(args, "ss:symlink", symlink);
  2248. }
  2249. #endif /* HAVE_SYMLINK */
  2250.  
  2251.  
  2252. #ifdef HAVE_TIMES
  2253. #ifndef HZ
  2254. #define HZ 60 /* Universal constant :-) */
  2255. #endif /* HZ */
  2256.     
  2257. #if defined(PYCC_VACPP) && defined(PYOS_OS2)
  2258. static long
  2259. system_uptime()
  2260. {
  2261.     ULONG     value = 0;
  2262.  
  2263.     Py_BEGIN_ALLOW_THREADS
  2264.     DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
  2265.     Py_END_ALLOW_THREADS
  2266.  
  2267.     return value;
  2268. }
  2269.  
  2270. static PyObject *
  2271. posix_times(self, args)
  2272.     PyObject *self;
  2273.     PyObject *args;
  2274. {
  2275.     if (!PyArg_ParseTuple(args, ":times"))
  2276.         return NULL;
  2277.  
  2278.     /* Currently Only Uptime is Provided -- Others Later */
  2279.     return Py_BuildValue("ddddd",
  2280.                  (double)0 /* t.tms_utime / HZ */,
  2281.                  (double)0 /* t.tms_stime / HZ */,
  2282.                  (double)0 /* t.tms_cutime / HZ */,
  2283.                  (double)0 /* t.tms_cstime / HZ */,
  2284.                  (double)system_uptime() / 1000);
  2285. }
  2286. #else /* not OS2 */
  2287. static PyObject *
  2288. posix_times(self, args)
  2289.     PyObject *self;
  2290.     PyObject *args;
  2291. {
  2292.     struct tms t;
  2293.     clock_t c;
  2294.     if (!PyArg_ParseTuple(args, ":times"))
  2295.         return NULL;
  2296.     errno = 0;
  2297.     c = times(&t);
  2298.     if (c == (clock_t) -1)
  2299.         return posix_error();
  2300.     return Py_BuildValue("ddddd",
  2301.                  (double)t.tms_utime / HZ,
  2302.                  (double)t.tms_stime / HZ,
  2303.                  (double)t.tms_cutime / HZ,
  2304.                  (double)t.tms_cstime / HZ,
  2305.                  (double)c / HZ);
  2306. }
  2307. #endif /* not OS2 */
  2308. #endif /* HAVE_TIMES */
  2309.  
  2310.  
  2311. #ifdef MS_WIN32
  2312. #define HAVE_TIMES    /* so the method table will pick it up */
  2313. static PyObject *
  2314. posix_times(self, args)
  2315.     PyObject *self;
  2316.     PyObject *args;
  2317. {
  2318.     FILETIME create, exit, kernel, user;
  2319.     HANDLE hProc;
  2320.     if (!PyArg_ParseTuple(args, ":times"))
  2321.         return NULL;
  2322.     hProc = GetCurrentProcess();
  2323.     GetProcessTimes(hProc, &create, &exit, &kernel, &user);
  2324.     /* The fields of a FILETIME structure are the hi and lo part
  2325.        of a 64-bit value expressed in 100 nanosecond units.
  2326.        1e7 is one second in such units; 1e-7 the inverse.
  2327.        429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
  2328.     */
  2329.     return Py_BuildValue(
  2330.         "ddddd",
  2331.         (double)(kernel.dwHighDateTime*429.4967296 +
  2332.                  kernel.dwLowDateTime*1e-7),
  2333.         (double)(user.dwHighDateTime*429.4967296 +
  2334.                  user.dwLowDateTime*1e-7),
  2335.         (double)0,
  2336.         (double)0,
  2337.         (double)0);
  2338. }
  2339. #endif /* MS_WIN32 */
  2340.  
  2341. #ifdef HAVE_TIMES
  2342. static char posix_times__doc__[] =
  2343. "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
  2344. Return a tuple of floating point numbers indicating process times.";
  2345. #endif
  2346.  
  2347.  
  2348. #ifdef HAVE_SETSID
  2349. static char posix_setsid__doc__[] =
  2350. "setsid() -> None\n\
  2351. Call the system call setsid().";
  2352.  
  2353. static PyObject *
  2354. posix_setsid(self, args)
  2355.     PyObject *self;
  2356.     PyObject *args;
  2357. {
  2358.     if (!PyArg_ParseTuple(args, ":setsid"))
  2359.         return NULL;
  2360.     if (setsid() < 0)
  2361.         return posix_error();
  2362.     Py_INCREF(Py_None);
  2363.     return Py_None;
  2364. }
  2365. #endif /* HAVE_SETSID */
  2366.  
  2367. #ifdef HAVE_SETPGID
  2368. static char posix_setpgid__doc__[] =
  2369. "setpgid(pid, pgrp) -> None\n\
  2370. Call the system call setpgid().";
  2371.  
  2372. static PyObject *
  2373. posix_setpgid(self, args)
  2374.     PyObject *self;
  2375.     PyObject *args;
  2376. {
  2377.     int pid, pgrp;
  2378.     if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
  2379.         return NULL;
  2380.     if (setpgid(pid, pgrp) < 0)
  2381.         return posix_error();
  2382.     Py_INCREF(Py_None);
  2383.     return Py_None;
  2384. }
  2385. #endif /* HAVE_SETPGID */
  2386.  
  2387.  
  2388. #ifdef HAVE_TCGETPGRP
  2389. static char posix_tcgetpgrp__doc__[] =
  2390. "tcgetpgrp(fd) -> pgid\n\
  2391. Return the process group associated with the terminal given by a fd.";
  2392.  
  2393. static PyObject *
  2394. posix_tcgetpgrp(self, args)
  2395.     PyObject *self;
  2396.     PyObject *args;
  2397. {
  2398.     int fd, pgid;
  2399.     if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
  2400.         return NULL;
  2401.     pgid = tcgetpgrp(fd);
  2402.     if (pgid < 0)
  2403.         return posix_error();
  2404.     return PyInt_FromLong((long)pgid);
  2405. }
  2406. #endif /* HAVE_TCGETPGRP */
  2407.  
  2408.  
  2409. #ifdef HAVE_TCSETPGRP
  2410. static char posix_tcsetpgrp__doc__[] =
  2411. "tcsetpgrp(fd, pgid) -> None\n\
  2412. Set the process group associated with the terminal given by a fd.";
  2413.  
  2414. static PyObject *
  2415. posix_tcsetpgrp(self, args)
  2416.     PyObject *self;
  2417.     PyObject *args;
  2418. {
  2419.     int fd, pgid;
  2420.     if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
  2421.         return NULL;
  2422.     if (tcsetpgrp(fd, pgid) < 0)
  2423.         return posix_error();
  2424.     Py_INCREF(Py_None);
  2425.     return Py_None;
  2426. }
  2427. #endif /* HAVE_TCSETPGRP */
  2428.  
  2429. /* Functions acting on file descriptors */
  2430.  
  2431. static char posix_open__doc__[] =
  2432. "open(filename, flag [, mode=0777]) -> fd\n\
  2433. Open a file (for low level IO).";
  2434.  
  2435. static PyObject *
  2436. posix_open(self, args)
  2437.     PyObject *self;
  2438.     PyObject *args;
  2439. {
  2440.     char *file;
  2441.     int flag;
  2442.     int mode = 0777;
  2443.     int fd;
  2444.     if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
  2445.         return NULL;
  2446.  
  2447.     Py_BEGIN_ALLOW_THREADS
  2448.     fd = open(file, flag, mode);
  2449.     Py_END_ALLOW_THREADS
  2450.     if (fd < 0)
  2451.         return posix_error_with_filename(file);
  2452.     return PyInt_FromLong((long)fd);
  2453. }
  2454.  
  2455.  
  2456. static char posix_close__doc__[] =
  2457. "close(fd) -> None\n\
  2458. Close a file descriptor (for low level IO).";
  2459.  
  2460. static PyObject *
  2461. posix_close(self, args)
  2462.     PyObject *self;
  2463.     PyObject *args;
  2464. {
  2465.     int fd, res;
  2466.     if (!PyArg_ParseTuple(args, "i:close", &fd))
  2467.         return NULL;
  2468.     Py_BEGIN_ALLOW_THREADS
  2469.     res = close(fd);
  2470.     Py_END_ALLOW_THREADS
  2471.     if (res < 0)
  2472.         return posix_error();
  2473.     Py_INCREF(Py_None);
  2474.     return Py_None;
  2475. }
  2476.  
  2477.  
  2478. static char posix_dup__doc__[] =
  2479. "dup(fd) -> fd2\n\
  2480. Return a duplicate of a file descriptor.";
  2481.  
  2482. static PyObject *
  2483. posix_dup(self, args)
  2484.     PyObject *self;
  2485.     PyObject *args;
  2486. {
  2487.     int fd;
  2488.     if (!PyArg_ParseTuple(args, "i:dup", &fd))
  2489.         return NULL;
  2490.     Py_BEGIN_ALLOW_THREADS
  2491.     fd = dup(fd);
  2492.     Py_END_ALLOW_THREADS
  2493.     if (fd < 0)
  2494.         return posix_error();
  2495.     return PyInt_FromLong((long)fd);
  2496. }
  2497.  
  2498.  
  2499. static char posix_dup2__doc__[] =
  2500. "dup2(fd, fd2) -> None\n\
  2501. Duplicate file descriptor.";
  2502.  
  2503. static PyObject *
  2504. posix_dup2(self, args)
  2505.     PyObject *self;
  2506.     PyObject *args;
  2507. {
  2508.     int fd, fd2, res;
  2509.     if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
  2510.         return NULL;
  2511.     Py_BEGIN_ALLOW_THREADS
  2512.     res = dup2(fd, fd2);
  2513.     Py_END_ALLOW_THREADS
  2514.     if (res < 0)
  2515.         return posix_error();
  2516.     Py_INCREF(Py_None);
  2517.     return Py_None;
  2518. }
  2519.  
  2520.  
  2521. static char posix_lseek__doc__[] =
  2522. "lseek(fd, pos, how) -> newpos\n\
  2523. Set the current position of a file descriptor.";
  2524.  
  2525. static PyObject *
  2526. posix_lseek(self, args)
  2527.     PyObject *self;
  2528.     PyObject *args;
  2529. {
  2530.     int fd, how;
  2531.     off_t pos, res;
  2532.     PyObject *posobj;
  2533.     if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
  2534.         return NULL;
  2535. #ifdef SEEK_SET
  2536.     /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
  2537.     switch (how) {
  2538.     case 0: how = SEEK_SET; break;
  2539.     case 1: how = SEEK_CUR; break;
  2540.     case 2: how = SEEK_END; break;
  2541.     }
  2542. #endif /* SEEK_END */
  2543.  
  2544. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2545.     pos = PyInt_AsLong(posobj);
  2546. #else
  2547.     pos = PyLong_Check(posobj) ?
  2548.         PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
  2549. #endif
  2550.     if (PyErr_Occurred())
  2551.         return NULL;
  2552.  
  2553.     Py_BEGIN_ALLOW_THREADS
  2554.     res = lseek(fd, pos, how);
  2555.     Py_END_ALLOW_THREADS
  2556.     if (res < 0)
  2557.         return posix_error();
  2558.  
  2559. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2560.     return PyInt_FromLong(res);
  2561. #else
  2562.     return PyLong_FromLongLong(res);
  2563. #endif
  2564. }
  2565.  
  2566.  
  2567. static char posix_read__doc__[] =
  2568. "read(fd, buffersize) -> string\n\
  2569. Read a file descriptor.";
  2570.  
  2571. static PyObject *
  2572. posix_read(self, args)
  2573.     PyObject *self;
  2574.     PyObject *args;
  2575. {
  2576.     int fd, size, n;
  2577.     PyObject *buffer;
  2578.     if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
  2579.         return NULL;
  2580.     buffer = PyString_FromStringAndSize((char *)NULL, size);
  2581.     if (buffer == NULL)
  2582.         return NULL;
  2583.     Py_BEGIN_ALLOW_THREADS
  2584.     n = read(fd, PyString_AsString(buffer), size);
  2585.     Py_END_ALLOW_THREADS
  2586.     if (n < 0) {
  2587.         Py_DECREF(buffer);
  2588.         return posix_error();
  2589.     }
  2590.     if (n != size)
  2591.         _PyString_Resize(&buffer, n);
  2592.     return buffer;
  2593. }
  2594.  
  2595.  
  2596. static char posix_write__doc__[] =
  2597. "write(fd, string) -> byteswritten\n\
  2598. Write a string to a file descriptor.";
  2599.  
  2600. static PyObject *
  2601. posix_write(self, args)
  2602.     PyObject *self;
  2603.     PyObject *args;
  2604. {
  2605.     int fd, size;
  2606.     char *buffer;
  2607.     if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
  2608.         return NULL;
  2609.     Py_BEGIN_ALLOW_THREADS
  2610.     size = write(fd, buffer, size);
  2611.     Py_END_ALLOW_THREADS
  2612.     if (size < 0)
  2613.         return posix_error();
  2614.     return PyInt_FromLong((long)size);
  2615. }
  2616.  
  2617.  
  2618. static char posix_fstat__doc__[]=
  2619. "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
  2620. Like stat(), but for an open file descriptor.";
  2621.  
  2622. static PyObject *
  2623. posix_fstat(self, args)
  2624.     PyObject *self;
  2625.     PyObject *args;
  2626. {
  2627.     int fd;
  2628.     struct stat st;
  2629.     int res;
  2630.     if (!PyArg_ParseTuple(args, "i:fstat", &fd))
  2631.         return NULL;
  2632.     Py_BEGIN_ALLOW_THREADS
  2633.     res = fstat(fd, &st);
  2634.     Py_END_ALLOW_THREADS
  2635.     if (res != 0)
  2636.         return posix_error();
  2637. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2638.     return Py_BuildValue("(llllllllll)",
  2639.                  (long)st.st_mode,
  2640.                  (long)st.st_ino,
  2641.                  (long)st.st_dev,
  2642.                  (long)st.st_nlink,
  2643.                  (long)st.st_uid,
  2644.                  (long)st.st_gid,
  2645.                  (long)st.st_size,
  2646.                  (long)st.st_atime,
  2647.                  (long)st.st_mtime,
  2648.                  (long)st.st_ctime);
  2649. #else
  2650.     return Py_BuildValue("(lLllllLlll)",
  2651.                  (long)st.st_mode,
  2652.                  (LONG_LONG)st.st_ino,
  2653.                  (long)st.st_dev,
  2654.                  (long)st.st_nlink,
  2655.                  (long)st.st_uid,
  2656.                  (long)st.st_gid,
  2657.                  (LONG_LONG)st.st_size,
  2658.                  (long)st.st_atime,
  2659.                  (long)st.st_mtime,
  2660.                  (long)st.st_ctime);
  2661. #endif
  2662. }
  2663.  
  2664.  
  2665. static char posix_fdopen__doc__[] =
  2666. "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
  2667. Return an open file object connected to a file descriptor.";
  2668.  
  2669. static PyObject *
  2670. posix_fdopen(self, args)
  2671.     PyObject *self;
  2672.     PyObject *args;
  2673. {
  2674.     extern int fclose Py_PROTO((FILE *));
  2675.     int fd;
  2676.     char *mode = "r";
  2677.     int bufsize = -1;
  2678.     FILE *fp;
  2679.     PyObject *f;
  2680.     if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
  2681.         return NULL;
  2682.  
  2683.     Py_BEGIN_ALLOW_THREADS
  2684.     fp = fdopen(fd, mode);
  2685.     Py_END_ALLOW_THREADS
  2686.     if (fp == NULL)
  2687.         return posix_error();
  2688.     f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
  2689.     if (f != NULL)
  2690.         PyFile_SetBufSize(f, bufsize);
  2691.     return f;
  2692. }
  2693.  
  2694.  
  2695. #ifdef HAVE_PIPE
  2696. static char posix_pipe__doc__[] =
  2697. "pipe() -> (read_end, write_end)\n\
  2698. Create a pipe.";
  2699.  
  2700. static PyObject *
  2701. posix_pipe(self, args)
  2702.     PyObject *self;
  2703.     PyObject *args;
  2704. {
  2705. #if defined(PYOS_OS2)
  2706.     HFILE read, write;
  2707.     APIRET rc;
  2708.  
  2709.     if (!PyArg_ParseTuple(args, ":pipe"))
  2710.         return NULL;
  2711.  
  2712.     Py_BEGIN_ALLOW_THREADS
  2713.     rc = DosCreatePipe( &read, &write, 4096);
  2714.     Py_END_ALLOW_THREADS
  2715.     if (rc != NO_ERROR)
  2716.         return os2_error(rc);
  2717.  
  2718.     return Py_BuildValue("(ii)", read, write);
  2719. #else
  2720. #if !defined(MS_WIN32)
  2721.     int fds[2];
  2722.     int res;
  2723.     if (!PyArg_ParseTuple(args, ":pipe"))
  2724.         return NULL;
  2725.     Py_BEGIN_ALLOW_THREADS
  2726.     res = pipe(fds);
  2727.     Py_END_ALLOW_THREADS
  2728.     if (res != 0)
  2729.         return posix_error();
  2730.     return Py_BuildValue("(ii)", fds[0], fds[1]);
  2731. #else /* MS_WIN32 */
  2732.     HANDLE read, write;
  2733.     int read_fd, write_fd;
  2734.     BOOL ok;
  2735.     if (!PyArg_ParseTuple(args, ":pipe"))
  2736.         return NULL;
  2737.     Py_BEGIN_ALLOW_THREADS
  2738.     ok = CreatePipe(&read, &write, NULL, 0);
  2739.     Py_END_ALLOW_THREADS
  2740.     if (!ok)
  2741.         return posix_error();
  2742.     read_fd = _open_osfhandle((long)read, 0);
  2743.     write_fd = _open_osfhandle((long)write, 1);
  2744.     return Py_BuildValue("(ii)", read_fd, write_fd);
  2745. #endif /* MS_WIN32 */
  2746. #endif
  2747. }
  2748. #endif  /* HAVE_PIPE */
  2749.  
  2750.  
  2751. #ifdef HAVE_MKFIFO
  2752. static char posix_mkfifo__doc__[] =
  2753. "mkfifo(file, [, mode=0666]) -> None\n\
  2754. Create a FIFO (a POSIX named pipe).";
  2755.  
  2756. static PyObject *
  2757. posix_mkfifo(self, args)
  2758.     PyObject *self;
  2759.     PyObject *args;
  2760. {
  2761.     char *file;
  2762.     int mode = 0666;
  2763.     int res;
  2764.     if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
  2765.         return NULL;
  2766.     Py_BEGIN_ALLOW_THREADS
  2767.     res = mkfifo(file, mode);
  2768.     Py_END_ALLOW_THREADS
  2769.     if (res < 0)
  2770.         return posix_error();
  2771.     Py_INCREF(Py_None);
  2772.     return Py_None;
  2773. }
  2774. #endif
  2775.  
  2776.  
  2777. #ifdef HAVE_FTRUNCATE
  2778. static char posix_ftruncate__doc__[] =
  2779. "ftruncate(fd, length) -> None\n\
  2780. Truncate a file to a specified length.";
  2781.  
  2782. static PyObject *
  2783. posix_ftruncate(self, args)
  2784.     PyObject *self; /* Not used */
  2785.     PyObject *args;
  2786. {
  2787.     int fd;
  2788.     off_t length;
  2789.     int res;
  2790.     PyObject *lenobj;
  2791.  
  2792.     if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
  2793.         return NULL;
  2794.  
  2795. #if !defined(HAVE_LARGEFILE_SUPPORT)
  2796.     length = PyInt_AsLong(lenobj);
  2797. #else
  2798.     length = PyLong_Check(lenobj) ?
  2799.         PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
  2800. #endif
  2801.     if (PyErr_Occurred())
  2802.         return NULL;
  2803.  
  2804.     Py_BEGIN_ALLOW_THREADS
  2805.     res = ftruncate(fd, length);
  2806.     Py_END_ALLOW_THREADS
  2807.     if (res < 0) {
  2808.         PyErr_SetFromErrno(PyExc_IOError);
  2809.         return NULL;
  2810.     }
  2811.     Py_INCREF(Py_None);
  2812.     return Py_None;
  2813. }
  2814. #endif
  2815.  
  2816. #ifdef NeXT
  2817. #define HAVE_PUTENV
  2818. /* Steve Spicklemire got this putenv from NeXTAnswers */
  2819. static int
  2820. putenv(char *newval)
  2821. {
  2822.     extern char **environ;
  2823.  
  2824.     static int firstTime = 1;
  2825.     char **ep;
  2826.     char *cp;
  2827.     int esiz;
  2828.     char *np;
  2829.  
  2830.     if (!(np = strchr(newval, '=')))
  2831.         return 1;
  2832.     *np = '\0';
  2833.  
  2834.     /* look it up */
  2835.     for (ep=environ ; *ep ; ep++)
  2836.     {
  2837.         /* this should always be true... */
  2838.         if (cp = strchr(*ep, '='))
  2839.         {
  2840.             *cp = '\0';
  2841.             if (!strcmp(*ep, newval))
  2842.             {
  2843.                 /* got it! */
  2844.                 *cp = '=';
  2845.                 break;
  2846.             }
  2847.             *cp = '=';
  2848.         }
  2849.         else
  2850.         {
  2851.             *np = '=';
  2852.             return 1;
  2853.         }
  2854.     }
  2855.  
  2856.     *np = '=';
  2857.     if (*ep)
  2858.     {
  2859.         /* the string was already there:
  2860.            just replace it with the new one */
  2861.         *ep = newval;
  2862.         return 0;
  2863.     }
  2864.  
  2865.     /* expand environ by one */
  2866.     for (esiz=2, ep=environ ; *ep ; ep++)
  2867.         esiz++;
  2868.     if (firstTime)
  2869.     {
  2870.         char **epp;
  2871.         char **newenv;
  2872.         if (!(newenv = malloc(esiz * sizeof(char *))))
  2873.             return 1;
  2874.    
  2875.         for (ep=environ, epp=newenv ; *ep ;)
  2876.             *epp++ = *ep++;
  2877.         *epp++ = newval;
  2878.         *epp = (char *) 0;
  2879.         environ = newenv;
  2880.     }
  2881.     else
  2882.     {
  2883.         if (!(environ = realloc(environ, esiz * sizeof(char *))))
  2884.             return 1;
  2885.         environ[esiz - 2] = newval;
  2886.         environ[esiz - 1] = (char *) 0;
  2887.         firstTime = 0;
  2888.     }
  2889.  
  2890.     return 0;
  2891. }
  2892. #endif /* NeXT */
  2893.  
  2894.  
  2895. #ifdef HAVE_PUTENV
  2896. static char posix_putenv__doc__[] =
  2897. "putenv(key, value) -> None\n\
  2898. Change or add an environment variable.";
  2899.  
  2900. #ifdef __BEOS__
  2901. /* We have putenv(), but not in the headers (as of PR2). - [cjh] */
  2902. int putenv( const char *str );
  2903. #endif
  2904.  
  2905. /* Save putenv() parameters as values here, so we can collect them when they
  2906.  * get re-set with another call for the same key. */
  2907. static PyObject *posix_putenv_garbage;
  2908.  
  2909. static PyObject * 
  2910. posix_putenv(self, args)
  2911.     PyObject *self;
  2912.     PyObject *args;
  2913. {
  2914.         char *s1, *s2;
  2915.         char *new;
  2916.     PyObject *newstr;
  2917.  
  2918.     if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
  2919.         return NULL;
  2920.  
  2921. #if defined(PYOS_OS2)
  2922.     if (stricmp(s1, "BEGINLIBPATH") == 0) {
  2923.         APIRET rc;
  2924.  
  2925.         if (strlen(s2) == 0)  /* If New Value is an Empty String */
  2926.             s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */
  2927.  
  2928.         rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
  2929.         if (rc != NO_ERROR)
  2930.             return os2_error(rc);
  2931.  
  2932.     } else if (stricmp(s1, "ENDLIBPATH") == 0) {
  2933.         APIRET rc;
  2934.  
  2935.         if (strlen(s2) == 0)  /* If New Value is an Empty String */
  2936.             s2 = NULL;        /* Then OS/2 API Wants a NULL to Undefine It */
  2937.  
  2938.         rc = DosSetExtLIBPATH(s2, END_LIBPATH);
  2939.         if (rc != NO_ERROR)
  2940.             return os2_error(rc);
  2941.     } else {
  2942. #endif
  2943.  
  2944.     /* XXX This can leak memory -- not easy to fix :-( */
  2945.     newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
  2946.     if (newstr == NULL)
  2947.         return PyErr_NoMemory();
  2948.     new = PyString_AS_STRING(newstr);
  2949.     (void) sprintf(new, "%s=%s", s1, s2);
  2950.     if (putenv(new)) {
  2951.                 posix_error();
  2952.                 return NULL;
  2953.     }
  2954.     /* Install the first arg and newstr in posix_putenv_garbage;
  2955.      * this will cause previous value to be collected.  This has to
  2956.      * happen after the real putenv() call because the old value
  2957.      * was still accessible until then. */
  2958.     if (PyDict_SetItem(posix_putenv_garbage,
  2959.                PyTuple_GET_ITEM(args, 0), newstr)) {
  2960.         /* really not much we can do; just leak */
  2961.         PyErr_Clear();
  2962.     }
  2963.     else {
  2964.         Py_DECREF(newstr);
  2965.     }
  2966.  
  2967. #if defined(PYOS_OS2)
  2968.     }
  2969. #endif
  2970.     Py_INCREF(Py_None);
  2971.         return Py_None;
  2972. }
  2973. #endif /* putenv */
  2974.  
  2975. #ifdef HAVE_STRERROR
  2976. static char posix_strerror__doc__[] =
  2977. "strerror(code) -> string\n\
  2978. Translate an error code to a message string.";
  2979.  
  2980. PyObject *
  2981. posix_strerror(self, args)
  2982.     PyObject *self;
  2983.     PyObject *args;
  2984. {
  2985.     int code;
  2986.     char *message;
  2987.     if (!PyArg_ParseTuple(args, "i:strerror", &code))
  2988.         return NULL;
  2989.     message = strerror(code);
  2990.     if (message == NULL) {
  2991.         PyErr_SetString(PyExc_ValueError,
  2992.                 "strerror code out of range");
  2993.         return NULL;
  2994.     }
  2995.     return PyString_FromString(message);
  2996. }
  2997. #endif /* strerror */
  2998.  
  2999.  
  3000. #ifdef HAVE_SYS_WAIT_H
  3001.  
  3002. #ifdef WIFSTOPPED
  3003. static char posix_WIFSTOPPED__doc__[] =
  3004. "WIFSTOPPED(status) -> Boolean\n\
  3005. Return true if the process returning 'status' was stopped.";
  3006.  
  3007. static PyObject *
  3008. posix_WIFSTOPPED(self, args)
  3009.     PyObject *self;
  3010.     PyObject *args;
  3011. {
  3012. #ifdef UNION_WAIT
  3013.     union wait status;
  3014. #define status_i (status.w_status)
  3015. #else
  3016.     int status;
  3017. #define status_i status
  3018. #endif
  3019.     status_i = 0;
  3020.    
  3021.     if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
  3022.     {
  3023.         return NULL;
  3024.     }
  3025.    
  3026.     return Py_BuildValue("i", WIFSTOPPED(status));
  3027. #undef status_i
  3028. }
  3029. #endif /* WIFSTOPPED */
  3030.  
  3031. #ifdef WIFSIGNALED
  3032. static char posix_WIFSIGNALED__doc__[] =
  3033. "WIFSIGNALED(status) -> Boolean\n\
  3034. Return true if the process returning 'status' was terminated by a signal.";
  3035.  
  3036. static PyObject *
  3037. posix_WIFSIGNALED(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_ParseTuple(args, "i:WIFSIGNALED", &status_i))
  3051.     {
  3052.         return NULL;
  3053.     }
  3054.    
  3055.     return Py_BuildValue("i", WIFSIGNALED(status));
  3056. #undef status_i
  3057. }
  3058. #endif /* WIFSIGNALED */
  3059.  
  3060. #ifdef WIFEXITED
  3061. static char posix_WIFEXITED__doc__[] =
  3062. "WIFEXITED(status) -> Boolean\n\
  3063. Return true if the process returning 'status' exited using the exit()\n\
  3064. system call.";
  3065.  
  3066. static PyObject *
  3067. posix_WIFEXITED(self, args)
  3068.     PyObject *self;
  3069.     PyObject *args;
  3070. {
  3071. #ifdef UNION_WAIT
  3072.     union wait status;
  3073. #define status_i (status.w_status)
  3074. #else
  3075.     int status;
  3076. #define status_i status
  3077. #endif
  3078.     status_i = 0;
  3079.    
  3080.     if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
  3081.     {
  3082.         return NULL;
  3083.     }
  3084.    
  3085.     return Py_BuildValue("i", WIFEXITED(status));
  3086. #undef status_i
  3087. }
  3088. #endif /* WIFEXITED */
  3089.  
  3090. #ifdef WEXITSTATUS
  3091. static char posix_WEXITSTATUS__doc__[] =
  3092. "WEXITSTATUS(status) -> integer\n\
  3093. Return the process return code from 'status'.";
  3094.  
  3095. static PyObject *
  3096. posix_WEXITSTATUS(self, args)
  3097.     PyObject *self;
  3098.     PyObject *args;
  3099. {
  3100. #ifdef UNION_WAIT
  3101.     union wait status;
  3102. #define status_i (status.w_status)
  3103. #else
  3104.     int status;
  3105. #define status_i status
  3106. #endif
  3107.     status_i = 0;
  3108.    
  3109.     if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
  3110.     {
  3111.         return NULL;
  3112.     }
  3113.    
  3114.     return Py_BuildValue("i", WEXITSTATUS(status));
  3115. #undef status_i
  3116. }
  3117. #endif /* WEXITSTATUS */
  3118.  
  3119. #ifdef WTERMSIG
  3120. static char posix_WTERMSIG__doc__[] =
  3121. "WTERMSIG(status) -> integer\n\
  3122. Return the signal that terminated the process that provided the 'status'\n\
  3123. value.";
  3124.  
  3125. static PyObject *
  3126. posix_WTERMSIG(self, args)
  3127.     PyObject *self;
  3128.     PyObject *args;
  3129. {
  3130. #ifdef UNION_WAIT
  3131.     union wait status;
  3132. #define status_i (status.w_status)
  3133. #else
  3134.     int status;
  3135. #define status_i status
  3136. #endif
  3137.     status_i = 0;
  3138.    
  3139.     if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
  3140.     {
  3141.         return NULL;
  3142.     }
  3143.    
  3144.     return Py_BuildValue("i", WTERMSIG(status));
  3145. #undef status_i
  3146. }
  3147. #endif /* WTERMSIG */
  3148.  
  3149. #ifdef WSTOPSIG
  3150. static char posix_WSTOPSIG__doc__[] =
  3151. "WSTOPSIG(status) -> integer\n\
  3152. Return the signal that stopped the process that provided the 'status' value.";
  3153.  
  3154. static PyObject *
  3155. posix_WSTOPSIG(self, args)
  3156.     PyObject *self;
  3157.     PyObject *args;
  3158. {
  3159. #ifdef UNION_WAIT
  3160.     union wait status;
  3161. #define status_i (status.w_status)
  3162. #else
  3163.     int status;
  3164. #define status_i status
  3165. #endif
  3166.     status_i = 0;
  3167.    
  3168.     if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
  3169.     {
  3170.         return NULL;
  3171.     }
  3172.    
  3173.     return Py_BuildValue("i", WSTOPSIG(status));
  3174. #undef status_i
  3175. }
  3176. #endif /* WSTOPSIG */
  3177.  
  3178. #endif /* HAVE_SYS_WAIT_H */
  3179.  
  3180.  
  3181. #if defined(HAVE_FSTATVFS)
  3182. #ifdef _SCO_DS
  3183. /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
  3184.    needed definitions in sys/statvfs.h */
  3185. #define _SVID3
  3186. #endif
  3187. #include <sys/statvfs.h>
  3188.  
  3189. static char posix_fstatvfs__doc__[] =
  3190. "fstatvfs(fd) -> \n\
  3191.  (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
  3192. Perform an fstatvfs system call on the given fd.";
  3193.  
  3194. static PyObject *
  3195. posix_fstatvfs(self, args)
  3196.     PyObject *self;
  3197.     PyObject *args;
  3198. {
  3199.     int fd, res;
  3200.     struct statvfs st;
  3201.     if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
  3202.         return NULL;
  3203.     Py_BEGIN_ALLOW_THREADS
  3204.     res = fstatvfs(fd, &st);
  3205.     Py_END_ALLOW_THREADS
  3206.     if (res != 0)
  3207.         return posix_error();
  3208. #if !defined(HAVE_LARGEFILE_SUPPORT)
  3209.     return Py_BuildValue("(llllllllll)",
  3210.             (long) st.f_bsize,
  3211.             (long) st.f_frsize,
  3212.             (long) st.f_blocks,
  3213.             (long) st.f_bfree,
  3214.             (long) st.f_bavail,
  3215.             (long) st.f_files,
  3216.             (long) st.f_ffree,
  3217.             (long) st.f_favail,
  3218.             (long) st.f_flag,
  3219.             (long) st.f_namemax);
  3220. #else
  3221.     return Py_BuildValue("(llLLLLLLll)",
  3222.             (long) st.f_bsize,
  3223.             (long) st.f_frsize,
  3224.             (LONG_LONG) st.f_blocks,
  3225.             (LONG_LONG) st.f_bfree,
  3226.             (LONG_LONG) st.f_bavail,
  3227.             (LONG_LONG) st.f_files,
  3228.             (LONG_LONG) st.f_ffree,
  3229.             (LONG_LONG) st.f_favail,
  3230.             (long) st.f_flag,
  3231.             (long) st.f_namemax);
  3232. #endif
  3233. }
  3234. #endif /* HAVE_FSTATVFS */
  3235.  
  3236.  
  3237. #if defined(HAVE_STATVFS)
  3238. #include <sys/statvfs.h>
  3239.  
  3240. static char posix_statvfs__doc__[] =
  3241. "statvfs(path) -> \n\
  3242.  (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
  3243. Perform a statvfs system call on the given path.";
  3244.  
  3245. static PyObject *
  3246. posix_statvfs(self, args)
  3247.     PyObject *self;
  3248.     PyObject *args;
  3249. {
  3250.     char *path;
  3251.     int res;
  3252.     struct statvfs st;
  3253.     if (!PyArg_ParseTuple(args, "s:statvfs", &path))
  3254.         return NULL;
  3255.     Py_BEGIN_ALLOW_THREADS
  3256.     res = statvfs(path, &st);
  3257.     Py_END_ALLOW_THREADS
  3258.     if (res != 0)
  3259.         return posix_error_with_filename(path);
  3260. #if !defined(HAVE_LARGEFILE_SUPPORT)
  3261.     return Py_BuildValue("(llllllllll)",
  3262.             (long) st.f_bsize,
  3263.             (long) st.f_frsize,
  3264.             (long) st.f_blocks,
  3265.             (long) st.f_bfree,
  3266.             (long) st.f_bavail,
  3267.             (long) st.f_files,
  3268.             (long) st.f_ffree,
  3269.             (long) st.f_favail,
  3270.             (long) st.f_flag,
  3271.             (long) st.f_namemax);
  3272. #else    /* HAVE_LARGEFILE_SUPPORT */
  3273.     return Py_BuildValue("(llLLLLLLll)",
  3274.             (long) st.f_bsize,
  3275.             (long) st.f_frsize,
  3276.             (LONG_LONG) st.f_blocks,
  3277.             (LONG_LONG) st.f_bfree,
  3278.             (LONG_LONG) st.f_bavail,
  3279.             (LONG_LONG) st.f_files,
  3280.             (LONG_LONG) st.f_ffree,
  3281.             (LONG_LONG) st.f_favail,
  3282.             (long) st.f_flag,
  3283.             (long) st.f_namemax);
  3284. #endif
  3285. }
  3286. #endif /* HAVE_STATVFS */
  3287.  
  3288.  
  3289. #ifdef HAVE_TEMPNAM
  3290. static char posix_tempnam__doc__[] = "\
  3291. tempnam([dir[, prefix]]) -> string\n\
  3292. Return a unique name for a temporary file.\n\
  3293. The directory and a short may be specified as strings; they may be omitted\n\
  3294. or None if not needed.";
  3295.  
  3296. static PyObject *
  3297. posix_tempnam(self, args)
  3298.      PyObject *self;
  3299.      PyObject *args;
  3300. {
  3301.     PyObject *result = NULL;
  3302.     char *dir = NULL;
  3303.     char *pfx = NULL;
  3304.     char *name;
  3305.  
  3306.     if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
  3307.         return NULL;
  3308.     name = tempnam(dir, pfx);
  3309.     if (name == NULL)
  3310.         return PyErr_NoMemory();
  3311.     result = PyString_FromString(name);
  3312.     free(name);
  3313.     return result;
  3314. }
  3315. #endif
  3316.  
  3317.  
  3318. #ifdef HAVE_TMPFILE
  3319. static char posix_tmpfile__doc__[] = "\
  3320. tmpfile() -> file object\n\
  3321. Create a temporary file with no directory entries.";
  3322.  
  3323. static PyObject *
  3324. posix_tmpfile(self, args)
  3325.      PyObject *self;
  3326.      PyObject *args;
  3327. {
  3328.     FILE *fp;
  3329.  
  3330.     if (!PyArg_ParseTuple(args, ":tmpfile"))
  3331.         return NULL;
  3332.     fp = tmpfile();
  3333.     if (fp == NULL)
  3334.         return posix_error();
  3335.     return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
  3336. }
  3337. #endif
  3338.  
  3339.  
  3340. #ifdef HAVE_TMPNAM
  3341. static char posix_tmpnam__doc__[] = "\
  3342. tmpnam() -> string\n\
  3343. Return a unique name for a temporary file.";
  3344.  
  3345. static PyObject *
  3346. posix_tmpnam(self, args)
  3347.      PyObject *self;
  3348.      PyObject *args;
  3349. {
  3350.     char buffer[L_tmpnam];
  3351.     char *name;
  3352.  
  3353.     if (!PyArg_ParseTuple(args, ":tmpnam"))
  3354.         return NULL;
  3355. #ifdef USE_TMPNAM_R
  3356.     name = tmpnam_r(buffer);
  3357. #else
  3358.     name = tmpnam(buffer);
  3359. #endif
  3360.     if (name == NULL) {
  3361.         PyErr_SetObject(PyExc_OSError,
  3362.                         Py_BuildValue("is", 0,
  3363. #ifdef USE_TMPNAM_R
  3364.                                       "unexpected NULL from tmpnam_r"
  3365. #else
  3366.                                       "unexpected NULL from tmpnam"
  3367. #endif
  3368.                                       ));
  3369.         return NULL;
  3370.     }
  3371.     return PyString_FromString(buffer);
  3372. }
  3373. #endif
  3374.  
  3375.  
  3376. /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
  3377.  * It maps strings representing configuration variable names to
  3378.  * integer values, allowing those functions to be called with the
  3379.  * magic names instead of poluting the module's namespace with tons of
  3380.  * rarely-used constants.  There are three separate tables that use
  3381.  * these definitions.
  3382.  *
  3383.  * This code is always included, even if none of the interfaces that
  3384.  * need it are included.  The #if hackery needed to avoid it would be
  3385.  * sufficiently pervasive that it's not worth the loss of readability.
  3386.  */
  3387. struct constdef {
  3388.     char *name;
  3389.     long value;
  3390. };
  3391.  
  3392. static int
  3393. conv_confname(arg, valuep, table, tablesize)
  3394.      PyObject *arg;
  3395.      int *valuep;
  3396.      struct constdef *table;
  3397.      size_t tablesize;
  3398. {
  3399.     if (PyInt_Check(arg)) {
  3400.         *valuep = PyInt_AS_LONG(arg);
  3401.         return 1;
  3402.     }
  3403.     if (PyString_Check(arg)) {
  3404.         /* look up the value in the table using a binary search */
  3405.         int lo = 0;
  3406.         int hi = tablesize;
  3407.         int cmp, mid;
  3408.         char *confname = PyString_AS_STRING(arg);
  3409.         while (lo < hi) {
  3410.             mid = (lo + hi) / 2;
  3411.             cmp = strcmp(confname, table[mid].name);
  3412.             if (cmp < 0)
  3413.                 hi = mid;
  3414.             else if (cmp > 0)
  3415.                 lo = mid + 1;
  3416.             else {
  3417.                 *valuep = table[mid].value;
  3418.                 return 1;
  3419.             }
  3420.         }
  3421.         PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
  3422.     }
  3423.     else
  3424.         PyErr_SetString(PyExc_TypeError,
  3425.                         "configuration names must be strings or integers");
  3426.     return 0;
  3427. }
  3428.  
  3429.  
  3430. #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
  3431. static struct constdef  posix_constants_pathconf[] = {
  3432. #ifdef _PC_ABI_AIO_XFER_MAX
  3433.     {"PC_ABI_AIO_XFER_MAX",    _PC_ABI_AIO_XFER_MAX},
  3434. #endif
  3435. #ifdef _PC_ABI_ASYNC_IO
  3436.     {"PC_ABI_ASYNC_IO",    _PC_ABI_ASYNC_IO},
  3437. #endif
  3438. #ifdef _PC_ASYNC_IO
  3439.     {"PC_ASYNC_IO",    _PC_ASYNC_IO},
  3440. #endif
  3441. #ifdef _PC_CHOWN_RESTRICTED
  3442.     {"PC_CHOWN_RESTRICTED",    _PC_CHOWN_RESTRICTED},
  3443. #endif
  3444. #ifdef _PC_FILESIZEBITS
  3445.     {"PC_FILESIZEBITS",    _PC_FILESIZEBITS},
  3446. #endif
  3447. #ifdef _PC_LAST
  3448.     {"PC_LAST",    _PC_LAST},
  3449. #endif
  3450. #ifdef _PC_LINK_MAX
  3451.     {"PC_LINK_MAX",    _PC_LINK_MAX},
  3452. #endif
  3453. #ifdef _PC_MAX_CANON
  3454.     {"PC_MAX_CANON",    _PC_MAX_CANON},
  3455. #endif
  3456. #ifdef _PC_MAX_INPUT
  3457.     {"PC_MAX_INPUT",    _PC_MAX_INPUT},
  3458. #endif
  3459. #ifdef _PC_NAME_MAX
  3460.     {"PC_NAME_MAX",    _PC_NAME_MAX},
  3461. #endif
  3462. #ifdef _PC_NO_TRUNC
  3463.     {"PC_NO_TRUNC",    _PC_NO_TRUNC},
  3464. #endif
  3465. #ifdef _PC_PATH_MAX
  3466.     {"PC_PATH_MAX",    _PC_PATH_MAX},
  3467. #endif
  3468. #ifdef _PC_PIPE_BUF
  3469.     {"PC_PIPE_BUF",    _PC_PIPE_BUF},
  3470. #endif
  3471. #ifdef _PC_PRIO_IO
  3472.     {"PC_PRIO_IO",    _PC_PRIO_IO},
  3473. #endif
  3474. #ifdef _PC_SOCK_MAXBUF
  3475.     {"PC_SOCK_MAXBUF",    _PC_SOCK_MAXBUF},
  3476. #endif
  3477. #ifdef _PC_SYNC_IO
  3478.     {"PC_SYNC_IO",    _PC_SYNC_IO},
  3479. #endif
  3480. #ifdef _PC_VDISABLE
  3481.     {"PC_VDISABLE",    _PC_VDISABLE},
  3482. #endif
  3483. };
  3484.  
  3485. static int
  3486. conv_path_confname(arg, valuep)
  3487.      PyObject *arg;
  3488.      int *valuep;
  3489. {
  3490.     return conv_confname(arg, valuep, posix_constants_pathconf,
  3491.                          sizeof(posix_constants_pathconf)
  3492.                            / sizeof(struct constdef));
  3493. }
  3494. #endif
  3495.  
  3496. #ifdef HAVE_FPATHCONF
  3497. static char posix_fpathconf__doc__[] = "\
  3498. fpathconf(fd, name) -> integer\n\
  3499. Return the configuration limit name for the file descriptor fd.\n\
  3500. If there is no limit, return -1.";
  3501.  
  3502. static PyObject *
  3503. posix_fpathconf(self, args)
  3504.      PyObject *self;
  3505.      PyObject *args;
  3506. {
  3507.     PyObject *result = NULL;
  3508.     int name, fd;
  3509.  
  3510.     if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
  3511.                          conv_path_confname, &name)) {
  3512.         long limit;
  3513.  
  3514.         errno = 0;
  3515.         limit = fpathconf(fd, name);
  3516.         if (limit == -1 && errno != 0)
  3517.             posix_error();
  3518.         else
  3519.             result = PyInt_FromLong(limit);
  3520.     }
  3521.     return result;
  3522. }
  3523. #endif
  3524.  
  3525.  
  3526. #ifdef HAVE_PATHCONF
  3527. static char posix_pathconf__doc__[] = "\
  3528. pathconf(path, name) -> integer\n\
  3529. Return the configuration limit name for the file or directory path.\n\
  3530. If there is no limit, return -1.";
  3531.  
  3532. static PyObject *
  3533. posix_pathconf(self, args)
  3534.      PyObject *self;
  3535.      PyObject *args;
  3536. {
  3537.     PyObject *result = NULL;
  3538.     int name;
  3539.     char *path;
  3540.  
  3541.     if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
  3542.                          conv_path_confname, &name)) {
  3543.         long limit;
  3544.  
  3545.         errno = 0;
  3546.         limit = pathconf(path, name);
  3547.         if (limit == -1 && errno != 0) {
  3548.             if (errno == EINVAL)
  3549.                 /* could be a path or name problem */
  3550.                 posix_error();
  3551.             else
  3552.                 posix_error_with_filename(path);
  3553.         }
  3554.         else
  3555.             result = PyInt_FromLong(limit);
  3556.     }
  3557.     return result;
  3558. }
  3559. #endif
  3560.  
  3561. #ifdef HAVE_CONFSTR
  3562. static struct constdef posix_constants_confstr[] = {
  3563. #ifdef _CS_ARCHITECTURE
  3564.     {"CS_ARCHITECTURE",    _CS_ARCHITECTURE},
  3565. #endif
  3566. #ifdef _CS_HOSTNAME
  3567.     {"CS_HOSTNAME",    _CS_HOSTNAME},
  3568. #endif
  3569. #ifdef _CS_HW_PROVIDER
  3570.     {"CS_HW_PROVIDER",    _CS_HW_PROVIDER},
  3571. #endif
  3572. #ifdef _CS_HW_SERIAL
  3573.     {"CS_HW_SERIAL",    _CS_HW_SERIAL},
  3574. #endif
  3575. #ifdef _CS_INITTAB_NAME
  3576.     {"CS_INITTAB_NAME",    _CS_INITTAB_NAME},
  3577. #endif
  3578. #ifdef _CS_LFS64_CFLAGS
  3579.     {"CS_LFS64_CFLAGS",    _CS_LFS64_CFLAGS},
  3580. #endif
  3581. #ifdef _CS_LFS64_LDFLAGS
  3582.     {"CS_LFS64_LDFLAGS",    _CS_LFS64_LDFLAGS},
  3583. #endif
  3584. #ifdef _CS_LFS64_LIBS
  3585.     {"CS_LFS64_LIBS",    _CS_LFS64_LIBS},
  3586. #endif
  3587. #ifdef _CS_LFS64_LINTFLAGS
  3588.     {"CS_LFS64_LINTFLAGS",    _CS_LFS64_LINTFLAGS},
  3589. #endif
  3590. #ifdef _CS_LFS_CFLAGS
  3591.     {"CS_LFS_CFLAGS",    _CS_LFS_CFLAGS},
  3592. #endif
  3593. #ifdef _CS_LFS_LDFLAGS
  3594.     {"CS_LFS_LDFLAGS",    _CS_LFS_LDFLAGS},
  3595. #endif
  3596. #ifdef _CS_LFS_LIBS
  3597.     {"CS_LFS_LIBS",    _CS_LFS_LIBS},
  3598. #endif
  3599. #ifdef _CS_LFS_LINTFLAGS
  3600.     {"CS_LFS_LINTFLAGS",    _CS_LFS_LINTFLAGS},
  3601. #endif
  3602. #ifdef _CS_MACHINE
  3603.     {"CS_MACHINE",    _CS_MACHINE},
  3604. #endif
  3605. #ifdef _CS_PATH
  3606.     {"CS_PATH",    _CS_PATH},
  3607. #endif
  3608. #ifdef _CS_RELEASE
  3609.     {"CS_RELEASE",    _CS_RELEASE},
  3610. #endif
  3611. #ifdef _CS_SRPC_DOMAIN
  3612.     {"CS_SRPC_DOMAIN",    _CS_SRPC_DOMAIN},
  3613. #endif
  3614. #ifdef _CS_SYSNAME
  3615.     {"CS_SYSNAME",    _CS_SYSNAME},
  3616. #endif
  3617. #ifdef _CS_VERSION
  3618.     {"CS_VERSION",    _CS_VERSION},
  3619. #endif
  3620. #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
  3621.     {"CS_XBS5_ILP32_OFF32_CFLAGS",    _CS_XBS5_ILP32_OFF32_CFLAGS},
  3622. #endif
  3623. #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
  3624.     {"CS_XBS5_ILP32_OFF32_LDFLAGS",    _CS_XBS5_ILP32_OFF32_LDFLAGS},
  3625. #endif
  3626. #ifdef _CS_XBS5_ILP32_OFF32_LIBS
  3627.     {"CS_XBS5_ILP32_OFF32_LIBS",    _CS_XBS5_ILP32_OFF32_LIBS},
  3628. #endif
  3629. #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
  3630.     {"CS_XBS5_ILP32_OFF32_LINTFLAGS",    _CS_XBS5_ILP32_OFF32_LINTFLAGS},
  3631. #endif
  3632. #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
  3633.     {"CS_XBS5_ILP32_OFFBIG_CFLAGS",    _CS_XBS5_ILP32_OFFBIG_CFLAGS},
  3634. #endif
  3635. #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
  3636.     {"CS_XBS5_ILP32_OFFBIG_LDFLAGS",    _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
  3637. #endif
  3638. #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
  3639.     {"CS_XBS5_ILP32_OFFBIG_LIBS",    _CS_XBS5_ILP32_OFFBIG_LIBS},
  3640. #endif
  3641. #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
  3642.     {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS",    _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
  3643. #endif
  3644. #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
  3645.     {"CS_XBS5_LP64_OFF64_CFLAGS",    _CS_XBS5_LP64_OFF64_CFLAGS},
  3646. #endif
  3647. #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
  3648.     {"CS_XBS5_LP64_OFF64_LDFLAGS",    _CS_XBS5_LP64_OFF64_LDFLAGS},
  3649. #endif
  3650. #ifdef _CS_XBS5_LP64_OFF64_LIBS
  3651.     {"CS_XBS5_LP64_OFF64_LIBS",    _CS_XBS5_LP64_OFF64_LIBS},
  3652. #endif
  3653. #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
  3654.     {"CS_XBS5_LP64_OFF64_LINTFLAGS",    _CS_XBS5_LP64_OFF64_LINTFLAGS},
  3655. #endif
  3656. #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
  3657.     {"CS_XBS5_LPBIG_OFFBIG_CFLAGS",    _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
  3658. #endif
  3659. #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
  3660.     {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS",    _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
  3661. #endif
  3662. #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
  3663.     {"CS_XBS5_LPBIG_OFFBIG_LIBS",    _CS_XBS5_LPBIG_OFFBIG_LIBS},
  3664. #endif
  3665. #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
  3666.     {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS",    _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
  3667. #endif
  3668. #ifdef _MIPS_CS_AVAIL_PROCESSORS
  3669.     {"MIPS_CS_AVAIL_PROCESSORS",    _MIPS_CS_AVAIL_PROCESSORS},
  3670. #endif
  3671. #ifdef _MIPS_CS_BASE
  3672.     {"MIPS_CS_BASE",    _MIPS_CS_BASE},
  3673. #endif
  3674. #ifdef _MIPS_CS_HOSTID
  3675.     {"MIPS_CS_HOSTID",    _MIPS_CS_HOSTID},
  3676. #endif
  3677. #ifdef _MIPS_CS_HW_NAME
  3678.     {"MIPS_CS_HW_NAME",    _MIPS_CS_HW_NAME},
  3679. #endif
  3680. #ifdef _MIPS_CS_NUM_PROCESSORS
  3681.     {"MIPS_CS_NUM_PROCESSORS",    _MIPS_CS_NUM_PROCESSORS},
  3682. #endif
  3683. #ifdef _MIPS_CS_OSREL_MAJ
  3684.     {"MIPS_CS_OSREL_MAJ",    _MIPS_CS_OSREL_MAJ},
  3685. #endif
  3686. #ifdef _MIPS_CS_OSREL_MIN
  3687.     {"MIPS_CS_OSREL_MIN",    _MIPS_CS_OSREL_MIN},
  3688. #endif
  3689. #ifdef _MIPS_CS_OSREL_PATCH
  3690.     {"MIPS_CS_OSREL_PATCH",    _MIPS_CS_OSREL_PATCH},
  3691. #endif
  3692. #ifdef _MIPS_CS_OS_NAME
  3693.     {"MIPS_CS_OS_NAME",    _MIPS_CS_OS_NAME},
  3694. #endif
  3695. #ifdef _MIPS_CS_OS_PROVIDER
  3696.     {"MIPS_CS_OS_PROVIDER",    _MIPS_CS_OS_PROVIDER},
  3697. #endif
  3698. #ifdef _MIPS_CS_PROCESSORS
  3699.     {"MIPS_CS_PROCESSORS",    _MIPS_CS_PROCESSORS},
  3700. #endif
  3701. #ifdef _MIPS_CS_SERIAL
  3702.     {"MIPS_CS_SERIAL",    _MIPS_CS_SERIAL},
  3703. #endif
  3704. #ifdef _MIPS_CS_VENDOR
  3705.     {"MIPS_CS_VENDOR",    _MIPS_CS_VENDOR},
  3706. #endif
  3707. };
  3708.  
  3709. static int
  3710. conv_confstr_confname(arg, valuep)
  3711.      PyObject *arg;
  3712.      int *valuep;
  3713. {
  3714.     return conv_confname(arg, valuep, posix_constants_confstr,
  3715.                          sizeof(posix_constants_confstr)
  3716.                            / sizeof(struct constdef));
  3717. }
  3718.  
  3719. static char posix_confstr__doc__[] = "\
  3720. confstr(name) -> string\n\
  3721. Return a string-valued system configuration variable.";
  3722.  
  3723. static PyObject *
  3724. posix_confstr(self, args)
  3725.      PyObject *self;
  3726.      PyObject *args;
  3727. {
  3728.     PyObject *result = NULL;
  3729.     int name;
  3730.     char buffer[64];
  3731.  
  3732.     if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
  3733.         int len = confstr(name, buffer, sizeof(buffer));
  3734.  
  3735.         errno = 0;
  3736.         if (len == 0) {
  3737.             if (errno != 0)
  3738.                 posix_error();
  3739.             else
  3740.                 result = PyString_FromString("");
  3741.         }
  3742.         else {
  3743.             if (len >= sizeof(buffer)) {
  3744.                 result = PyString_FromStringAndSize(NULL, len);
  3745.                 if (result != NULL)
  3746.                     confstr(name, PyString_AS_STRING(result), len+1);
  3747.             }
  3748.             else
  3749.                 result = PyString_FromString(buffer);
  3750.         }
  3751.     }
  3752.     return result;
  3753. }
  3754. #endif
  3755.  
  3756.  
  3757. #ifdef HAVE_SYSCONF
  3758. static struct constdef posix_constants_sysconf[] = {
  3759. #ifdef _SC_2_CHAR_TERM
  3760.     {"SC_2_CHAR_TERM",    _SC_2_CHAR_TERM},
  3761. #endif
  3762. #ifdef _SC_2_C_BIND
  3763.     {"SC_2_C_BIND",    _SC_2_C_BIND},
  3764. #endif
  3765. #ifdef _SC_2_C_DEV
  3766.     {"SC_2_C_DEV",    _SC_2_C_DEV},
  3767. #endif
  3768. #ifdef _SC_2_C_VERSION
  3769.     {"SC_2_C_VERSION",    _SC_2_C_VERSION},
  3770. #endif
  3771. #ifdef _SC_2_FORT_DEV
  3772.     {"SC_2_FORT_DEV",    _SC_2_FORT_DEV},
  3773. #endif
  3774. #ifdef _SC_2_FORT_RUN
  3775.     {"SC_2_FORT_RUN",    _SC_2_FORT_RUN},
  3776. #endif
  3777. #ifdef _SC_2_LOCALEDEF
  3778.     {"SC_2_LOCALEDEF",    _SC_2_LOCALEDEF},
  3779. #endif
  3780. #ifdef _SC_2_SW_DEV
  3781.     {"SC_2_SW_DEV",    _SC_2_SW_DEV},
  3782. #endif
  3783. #ifdef _SC_2_UPE
  3784.     {"SC_2_UPE",    _SC_2_UPE},
  3785. #endif
  3786. #ifdef _SC_2_VERSION
  3787.     {"SC_2_VERSION",    _SC_2_VERSION},
  3788. #endif
  3789. #ifdef _SC_ABI_ASYNCHRONOUS_IO
  3790.     {"SC_ABI_ASYNCHRONOUS_IO",    _SC_ABI_ASYNCHRONOUS_IO},
  3791. #endif
  3792. #ifdef _SC_ACL
  3793.     {"SC_ACL",    _SC_ACL},
  3794. #endif
  3795. #ifdef _SC_AIO_LISTIO_MAX
  3796.     {"SC_AIO_LISTIO_MAX",    _SC_AIO_LISTIO_MAX},
  3797. #endif
  3798. #ifdef _SC_AIO_MAX
  3799.     {"SC_AIO_MAX",    _SC_AIO_MAX},
  3800. #endif
  3801. #ifdef _SC_AIO_PRIO_DELTA_MAX
  3802.     {"SC_AIO_PRIO_DELTA_MAX",    _SC_AIO_PRIO_DELTA_MAX},
  3803. #endif
  3804. #ifdef _SC_ARG_MAX
  3805.     {"SC_ARG_MAX",    _SC_ARG_MAX},
  3806. #endif
  3807. #ifdef _SC_ASYNCHRONOUS_IO
  3808.     {"SC_ASYNCHRONOUS_IO",    _SC_ASYNCHRONOUS_IO},
  3809. #endif
  3810. #ifdef _SC_ATEXIT_MAX
  3811.     {"SC_ATEXIT_MAX",    _SC_ATEXIT_MAX},
  3812. #endif
  3813. #ifdef _SC_AUDIT
  3814.     {"SC_AUDIT",    _SC_AUDIT},
  3815. #endif
  3816. #ifdef _SC_AVPHYS_PAGES
  3817.     {"SC_AVPHYS_PAGES",    _SC_AVPHYS_PAGES},
  3818. #endif
  3819. #ifdef _SC_BC_BASE_MAX
  3820.     {"SC_BC_BASE_MAX",    _SC_BC_BASE_MAX},
  3821. #endif
  3822. #ifdef _SC_BC_DIM_MAX
  3823.     {"SC_BC_DIM_MAX",    _SC_BC_DIM_MAX},
  3824. #endif
  3825. #ifdef _SC_BC_SCALE_MAX
  3826.     {"SC_BC_SCALE_MAX",    _SC_BC_SCALE_MAX},
  3827. #endif
  3828. #ifdef _SC_BC_STRING_MAX
  3829.     {"SC_BC_STRING_MAX",    _SC_BC_STRING_MAX},
  3830. #endif
  3831. #ifdef _SC_CAP
  3832.     {"SC_CAP",    _SC_CAP},
  3833. #endif
  3834. #ifdef _SC_CHARCLASS_NAME_MAX
  3835.     {"SC_CHARCLASS_NAME_MAX",    _SC_CHARCLASS_NAME_MAX},
  3836. #endif
  3837. #ifdef _SC_CHAR_BIT
  3838.     {"SC_CHAR_BIT",    _SC_CHAR_BIT},
  3839. #endif
  3840. #ifdef _SC_CHAR_MAX
  3841.     {"SC_CHAR_MAX",    _SC_CHAR_MAX},
  3842. #endif
  3843. #ifdef _SC_CHAR_MIN
  3844.     {"SC_CHAR_MIN",    _SC_CHAR_MIN},
  3845. #endif
  3846. #ifdef _SC_CHILD_MAX
  3847.     {"SC_CHILD_MAX",    _SC_CHILD_MAX},
  3848. #endif
  3849. #ifdef _SC_CLK_TCK
  3850.     {"SC_CLK_TCK",    _SC_CLK_TCK},
  3851. #endif
  3852. #ifdef _SC_COHER_BLKSZ
  3853.     {"SC_COHER_BLKSZ",    _SC_COHER_BLKSZ},
  3854. #endif
  3855. #ifdef _SC_COLL_WEIGHTS_MAX
  3856.     {"SC_COLL_WEIGHTS_MAX",    _SC_COLL_WEIGHTS_MAX},
  3857. #endif
  3858. #ifdef _SC_DCACHE_ASSOC
  3859.     {"SC_DCACHE_ASSOC",    _SC_DCACHE_ASSOC},
  3860. #endif
  3861. #ifdef _SC_DCACHE_BLKSZ
  3862.     {"SC_DCACHE_BLKSZ",    _SC_DCACHE_BLKSZ},
  3863. #endif
  3864. #ifdef _SC_DCACHE_LINESZ
  3865.     {"SC_DCACHE_LINESZ",    _SC_DCACHE_LINESZ},
  3866. #endif
  3867. #ifdef _SC_DCACHE_SZ
  3868.     {"SC_DCACHE_SZ",    _SC_DCACHE_SZ},
  3869. #endif
  3870. #ifdef _SC_DCACHE_TBLKSZ
  3871.     {"SC_DCACHE_TBLKSZ",    _SC_DCACHE_TBLKSZ},
  3872. #endif
  3873. #ifdef _SC_DELAYTIMER_MAX
  3874.     {"SC_DELAYTIMER_MAX",    _SC_DELAYTIMER_MAX},
  3875. #endif
  3876. #ifdef _SC_EQUIV_CLASS_MAX
  3877.     {"SC_EQUIV_CLASS_MAX",    _SC_EQUIV_CLASS_MAX},
  3878. #endif
  3879. #ifdef _SC_EXPR_NEST_MAX
  3880.     {"SC_EXPR_NEST_MAX",    _SC_EXPR_NEST_MAX},
  3881. #endif
  3882. #ifdef _SC_FSYNC
  3883.     {"SC_FSYNC",    _SC_FSYNC},
  3884. #endif
  3885. #ifdef _SC_GETGR_R_SIZE_MAX
  3886.     {"SC_GETGR_R_SIZE_MAX",    _SC_GETGR_R_SIZE_MAX},
  3887. #endif
  3888. #ifdef _SC_GETPW_R_SIZE_MAX
  3889.     {"SC_GETPW_R_SIZE_MAX",    _SC_GETPW_R_SIZE_MAX},
  3890. #endif
  3891. #ifdef _SC_ICACHE_ASSOC
  3892.     {"SC_ICACHE_ASSOC",    _SC_ICACHE_ASSOC},
  3893. #endif
  3894. #ifdef _SC_ICACHE_BLKSZ
  3895.     {"SC_ICACHE_BLKSZ",    _SC_ICACHE_BLKSZ},
  3896. #endif
  3897. #ifdef _SC_ICACHE_LINESZ
  3898.     {"SC_ICACHE_LINESZ",    _SC_ICACHE_LINESZ},
  3899. #endif
  3900. #ifdef _SC_ICACHE_SZ
  3901.     {"SC_ICACHE_SZ",    _SC_ICACHE_SZ},
  3902. #endif
  3903. #ifdef _SC_INF
  3904.     {"SC_INF",    _SC_INF},
  3905. #endif
  3906. #ifdef _SC_INT_MAX
  3907.     {"SC_INT_MAX",    _SC_INT_MAX},
  3908. #endif
  3909. #ifdef _SC_INT_MIN
  3910.     {"SC_INT_MIN",    _SC_INT_MIN},
  3911. #endif
  3912. #ifdef _SC_IOV_MAX
  3913.     {"SC_IOV_MAX",    _SC_IOV_MAX},
  3914. #endif
  3915. #ifdef _SC_IP_SECOPTS
  3916.     {"SC_IP_SECOPTS",    _SC_IP_SECOPTS},
  3917. #endif
  3918. #ifdef _SC_JOB_CONTROL
  3919.     {"SC_JOB_CONTROL",    _SC_JOB_CONTROL},
  3920. #endif
  3921. #ifdef _SC_KERN_POINTERS
  3922.     {"SC_KERN_POINTERS",    _SC_KERN_POINTERS},
  3923. #endif
  3924. #ifdef _SC_KERN_SIM
  3925.     {"SC_KERN_SIM",    _SC_KERN_SIM},
  3926. #endif
  3927. #ifdef _SC_LINE_MAX
  3928.     {"SC_LINE_MAX",    _SC_LINE_MAX},
  3929. #endif
  3930. #ifdef _SC_LOGIN_NAME_MAX
  3931.     {"SC_LOGIN_NAME_MAX",    _SC_LOGIN_NAME_MAX},
  3932. #endif
  3933. #ifdef _SC_LOGNAME_MAX
  3934.     {"SC_LOGNAME_MAX",    _SC_LOGNAME_MAX},
  3935. #endif
  3936. #ifdef _SC_LONG_BIT
  3937.     {"SC_LONG_BIT",    _SC_LONG_BIT},
  3938. #endif
  3939. #ifdef _SC_MAC
  3940.     {"SC_MAC",    _SC_MAC},
  3941. #endif
  3942. #ifdef _SC_MAPPED_FILES
  3943.     {"SC_MAPPED_FILES",    _SC_MAPPED_FILES},
  3944. #endif
  3945. #ifdef _SC_MAXPID
  3946.     {"SC_MAXPID",    _SC_MAXPID},
  3947. #endif
  3948. #ifdef _SC_MB_LEN_MAX
  3949.     {"SC_MB_LEN_MAX",    _SC_MB_LEN_MAX},
  3950. #endif
  3951. #ifdef _SC_MEMLOCK
  3952.     {"SC_MEMLOCK",    _SC_MEMLOCK},
  3953. #endif
  3954. #ifdef _SC_MEMLOCK_RANGE
  3955.     {"SC_MEMLOCK_RANGE",    _SC_MEMLOCK_RANGE},
  3956. #endif
  3957. #ifdef _SC_MEMORY_PROTECTION
  3958.     {"SC_MEMORY_PROTECTION",    _SC_MEMORY_PROTECTION},
  3959. #endif
  3960. #ifdef _SC_MESSAGE_PASSING
  3961.     {"SC_MESSAGE_PASSING",    _SC_MESSAGE_PASSING},
  3962. #endif
  3963. #ifdef _SC_MMAP_FIXED_ALIGNMENT
  3964.     {"SC_MMAP_FIXED_ALIGNMENT",    _SC_MMAP_FIXED_ALIGNMENT},
  3965. #endif
  3966. #ifdef _SC_MQ_OPEN_MAX
  3967.     {"SC_MQ_OPEN_MAX",    _SC_MQ_OPEN_MAX},
  3968. #endif
  3969. #ifdef _SC_MQ_PRIO_MAX
  3970.     {"SC_MQ_PRIO_MAX",    _SC_MQ_PRIO_MAX},
  3971. #endif
  3972. #ifdef _SC_NACLS_MAX
  3973.     {"SC_NACLS_MAX",    _SC_NACLS_MAX},
  3974. #endif
  3975. #ifdef _SC_NGROUPS_MAX
  3976.     {"SC_NGROUPS_MAX",    _SC_NGROUPS_MAX},
  3977. #endif
  3978. #ifdef _SC_NL_ARGMAX
  3979.     {"SC_NL_ARGMAX",    _SC_NL_ARGMAX},
  3980. #endif
  3981. #ifdef _SC_NL_LANGMAX
  3982.     {"SC_NL_LANGMAX",    _SC_NL_LANGMAX},
  3983. #endif
  3984. #ifdef _SC_NL_MSGMAX
  3985.     {"SC_NL_MSGMAX",    _SC_NL_MSGMAX},
  3986. #endif
  3987. #ifdef _SC_NL_NMAX
  3988.     {"SC_NL_NMAX",    _SC_NL_NMAX},
  3989. #endif
  3990. #ifdef _SC_NL_SETMAX
  3991.     {"SC_NL_SETMAX",    _SC_NL_SETMAX},
  3992. #endif
  3993. #ifdef _SC_NL_TEXTMAX
  3994.     {"SC_NL_TEXTMAX",    _SC_NL_TEXTMAX},
  3995. #endif
  3996. #ifdef _SC_NPROCESSORS_CONF
  3997.     {"SC_NPROCESSORS_CONF",    _SC_NPROCESSORS_CONF},
  3998. #endif
  3999. #ifdef _SC_NPROCESSORS_ONLN
  4000.     {"SC_NPROCESSORS_ONLN",    _SC_NPROCESSORS_ONLN},
  4001. #endif
  4002. #ifdef _SC_NPROC_CONF
  4003.     {"SC_NPROC_CONF",    _SC_NPROC_CONF},
  4004. #endif
  4005. #ifdef _SC_NPROC_ONLN
  4006.     {"SC_NPROC_ONLN",    _SC_NPROC_ONLN},
  4007. #endif
  4008. #ifdef _SC_NZERO
  4009.     {"SC_NZERO",    _SC_NZERO},
  4010. #endif
  4011. #ifdef _SC_OPEN_MAX
  4012.     {"SC_OPEN_MAX",    _SC_OPEN_MAX},
  4013. #endif
  4014. #ifdef _SC_PAGESIZE
  4015.     {"SC_PAGESIZE",    _SC_PAGESIZE},
  4016. #endif
  4017. #ifdef _SC_PAGE_SIZE
  4018.     {"SC_PAGE_SIZE",    _SC_PAGE_SIZE},
  4019. #endif
  4020. #ifdef _SC_PASS_MAX
  4021.     {"SC_PASS_MAX",    _SC_PASS_MAX},
  4022. #endif
  4023. #ifdef _SC_PHYS_PAGES
  4024.     {"SC_PHYS_PAGES",    _SC_PHYS_PAGES},
  4025. #endif
  4026. #ifdef _SC_PII
  4027.     {"SC_PII",    _SC_PII},
  4028. #endif
  4029. #ifdef _SC_PII_INTERNET
  4030.     {"SC_PII_INTERNET",    _SC_PII_INTERNET},
  4031. #endif
  4032. #ifdef _SC_PII_INTERNET_DGRAM
  4033.     {"SC_PII_INTERNET_DGRAM",    _SC_PII_INTERNET_DGRAM},
  4034. #endif
  4035. #ifdef _SC_PII_INTERNET_STREAM
  4036.     {"SC_PII_INTERNET_STREAM",    _SC_PII_INTERNET_STREAM},
  4037. #endif
  4038. #ifdef _SC_PII_OSI
  4039.     {"SC_PII_OSI",    _SC_PII_OSI},
  4040. #endif
  4041. #ifdef _SC_PII_OSI_CLTS
  4042.     {"SC_PII_OSI_CLTS",    _SC_PII_OSI_CLTS},
  4043. #endif
  4044. #ifdef _SC_PII_OSI_COTS
  4045.     {"SC_PII_OSI_COTS",    _SC_PII_OSI_COTS},
  4046. #endif
  4047. #ifdef _SC_PII_OSI_M
  4048.     {"SC_PII_OSI_M",    _SC_PII_OSI_M},
  4049. #endif
  4050. #ifdef _SC_PII_SOCKET
  4051.     {"SC_PII_SOCKET",    _SC_PII_SOCKET},
  4052. #endif
  4053. #ifdef _SC_PII_XTI
  4054.     {"SC_PII_XTI",    _SC_PII_XTI},
  4055. #endif
  4056. #ifdef _SC_POLL
  4057.     {"SC_POLL",    _SC_POLL},
  4058. #endif
  4059. #ifdef _SC_PRIORITIZED_IO
  4060.     {"SC_PRIORITIZED_IO",    _SC_PRIORITIZED_IO},
  4061. #endif
  4062. #ifdef _SC_PRIORITY_SCHEDULING
  4063.     {"SC_PRIORITY_SCHEDULING",    _SC_PRIORITY_SCHEDULING},
  4064. #endif
  4065. #ifdef _SC_REALTIME_SIGNALS
  4066.     {"SC_REALTIME_SIGNALS",    _SC_REALTIME_SIGNALS},
  4067. #endif
  4068. #ifdef _SC_RE_DUP_MAX
  4069.     {"SC_RE_DUP_MAX",    _SC_RE_DUP_MAX},
  4070. #endif
  4071. #ifdef _SC_RTSIG_MAX
  4072.     {"SC_RTSIG_MAX",    _SC_RTSIG_MAX},
  4073. #endif
  4074. #ifdef _SC_SAVED_IDS
  4075.     {"SC_SAVED_IDS",    _SC_SAVED_IDS},
  4076. #endif
  4077. #ifdef _SC_SCHAR_MAX
  4078.     {"SC_SCHAR_MAX",    _SC_SCHAR_MAX},
  4079. #endif
  4080. #ifdef _SC_SCHAR_MIN
  4081.     {"SC_SCHAR_MIN",    _SC_SCHAR_MIN},
  4082. #endif
  4083. #ifdef _SC_SELECT
  4084.     {"SC_SELECT",    _SC_SELECT},
  4085. #endif
  4086. #ifdef _SC_SEMAPHORES
  4087.     {"SC_SEMAPHORES",    _SC_SEMAPHORES},
  4088. #endif
  4089. #ifdef _SC_SEM_NSEMS_MAX
  4090.     {"SC_SEM_NSEMS_MAX",    _SC_SEM_NSEMS_MAX},
  4091. #endif
  4092. #ifdef _SC_SEM_VALUE_MAX
  4093.     {"SC_SEM_VALUE_MAX",    _SC_SEM_VALUE_MAX},
  4094. #endif
  4095. #ifdef _SC_SHARED_MEMORY_OBJECTS
  4096.     {"SC_SHARED_MEMORY_OBJECTS",    _SC_SHARED_MEMORY_OBJECTS},
  4097. #endif
  4098. #ifdef _SC_SHRT_MAX
  4099.     {"SC_SHRT_MAX",    _SC_SHRT_MAX},
  4100. #endif
  4101. #ifdef _SC_SHRT_MIN
  4102.     {"SC_SHRT_MIN",    _SC_SHRT_MIN},
  4103. #endif
  4104. #ifdef _SC_SIGQUEUE_MAX
  4105.     {"SC_SIGQUEUE_MAX",    _SC_SIGQUEUE_MAX},
  4106. #endif
  4107. #ifdef _SC_SIGRT_MAX
  4108.     {"SC_SIGRT_MAX",    _SC_SIGRT_MAX},
  4109. #endif
  4110. #ifdef _SC_SIGRT_MIN
  4111.     {"SC_SIGRT_MIN",    _SC_SIGRT_MIN},
  4112. #endif
  4113. #ifdef _SC_SOFTPOWER
  4114.     {"SC_SOFTPOWER",    _SC_SOFTPOWER},
  4115. #endif
  4116. #ifdef _SC_SPLIT_CACHE
  4117.     {"SC_SPLIT_CACHE",    _SC_SPLIT_CACHE},
  4118. #endif
  4119. #ifdef _SC_SSIZE_MAX
  4120.     {"SC_SSIZE_MAX",    _SC_SSIZE_MAX},
  4121. #endif
  4122. #ifdef _SC_STACK_PROT
  4123.     {"SC_STACK_PROT",    _SC_STACK_PROT},
  4124. #endif
  4125. #ifdef _SC_STREAM_MAX
  4126.     {"SC_STREAM_MAX",    _SC_STREAM_MAX},
  4127. #endif
  4128. #ifdef _SC_SYNCHRONIZED_IO
  4129.     {"SC_SYNCHRONIZED_IO",    _SC_SYNCHRONIZED_IO},
  4130. #endif
  4131. #ifdef _SC_THREADS
  4132.     {"SC_THREADS",    _SC_THREADS},
  4133. #endif
  4134. #ifdef _SC_THREAD_ATTR_STACKADDR
  4135.     {"SC_THREAD_ATTR_STACKADDR",    _SC_THREAD_ATTR_STACKADDR},
  4136. #endif
  4137. #ifdef _SC_THREAD_ATTR_STACKSIZE
  4138.     {"SC_THREAD_ATTR_STACKSIZE",    _SC_THREAD_ATTR_STACKSIZE},
  4139. #endif
  4140. #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
  4141.     {"SC_THREAD_DESTRUCTOR_ITERATIONS",    _SC_THREAD_DESTRUCTOR_ITERATIONS},
  4142. #endif
  4143. #ifdef _SC_THREAD_KEYS_MAX
  4144.     {"SC_THREAD_KEYS_MAX",    _SC_THREAD_KEYS_MAX},
  4145. #endif
  4146. #ifdef _SC_THREAD_PRIORITY_SCHEDULING
  4147.     {"SC_THREAD_PRIORITY_SCHEDULING",    _SC_THREAD_PRIORITY_SCHEDULING},
  4148. #endif
  4149. #ifdef _SC_THREAD_PRIO_INHERIT
  4150.     {"SC_THREAD_PRIO_INHERIT",    _SC_THREAD_PRIO_INHERIT},
  4151. #endif
  4152. #ifdef _SC_THREAD_PRIO_PROTECT
  4153.     {"SC_THREAD_PRIO_PROTECT",    _SC_THREAD_PRIO_PROTECT},
  4154. #endif
  4155. #ifdef _SC_THREAD_PROCESS_SHARED
  4156.     {"SC_THREAD_PROCESS_SHARED",    _SC_THREAD_PROCESS_SHARED},
  4157. #endif
  4158. #ifdef _SC_THREAD_SAFE_FUNCTIONS
  4159.     {"SC_THREAD_SAFE_FUNCTIONS",    _SC_THREAD_SAFE_FUNCTIONS},
  4160. #endif
  4161. #ifdef _SC_THREAD_STACK_MIN
  4162.     {"SC_THREAD_STACK_MIN",    _SC_THREAD_STACK_MIN},
  4163. #endif
  4164. #ifdef _SC_THREAD_THREADS_MAX
  4165.     {"SC_THREAD_THREADS_MAX",    _SC_THREAD_THREADS_MAX},
  4166. #endif
  4167. #ifdef _SC_TIMERS
  4168.     {"SC_TIMERS",    _SC_TIMERS},
  4169. #endif
  4170. #ifdef _SC_TIMER_MAX
  4171.     {"SC_TIMER_MAX",    _SC_TIMER_MAX},
  4172. #endif
  4173. #ifdef _SC_TTY_NAME_MAX
  4174.     {"SC_TTY_NAME_MAX",    _SC_TTY_NAME_MAX},
  4175. #endif
  4176. #ifdef _SC_TZNAME_MAX
  4177.     {"SC_TZNAME_MAX",    _SC_TZNAME_MAX},
  4178. #endif
  4179. #ifdef _SC_T_IOV_MAX
  4180.     {"SC_T_IOV_MAX",    _SC_T_IOV_MAX},
  4181. #endif
  4182. #ifdef _SC_UCHAR_MAX
  4183.     {"SC_UCHAR_MAX",    _SC_UCHAR_MAX},
  4184. #endif
  4185. #ifdef _SC_UINT_MAX
  4186.     {"SC_UINT_MAX",    _SC_UINT_MAX},
  4187. #endif
  4188. #ifdef _SC_UIO_MAXIOV
  4189.     {"SC_UIO_MAXIOV",    _SC_UIO_MAXIOV},
  4190. #endif
  4191. #ifdef _SC_ULONG_MAX
  4192.     {"SC_ULONG_MAX",    _SC_ULONG_MAX},
  4193. #endif
  4194. #ifdef _SC_USHRT_MAX
  4195.     {"SC_USHRT_MAX",    _SC_USHRT_MAX},
  4196. #endif
  4197. #ifdef _SC_VERSION
  4198.     {"SC_VERSION",    _SC_VERSION},
  4199. #endif
  4200. #ifdef _SC_WORD_BIT
  4201.     {"SC_WORD_BIT",    _SC_WORD_BIT},
  4202. #endif
  4203. #ifdef _SC_XBS5_ILP32_OFF32
  4204.     {"SC_XBS5_ILP32_OFF32",    _SC_XBS5_ILP32_OFF32},
  4205. #endif
  4206. #ifdef _SC_XBS5_ILP32_OFFBIG
  4207.     {"SC_XBS5_ILP32_OFFBIG",    _SC_XBS5_ILP32_OFFBIG},
  4208. #endif
  4209. #ifdef _SC_XBS5_LP64_OFF64
  4210.     {"SC_XBS5_LP64_OFF64",    _SC_XBS5_LP64_OFF64},
  4211. #endif
  4212. #ifdef _SC_XBS5_LPBIG_OFFBIG
  4213.     {"SC_XBS5_LPBIG_OFFBIG",    _SC_XBS5_LPBIG_OFFBIG},
  4214. #endif
  4215. #ifdef _SC_XOPEN_CRYPT
  4216.     {"SC_XOPEN_CRYPT",    _SC_XOPEN_CRYPT},
  4217. #endif
  4218. #ifdef _SC_XOPEN_ENH_I18N
  4219.     {"SC_XOPEN_ENH_I18N",    _SC_XOPEN_ENH_I18N},
  4220. #endif
  4221. #ifdef _SC_XOPEN_LEGACY
  4222.     {"SC_XOPEN_LEGACY",    _SC_XOPEN_LEGACY},
  4223. #endif
  4224. #ifdef _SC_XOPEN_REALTIME
  4225.     {"SC_XOPEN_REALTIME",    _SC_XOPEN_REALTIME},
  4226. #endif
  4227. #ifdef _SC_XOPEN_REALTIME_THREADS
  4228.     {"SC_XOPEN_REALTIME_THREADS",    _SC_XOPEN_REALTIME_THREADS},
  4229. #endif
  4230. #ifdef _SC_XOPEN_SHM
  4231.     {"SC_XOPEN_SHM",    _SC_XOPEN_SHM},
  4232. #endif
  4233. #ifdef _SC_XOPEN_UNIX
  4234.     {"SC_XOPEN_UNIX",    _SC_XOPEN_UNIX},
  4235. #endif
  4236. #ifdef _SC_XOPEN_VERSION
  4237.     {"SC_XOPEN_VERSION",    _SC_XOPEN_VERSION},
  4238. #endif
  4239. #ifdef _SC_XOPEN_XCU_VERSION
  4240.     {"SC_XOPEN_XCU_VERSION",    _SC_XOPEN_XCU_VERSION},
  4241. #endif
  4242. #ifdef _SC_XOPEN_XPG2
  4243.     {"SC_XOPEN_XPG2",    _SC_XOPEN_XPG2},
  4244. #endif
  4245. #ifdef _SC_XOPEN_XPG3
  4246.     {"SC_XOPEN_XPG3",    _SC_XOPEN_XPG3},
  4247. #endif
  4248. #ifdef _SC_XOPEN_XPG4
  4249.     {"SC_XOPEN_XPG4",    _SC_XOPEN_XPG4},
  4250. #endif
  4251. };
  4252.  
  4253. static int
  4254. conv_sysconf_confname(arg, valuep)
  4255.      PyObject *arg;
  4256.      int *valuep;
  4257. {
  4258.     return conv_confname(arg, valuep, posix_constants_sysconf,
  4259.                          sizeof(posix_constants_sysconf)
  4260.                            / sizeof(struct constdef));
  4261. }
  4262.  
  4263. static char posix_sysconf__doc__[] = "\
  4264. sysconf(name) -> integer\n\
  4265. Return an integer-valued system configuration variable.";
  4266.  
  4267. static PyObject *
  4268. posix_sysconf(self, args)
  4269.      PyObject *self;
  4270.      PyObject *args;
  4271. {
  4272.     PyObject *result = NULL;
  4273.     int name;
  4274.  
  4275.     if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
  4276.         int value;
  4277.  
  4278.         errno = 0;
  4279.         value = sysconf(name);
  4280.         if (value == -1 && errno != 0)
  4281.             posix_error();
  4282.         else
  4283.             result = PyInt_FromLong(value);
  4284.     }
  4285.     return result;
  4286. }
  4287. #endif
  4288.  
  4289.  
  4290. /* This code is used to ensure that the tables of configuration value names
  4291.  * are in sorted order as required by conv_confname(), and also to build the
  4292.  * the exported dictionaries that are used to publish information about the
  4293.  * names available on the host platform.
  4294.  *
  4295.  * Sorting the table at runtime ensures that the table is properly ordered
  4296.  * when used, even for platforms we're not able to test on.  It also makes
  4297.  * it easier to add additional entries to the tables.
  4298.  */
  4299.  
  4300. static int
  4301. cmp_constdefs(v1, v2)
  4302.      const void *v1;
  4303.      const void *v2;
  4304. {
  4305.     const struct constdef *c1 =
  4306.         (const struct constdef *) v1;
  4307.     const struct constdef *c2 =
  4308.         (const struct constdef *) v2;
  4309.  
  4310.     return strcmp(c1->name, c2->name);
  4311. }
  4312.  
  4313. static int
  4314. setup_confname_table(table, tablesize, tablename, moddict)
  4315.      struct constdef *table;
  4316.      size_t tablesize;
  4317.      char * tablename;
  4318.      PyObject *moddict;
  4319. {
  4320.     PyObject *d = NULL;
  4321.  
  4322.     qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
  4323.     d = PyDict_New();
  4324.     if (d != NULL) {
  4325.         PyObject *o;
  4326.         size_t i = 0;
  4327.  
  4328.         for (; i < tablesize; ++i) {
  4329.             o = PyInt_FromLong(table[i].value);
  4330.             if (o == NULL
  4331.                 || PyDict_SetItemString(d, table[i].name, o) == -1) {
  4332.                 Py_DECREF(d);
  4333.                 d = NULL;
  4334.                 return -1;
  4335.             }
  4336.         }
  4337.         if (PyDict_SetItemString(moddict, tablename, d) == -1)
  4338.             return -1;
  4339.         return 0;
  4340.     }
  4341.     return -1;
  4342. }
  4343.  
  4344. /* Return -1 on failure, 0 on success. */
  4345. static int
  4346. setup_confname_tables(moddict)
  4347.      PyObject *moddict;
  4348. {
  4349. #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
  4350.     if (setup_confname_table(posix_constants_pathconf,
  4351.                              sizeof(posix_constants_pathconf)
  4352.                                / sizeof(struct constdef),
  4353.                              "pathconf_names", moddict))
  4354.         return -1;
  4355. #endif
  4356. #ifdef HAVE_CONFSTR
  4357.     if (setup_confname_table(posix_constants_confstr,
  4358.                              sizeof(posix_constants_confstr)
  4359.                                / sizeof(struct constdef),
  4360.                              "confstr_names", moddict))
  4361.         return -1;
  4362. #endif
  4363. #ifdef HAVE_SYSCONF
  4364.     if (setup_confname_table(posix_constants_sysconf,
  4365.                              sizeof(posix_constants_sysconf)
  4366.                                / sizeof(struct constdef),
  4367.                              "sysconf_names", moddict))
  4368.         return -1;
  4369. #endif
  4370.     return 0;
  4371. }
  4372.  
  4373.  
  4374. static char posix_abort__doc__[] = "\
  4375. abort() -> does not return!\n\
  4376. Abort the interpreter immediately.  This 'dumps core' or otherwise fails\n\
  4377. in the hardest way possible on the hosting operating system.";
  4378.  
  4379. static PyObject *
  4380. posix_abort(self, args)
  4381.      PyObject *self;
  4382.      PyObject *args;
  4383. {
  4384.     if (!PyArg_ParseTuple(args, ":abort"))
  4385.         return NULL;
  4386.     abort();
  4387.     /*NOTREACHED*/
  4388.     Py_FatalError("abort() called from Python code didn't abort!");
  4389.     return NULL;
  4390. }
  4391.  
  4392.  
  4393. static PyMethodDef posix_methods[] = {
  4394.     {"access",    posix_access, METH_VARARGS, posix_access__doc__},
  4395. #ifdef HAVE_TTYNAME
  4396.     {"ttyname",    posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
  4397. #endif
  4398.     {"chdir",    posix_chdir, METH_VARARGS, posix_chdir__doc__},
  4399.     {"chmod",    posix_chmod, METH_VARARGS, posix_chmod__doc__},
  4400. #ifdef HAVE_CHOWN
  4401.     {"chown",    posix_chown, METH_VARARGS, posix_chown__doc__},
  4402. #endif /* HAVE_CHOWN */
  4403. #ifdef HAVE_CTERMID
  4404.     {"ctermid",    posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
  4405. #endif
  4406. #ifdef HAVE_GETCWD
  4407.     {"getcwd",    posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
  4408. #endif
  4409. #ifdef HAVE_LINK
  4410.     {"link",    posix_link, METH_VARARGS, posix_link__doc__},
  4411. #endif /* HAVE_LINK */
  4412.     {"listdir",    posix_listdir, METH_VARARGS, posix_listdir__doc__},
  4413.     {"lstat",    posix_lstat, METH_VARARGS, posix_lstat__doc__},
  4414.     {"mkdir",    posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
  4415. #ifdef HAVE_NICE
  4416.     {"nice",    posix_nice, METH_VARARGS, posix_nice__doc__},
  4417. #endif /* HAVE_NICE */
  4418. #ifdef HAVE_READLINK
  4419.     {"readlink",    posix_readlink, METH_VARARGS, posix_readlink__doc__},
  4420. #endif /* HAVE_READLINK */
  4421.     {"rename",    posix_rename, METH_VARARGS, posix_rename__doc__},
  4422.     {"rmdir",    posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
  4423.     {"stat",    posix_stat, METH_VARARGS, posix_stat__doc__},
  4424. #ifdef HAVE_SYMLINK
  4425.     {"symlink",    posix_symlink, METH_VARARGS, posix_symlink__doc__},
  4426. #endif /* HAVE_SYMLINK */
  4427. #ifdef HAVE_SYSTEM
  4428.     {"system",    posix_system, METH_VARARGS, posix_system__doc__},
  4429. #endif
  4430.     {"umask",    posix_umask, METH_VARARGS, posix_umask__doc__},
  4431. #ifdef HAVE_UNAME
  4432.     {"uname",    posix_uname, METH_VARARGS, posix_uname__doc__},
  4433. #endif /* HAVE_UNAME */
  4434.     {"unlink",    posix_unlink, METH_VARARGS, posix_unlink__doc__},
  4435.     {"remove",    posix_unlink, METH_VARARGS, posix_remove__doc__},
  4436.     {"utime",    posix_utime, METH_VARARGS, posix_utime__doc__},
  4437. #ifdef HAVE_TIMES
  4438.     {"times",    posix_times, METH_VARARGS, posix_times__doc__},
  4439. #endif /* HAVE_TIMES */
  4440.     {"_exit",    posix__exit, METH_VARARGS, posix__exit__doc__},
  4441. #ifdef HAVE_EXECV
  4442.     {"execv",    posix_execv, METH_VARARGS, posix_execv__doc__},
  4443.     {"execve",    posix_execve, METH_VARARGS, posix_execve__doc__},
  4444. #endif /* HAVE_EXECV */
  4445. #ifdef HAVE_SPAWNV
  4446.     {"spawnv",    posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
  4447.     {"spawnve",    posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
  4448. #endif /* HAVE_SPAWNV */
  4449. #ifdef HAVE_FORK
  4450.     {"fork",    posix_fork, METH_VARARGS, posix_fork__doc__},
  4451. #endif /* HAVE_FORK */
  4452. #ifdef HAVE_GETEGID
  4453.     {"getegid",    posix_getegid, METH_VARARGS, posix_getegid__doc__},
  4454. #endif /* HAVE_GETEGID */
  4455. #ifdef HAVE_GETEUID
  4456.     {"geteuid",    posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
  4457. #endif /* HAVE_GETEUID */
  4458. #ifdef HAVE_GETGID
  4459.     {"getgid",    posix_getgid, METH_VARARGS, posix_getgid__doc__},
  4460. #endif /* HAVE_GETGID */
  4461. #ifdef HAVE_GETGROUPS
  4462.     {"getgroups",    posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
  4463. #endif
  4464.     {"getpid",    posix_getpid, METH_VARARGS, posix_getpid__doc__},
  4465. #ifdef HAVE_GETPGRP
  4466.     {"getpgrp",    posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
  4467. #endif /* HAVE_GETPGRP */
  4468. #ifdef HAVE_GETPPID
  4469.     {"getppid",    posix_getppid, METH_VARARGS, posix_getppid__doc__},
  4470. #endif /* HAVE_GETPPID */
  4471. #ifdef HAVE_GETUID
  4472.     {"getuid",    posix_getuid, METH_VARARGS, posix_getuid__doc__},
  4473. #endif /* HAVE_GETUID */
  4474. #ifdef HAVE_GETLOGIN
  4475.     {"getlogin",    posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
  4476. #endif
  4477. #ifdef HAVE_KILL
  4478.     {"kill",    posix_kill, METH_VARARGS, posix_kill__doc__},
  4479. #endif /* HAVE_KILL */
  4480. #ifdef HAVE_PLOCK
  4481.     {"plock",    posix_plock, METH_VARARGS, posix_plock__doc__},
  4482. #endif /* HAVE_PLOCK */
  4483. #ifdef HAVE_POPEN
  4484.     {"popen",    posix_popen, METH_VARARGS, posix_popen__doc__},
  4485. #endif /* HAVE_POPEN */
  4486. #ifdef HAVE_SETUID
  4487.     {"setuid",    posix_setuid, METH_VARARGS, posix_setuid__doc__},
  4488. #endif /* HAVE_SETUID */
  4489. #ifdef HAVE_SETGID
  4490.     {"setgid",    posix_setgid, METH_VARARGS, posix_setgid__doc__},
  4491. #endif /* HAVE_SETGID */
  4492. #ifdef HAVE_SETPGRP
  4493.     {"setpgrp",    posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
  4494. #endif /* HAVE_SETPGRP */
  4495. #ifdef HAVE_WAIT
  4496.     {"wait",    posix_wait, METH_VARARGS, posix_wait__doc__},
  4497. #endif /* HAVE_WAIT */
  4498. #ifdef HAVE_WAITPID
  4499.     {"waitpid",    posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
  4500. #endif /* HAVE_WAITPID */
  4501. #ifdef HAVE_SETSID
  4502.     {"setsid",    posix_setsid, METH_VARARGS, posix_setsid__doc__},
  4503. #endif /* HAVE_SETSID */
  4504. #ifdef HAVE_SETPGID
  4505.     {"setpgid",    posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
  4506. #endif /* HAVE_SETPGID */
  4507. #ifdef HAVE_TCGETPGRP
  4508.     {"tcgetpgrp",    posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
  4509. #endif /* HAVE_TCGETPGRP */
  4510. #ifdef HAVE_TCSETPGRP
  4511.     {"tcsetpgrp",    posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
  4512. #endif /* HAVE_TCSETPGRP */
  4513.     {"open",    posix_open, METH_VARARGS, posix_open__doc__},
  4514.     {"close",    posix_close, METH_VARARGS, posix_close__doc__},
  4515.     {"dup",        posix_dup, METH_VARARGS, posix_dup__doc__},
  4516.     {"dup2",    posix_dup2, METH_VARARGS, posix_dup2__doc__},
  4517.     {"lseek",    posix_lseek, METH_VARARGS, posix_lseek__doc__},
  4518.     {"read",    posix_read, METH_VARARGS, posix_read__doc__},
  4519.     {"write",    posix_write, METH_VARARGS, posix_write__doc__},
  4520.     {"fstat",    posix_fstat, METH_VARARGS, posix_fstat__doc__},
  4521.     {"fdopen",    posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
  4522. #ifdef HAVE_PIPE
  4523.     {"pipe",    posix_pipe, METH_VARARGS, posix_pipe__doc__},
  4524. #endif
  4525. #ifdef HAVE_MKFIFO
  4526.     {"mkfifo",    posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
  4527. #endif
  4528. #ifdef HAVE_FTRUNCATE
  4529.     {"ftruncate",    posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
  4530. #endif
  4531. #ifdef HAVE_PUTENV
  4532.     {"putenv",    posix_putenv, METH_VARARGS, posix_putenv__doc__},
  4533. #endif
  4534. #ifdef HAVE_STRERROR
  4535.     {"strerror",    posix_strerror, METH_VARARGS, posix_strerror__doc__},
  4536. #endif
  4537. #ifdef HAVE_FSYNC
  4538.     {"fsync",       posix_fsync, METH_VARARGS, posix_fsync__doc__},
  4539. #endif
  4540. #ifdef HAVE_FDATASYNC
  4541.     {"fdatasync",   posix_fdatasync,  METH_VARARGS, posix_fdatasync__doc__},
  4542. #endif
  4543. #ifdef HAVE_SYS_WAIT_H
  4544. #ifdef WIFSTOPPED
  4545.         {"WIFSTOPPED",    posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
  4546. #endif /* WIFSTOPPED */
  4547. #ifdef WIFSIGNALED
  4548.         {"WIFSIGNALED",    posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
  4549. #endif /* WIFSIGNALED */
  4550. #ifdef WIFEXITED
  4551.         {"WIFEXITED",    posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
  4552. #endif /* WIFEXITED */
  4553. #ifdef WEXITSTATUS
  4554.         {"WEXITSTATUS",    posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
  4555. #endif /* WEXITSTATUS */
  4556. #ifdef WTERMSIG
  4557.         {"WTERMSIG",    posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
  4558. #endif /* WTERMSIG */
  4559. #ifdef WSTOPSIG
  4560.         {"WSTOPSIG",    posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
  4561. #endif /* WSTOPSIG */
  4562. #endif /* HAVE_SYS_WAIT_H */
  4563. #ifdef HAVE_FSTATVFS
  4564.     {"fstatvfs",    posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
  4565. #endif
  4566. #ifdef HAVE_STATVFS
  4567.     {"statvfs",    posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
  4568. #endif
  4569. #ifdef HAVE_TMPNAM
  4570.     {"tmpfile",    posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
  4571. #endif
  4572. #ifdef HAVE_TEMPNAM
  4573.     {"tempnam",    posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
  4574. #endif
  4575. #ifdef HAVE_TMPNAM
  4576.     {"tmpnam",    posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
  4577. #endif
  4578. #ifdef HAVE_CONFSTR
  4579.     {"confstr",    posix_confstr, METH_VARARGS, posix_confstr__doc__},
  4580. #endif
  4581. #ifdef HAVE_SYSCONF
  4582.     {"sysconf",    posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
  4583. #endif
  4584. #ifdef HAVE_FPATHCONF
  4585.     {"fpathconf",    posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
  4586. #endif
  4587. #ifdef HAVE_PATHCONF
  4588.     {"pathconf",    posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
  4589. #endif
  4590.     {"abort",    posix_abort, METH_VARARGS, posix_abort__doc__},
  4591.     {NULL,        NULL}         /* Sentinel */
  4592. };
  4593.  
  4594.  
  4595. static int
  4596. ins(d, symbol, value)
  4597.         PyObject* d;
  4598.         char* symbol;
  4599.         long value;
  4600. {
  4601.         PyObject* v = PyInt_FromLong(value);
  4602.         if (!v || PyDict_SetItemString(d, symbol, v) < 0)
  4603.                 return -1;                   /* triggers fatal error */
  4604.  
  4605.         Py_DECREF(v);
  4606.         return 0;
  4607. }
  4608.  
  4609. #if defined(PYOS_OS2)
  4610. /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
  4611. static int insertvalues(PyObject *d)
  4612. {
  4613.     APIRET    rc;
  4614.     ULONG     values[QSV_MAX+1];
  4615.     PyObject *v;
  4616.     char     *ver, tmp[10];
  4617.  
  4618.     Py_BEGIN_ALLOW_THREADS
  4619.     rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
  4620.     Py_END_ALLOW_THREADS
  4621.  
  4622.     if (rc != NO_ERROR) {
  4623.         os2_error(rc);
  4624.         return -1;
  4625.     }
  4626.  
  4627.     if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
  4628.     if (ins(d, "memkernel",    values[QSV_TOTRESMEM])) return -1;
  4629.     if (ins(d, "memvirtual",   values[QSV_TOTAVAILMEM])) return -1;
  4630.     if (ins(d, "maxpathlen",   values[QSV_MAX_PATH_LENGTH])) return -1;
  4631.     if (ins(d, "maxnamelen",   values[QSV_MAX_COMP_LENGTH])) return -1;
  4632.     if (ins(d, "revision",     values[QSV_VERSION_REVISION])) return -1;
  4633.     if (ins(d, "timeslice",    values[QSV_MIN_SLICE])) return -1;
  4634.  
  4635.     switch (values[QSV_VERSION_MINOR]) {
  4636.     case 0:  ver = "2.00"; break;
  4637.     case 10: ver = "2.10"; break;
  4638.     case 11: ver = "2.11"; break;
  4639.     case 30: ver = "3.00"; break;
  4640.     case 40: ver = "4.00"; break;
  4641.     case 50: ver = "5.00"; break;
  4642.     default:
  4643.         sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
  4644.                               values[QSV_VERSION_MINOR]);
  4645.         ver = &tmp[0];
  4646.     }
  4647.  
  4648.     /* Add Indicator of the Version of the Operating System */
  4649.     v = PyString_FromString(ver);
  4650.     if (!v || PyDict_SetItemString(d, "version", v) < 0)
  4651.         return -1;
  4652.     Py_DECREF(v);
  4653.  
  4654.     /* Add Indicator of Which Drive was Used to Boot the System */
  4655.     tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
  4656.     tmp[1] = ':';
  4657.     tmp[2] = '\0';
  4658.  
  4659.     v = PyString_FromString(tmp);
  4660.     if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
  4661.         return -1;
  4662.     Py_DECREF(v);
  4663.  
  4664.     return 0;
  4665. }
  4666. #endif
  4667.  
  4668. static int
  4669. all_ins(d)
  4670.         PyObject* d;
  4671. {
  4672. #ifdef F_OK
  4673.         if (ins(d, "F_OK", (long)F_OK)) return -1;
  4674. #endif        
  4675. #ifdef R_OK
  4676.         if (ins(d, "R_OK", (long)R_OK)) return -1;
  4677. #endif        
  4678. #ifdef W_OK
  4679.         if (ins(d, "W_OK", (long)W_OK)) return -1;
  4680. #endif        
  4681. #ifdef X_OK
  4682.         if (ins(d, "X_OK", (long)X_OK)) return -1;
  4683. #endif        
  4684. #ifdef NGROUPS_MAX
  4685.         if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
  4686. #endif
  4687. #ifdef TMP_MAX
  4688.         if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
  4689. #endif
  4690. #ifdef WNOHANG
  4691.         if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
  4692. #endif        
  4693. #ifdef O_RDONLY
  4694.         if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
  4695. #endif
  4696. #ifdef O_WRONLY
  4697.         if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
  4698. #endif
  4699. #ifdef O_RDWR
  4700.         if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
  4701. #endif
  4702. #ifdef O_NDELAY
  4703.         if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
  4704. #endif
  4705. #ifdef O_NONBLOCK
  4706.         if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
  4707. #endif
  4708. #ifdef O_APPEND
  4709.         if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
  4710. #endif
  4711. #ifdef O_DSYNC
  4712.         if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
  4713. #endif
  4714. #ifdef O_RSYNC
  4715.         if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
  4716. #endif
  4717. #ifdef O_SYNC
  4718.         if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
  4719. #endif
  4720. #ifdef O_NOCTTY
  4721.         if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
  4722. #endif
  4723. #ifdef O_CREAT
  4724.         if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
  4725. #endif
  4726. #ifdef O_EXCL
  4727.         if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
  4728. #endif
  4729. #ifdef O_TRUNC
  4730.         if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
  4731. #endif
  4732. #ifdef O_BINARY
  4733.         if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
  4734. #endif
  4735. #ifdef O_TEXT
  4736.         if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
  4737. #endif
  4738.  
  4739. #ifdef HAVE_SPAWNV
  4740.         if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
  4741.         if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
  4742.         if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
  4743.         if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
  4744.         if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
  4745. #endif
  4746.  
  4747. #if defined(PYOS_OS2)
  4748.         if (insertvalues(d)) return -1;
  4749. #endif
  4750.         return 0;
  4751. }
  4752.  
  4753.  
  4754. #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
  4755. #define INITFUNC initnt
  4756. #define MODNAME "nt"
  4757. #else
  4758. #if defined(PYOS_OS2)
  4759. #define INITFUNC initos2
  4760. #define MODNAME "os2"
  4761. #else
  4762. #define INITFUNC initposix
  4763. #define MODNAME "posix"
  4764. #endif
  4765. #endif
  4766.  
  4767. DL_EXPORT(void)
  4768. INITFUNC()
  4769. {
  4770.     PyObject *m, *d, *v;
  4771.     
  4772.     m = Py_InitModule4(MODNAME,
  4773.                posix_methods,
  4774.                posix__doc__,
  4775.                (PyObject *)NULL,
  4776.                PYTHON_API_VERSION);
  4777.     d = PyModule_GetDict(m);
  4778.     
  4779.     /* Initialize environ dictionary */
  4780.     v = convertenviron();
  4781.     if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
  4782.         return;
  4783.     Py_DECREF(v);
  4784.  
  4785.         if (all_ins(d))
  4786.                 return;
  4787.  
  4788.         if (setup_confname_tables(d))
  4789.                 return;
  4790.  
  4791.     PyDict_SetItemString(d, "error", PyExc_OSError);
  4792.  
  4793. #ifdef HAVE_PUTENV
  4794.     posix_putenv_garbage = PyDict_New();
  4795. #endif
  4796. }
  4797.