From 2e35aca50c317fe3d01f68aecff1472984598127 Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 13 Sep 2024 09:06:44 +0000 Subject: [PATCH] string op works, log works too git-svn-id: file:///raid/svn-personal/tewi/trunk@3 8739d7e6-ffea-ec47-b151-bdff447c6205 --- Common/Makefile | 2 +- Common/cm_log.h | 8 +++++++ Common/cm_string.h | 9 ++++++++ Common/log.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ Common/string.c | 13 +++++++++++ Platform/generic.mk | 2 +- Server/Makefile | 6 ++--- Server/main.c | 30 ++++++++++++++++++++++++- Server/tw_version.h | 8 +++++++ Server/version.c | 7 ++++++ 10 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 Common/cm_log.h create mode 100644 Common/cm_string.h create mode 100644 Common/log.c create mode 100644 Server/tw_version.h create mode 100644 Server/version.c diff --git a/Common/Makefile b/Common/Makefile index f9243e9..e6eccda 100644 --- a/Common/Makefile +++ b/Common/Makefile @@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean .SUFFIXES: .c .o -OBJS = string.o +OBJS = string.o log.o all: common.a diff --git a/Common/cm_log.h b/Common/cm_log.h new file mode 100644 index 0000000..b4807e2 --- /dev/null +++ b/Common/cm_log.h @@ -0,0 +1,8 @@ +/* $Id$ */ + +#ifndef __CM_LOG_H__ +#define __CM_LOG_H__ + +void cm_log(const char* name, const char* log, ...); + +#endif diff --git a/Common/cm_string.h b/Common/cm_string.h new file mode 100644 index 0000000..66e646a --- /dev/null +++ b/Common/cm_string.h @@ -0,0 +1,9 @@ +/* $Id$ */ + +#ifndef __CM_STRING_H__ +#define __CM_STRING_H__ + +char* cm_strcat(const char* a, const char* b); +char* cm_strdup(const char* str); + +#endif diff --git a/Common/log.c b/Common/log.c new file mode 100644 index 0000000..6eb7bed --- /dev/null +++ b/Common/log.c @@ -0,0 +1,55 @@ +/* $Id$ */ + +#include "cm_log.h" + +#include "cm_string.h" + +#include +#include +#include +#include +#include + +bool cm_do_log = false; + +#define LOGNAME_LENGTH 12 + +void cm_log(const char* name, const char* log, ...) { + if(!cm_do_log) return; + va_list args; + va_start(args, log); + char namebuf[LOGNAME_LENGTH + 1]; + memset(namebuf, '.', LOGNAME_LENGTH); + namebuf[LOGNAME_LENGTH] = 0; + int i; + for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) { + namebuf[i] = name[i]; + } + + char* result = malloc(1); + result[0] = 0; + + char cbuf[2]; + cbuf[1] = 0; + + for(i = 0; log[i] != 0; i++) { + if(log[i] == '%') { + i++; + if(log[i] == 's') { + char* tmp = result; + result = cm_strcat(tmp, va_arg(args, char*)); + free(tmp); + } + } else { + cbuf[0] = log[i]; + char* tmp = result; + result = cm_strcat(tmp, cbuf); + free(tmp); + } + } + + fprintf(stderr, "%s %s\n", namebuf, result); + va_end(args); + + free(result); +} diff --git a/Common/string.c b/Common/string.c index a0749a0..dfd94d3 100644 --- a/Common/string.c +++ b/Common/string.c @@ -1 +1,14 @@ /* $Id$ */ + +#include +#include + +char* cm_strcat(const char* a, const char* b) { + char* str = malloc(strlen(a) + strlen(b) + 1); + memcpy(str, a, strlen(a)); + memcpy(str + strlen(a), b, strlen(b)); + str[strlen(a) + strlen(b)] = 0; + return str; +} + +char* cm_strdup(const char* str) { return cm_strcat(str, ""); } diff --git a/Platform/generic.mk b/Platform/generic.mk index 717b4c6..d0e4948 100644 --- a/Platform/generic.mk +++ b/Platform/generic.mk @@ -2,6 +2,6 @@ CC = cc AR = ar -CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" +CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common LDFLAGS = LIBS = diff --git a/Server/Makefile b/Server/Makefile index 974d35a..f427984 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -5,12 +5,12 @@ include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean .SUFFIXES: .c .o -OBJS = ../Common/common.a main.o +OBJS = version.o main.o all: tewi$(EXEC) -tewi$(EXEC): $(OBJS) - $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) +tewi$(EXEC): $(OBJS) ../Common/common.a + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a .c.o: $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Server/main.c b/Server/main.c index db19da6..ec48162 100644 --- a/Server/main.c +++ b/Server/main.c @@ -1,3 +1,31 @@ /* $Id$ */ -int main() {} +#include +#include +#include + +#include + +#include "tw_version.h" + +extern bool cm_do_log; + +int main(int argc, char** argv) { + int i; + for(i = 1; i < argc; i++) { + if(argv[i][0] == '-') { + if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) { + if(!cm_do_log) { + cm_do_log = true; + cm_log("", "This is Tewi HTTPd, version %s", tw_get_version()); + } else { + cm_do_log = true; + } + } else { + fprintf(stderr, "Unknown option: %s\n", argv[i]); + return 1; + } + } + } + cm_log("Daemon", "Ready"); +} diff --git a/Server/tw_version.h b/Server/tw_version.h new file mode 100644 index 0000000..4711481 --- /dev/null +++ b/Server/tw_version.h @@ -0,0 +1,8 @@ +/* $Id$ */ + +#ifndef __TW_VERSION_H__ +#define __TW_VERSION_H__ + +const char* tw_get_version(void); + +#endif diff --git a/Server/version.c b/Server/version.c new file mode 100644 index 0000000..ef33fab --- /dev/null +++ b/Server/version.c @@ -0,0 +1,7 @@ +/* $Id$ */ + +#include "tw_version.h" + +const char* tw_version = "0.00"; + +const char* tw_get_version(void) { return tw_version; } -- 2.43.0