Exemplo n.º 1
0
void CommandDir::displayFileSize(int n, int length /*=0*/)
{
    // 3位一组,每组包含1个逗号分隔符。最多输出18个字符。
    static const int FIELD = 1000;
    static const int FIELD_SIZE = 4;
    static const int MAX_SIZE = 15;
    if (n > FIELD)
    {
        int field = n % 1000;
        displayFileSize(n / 1000, length + FIELD_SIZE);
        printf(",%d", field);
    }
    else
    {
        assert(n >= 0);
        if (n > 0)
            length += static_cast<int>(log10(static_cast<float>(n)));
        else
            length = 0;
        for (int i = 0; i < (MAX_SIZE - (length + FIELD_SIZE - 1)); ++i)
        {
            printf(" ");
        }
        printf("%d", n);
    }
}
Exemplo n.º 2
0
void CommandDir::displayState(state s, bool only_dir)
{
    static const int BUF_SIZE = 80;
    char buf[BUF_SIZE] = {0};
    tm t = {0};
    localtime_s(&t, &s.ctime);
    //                            两个空格
    strftime(buf, BUF_SIZE, "%Y/%m/%d  %H:%M", &t);
    printf("%s", buf);

    if (s.type == FILE_TYPE && !only_dir)
    {
        displayFileSize(s.size);
        _tprintf(_T(" %s\n"), s.name.c_str());
    }
    else if (s.type == DIR_TYPE)
    {
         //         三个空格   六个空格
        _tprintf(_T("   <DIR>      %s\n"), s.name.c_str());
    }
}
Exemplo n.º 3
0
/**
 *  \brief Hex dump the file.
 *  \param file File to dump.
 *  \result 1 if all OK.
 */
int showDir (DIR_ENTRY *file)
{
	unsigned char inFile[PATH_SIZE];
	FILE *readFile;

	/*----------------------------------------------------------------------------------------------------------------*
     * If the file is a link check it points to a regular file.                                                       *
	 *----------------------------------------------------------------------------------------------------------------*/
#ifdef USE_STATX
	if (S_ISLNK (file -> fileStat.stx_mode))
#else
	if (S_ISLNK (file -> fileStat.st_mode))
#endif
	{
		mode_t type = directoryTrueLinkType (file);
		if (!S_ISREG (type))
		{
			fprintf (stderr, "ERROR: Link does not point to file\n");
			return 0;
		}
	}

	/*----------------------------------------------------------------------------------------------------------------*
     * First display a table with the file name and size.                                                             *
	 *----------------------------------------------------------------------------------------------------------------*/
	if (!displayColumnInit (2, ptrFileColumn, 0))
	{
		fprintf (stderr, "ERROR in: displayColumnInit\n");
		return 0;
	}
	if (!displayQuiet)
	{
		displayDrawLine (0);
		displayHeading (0);
		displayNewLine (0);
		displayInColumn (0, "%s", file -> fileName);
#ifdef USE_STATX
		displayInColumn (1, displayFileSize (file -> fileStat.stx_size, (char *)inFile));
#else
		displayInColumn (1, displayFileSize (file -> fileStat.st_size, (char *)inFile));
#endif
		displayNewLine (DISPLAY_INFO);
		displayAllLines ();
	}
	displayTidy ();

	/*----------------------------------------------------------------------------------------------------------------*
     * Open the file and display a table containing the hex dump.                                                     *
	 *----------------------------------------------------------------------------------------------------------------*/
	strcpy ((char *)inFile, file -> fullPath);
	strcat ((char *)inFile, file -> fileName);

	if ((readFile = fopen ((char *)inFile, "rb")) != NULL)
	{
		if (!displayColumnInit (displayCols, ptrDumpColumn, displayFlags))
		{
			fprintf (stderr, "ERROR in: displayColumnInit\n");
			return 0;
		}

		if (!displayQuiet) displayDrawLine (0);
		if (!displayQuiet) displayHeading (0);

		processFile (readFile);
		fclose (readFile);
		++filesFound;
	}
	return 0;
}