From f27955b178ae831e8afb1a8f7fc8df7fbfb8bae9 Mon Sep 17 00:00:00 2001 From: cowmonk Date: Mon, 20 Jan 2025 01:33:05 -0700 Subject: [PATCH] Minor tweaks to slocc All of this just to be a little faster and reduce sloc count, it's at 44 sloc rn --- slocc.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/slocc.c b/slocc.c index 3037ded..d456a3b 100644 --- a/slocc.c +++ b/slocc.c @@ -1,48 +1,47 @@ #include +#include int main(int argc, char *argv[]) { if (argc != 2) { - printf("Usage: %s [file.c]\n", argv[0]); + fprintf(stderr, "Usage: %s [file.c]\n", argv[0]); return 1; } FILE *file = fopen(argv[1], "r"); if (!file) { - printf("Error opening file\n"); + fprintf(stderr, "Error: cannot open file\n"); return 1; } - char line[1024]; - int sloc = 0; - int in_multiline = 0; + char line[BUFSIZ]; + int sloc = 0, multi = 0; - while (fgets(line, sizeof(line), file)) { + while (fgets(line, sizeof line, file)) { char *p = line; int has_code = 0; while (*p) { - if (in_multiline) { + if (multi) { if (p[0] == '*' && p[1] == '/') { - in_multiline = 0; + multi = 0; p += 2; continue; } } else { if (p[0] == '/' && p[1] == '*') { - in_multiline = 1; + multi = 1; p += 2; continue; } if (p[0] == '/' && p[1] == '/') break; - if (p[0] != ' ' && p[0] != '\t' && p[0] != '\n' && p[0] != '\r') { + if (!strchr(" \t\n\r", *p)) has_code = 1; - } } p++; } - if (has_code && !in_multiline) + if (has_code && !multi) sloc++; }