📄 posixmodule.c
字号:
if (FileData.cFileName[0] == '.' &&
(FileData.cFileName[1] == '\0' ||
FileData.cFileName[1] == '.' &&
FileData.cFileName[2] == '\0'))
continue;
v = PyString_FromString(FileData.cFileName);
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 (FindNextFile(hFindFile, &FileData) == TRUE);
if (FindClose(hFindFile) == FALSE)
return win32_error("FindClose", namebuf);
return d;
#elif defined(_MSC_VER) /* 16-bit Windows */
#ifndef MAX_PATH
#define MAX_PATH 250
#endif
char *name, *pt;
int len;
PyObject *d, *v;
char namebuf[MAX_PATH+5];
struct _find_t ep;
if (!PyArg_ParseTuple(args, "t#:listdir", &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;
if (_dos_findfirst(namebuf, _A_RDONLY |
_A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
{
errno = ENOENT;
return posix_error_with_filename(name);
}
do {
if (ep.name[0] == '.' &&
(ep.name[1] == '\0' ||
ep.name[1] == '.' &&
ep.name[2] == '\0'))
continue;
strcpy(namebuf, ep.name);
for (pt = namebuf; *pt; pt++)
if (isupper(*pt))
*pt = tolower(*pt);
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 (_dos_findnext(&ep) == 0);
return d;
#elif 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_ParseTuple(args, "t#:listdir", &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_with_filename(name);
}
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;
PyObject *d, *v;
DIR *dirp;
struct dirent *ep;
if (!PyArg_ParseTuple(args, "s:listdir", &name))
return NULL;
if ((dirp = opendir(name)) == NULL) {
return posix_error_with_filename(name);
}
if ((d = PyList_New(0)) == NULL) {
closedir(dirp);
return NULL;
}
while ((ep = readdir(dirp)) != NULL) {
if (ep->d_name[0] == '.' &&
(NAMLEN(ep) == 1 ||
(ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
continue;
v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
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);
}
closedir(dirp);
return d;
#endif /* which OS */
} /* end of posix_listdir */
#ifdef MS_WIN32
/* A helper function for abspath on win32 */
static PyObject *
posix__getfullpathname(PyObject *self, PyObject *args)
{
/* assume encoded strings wont more than double no of chars */
char inbuf[MAX_PATH*2];
char *inbufp = inbuf;
int insize = sizeof(inbuf)/sizeof(inbuf[0]);
char outbuf[MAX_PATH*2];
char *temp;
if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
Py_FileSystemDefaultEncoding, &inbufp,
&insize))
return NULL;
if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
outbuf, &temp))
return win32_error("GetFullPathName", inbuf);
return PyString_FromString(outbuf);
} /* end of posix__getfullpathname */
#endif /* MS_WIN32 */
static const char posix_mkdir__doc__[] =
#ifndef SYMBIAN
"mkdir(path [, mode=0777]) -> None\n\
Create a directory.";
#else
"";
#endif
static PyObject *
posix_mkdir(PyObject *self, PyObject *args)
{
int res;
char *path = NULL;
int mode = 0777;
if (!PyArg_ParseTuple(args, "et|i:mkdir",
Py_FileSystemDefaultEncoding, &path, &mode))
return NULL;
Py_BEGIN_ALLOW_THREADS
#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
res = mkdir(path);
#else
res = mkdir(path, mode);
#endif
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error_with_allocated_filename(path);
PyMem_Free(path);
Py_INCREF(Py_None);
return Py_None;
}
#ifdef HAVE_NICE
#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
#include <sys/resource.h>
#endif
#endif
static const char posix_nice__doc__[] =
"nice(inc) -> new_priority\n\
Decrease the priority of process and return new priority.";
static PyObject *
posix_nice(PyObject *self, PyObject *args)
{
int increment, value;
if (!PyArg_ParseTuple(args, "i:nice", &increment))
return NULL;
/* There are two flavours of 'nice': one that returns the new
priority (as required by almost all standards out there) and the
Linux/FreeBSD/BSDI one, which returns '0' on success and advices
the use of getpriority() to get the new priority.
If we are of the nice family that returns the new priority, we
need to clear errno before the call, and check if errno is filled
before calling posix_error() on a returnvalue of -1, because the
-1 may be the actual new priority! */
errno = 0;
value = nice(increment);
#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
if (value == 0)
value = getpriority(PRIO_PROCESS, 0);
#endif
if (value == -1 && errno != 0)
/* either nice() or getpriority() returned an error */
return posix_error();
return PyInt_FromLong((long) value);
}
#endif /* HAVE_NICE */
static const char posix_rename__doc__[] =
#ifndef SYMBIAN
"rename(old, new) -> None\n\
Rename a file or directory.";
#else
"";
#endif
static PyObject *
posix_rename(PyObject *self, PyObject *args)
{
return posix_2str(args, "etet:rename", rename);
}
static const char posix_rmdir__doc__[] =
#ifndef SYMBIAN
"rmdir(path) -> None\n\
Remove a directory.";
#else
"";
#endif
static PyObject *
posix_rmdir(PyObject *self, PyObject *args)
{
return posix_1str(args, "et:rmdir", rmdir);
}
static const char posix_stat__doc__[] =
#ifndef SYMBIAN
"stat(path) -> (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,\n\
st_size, st_atime, st_mtime, st_ctime)\n\
Perform a stat system call on the given path.";
#else
"";
#endif
static PyObject *
posix_stat(PyObject *self, PyObject *args)
{
return posix_do_stat(self, args, "et:stat", STAT);
}
#ifdef HAVE_SYSTEM
static const char posix_system__doc__[] =
"system(command) -> exit_status\n\
Execute the command (a string) in a subshell.";
static PyObject *
posix_system(PyObject *self, PyObject *args)
{
char *command;
long sts;
if (!PyArg_ParseTuple(args, "s:system", &command))
return NULL;
Py_BEGIN_ALLOW_THREADS
sts = system(command);
Py_END_ALLOW_THREADS
return PyInt_FromLong(sts);
}
#endif
#ifndef SYMBIAN
static const char posix_umask__doc__[] =
"umask(new_mask) -> old_mask\n\
Set the current numeric umask and return the previous umask.";
static PyObject *
posix_umask(PyObject *self, PyObject *args)
{
int i;
if (!PyArg_ParseTuple(args, "i:umask", &i))
return NULL;
i = (int)umask(i);
if (i < 0)
return posix_error();
return PyInt_FromLong((long)i);
}
#endif /* not SYMBIAN */
static const char posix_unlink__doc__[] =
#ifndef SYMBIAN
"unlink(path) -> None\n\
Remove a file (same as remove(path)).";
#else
"";
#endif
static const char posix_remove__doc__[] =
#ifndef SYMBIAN
"remove(path) -> None\n\
Remove a file (same as unlink(path)).";
#else
"";
#endif
static PyObject *
posix_unlink(PyObject *self, PyObject *args)
{
return posix_1str(args, "et:remove", unlink);
}
#ifdef HAVE_UNAME
static const char posix_uname__doc__[] =
"uname() -> (sysname, nodename, release, version, machine)\n\
Return a tuple identifying the current operating system.";
static PyObject *
posix_uname(PyObject *self, PyObject *args)
{
struct utsname u;
int res;
if (!PyArg_ParseTuple(args, ":uname"))
return NULL;
Py_BEGIN_ALLOW_THREADS
res = uname(&u);
Py_END_ALLOW_THREADS
if (res < 0)
return posix_error();
return Py_BuildValue("(sssss)",
u.sysname,
u.nodename,
u.release,
u.version,
u.machine);
}
#endif /* HAVE_UNAME */
#ifndef SYMBIAN
static const char posix_utime__doc__[] =
"utime(path, (atime, utime)) -> None\n\
utime(path, None) -> None\n\
Set the access and modified time of the file to the given values. If the\n\
second form is used, set the access and modified times to the current time.";
static PyObject *
posix_utime(PyObject *self, PyObject *args)
{
char *path;
long atime, mtime;
int res;
PyObject* arg;
/* XXX should define struct utimbuf instead, above */
#ifdef HAVE_UTIME_H
struct utimbuf buf;
#define ATIME buf.actime
#define MTIME buf.modtime
#define UTIME_ARG &buf
#else /* HAVE_UTIME_H */
time_t buf[2];
#define ATIME buf[0]
#define MTIME buf[1]
#define UTIME_ARG buf
#endif /* HAVE_UTIME_H */
if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
return NULL;
if (arg == Py_None) {
/* optional time values not given */
Py_BEGIN_ALLOW_THREADS
res = utime(path, NULL);
Py_END_ALLOW_THREADS
}
else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) {
PyErr_SetString(PyExc_TypeError,
"utime() arg 2 must be a tuple (atime, mtime)");
return NULL;
}
else {
ATIME = atime;
MTIME = mtime;
Py_BEGIN_ALLOW_THREADS
res = utime(path, UTIME_ARG);
Py_END_ALLOW_THREADS
}
if (res < 0)
return posix_error_with_filename(path);
Py_INCREF(Py_None);
return Py_None;
#undef UTIME_ARG
#undef ATIME
#undef MTIME
}
#endif /* not SYMBIAN */
/* Process operations */
static const char posix__exit__doc__[] =
#ifndef SYMBIAN
"_exit(status)\n\
Exit to the system with specified status, without normal exit processing.";
#else
"";
#endif
static PyObject *
posix__exit(PyObject *self, PyObject *args)
{
int sts;
if (!PyArg_ParseTuple(args, "i:_exit", &sts))
return NULL;
_exit(sts);
return NULL; /* Make gcc -Wall happy */
}
#ifdef HAVE_EXECV
static const char posix_execv__doc__[] =
"execv(path, args)\n\
Execute an executable path with arguments, replacing current process.\n\
\n\
path: path of executable file\n\
args: tuple or list of strings";
static PyObject *
posix_execv(PyObject *self, PyObject *args)
{
char *path;
PyObject *argv;
char **argvlist;
int i, argc;
PyObject *(*getitem)(PyObject *, int);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -