Code example for dir scanning

/*
* Includes
*/
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include

typedef struct dirstruct{
    char fname[256];
    char localtime[256];
    time_t time;
}DirStruct_t;

static int dirlist(const gchar *path, GList **list);
static int file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB);

#define FILE_PATH "/tmp/DirectoryList.txt"

/*
* First argument can be the directory that needs to be listed
* For example if we want to list all the files in Backups folder,
* ./dirlisting /mnt/pools/B/B0/Backups
* This will list all the files in sorted order in /tmp/DirectoryList.txt
*/
int
main(int argc, char **argv)
{
    printf("Starting the directory list");
    gchar path[256];
    GList *list = NULL;
    GList *next = NULL;
    FILE *fp = NULL;
    if(argc > 1){
        strncpy(path, argv[1], 256);
    }else{
        strncpy(path, "/usr/local/amazon", 256);
    }

    dirlist(path, &list);
    list = g_list_sort(list, (GCompareFunc)file_time_comparefunc);
    next = list;
    if(next != NULL){
        fp = fopen(FILE_PATH, "w");
        while(next != NULL){
            DirStruct_t *data = (DirStruct_t *)(next->data);
            if(data != NULL){
                fprintf(fp, "/n %ld %s %s ", data->time, data->localtime, data->fname);
                g_free(data);
            }else{
                printf("/n Data is NULL");
            }
            next = g_list_next(next);
        }
        g_list_free(list);
        fclose(fp);
    }else{
        printf("/n List is NULL");
    }
    return (0);
}

static int
file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB)
{
    if(fileA->time > fileB->time){
        return 1;
    }else if(fileA->time == fileB->time){
        return 0;
    }else {
        return -1;
    }
}

static int
dirlist(const gchar *path, GList **list)
{
    GError *error = NULL;
    struct stat statbuf;
    struct tm *tm;
    char datestring[256];
    const gchar *fname = NULL;
    GDir *dir = g_dir_open(path, 0, &error);
    while((fname = g_dir_read_name(dir)) != NULL){
        char *fullpath = g_strconcat(path,"/", fname, NULL);
        if(stat(fullpath, &statbuf) != -1){
            tm = localtime(&statbuf.st_ctime);
            strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
            gboolean isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR)? TRUE : FALSE;
            time_t t = mktime(tm);
            if(isdir == FALSE){
                DirStruct_t *fstruct = g_try_new0(DirStruct_t, 1);
                fstruct->time = t;
                strncpy(fstruct->localtime, datestring, 256);
                strncpy(fstruct->fname, fullpath, 256);
                *list = g_list_append(*list, fstruct);
                //printf("/n %s %s %ld", fullpath, datestring, t);
            }else{
                //printf("/n DIR %s %s %ld", fullpath, datestring, t);
                dirlist(fullpath, list);
            }
        }else{
            printf("/n Stat error on file %s ", fname);
        }
        g_free(fullpath);
    }
    g_dir_close(dir);
    return (0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值