2019-04-20 19:29:15 +02:00
|
|
|
/**
|
|
|
|
|
* @PROJECT PH7 Engine for the AerScript Interpreter
|
|
|
|
|
* @COPYRIGHT See COPYING in the top level directory
|
|
|
|
|
* @FILE engine/vfs.c
|
|
|
|
|
* @DESCRIPTION Implements a virtual file systems (VFS) for the PH7 engine
|
|
|
|
|
* @DEVELOPERS Symisc Systems <devel@symisc.net>
|
|
|
|
|
* Rafal Kupiec <belliash@codingworkshop.eu.org>
|
2019-04-20 19:53:16 +02:00
|
|
|
* David Carlier <devnexen@gmail.com>
|
2019-04-20 19:29:15 +02:00
|
|
|
*/
|
2018-07-12 13:26:32 +02:00
|
|
|
#include "ph7int.h"
|
|
|
|
|
/*
|
2018-07-12 17:24:46 +02:00
|
|
|
* Given a string containing the path of a file or directory, this function
|
2018-07-12 13:26:32 +02:00
|
|
|
* return the parent directory's path.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_PRIVATE const char *PH7_ExtractDirName(const char *zPath, int nByte, int *pLen) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zEnd = &zPath[nByte - 1];
|
2018-07-12 17:24:46 +02:00
|
|
|
int c, d;
|
2018-07-12 13:26:32 +02:00
|
|
|
c = d = '/';
|
|
|
|
|
#ifdef __WINNT__
|
|
|
|
|
d = '\\';
|
|
|
|
|
#endif
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zEnd > zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd--;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
*pLen = (int)(zEnd - zPath);
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
if((*pLen) == (int)sizeof(char) && zPath[0] == '/') {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Normalize path on windows */
|
|
|
|
|
return "\\";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-07-12 17:24:46 +02:00
|
|
|
if(zEnd == zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* No separator, return "." as the current directory */
|
2018-07-12 13:26:32 +02:00
|
|
|
*pLen = sizeof(char);
|
|
|
|
|
return ".";
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if((*pLen) == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
*pLen = sizeof(char);
|
|
|
|
|
#ifdef __WINNT__
|
|
|
|
|
return "\\";
|
|
|
|
|
#else
|
|
|
|
|
return "/";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return zPath;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool chdir(string $directory)
|
|
|
|
|
* Change the current directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $directory
|
|
|
|
|
* The new current directory
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_chdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xChdir == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xChdir(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool chroot(string $directory)
|
|
|
|
|
* Change the root directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $directory
|
|
|
|
|
* The path to change the root directory to
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_chroot(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xChroot == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xChroot(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string getcwd(void)
|
|
|
|
|
* Gets the current working directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the current working directory on success, or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_getcwd(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xGetcwd == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xGetcwd(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc != PH7_OK) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool rmdir(string $directory)
|
|
|
|
|
* Removes directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $directory
|
|
|
|
|
* The path to the directory
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_rmdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xRmdir == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xRmdir(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool is_dir(string $filename)
|
|
|
|
|
* Tells whether the given filename is a directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_dir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xIsdir == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xIsdir(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool mkdir(string $pathname[,int $mode = 0777 [,bool $recursive = false])
|
|
|
|
|
* Make a directory.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $pathname
|
|
|
|
|
* The directory path.
|
|
|
|
|
* $mode
|
|
|
|
|
* The mode is 0777 by default, which means the widest possible access.
|
|
|
|
|
* Note:
|
|
|
|
|
* mode is ignored on Windows.
|
|
|
|
|
* Note that you probably want to specify the mode as an octal number, which means
|
|
|
|
|
* it should have a leading zero. The mode is also modified by the current umask
|
|
|
|
|
* which you can change using umask().
|
|
|
|
|
* $recursive
|
|
|
|
|
* Allows the creation of nested directories specified in the pathname.
|
|
|
|
|
* Defaults to FALSE. (Not used)
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_mkdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
int iRecursive = 0;
|
|
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
int iMode, rc;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xMkdir == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
|
|
|
|
iMode = 0;
|
|
|
|
|
#else
|
|
|
|
|
/* Assume UNIX */
|
|
|
|
|
iMode = 0777;
|
|
|
|
|
#endif
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iMode = ph7_value_to_int(apArg[1]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iRecursive = ph7_value_to_bool(apArg[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xMkdir(zPath, iMode, iRecursive);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool rename(string $oldname,string $newname)
|
|
|
|
|
* Attempts to rename oldname to newname.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $oldname
|
|
|
|
|
* Old name.
|
|
|
|
|
* $newname
|
|
|
|
|
* New name.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_rename(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zOld, *zNew;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xRename == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
zOld = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
zNew = ph7_value_to_string(apArg[1], 0);
|
|
|
|
|
rc = pVfs->xRename(zOld, zNew);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string realpath(string $path)
|
|
|
|
|
* Returns canonicalized absolute pathname.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $path
|
|
|
|
|
* Target path.
|
|
|
|
|
* Return
|
|
|
|
|
* Canonicalized absolute pathname on success. or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_realpath(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
int rc;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xRealpath == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Set an empty string until the underlying OS interface change that */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
rc = pVfs->xRealpath(zPath, pCtx);
|
|
|
|
|
if(rc != PH7_OK) {
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int sleep(int $seconds)
|
|
|
|
|
* Delays the program execution for the given number of seconds.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $seconds
|
|
|
|
|
* Halt time in seconds.
|
|
|
|
|
* Return
|
|
|
|
|
* Zero on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
int rc, nSleep;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_int(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xSleep == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Amount to sleep */
|
|
|
|
|
nSleep = ph7_value_to_int(apArg[0]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nSleep < 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Invalid value, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation (Microseconds) */
|
|
|
|
|
rc = pVfs->xSleep((unsigned int)(nSleep * SX_USEC_PER_SEC));
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc != PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return zero */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* void usleep(int $micro_seconds)
|
|
|
|
|
* Delays program execution for the given number of micro seconds.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $micro_seconds
|
|
|
|
|
* Halt time in micro seconds. A micro second is one millionth of a second.
|
|
|
|
|
* Return
|
|
|
|
|
* None.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_usleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int nSleep;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_int(apArg[0])) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Missing/Invalid argument, return immediately */
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xSleep == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Amount to sleep */
|
|
|
|
|
nSleep = ph7_value_to_int(apArg[0]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nSleep < 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Invalid value, return immediately */
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation (Microseconds) */
|
|
|
|
|
pVfs->xSleep((unsigned int)nSleep);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool unlink (string $filename)
|
|
|
|
|
* Delete a file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_unlink(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xUnlink == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xUnlink(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool chmod(string $filename,int $mode)
|
|
|
|
|
* Attempts to change the mode of the specified file to that given in mode.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* $mode
|
|
|
|
|
* Mode (Must be an integer)
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_chmod(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int iMode;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xChmod == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the mode */
|
|
|
|
|
iMode = ph7_value_to_int(apArg[1]);
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xChmod(zPath, iMode);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool chown(string $filename,string $user)
|
|
|
|
|
* Attempts to change the owner of the file filename to user user.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* $user
|
|
|
|
|
* Username.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_chown(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath, *zUser;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xChown == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the user */
|
2018-07-12 17:24:46 +02:00
|
|
|
zUser = ph7_value_to_string(apArg[1], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xChown(zPath, zUser);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool chgrp(string $filename,string $group)
|
|
|
|
|
* Attempts to change the group of the file filename to group.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* $group
|
|
|
|
|
* groupname.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_chgrp(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath, *zGroup;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xChgrp == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the user */
|
2018-07-12 17:24:46 +02:00
|
|
|
zGroup = ph7_value_to_string(apArg[1], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xChgrp(zPath, zGroup);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 disk_free_space(string $directory)
|
|
|
|
|
* Returns available space on filesystem or disk partition.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $directory
|
|
|
|
|
* A directory of the filesystem or disk partition.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the number of available bytes as a 64-bit integer or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_disk_free_space(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iSize;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFreeSpace == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iSize = pVfs->xFreeSpace(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iSize);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 disk_total_space(string $directory)
|
|
|
|
|
* Returns the total size of a filesystem or disk partition.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $directory
|
|
|
|
|
* A directory of the filesystem or disk partition.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the number of available bytes as a 64-bit integer or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_disk_total_space(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iSize;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xTotalSpace == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iSize = pVfs->xTotalSpace(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iSize);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool file_exists(string $filename)
|
|
|
|
|
* Checks whether a file or directory exists.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_file_exists(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFileExists == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xFileExists(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 file_size(string $filename)
|
|
|
|
|
* Gets the size for the given file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* File size on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_file_size(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iSize;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFileSize == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iSize = pVfs->xFileSize(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iSize);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 fileatime(string $filename)
|
|
|
|
|
* Gets the last access time of the given file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* File atime on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_file_atime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iTime;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFileAtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iTime = pVfs->xFileAtime(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iTime);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 filemtime(string $filename)
|
|
|
|
|
* Gets file modification time.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* File mtime on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_file_mtime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iTime;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFileMtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iTime = pVfs->xFileMtime(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iTime);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 filectime(string $filename)
|
|
|
|
|
* Gets inode change time of file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* File ctime on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_file_ctime(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iTime;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFileCtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iTime = pVfs->xFileCtime(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iTime);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-08-15 21:58:21 +02:00
|
|
|
/*
|
|
|
|
|
* int64 filegroup(string $filename)
|
|
|
|
|
* Gets the file group.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* The group ID of the file or FALSE on failure.
|
|
|
|
|
*/
|
|
|
|
|
static int PH7_vfs_file_group(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iGroup;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-08-15 21:58:21 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
|
|
|
|
if(pVfs == 0 || pVfs->xFileCtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:58:21 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
|
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iGroup = pVfs->xFileGroup(zPath);
|
|
|
|
|
/* IO return value */
|
|
|
|
|
ph7_result_int64(pCtx, iGroup);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-08-15 21:28:59 +02:00
|
|
|
/*
|
|
|
|
|
* int64 fileinode(string $filename)
|
|
|
|
|
* Gets the file inode.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* The inode number of the file or FALSE on failure.
|
|
|
|
|
*/
|
|
|
|
|
static int PH7_vfs_file_inode(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iInode;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-08-15 21:28:59 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
|
|
|
|
if(pVfs == 0 || pVfs->xFileCtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-08-15 21:28:59 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
|
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iInode = pVfs->xFileInode(zPath);
|
|
|
|
|
/* IO return value */
|
|
|
|
|
ph7_result_int64(pCtx, iInode);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-08-15 21:58:21 +02:00
|
|
|
/*
|
|
|
|
|
* int64 fileowner(string $filename)
|
|
|
|
|
* Gets the file owner.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* The user ID of the owner of the file or FALSE on failure.
|
|
|
|
|
*/
|
|
|
|
|
static int PH7_vfs_file_owner(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath;
|
|
|
|
|
ph7_int64 iOwner;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-08-15 21:58:21 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
|
|
|
|
if(pVfs == 0 || pVfs->xFileCtime == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:58:21 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
|
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iOwner = pVfs->xFileOwner(zPath);
|
|
|
|
|
/* IO return value */
|
|
|
|
|
ph7_result_int64(pCtx, iOwner);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
/*
|
|
|
|
|
* bool is_file(string $filename)
|
|
|
|
|
* Tells whether the filename is a regular file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_file(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-08-15 21:35:44 +02:00
|
|
|
if(pVfs == 0 || pVfs->xIsFile == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-08-15 21:35:44 +02:00
|
|
|
rc = pVfs->xIsFile(zPath);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool is_link(string $filename)
|
|
|
|
|
* Tells whether the filename is a symbolic link.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_link(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-08-15 21:35:44 +02:00
|
|
|
if(pVfs == 0 || pVfs->xIsLink == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-08-15 21:35:44 +02:00
|
|
|
rc = pVfs->xIsLink(zPath);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool is_readable(string $filename)
|
|
|
|
|
* Tells whether a file exists and is readable.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_readable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xReadable == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xReadable(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool is_writable(string $filename)
|
|
|
|
|
* Tells whether the filename is writable.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_writable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xWritable == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xWritable(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool is_executable(string $filename)
|
|
|
|
|
* Tells whether the filename is executable.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_is_executable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xExecutable == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pVfs->xExecutable(zPath);
|
|
|
|
|
/* IO return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string filetype(string $filename)
|
|
|
|
|
* Gets file type.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* The type of the file. Possible values are fifo, char, dir, block, link
|
|
|
|
|
* file, socket and unknown.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_filetype(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Missing/Invalid argument, return 'unknown' */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "unknown", sizeof("unknown") - 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xFiletype == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the desired directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Set the empty string as the default return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
pVfs->xFiletype(zPath, pCtx);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* array stat(string $filename)
|
|
|
|
|
* Gives information about a file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* An associative array on success holding the following entries on success
|
|
|
|
|
* 0 dev device number
|
|
|
|
|
* 1 ino inode number (zero on windows)
|
|
|
|
|
* 2 mode inode protection mode
|
|
|
|
|
* 3 nlink number of links
|
|
|
|
|
* 4 uid userid of owner (zero on windows)
|
|
|
|
|
* 5 gid groupid of owner (zero on windows)
|
|
|
|
|
* 6 rdev device type, if inode device
|
|
|
|
|
* 7 size size in bytes
|
|
|
|
|
* 8 atime time of last access (Unix timestamp)
|
|
|
|
|
* 9 mtime time of last modification (Unix timestamp)
|
|
|
|
|
* 10 ctime time of last inode change (Unix timestamp)
|
|
|
|
|
* 11 blksize blocksize of filesystem IO (zero on windows)
|
|
|
|
|
* 12 blocks number of 512-byte blocks allocated.
|
|
|
|
|
* Note:
|
|
|
|
|
* FALSE is returned on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_stat(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
ph7_value *pArray, *pValue;
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xStat == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Create the array and the working value */
|
|
|
|
|
pArray = ph7_context_new_array(pCtx);
|
|
|
|
|
pValue = ph7_context_new_scalar(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pArray == 0 || pValue == 0) {
|
2018-09-04 08:26:58 +02:00
|
|
|
PH7_VmMemoryError(pCtx->pVm);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Extract the file path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xStat(zPath, pArray, pValue);
|
|
|
|
|
if(rc != PH7_OK) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the associative array */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_value(pCtx, pArray);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Don't worry about freeing memory here, everything will be released
|
2018-07-12 13:26:32 +02:00
|
|
|
* automatically as soon we return from this function. */
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* array lstat(string $filename)
|
|
|
|
|
* Gives information about a file or symbolic link.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file.
|
|
|
|
|
* Return
|
|
|
|
|
* An associative array on success holding the following entries on success
|
|
|
|
|
* 0 dev device number
|
|
|
|
|
* 1 ino inode number (zero on windows)
|
|
|
|
|
* 2 mode inode protection mode
|
|
|
|
|
* 3 nlink number of links
|
|
|
|
|
* 4 uid userid of owner (zero on windows)
|
|
|
|
|
* 5 gid groupid of owner (zero on windows)
|
|
|
|
|
* 6 rdev device type, if inode device
|
|
|
|
|
* 7 size size in bytes
|
|
|
|
|
* 8 atime time of last access (Unix timestamp)
|
|
|
|
|
* 9 mtime time of last modification (Unix timestamp)
|
|
|
|
|
* 10 ctime time of last inode change (Unix timestamp)
|
|
|
|
|
* 11 blksize blocksize of filesystem IO (zero on windows)
|
|
|
|
|
* 12 blocks number of 512-byte blocks allocated.
|
|
|
|
|
* Note:
|
|
|
|
|
* FALSE is returned on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_lstat(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
ph7_value *pArray, *pValue;
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xlStat == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Create the array and the working value */
|
|
|
|
|
pArray = ph7_context_new_array(pCtx);
|
|
|
|
|
pValue = ph7_context_new_scalar(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pArray == 0 || pValue == 0) {
|
2018-09-04 08:26:58 +02:00
|
|
|
PH7_VmMemoryError(pCtx->pVm);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Extract the file path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xlStat(zPath, pArray, pValue);
|
|
|
|
|
if(rc != PH7_OK) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the associative array */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_value(pCtx, pArray);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Don't worry about freeing memory here, everything will be released
|
2018-07-12 13:26:32 +02:00
|
|
|
* automatically as soon we return from this function. */
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string getenv(string $varname)
|
|
|
|
|
* Gets the value of an environment variable.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $varname
|
|
|
|
|
* The variable name.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the value of the environment variable varname, or FALSE if the environment
|
2018-07-12 17:24:46 +02:00
|
|
|
* variable varname does not exist.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_getenv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zEnv;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int iLen;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xGetenv == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the environment variable */
|
2018-07-12 17:24:46 +02:00
|
|
|
zEnv = ph7_value_to_string(apArg[0], &iLen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Set a boolean FALSE as the default return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
if(iLen < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Empty string */
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
pVfs->xGetenv(zEnv, pCtx);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool putenv(string $settings)
|
|
|
|
|
* Set the value of an environment variable.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $setting
|
|
|
|
|
* The setting, like "FOO=BAR"
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* TRUE on success or FALSE on failure.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_putenv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zName, *zValue;
|
|
|
|
|
char *zSettings, *zEnd;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
2018-07-12 17:24:46 +02:00
|
|
|
int iLen, rc;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the setting variable */
|
2018-07-12 17:24:46 +02:00
|
|
|
zSettings = (char *)ph7_value_to_string(apArg[0], &iLen);
|
|
|
|
|
if(iLen < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Empty string, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Parse the setting */
|
|
|
|
|
zEnd = &zSettings[iLen];
|
|
|
|
|
zValue = 0;
|
|
|
|
|
zName = zSettings;
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zSettings < zEnd) {
|
|
|
|
|
if(zSettings[0] == '=') {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Null terminate the name */
|
|
|
|
|
zSettings[0] = 0;
|
|
|
|
|
zValue = &zSettings[1];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
zSettings++;
|
|
|
|
|
}
|
|
|
|
|
/* Install the environment variable in the $_Env array */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(zValue == 0 || zName[0] == 0 || zValue >= zEnd || zName >= zValue) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Invalid settings,retun FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
if(zSettings < zEnd) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zSettings[0] = '=';
|
|
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_vm_config(pCtx->pVm, PH7_VM_CONFIG_ENV_ATTR, zName, zValue, (int)(zEnd - zValue));
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xSetenv == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
zSettings[0] = '=';
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xSetenv(zName, zValue);
|
|
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
zSettings[0] = '=';
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool touch(string $filename[,int64 $time = time()[,int64 $atime]])
|
|
|
|
|
* Sets access and modification time of file.
|
|
|
|
|
* Note: On windows
|
|
|
|
|
* If the file does not exists,it will not be created.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* The name of the file being touched.
|
|
|
|
|
* $time
|
|
|
|
|
* The touch time. If time is not supplied, the current system time is used.
|
|
|
|
|
* $atime
|
|
|
|
|
* If present, the access time of the given filename is set to the value of atime.
|
2018-07-12 17:24:46 +02:00
|
|
|
* Otherwise, it is set to the value passed to the time parameter. If neither are
|
2018-07-12 13:26:32 +02:00
|
|
|
* present, the current system time is used.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_touch(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
ph7_int64 nTime, nAccess;
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zFile;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid argument, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xTouch == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nTime = nAccess = -1;
|
2018-07-12 17:24:46 +02:00
|
|
|
zFile = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
nTime = ph7_value_to_int64(apArg[1]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2) {
|
2018-07-12 13:26:32 +02:00
|
|
|
nAccess = ph7_value_to_int64(apArg[1]);
|
2018-07-12 17:24:46 +02:00
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
nAccess = nTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xTouch(zFile, nTime, nAccess);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Path processing functions that do not need access to the VFS layer
|
|
|
|
|
* Authors:
|
|
|
|
|
* Symisc Systems,devel@symisc.net.
|
2019-05-15 20:02:39 +02:00
|
|
|
* Copyright (C) Symisc Systems,https://ph7.symisc.net
|
2018-07-12 13:26:32 +02:00
|
|
|
* Status:
|
|
|
|
|
* Stable.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* string dirname(string $path)
|
|
|
|
|
* Returns parent directory's path.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $path
|
|
|
|
|
* Target path.
|
|
|
|
|
* On Windows, both slash (/) and backslash (\) are used as directory separator character.
|
|
|
|
|
* In other environments, it is the forward slash (/).
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* The path of the parent directory. If there are no slashes in path, a dot ('.')
|
2018-07-12 13:26:32 +02:00
|
|
|
* is returned, indicating the current directory.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_dirname(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath, *zDir;
|
|
|
|
|
int iLen, iDirlen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return the empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], &iLen);
|
|
|
|
|
if(iLen < 1) {
|
2018-08-15 16:38:33 +02:00
|
|
|
/* Return "." */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, ".", sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
zDir = PH7_ExtractDirName(zPath, iLen, &iDirlen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return directory name */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, zDir, iDirlen);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string basename(string $path[, string $suffix ])
|
|
|
|
|
* Returns trailing name component of path.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $path
|
|
|
|
|
* Target path.
|
|
|
|
|
* On Windows, both slash (/) and backslash (\) are used as directory separator character.
|
|
|
|
|
* In other environments, it is the forward slash (/).
|
|
|
|
|
* $suffix
|
|
|
|
|
* If the name component ends in suffix this will also be cut off.
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* The base name of the given path.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_basename(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zPath, *zBase, *zEnd;
|
|
|
|
|
int c, d, iLen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Missing/Invalid argument, return the empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
c = d = '/';
|
|
|
|
|
#ifdef __WINNT__
|
|
|
|
|
d = '\\';
|
|
|
|
|
#endif
|
|
|
|
|
/* Point to the target path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], &iLen);
|
|
|
|
|
if(iLen < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
zEnd = &zPath[iLen - 1];
|
|
|
|
|
/* Ignore trailing '/' */
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zEnd > zPath && ((int)zEnd[0] == c || (int)zEnd[0] == d)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd--;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
iLen = (int)(&zEnd[1] - zPath);
|
|
|
|
|
while(zEnd > zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd--;
|
|
|
|
|
}
|
|
|
|
|
zBase = (zEnd > zPath) ? &zEnd[1] : zPath;
|
|
|
|
|
zEnd = &zPath[iLen];
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1 && ph7_value_is_string(apArg[1])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zSuffix;
|
|
|
|
|
int nSuffix;
|
|
|
|
|
/* Strip suffix */
|
2018-07-12 17:24:46 +02:00
|
|
|
zSuffix = ph7_value_to_string(apArg[1], &nSuffix);
|
|
|
|
|
if(nSuffix > 0 && nSuffix < iLen && SyMemcmp(&zEnd[-nSuffix], zSuffix, nSuffix) == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd -= nSuffix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Store the basename */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, zBase, (int)(zEnd - zBase));
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* value pathinfo(string $path [,int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ])
|
|
|
|
|
* Returns information about a file path.
|
|
|
|
|
* Parameter
|
|
|
|
|
* $path
|
|
|
|
|
* The path to be parsed.
|
|
|
|
|
* $options
|
|
|
|
|
* If present, specifies a specific element to be returned; one of
|
|
|
|
|
* PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.
|
|
|
|
|
* Return
|
|
|
|
|
* If the options parameter is not passed, an associative array containing the following
|
2018-07-12 17:24:46 +02:00
|
|
|
* elements is returned: dirname, basename, extension (if any), and filename.
|
|
|
|
|
* If options is present, returns a string containing the requested element.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
|
|
|
|
typedef struct path_info path_info;
|
2018-07-12 17:24:46 +02:00
|
|
|
struct path_info {
|
2018-07-12 13:26:32 +02:00
|
|
|
SyString sDir; /* Directory [i.e: /var/www] */
|
|
|
|
|
SyString sBasename; /* Basename [i.e httpd.conf] */
|
|
|
|
|
SyString sExtension; /* File extension [i.e xml,pdf..] */
|
|
|
|
|
SyString sFilename; /* Filename */
|
|
|
|
|
};
|
|
|
|
|
/*
|
|
|
|
|
* Extract path fields.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static sxi32 ExtractPathInfo(const char *zPath, int nByte, path_info *pOut) {
|
|
|
|
|
const char *zPtr, *zEnd = &zPath[nByte - 1];
|
2018-07-12 13:26:32 +02:00
|
|
|
SyString *pCur;
|
2018-07-12 17:24:46 +02:00
|
|
|
int c, d;
|
2018-07-12 13:26:32 +02:00
|
|
|
c = d = '/';
|
|
|
|
|
#ifdef __WINNT__
|
|
|
|
|
d = '\\';
|
|
|
|
|
#endif
|
|
|
|
|
/* Zero the structure */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyZero(pOut, sizeof(path_info));
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Handle special case */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nByte == sizeof(char) && ((int)zPath[0] == c || (int)zPath[0] == d)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&pOut->sDir, "\\", sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
#else
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&pOut->sDir, "/", sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
|
|
|
|
return SXRET_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the basename */
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zEnd > zPath && ((int)zEnd[0] != c && (int)zEnd[0] != d)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd--;
|
|
|
|
|
}
|
|
|
|
|
zPtr = (zEnd > zPath) ? &zEnd[1] : zPath;
|
|
|
|
|
zEnd = &zPath[nByte];
|
|
|
|
|
/* dirname */
|
|
|
|
|
pCur = &pOut->sDir;
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(pCur, zPath, zPtr - zPath);
|
|
|
|
|
if(pCur->nByte > 1) {
|
|
|
|
|
SyStringTrimTrailingChar(pCur, '/');
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringTrimTrailingChar(pCur, '\\');
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
2018-07-12 17:24:46 +02:00
|
|
|
} else if((int)zPath[0] == c || (int)zPath[0] == d) {
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&pOut->sDir, "\\", sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
#else
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&pOut->sDir, "/", sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
/* basename/filename */
|
|
|
|
|
pCur = &pOut->sBasename;
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(pCur, zPtr, zEnd - zPtr);
|
|
|
|
|
SyStringTrimLeadingChar(pCur, '/');
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringTrimLeadingChar(pCur, '\\');
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringDupPtr(&pOut->sFilename, pCur);
|
|
|
|
|
if(pCur->nByte > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* extension */
|
|
|
|
|
zEnd--;
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zEnd > pCur->zString /*basename*/ && zEnd[0] != '.') {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd--;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(zEnd > pCur->zString) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zEnd++; /* Jump leading dot */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&pOut->sExtension, zEnd, &zPath[nByte] - zEnd);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Fix filename */
|
|
|
|
|
pCur = &pOut->sFilename;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pCur->nByte > SyStringLength(&pOut->sExtension)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
pCur->nByte -= 1 + SyStringLength(&pOut->sExtension);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return SXRET_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* value pathinfo(string $path [,int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ])
|
|
|
|
|
* See block comment above.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_pathinfo(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPath;
|
|
|
|
|
path_info sInfo;
|
|
|
|
|
SyString *pComp;
|
|
|
|
|
int iLen;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Missing/Invalid argument, return the empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], &iLen);
|
|
|
|
|
if(iLen < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract path info */
|
2018-07-12 17:24:46 +02:00
|
|
|
ExtractPathInfo(zPath, iLen, &sInfo);
|
|
|
|
|
if(nArg > 1 && ph7_value_is_int(apArg[1])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return path component */
|
|
|
|
|
int nComp = ph7_value_to_int(apArg[1]);
|
2018-07-12 17:24:46 +02:00
|
|
|
switch(nComp) {
|
|
|
|
|
case 1: /* PATHINFO_DIRNAME */
|
|
|
|
|
pComp = &sInfo.sDir;
|
|
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_result_string(pCtx, pComp->zString, (int)pComp->nByte);
|
|
|
|
|
} else {
|
|
|
|
|
/* Expand the empty string */
|
|
|
|
|
ph7_result_string(pCtx, "", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2: /*PATHINFO_BASENAME*/
|
|
|
|
|
pComp = &sInfo.sBasename;
|
|
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_result_string(pCtx, pComp->zString, (int)pComp->nByte);
|
|
|
|
|
} else {
|
|
|
|
|
/* Expand the empty string */
|
|
|
|
|
ph7_result_string(pCtx, "", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3: /*PATHINFO_EXTENSION*/
|
|
|
|
|
pComp = &sInfo.sExtension;
|
|
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_result_string(pCtx, pComp->zString, (int)pComp->nByte);
|
|
|
|
|
} else {
|
|
|
|
|
/* Expand the empty string */
|
|
|
|
|
ph7_result_string(pCtx, "", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4: /*PATHINFO_FILENAME*/
|
|
|
|
|
pComp = &sInfo.sFilename;
|
|
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_result_string(pCtx, pComp->zString, (int)pComp->nByte);
|
|
|
|
|
} else {
|
|
|
|
|
/* Expand the empty string */
|
|
|
|
|
ph7_result_string(pCtx, "", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Expand the empty string */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
|
|
|
|
break;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return an associative array */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_value *pArray, *pValue;
|
2018-07-12 13:26:32 +02:00
|
|
|
pArray = ph7_context_new_array(pCtx);
|
|
|
|
|
pValue = ph7_context_new_scalar(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pArray == 0 || pValue == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Out of mem, return NULL */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* dirname */
|
|
|
|
|
pComp = &sInfo.sDir;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_value_string(pValue, pComp->zString, (int)pComp->nByte);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the insertion */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_array_add_strkey_elem(pArray, "dirname", pValue); /* Will make it's own copy */
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Reset the string cursor */
|
|
|
|
|
ph7_value_reset_string_cursor(pValue);
|
|
|
|
|
/* basername */
|
|
|
|
|
pComp = &sInfo.sBasename;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_value_string(pValue, pComp->zString, (int)pComp->nByte);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the insertion */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_array_add_strkey_elem(pArray, "basename", pValue); /* Will make it's own copy */
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Reset the string cursor */
|
|
|
|
|
ph7_value_reset_string_cursor(pValue);
|
|
|
|
|
/* extension */
|
|
|
|
|
pComp = &sInfo.sExtension;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_value_string(pValue, pComp->zString, (int)pComp->nByte);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the insertion */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_array_add_strkey_elem(pArray, "extension", pValue); /* Will make it's own copy */
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Reset the string cursor */
|
|
|
|
|
ph7_value_reset_string_cursor(pValue);
|
|
|
|
|
/* filename */
|
|
|
|
|
pComp = &sInfo.sFilename;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pComp->nByte > 0) {
|
|
|
|
|
ph7_value_string(pValue, pComp->zString, (int)pComp->nByte);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the insertion */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_array_add_strkey_elem(pArray, "filename", pValue); /* Will make it's own copy */
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Return the created array */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_value(pCtx, pArray);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Don't worry about freeing memory, everything will be released
|
|
|
|
|
* automatically as soon we return from this foreign function.
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Globbing implementation extracted from the sqlite3 source tree.
|
2019-05-15 20:02:39 +02:00
|
|
|
* Original author: D. Richard Hipp (https://www.sqlite.org)
|
2018-07-12 13:26:32 +02:00
|
|
|
* Status: Public Domain
|
|
|
|
|
*/
|
|
|
|
|
typedef unsigned char u8;
|
|
|
|
|
/* An array to map all upper-case characters into their corresponding
|
2018-07-12 17:24:46 +02:00
|
|
|
** lower-case character.
|
2018-07-12 13:26:32 +02:00
|
|
|
**
|
|
|
|
|
** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
|
|
|
|
|
** handle case conversions for the UTF character set since the tables
|
|
|
|
|
** involved are nearly as big or bigger than SQLite itself.
|
|
|
|
|
*/
|
|
|
|
|
static const unsigned char sqlite3UpperToLower[] = {
|
2018-07-12 17:24:46 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
|
|
|
|
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
|
|
|
|
|
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
|
|
|
|
|
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103,
|
|
|
|
|
104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
|
|
|
|
|
122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
|
|
|
|
|
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
|
|
|
|
|
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
|
|
|
|
|
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
|
|
|
|
|
162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
|
|
|
|
|
180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
|
|
|
|
|
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215,
|
|
|
|
|
216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
|
|
|
|
|
234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
|
|
|
|
|
252, 253, 254, 255
|
2018-07-12 13:26:32 +02:00
|
|
|
};
|
|
|
|
|
#define GlogUpperToLower(A) if( A<0x80 ){ A = sqlite3UpperToLower[A]; }
|
|
|
|
|
/*
|
|
|
|
|
** Assuming zIn points to the first byte of a UTF-8 character,
|
|
|
|
|
** advance zIn to point to the first byte of the next UTF-8 character.
|
|
|
|
|
*/
|
|
|
|
|
#define SQLITE_SKIP_UTF8(zIn) { \
|
2018-07-12 17:24:46 +02:00
|
|
|
if( (*(zIn++))>=0xc0 ){ \
|
|
|
|
|
while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
|
|
|
|
|
} \
|
|
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
/*
|
|
|
|
|
** Compare two UTF-8 strings for equality where the first string can
|
|
|
|
|
** potentially be a "glob" expression. Return true (1) if they
|
|
|
|
|
** are the same and false (0) if they are different.
|
|
|
|
|
**
|
|
|
|
|
** Globbing rules:
|
|
|
|
|
**
|
|
|
|
|
** '*' Matches any sequence of zero or more characters.
|
|
|
|
|
**
|
|
|
|
|
** '?' Matches exactly one character.
|
|
|
|
|
**
|
|
|
|
|
** [...] Matches one character from the enclosed list of
|
|
|
|
|
** characters.
|
|
|
|
|
**
|
|
|
|
|
** [^...] Matches one character not in the enclosed list.
|
|
|
|
|
**
|
|
|
|
|
** With the [...] and [^...] matching, a ']' character can be included
|
|
|
|
|
** in the list by making it the first character after '[' or '^'. A
|
|
|
|
|
** range of characters can be specified using '-'. Example:
|
|
|
|
|
** "[a-z]" matches any single lower-case letter. To match a '-', make
|
|
|
|
|
** it the last character in the list.
|
|
|
|
|
**
|
|
|
|
|
** This routine is usually quick, but can be N**2 in the worst case.
|
|
|
|
|
**
|
|
|
|
|
** Hints: to match '*' or '?', put them in "[]". Like this:
|
|
|
|
|
**
|
|
|
|
|
** abc[*]xyz Matches "abc*xyz" only
|
|
|
|
|
*/
|
|
|
|
|
static int patternCompare(
|
2018-07-12 17:24:46 +02:00
|
|
|
const u8 *zPattern, /* The glob pattern */
|
|
|
|
|
const u8 *zString, /* The string to compare against the glob */
|
|
|
|
|
const int esc, /* The escape character */
|
|
|
|
|
int noCase
|
|
|
|
|
) {
|
|
|
|
|
int c, c2;
|
|
|
|
|
int invert;
|
|
|
|
|
int seen;
|
|
|
|
|
u8 matchOne = '?';
|
|
|
|
|
u8 matchAll = '*';
|
|
|
|
|
u8 matchSet = '[';
|
|
|
|
|
int prevEscape = 0; /* True if the previous character was 'escape' */
|
|
|
|
|
if(!zPattern || !zString) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
while((c = PH7_Utf8Read(zPattern, 0, &zPattern)) != 0) {
|
|
|
|
|
if(!prevEscape && c == matchAll) {
|
|
|
|
|
while((c = PH7_Utf8Read(zPattern, 0, &zPattern)) == matchAll
|
|
|
|
|
|| c == matchOne) {
|
|
|
|
|
if(c == matchOne && PH7_Utf8Read(zString, 0, &zString) == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(c == 0) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else if(c == esc) {
|
|
|
|
|
c = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
if(c == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if(c == matchSet) {
|
|
|
|
|
if((esc == 0) || (matchSet < 0x80)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
while(*zString && patternCompare(&zPattern[-1], zString, esc, noCase) == 0) {
|
|
|
|
|
SQLITE_SKIP_UTF8(zString);
|
|
|
|
|
}
|
|
|
|
|
return *zString != 0;
|
|
|
|
|
}
|
|
|
|
|
while((c2 = PH7_Utf8Read(zString, 0, &zString)) != 0) {
|
|
|
|
|
if(noCase) {
|
|
|
|
|
GlogUpperToLower(c2);
|
|
|
|
|
GlogUpperToLower(c);
|
|
|
|
|
while(c2 != 0 && c2 != c) {
|
|
|
|
|
c2 = PH7_Utf8Read(zString, 0, &zString);
|
|
|
|
|
GlogUpperToLower(c2);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
while(c2 != 0 && c2 != c) {
|
|
|
|
|
c2 = PH7_Utf8Read(zString, 0, &zString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(c2 == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if(patternCompare(zPattern, zString, esc, noCase)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
} else if(!prevEscape && c == matchOne) {
|
|
|
|
|
if(PH7_Utf8Read(zString, 0, &zString) == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if(c == matchSet) {
|
|
|
|
|
int prior_c = 0;
|
|
|
|
|
if(esc == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
seen = 0;
|
|
|
|
|
invert = 0;
|
|
|
|
|
c = PH7_Utf8Read(zString, 0, &zString);
|
|
|
|
|
if(c == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
c2 = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
if(c2 == '^') {
|
|
|
|
|
invert = 1;
|
|
|
|
|
c2 = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
}
|
|
|
|
|
if(c2 == ']') {
|
|
|
|
|
if(c == ']') {
|
|
|
|
|
seen = 1;
|
|
|
|
|
}
|
|
|
|
|
c2 = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
}
|
|
|
|
|
while(c2 && c2 != ']') {
|
|
|
|
|
if(c2 == '-' && zPattern[0] != ']' && zPattern[0] != 0 && prior_c > 0) {
|
|
|
|
|
c2 = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
if(c >= prior_c && c <= c2) {
|
|
|
|
|
seen = 1;
|
|
|
|
|
}
|
|
|
|
|
prior_c = 0;
|
|
|
|
|
} else {
|
|
|
|
|
if(c == c2) {
|
|
|
|
|
seen = 1;
|
|
|
|
|
}
|
|
|
|
|
prior_c = c2;
|
|
|
|
|
}
|
|
|
|
|
c2 = PH7_Utf8Read(zPattern, 0, &zPattern);
|
|
|
|
|
}
|
|
|
|
|
if(c2 == 0 || (seen ^ invert) == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} else if(esc == c && !prevEscape) {
|
|
|
|
|
prevEscape = 1;
|
|
|
|
|
} else {
|
|
|
|
|
c2 = PH7_Utf8Read(zString, 0, &zString);
|
|
|
|
|
if(noCase) {
|
|
|
|
|
GlogUpperToLower(c);
|
|
|
|
|
GlogUpperToLower(c2);
|
|
|
|
|
}
|
|
|
|
|
if(c != c2) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
prevEscape = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *zString == 0;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Wrapper around patternCompare() defined above.
|
|
|
|
|
* See block comment above for more information.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int Glob(const unsigned char *zPattern, const unsigned char *zString, int iEsc, int CaseCompare) {
|
2018-07-12 13:26:32 +02:00
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(iEsc < 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iEsc = '\\';
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = patternCompare(zPattern, zString, iEsc, CaseCompare);
|
2018-07-12 13:26:32 +02:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool fnmatch(string $pattern,string $string[,int $flags = 0 ])
|
|
|
|
|
* Match filename against a pattern.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $pattern
|
|
|
|
|
* The shell wildcard pattern.
|
|
|
|
|
* $string
|
|
|
|
|
* The tested string.
|
|
|
|
|
* $flags
|
|
|
|
|
* A list of possible flags:
|
|
|
|
|
* FNM_NOESCAPE Disable backslash escaping.
|
|
|
|
|
* FNM_PATHNAME Slash in string only matches slash in the given pattern.
|
|
|
|
|
* FNM_PERIOD Leading period in string must be exactly matched by period in the given pattern.
|
|
|
|
|
* FNM_CASEFOLD Caseless match.
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* TRUE if there is a match, FALSE otherwise.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fnmatch(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zString, *zPattern;
|
2018-07-12 13:26:32 +02:00
|
|
|
int iEsc = '\\';
|
|
|
|
|
int noCase = 0;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the pattern and the string */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPattern = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
zString = ph7_value_to_string(apArg[1], 0);
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Extract the flags if available */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2 && ph7_value_is_int(apArg[2])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
rc = ph7_value_to_int(apArg[2]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc & 0x01 /*FNM_NOESCAPE*/) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iEsc = 0;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc & 0x08 /*FNM_CASEFOLD*/) {
|
2018-07-12 13:26:32 +02:00
|
|
|
noCase = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Go globbing */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = Glob((const unsigned char *)zPattern, (const unsigned char *)zString, iEsc, noCase);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Globbing result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool strglob(string $pattern,string $string)
|
|
|
|
|
* Match string against a pattern.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $pattern
|
|
|
|
|
* The shell wildcard pattern.
|
|
|
|
|
* $string
|
|
|
|
|
* The tested string.
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* TRUE if there is a match, FALSE otherwise.
|
2018-07-12 13:26:32 +02:00
|
|
|
* Note that this a symisc eXtension.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_strglob(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zString, *zPattern;
|
2018-07-12 13:26:32 +02:00
|
|
|
int iEsc = '\\';
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the pattern and the string */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPattern = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
zString = ph7_value_to_string(apArg[1], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Go globbing */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = Glob((const unsigned char *)zPattern, (const unsigned char *)zString, iEsc, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Globbing result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool link(string $target,string $link)
|
|
|
|
|
* Create a hard link.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $target
|
|
|
|
|
* Target of the link.
|
|
|
|
|
* $link
|
|
|
|
|
* The link name.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_link(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zTarget, *zLink;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xLink == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the given arguments */
|
2018-07-12 17:24:46 +02:00
|
|
|
zTarget = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
zLink = ph7_value_to_string(apArg[1], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xLink(zTarget, zLink, 0/*Not a symbolic link */);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool symlink(string $target,string $link)
|
|
|
|
|
* Creates a symbolic link.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $target
|
|
|
|
|
* Target of the link.
|
|
|
|
|
* $link
|
|
|
|
|
* The link name.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_symlink(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
const char *zTarget, *zLink;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0]) || !ph7_value_is_string(apArg[1])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xLink == 0) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO routine not implemented, return NULL */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the given arguments */
|
2018-07-12 17:24:46 +02:00
|
|
|
zTarget = ph7_value_to_string(apArg[0], 0);
|
|
|
|
|
zLink = ph7_value_to_string(apArg[1], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pVfs->xLink(zTarget, zLink, 1/*A symbolic link */);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int umask([ int $mask ])
|
|
|
|
|
* Changes the current umask.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $mask
|
|
|
|
|
* The new umask.
|
|
|
|
|
* Return
|
|
|
|
|
* umask() without arguments simply returns the current umask.
|
|
|
|
|
* Otherwise the old umask is returned.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_umask(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
|
|
|
int iOld, iNew;
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xUmask == 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return -1 */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_int(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
iNew = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iNew = ph7_value_to_int(apArg[0]);
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iOld = pVfs->xUmask(iNew);
|
|
|
|
|
/* Old mask */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, iOld);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string sys_get_temp_dir()
|
|
|
|
|
* Returns directory path used for temporary files.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the path of the temporary directory.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_sys_get_temp_dir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
/* Set the empty string as the default return value */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xTempDir == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return "" */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
pVfs->xTempDir(pCtx);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string get_current_user()
|
|
|
|
|
* Returns the name of the current working user.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the name of the current working user.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_get_current_user(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xUsername == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
|
|
|
|
/* IO routine not implemented */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Set a dummy username */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "unknown", sizeof("unknown") - 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
pVfs->xUsername(pCtx);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 getmypid()
|
|
|
|
|
* Gets process ID.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the process ID.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_getmypid(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_int64 nProcessId;
|
|
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xProcessId == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return -1 */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nProcessId = (ph7_int64)pVfs->xProcessId();
|
|
|
|
|
/* Set the result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, nProcessId);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int getmyuid()
|
|
|
|
|
* Get user ID.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the user ID.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_getmyuid(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int nUid;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xUid == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return -1 */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nUid = pVfs->xUid();
|
|
|
|
|
/* Set the result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, nUid);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int getmygid()
|
|
|
|
|
* Get group ID.
|
|
|
|
|
* Parameters
|
|
|
|
|
* None
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the group ID.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_getmygid(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_vfs *pVfs;
|
|
|
|
|
int nGid;
|
|
|
|
|
/* Point to the underlying vfs */
|
|
|
|
|
pVfs = (ph7_vfs *)ph7_context_user_data(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pVfs == 0 || pVfs->xGid == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
SXUNUSED(nArg); /* cc warning */
|
|
|
|
|
SXUNUSED(apArg);
|
2019-11-18 08:11:13 +01:00
|
|
|
/* IO routine not implemented, return -1 */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying VFS",
|
|
|
|
|
ph7_function_name(pCtx)
|
|
|
|
|
);
|
|
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nGid = pVfs->xGid();
|
|
|
|
|
/* Set the result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, nGid);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
#ifdef __WINNT__
|
2018-07-12 17:24:46 +02:00
|
|
|
#include <Windows.h>
|
2018-07-12 13:26:32 +02:00
|
|
|
#elif defined(__UNIXES__)
|
2018-07-12 17:24:46 +02:00
|
|
|
#include <sys/utsname.h>
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
|
|
|
|
/*
|
|
|
|
|
* string php_uname([ string $mode = "a" ])
|
|
|
|
|
* Returns information about the host operating system.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $mode
|
|
|
|
|
* mode is a single character that defines what information is returned:
|
|
|
|
|
* 'a': This is the default. Contains all modes in the sequence "s n r v m".
|
|
|
|
|
* 's': Operating system name. eg. FreeBSD.
|
|
|
|
|
* 'n': Host name. eg. localhost.example.com.
|
|
|
|
|
* 'r': Release name. eg. 5.1.2-RELEASE.
|
|
|
|
|
* 'v': Version information. Varies a lot between operating systems.
|
|
|
|
|
* 'm': Machine type. eg. i386.
|
|
|
|
|
* Return
|
|
|
|
|
* OS description as a string.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_vfs_ph7_uname(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
#if defined(__WINNT__)
|
|
|
|
|
const char *zName = "Microsoft Windows";
|
|
|
|
|
OSVERSIONINFOW sVer;
|
|
|
|
|
#elif defined(__UNIXES__)
|
|
|
|
|
struct utsname sName;
|
|
|
|
|
#endif
|
|
|
|
|
const char *zMode = "a";
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 0 && ph7_value_is_string(apArg[0])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the desired mode */
|
2018-07-12 17:24:46 +02:00
|
|
|
zMode = ph7_value_to_string(apArg[0], 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
#if defined(__WINNT__)
|
|
|
|
|
sVer.dwOSVersionInfoSize = sizeof(sVer);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(TRUE != GetVersionExW(&sVer)) {
|
|
|
|
|
ph7_result_string(pCtx, zName, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(sVer.dwPlatformId == VER_PLATFORM_WIN32_NT) {
|
|
|
|
|
if(sVer.dwMajorVersion <= 4) {
|
2018-07-12 13:26:32 +02:00
|
|
|
zName = "Microsoft Windows NT";
|
2018-07-12 17:24:46 +02:00
|
|
|
} else if(sVer.dwMajorVersion == 5) {
|
|
|
|
|
switch(sVer.dwMinorVersion) {
|
|
|
|
|
case 0:
|
|
|
|
|
zName = "Microsoft Windows 2000";
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
zName = "Microsoft Windows XP";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
zName = "Microsoft Windows Server 2003";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if(sVer.dwMajorVersion == 6) {
|
|
|
|
|
switch(sVer.dwMinorVersion) {
|
|
|
|
|
case 0:
|
|
|
|
|
zName = "Microsoft Windows Vista";
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
zName = "Microsoft Windows 7";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
zName = "Microsoft Windows Server 2008";
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
zName = "Microsoft Windows 8";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-08-16 18:19:33 +02:00
|
|
|
} else if(sVer.dwMajorVersion == 10) {
|
|
|
|
|
switch(sVer.dwMinorVersion) {
|
|
|
|
|
case 0:
|
|
|
|
|
zName = "Microsoft Windows 10";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
switch(zMode[0]) {
|
|
|
|
|
case 's':
|
|
|
|
|
/* Operating system name */
|
|
|
|
|
ph7_result_string(pCtx, zName, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
/* Host name */
|
|
|
|
|
ph7_result_string(pCtx, "localhost", (int)sizeof("localhost") - 1);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
case 'v':
|
|
|
|
|
/* Version information. */
|
|
|
|
|
ph7_result_string_format(pCtx, "%u.%u build %u",
|
|
|
|
|
sVer.dwMajorVersion, sVer.dwMinorVersion, sVer.dwBuildNumber
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
case 'm':
|
|
|
|
|
/* Machine name */
|
|
|
|
|
ph7_result_string(pCtx, "x86", (int)sizeof("x86") - 1);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ph7_result_string_format(pCtx, "%s localhost %u.%u build %u x86",
|
|
|
|
|
zName,
|
|
|
|
|
sVer.dwMajorVersion, sVer.dwMinorVersion, sVer.dwBuildNumber
|
|
|
|
|
);
|
|
|
|
|
break;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
#elif defined(__UNIXES__)
|
2018-07-12 17:24:46 +02:00
|
|
|
if(uname(&sName) != 0) {
|
|
|
|
|
ph7_result_string(pCtx, "Unix", (int)sizeof("Unix") - 1);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
switch(zMode[0]) {
|
|
|
|
|
case 's':
|
|
|
|
|
/* Operating system name */
|
|
|
|
|
ph7_result_string(pCtx, sName.sysname, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
/* Host name */
|
|
|
|
|
ph7_result_string(pCtx, sName.nodename, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
/* Release information */
|
|
|
|
|
ph7_result_string(pCtx, sName.release, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
/* Version information. */
|
|
|
|
|
ph7_result_string(pCtx, sName.version, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
case 'm':
|
|
|
|
|
/* Machine name */
|
|
|
|
|
ph7_result_string(pCtx, sName.machine, -1/* Compute length automatically*/);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ph7_result_string_format(pCtx,
|
|
|
|
|
"%s %s %s %s %s",
|
|
|
|
|
sName.sysname,
|
|
|
|
|
sName.release,
|
|
|
|
|
sName.version,
|
|
|
|
|
sName.nodename,
|
|
|
|
|
sName.machine
|
|
|
|
|
);
|
|
|
|
|
break;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
#else
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, "Unknown Operating System", (int)sizeof("Unknown Operating System") - 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Section:
|
|
|
|
|
* IO stream implementation.
|
|
|
|
|
* Authors:
|
|
|
|
|
* Symisc Systems,devel@symisc.net.
|
2019-05-15 20:02:39 +02:00
|
|
|
* Copyright (C) Symisc Systems,https://ph7.symisc.net
|
2018-07-12 13:26:32 +02:00
|
|
|
* Status:
|
|
|
|
|
* Stable.
|
|
|
|
|
*/
|
|
|
|
|
typedef struct io_private io_private;
|
2018-07-12 17:24:46 +02:00
|
|
|
struct io_private {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream; /* Underlying IO device */
|
|
|
|
|
void *pHandle; /* IO handle */
|
|
|
|
|
/* Unbuffered IO */
|
|
|
|
|
SyBlob sBuffer; /* Working buffer */
|
|
|
|
|
sxu32 nOfft; /* Current read offset */
|
|
|
|
|
sxu32 iMagic; /* Sanity check to avoid misuse */
|
|
|
|
|
};
|
|
|
|
|
#define IO_PRIVATE_MAGIC 0xFEAC14
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
#define IO_PRIVATE_INVALID(IO) ( IO == 0 || IO->iMagic != IO_PRIVATE_MAGIC )
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Forward declaration */
|
|
|
|
|
static void ResetIOPrivate(io_private *pDev);
|
|
|
|
|
/*
|
|
|
|
|
* bool ftruncate(resource $handle,int64 $size)
|
|
|
|
|
* Truncates a file to a given length.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Note:
|
|
|
|
|
* The handle must be open for writing.
|
|
|
|
|
* $size
|
|
|
|
|
* The size to truncate to.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_ftruncate(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xTrunc == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xTrunc(pDev->pHandle, ph7_value_to_int64(apArg[1]));
|
|
|
|
|
if(rc == PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Discard buffered data */
|
|
|
|
|
ResetIOPrivate(pDev);
|
|
|
|
|
}
|
|
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int fseek(resource $handle,int $offset[,int $whence = SEEK_SET ])
|
|
|
|
|
* Seeks on a file pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* A file system pointer resource that is typically created using fopen().
|
|
|
|
|
* $offset
|
|
|
|
|
* The offset.
|
|
|
|
|
* To move to a position before the end-of-file, you need to pass a negative
|
|
|
|
|
* value in offset and set whence to SEEK_END.
|
|
|
|
|
* whence
|
|
|
|
|
* whence values are:
|
|
|
|
|
* SEEK_SET - Set position equal to offset bytes.
|
|
|
|
|
* SEEK_CUR - Set position to current location plus offset.
|
|
|
|
|
* SEEK_END - Set position to end-of-file plus offset.
|
|
|
|
|
* Return
|
|
|
|
|
* 0 on success,-1 on failure
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fseek(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
ph7_int64 iOfft;
|
2018-07-12 17:24:46 +02:00
|
|
|
int whence;
|
2018-07-12 13:26:32 +02:00
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xSeek == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
|
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_int(pCtx, -1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the offset */
|
|
|
|
|
iOfft = ph7_value_to_int64(apArg[1]);
|
|
|
|
|
whence = 0;/* SEEK_SET */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2 && ph7_value_is_int(apArg[2])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
whence = ph7_value_to_int(apArg[2]);
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xSeek(pDev->pHandle, iOfft, whence);
|
|
|
|
|
if(rc == PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Ignore buffered data */
|
|
|
|
|
ResetIOPrivate(pDev);
|
|
|
|
|
}
|
|
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int(pCtx, rc == PH7_OK ? 0 : - 1);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int64 ftell(resource $handle)
|
|
|
|
|
* Returns the current position of the file read/write pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the position of the file pointer referenced by handle
|
|
|
|
|
* as an integer; i.e., its offset into the file stream.
|
|
|
|
|
* FALSE is returned on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_ftell(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
ph7_int64 iOfft;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xTell == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
iOfft = pStream->xTell(pDev->pHandle);
|
|
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, iOfft);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool rewind(resource $handle)
|
|
|
|
|
* Rewind the position of a file pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_rewind(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xSeek == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xSeek(pDev->pHandle, 0, 0/*SEEK_SET*/);
|
|
|
|
|
if(rc == PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Ignore buffered data */
|
|
|
|
|
ResetIOPrivate(pDev);
|
|
|
|
|
}
|
|
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool fflush(resource $handle)
|
|
|
|
|
* Flushes the output to a file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Return
|
|
|
|
|
* TRUE on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fflush(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xSync == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
rc = pStream->xSync(pDev->pHandle);
|
|
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == PH7_OK);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* bool feof(resource $handle)
|
|
|
|
|
* Tests for end-of-file on a file pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns TRUE if the file pointer is at EOF.FALSE otherwise
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_feof(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Missing/Invalid arguments */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 1);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
rc = SXERR_EOF;
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(SyBlobLength(&pDev->sBuffer) - pDev->nOfft > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Data is available */
|
|
|
|
|
rc = PH7_OK;
|
2018-07-12 17:24:46 +02:00
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
char zBuf[4096];
|
|
|
|
|
ph7_int64 n;
|
|
|
|
|
/* Perform a buffered read */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = pStream->xRead(pDev->pHandle, zBuf, sizeof(zBuf));
|
|
|
|
|
if(n > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Copy buffered data */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyBlobAppend(&pDev->sBuffer, zBuf, (sxu32)n);
|
2018-07-12 13:26:32 +02:00
|
|
|
rc = PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* EOF or not */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, rc == SXERR_EOF);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Read n bytes from the underlying IO stream device.
|
2018-07-12 17:24:46 +02:00
|
|
|
* Return total numbers of bytes readen on success. A number < 1 on failure
|
2018-07-12 13:26:32 +02:00
|
|
|
* [i.e: IO error ] or EOF.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static ph7_int64 StreamRead(io_private *pDev, void *pBuf, ph7_int64 nLen) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream = pDev->pStream;
|
|
|
|
|
char *zBuf = (char *)pBuf;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nRead;
|
2018-07-12 13:26:32 +02:00
|
|
|
n = SyBlobLength(&pDev->sBuffer) - pDev->nOfft;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(n > 0) {
|
|
|
|
|
if(n > nLen) {
|
2018-07-12 13:26:32 +02:00
|
|
|
n = nLen;
|
|
|
|
|
}
|
|
|
|
|
/* Copy the buffered data */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyMemcpy(SyBlobDataAt(&pDev->sBuffer, pDev->nOfft), pBuf, (sxu32)n);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Update the read offset */
|
|
|
|
|
pDev->nOfft += (sxu32)n;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pDev->nOfft >= SyBlobLength(&pDev->sBuffer)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Reset the working buffer so that we avoid excessive memory allocation */
|
|
|
|
|
SyBlobReset(&pDev->sBuffer);
|
|
|
|
|
pDev->nOfft = 0;
|
|
|
|
|
}
|
|
|
|
|
nLen -= n;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nLen < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* All done */
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
/* Advance the cursor */
|
|
|
|
|
zBuf += n;
|
|
|
|
|
}
|
|
|
|
|
/* Read without buffering */
|
2018-07-12 17:24:46 +02:00
|
|
|
nRead = pStream->xRead(pDev->pHandle, zBuf, nLen);
|
|
|
|
|
if(nRead > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
n += nRead;
|
2018-07-12 17:24:46 +02:00
|
|
|
} else if(n < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* EOF or IO error */
|
|
|
|
|
return nRead;
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Extract a single line from the buffered input.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static sxi32 GetLine(io_private *pDev, ph7_int64 *pLen, const char **pzLine) {
|
|
|
|
|
const char *zIn, *zEnd, *zPtr;
|
|
|
|
|
zIn = (const char *)SyBlobDataAt(&pDev->sBuffer, pDev->nOfft);
|
|
|
|
|
zEnd = &zIn[SyBlobLength(&pDev->sBuffer) - pDev->nOfft];
|
2018-07-12 13:26:32 +02:00
|
|
|
zPtr = zIn;
|
2018-07-12 17:24:46 +02:00
|
|
|
while(zIn < zEnd) {
|
|
|
|
|
if(zIn[0] == '\n') {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Line found */
|
|
|
|
|
zIn++; /* Include the line ending as requested by the PHP specification */
|
2018-07-12 17:24:46 +02:00
|
|
|
*pLen = (ph7_int64)(zIn - zPtr);
|
2018-07-12 13:26:32 +02:00
|
|
|
*pzLine = zPtr;
|
|
|
|
|
return SXRET_OK;
|
|
|
|
|
}
|
|
|
|
|
zIn++;
|
|
|
|
|
}
|
|
|
|
|
/* No line were found */
|
|
|
|
|
return SXERR_NOTFOUND;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Read a single line from the underlying IO stream device.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static ph7_int64 StreamReadLine(io_private *pDev, const char **pzData, ph7_int64 nMaxLen) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream = pDev->pStream;
|
|
|
|
|
char zBuf[8192];
|
|
|
|
|
ph7_int64 n;
|
|
|
|
|
sxi32 rc;
|
|
|
|
|
n = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pDev->nOfft >= SyBlobLength(&pDev->sBuffer)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Reset the working buffer so that we avoid excessive memory allocation */
|
|
|
|
|
SyBlobReset(&pDev->sBuffer);
|
|
|
|
|
pDev->nOfft = 0;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(SyBlobLength(&pDev->sBuffer) - pDev->nOfft > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Check if there is a line */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = GetLine(pDev, &n, pzData);
|
|
|
|
|
if(rc == SXRET_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Got line,update the cursor */
|
|
|
|
|
pDev->nOfft += (sxu32)n;
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Perform the read operation until a new line is extracted or length
|
|
|
|
|
* limit is reached.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
for(;;) {
|
2019-06-22 12:34:49 +02:00
|
|
|
n = pStream->xRead(pDev->pHandle, zBuf, (nMaxLen > 0 && nMaxLen < (ph7_int64) sizeof(zBuf)) ? nMaxLen : (ph7_int64) sizeof(zBuf));
|
2018-07-12 17:24:46 +02:00
|
|
|
if(n < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* EOF or IO error */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Append the data just read */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyBlobAppend(&pDev->sBuffer, zBuf, (sxu32)n);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Try to extract a line */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = GetLine(pDev, &n, pzData);
|
|
|
|
|
if(rc == SXRET_OK) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Got one, return immediately */
|
2018-07-12 13:26:32 +02:00
|
|
|
pDev->nOfft += (sxu32)n;
|
|
|
|
|
return n;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nMaxLen > 0 && (SyBlobLength(&pDev->sBuffer) - pDev->nOfft >= nMaxLen)) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Read limit reached, return the available data */
|
2018-07-12 17:24:46 +02:00
|
|
|
*pzData = (const char *)SyBlobDataAt(&pDev->sBuffer, pDev->nOfft);
|
2018-07-12 13:26:32 +02:00
|
|
|
n = SyBlobLength(&pDev->sBuffer) - pDev->nOfft;
|
|
|
|
|
/* Reset the working buffer */
|
|
|
|
|
SyBlobReset(&pDev->sBuffer);
|
|
|
|
|
pDev->nOfft = 0;
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(SyBlobLength(&pDev->sBuffer) - pDev->nOfft > 0) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Read limit reached, return the available data */
|
2018-07-12 17:24:46 +02:00
|
|
|
*pzData = (const char *)SyBlobDataAt(&pDev->sBuffer, pDev->nOfft);
|
2018-07-12 13:26:32 +02:00
|
|
|
n = SyBlobLength(&pDev->sBuffer) - pDev->nOfft;
|
|
|
|
|
/* Reset the working buffer */
|
|
|
|
|
SyBlobReset(&pDev->sBuffer);
|
|
|
|
|
pDev->nOfft = 0;
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Open an IO stream handle.
|
|
|
|
|
* Notes on stream:
|
|
|
|
|
* According to the PHP reference manual.
|
|
|
|
|
* In its simplest definition, a stream is a resource object which exhibits streamable behavior.
|
2018-07-12 17:24:46 +02:00
|
|
|
* That is, it can be read from or written to in a linear fashion, and may be able to fseek()
|
2018-07-12 13:26:32 +02:00
|
|
|
* to an arbitrary locations within the stream.
|
|
|
|
|
* A wrapper is additional code which tells the stream how to handle specific protocols/encodings.
|
|
|
|
|
* For example, the http wrapper knows how to translate a URL into an HTTP/1.0 request for a file
|
|
|
|
|
* on a remote server.
|
|
|
|
|
* A stream is referenced as: scheme://target
|
|
|
|
|
* scheme(string) - The name of the wrapper to be used. Examples include: file, http...
|
|
|
|
|
* If no wrapper is specified, the function default is used (typically file://).
|
|
|
|
|
* target - Depends on the wrapper used. For filesystem related streams this is typically a path
|
|
|
|
|
* and filename of the desired file. For network related streams this is typically a hostname, often
|
2018-07-12 17:24:46 +02:00
|
|
|
* with a path appended.
|
2018-07-12 13:26:32 +02:00
|
|
|
*
|
|
|
|
|
* Note that PH7 IO streams looks like PHP streams but their implementation differ greately.
|
|
|
|
|
* Please refer to the official documentation for a full discussion.
|
|
|
|
|
* This function return a handle on success. Otherwise null.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_PRIVATE void *PH7_StreamOpenHandle(ph7_vm *pVm, const ph7_io_stream *pStream, const char *zFile,
|
|
|
|
|
int iFlags, int use_include, ph7_value *pResource, int bPushInclude, int *pNew) {
|
2018-07-12 13:26:32 +02:00
|
|
|
void *pHandle = 0; /* cc warning */
|
|
|
|
|
SyString sFile;
|
2019-06-25 20:10:27 +02:00
|
|
|
char sFilePath[PATH_MAX + 1];
|
2018-07-12 13:26:32 +02:00
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* No such stream device */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
SyStringInitFromBuf(&sFile, zFile, SyStrlen(zFile));
|
|
|
|
|
if(use_include) {
|
2018-07-24 10:29:16 +02:00
|
|
|
if(sFile.zString[0] == '/'
|
2018-07-12 13:26:32 +02:00
|
|
|
#ifdef __WINNT__
|
2018-08-02 02:45:23 +02:00
|
|
|
|| (sFile.nByte > 2 && sFile.zString[1] == ':' && (sFile.zString[2] == '\\' || sFile.zString[2] == '/'))
|
2018-07-12 13:26:32 +02:00
|
|
|
#endif
|
2018-07-24 10:29:16 +02:00
|
|
|
) {
|
2018-07-24 10:10:09 +02:00
|
|
|
/* Get real path to the included file */
|
|
|
|
|
SyRealPath(zFile, sFilePath);
|
|
|
|
|
/* Open the file directly */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xOpen(zFile, iFlags, pResource, &pHandle);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
SyString *pPath;
|
|
|
|
|
SyBlob sWorker;
|
|
|
|
|
#ifdef __WINNT__
|
|
|
|
|
static const int c = '\\';
|
|
|
|
|
#else
|
|
|
|
|
static const int c = '/';
|
|
|
|
|
#endif
|
|
|
|
|
/* Build a path from the set of include path */
|
|
|
|
|
SySetResetCursor(&pVm->aPaths);
|
|
|
|
|
rc = SXERR_IO;
|
2018-07-12 17:24:46 +02:00
|
|
|
while(SXRET_OK == SySetGetNextEntry(&pVm->aPaths, (void **)&pPath)) {
|
2018-07-23 19:43:04 +02:00
|
|
|
/* Init the path builder working buffer everytime to avoid trash */
|
|
|
|
|
SyBlobInit(&sWorker, &pVm->sAllocator);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Build full path */
|
2018-07-12 17:24:46 +02:00
|
|
|
SyBlobFormat(&sWorker, "%z%c%z", pPath, c, &sFile);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Append null terminator */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(SXRET_OK != SyBlobNullAppend(&sWorker)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* Try to open the file */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xOpen((const char *)SyBlobData(&sWorker), iFlags, pResource, &pHandle);
|
|
|
|
|
if(rc == PH7_OK) {
|
2018-07-24 10:10:09 +02:00
|
|
|
/* Get real path to the included file */
|
|
|
|
|
SyRealPath((const char *)SyBlobData(&sWorker), sFilePath);
|
2018-07-12 13:26:32 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Reset the working buffer */
|
|
|
|
|
SyBlobReset(&sWorker);
|
|
|
|
|
/* Check the next path */
|
|
|
|
|
}
|
|
|
|
|
SyBlobRelease(&sWorker);
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc == PH7_OK) {
|
|
|
|
|
if(bPushInclude) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Mark as included */
|
2018-07-24 10:10:09 +02:00
|
|
|
PH7_VmPushFilePath(pVm, sFilePath, -1, FALSE, pNew);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
} else {
|
2018-07-23 19:51:15 +02:00
|
|
|
/* Open the URI directly */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xOpen(zFile, iFlags, pResource, &pHandle);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(rc != PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO error */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* Return the file handle */
|
|
|
|
|
return pHandle;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Read the whole contents of an open IO stream handle [i.e local file/URL..]
|
|
|
|
|
* Store the read data in the given BLOB (last argument).
|
|
|
|
|
* The read operation is stopped when he hit the EOF or an IO error occurs.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_PRIVATE sxi32 PH7_StreamReadWholeFile(void *pHandle, const ph7_io_stream *pStream, SyBlob *pOut) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_int64 nRead;
|
|
|
|
|
char zBuf[8192]; /* 8K */
|
|
|
|
|
int rc;
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
for(;;) {
|
|
|
|
|
nRead = pStream->xRead(pHandle, zBuf, sizeof(zBuf));
|
|
|
|
|
if(nRead < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* EOF or IO error */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Append contents */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = SyBlobAppend(pOut, zBuf, (sxu32)nRead);
|
|
|
|
|
if(rc != SXRET_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return SyBlobLength(pOut) > 0 ? SXRET_OK : -1;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Close an open IO stream handle [i.e local file/URI..].
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_PRIVATE void PH7_StreamCloseHandle(const ph7_io_stream *pStream, void *pHandle) {
|
|
|
|
|
if(pStream->xClose) {
|
2018-07-12 13:26:32 +02:00
|
|
|
pStream->xClose(pHandle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string fgetc(resource $handle)
|
|
|
|
|
* Gets a character from the given file pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* Return
|
|
|
|
|
* Returns a string containing a single character read from the file
|
2018-07-12 17:24:46 +02:00
|
|
|
* pointed to by handle. Returns FALSE on EOF.
|
2018-07-12 13:26:32 +02:00
|
|
|
* WARNING
|
|
|
|
|
* This operation is extremely slow.Avoid using it.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fgetc(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
int c, n;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = (int)StreamRead(pDev, (void *)&c, sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
/* IO result */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(n < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* EOF or error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the string holding the character */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, (const char *)&c, sizeof(char));
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string fgets(resource $handle[,int64 $length ])
|
|
|
|
|
* Gets line from file pointer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* $length
|
|
|
|
|
* Reading ends when length - 1 bytes have been read, on a newline
|
|
|
|
|
* (which is included in the return value), or on EOF (whichever comes first).
|
|
|
|
|
* If no length is specified, it will keep reading from the stream until it reaches
|
2018-07-12 17:24:46 +02:00
|
|
|
* the end of the line.
|
2018-07-12 13:26:32 +02:00
|
|
|
* Return
|
|
|
|
|
* Returns a string of up to length - 1 bytes read from the file pointed to by handle.
|
|
|
|
|
* If there is no more data to read in the file pointer, then FALSE is returned.
|
|
|
|
|
* If an error occurs, FALSE is returned.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fgets(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
const char *zLine;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nLen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
nLen = -1;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Maximum data to read */
|
|
|
|
|
nLen = ph7_value_to_int64(apArg[1]);
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = StreamReadLine(pDev, &zLine, nLen);
|
|
|
|
|
if(n < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* EOF or IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the freshly extracted line */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, zLine, (int)n);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string fread(resource $handle,int64 $length)
|
|
|
|
|
* Binary-safe file read.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* $length
|
|
|
|
|
* Up to length number of bytes read.
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* The data readen on success or FALSE on failure.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fread(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
ph7_int64 nRead;
|
|
|
|
|
void *pBuf;
|
|
|
|
|
int nLen;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
nLen = 4096;
|
|
|
|
|
if(nArg > 1) {
|
|
|
|
|
nLen = ph7_value_to_int(apArg[1]);
|
|
|
|
|
if(nLen < 1) {
|
|
|
|
|
/* Invalid length,set a default length */
|
|
|
|
|
nLen = 4096;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Allocate enough buffer */
|
2018-07-12 17:24:46 +02:00
|
|
|
pBuf = ph7_context_alloc_chunk(pCtx, (unsigned int)nLen, FALSE, FALSE);
|
|
|
|
|
if(pBuf == 0) {
|
2018-09-04 08:26:58 +02:00
|
|
|
PH7_VmMemoryError(pCtx->pVm);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
nRead = StreamRead(pDev, pBuf, (ph7_int64)nLen);
|
|
|
|
|
if(nRead < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Nothing read, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Make a copy of the data just read */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, (const char *)pBuf, (int)nRead);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Release the buffer */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_context_free_chunk(pCtx, pBuf);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
2018-07-12 17:24:46 +02:00
|
|
|
* array fgetcsv(resource $handle [, int $length = 0
|
2018-07-12 13:26:32 +02:00
|
|
|
* [,string $delimiter = ','[,string $enclosure = '"'[,string $escape='\\']]]])
|
|
|
|
|
* Gets line from file pointer and parse for CSV fields.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* $length
|
|
|
|
|
* Reading ends when length - 1 bytes have been read, on a newline
|
|
|
|
|
* (which is included in the return value), or on EOF (whichever comes first).
|
|
|
|
|
* If no length is specified, it will keep reading from the stream until it reaches
|
2018-07-12 17:24:46 +02:00
|
|
|
* the end of the line.
|
2018-07-12 13:26:32 +02:00
|
|
|
* $delimiter
|
|
|
|
|
* Set the field delimiter (one character only).
|
|
|
|
|
* $enclosure
|
|
|
|
|
* Set the field enclosure character (one character only).
|
|
|
|
|
* $escape
|
|
|
|
|
* Set the escape character (one character only). Defaults as a backslash (\)
|
|
|
|
|
* Return
|
|
|
|
|
* Returns a string of up to length - 1 bytes read from the file pointed to by handle.
|
|
|
|
|
* If there is no more data to read in the file pointer, then FALSE is returned.
|
|
|
|
|
* If an error occurs, FALSE is returned.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fgetcsv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
const char *zLine;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nLen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
nLen = -1;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Maximum data to read */
|
|
|
|
|
nLen = ph7_value_to_int64(apArg[1]);
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = StreamReadLine(pDev, &zLine, nLen);
|
|
|
|
|
if(n < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* EOF or IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_value *pArray;
|
|
|
|
|
int delim = ','; /* Delimiter */
|
|
|
|
|
int encl = '"' ; /* Enclosure */
|
|
|
|
|
int escape = '\\'; /* Escape character */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zPtr;
|
|
|
|
|
int i;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(ph7_value_is_string(apArg[2])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the delimiter */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPtr = ph7_value_to_string(apArg[2], &i);
|
|
|
|
|
if(i > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
delim = zPtr[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 3) {
|
|
|
|
|
if(ph7_value_is_string(apArg[3])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the enclosure */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPtr = ph7_value_to_string(apArg[3], &i);
|
|
|
|
|
if(i > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
encl = zPtr[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 4) {
|
|
|
|
|
if(ph7_value_is_string(apArg[4])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the escape character */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPtr = ph7_value_to_string(apArg[4], &i);
|
|
|
|
|
if(i > 0) {
|
2018-07-12 13:26:32 +02:00
|
|
|
escape = zPtr[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Create our array */
|
|
|
|
|
pArray = ph7_context_new_array(pCtx);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pArray == 0) {
|
2018-09-04 08:26:58 +02:00
|
|
|
PH7_VmMemoryError(pCtx->pVm);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Parse the raw input */
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_ProcessCsv(zLine, (int)n, delim, encl, escape, PH7_CsvConsumer, pArray);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the freshly created array */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_value(pCtx, pArray);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string fgetss(resource $handle [,int $length [,string $allowable_tags ]])
|
|
|
|
|
* Gets line from file pointer and strip HTML tags.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $handle
|
|
|
|
|
* The file pointer.
|
|
|
|
|
* $length
|
|
|
|
|
* Reading ends when length - 1 bytes have been read, on a newline
|
|
|
|
|
* (which is included in the return value), or on EOF (whichever comes first).
|
|
|
|
|
* If no length is specified, it will keep reading from the stream until it reaches
|
2018-07-12 17:24:46 +02:00
|
|
|
* the end of the line.
|
2018-07-12 13:26:32 +02:00
|
|
|
* $allowable_tags
|
2018-07-12 17:24:46 +02:00
|
|
|
* You can use the optional second parameter to specify tags which should not be stripped.
|
2018-07-12 13:26:32 +02:00
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* Returns a string of up to length - 1 bytes read from the file pointed to by
|
|
|
|
|
* handle, with all HTML and PHP code stripped. If an error occurs, returns FALSE.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_fgetss(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
const char *zLine;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nLen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
nLen = -1;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Maximum data to read */
|
|
|
|
|
nLen = ph7_value_to_int64(apArg[1]);
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = StreamReadLine(pDev, &zLine, nLen);
|
|
|
|
|
if(n < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* EOF or IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zTaglist = 0;
|
|
|
|
|
int nTaglen = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2 && ph7_value_is_string(apArg[2])) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Allowed tag */
|
2018-07-12 17:24:46 +02:00
|
|
|
zTaglist = ph7_value_to_string(apArg[2], &nTaglen);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Process data just read */
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_StripTagsFromString(pCtx, zLine, (int)n, zTaglist, nTaglen);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
return PH7_OK;
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* string readdir(resource $dir_handle)
|
|
|
|
|
* Read entry from directory handle.
|
|
|
|
|
* Parameter
|
|
|
|
|
* $dir_handle
|
|
|
|
|
* The directory handle resource previously opened with opendir().
|
|
|
|
|
* Return
|
|
|
|
|
* Returns the filename on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_readdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
|
|
|
|
int rc;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2019-04-05 07:54:47 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2019-04-05 07:54:47 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xReadDir == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
2019-04-05 07:54:47 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Perform the requested operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xReadDir(pDev->pHandle, pCtx);
|
|
|
|
|
if(rc != PH7_OK) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return FALSE */
|
2019-04-05 07:54:47 +02:00
|
|
|
ph7_result_string(pCtx, "", 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* void rewinddir(resource $dir_handle)
|
|
|
|
|
* Rewind directory handle.
|
|
|
|
|
* Parameter
|
|
|
|
|
* $dir_handle
|
|
|
|
|
* The directory handle resource previously opened with opendir().
|
|
|
|
|
* Return
|
|
|
|
|
* FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_rewinddir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xRewindDir == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
pStream->xRewindDir(pDev->pHandle);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 17:24:46 +02:00
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Forward declaration */
|
2018-07-12 17:24:46 +02:00
|
|
|
static void InitIOPrivate(ph7_vm *pVm, const ph7_io_stream *pStream, io_private *pOut);
|
|
|
|
|
static void ReleaseIOPrivate(ph7_context *pCtx, io_private *pDev);
|
2018-07-12 13:26:32 +02:00
|
|
|
/*
|
|
|
|
|
* void closedir(resource $dir_handle)
|
|
|
|
|
* Close directory handle.
|
|
|
|
|
* Parameter
|
|
|
|
|
* $dir_handle
|
|
|
|
|
* The directory handle resource previously opened with opendir().
|
|
|
|
|
* Return
|
|
|
|
|
* FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_closedir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_resource(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract our private data */
|
|
|
|
|
pDev = (io_private *)ph7_value_to_resource(apArg[0]);
|
|
|
|
|
/* Make sure we are dealing with a valid io_private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(IO_PRIVATE_INVALID(pDev)) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/*Expecting an IO handle */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Point to the target IO stream device */
|
|
|
|
|
pStream = pDev->pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream == 0 || pStream->xCloseDir == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-08-15 21:43:17 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_function_name(pCtx), pStream ? pStream->zName : "null_stream"
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
pStream->xCloseDir(pDev->pHandle);
|
|
|
|
|
/* Release the private stucture */
|
2018-07-12 17:24:46 +02:00
|
|
|
ReleaseIOPrivate(pCtx, pDev);
|
2018-07-12 13:26:32 +02:00
|
|
|
PH7_MemObjRelease(apArg[0]);
|
|
|
|
|
return PH7_OK;
|
2018-07-12 17:24:46 +02:00
|
|
|
}
|
2018-07-12 13:26:32 +02:00
|
|
|
/*
|
|
|
|
|
* resource opendir(string $path[,resource $context])
|
|
|
|
|
* Open directory handle.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $path
|
|
|
|
|
* The directory path that is to be opened.
|
|
|
|
|
* $context
|
|
|
|
|
* A context stream resource.
|
|
|
|
|
* Return
|
|
|
|
|
* A directory handle resource on success,or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_opendir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
const char *zPath;
|
|
|
|
|
io_private *pDev;
|
2018-07-12 17:24:46 +02:00
|
|
|
int iLen, rc;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting a directory path");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the target path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zPath = ph7_value_to_string(apArg[0], &iLen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Try to extract a stream */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream = PH7_VmGetStreamDevice(pCtx->pVm, &zPath, iLen);
|
|
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"No stream device is associated with the given path(%s)", zPath);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream->xOpenDir == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
2018-07-12 17:24:46 +02:00
|
|
|
"IO routine(%s) not implemented in the underlying stream(%s) device",
|
|
|
|
|
ph7_function_name(pCtx), pStream->zName
|
|
|
|
|
);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Allocate a new IO private instance */
|
2018-07-12 17:24:46 +02:00
|
|
|
pDev = (io_private *)ph7_context_alloc_chunk(pCtx, sizeof(io_private), TRUE, FALSE);
|
|
|
|
|
if(pDev == 0) {
|
2018-09-04 08:26:58 +02:00
|
|
|
PH7_VmMemoryError(pCtx->pVm);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Initialize the structure */
|
2018-07-12 17:24:46 +02:00
|
|
|
InitIOPrivate(pCtx->pVm, pStream, pDev);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Open the target directory */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = pStream->xOpenDir(zPath, nArg > 1 ? apArg[1] : 0, &pDev->pHandle);
|
|
|
|
|
if(rc != PH7_OK) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO error, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ReleaseIOPrivate(pCtx, pDev);
|
|
|
|
|
ph7_result_bool(pCtx, 0);
|
|
|
|
|
} else {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Return the handle as a resource */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_resource(pCtx, pDev);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int readfile(string $filename[,bool $use_include_path = false [,resource $context ]])
|
|
|
|
|
* Reads a file and writes it to the output buffer.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* The filename being read.
|
|
|
|
|
* $use_include_path
|
|
|
|
|
* You can use the optional second parameter and set it to
|
|
|
|
|
* TRUE, if you want to search for the file in the include_path, too.
|
|
|
|
|
* $context
|
|
|
|
|
* A context stream resource.
|
|
|
|
|
* Return
|
|
|
|
|
* The number of bytes read from the file on success or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_readfile(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
int use_include = FALSE;
|
|
|
|
|
const ph7_io_stream *pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nRead;
|
2018-07-12 13:26:32 +02:00
|
|
|
const char *zFile;
|
|
|
|
|
char zBuf[8192];
|
|
|
|
|
void *pHandle;
|
2018-07-12 17:24:46 +02:00
|
|
|
int rc, nLen;
|
|
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting a file path");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the file path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zFile = ph7_value_to_string(apArg[0], &nLen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Point to the target IO stream device */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream = PH7_VmGetStreamDevice(pCtx->pVm, &zFile, nLen);
|
|
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "No such stream device,PH7 is returning FALSE");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
use_include = ph7_value_to_bool(apArg[1]);
|
|
|
|
|
}
|
|
|
|
|
/* Try to open the file in read-only mode */
|
2018-07-12 17:24:46 +02:00
|
|
|
pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, PH7_IO_OPEN_RDONLY,
|
|
|
|
|
use_include, nArg > 2 ? apArg[2] : 0, FALSE, 0);
|
|
|
|
|
if(pHandle == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR, "IO error while opening '%s'", zFile);
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nRead = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
for(;;) {
|
|
|
|
|
n = pStream->xRead(pHandle, zBuf, sizeof(zBuf));
|
|
|
|
|
if(n < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* EOF or IO error,break immediately */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Output data */
|
2018-07-12 17:24:46 +02:00
|
|
|
rc = ph7_context_output(pCtx, zBuf, (int)n);
|
|
|
|
|
if(rc == PH7_ABORT) {
|
2018-07-12 13:26:32 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Increment counter */
|
|
|
|
|
nRead += n;
|
|
|
|
|
}
|
|
|
|
|
/* Close the stream */
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_StreamCloseHandle(pStream, pHandle);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Total number of bytes readen */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_int64(pCtx, nRead);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
2018-07-12 17:24:46 +02:00
|
|
|
* string file_get_contents(string $filename[,bool $use_include_path = false
|
2018-07-12 13:26:32 +02:00
|
|
|
* [, resource $context [, int $offset = -1 [, int $maxlen ]]]])
|
|
|
|
|
* Reads entire file into a string.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* The filename being read.
|
|
|
|
|
* $use_include_path
|
|
|
|
|
* You can use the optional second parameter and set it to
|
|
|
|
|
* TRUE, if you want to search for the file in the include_path, too.
|
|
|
|
|
* $context
|
|
|
|
|
* A context stream resource.
|
|
|
|
|
* $offset
|
|
|
|
|
* The offset where the reading starts on the original stream.
|
|
|
|
|
* $maxlen
|
2018-07-12 17:24:46 +02:00
|
|
|
* Maximum length of data read. The default is to read until end of file
|
2018-07-12 13:26:32 +02:00
|
|
|
* is reached. Note that this parameter is applied to the stream processed by the filters.
|
|
|
|
|
* Return
|
|
|
|
|
* The function returns the read data or FALSE on failure.
|
|
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_file_get_contents(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
const ph7_io_stream *pStream;
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_int64 n, nRead, nMaxlen;
|
2018-07-12 13:26:32 +02:00
|
|
|
int use_include = FALSE;
|
|
|
|
|
const char *zFile;
|
|
|
|
|
char zBuf[8192];
|
|
|
|
|
void *pHandle;
|
|
|
|
|
int nLen;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting a file path");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the file path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zFile = ph7_value_to_string(apArg[0], &nLen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Point to the target IO stream device */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream = PH7_VmGetStreamDevice(pCtx->pVm, &zFile, nLen);
|
|
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "No such stream device,PH7 is returning FALSE");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
nMaxlen = -1;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
use_include = ph7_value_to_bool(apArg[1]);
|
|
|
|
|
}
|
|
|
|
|
/* Try to open the file in read-only mode */
|
2018-07-12 17:24:46 +02:00
|
|
|
pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, PH7_IO_OPEN_RDONLY, use_include, nArg > 2 ? apArg[2] : 0, FALSE, 0);
|
|
|
|
|
if(pHandle == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR, "IO error while opening '%s'", zFile);
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 3) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the offset */
|
|
|
|
|
n = ph7_value_to_int64(apArg[3]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(n > 0) {
|
|
|
|
|
if(pStream->xSeek) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Seek to the desired offset */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream->xSeek(pHandle, n, 0/*SEEK_SET*/);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 4) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Maximum data to read */
|
|
|
|
|
nMaxlen = ph7_value_to_int64(apArg[4]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Perform the requested operation */
|
|
|
|
|
nRead = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
for(;;) {
|
|
|
|
|
n = pStream->xRead(pHandle, zBuf,
|
2019-06-22 12:34:49 +02:00
|
|
|
(nMaxlen > 0 && (nMaxlen < (ph7_int64) sizeof(zBuf))) ? nMaxlen : (ph7_int64) sizeof(zBuf));
|
2018-07-12 17:24:46 +02:00
|
|
|
if(n < 1) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* EOF or IO error,break immediately */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Append data */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_string(pCtx, zBuf, (int)n);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Increment read counter */
|
|
|
|
|
nRead += n;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nMaxlen > 0 && nRead >= nMaxlen) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Read limit reached */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Close the stream */
|
2018-07-12 17:24:46 +02:00
|
|
|
PH7_StreamCloseHandle(pStream, pHandle);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Check if we have read something */
|
2018-07-12 17:24:46 +02:00
|
|
|
if(ph7_context_result_buf_length(pCtx) < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Nothing read, return FALSE */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* int file_put_contents(string $filename,mixed $data[,int $flags = 0[,resource $context]])
|
|
|
|
|
* Write a string to a file.
|
|
|
|
|
* Parameters
|
|
|
|
|
* $filename
|
|
|
|
|
* Path to the file where to write the data.
|
|
|
|
|
* $data
|
|
|
|
|
* The data to write(Must be a string).
|
|
|
|
|
* $flags
|
|
|
|
|
* The value of flags can be any combination of the following
|
|
|
|
|
* flags, joined with the binary OR (|) operator.
|
|
|
|
|
* FILE_USE_INCLUDE_PATH Search for filename in the include directory. See include_path for more information.
|
|
|
|
|
* FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.
|
|
|
|
|
* LOCK_EX Acquire an exclusive lock on the file while proceeding to the writing.
|
|
|
|
|
* context
|
|
|
|
|
* A context stream resource.
|
|
|
|
|
* Return
|
2018-07-12 17:24:46 +02:00
|
|
|
* The function returns the number of bytes that were written to the file, or FALSE on failure.
|
2018-07-12 13:26:32 +02:00
|
|
|
*/
|
2018-07-12 17:24:46 +02:00
|
|
|
static int PH7_builtin_file_put_contents(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
2018-07-12 13:26:32 +02:00
|
|
|
int use_include = FALSE;
|
|
|
|
|
const ph7_io_stream *pStream;
|
|
|
|
|
const char *zFile;
|
|
|
|
|
const char *zData;
|
|
|
|
|
int iOpenFlags;
|
|
|
|
|
void *pHandle;
|
|
|
|
|
int iFlags;
|
|
|
|
|
int nLen;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg < 2 || !ph7_value_is_string(apArg[0])) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* Missing/Invalid arguments, return FALSE */
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting a file path");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Extract the file path */
|
2018-07-12 17:24:46 +02:00
|
|
|
zFile = ph7_value_to_string(apArg[0], &nLen);
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Point to the target IO stream device */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream = PH7_VmGetStreamDevice(pCtx->pVm, &zFile, nLen);
|
|
|
|
|
if(pStream == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "No such stream device,PH7 is returning FALSE");
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Data to write */
|
2018-07-12 17:24:46 +02:00
|
|
|
zData = ph7_value_to_string(apArg[1], &nLen);
|
|
|
|
|
if(nLen < 1) {
|
2019-11-18 08:11:13 +01:00
|
|
|
/* Nothing to write, return immediately */
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
|
|
|
|
/* Try to open the file in read-write mode */
|
2018-07-12 17:24:46 +02:00
|
|
|
iOpenFlags = PH7_IO_OPEN_CREATE | PH7_IO_OPEN_RDWR | PH7_IO_OPEN_TRUNC;
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Extract the flags */
|
|
|
|
|
iFlags = 0;
|
2018-07-12 17:24:46 +02:00
|
|
|
if(nArg > 2) {
|
2018-07-12 13:26:32 +02:00
|
|
|
iFlags = ph7_value_to_int(apArg[2]);
|
2018-07-12 17:24:46 +02:00
|
|
|
if(iFlags & 0x01 /*FILE_USE_INCLUDE_PATH*/) {
|
2018-07-12 13:26:32 +02:00
|
|
|
use_include = TRUE;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(iFlags & 0x08 /* FILE_APPEND */) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* If the file already exists, append the data to the file
|
|
|
|
|
* instead of overwriting it.
|
|
|
|
|
*/
|
|
|
|
|
iOpenFlags &= ~PH7_IO_OPEN_TRUNC;
|
|
|
|
|
/* Append mode */
|
|
|
|
|
iOpenFlags |= PH7_IO_OPEN_APPEND;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, iOpenFlags, use_include,
|
|
|
|
|
nArg > 3 ? apArg[3] : 0, FALSE, FALSE);
|
|
|
|
|
if(pHandle == 0) {
|
2018-09-04 08:54:48 +02:00
|
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_ERR, "IO error while opening '%s'", zFile);
|
2018-07-12 17:24:46 +02:00
|
|
|
ph7_result_bool(pCtx, 0);
|
2018-07-12 13:26:32 +02:00
|
|
|
return PH7_OK;
|
|
|
|
|
}
|
2018-07-12 17:24:46 +02:00
|
|
|
if(pStream->xWrite) {
|
2018-07-12 13:26:32 +02:00
|
|
|
ph7_int64 n;
|
2018-07-12 17:24:46 +02:00
|
|
|
if((iFlags & 0x01/* LOCK_EX */) && pStream->xLock) {
|
2018-07-12 13:26:32 +02:00
|
|
|
/* Try to acquire an exclusive lock */
|
2018-07-12 17:24:46 +02:00
|
|
|
pStream->xLock(pHandle, 1/* LOCK_EX */);
|
2018-07-12 13:26:32 +02:00
|
|
|
}
|
|
|
|
|
/* Perform the write operation */
|
2018-07-12 17:24:46 +02:00
|
|
|
n = pStream->xWrite(pHandle, (const void *)zData, nLen);
|
|
|
|
|
if(n < 1) {
|
2019-06-30 14:13:35 +02:00
|
|
|
/* IO error, return FALSE */
|
|
|