?? curpath.c
字號:
/*************************************************************
File Name: curpath.C
Last Modified Date: 2001/04/17
Programmer: MSC
Compiler:
Platform:
Usage:
Current path API set.
**************************************************************/
#include <sys/syscall.h>
#include <stdio.h>
#include <string.h>
//#include "../rDebug/include/rdebug.h"
#include "../include/filesys.h"
#ifdef FS_CURRENT_DIRECTORY_ENABLED
// current pathname
static unsigned char *CurrentPath[DEVICE_NUM] = { NULL, NULL, NULL, NULL };
// FileSys.c
extern int CurrentDrive;
/* marked by chilong 01/15/2002
extern unsigned char *Pathname1;
marked by chilong 01/15/2002 */
/**** added by chilong ****/
extern short FileSysDeviceTable[DEVICE_NUM];
/**** added by chilong ****/
/*************************************************************
Function: initCurrentPath
Description:
Initialize the current path.
Return value:
0: init succeeded
-1: init failed
**************************************************************/
int initCurrentPath(void)
{
int i;
unsigned char *pathBuf;
pathBuf = (unsigned char *)ap_malloc(DEVICE_NUM * (MAX_PATHNAME_LEN + 2));
if (pathBuf == NULL)
{
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
for (i = 0; i < DEVICE_NUM; i++)
CurrentPath[i] = (unsigned char *)((unsigned long)pathBuf + i * (MAX_PATHNAME_LEN + 2));
// the current path after initialization is "\\"
resetAllCurrentPath();
return 0;
}
/*************************************************************
Function: resetAllCurrentPath
Description:
Reset the current path.
Return value:
0: reset succeeded
-1: reset failed
**************************************************************/
int resetAllCurrentPath(void)
{
int i;
for (i = 0; i < DEVICE_NUM; i++)
{
if (CurrentPath[i] == NULL)
return -1;
CurrentPath[i][0] = '\\';
CurrentPath[i][1] = '\0';
}
return 0;
}
/*************************************************************
Function: resetCurrentPath
Description:
Reset the specified current path.
Input:
driveNO - the drive number (0 = A:, 1 = B:, ...)
Return value:
0: reset succeeded
-1: reset failed
**************************************************************/
int resetCurrentPath(int driveNO)
{
if (checkDrive(driveNO) == -1)
{
FileSys_Errno = ERROR_INVALID_DRIVE;
return -1;
}
if (CurrentPath[driveNO] == NULL)
return -1;
CurrentPath[driveNO][0] = '\\';
CurrentPath[driveNO][1] = '\0';
return 0;
}
/*************************************************************
Function: getCompletePath
Description:
Merge the given partial pathname with the current directory
to give a complete pathname
Input:
driveNO - the drive number (0 = A:, 1 = B:, ...)
partialName - the given pathname
Output:
completeName - the complete pathname
Return value:
0: merge succeeded
-1: merge failed
**************************************************************/
int getCompletePath(int driveNO, unsigned char *partialName, unsigned char *completeName)
{
int nameTail = 0;
int i = 0;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[getCompletePath] driveNO = %d, partialName = %s", driveNO, partialName);
SprintStringLn(FS_DebugString);
sprintf(FS_DebugString, " currentPath = %s", CurrentPath[driveNO]);
SprintStringLn(FS_DebugString);
#endif
if (checkDrive(driveNO) == -1)
{
FileSys_Errno = ERROR_INVALID_DRIVE;
return -1;
}
if (partialName[0] == '\\' || partialName[0] == '/')
{
// the given pathname is a complete pathname
do
{
completeName[nameTail] = partialName[nameTail];
nameTail++;
if (nameTail >= MAX_PATHNAME_LEN)
{
completeName[0] = '\0';
FileSys_Errno = ERROR_PATHNAME_TOO_LONG;
return -1;
}
} while (completeName[nameTail - 1] != '\0');
return 0;
}
// copy the current directory name to the complete pathname
while (CurrentPath[driveNO][nameTail] != '\0')
{
completeName[nameTail] = CurrentPath[driveNO][nameTail];
nameTail++;
if (nameTail >= MAX_PATHNAME_LEN)
{
// current directory name corrupted
// reset current directory to "\"
memoryZero((void *)CurrentPath[driveNO], MAX_PATHNAME_LEN);
CurrentPath[driveNO][0] = '\\';
CurrentPath[driveNO][1] = '\0';
// the complete pathname is also "\"
completeName[0] = '\0';
FileSys_Errno = ERROR_CURRENT_DIRECTORY_BAD;
return -1;
}
}
// if (completeName[nameTail - 1] == '\\' || completeName[nameTail - 1] == '/')
if (nameTail > 0 && completeName[nameTail - 1] == '\\' || completeName[nameTail - 1] == '/')
{
completeName[nameTail] = '\0';
}
else
{
completeName[nameTail++] = '\\';
completeName[nameTail] = '\0';
}
while (partialName[i] != '\0')
{
if (partialName[i] == '.')
{
i++;
if (partialName[i] == '.')
{
// name is ".."
i++;
if (partialName[i] == '\\' || partialName[i] == '/' || partialName[i] == '\0')
{
// name is "..\\" or "..\0"
while (partialName[i] == '\\' || partialName[i] == '/')
i++;
// the complete directory name loops back one directory level
if (nameTail > 1)
{
// go before the last '\\'
nameTail -= 2;
}
else
{
// the complete directory is already "\\", can't go back
completeName[0] = '\0';
FileSys_Errno = ERROR_INVALID_PATHNAME;
return -1;
}
/* marked by chilong 02/05/2002
while (completeName[nameTail] != '\\' && completeName[nameTail] != '/' && nameTail > 0)
{
nameTail--;
}
marked by chilong 02/05/2002 */
/**** modified by chilong 02/05/2002 ****/
while (1)
{
#ifdef CHINESE_CODE_USED
if ( (completeName[nameTail] <= 0x80 && completeName[nameTail] == '\\') ||
(completeName[nameTail] <= 0x80 && completeName[nameTail] == '/') ||
nameTail <= 0)
break;
#else
if (completeName[nameTail] == '\\' || completeName[nameTail] == '/' || nameTail <= 0)
break;
#endif
nameTail--;
}
/**** modified by chilong 02/05/2002 ****/
if (partialName[i] == '\0')
{
// completeName[nameTail] = '\0';
/**** modified by chilong ****/
if (nameTail != 0)
completeName[nameTail] = '\0';
else
completeName[1] = '\0';
/**** modified by chilong ****/
return 0;
}
else
{
nameTail++;
completeName[nameTail] = '\0';
}
}
else
{
// name is not "..\\" or "..\0"
completeName[0] = '\0';
FileSys_Errno = ERROR_INVALID_PATHNAME;
return -1;
}
}
else if (partialName[i] == '\\' || partialName[i] == '/')
{
// name is ".\\"
while (partialName[i] == '\\' || partialName[i] == '/')
i++;
}
else
{
// partial directory name incorrect
completeName[0] = '\0';
FileSys_Errno = ERROR_INVALID_PATHNAME;
return -1;
}
}
else
{
// append a level of directory from given pathname to complete pathname
while (partialName[i] != '\\' && partialName[i] != '/' && partialName[i] != '\0')
{
/* marked by chilong 02/05/2002
completeName[nameTail] = partialName[i];
nameTail++;
i++;
marked by chilong 02/05/2002 */
/**** modified by chilong 02/05/2002 ****/
#ifdef CHINESE_CODE_USED
if (partialName[i] > 0x80)
{
completeName[nameTail] = partialName[i];
completeName[nameTail+1] = partialName[i+1];
nameTail+=2;
i+=2;
}
else
{
completeName[nameTail] = partialName[i];
nameTail++;
i++;
}
#else
completeName[nameTail] = partialName[i];
nameTail++;
i++;
#endif
/**** modified by chilong 02/05/2002 ****/
if (nameTail >= MAX_PATHNAME_LEN)
{
// pathname too long
completeName[0] = '\0';
FileSys_Errno = ERROR_PATHNAME_TOO_LONG;
return -1;
}
}
if (partialName[i] == '\0')
{
completeName[nameTail] = '\0';
return 0;
}
else if (partialName[i] == '\\' || partialName[i] == '/')
{
completeName[nameTail] = '\\';
nameTail++;
i++;
}
else
{
completeName[0] = '\0';
FileSys_Errno = ERROR_INVALID_PATHNAME;
return -1;
}
}
}
completeName[nameTail] = '\0';
return 0;
}
/*************************************************************
Function: checkPath
Description:
check to see if the pathname is valid
Input:
driveNO - the drive number (0 = A:, 1 = B:, ...)
pathname - the complete pathname
Return value:
0: pathname valid
-1: pathname invalid
**************************************************************/
int checkPath(int driveNO, unsigned char *pathname)
{
struct _stat status;
int origDrive;
// int returnValue = 0;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[checkPath] driveNO = %d, pathname = %s", driveNO, pathname);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if (checkDrive(driveNO) == -1)
{
FileSys_Errno = ERROR_INVALID_DRIVE;
return -1;
}
origDrive = fs_getdrive();
// we change drive here but if this functions fails,
// we'll change CurrentDrive back to origDrive
CurrentDrive = driveNO;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " original driveNO = %d", origDrive - 1);
SprintStringLn(FS_DebugString);
#endif
/*
if (origDrive != driveNO + 1)
{
if (chdrive(driveNO + 1) == -1)
return -1;
}
*/
/* marked by chilong 01/15/2002
if (stat(pathname, &status) == -1)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -