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

📄 dir.c

📁 winNT技术操作系统,国外开放的原代码和LIUX一样
💻 C
📖 第 1 页 / 共 4 页
字号:
				}
				else if(cCurUChar == _T('G'))
				{
					if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
					lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
					lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_DIRECTORY;
				}
				else if(cCurUChar == _T('E'))
				{
					if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
					lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
					lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_EXTENSION;
				}
				else if(cCurUChar == _T('D'))
				{
					if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
					lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
					lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_TIME;
				}

				else
				{
					error_parameter_format((TCHAR)_totupper (*Line));
					return FALSE;
				}


			}
			/* We check if we calculated the negative value and realese the flag */
			if ((cCurChar != _T('-')) && bPNegative)
				bPNegative = FALSE;
		}

		Line++;
	}
	/* Terminate the parameters */
	if(ptrStart && ptrEnd)
	{		
		temp = malloc((ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
		if(!temp)
			return FALSE;
		memcpy(temp, ptrStart, (ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
		temp[(ptrEnd - ptrStart + 1)] = _T('\0');
		if(!add_entry(entries, params, temp))
		{
			free(temp);
			freep(*params);
			return FALSE;
		}

		free(temp);

		ptrStart = NULL;
		ptrEnd = NULL;
	}

	/* Calculate the switches with no switch paramater  */
	if (!(lpFlags->stAttribs.bParSetted))
	{
		lpFlags->stAttribs.dwAttribVal = 0L;
		lpFlags->stAttribs.dwAttribMask = lpFlags->stAttribs.dwAttribVal;
	}
	if (!(lpFlags->stOrderBy.bParSetted))
	{
		lpFlags->stOrderBy.sCriteriaCount = 1;
		lpFlags->stOrderBy.eCriteria[0] = ORDER_NAME;
		lpFlags->stOrderBy.bCriteriaRev[0] = FALSE;
	}
	if (!(lpFlags->stOrderBy.bParSetted))
		lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE ;

	/* Calculate the unsetted switches (the "-" prefixed)*/
	if (lpFlags->stAttribs.bUnSet)
	{
		lpFlags->stAttribs.bUnSet = FALSE;
		lpFlags->stAttribs.dwAttribVal = 0L;
		lpFlags->stAttribs.dwAttribMask = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
	}
	if (lpFlags->stOrderBy.bUnSet)
	{
		lpFlags->stOrderBy.bUnSet = FALSE;
		lpFlags->stOrderBy.sCriteriaCount = 0;
	}
	if (lpFlags->stTimeField.bUnSet )
	{
		lpFlags->stTimeField.bUnSet = FALSE;
		lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE;
	}
	return TRUE;
}


/*
 * ExtendFilespec
 *
 * extend the filespec, possibly adding wildcards
 */
static VOID
ExtendFilespec (LPTSTR file)
{
	INT len = 0;

	if (!file)
		return;


	/* if no file spec, change to "*.*" */
	if (*file == _T('\0'))
	{
		_tcscpy (file, _T("*.*"));
		return;
	}

	// add support for *.
	if ((file[0] == _T('*')) && (file[1] == _T('.') ))
	 {
		 return;
	 }

	/* if starts with . add * in front */
	if (*file == _T('.'))
	{
		memmove (&file[1], &file[0], (_tcslen (file) + 1) * sizeof(TCHAR));
		file[0] = _T('*');
	}

	/* if no . add .* */
	if (!_tcschr (file, _T('.')))
	{
		_tcscat (file, _T(".*"));
		return;
	}



	/* if last character is '.' add '*' */
	len = _tcslen (file);
	if (file[len - 1] == _T('.'))
	{
		_tcscat (file, _T("*"));
		return;
	}
}


/*
 * dir_parse_pathspec
 *
 * split the pathspec into drive, directory, and filespec
 */
static INT
DirParsePathspec (LPTSTR szPathspec, LPTSTR szPath, LPTSTR szFilespec)
{
	TCHAR  szOrigPath[MAX_PATH];
	LPTSTR start;
	LPTSTR tmp;
	INT    i;
	BOOL   bWildcards = FALSE;

	GetCurrentDirectory (MAX_PATH, szOrigPath);

	/* get the drive and change to it */
	if (szPathspec[1] == _T(':'))
	{
		TCHAR szRootPath[] = _T("A:");

		szRootPath[0] = szPathspec[0];
		start = szPathspec + 2;
		if (!SetCurrentDirectory (szRootPath))
		{
			ErrorMessage (GetLastError(), NULL);
			return 1;
		}
	}
	else
	{
		start = szPathspec;
	}


	/* check for wildcards */
	for (i = 0; szPathspec[i]; i++)
	{
		if (szPathspec[i] == _T('*') || szPathspec[i] == _T('?'))
			bWildcards = TRUE;
	}

	/* check if this spec is a directory */
	if (!bWildcards)
	{
		if (SetCurrentDirectory (szPathspec))
		{
			_tcscpy (szFilespec, _T("*.*"));

			if (!GetCurrentDirectory (MAX_PATH, szPath))
			{
				szFilespec[0] = _T('\0');
				SetCurrentDirectory (szOrigPath);
				error_out_of_memory();
				return 1;
			}

			SetCurrentDirectory (szOrigPath);
			return 0;
		}
	}

	/* find the file spec */
	tmp = _tcsrchr (start, _T('\\'));

	/* if no path is specified */
	if (!tmp)
	{
		_tcscpy (szFilespec, start);
		ExtendFilespec (szFilespec);
		if (!GetCurrentDirectory (MAX_PATH, szPath))
		{
			szFilespec[0] = _T('\0');
			SetCurrentDirectory (szOrigPath);
			error_out_of_memory();
			return 1;
		}

		SetCurrentDirectory (szOrigPath);
		return 0;
	}

	/* get the filename */
	_tcscpy (szFilespec, tmp+1);
	ExtendFilespec (szFilespec);

	if (tmp == start)
	{
		/* change to the root directory */
		if (!SetCurrentDirectory (_T("\\")))
		{
			szFilespec[0] = _T('\0');
			SetCurrentDirectory (szOrigPath);
			error_path_not_found ();
			return 1;
		}
	}
	else
	{
		*tmp = _T('\0');

		/* change to this directory */
		if (!SetCurrentDirectory (start))
		{
			*tmp = _T('\\');
			szFilespec[0] = _T('\0');
			SetCurrentDirectory (szOrigPath);
			error_path_not_found ();
			return 1;
		}
	}

	/* get the full name of the directory */
	if (!GetCurrentDirectory (MAX_PATH, szPath))
	{
		*tmp = _T('\\');
		szFilespec[0] = _T('\0');
		SetCurrentDirectory (szOrigPath);
		error_out_of_memory ();
		return 1;
	}

	*tmp = _T('\\');

	SetCurrentDirectory (szOrigPath);

	return 0;
}


/*
 * incline
 *
 * increment our line if paginating, display message at end of screen
 */
#if 0
static BOOL
IncLine (LPINT pLine, LPDIRSWITCHFLAGS lpFlags)
{
	BOOL bError;
	CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo;
	LONG WindowHeight;

	bError = GetConsoleScreenBufferInfo(hConsole, &lpConsoleScreenBufferInfo);

	WindowHeight = lpConsoleScreenBufferInfo.srWindow.Bottom - lpConsoleScreenBufferInfo.srWindow.Top;

	/* That prevents bad behiour if WindowHeight could not be calculated */
	if (!WindowHeight)
	{
		 WindowHeight= 1000000;
	}

	if (!(lpFlags->bPause))
		return FALSE;

	(*pLine)++;

	/*
	 * Because I don't know if WindowsHeight work in all cases,
	 * perhaps then maxy is the right value
	 */
	if (*pLine >= (int)maxy - 2 || *pLine >= WindowHeight)
	{
		*pLine = 0;
		return (PagePrompt () == PROMPT_BREAK);
	}

	return FALSE;
}
#endif

/*
 * PrintDirectoryHeader
 *
 * print the header for the dir command
 */
static BOOL
PrintDirectoryHeader(LPTSTR szPath, LPINT pLine, LPDIRSWITCHFLAGS lpFlags)
{
  TCHAR szMsg[RC_STRING_MAX_SIZE];
  TCHAR szRootName[MAX_PATH];
  TCHAR szVolName[80];
  DWORD dwSerialNr;
  LPTSTR p;

  if (lpFlags->bBareFormat)
    return TRUE;

  /* build usable root path */
  if (szPath[1] == _T(':') && szPath[2] == _T('\\'))
    {
      /* normal path */
      szRootName[0] = szPath[0];
      szRootName[1] = _T(':');
      szRootName[2] = _T('\\');
      szRootName[3] = 0;
    }
  else if (szPath[0] == _T('\\') && szPath[1] == _T('\\'))
    {
      /* UNC path */
      p = _tcschr(&szPath[2], _T('\\'));
      if (p == NULL)
	{
	  error_invalid_drive();
	  return(FALSE);
	}
      p = _tcschr(p+1, _T('\\'));
      if (p == NULL)
	{
	  _tcscpy(szRootName, szPath);
	  _tcscat(szRootName, _T("\\"));
	}
      else
	{
	  *p = 0;
	  _tcscpy(szRootName, szPath);
	  _tcscat(szRootName, _T("\\"));
	  *p = _T('\\');
	}
    }
  else
    {
      error_invalid_drive();
      return(FALSE);
    }

  /* get the media ID of the drive */
  if (!GetVolumeInformation(szRootName, szVolName, 80, &dwSerialNr,
			    NULL, NULL, NULL, 0))
    {
      error_invalid_drive();
      return(FALSE);
    }

  /* print drive info */
  if (szVolName[0] != _T('\0'))
    {
      LoadString(CMD_ModuleHandle, STRING_DIR_HELP2, szMsg, RC_STRING_MAX_SIZE);
      //needs to have first paramter as TRUE because
	  //this is the first output and need to clear the static
	  if(lpFlags->bPause)
		 ConOutPrintfPaging(TRUE,szMsg, szRootName[0], szVolName);
	  else
		 ConOutPrintf(szMsg, szRootName[0], szVolName);
		 
    }
  else
    {
      LoadString(CMD_ModuleHandle, STRING_DIR_HELP3, szMsg, RC_STRING_MAX_SIZE);
	if(lpFlags->bPause)
		 ConOutPrintfPaging(TRUE,szMsg, szRootName[0]);
	else
		 ConOutPrintf(szMsg, szRootName[0]);
    }

  /* print the volume serial number if the return was successful */
  LoadString(CMD_ModuleHandle, STRING_DIR_HELP4, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
  if(lpFlags->bPause)
	 ConOutPrintfPaging(FALSE,szMsg,
               HIWORD(dwSerialNr),
               LOWORD(dwSerialNr));
  else
	 ConOutPrintf(szMsg,
               HIWORD(dwSerialNr),
               LOWORD(dwSerialNr));


  return TRUE;
}


/*
 * convert
 *
 * insert commas into a number
 *
 */
#if 0
static INT
ConvertULong (ULONG num, LPTSTR des, INT len)
{
	TCHAR temp[32];
	INT c = 0;
	INT n = 0;

	if (num == 0)
	{
		des[0] = _T('0');
		des[1] = _T('\0');
		n = 1;
	}
	else
	{
		temp[31] = 0;
		while (num > 0)
		{
			if (((c + 1) % (nNumberGroups + 1)) == 0)
				temp[30 - c++] = cThousandSeparator;
			temp[30 - c++] = (TCHAR)(num % 10) + _T('0');
			num /= 10;
		}

		for (n = 0; n <= c; n++)
			des[n] = temp[31 - c + n];
	}

	return n;
}
#endif

static VOID
DirPrintFileDateTime(TCHAR *lpDate,
                     TCHAR *lpTime,
                     LPWIN32_FIND_DATA lpFile,
                     LPDIRSWITCHFLAGS lpFlags)
{
	FILETIME ft;
	SYSTEMTIME dt;
	TCHAR szDate[30];
	TCHAR szTime[30];
	WORD wYear;

	/* Select the right time field */
	switch (lpFlags->stTimeField.eTimeField)
	{
		case TF_CREATIONDATE:
			if (!FileTimeToLocalFileTime(&lpFile->ftCreationTime, &ft))
				return;
			FileTimeToSystemTime(&ft, &dt);
			break;

		case TF_LASTACCESSEDDATE :
			if (!FileTimeToLocalFileTime(&lpFile->ftLastAccessTime, &ft))
				return;
			FileTimeToSystemTime(&ft, &dt);
			break;

		case TF_MODIFIEDDATE:
			if (!FileTimeToLocalFileTime(&lpFile->ftLastWriteTime, &ft))
				return;
			FileTimeToSystemTime(&ft, &dt);
			break;
	}

	/* Format date */
	wYear = (lpFlags->b4Digit) ? dt.wYear : dt.wYear%100;
	switch (nDateFormat)
	{
		case 0: /* mmddyy */
		default:
			_stprintf (szDate, _T("%02d%c%02d%c%0*d"),
					dt.wMonth, cDateSeparator,
					dt.wDay, cDateSeparator,
					lpFlags->b4Digit?4:2, wYear);
			break;

		case 1: /* ddmmyy */
			_stprintf (szDate, _T("%02d%c%02d%c%0*d"),
					dt.wDay, cDateSeparator, dt.wMonth,
					cDateSeparator,lpFlags->b4Digit?4:2, wYear);
			break;

		case 2: /* yymmdd */
			_stprintf (szDate, _T("%0*d%c%02d%c%02d"),
					lpFlags->b4Digit?4:2, wYear, cDateSeparator,
					dt.wMonth, cDateSeparator, dt.wDay);
			break;
	}
	/* Format Time */
	switch (nTimeFormat)
	{
		case 0: /* 12 hour format */
		default:
			_stprintf (szTime,_T("  %02d%c%02u%c"),
					(dt.wHour == 0 ? 12 : (dt.wHour <= 12 ? dt.wHour : dt.wHour - 12)),
					cTimeSeparator,
					 dt.wMinute, (dt.wHour <= 11 ? _T('a') : _T('p')));
			break;

		case 1: /* 24 hour format */
			_stprintf (szTime, _T("  %02d%c%02u"),
					dt.wHour, cTimeSeparator, dt.wMinute);

⌨️ 快捷键说明

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