Skip to content

Commit 01a607c

Browse files
Compatibility renewed.
1 parent 6cf0431 commit 01a607c

File tree

6 files changed

+152
-144
lines changed

6 files changed

+152
-144
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
## Counts lines of code of certain file types in a directory in Linux
33
Simply compiling the C files directly will do.
44

5-
Theoretically, version 1.8.2 and older work on windows.
6-
75
To install, simply copy the executable file to /usr/bin or /usr/local/bin on BSD, Linux, or OS X.
86

97
On Windows, there is no predefined folder for storing executables, unless you count System32 and related.
@@ -25,9 +23,11 @@ csloc -qt folder -x h c
2523
```
2624
Count all the .h and .c files in folder, and display each file's counts, sorted greatest to least.
2725
```
28-
csloc -fr folder -x hpp cpp
26+
csloc -flry folder -x hpp cpp
2927
```
30-
Count all the .hpp and .cpp files in folder, and display the file size of each file, sorted least to greatest, with complete sentences.
28+
Count all the .hpp and .cpp files in folder, and display the file size of each file (-f option), sorted least to greatest (-r option), with complete sentences.
29+
Then gets the total size of all .hpp files and the total for .cpp files, displays both, from -y option.
30+
Treats symbolic links as regular files, from -l option.
3131
## Compilation
3232
```
3333
clang -O3 -c csloc.c get_sub_dir.c main.c

csloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is part of csloc.
2-
// Copyright (C) 2020-2022, github.com/CubedProgrammer, owner of said account.
2+
// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account.
33

44
// 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.
55

@@ -428,7 +428,8 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
428428
free(apath);
429429
if(CSLOC_ISSIF(ops))
430430
{
431-
csloc_sort_filen(CSLOC_ISSORT(ops), d, datsz);
431+
if(datsz)
432+
csloc_sort_filen(CSLOC_ISSORT(ops), d, datsz);
432433
if(CSLOC_ISRSORT(ops))
433434
{
434435
csloc_filen tmp;

csloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is part of csloc.
2-
// Copyright (C) 2020-2022, github.com/CubedProgrammer, owner of said account.
2+
// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account.
33

44
// 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.
55

get_sub_dir.c

Lines changed: 137 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,137 @@
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+
}

get_sub_dir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is part of csloc.
2-
// Copyright (C) 2020-2022, github.com/CubedProgrammer, owner of said account.
2+
// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account.
33

44
// 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.
55

main.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is part of csloc.
2-
// Copyright (C) 2020-2022, github.com/CubedProgrammer, owner of said account.
2+
// Copyright (C) 2020-2023, github.com/CubedProgrammer, owner of said account.
33

44
// 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.
55

@@ -17,17 +17,19 @@
1717
#endif
1818
#include"csloc.h"
1919
#define VERSION_MINOR "9"
20-
#define VERSION_PATCH "5-rc1"
20+
#define VERSION_PATCH "5"
2121
int main(int argl,char*argv[])
2222
{
2323
if(argl==1)
2424
{
2525
help:
26-
printf("csloc version 1.%s.%s\n",VERSION_MINOR,VERSION_PATCH);
27-
printf("Usage: %s [OPTIONS...] FILES... [-x] [EXTENSIONS...]\nCommand line options...\n\n", argv[0]);
26+
printf("%s version 1.%s.%s\n",*argv,VERSION_MINOR,VERSION_PATCH);
27+
printf("Usage: %s [OPTIONS...] FILES/DIRECTORIES... [-x] [EXTENSIONS...]\nCommand line options...\n\n", argv[0]);
2828
puts("If a file starts with '-', escape it with a \\, otherwise the first \\ of an argument is ignored.");
2929
puts("-y to show subtotals for each file type");
30+
#ifndef _WIN32
3031
puts("-l to treat symbolic links as regular files.");
32+
#endif
3133
puts("-o to write output to a file instead of stdout, the next argument MUST be that file.");
3234
puts("-e to alternate colours in -s mode, making output easier to read.");
3335
puts("-n to list the number before the path in -qs mode.");

0 commit comments

Comments
 (0)