#include "apr.h"
#include "apr_arch_file_io.h"
#include "apr_strings.h"
const char apr_c_is_fnchar[256] =
{
0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,2,1,3,3,3,3,3,3,2,1,1,1,1,0, 1,1,1,1,1,1,1,1,1,1,0,3,2,1,2,2,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
apr_status_t filepath_root_test(char *path, apr_pool_t *p)
{
apr_status_t rv;
#if APR_HAS_UNICODE_FS
if (apr_os_level >= APR_WIN_NT)
{
apr_wchar_t wpath[APR_PATH_MAX];
if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
/ sizeof(apr_wchar_t), path)))
return rv;
rv = GetDriveTypeW(wpath);
}
else
#endif
rv = GetDriveType(path);
if (rv == DRIVE_UNKNOWN || rv == DRIVE_NO_ROOT_DIR)
return APR_EBADPATH;
return APR_SUCCESS;
}
apr_status_t filepath_drive_get(char **rootpath, char drive,
apr_int32_t flags, apr_pool_t *p)
{
char path[APR_PATH_MAX];
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t *ignored;
apr_wchar_t wdrive[8];
apr_wchar_t wpath[APR_PATH_MAX];
apr_status_t rv;
wcscpy(wdrive, L"D:.");
wdrive[0] = (apr_wchar_t)(unsigned char)drive;
if (!GetFullPathNameW(wdrive, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
return apr_get_os_error();
if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
return rv;
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
char *ignored;
char drivestr[4];
drivestr[0] = drive;
drivestr[1] = ':';
drivestr[2] = '.';;
drivestr[3] = '\0';
if (!GetFullPathName(drivestr, sizeof(path), path, &ignored))
return apr_get_os_error();
}
#endif
if (!(flags & APR_FILEPATH_NATIVE)) {
for (*rootpath = path; **rootpath; ++*rootpath) {
if (**rootpath == '\\')
**rootpath = '/';
}
}
*rootpath = apr_pstrdup(p, path);
return APR_SUCCESS;
}
apr_status_t filepath_root_case(char **rootpath, char *root, apr_pool_t *p)
{
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t *ignored;
apr_wchar_t wpath[APR_PATH_MAX];
apr_status_t rv;
apr_wchar_t wroot[APR_PATH_MAX];
if ((rv = utf8_to_unicode_path(wroot, sizeof(wroot)
/ sizeof(apr_wchar_t), root)))
return rv;
if (!GetFullPathNameW(wroot, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
return apr_get_os_error();
if ((rv = unicode_to_utf8_path((char*)wroot, sizeof(wroot), wpath)))
return rv;
*rootpath = apr_pstrdup(p, (char*)wroot);
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
char path[APR_PATH_MAX];
char *ignored;
if (!GetFullPathName(root, sizeof(path), path, &ignored))
return apr_get_os_error();
*rootpath = apr_pstrdup(p, path);
}
#endif
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_filepath_get(char **rootpath, apr_int32_t flags,
apr_pool_t *p)
{
char path[APR_PATH_MAX];
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t wpath[APR_PATH_MAX];
apr_status_t rv;
if (!GetCurrentDirectoryW(sizeof(wpath) / sizeof(apr_wchar_t), wpath))
return apr_get_os_error();
if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
return rv;
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
if (!GetCurrentDirectory(sizeof(path), path))
return apr_get_os_error();
}
#endif
if (!(flags & APR_FILEPATH_NATIVE)) {
for (*rootpath = path; **rootpath; ++*rootpath) {
if (**rootpath == '\\')
**rootpath = '/';
}
}
*rootpath = apr_pstrdup(p, path);
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_filepath_set(const char *rootpath,
apr_pool_t *p)
{
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
apr_wchar_t wpath[APR_PATH_MAX];
apr_status_t rv;
if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
/ sizeof(apr_wchar_t), rootpath)))
return rv;
if (!SetCurrentDirectoryW(wpath))
return apr_get_os_error();
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
if (!SetCurrentDirectory(rootpath))
return apr_get_os_error();
}
#endif
return APR_SUCCESS;
}