C program to sort file in ascending or descending order on the basis of date and timestamp, which are embed into filename

C program to sort file in ascending or descending order on the basis of date and timestamp, which are embed into filename

Step1:
In the current directory, Create 6 files, where in the filenames have date and timestamp embed into the filename.

 -rw-rw-r-- 1 labuser labuser     0 Jan 23 16:21 2018-01-01_00:05:38_test_file
-rw-rw-r-- 1 labuser labuser     0 Jan 23 16:22 2018-02-01_02:16:38_test_file
-rw-rw-r-- 1 labuser labuser     0 Jan 23 16:22 2018-03-01_21:16:35_test_file
-rw-rw-r-- 1 labuser labuser     0 Jan 23 16:22 2018-04-01_04:05:38_test_file
-rw-rw-r-- 1 labuser labuser     0 Jan 23 16:22 2018-05-01_05:05:38_test_file
-rw-rw-r-- 1 labuser labuser     0 Jan 23 16:23 2018-06-01_06:05:38_test_file

Step 2:
C program in the same directory. the below code would sort the filenames in ascending and descending order, on the basis of the timestamp and date embed into filename.


#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

/*****************************************************************************
 Format in which the crashdump files are created

 "%ld-%02d-%02d_%02d:%02d:%02d_ATT_crashdump"

 1900 + tm_val.tm_year,
 tm_val.tm_mon + 1,
 tm_val.tm_mday,
 tm_val.tm_hour,
 tm_val.tm_min,
 tm_val.tm_sec,
 serial_number

*****************************************************************************/

#define FILE_FORMAT "%d-%02d-%02d_%02d:%02d:%02d_test_file"

typedef struct file_db {
    char filename[100];
    unsigned long long int converted;
}file_db;

/* Array of file structures */
file_db file_info[10];
int i = 0;

/* Sort file based on their time of creation. Here, the file name itself contains the timestamp */
/* Sorting in Decending order */
int sort_files_based_on_time_of_creation_decending_order()
{
    int g, j;
    file_db file_info_temp;

    for (g = 0; g < i - 1; g++)
    {
        for (j = 0; j < (i - 1- g); j++)
        {
            if (file_info[j].converted < file_info[j + 1].converted)
            {
                file_info_temp = file_info[j];
                file_info[j] = file_info[j + 1];
                file_info[j + 1] = file_info_temp;
            }
        }
    }

    return 0;
}

/* Sorting in Ascending order */
int sort_files_based_on_time_of_creation_ascending_order()
{
    int g, j;
    file_db file_info_temp;

    for (g = i; g > 0; g--)
    {
        for (j = (i -g); j > 0; j--)
        {
            if (file_info[j].converted < file_info[j - 1].converted)
            {
                file_info_temp = file_info[j];
                file_info[j] = file_info[j - 1];
                file_info[j - 1] = file_info_temp;
            }
        }
    }

    return 0;
}


void print_files_after_sorting(int s)
{
    int g;
    printf("After Sorting \n");
    for(g = 0; g < s; g++)
    {
        printf("File [%d] : %s converted_time : %lld \n", g, file_info[g].filename, file_info[g].converted);
    }
}

void print_files_before_sorting(void)
{
    int g;
    printf("Before Sorting \n");
    for(g = 0; g < i; g++)
    {
        printf("File [%d] : %s converted_time : %lld \n", g, file_info[g].filename, file_info[g].converted);
    }
}


int main(void) {
    DIR *d;
    struct dirent *dir;
    d = opendir(".");

    struct tm str_time;

    unsigned long long int converted_time = 0;

    char buf[100];
    memset(buf, '\0', sizeof(buf));

    if (d) {
        while ( ((dir = readdir(d)) != NULL) )
        {
            /* If there is a file match  */
            if (strstr(dir->d_name,  "_test_file") != NULL)
            {
#ifdef DEBUG
                printf("*********************************************************** \n");
                printf("Before scanf \n");
                printf("File_name : %s\n", dir->d_name);
#endif

                /* File name syntax: 2018-03-01_21:16:35_ATT_crashdump */
                sscanf(dir->d_name, FILE_FORMAT,
                        &str_time.tm_year,
                        &str_time.tm_mon,
                        &str_time.tm_mday,
                        &str_time.tm_hour,
                        &str_time.tm_min,
                        &str_time.tm_sec);

#ifdef DEBUG
                printf("After scanf \n");
                printf(FILE_FORMAT,
                        str_time.tm_year,
                        str_time.tm_mon,
                        str_time.tm_mday,
                        str_time.tm_hour,
                        str_time.tm_min,
                        str_time.tm_sec);
#endif

                str_time.tm_year = str_time.tm_year - 1900;
                str_time.tm_mday = str_time.tm_mday -1;

                converted_time = mktime(&str_time);
#ifdef DEBUG
                printf("converted time : %lld \n", converted_time);
#endif

                /* copy converted time and file name value in structure*/
                memset(&file_info[i], '\0', sizeof(file_db));

                strncpy(file_info[i].filename, dir->d_name, sizeof(file_info[i].filename));
                file_info[i].converted = converted_time;

#ifdef DEBUG
                printf("Print array of structure  File [%d] : %s converted time value: %lld  \n\n",
                        i, file_info[i].filename, file_info[i].converted);
#endif
                i++;  /* Increment the index of array of structure here */


            }
        }
        closedir(d);
    }
    print_files_before_sorting();

    sort_files_based_on_time_of_creation_ascending_order();
    printf("**************** Ascending Order ********************* \n");
    print_files_after_sorting(i);

    printf("**************** Descending Order ********************* \n");
    sort_files_based_on_time_of_creation_decending_order();
    print_files_after_sorting(i);

    return(0);
}

Output:

Before Sorting
File [0] : 2018-04-01_04:05:38_test_file converted_time : 1525041338
File [1] : 2018-03-01_21:16:35_test_file converted_time : 1522511195
File [2] : 2018-05-01_05:05:38_test_file converted_time : 1527723338
File [3] : 2018-02-01_02:16:38_test_file converted_time : 1519764398
File [4] : 2018-06-01_06:05:38_test_file converted_time : 1530318938
File [5] : 2018-01-01_00:05:38_test_file converted_time : 1517337338
**************** Ascending Order *********************
After Sorting
File [0] : 2018-01-01_00:05:38_test_file converted_time : 1517337338
File [1] : 2018-02-01_02:16:38_test_file converted_time : 1519764398
File [2] : 2018-03-01_21:16:35_test_file converted_time : 1522511195
File [3] : 2018-04-01_04:05:38_test_file converted_time : 1525041338
File [4] : 2018-05-01_05:05:38_test_file converted_time : 1527723338
File [5] : 2018-06-01_06:05:38_test_file converted_time : 1530318938
**************** Descending Order *********************
After Sorting
File [0] : 2018-06-01_06:05:38_test_file converted_time : 1530318938
File [1] : 2018-05-01_05:05:38_test_file converted_time : 1527723338
File [2] : 2018-04-01_04:05:38_test_file converted_time : 1525041338
File [3] : 2018-03-01_21:16:35_test_file converted_time : 1522511195
File [4] : 2018-02-01_02:16:38_test_file converted_time : 1519764398


File [5] : 2018-01-01_00:05:38_test_file converted_time : 1517337338