-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_sub_dir.c
More file actions
137 lines (128 loc) · 3.16 KB
/
Copy pathget_sub_dir.c
File metadata and controls
137 lines (128 loc) · 3.16 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This file is part of csloc.
// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account.
// csloc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// csloc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with csloc. If not, see <https://www.gnu.org/licenses/>.
#ifdef _WIN32
#include<windows.h>
#else
#include<dirent.h>
#include<errno.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"get_sub_dir.h"
int csloc____cnt_sub_dirs(const char *dir)
{
// make the wildcard search
char search[300];
strcpy(search, dir);
int cnt=0;
#ifdef _WIN32
size_t len=strlen(dir);
strcpy(search + len, "\\*");
#endif
// find data and start looking
#ifdef _WIN32
WIN32_FIND_DATA wff;
HANDLE ff = FindFirstFileA(search, &wff);
#else
DIR *dr = opendir(search);
if(dr == NULL)
{
if(errno == EACCES)
fprintf(stderr, "Could not go into %s, permission denied, maybe try running as superuser.\n", search);
else
perror("Opening directory failed.");
goto fini;
}
struct dirent *de = readdir(dr);
#endif
// keep looking while it can find more
do
++cnt
#ifdef _WIN32
;
while(FindNextFileA(ff, &wff));
#else
, de = readdir(dr);
while(de);
closedir(dr);
#endif
fini:
return cnt;
}
void csloc____get_sub_dirs(const char *dir,char *names[],enum cfs____file_or_directory fd[])
{
// make the wildcard search
char search[3000];
strcpy(search, dir);
#ifdef _WIN32
size_t len=strlen(dir);
strcpy(search + len, "\\*");
#endif
// find data and start looking
#ifdef _WIN32
WIN32_FIND_DATA wff;
HANDLE ff = FindFirstFileA(search, &wff);
#else
DIR *dr = opendir(search);
csloc_check_pointer(dr);
struct dirent *de = readdir(dr);
csloc_check_pointer(de);
#endif
size_t cnt=0, fnlen;
// keep looking while it can find more
do
{
#ifdef _WIN32
fnlen = strlen(wff.cFileName);
#else
fnlen = strlen(de->d_name);
#endif
names[cnt]=malloc(fnlen + sizeof(char));
csloc_check_pointer(names[cnt]);
#ifdef _WIN32
strcpy(names[cnt], wff.cFileName);
#else
strcpy(names[cnt], de->d_name);
#endif
#ifdef _WIN32
if(wff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
#else
if(de->d_type == DT_DIR)
#endif
fd[cnt]=DIRECTORY;
else if
#ifdef _WIN32
(wff.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
#else
(de->d_type == DT_REG)
#endif
fd[cnt]=NFILE;
else if
#ifdef _WIN32
(wff.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
#else
(de->d_type == DT_LNK)
#endif
fd[cnt]=CSLOCSYMLINK;
else
#ifdef _WIN32
fd[cnt]=NFILE;
#else
fd[cnt]=CSLOCOTHER;
#endif
++cnt;
#ifndef _WIN32
de = readdir(dr);
#endif
}
#ifdef _WIN32
while(FindNextFileA(ff, &wff));
#else
while(de);
closedir(dr);
#endif
}