BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionUserland / commit / 19aefaad8af9de8719ba1e5b5340e0a1b9c68853 / date

// Related

OrionUserland

Barry Importing existing Orion Userland 19aefaa (2 years, 4 months ago)
diff --git a/date/Makefile b/date/Makefile
new file mode 100644
index 0000000..5254c7c
--- /dev/null
+++ b/date/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/date/date b/date/date
new file mode 100755
index 0000000..0e0bee7
Binary files /dev/null and b/date/date differ
diff --git a/date/main.c b/date/main.c
new file mode 100644
index 0000000..2e35681
--- /dev/null
+++ b/date/main.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <time.h>
+
+#define LEAP_YEAR(x) ((((x % 4) == 0) && ((x % 100) != 0)) || ((x % 400) == 0))
+
+/* Main function */
+int
+main(int argc, char *argv[])
+{
+	/* Length of months for normal and leap years */
+	const uint16_t monthLen[2][12] = {
+		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
+		{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
+	};
+
+	uint8_t second, minute, hour, day, month;
+	time_t now = time(NULL);
+	uint32_t year = 1970, dayclock = now % 86400, dayno = now / 86400;
+
+	second = dayclock % 60;
+	minute = (dayclock % 3600) / 60;
+	hour = dayclock / 3600;
+	while (dayno >= (LEAP_YEAR(year) ? 366 : 365)) {
+		dayno -= (LEAP_YEAR(year) ? 366 : 365);
+		year++;
+	}
+	year -= 1970;
+	month = 0;
+	while (dayno >= monthLen[LEAP_YEAR(year)][month]) {
+		dayno -= monthLen[LEAP_YEAR(year)][month];
+		month++;
+	}
+	day = dayno + 1;
+
+	printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d UTC\n",
+	       year+1970, month+1, day+1, hour, minute, second);
+
+	return 0;
+}