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

📄 curpath.c

📁 嵌入式系统中文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  {
  	CurrentDrive = origDrive;
	return -1;
  }
  else
  {
	// pathname found, check if it is a directory
	if ((status.st_mode & S_IFDIR) != S_IFDIR)
	{
		// this is not a directory
		FileSys_Errno = ERROR_DIR_NOT_EXIST;
  		CurrentDrive = origDrive;
		return -1;
	}
  }
  
     marked by chilong 01/15/2002  */
     
  /**** modified by chilong 01/15/2002 ****/
  if (fs_stat((char *)pathname, &status) == -1)
  {
  	CurrentDrive = origDrive;
	return -1;
  }
  else
  {
	// pathname found, check if it is a directory
	if ((status.st_mode & S_IFDIR) != S_IFDIR)
	{
		// this is not a directory
		FileSys_Errno = ERROR_DIR_NOT_EXIST;
  		CurrentDrive = origDrive;
		return -1;
	}
  }
  /**** modified by chilong 01/15/2002 ****/

/*
  if (origDrive != driveNO + 1)
	chdrive(origDrive);
*/
  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    return value = %d", returnValue);
	SprintStringLn(FS_DebugString);
  #endif

  return 0;
}



/*************************************************************
Function: chdir
Description:
	change current directory
Input:
	dirname - the new current directory
Return value:
	 0: change succeeded
	-1: change failed
**************************************************************/
int fs_chdir(char *dirName)
{
  int returnValue;

  /**** added by chilong 12/7/2001 ****/
  if (dirName == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/
  
  if (dirName[0] == '\0')
	return 0;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  FileSys_Errno = 0;

  returnValue = chdir_r((unsigned char *)dirName);

  return returnValue;
}

int chdir_r(unsigned char *dirname)
{
  int driveNO;

  /**** added by chilong 01/15/2002 ****/
  unsigned char *Pathname1;
  
  if ((Pathname1 = (unsigned char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }
  
  /**** added by chilong 01/15/2002 ****/

  // dirname may be a partial pathname, so get the complete pathname
  driveNO = getDriveNO((char *)dirname, (char *)Pathname1);
  if (driveNO == -1)
  {
	/**** added by chilong 01/15/2002 ****/
	ap_free(Pathname1);
	/**** added by chilong 01/15/2002 ****/
	
	// FileSys_Errno set in getDriveNO()
	return -1;
  }

  /**** added by chilong 12/4/2001 ****/
  // this part will be deleted after directory
  // has been supported in NAND or NOR FLASH
  #ifdef NAND_FLASH_DISK_ID
	if (driveNO == NAND_FLASH_DISK_ID)
	{
		/**** added by chilong 01/15/2002 ****/
		ap_free(Pathname1);
		/**** added by chilong 01/15/2002 ****/
		return -1;
	}
  #endif
  #ifdef NOR_FLASH_DISK_ID
	if (driveNO == NOR_FLASH_DISK_ID)
	{
		/**** added by chilong 01/15/2002 ****/
		ap_free(Pathname1);
		/**** added by chilong 01/15/2002 ****/
		return -1;
	}
  #endif
  /**** added by chilong 12/4/2001 ****/

  // check if the pathname is valid
  if (checkPath(driveNO, Pathname1) == -1)
  {
	FileSys_Errno = ERROR_INVALID_PATHNAME;
	
	/**** added by chilong 01/15/2002 ****/
	ap_free(Pathname1);
	/**** added by chilong 01/15/2002 ****/
	
	return -1;
  }

  // pathname is valid, replace the current path
  strcpy((char *)CurrentPath[driveNO], (char *)Pathname1);

  /**** added by chilong 01/15/2002 ****/
  ap_free(Pathname1);
  /**** added by chilong 01/15/2002 ****/

  return 0;
}



/*************************************************************
Function: getcwd
Description:
	get the current working directory
Input:
	buffer - the storage location for the pathname
	maxlen - the maximum length of the pathname
Return value:
	the pointer to the pathname if succeeded
	NULL if failed
Note:
	If [buffer] is not NULL, this function assumes that
	the user has already allocated a buffer and the string
	of the current working directory will be filled in the
	buffer pointed by [buffer].

	If [buffer] is NULL, a buffer will be automatically
	allocated with the size specified by [maxlen]. The
	pointer to this buffer will be the returned value of
	this function. User is responsible to free this
	buffer whenever he is done with this string.

	There is no '\\' or '/' at the end of the string.
**************************************************************/
char *fs_getcwd(char *buffer, int maxlen)
{
  char *returnValue;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return NULL;
  }
  
//  if (buffer == NULL)
  //	return NULL;

  FileSys_Errno = 0;

  returnValue = (char *)getcwd_r((unsigned char *)buffer, maxlen);

  return returnValue;
}

unsigned char *getcwd_r(unsigned char *buffer, int maxlen)
{
  int i = 0;
  int hasAlloc = 0;

  if (buffer == NULL)
  {
	hasAlloc = 1;

	buffer = (unsigned char *)ap_malloc(maxlen);
	if (buffer == NULL)
	{
		FileSys_Errno = ERROR_ALLOC_MEM;
		return NULL;
	}
  }

  while (CurrentPath[CurrentDrive][i] != '\0' && i < (MAX_PATHNAME_LEN - 1) && i < (maxlen - 1))
  {
	buffer[i] = CurrentPath[CurrentDrive][i];
	i++;
  }

  buffer[i] = '\0';

  if (CurrentPath[CurrentDrive][i] != '\0')
  {
	// pathname not complete
	if (hasAlloc == 1)
		ap_free(buffer);

	if ((i + 1) >= MAX_PATHNAME_LEN)
	{
		FileSys_Errno = ERROR_PATHNAME_TOO_LONG;
		return NULL;
	}
	else if ((i + 1) >= maxlen)
	{
		FileSys_Errno = ERROR_MAXLEN_NOT_LONG_ENOUGH;
		return NULL;
	}
  }

  return buffer;
}



/*************************************************************
Function: getdcwd
Description:
	get the current working directory of the specified drive
Input:
	driveNO - the drive number (0 = current drive, 1 = A:, 2 = B:, ...)
	buffer - the storage location for the pathname
	maxlen - the maximum length of the pathname
Return value:
	the pointer to the pathname if succeeded
	NULL if failed
Note:
	If [buffer] is not NULL, this function assumes that
	the user has already allocated a buffer and the string
	of the current working directory will be filled in the
	buffer pointed by [buffer].

	If [buffer] is NULL, a buffer will be automatically
	allocated with the size specified by [maxlen]. The
	pointer to this buffer will be the returned value of
	this function. User is responsible to free this
	buffer whenever he is done with this string.

	There is no '\\' or '/' at the end of the string.
**************************************************************/
char *fs_getdcwd(int driveNO, char *buffer, int maxlen)
{
  char *returnValue;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return NULL;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (driveNO < 0 || buffer == NULL)
  	return NULL;  
  /**** added by chilong 12/7/2001 ****/

  FileSys_Errno = 0;

  returnValue = (char *)getdcwd_r(driveNO, (unsigned char *)buffer, maxlen);

  return returnValue;
}

unsigned char *getdcwd_r(int driveNO, unsigned char *buffer, int maxlen)
{
  int i = 0;
  int hasAlloc = 0;

  if (driveNO == 0)
	driveNO = CurrentDrive;
  else
	driveNO--;

  if (checkDrive(driveNO) == -1)
  {
	FileSys_Errno = ERROR_INVALID_DRIVE;
	return NULL;
  }

  if (buffer == NULL)
  {
	hasAlloc = 1;

	buffer = (unsigned char *)ap_malloc(maxlen);
	if (buffer == NULL)
	{
		FileSys_Errno = ERROR_ALLOC_MEM;
		return NULL;
	}
  }

  while (CurrentPath[driveNO][i] != '\0' && i < (MAX_PATHNAME_LEN - 1) && i < (maxlen - 1))
  {
	buffer[i] = CurrentPath[driveNO][i];
	i++;
  }

  buffer[i] = '\0';

  if (CurrentPath[driveNO][i] != '\0')
  {
	// pathname not complete
	if (hasAlloc == 1)
		ap_free(buffer);

	if ((i + 1) >= MAX_PATHNAME_LEN)
	{
		FileSys_Errno = ERROR_PATHNAME_TOO_LONG;
		return NULL;
	}
	else if ((i + 1) >= maxlen)
	{
		FileSys_Errno = ERROR_MAXLEN_NOT_LONG_ENOUGH;
		return NULL;
	}
  }

  return buffer;
}

/**** added by MSC 03/07/2002 ****/
/*************************************************************
Function: getcwd2
Description:
	get the current working directory
Input:
	buffer - the storage location for the pathname
	len - the length of the buffer
Return value:
	If succeeded, the number of characters.
	If failed, the real length (including '\0') of the
	current path.
Note:
	There is no '\\' or '/' at the end of the string.
**************************************************************/
int fs_getcwd2(char *buffer, int len)
{
  int returnValue;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return 0;
  }

  FileSys_Errno = 0;

  returnValue = getcwd2_r((unsigned char *)buffer, len);

  return returnValue;
}

int getcwd2_r(unsigned char *buffer, int len)
{
  int i;
  int pathLen;

  // find the length of the current path
  pathLen = (int)strlen((char *)(CurrentPath[CurrentDrive]));
  pathLen += 3;	// including drive name, ':' and '\0'

  if (buffer == NULL || len < pathLen)
	return pathLen;

  buffer[0] = BEGIN_DRIVE_NAME + CurrentDrive;
  buffer[1] = ':';

  i = 2;
  while ((CurrentPath[CurrentDrive][i - 2] != '\0') && (i < pathLen))
  {
	buffer[i] = CurrentPath[CurrentDrive][i - 2];
	i++;
  }

  buffer[i] = '\0';

  return pathLen;
}

/**** added by MSC 03/07/2002 ****/


#endif	// #ifdef FS_CURRENT_DIRECTORY_ENABLED



⌨️ 快捷键说明

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