OrionUserland
Barry Importing existing Orion Userland 19aefaa (2 years, 4 months ago)diff --git a/cat/Makefile b/cat/Makefile new file mode 100644 index 0000000..5254c7c --- /dev/null +++ b/cat/Makefile @@ -0,0 +1,46 @@ +PRODUCT=$(notdir $(abspath $(CURDIR))) + +CC=i686-orion-gcc +CFLAGS= + +AS=i686-orion-as +AFLAGS= + +LD=i686-orion-gcc +LFLAGS= + +AS_SOURCES := $(shell find . -name '*.S') +OBJS = $(sort $(subst ./,build/,$(subst .S,.o,$(AS_SOURCES)))) + +C_SOURCES := $(shell find . -name '*.c') +OBJS += $(sort $(subst ./,build/,$(subst .c,.o,$(C_SOURCES)))) + +.PHONY: clean all install + +all: $(PRODUCT) + +clean: + @echo "REMOVING OBJECT FILES" + @mkdir -p build + @rm -rf build + @touch $(PRODUCT) + @rm $(PRODUCT) + +install: $(PRODUCT) + @echo "INSTALLING $^" + @mkdir -p ${SYSROOT}/bin/ + @install -Dm 755 $(PRODUCT) -t ${SYSROOT}/bin/ + +$(PRODUCT): $(OBJS) + @echo "LINKING $@" + @$(LD) -o $@ $^ $(LFLAGS) + +build/%.o: %.c + @echo "COMPILING $<" + @mkdir -p $(@D) + @$(CC) -c $< -o $@ $(CFLAGS) + +build/%.o: %.S + @echo "ASSEMBLING $<" + @mkdir -p $(@D) + @$(AS) $< -o $@ $(AFLAGS) diff --git a/cat/cat b/cat/cat new file mode 100755 index 0000000..7b4fdb6 Binary files /dev/null and b/cat/cat differ diff --git a/cat/main.c b/cat/main.c new file mode 100644 index 0000000..f1570bc --- /dev/null +++ b/cat/main.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <sys/stat.h> +#include <dirent.h> + +/* Main function */ +int +main(int argc, char *argv[]) +{ + int fd, sz, i; + char *buf; + struct stat statbuf; + + if (argc < 2) { + printf("Usage: %s <file> [<file>...]\n"); + return 0; + } + + for (i = 1; i < argc; i++) { + fd = open(argv[i], O_RDONLY); + if (fd < 0) { + printf("%s: %s: ", argv[0], argv[i]); + perror(NULL); + continue; + } + + stat(argv[i], &statbuf); + if (S_ISREG(statbuf.mode)) { + sz = lseek(fd, 0, SEEK_END); + buf = malloc(sz + 1); + lseek(fd, 0, SEEK_SET); + read(fd, buf, sz); + printf("%s", buf); + free(buf); + } + + close(fd); + } + + return 0; +}