1

I was working on a shell project, where I encountered a problem to which I can't find a solution to. When using stat() to get modification time, I was getting weird times with Linux directories like /home, /mnt, etc.

Code:

DIR *d;
struct dirent *dir;
struct stat s;
struct tm date;
char second[4];
char minute[4];
char hour[4];
char day[4];
char month[4]; 
char* dateStr;

d = opendir("/home");

if (d)
{
    printf("Directory Contents of \"%s\":\n\n", directory); // Printing Directory Contents message
    printf("%7s    %16s       Name\n", "Size", "Last Modified");
    printf("==================================================\n");
    
    while ((dir = readdir(d)) != NULL)
    {
        stat(dir->d_name, &s);

        date = *(localtime(&s.st_mtime));

        if (date.tm_sec < 10)
        {
            sprintf(second, "0%d", date.tm_sec);
        }

        else
        {
            sprintf(second, "%d", date.tm_sec);
        }

        if (date.tm_min < 10)
        {
            sprintf(minute, "0%d", date.tm_min);
        }

        else
        {
            sprintf(minute, "%d", date.tm_min);
        }
        
        if (date.tm_hour < 10)
        {
            sprintf(hour, "0%d", date.tm_hour);
        }

        else
        {
            sprintf(hour, "%d", date.tm_hour);
        }

        if (date.tm_mday < 10)
        {
            sprintf(day, "0%d", date.tm_mday);
        }

        else
        {
            sprintf(day, "%d", date.tm_mday);
        }

        if (date.tm_mon < 10)
        {
            sprintf(month, "0%d", date.tm_mon + 1);
        }

        else
        {
            sprintf(month, "%d", date.tm_mon + 1);
        }

        if (dir->d_type == 4 || dir->d_type == 8 || dir->d_type == 10)
        {
            if (s.st_size < 1000)
            {
                printf(" <DIR>     %2s/%2s/%d %2s:%2s:%2s    %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name);
            }

            else if (s.st_size > 1024 && s.st_size < 1048576)
            {
                printf(" <DIR>    %2s/%2s/%d %2s:%2s:%2s    %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name);
            }
            
            else if (s.st_size > 1048576 && s.st_size < 1073741824)
            {
                printf(" <DIR>    %2s/%2s/%d %2s:%2s:%2s    %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name);
            }
            
            else if (s.st_size > 1073741824 && s.st_size < 1099511627776)
            {
                printf(" <DIR>    %2s/%2s/%d %2s:%2s:%2s    %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name);
            }
            
            else
            {
                printf(" <DIR>    %2s/%2s/%d %2s:%2s:%2s    %s\n", day, month, (date.tm_year + 1900), hour, minute, second, dir->d_name);
            }
        }
    }
}

Output:

Directory Contents of "/":

   Size       Last Modified       Name
==================================================
 <DIR>    24/08/2975165 08:56:24    home
 <DIR>    24/08/2975165 08:56:24    srv
 <DIR>    24/08/2975165 08:56:24    etc
 <DIR>    24/08/2975165 08:56:24    opt
 <DIR>    24/08/2975165 08:56:24    root
 <DIR>    24/08/2975165 08:56:24    lib
 <DIR>    24/08/2975165 08:56:24    mnt
 <DIR>    24/08/2975165 08:56:24    usr
 <DIR>    24/08/2975165 08:56:24    media
 <DIR>    24/08/2975165 08:56:24    lib64
 <DIR>    24/08/2975165 08:56:24    sys
 <DIR>    24/08/2975165 08:56:24    dev
 <DIR>    24/08/2975165 08:56:24    sbin
 <DIR>    24/08/2975165 08:56:24    boot
 <DIR>    24/08/2975165 08:56:24    bin
 <DIR>    24/08/2975165 08:56:24    run
 <DIR>    24/08/2975165 08:56:24    lib32
 <DIR>    24/08/2975165 08:56:24    libx32
 <DIR>    24/08/2975165 08:56:24    init
 <DIR>    24/08/2975165 08:56:24    proc
 <DIR>    24/08/2975165 08:56:24    snap
 <DIR>    24/08/2975165 08:56:24    tmp
 <DIR>    24/08/2975165 08:56:24    var
 <DIR>    24/08/2975165 08:56:24    lost+found
 <DIR>    16/04/2022 18:02:15    ..
 <DIR>    21/04/2022 17:07:47    .

The output should be something like 03/04/2022 16:03:22 (it is correct for . and ..), so where I am making a mistake?

I am sorry if my code is not clean (because I am a beginner) or I have given some unnecessary parts of the code.

0 Answers0