disguised Unix interface). Refer to the library manual and\n\
corresponding Unix manual entries for more information on calls.";
-
-
#include "Python.h"
+#if defined(PYOS_OS2)
+#define INCL_DOS
+#define INCL_DOSERRORS
+#define INCL_DOSPROCESS
+#define INCL_NOPMAPI
+#include <os2.h>
+#endif
+
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_WAIT_H
/* Various compilers have only certain posix functions */
/* XXX Gosh I wish these were all moved into config.h */
+#if defined(PYCC_VACPP) && defined(PYOS_OS2)
+ #define HAVE_EXECV 1
+ #define HAVE_GETCWD 1
+ #define HAVE_SYSTEM 1
+ #define HAVE_WAIT 1
+ #define HAVE_KILL 1
+ #define HAVE_PIPE 1
+ #define HAVE_POPEN 1
+
+// #define HAVE_FORK 1
+// #define HAVE_GETEGID 1
+// #define HAVE_GETEUID 1
+// #define HAVE_GETGID 1
+// #define HAVE_GETPPID 1
+// #define HAVE_GETUID 1
+// #define HAVE_OPENDIR 1
+#include <process.h>
+#else
#ifdef __WATCOMC__ /* Watcom compiler */
#define HAVE_GETCWD 1
#define HAVE_OPENDIR 1
#endif /* _MSC_VER */
#endif /* __BORLANDC__ */
#endif /* ! __WATCOMC__ */
+#endif /* ! __IBMC__ */
#ifndef _MSC_VER
extern int lstat();
extern int symlink();
#else /* !HAVE_UNISTD_H */
+#if defined(PYCC_VACPP)
+extern int mkdir Py_PROTO((char *));
+#else
#if defined(__WATCOMC__) || defined(_MSC_VER)
extern int mkdir Py_PROTO((const char *));
#else
extern int mkdir Py_PROTO((const char *, mode_t));
#endif
+#endif
+#if defined(__IBMC__) || defined(__IBMCPP__)
+extern int chdir Py_PROTO((char *));
+extern int rmdir Py_PROTO((char *));
+#else
extern int chdir Py_PROTO((const char *));
extern int rmdir Py_PROTO((const char *));
+#endif
extern int chmod Py_PROTO((const char *, mode_t));
extern int chown Py_PROTO((const char *, uid_t, gid_t));
extern char *getcwd Py_PROTO((char *, int));
Py_BEGIN_ALLOW_THREADS
res = (*func)(path1, path2);
Py_END_ALLOW_THREADS
- if (res < 0)
+ if (res != 0)
return posix_error();
Py_INCREF(Py_None);
return Py_None;
PyObject *self;
PyObject *args;
{
- /* XXX Should redo this putting the three versions of opendir
+ /* XXX Should redo this putting the (now four) versions of opendir
in separate files instead of having them all here... */
#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
return d;
+#else
+#if defined(PYOS_OS2)
+
+#ifndef MAX_PATH
+#define MAX_PATH CCHMAXPATH
+#endif
+ char *name, *pt;
+ int len;
+ PyObject *d, *v;
+ char namebuf[MAX_PATH+5];
+ HDIR hdir = 1;
+ ULONG srchcnt = 1;
+ FILEFINDBUF3 ep;
+ APIRET rc;
+
+ if (!PyArg_Parse(args, "s#", &name, &len))
+ return NULL;
+ if (len >= MAX_PATH) {
+ PyErr_SetString(PyExc_ValueError, "path too long");
+ return NULL;
+ }
+ strcpy(namebuf, name);
+ for (pt = namebuf; *pt; pt++)
+ if (*pt == '/')
+ *pt = '\\';
+ if (namebuf[len-1] != '\\')
+ namebuf[len++] = '\\';
+ strcpy(namebuf + len, "*.*");
+
+ if ((d = PyList_New(0)) == NULL)
+ return NULL;
+
+ rc = DosFindFirst(namebuf, // Wildcard Pattern to Match
+ &hdir, // Handle to Use While Search Directory
+ FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
+ &ep, sizeof(ep), // Structure to Receive Directory Entry
+ &srchcnt, // Max and Actual Count of Entries Per Iteration
+ FIL_STANDARD); // Format of Entry (EAs or Not)
+
+ if (rc != NO_ERROR) {
+ errno = ENOENT;
+ return posix_error();
+ }
+
+ if (srchcnt > 0) { // If Directory is NOT Totally Empty,
+ do {
+ if (ep.achName[0] == '.'
+ && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
+ continue; // Skip Over "." and ".." Names
+
+ strcpy(namebuf, ep.achName);
+
+ // Leave Case of Name Alone -- In Native Form
+ // (Removed Forced Lowercasing Code)
+
+ v = PyString_FromString(namebuf);
+ if (v == NULL) {
+ Py_DECREF(d);
+ d = NULL;
+ break;
+ }
+ if (PyList_Append(d, v) != 0) {
+ Py_DECREF(v);
+ Py_DECREF(d);
+ d = NULL;
+ break;
+ }
+ Py_DECREF(v);
+ } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
+ }
+
+ return d;
#else
char *name;
return d;
+#endif /* !PYOS_OS2 */
#endif /* !_MSC_VER */
#endif /* !MS_WIN32 */
}
if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
return NULL;
Py_BEGIN_ALLOW_THREADS
-#if defined(__WATCOMC__) || defined(_MSC_VER)
+#if defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP)
res = mkdir(path);
#else
res = mkdir(path, mode);
int pid, sig;
if (!PyArg_Parse(args, "(ii)", &pid, &sig))
return NULL;
+#if defined(__TOS_OS2__)
+ if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
+ APIRET rc;
+ if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
+ return posix_error();
+
+ } else if (sig == XCPT_SIGNAL_KILLPROC) {
+ APIRET rc;
+ if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
+ return posix_error();
+
+ } else
+ return NULL; // Unrecognized Signal Requested
+#else
if (kill(pid, sig) == -1)
return posix_error();
+#endif
Py_INCREF(Py_None);
return Py_None;
}
"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
Open a pipe to/from a command returning a file object.";
+#if defined(PYOS_OS2)
+int
+async_system(const char *command)
+{
+ char *p, errormsg[256], args[1024];
+ RESULTCODES rcodes;
+ APIRET rc;
+ char *shell = getenv("COMSPEC");
+ if (!shell)
+ shell = "cmd";
+
+ strcpy(args, shell);
+ p = &args[ strlen(args)+1 ];
+ strcpy(p, "/c ");
+ strcat(p, command);
+ p += strlen(p) + 1;
+ *p = '\0';
+
+ rc = DosExecPgm(errormsg, sizeof(errormsg),
+ EXEC_ASYNC, // Execute Async w/o Wait for Results
+ args,
+ NULL, // Inherit Parent's Environment
+ &rcodes, shell);
+ return rc;
+}
+
+FILE *
+popen(const char *command, const char *mode, int pipesize)
+{
+ HFILE rhan, whan;
+ FILE *retfd = NULL;
+ APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
+
+ if (rc != NO_ERROR)
+ return NULL; // ERROR - Unable to Create Anon Pipe
+
+ if (strchr(mode, 'r') != NULL) { // Treat Command as a Data Source
+ int oldfd = dup(1); // Save STDOUT Handle in Another Handle
+
+ DosEnterCritSec(); // Stop Other Threads While Changing Handles
+ close(1); // Make STDOUT Available for Reallocation
+
+ if (dup2(whan, 1) == 0) { // Connect STDOUT to Pipe Write Side
+ DosClose(whan); // Close Now-Unused Pipe Write Handle
+
+ if (async_system(command) == NO_ERROR)
+ retfd = fdopen(rhan, "rb"); // And Return Pipe Read Handle
+ }
+
+ dup2(oldfd, 1); // Reconnect STDOUT to Original Handle
+ DosExitCritSec(); // Now Allow Other Threads to Run
+
+ close(oldfd); // And Close Saved STDOUT Handle
+ return retfd; // Return fd of Pipe or NULL if Error
+
+ } else if (strchr(mode, 'w')) { // Treat Command as a Data Sink
+ int oldfd = dup(0); // Save STDIN Handle in Another Handle
+
+ DosEnterCritSec(); // Stop Other Threads While Changing Handles
+ close(0); // Make STDIN Available for Reallocation
+
+ if (dup2(rhan, 0) == 0) { // Connect STDIN to Pipe Read Side
+ DosClose(rhan); // Close Now-Unused Pipe Read Handle
+
+ if (async_system(command) == NO_ERROR)
+ retfd = fdopen(whan, "wb"); // And Return Pipe Write Handle
+ }
+
+ dup2(oldfd, 0); // Reconnect STDIN to Original Handle
+ DosExitCritSec(); // Now Allow Other Threads to Run
+
+ close(oldfd); // And Close Saved STDIN Handle
+ return retfd; // Return fd of Pipe or NULL if Error
+
+ } else
+ return NULL; // ERROR - Invalid Mode (Neither Read nor Write)
+}
+
+static PyObject *
+posix_popen(self, args)
+ PyObject *self;
+ PyObject *args;
+{
+ char *name;
+ char *mode = "r";
+ int bufsize = -1;
+ FILE *fp;
+ PyObject *f;
+ if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096);
+ Py_END_ALLOW_THREADS
+ if (fp == NULL)
+ return posix_error();
+ f = PyFile_FromFile(fp, name, mode, fclose);
+ if (f != NULL)
+ PyFile_SetBufSize(f, bufsize);
+ return f;
+}
+
+#else
static PyObject *
posix_popen(self, args)
PyObject *self;
PyFile_SetBufSize(f, bufsize);
return f;
}
+#endif
+
#endif /* HAVE_POPEN */
PyObject *self;
PyObject *args;
{
+#if defined(PYOS_OS2)
+ HFILE read, write;
+ APIRET rc;
+
+ if (!PyArg_Parse(args, ""))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ rc = DosCreatePipe( &read, &write, 4096);
+ Py_END_ALLOW_THREADS
+ if (rc != NO_ERROR)
+ return posix_error();
+
+ return Py_BuildValue("(ii)", read, write);
+#else
#if !defined(MS_WIN32)
int fds[2];
int res;
return posix_error();
return Py_BuildValue("(ii)", read, write);
#endif /* MS_WIN32 */
+#endif
}
#endif /* HAVE_PIPE */
#define INITFUNC initnt
#define MODNAME "nt"
#else
+#if defined(PYOS_OS2)
+#define INITFUNC initos2
+#define MODNAME "os2"
+#else
#define INITFUNC initposix
#define MODNAME "posix"
#endif
+#endif
void
INITFUNC()
#include <unistd.h>
#endif
-#ifndef MS_WINDOWS
+#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
extern int gethostname(); /* For Solaris, at least */
#endif
+#if defined(PYCC_VACPP)
+#include <types.h>
+#include <io.h>
+#include <sys/ioctl.h>
+#include <utils.h>
+#include <ctype.h>
+#endif
+
+#if defined(PYOS_OS2)
+#define INCL_DOS
+#define INCL_DOSERRORS
+#define INCL_NOPMAPI
+#include <os2.h>
+#endif
+
#include <sys/types.h>
#include "mytime.h"
#define FORCE_ANSI_FUNC_DEFS
#endif
+#if defined(PYOS_OS2)
+#define close soclose
+#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
+#define FORCE_ANSI_FUNC_DEFS
+#endif
+
#ifdef FORCE_ANSI_FUNC_DEFS
#define BUILD_FUNC_DEF_1( fnname, arg1type, arg1name ) \
fnname( arg1type arg1name )
}
else
#endif
+
+#if defined(PYOS_OS2)
+ if (sock_errno() != NO_ERROR) {
+ APIRET rc;
+ ULONG msglen;
+ char outbuf[100];
+ int myerrorcode = sock_errno();
+
+ /* Retrieve Socket-Related Error Message from MPTN.MSG File */
+ rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
+ myerrorcode - SOCBASEERR + 26, "mptn.msg", &msglen);
+ if (rc == NO_ERROR) {
+ PyObject *v;
+
+ outbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
+ if (strlen(outbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
+ char *lastc = &outbuf[ strlen(outbuf)-1 ];
+ while (lastc > outbuf && isspace(*lastc))
+ *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
+ }
+ v = Py_BuildValue("(is)", myerrorcode, outbuf);
+ if (v != NULL) {
+ PyErr_SetObject(PySocket_Error, v);
+ Py_DECREF(v);
+ }
+ return NULL;
+ }
+ }
+#endif
+
return PyErr_SetFromErrno(PySocket_Error);
}
return NULL;
Py_BEGIN_ALLOW_THREADS
#ifndef MS_WINDOWS
+#ifdef PYOS_OS2
+ block = !block;
+ ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
+#else /* !PYOS_OS2 */
delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
if (block)
delay_flag &= (~O_NDELAY);
else
delay_flag |= O_NDELAY;
fcntl (s->sock_fd, F_SETFL, delay_flag);
-#else
+#endif /* !PYOS_OS2 */
+#else /* MS_WINDOWS */
block = !block;
ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
-#endif
+#endif /* MS_WINDOWS */
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
Py_BEGIN_ALLOW_THREADS
n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
#ifndef MS_WINDOWS
+ #if defined(PYOS_OS2)
+ (struct sockaddr *)addrbuf, &addrlen
+ #else
(ANY *)addrbuf, &addrlen
+ #endif
#else
(struct sockaddr *)addrbuf, &addrlen
#endif
#endif /* MS_WINDOWS */
+#if defined(PYOS_OS2)
+
+/* Additional initialization and cleanup for OS/2 */
+
+static void
+OS2cleanup()
+{
+ /* No cleanup is necessary for OS/2 Sockets */
+}
+
+static int
+OS2init()
+{
+ char reason[64];
+ int rc = sock_init();
+
+ if (rc == 0) {
+ atexit(OS2cleanup);
+ return 1; // Indicate Success
+ }
+
+ sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
+ PyErr_SetString(PyExc_ImportError, reason);
+
+ return 0; // Indicate Failure
+}
+
+#endif /* PYOS_OS2 */
+
/* Initialize this module.
- This is called when the first 'import socket' is done,
- via a table in config.c, if config.c is compiled with USE_SOCKET
- defined.
-
- For MS_WINDOWS (which means any Windows variant), this module
- is actually called "_socket", and there's a wrapper "socket.py"
- which implements some missing functionality (such as makefile(),
- dup() and fromfd()). The import of "_socket" may fail with an
- ImportError exception if initialization of WINSOCK fails. When
- WINSOCK is initialized succesfully, a call to WSACleanup() is
- scheduled to be made at exit time. */
+ * This is called when the first 'import socket' is done,
+ * via a table in config.c, if config.c is compiled with USE_SOCKET
+ * defined.
+ *
+ * For MS_WINDOWS (which means any Windows variant), this module
+ * is actually called "_socket", and there's a wrapper "socket.py"
+ * which implements some missing functionality (such as makefile(),
+ * dup() and fromfd()). The import of "_socket" may fail with an
+ * ImportError exception if initialization of WINSOCK fails. When
+ * WINSOCK is initialized succesfully, a call to WSACleanup() is
+ * scheduled to be made at exit time.
+ *
+ * For OS/2, this module is also called "_socket" and uses a wrapper
+ * "socket.py" which implements that functionality that is missing
+ * when PC operating systems don't put socket descriptors in the
+ * operating system's filesystem layer.
+ */
void
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) || defined(PYOS_OS2)
init_socket()
#else
initsocket()
return;
m = Py_InitModule("_socket", PySocket_methods);
#else
+ #if defined(__TOS_OS2__)
+ if (!OS2init())
+ return;
+ m = Py_InitModule("_socket", PySocket_methods);
+ #else
m = Py_InitModule("socket", PySocket_methods);
+ #endif
#endif
d = PyModule_GetDict(m);
PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);