diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..665db91 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +include config.mk + +.PHONY: all clean install uninstall + +all: $(TARGET) + +$(TARGET): $(TARGET).c + $(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c + +install: $(TARGET) + install -d $(DESTDIR)$(PREFIX)/bin + install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/$(TARGET) + +clean: + rm -f $(TARGET) diff --git a/README.md b/README.md index faf4696..b4de68e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ # slocc -Sloc Counter for C \ No newline at end of file +## What is slocc? + +Slocc is just a little fun utility to count the lines of code that exist in a program. +In fact I use this fun utility for [pms](https://github.com/LearnixOS.com/pms) to count the sloc. + +For people who are confused; sloc is a unit of measurement in coding and especially suckless software, standing for "Single Lines Of Code". +Sloc doesn't dictate how "bad" your code is, but it is a unit of measurement to help keep track of how messy your code could be. +It's generally a good idea to keep your sloc count low, **HOWEVER** never do it at the expense of performance/security. + +## General installation + +### Dependencies: +- C compiler (gcc or clang) +- C libraries (literally only uses stdio.h) + +Both of these dependencies should already exist. + +### Installation +```bash +git clone https://git.based.pt/cowmonk/slocc.git +cd slocc +make +sudo/doas make install +``` +NOTE: Please check config.mk if anything goes wrong in the installation, the default prefix is /usr/local/. diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..6195e89 --- /dev/null +++ b/config.mk @@ -0,0 +1,20 @@ +# SLOCC - Sloc Counter +# +# Developer: cowmonk +# License: AGPL v3.0 (see License) +# + + +### Compiler Flags & default C compiler + +# CFLAGS = -std=c99 -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" -s -pedantic -Wall -Os +CFLAGS = -std=c99 -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" -g -pedantic -Wall -Os +# INCLUDES = +# LDFLAGS = +CC=cc + +### Install Directory +PREFIX=/usr + +#### Target name +TARGET="slocc" diff --git a/slocc.c b/slocc.c new file mode 100644 index 0000000..3037ded --- /dev/null +++ b/slocc.c @@ -0,0 +1,52 @@ +#include + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s [file.c]\n", argv[0]); + return 1; + } + + FILE *file = fopen(argv[1], "r"); + if (!file) { + printf("Error opening file\n"); + return 1; + } + + char line[1024]; + int sloc = 0; + int in_multiline = 0; + + while (fgets(line, sizeof(line), file)) { + char *p = line; + int has_code = 0; + + while (*p) { + if (in_multiline) { + if (p[0] == '*' && p[1] == '/') { + in_multiline = 0; + p += 2; + continue; + } + } else { + if (p[0] == '/' && p[1] == '*') { + in_multiline = 1; + p += 2; + continue; + } + if (p[0] == '/' && p[1] == '/') + break; + if (p[0] != ' ' && p[0] != '\t' && p[0] != '\n' && p[0] != '\r') { + has_code = 1; + } + } + p++; + } + + if (has_code && !in_multiline) + sloc++; + } + + fclose(file); + printf("SLOC Count: %d\n", sloc); + return 0; +}