⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 posixmodule.c

📁 python s60 1.4.5版本的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
static const char stat_result__doc__[] = 
#ifndef SYMBIAN
"stat_result: Result from stat or lstat.\n\n\
This object may be accessed either as a tuple of\n\
  (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
\n\
Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
they are available as attributes only.\n\
\n\
See os.stat for more information.\n";
#else
"";
#endif

static const PyStructSequence_Field stat_result_fields[] = {
	{"st_mode",    "protection bits"},
	{"st_ino",     "inode"},
	{"st_dev",     "device"},
	{"st_nlink",   "number of hard links"},
	{"st_uid",     "user ID of owner"},
	{"st_gid",     "group ID of owner"},
	{"st_size",    "total size, in bytes"},
	{"st_atime",   "time of last access"},
	{"st_mtime",   "time of last modification"},
	{"st_ctime",   "time of last change"},
#ifdef HAVE_ST_BLKSIZE
	{"st_blksize", "blocksize for filesystem I/O"},
#endif
#ifdef HAVE_ST_BLOCKS
	{"st_blocks",  "number of blocks allocated"},
#endif
#ifdef HAVE_ST_RDEV
	{"st_rdev",    "device type (if inode device)"},
#endif
	{0}
};

#ifdef HAVE_ST_BLKSIZE
#define ST_BLKSIZE_IDX 10
#else
#define ST_BLKSIZE_IDX 9
#endif

#ifdef HAVE_ST_BLOCKS
#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
#else
#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
#endif

#ifdef HAVE_ST_RDEV
#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
#else
#define ST_RDEV_IDX ST_BLOCKS_IDX
#endif

static const PyStructSequence_Desc stat_result_desc = {
#ifndef SYMBIAN
	"stat_result", /* name */
#else
	"e32posix.stat_result", /* name */
#endif
	stat_result__doc__, /* doc */
	stat_result_fields,
	10
};

#ifndef SYMBIAN
static const char statvfs_result__doc__[] = 
"statvfs_result: Result from statvfs or fstatvfs.\n\n\
This object may be accessed either as a tuple of\n\
  (bsize,frsize,blocks,bfree,bavail,files,ffree,favail,flag,namemax),\n\
or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
\n\
See os.statvfs for more information.\n";

static const PyStructSequence_Field statvfs_result_fields[] = {
        {"f_bsize",  },
        {"f_frsize", },
        {"f_blocks", },
        {"f_bfree",  },
        {"f_bavail", },
        {"f_files",  },
        {"f_ffree",  },
        {"f_favail", },
        {"f_flag",   },
        {"f_namemax",},
        {0}
};

static const PyStructSequence_Desc statvfs_result_desc = {
	"statvfs_result", /* name */
	statvfs_result__doc__, /* doc */
	statvfs_result_fields,
	10
};
#endif /* not SYMBIAN */

#ifndef SYMBIAN
static PyTypeObject StatResultType;
static PyTypeObject StatVFSResultType;
#else
#define StatResultType (PYTHON_GLOBALS->tobj.t_StatResultType)
extern double e32_UTC_offset(void);
#endif

/* pack a system stat C structure into the Python stat tuple 
   (used by posix_stat() and posix_fstat()) */
static PyObject*
_pystat_fromstructstat(STRUCT_STAT st)
{
	PyObject *v = PyStructSequence_New(&StatResultType);
	if (v == NULL)
		return NULL;

        PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
#ifdef HAVE_LARGEFILE_SUPPORT
        PyStructSequence_SET_ITEM(v, 1, 
				  PyLong_FromLongLong((LONG_LONG)st.st_ino));
#else
        PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
#endif
#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
        PyStructSequence_SET_ITEM(v, 2, 
				  PyLong_FromLongLong((LONG_LONG)st.st_dev));
#else
        PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
#endif
        PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
        PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
        PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
#ifdef HAVE_LARGEFILE_SUPPORT
        PyStructSequence_SET_ITEM(v, 6, 
				  PyLong_FromLongLong((LONG_LONG)st.st_size));
#else
        PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
#endif
#if SIZEOF_TIME_T > SIZEOF_LONG
        PyStructSequence_SET_ITEM(v, 7, 
				  PyLong_FromLongLong((LONG_LONG)st.st_atime));
        PyStructSequence_SET_ITEM(v, 8, 
				  PyLong_FromLongLong((LONG_LONG)st.st_mtime));
        PyStructSequence_SET_ITEM(v, 9, 
				  PyLong_FromLongLong((LONG_LONG)st.st_ctime));
#else
	PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long)st.st_atime));
#ifndef SYMBIAN
        PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime));
#else
        PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime-
						       (long)e32_UTC_offset()));
#endif
        PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long)st.st_ctime));
#endif

#ifdef HAVE_ST_BLKSIZE
	PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, 
			 PyInt_FromLong((long)st.st_blksize));
#endif
#ifdef HAVE_ST_BLOCKS
	PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, 
			 PyInt_FromLong((long)st.st_blocks));
#endif
#ifdef HAVE_ST_RDEV
	PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
			 PyInt_FromLong((long)st.st_rdev));
#endif

	if (PyErr_Occurred()) {
		Py_DECREF(v);
		return NULL;
	}

	return v;
}

static PyObject *
posix_do_stat(PyObject *self, PyObject *args, char *format,
	      int (*statfunc)(const char *, STRUCT_STAT *))
{
	STRUCT_STAT st;
	char *path = NULL;	/* pass this to stat; do not free() it */
	char *pathfree = NULL;  /* this memory must be free'd */
	int res;

#ifdef MS_WIN32
	int pathlen;
	char pathcopy[MAX_PATH];
#endif /* MS_WIN32 */

	if (!PyArg_ParseTuple(args, format, 
	                      Py_FileSystemDefaultEncoding, &path))
		return NULL;
	pathfree = path;

#ifdef MS_WIN32
	pathlen = strlen(path);
	/* the library call can blow up if the file name is too long! */
	if (pathlen > MAX_PATH) {
		PyMem_Free(pathfree);
		errno = ENAMETOOLONG;
		return posix_error();
	}

	/* Remove trailing slash or backslash, unless it's the current
	   drive root (/ or \) or a specific drive's root (like c:\ or c:/).
	*/
	if (pathlen > 0 &&
	    (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) {
	    	/* It does end with a slash -- exempt the root drive cases. */
	    	/* XXX UNC root drives should also be exempted? */
	    	if (pathlen == 1 || (pathlen == 3 && path[1] == ':'))
	    		/* leave it alone */;
	    	else {
			/* nuke the trailing backslash */
			strncpy(pathcopy, path, pathlen);
			pathcopy[pathlen-1] = '\0';
			path = pathcopy;
		}
	}
#endif /* MS_WIN32 */

	Py_BEGIN_ALLOW_THREADS
	res = (*statfunc)(path, &st);
	Py_END_ALLOW_THREADS
	if (res != 0)
		return posix_error_with_allocated_filename(pathfree);

	PyMem_Free(pathfree);
	return _pystat_fromstructstat(st);
}


/* POSIX methods */

#ifndef SYMBIAN
static const char posix_access__doc__[] =
"access(path, mode) -> 1 if granted, 0 otherwise\n\
Use the real uid/gid to test for access to a path.  Note that most\n\
operations will use the effective uid/gid, therefore this routine can\n\
be used in a suid/sgid environment to test if the invoking user has the\n\
specified access to the path.  The mode argument can be F_OK to test\n\
existence, or the inclusive-OR of R_OK, W_OK, and X_OK.";

static PyObject *
posix_access(PyObject *self, PyObject *args)
{
	char *path;
	int mode;
	int res;

	if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = access(path, mode);
	Py_END_ALLOW_THREADS
	return(PyInt_FromLong(res == 0 ? 1L : 0L));
}
#endif /* not SYMBIAN */

#ifndef F_OK
#define F_OK 0
#endif
#ifndef R_OK
#define R_OK 4
#endif
#ifndef W_OK
#define W_OK 2
#endif
#ifndef X_OK
#define X_OK 1
#endif

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

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

	if (!PyArg_ParseTuple(args, "i:ttyname", &id))
		return NULL;

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

#ifdef HAVE_CTERMID
static const char posix_ctermid__doc__[] =
"ctermid() -> String\n\
Return the name of the controlling terminal for this process.";

static PyObject *
posix_ctermid(PyObject *self, PyObject *args)
{
        char *ret;
        char buffer[L_ctermid];

	if (!PyArg_ParseTuple(args, ":ctermid"))
		return NULL;

#ifdef USE_CTERMID_R
	ret = ctermid_r(buffer);
#else
        ret = ctermid(buffer);
#endif
	if (ret == NULL)
		return(posix_error());
	return(PyString_FromString(buffer));
}
#endif

static const char posix_chdir__doc__[] =
#ifndef SYMBIAN
"chdir(path) -> None\n\
Change the current working directory to the specified path.";
#else
"";
#endif

static PyObject *
posix_chdir(PyObject *self, PyObject *args)
{
	return posix_1str(args, "et:chdir", chdir);
}


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

static PyObject *
posix_chmod(PyObject *self, PyObject *args)
{
	char *path = NULL;
	int i;
	int res;
	if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, 
	                      &path, &i))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = chmod(path, i);
	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_CHROOT
static const char posix_chroot__doc__[] = 
"chroot(path) -> None\n\
Change root directory to path.";

static PyObject *
posix_chroot(PyObject *self, PyObject *args)
{
	return posix_1str(args, "et:chroot", chroot);
}
#endif

#ifdef HAVE_FSYNC
static const char posix_fsync__doc__[] =
#ifndef SYMBIAN
"fsync(fildes) -> None\n\
force write of file with filedescriptor to disk.";
#else
"";
#endif

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

#ifdef HAVE_FDATASYNC

#ifdef __hpux
extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
#endif

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

static PyObject *
posix_fdatasync(PyObject *self, PyObject *args)
{
       return posix_int(args, "i:fdatasync", fdatasync);
}
#endif /* HAVE_FDATASYNC */


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

static PyObject *
posix_chown(PyObject *self, PyObject *args)
{
	char *path = NULL;
	int uid, gid;
	int res;
	if (!PyArg_ParseTuple(args, "etii:chown", 
	                      Py_FileSystemDefaultEncoding, &path, 
	                      &uid, &gid))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = chown(path, (uid_t) uid, (gid_t) gid);
	Py_END_ALLOW_THREADS
	if (res < 0)
		return posix_error_with_allocated_filename(path);
	PyMem_Free(path);
	Py_INCREF(Py_None);
	return Py_None;
}
#endif /* HAVE_CHOWN */


#ifdef HAVE_GETCWD
static const char posix_getcwd__doc__[] =
#ifndef SYMBIAN
"getcwd() -> path\n\
Return a string representing the current working directory.";
#else
"";
#endif

static PyObject *
posix_getcwd(PyObject *self, PyObject *args)
{
	char buf[1026];
	char *res;
	if (!PyArg_ParseTuple(args, ":getcwd"))
		return NULL;
	Py_BEGIN_ALLOW_THREADS
	res = getcwd(buf, sizeof buf);
	Py_END_ALLOW_THREADS
	if (res == NULL)
		return posix_error();
	return PyString_FromString(buf);
}
#endif


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

static PyObject *
posix_link(PyObject *self, PyObject *args)
{
	return posix_2str(args, "etet:link", link);
}
#endif /* HAVE_LINK */

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

static PyObject *
posix_listdir(PyObject *self, PyObject *args)
{
	/* 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)

	PyObject *d, *v;
	HANDLE hFindFile;
	WIN32_FIND_DATA FileData;
	/* MAX_PATH characters could mean a bigger encoded string */
	char namebuf[MAX_PATH*2+5];
	char *bufptr = namebuf;
	int len = sizeof(namebuf)/sizeof(namebuf[0]);
	char ch;

	if (!PyArg_ParseTuple(args, "et#:listdir", 
	                      Py_FileSystemDefaultEncoding, &bufptr, &len))
		return NULL;
	ch = namebuf[len-1];
	if (ch != '/' && ch != '\\' && ch != ':')
		namebuf[len++] = '/';
	strcpy(namebuf + len, "*.*");

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

	hFindFile = FindFirstFile(namebuf, &FileData);
	if (hFindFile == INVALID_HANDLE_VALUE) {
		errno = GetLastError();
		if (errno == ERROR_FILE_NOT_FOUND)
			return PyList_New(0);
		return win32_error("FindFirstFile", namebuf);
	}
	do {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -