diff options
-rw-r--r-- | .gitmodules | 6 | ||||
-rw-r--r-- | Makefile | 38 | ||||
-rw-r--r-- | Makefile.musl | 28 | ||||
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | lolcat.c | 60 | ||||
m--------- | memorymapping | 0 | ||||
m--------- | musl | 0 |
7 files changed, 49 insertions, 99 deletions
diff --git a/.gitmodules b/.gitmodules index a956631..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +0,0 @@ -[submodule "musl"] - path = musl - url = git://git.musl-libc.org/musl -[submodule "memorymapping"] - path = memorymapping - url = https://github.com/NimbusKit/memorymapping @@ -1,43 +1,25 @@ +CC ?= gcc LOLCAT_SRC ?= lolcat.c CENSOR_SRC ?= censor.c -CFLAGS ?= -std=c11 -Wall -g +CFLAGS ?= -std=c11 -Wall -Wextra -g DESTDIR ?= /usr/local/bin -all: lolcat-static censor-static +all: lolcat censor -include Makefile.musl - -ifeq ($(shell uname -s),Darwin) - LOLCAT_SRC += memorymapping/src/fmemopen.c - CENSOR_SRC += memorymapping/src/fmemopen.c - CFLAGS += -Imemorymapping/src -endif - -.PHONY: install clean static - -static: lolcat-static censor-static - -lolcat-static: lolcat.c - gcc -c $(CFLAGS) -I$(MUSLDIR)/include -o lolcat.o $< - gcc -s -nostartfiles -nodefaultlibs -nostdinc -static -ffunction-sections -fdata-sections -Wl,--gc-sections -o $@ lolcat.o $(MUSLDIR)/lib/crt1.o $(MUSLDIR)/lib/libc.a - -censor-static: censor.c - gcc -c $(CFLAGS) -I$(MUSLDIR)/include -o censor.o $< - gcc -s -nostartfiles -nodefaultlibs -nostdinc -static -ffunction-sections -fdata-sections -Wl,--gc-sections -o $@ censor.o $(MUSLDIR)/lib/crt1.o $(MUSLDIR)/lib/libc.a +.PHONY: install clean lolcat: $(LOLCAT_SRC) - gcc $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ censor: $(CENSOR_SRC) - gcc $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ -install: lolcat-static censor-static - install lolcat-static $(DESTDIR)/lolcat - install censor-static $(DESTDIR)/censor +install: lolcat censor + install lolcat $(DESTDIR)/lolcat + install censor $(DESTDIR)/censor clean: - rm -f lolcat lolcat-static.o lolcat-static censor censor-static.o censor-static - # make -C musl clean + rm -f lolcat censor diff --git a/Makefile.musl b/Makefile.musl deleted file mode 100644 index d50de24..0000000 --- a/Makefile.musl +++ /dev/null @@ -1,28 +0,0 @@ - -ifneq (,$(wildcard /usr/local/musl)) -MUSLDIR ?= /usr/local/musl -else ifneq (,$(wildcard /usr/lib/musl)) -MUSLDIR ?= /usr/lib/musl -else ifneq (,$(wildcard musl/Makefile)) -MUSLDIR ?= musl -lolcat-static: musl -censor-static: musl -else -$(info "musl not found. If the build does not go through, consider either installing") -$(info "musl system-wide using your favorite package manager or fetching and building a") -$(info "local copy using:") -$(info "$ git submodule init") -$(info "$ git submodule update") -$(info "$ make musl") -endif -$(info "Using musl at $(MUSLDIR)") - -musl/lib/libc.a musl/lib/crt1.o: musl/config.mak - make -C musl - -musl/config.mak: - cd musl; ./configure - -.PHONY: musl -musl: musl/lib/libc.a musl/lib/crt1.o - @@ -28,7 +28,7 @@ $ snap install lolcat-c ### Mac -Build the system-libc version (instead of the statically linked version) with: +Build loclcat with: ``` $ make lolcat ``` @@ -40,20 +40,6 @@ $ make lolcat $ make && sudo make install ``` -If this can't find musl and you have it installed somewhere, run -```bash -$ make MUSLDIR=/path/to/musl -``` - -If you don't have a musl around yet, the easiest way to build is to run -```bash -$ git submodule init -$ git submodule update -$ make -``` - -This will checkout and build musl in this repository's ```musl``` dir. - ## Why? This `lolcat` clone is an attempt to reduce the world's carbon dioxide emissions by optimizing inefficient code. It's >10x as fast and <0.1% as large as the original one. @@ -13,6 +13,8 @@ * 0. You just DO WHAT THE FUCK YOU WANT TO. */ +#define _XOPEN_SOURCE + #include <stdint.h> #include <stdio.h> #include <wchar.h> @@ -25,13 +27,6 @@ #include <unistd.h> #include <sys/time.h> -#ifdef __APPLE__ -#include "fmemopen.h" -#else // __APPLE__ -#define _GNU_SOURCE //for fmemopen -#endif // __APPLE__ - - static char helpstr[] = "\n" "Usage: lolcat [-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n" "\n" @@ -79,7 +74,8 @@ void version(){ } int main(int argc, char **argv){ - int c, cc=-1, i, l=0; + int cc=-1, i, l=0; + wint_t c; int colors=1; double freq_h = 0.23, freq_v = 0.1; @@ -128,20 +124,38 @@ int main(int argc, char **argv){ i=0; for(char **filename=inputs; filename<inputs_end; filename++){ - FILE *f = stdin; + wint_t (*this_file_read_wchar)(FILE *); /* Used for --help because fmemopen is universally broken when used with fgetwc */ + FILE *f; int escape_state = 0; - if(!strcmp(*filename, "--help")) - f = fmemopen(helpstr, strlen(helpstr), "r"); - else if(strcmp(*filename, "-")) + wint_t helpstr_hack(FILE * _ignored) { + (void) _ignored; + static size_t idx = 0; + char c = helpstr[idx++]; + if (c) + return c; + idx = 0; + return WEOF; + } + + if(!strcmp(*filename, "--help")) { + this_file_read_wchar = &helpstr_hack; + f = 0; + + } else if(!strcmp(*filename, "-")) { + this_file_read_wchar = &fgetwc; + f = stdin; + + } else { + this_file_read_wchar = &fgetwc; f = fopen(*filename, "r"); + if(!f){ + fprintf(stderr, "Cannot open input file \"%s\": %s\n", *filename, strerror(errno)); + return 2; + } + } - if(!f){ - fprintf(stderr, "Cannot open input file \"%s\": %s\n", *filename, strerror(errno)); - return 2; - } - - while((c = fgetwc(f)) > 0){ + while((c = this_file_read_wchar(f)) != WEOF) { if(colors){ find_escape_sequences(c, &escape_state); @@ -165,11 +179,13 @@ int main(int argc, char **argv){ printf("\n\033[0m"); cc = -1; - fclose(f); + if (f) { + fclose(f); - if(c != WEOF && c != 0){ - fprintf(stderr, "Error reading input file \"%s\": %s\n", *filename, strerror(errno)); - return 2; + if(ferror(f)){ + fprintf(stderr, "Error reading input file \"%s\": %s\n", *filename, strerror(errno)); + return 2; + } } } } diff --git a/memorymapping b/memorymapping deleted file mode 160000 -Subproject 79ce0ddd0de4b11e4944625eb866290368f867c diff --git a/musl b/musl deleted file mode 160000 -Subproject fb58545f8d1c5fa32122244caeaf3625c12ddc0 |