@@ -34,6 +34,22 @@ static inline int has_file_extension(const char *fname, const char *ext)
34
34
return status ;
35
35
}
36
36
}
37
+ int csloc____ispref (const char * pref , const char * str )
38
+ {
39
+ int p = 1 ;
40
+ for (; * pref != '\0' ; ++ pref , ++ str )
41
+ {
42
+ if (* str == '\0' )
43
+ {
44
+ p = 0 ;
45
+ pref = str - 1 ;
46
+ }
47
+ else if (* pref != * str )
48
+ p = 0 ;
49
+
50
+ }
51
+ return p ;
52
+ }
37
53
#ifdef _WIN32
38
54
long long
39
55
#else
@@ -185,6 +201,7 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
185
201
{
186
202
// prepare to get the files and subdirectories
187
203
char subdir [3000 ], lnpath [3000 ];
204
+ char * apath = NULL ;
188
205
size_t len = strlen (dir ), cnt = 0 ;//csloc____cnt_sub_dirs(dir);
189
206
strcpy (subdir , dir );
190
207
#ifdef _WIN32
@@ -238,12 +255,6 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
238
255
len = strlen (currf ), cnt = csloc____cnt_sub_dirs (currf );
239
256
if (cnt <= 2 )
240
257
continue ;
241
- strcpy (subdir , currf );
242
- #ifdef _WIN32
243
- subdir [len ]= '\\' ;
244
- #else
245
- subdir [len ]= '/' ;
246
- #endif
247
258
248
259
// get subdirectories and files
249
260
names = malloc (sizeof (char * )* cnt );
@@ -255,12 +266,21 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
255
266
// put all subdirectories in
256
267
for (size_t i = 0 ; i < cnt ; ++ i )
257
268
{
269
+ strcpy (subdir , currf );
270
+ #ifdef _WIN32
271
+ subdir [len ]= '\\' ;
272
+ #else
273
+ subdir [len ]= '/' ;
274
+ #endif
258
275
strcpy (subdir + len + 1 , names [i ]);
259
276
260
277
// get rid of hidden files if enabled
261
278
redirect :
262
279
if (CSLOC_ISIGNDOT (ops ) && names [i ][0 ] == '.' )
280
+ {
281
+ free (names [i ]);
263
282
continue ;
283
+ }
264
284
265
285
if (NFILE == tps [i ])
266
286
{
@@ -312,54 +332,71 @@ csloc(const char *dir, csloc_filenp *dat, size_t *sz, unsigned ops, size_t cr, c
312
332
}
313
333
sloc += sfl ;
314
334
}
315
- continue ;
316
335
}
317
336
else if (DIRECTORY == tps [i ])
318
337
{
319
338
// get rid of parent and self
320
- if (strcmp ("." , names [i ]) == 0 || strcmp (".." , names [i ]) == 0 )
321
- continue ;
322
-
323
- if (fcnt == rm )
339
+ if (strcmp ("." , names [i ]) != 0 && strcmp (".." , names [i ]) != 0 )
324
340
{
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
- }
341
+ realpath (subdir , lnpath );
342
+ if (apath == NULL )
343
+ apath = realpath (dir ,apath );
332
344
333
- stack [fcnt ]= malloc (strlen (subdir ) + 2 );
334
- csloc_check_pointer (stack [fcnt ]);
335
- strcpy (stack [fcnt ], subdir );
336
- fcnt ++ ;
337
- free (names [i ]);
345
+ if (strcmp (apath , lnpath ) != 0 )
346
+ {
347
+ if (fcnt == rm )
348
+ {
349
+ stack = realloc (stack , (rm + olr )* sizeof (char * ));
350
+ csloc_check_pointer (stack );
351
+ fcnt = olr ;
352
+ olr = rm ;
353
+ rm += fcnt ;
354
+ fcnt = olr ;
355
+ }
356
+
357
+ stack [fcnt ]= malloc (strlen (subdir ) + 2 );
358
+ csloc_check_pointer (stack [fcnt ]);
359
+ strcpy (stack [fcnt ], subdir );
360
+ fcnt ++ ;
361
+ }
362
+ }
338
363
}
339
364
else if (CSLOCSYMLINK == tps [i ])
340
365
{
341
366
realpath (subdir , lnpath );
342
- strcpy (subdir , lnpath );
343
- if (stat (subdir , & fdat ) == 0 )
367
+ if (apath == NULL )
368
+ apath = realpath (dir ,apath );
369
+ if (!csloc____ispref (apath , lnpath ))
344
370
{
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 ;
371
+ strcpy (subdir , lnpath );
372
+ realpath (currf , lnpath );
373
+ if (!csloc____ispref (lnpath , subdir ))
374
+ {
375
+ if (stat (subdir , & fdat )== 0 )
376
+ {
377
+ if (S_ISDIR (fdat .st_mode ))
378
+ tps [i ]= DIRECTORY ;
379
+ else if (S_ISREG (fdat .st_mode ))
380
+ tps [i ]= NFILE ;
381
+ else
382
+ tps [i ]= CSLOCOTHER ;
383
+ goto redirect ;
384
+ }
385
+ else
386
+ fprintf (stderr , "Could not stat %s.\n" , subdir );
387
+ }
352
388
}
353
- else
354
- fprintf (stderr , "Could not stat %s.\n" , subdir );
355
389
}
390
+ free (names [i ]);
356
391
}
357
392
358
393
free (names );
359
394
free (tps );
360
395
free (currf );
361
396
}
362
397
free (stack );
398
+ if (apath )
399
+ free (apath );
363
400
if (CSLOC_ISSIF (ops ))
364
401
{
365
402
csloc_sort_filen (CSLOC_ISSORT (ops ), d , datsz );
0 commit comments