-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoolbox-filesystem.c
117 lines (100 loc) · 2.55 KB
/
toolbox-filesystem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// ~~~~~~~~~~~~~~~ C Toolbox ~~~~~~~~~~~~~~~~
// portable data manipulation functions
// portable socket server functions
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// toolbox-filesystem.h
//
// Copyright (c) 2105 Francois Oligny-Lemieux
// All rights reserved
//
// Author : Francois Oligny-Lemieux
// Created : 10.Apr.2015
//
// License: Yipp Dual Personal Open Source License and Business Monetary License
// http://yipp.ca/licenses/dual-personal-open-source-business-monetary-license/
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <errno.h>
#define _GNU_SOURCE // for basename not to modify source
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#if ( defined(_MSC_VER) )
# include "dirent.h" // local implementation by Kevlin Henney ([email protected], [email protected])
#else
# include <dirent.h>
#endif
#include "toolbox.h"
#include "toolbox-basic-types.h"
#include "toolbox-filesystem.h"
#if defined(_MSC_VER)
const char * GetBaseName(const char * fullpath)
{
char * strRet = strrchr(fullpath, '\\');
if (strRet == NULL) return fullpath;
return strRet+1;
}
#else
const char * GetBaseName(char const *path)
{
char *s = strrchr(path, '/');
if (!s)
return strdup(path);
else
return strdup(s + 1);
}
#endif
int traverseDir(const char* directory, fileEntryCallback_func f_callback, void * opaque1, void * opaque2)
{
DIR *pRecordDir;
struct dirent *pEntry;
struct stat FStat;
const char *pszBaseName;
char szFullPathName[512];
fileEntry_T curEntry;
if (directory == NULL) return -1;
if (f_callback == NULL) return -2;
pRecordDir = opendir(directory);
if (pRecordDir == NULL)
{
return -10;
}
while ((pEntry = readdir(pRecordDir)) != NULL)
{
pszBaseName = GetBaseName(pEntry->d_name);
if (pszBaseName != NULL && pszBaseName[0] != '.')
{
memset(&curEntry, 0, sizeof(curEntry));
snprintf(szFullPathName, sizeof(szFullPathName)-1, "%s/%s", directory, pszBaseName);
if (pEntry->d_type == DT_REG)
{
// C_GetFileSize(szFullPathName, &curEntry.size);
}
else if (pEntry->d_type == DT_DIR)
{
curEntry.isDirectory = 1;
}
else if (pEntry->d_type == DT_UNKNOWN)
{
if (lstat(szFullPathName, &FStat) == 0)
{
if (S_ISDIR(FStat.st_mode))
{
curEntry.isDirectory = 1;
}
}
else
{
printf("lstat failed for \"%s\", errno=%d.\n", szFullPathName);
}
}
curEntry.name = pEntry->d_name;
f_callback(szFullPathName, &curEntry, opaque1, opaque2);
}
}
closedir (pRecordDir);
return 1;
}