-
Notifications
You must be signed in to change notification settings - Fork 0
/
sloc.c
522 lines (460 loc) · 12.3 KB
/
sloc.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* sloc.c
* implements functions for counting the lines of source code in a given
* directory (including all subdirectories). code is easily modified to
* add new languages.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include "sloc.h"
#include "languages.h"
#define NUM_LANGS sizeof(langs) / sizeof(lang_t)
int main(int argc, char **argv)
{
int i;
int numcounts = 0;
char * pwd;
sloc_t counts[NUM_LANGS];
int print_tots = 1;
/* initilize all the count values to 0 */
for (i = 0; i < NUM_LANGS; i++)
{
counts[i].tot = 0;
counts[i].code = 0;
counts[i].com = 0;
counts[i].blank = 0;
counts[i].files = 0;
}
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-v") == 0)
{
/* version info */
disp_version();
}
else if (strcmp(argv[i], "-h") == 0)
{
/* help message */
disp_usage(argv[0]);
}
else if (strcmp(argv[i], "-n") == 0)
{
/* don't print totals */
print_tots = 0;
}
else if (strcmp(argv[i], "-t") == 0)
{
/* check that the next argument exists */
if (++i == argc)
{
disp_usage(argv[0]);
}
/* tell program not to count pwd */
numcounts++;
/* count lines from stdin using the given language */
count_stdin(argv[i], counts);
}
else if (strcmp(argv[i], "-") == 0)
{
/* don't count pwd */
numcounts++;
/* get file list from stdin */
get_stdin_filenames(counts);
}
else
{
/* tell program not to count pwd */
numcounts++;
/* count lines from the given file */
count_lines(argv[i], counts);
}
}
/* if no counts were performed, count the pwd */
if (numcounts == 0)
{
/* pwd is dynamically allocated */
pwd = getcwd(NULL, 0);
count_lines(pwd, counts);
free(pwd);
}
/* print the results */
print_sloc(counts, print_tots);
return EXIT_SUCCESS;
}
void disp_version(void)
{
printf("sloc-"VERSION", (c) 2013-14 Brian Kubisiak, "
"see LICENSE for details\n");
exit(EXIT_SUCCESS);
}
void disp_usage(char *prog)
{
printf("usage: %s [-v] [-h] [-n] [-t lang] [-] [file] [...]\n", prog);
exit(EXIT_SUCCESS);
}
int get_lang_idx(char *name)
{
int i;
for (i = 0; i < NUM_LANGS; i++)
{
if (strcmp(name, langs[i].name) == 0)
{
return i;
}
}
return -1;
}
void count_lines(char *filename, sloc_t *counts)
{
struct stat sb;
int lang;
if (stat(filename, &sb) == -1)
{
return;
}
if (S_ISDIR(sb.st_mode) != 0)
{
count_folder(filename, counts);
}
else if ( S_ISREG(sb.st_mode) != 0 &&
(lang = get_file_lang(filename)) != -1)
{
count_file(filename, counts + lang, lang);
}
}
void get_stdin_filenames(sloc_t *counts)
{
char name[BUFSIZ];
char * nl;
while (fgets(name, BUFSIZ, stdin) != NULL)
{
nl = strchr(name, '\n');
if (nl != NULL)
{
*nl = '\0';
}
count_lines(name, counts);
}
}
int strends_with(char *haystack, char *needle)
{
char * end = haystack + strlen(haystack) - strlen(needle);
if (strcmp(end, needle) == 0)
{
return 1;
}
return 0;
}
int get_file_lang(char *filename)
{
int i;
int j;
for (i = 0; i < NUM_LANGS; i++)
{
for (j = 0; langs[i].ext[j] != NULL; j++)
{
if (strends_with(filename, langs[i].ext[j]) != 0)
{
return i;
}
}
}
return -1;
}
void count_file(char *filename, sloc_t *counter, int lang)
{
FILE * fp;
fp = fopen(filename, "r");
if (fp != NULL)
{
count_stream(fp, counter, lang);
}
}
void count_stdin(char *lang, sloc_t *counts)
{
int idxlang;
idxlang = get_lang_idx(lang);
if (idxlang == -1)
{
fprintf(stderr, "error: '%s' is not a known language!\n", lang);
exit(EXIT_FAILURE);
}
count_stream(stdin, &counts[idxlang], idxlang);
}
/* this method sucks - I should really rewrite it */
void count_stream(FILE *fp, sloc_t *counter, int lang)
{
char s[BUFSIZ];
char codeline = 0;
char comline = 0;
char comment = 0;
char eol = 0;
char * pos;
counter->files++;
/* loop through doc */
while (fgets(s, BUFSIZ, fp) != NULL)
{
/* loop through line */
for (pos = s; pos < s + BUFSIZ; pos++)
{
/* comment switch */
switch (comment)
{
case 0:
/* if not in comment, check for beginning of comment */
if (langs[lang].startblk != NULL &&
strstr(pos, langs[lang].startblk) == pos)
{
/* if beginning of block is found, enter comment mode */
comment = 1;
comline = 1;
/* continue to one less than the end of the comment */
pos += (strlen(langs[lang].startblk) - 1);
/* continue loop through line */
continue;
}
else if (langs[lang].eol != NULL &&
strstr(pos, langs[lang].eol) == pos)
{
comline = 1;
eol = 1;
}
/* break comment switch */
break;
default:
/* if in comment, check for end of block */
if (langs[lang].endblk != NULL &&
strstr(pos, langs[lang].endblk) == pos)
{
/* if the end of the block is found, exit comment mode */
comment = 0;
comline = 1;
/* continue to one less than the end of the comment */
pos += (strlen(langs[lang].endblk) - 1);
/* continue loop through line */
continue;
}
/* break comment switch */
break;
}
if (eol == 1)
{
/* break loop through line */
break;
}
/* add the character to the proper line count */
/* pos switch */
switch (*pos)
{
case '\t': /* fallthrough */
case ' ':
/* continue loop through line */
continue;
case '\r': /* fallthrough */
case '\n':
eol = 1; /* fallthrough */
case '\0':
/* break pos switch */
break;
default:
/* comment switch */
switch (comment)
{
case 0:
codeline = 1;
/* break comment switch */
break;
default:
comline = 1;
/* break comment switch */
break;
}
/* continue loop through line */
continue;
}
/* break loop through line */
break;
}
if (eol == 1)
{
eol = 0;
counter->tot++;
counter->code += codeline;
counter->com += comline;
counter->blank += (codeline == 0 && comline == 0) ? 1 : 0;
codeline = 0;
comline = 0;
}
}
fclose(fp);
}
void count_folder(char *dirname, sloc_t *counts)
{
char path[BUFSIZ]; /* path to the next file */
int idx; /* index of the NULL byte in 'path' */
DIR * dp;
struct dirent * next;
strncpy(path, dirname, BUFSIZ - 1);
path[BUFSIZ - 2] = '\0';
idx = strlen(path);
path[idx] = '/';
idx++;
if ((dp = opendir(dirname)) == NULL)
{
return;
}
while ((next = readdir(dp)) != NULL)
{
if (*(next->d_name) != '.')
{
strncpy(path + idx, next->d_name, BUFSIZ - idx - 1);
count_lines(path, counts);
}
}
closedir(dp);
}
void print_sloc(sloc_t *counts, int print_tots)
{
int i;
sloc_t tots;
char s[NUM_MEMBERS][BUFSIZ];
int maxn[NUM_MEMBERS] =
{
sizeof(STR_LANG) - 1,
sizeof(STR_TOT) - 1,
sizeof(STR_CODE) - 1,
sizeof(STR_COM) - 1,
sizeof(STR_BLANK) - 1,
sizeof(STR_FILE) - 1,
};
sloc_list_t * lst = NULL;
tots.tot = 0;
tots.code = 0;
tots.com = 0;
tots.blank = 0;
tots.files = 0;
/* put the data in a list that is suited for printing */
for (i = 0; i < NUM_LANGS; i++)
{
if (counts[i].files != 0)
{
tots.tot += counts[i].tot;
tots.code += counts[i].code;
tots.com += counts[i].com;
tots.blank += counts[i].blank;
tots.files += counts[i].files;
/* check lengths of the entries */
strcpy(s[IDX_LANG], langs[i].name);
snprintf(s[IDX_TOT], BUFSIZ, "%d", counts[i].tot);
snprintf(s[IDX_CODE], BUFSIZ, "%d", counts[i].code);
snprintf(s[IDX_COM], BUFSIZ, "%d", counts[i].com);
snprintf(s[IDX_BLANK], BUFSIZ, "%d", counts[i].blank);
snprintf(s[IDX_FILE], BUFSIZ, "%d", counts[i].files);
set_max_lens(s, maxn);
add_sloc_item(&lst, counts[i].code, s);
}
}
if (print_tots != 0)
{
strcpy(s[IDX_LANG], STR_TOT);
snprintf(s[IDX_TOT], BUFSIZ, "%d", tots.tot);
snprintf(s[IDX_CODE], BUFSIZ, "%d", tots.code);
snprintf(s[IDX_COM], BUFSIZ, "%d", tots.com);
snprintf(s[IDX_BLANK], BUFSIZ, "%d", tots.blank);
snprintf(s[IDX_FILE], BUFSIZ, "%d", tots.files);
set_max_lens(s, maxn);
add_sloc_item(&lst, tots.tot, s);
}
strcpy(s[IDX_LANG], STR_LANG);
strcpy(s[IDX_TOT], STR_TOT);
strcpy(s[IDX_CODE], STR_CODE);
strcpy(s[IDX_COM], STR_COM);
strcpy(s[IDX_BLANK], STR_BLANK);
strcpy(s[IDX_FILE], STR_FILE);
add_sloc_item(&lst, INT_MAX, s);
print_sloc_list(lst, maxn);
free_sloc_list(lst);
}
void set_max_lens(char mems[][BUFSIZ], int *maxn)
{
int i;
for (i = 0; i < NUM_MEMBERS; i++)
{
maxn[i] = MAX(maxn[i], strlen(mems[i]));
}
}
void add_sloc_item(sloc_list_t **lst, int idx, char s[][BUFSIZ])
{
sloc_list_t ** cur = lst;
sloc_list_t * new;
new = (sloc_list_t *)malloc(sizeof(sloc_list_t));
if (new == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
new->idx = idx;
new->name = strdup(s[IDX_LANG]);
new->tot = strdup(s[IDX_TOT]);
new->code = strdup(s[IDX_CODE]);
new->com = strdup(s[IDX_COM]);
new->blank = strdup(s[IDX_BLANK]);
new->files = strdup(s[IDX_FILE]);
for (;;)
{
if (*cur == NULL || (*cur)->idx < new->idx)
{
new->next = *cur;
*cur = new;
return;
}
cur = &((*cur)->next);
}
}
void free_sloc_list(sloc_list_t *lst)
{
sloc_list_t *cur;
sloc_list_t *next;
cur = lst;
while (cur != NULL)
{
next = cur->next;
free(cur->name);
free(cur->tot);
free(cur->code);
free(cur->com);
free(cur->blank);
free(cur->files);
free(cur);
cur = next;
}
}
void print_sloc_list(sloc_list_t *list, int *lens)
{
sloc_list_t * cur = list;
while (cur != NULL)
{
print_member(cur->name, lens[IDX_LANG]);
print_member(cur->files, lens[IDX_FILE]);
print_member(cur->code, lens[IDX_CODE]);
print_member(cur->com, lens[IDX_COM]);
print_member(cur->blank, lens[IDX_BLANK]);
print_member(cur->tot, lens[IDX_TOT]);
putchar('\n');
cur = cur->next;
}
}
void print_member(char *s, int n)
{
int i;
for (i = 0; i < n - strlen(s); i++)
{
putchar(' ');
}
printf("%s"SPACES, s);
}