Skip to content

Commit c7a259a

Browse files
Handles symlinks properly.
1 parent 8899b41 commit c7a259a

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
# Count Source Lines Of Code
22
## Counts lines of code of certain file types in a directory in Linux
3-
Simply compiling the C files directly will do. The older versions may work on windows.
3+
Simply compiling the C files directly will do.
4+
5+
Theoretically, version 1.8.2 and older work on windows.
6+
7+
To install, simply copy the executable file to /usr/bin or /usr/local/bin on BSD, Linux, or OS X.
8+
9+
On Microsoft Windows, there is no folder for storing executables, unless you count System32.
10+
11+
To install on Microsoft Windows, copy the executable file to a folder in the PATH enviornment variable.
12+
13+
You may need to modify the PATH variable in the settings and create a new directory if there is no suitable directory.
14+
15+
You may put the executable in a system directory such as System32, and that may work, but is inadvisable.
416
```
5-
gcc -O3 -c csloc.c get_sub_dir.c main.c
6-
gcc -static -o csloc csloc.o get_sub_dir.o main.o
17+
clang -O3 -c csloc.c get_sub_dir.c main.c
18+
clang -o csloc csloc.o get_sub_dir.o main.o
719
```
820
This is a command line tool, it has no GUI of any sort.
921

1022
Run with no args to get a help screen.
1123

1224
This repository can now be configured to be built into a shared library, include csloc.h in your code to use it.
1325
```
14-
gcc -O3 -c csloc.c get_sub_dir.c -fPIC
15-
gcc -shared -o libcsloc csloc.o get_sub_dir.o
26+
clang -O3 -c csloc.c get_sub_dir.c -fPIC
27+
clang -shared -o libcsloc csloc.o get_sub_dir.o
1628
```

csloc.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ long
184184
csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, const char *const*fexts, size_t fel)
185185
{
186186
// prepare to get the files and subdirectories
187-
char subdir[3000];
187+
char subdir[3000], lnpath[3000];
188188
size_t len=strlen(dir), cnt=0;//csloc____cnt_sub_dirs(dir);
189189
strcpy(subdir, dir);
190190
#ifdef _WIN32
@@ -258,6 +258,7 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
258258
strcpy(subdir + len + 1, names[i]);
259259

260260
// get rid of hidden files if enabled
261+
redirect:
261262
if(CSLOC_ISIGNDOT(ops) && names[i][0] == '.')
262263
continue;
263264

@@ -313,26 +314,45 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
313314
}
314315
continue;
315316
}
317+
else if(DIRECTORY==tps[i])
318+
{
319+
// get rid of parent and self
320+
if(strcmp(".", names[i]) == 0 || strcmp("..", names[i]) == 0)
321+
continue;
316322

317-
// get rid of parent and self
318-
if(strcmp(".", names[i]) == 0 || strcmp("..", names[i]) == 0)
319-
continue;
323+
if(fcnt==rm)
324+
{
325+
stack=realloc(stack, (rm+olr)*sizeof(char*));
326+
csloc_check_pointer(stack);
327+
fcnt=olr;
328+
olr=rm;
329+
rm+=fcnt;
330+
fcnt=olr;
331+
}
320332

321-
if(fcnt==rm)
333+
stack[fcnt]=malloc(strlen(subdir) + 2);
334+
csloc_check_pointer(stack[fcnt]);
335+
strcpy(stack[fcnt], subdir);
336+
fcnt++;
337+
free(names[i]);
338+
}
339+
else if(CSLOCSYMLINK==tps[i])
322340
{
323-
stack=realloc(stack, (rm+olr)*sizeof(char*));
324-
csloc_check_pointer(stack);
325-
fcnt=olr;
326-
olr=rm;
327-
rm+=fcnt;
328-
fcnt=olr;
341+
realpath(subdir, lnpath);
342+
strcpy(subdir, lnpath);
343+
if(stat(subdir, &fdat) == 0)
344+
{
345+
if(S_ISDIR(fdat.st_mode))
346+
tps[i]=DIRECTORY;
347+
else if(S_ISREG(fdat.st_mode))
348+
tps[i]=NFILE;
349+
else
350+
tps[i]=CSLOCOTHER;
351+
goto redirect;
352+
}
353+
else
354+
fprintf(stderr, "Could not stat %s.\n", subdir);
329355
}
330-
331-
stack[fcnt]=malloc(len + strlen(names[i]) + 2);
332-
csloc_check_pointer(stack[fcnt]);
333-
strcpy(stack[fcnt], subdir);
334-
fcnt++;
335-
free(names[i]);
336356
}
337357

338358
free(names);

get_sub_dir.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,22 @@ void csloc____get_sub_dirs(const char *dir,char *names[],enum cfs____file_or_dir
100100
if(de->d_type == DT_DIR)
101101
#endif
102102
fd[cnt]=DIRECTORY;
103-
else
103+
else if
104+
#ifdef _WIN32
105+
(wff.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
106+
#else
107+
(de->d_type == DT_REG)
108+
#endif
104109
fd[cnt]=NFILE;
110+
else if
111+
#ifdef _WIN32
112+
(wff.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
113+
#else
114+
(de->d_type == DT_LNK)
115+
#endif
116+
fd[cnt]=CSLOCSYMLINK;
117+
else
118+
fd[cnt]=CSLOCOTHER;
105119
++cnt;
106120
#ifndef _WIN32
107121
de = readdir(dr);

get_sub_dir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define GET_SUB_DIR_H_
1212
#define csloc_check_pointer(ptr)if(ptr == NULL)fprintf(stderr, "%s is NULL at %s, line %i\n", #ptr, __FILE__, __LINE__)
1313
enum cfs____file_or_directory
14-
{NFILE = 997, DIRECTORY};
14+
{NFILE = 997, DIRECTORY, CSLOCSYMLINK, CSLOCOTHER};
1515
int csloc____cnt_sub_dirs(const char *dir);
1616
void csloc____get_sub_dirs(const char *dir,char *names[],enum cfs____file_or_directory *fd);
1717
#endif

0 commit comments

Comments
 (0)