|
1 |
| -// This file is part of csloc. |
2 |
| -// Copyright (C) 2020-2022, github.com/CubedProgrammer, owner of said account. |
3 |
| - |
4 |
| -// 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. |
5 |
| - |
6 |
| -// 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. |
7 |
| - |
8 |
| -// You should have received a copy of the GNU General Public License along with csloc. If not, see <https://www.gnu.org/licenses/>. |
9 |
| - |
10 |
| -#ifdef _WIN32 |
11 |
| -#include<windows.h> |
12 |
| -#else |
13 |
| -#include<dirent.h> |
14 |
| -#include<errno.h> |
15 |
| -#endif |
16 |
| -#include<stdio.h> |
17 |
| -#include<stdlib.h> |
18 |
| -#include<string.h> |
19 |
| -#include"get_sub_dir.h" |
20 |
| - |
21 |
| -int csloc____cnt_sub_dirs(const char *dir) |
22 |
| -{ |
23 |
| - // make the wildcard search |
24 |
| - char search[300]; |
25 |
| - strcpy(search, dir); |
26 |
| - int cnt=0; |
27 |
| -#ifdef _WIN32 |
28 |
| - size_t len=strlen(dir); |
29 |
| - strcpy(search + len, "\\*"); |
30 |
| -#endif |
31 |
| - // find data and start looking |
32 |
| -#ifdef _WIN32 |
33 |
| - WIN32_FIND_DATA wff; |
34 |
| - HANDLE ff = FindFirstFileA(search, &wff); |
35 |
| -#else |
36 |
| - DIR *dr = opendir(search); |
37 |
| - if(dr == NULL) |
38 |
| - { |
39 |
| - if(errno == EACCES) |
40 |
| - fprintf(stderr, "Could not go into %s, permission denied, maybe try running as superuser.\n", search); |
41 |
| - else |
42 |
| - perror("Opening directory failed."); |
43 |
| - goto fini; |
44 |
| - } |
45 |
| - struct dirent *de = readdir(dr); |
46 |
| -#endif |
47 |
| - |
48 |
| - // keep looking while it can find more |
49 |
| - do |
50 |
| - ++cnt |
51 |
| -#ifdef _WIN32 |
52 |
| - ; |
53 |
| - while(FindNextFileA(ff, &wff)); |
54 |
| -#else |
55 |
| - , de = readdir(dr); |
56 |
| - while(de); |
57 |
| - closedir(dr); |
58 |
| -#endif |
59 |
| - fini: |
60 |
| - return cnt; |
61 |
| -} |
62 |
| -void csloc____get_sub_dirs(const char *dir,char *names[],enum cfs____file_or_directory fd[]) |
63 |
| -{ |
64 |
| - // make the wildcard search |
65 |
| - char search[3000]; |
66 |
| - strcpy(search, dir); |
67 |
| -#ifdef _WIN32 |
68 |
| - size_t len=strlen(dir); |
69 |
| - strcpy(search + len, "\\*"); |
70 |
| -#endif |
71 |
| - |
72 |
| - // find data and start looking |
73 |
| -#ifdef _WIN32 |
74 |
| - WIN32_FIND_DATA wff; |
75 |
| - HANDLE ff = FindFirstFileA(search, &wff); |
76 |
| -#else |
77 |
| - DIR *dr = opendir(search); |
78 |
| - csloc_check_pointer(dr); |
79 |
| - struct dirent *de = readdir(dr); |
80 |
| - csloc_check_pointer(de); |
81 |
| -#endif |
82 |
| - size_t cnt=0, fnlen; |
83 |
| - |
84 |
| - // keep looking while it can find more |
85 |
| - do |
86 |
| - { |
87 |
| -#ifdef _WIN32 |
88 |
| - fnlen = strlen(wff.cFileName); |
89 |
| -#else |
90 |
| - fnlen = strlen(de->d_name); |
91 |
| -#endif |
92 |
| - names[cnt]=malloc(fnlen + sizeof(char)); |
93 |
| - csloc_check_pointer(names[cnt]); |
94 |
| -#ifdef _WIN32 |
95 |
| - strcpy(names[cnt], wff.cFileName); |
96 |
| -#else |
97 |
| - strcpy(names[cnt], de->d_name); |
98 |
| -#endif |
99 |
| -#ifdef _WIN32 |
100 |
| - if(wff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
101 |
| -#else |
102 |
| - if(de->d_type == DT_DIR) |
103 |
| -#endif |
104 |
| - fd[cnt]=DIRECTORY; |
105 |
| - else if |
106 |
| -#ifdef _WIN32 |
107 |
| - (wff.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) |
108 |
| -#else |
109 |
| - (de->d_type == DT_REG) |
110 |
| -#endif |
111 |
| - fd[cnt]=NFILE; |
112 |
| - else if |
113 |
| -#ifdef _WIN32 |
114 |
| - (wff.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
115 |
| -#else |
116 |
| - (de->d_type == DT_LNK) |
117 |
| -#endif |
118 |
| - fd[cnt]=CSLOCSYMLINK; |
119 |
| - else |
120 |
| - fd[cnt]=CSLOCOTHER; |
121 |
| - ++cnt; |
122 |
| -#ifndef _WIN32 |
123 |
| - de = readdir(dr); |
124 |
| -#endif |
125 |
| - } |
126 |
| -#ifdef _WIN32 |
127 |
| - while(FindNextFileA(ff, &wff)); |
128 |
| -#else |
129 |
| - while(de); |
130 |
| - closedir(dr); |
131 |
| -#endif |
132 |
| -} |
| 1 | +// This file is part of csloc. |
| 2 | +// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account. |
| 3 | + |
| 4 | +// 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. |
| 5 | + |
| 6 | +// 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. |
| 7 | + |
| 8 | +// You should have received a copy of the GNU General Public License along with csloc. If not, see <https://www.gnu.org/licenses/>. |
| 9 | + |
| 10 | +#ifdef _WIN32 |
| 11 | +#include<windows.h> |
| 12 | +#else |
| 13 | +#include<dirent.h> |
| 14 | +#include<errno.h> |
| 15 | +#endif |
| 16 | +#include<stdio.h> |
| 17 | +#include<stdlib.h> |
| 18 | +#include<string.h> |
| 19 | +#include"get_sub_dir.h" |
| 20 | + |
| 21 | +int csloc____cnt_sub_dirs(const char *dir) |
| 22 | +{ |
| 23 | + // make the wildcard search |
| 24 | + char search[300]; |
| 25 | + strcpy(search, dir); |
| 26 | + int cnt=0; |
| 27 | +#ifdef _WIN32 |
| 28 | + size_t len=strlen(dir); |
| 29 | + strcpy(search + len, "\\*"); |
| 30 | +#endif |
| 31 | + // find data and start looking |
| 32 | +#ifdef _WIN32 |
| 33 | + WIN32_FIND_DATA wff; |
| 34 | + HANDLE ff = FindFirstFileA(search, &wff); |
| 35 | +#else |
| 36 | + DIR *dr = opendir(search); |
| 37 | + if(dr == NULL) |
| 38 | + { |
| 39 | + if(errno == EACCES) |
| 40 | + fprintf(stderr, "Could not go into %s, permission denied, maybe try running as superuser.\n", search); |
| 41 | + else |
| 42 | + perror("Opening directory failed."); |
| 43 | + goto fini; |
| 44 | + } |
| 45 | + struct dirent *de = readdir(dr); |
| 46 | +#endif |
| 47 | + |
| 48 | + // keep looking while it can find more |
| 49 | + do |
| 50 | + ++cnt |
| 51 | +#ifdef _WIN32 |
| 52 | + ; |
| 53 | + while(FindNextFileA(ff, &wff)); |
| 54 | +#else |
| 55 | + , de = readdir(dr); |
| 56 | + while(de); |
| 57 | + closedir(dr); |
| 58 | +#endif |
| 59 | + fini: |
| 60 | + return cnt; |
| 61 | +} |
| 62 | +void csloc____get_sub_dirs(const char *dir,char *names[],enum cfs____file_or_directory fd[]) |
| 63 | +{ |
| 64 | + // make the wildcard search |
| 65 | + char search[3000]; |
| 66 | + strcpy(search, dir); |
| 67 | +#ifdef _WIN32 |
| 68 | + size_t len=strlen(dir); |
| 69 | + strcpy(search + len, "\\*"); |
| 70 | +#endif |
| 71 | + |
| 72 | + // find data and start looking |
| 73 | +#ifdef _WIN32 |
| 74 | + WIN32_FIND_DATA wff; |
| 75 | + HANDLE ff = FindFirstFileA(search, &wff); |
| 76 | +#else |
| 77 | + DIR *dr = opendir(search); |
| 78 | + csloc_check_pointer(dr); |
| 79 | + struct dirent *de = readdir(dr); |
| 80 | + csloc_check_pointer(de); |
| 81 | +#endif |
| 82 | + size_t cnt=0, fnlen; |
| 83 | + |
| 84 | + // keep looking while it can find more |
| 85 | + do |
| 86 | + { |
| 87 | +#ifdef _WIN32 |
| 88 | + fnlen = strlen(wff.cFileName); |
| 89 | +#else |
| 90 | + fnlen = strlen(de->d_name); |
| 91 | +#endif |
| 92 | + names[cnt]=malloc(fnlen + sizeof(char)); |
| 93 | + csloc_check_pointer(names[cnt]); |
| 94 | +#ifdef _WIN32 |
| 95 | + strcpy(names[cnt], wff.cFileName); |
| 96 | +#else |
| 97 | + strcpy(names[cnt], de->d_name); |
| 98 | +#endif |
| 99 | +#ifdef _WIN32 |
| 100 | + if(wff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 101 | +#else |
| 102 | + if(de->d_type == DT_DIR) |
| 103 | +#endif |
| 104 | + fd[cnt]=DIRECTORY; |
| 105 | + else if |
| 106 | +#ifdef _WIN32 |
| 107 | + (wff.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) |
| 108 | +#else |
| 109 | + (de->d_type == DT_REG) |
| 110 | +#endif |
| 111 | + fd[cnt]=NFILE; |
| 112 | + else if |
| 113 | +#ifdef _WIN32 |
| 114 | + (wff.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 115 | +#else |
| 116 | + (de->d_type == DT_LNK) |
| 117 | +#endif |
| 118 | + fd[cnt]=CSLOCSYMLINK; |
| 119 | + else |
| 120 | +#ifdef _WIN32 |
| 121 | + fd[cnt]=NFILE; |
| 122 | +#else |
| 123 | + fd[cnt]=CSLOCOTHER; |
| 124 | +#endif |
| 125 | + |
| 126 | + ++cnt; |
| 127 | +#ifndef _WIN32 |
| 128 | + de = readdir(dr); |
| 129 | +#endif |
| 130 | + } |
| 131 | +#ifdef _WIN32 |
| 132 | + while(FindNextFileA(ff, &wff)); |
| 133 | +#else |
| 134 | + while(de); |
| 135 | + closedir(dr); |
| 136 | +#endif |
| 137 | +} |
0 commit comments