52 lines
951 B
C
52 lines
951 B
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: %s [file.c]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
FILE *file = fopen(argv[1], "r");
|
|
if (!file) {
|
|
fprintf(stderr, "Error: cannot open file\n");
|
|
return 1;
|
|
}
|
|
|
|
char line[BUFSIZ];
|
|
int sloc = 0, multi = 0;
|
|
|
|
while (fgets(line, sizeof line, file)) {
|
|
char *p = line;
|
|
int has_code = 0;
|
|
|
|
while (*p) {
|
|
if (multi) {
|
|
if (p[0] == '*' && p[1] == '/') {
|
|
multi = 0;
|
|
p += 2;
|
|
continue;
|
|
}
|
|
} else {
|
|
if (p[0] == '/' && p[1] == '*') {
|
|
multi = 1;
|
|
p += 2;
|
|
continue;
|
|
}
|
|
if (p[0] == '/' && p[1] == '/')
|
|
break;
|
|
if (!strchr(" \t\n\r", *p))
|
|
has_code = 1;
|
|
}
|
|
p++;
|
|
}
|
|
|
|
if (has_code && !multi)
|
|
sloc++;
|
|
}
|
|
|
|
fclose(file);
|
|
printf("SLOC Count: %d\n", sloc);
|
|
return 0;
|
|
}
|