string op works, log works too

git-svn-id: file:///raid/svn-personal/tewi/trunk@3 8739d7e6-ffea-ec47-b151-bdff447c6205
This commit is contained in:
Nishi 2024-09-13 09:06:44 +00:00
parent b7ef2d8f80
commit 2e35aca50c
10 changed files with 134 additions and 6 deletions

View File

@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
.PHONY: all clean .PHONY: all clean
.SUFFIXES: .c .o .SUFFIXES: .c .o
OBJS = string.o OBJS = string.o log.o
all: common.a all: common.a

8
Common/cm_log.h Normal file
View File

@ -0,0 +1,8 @@
/* $Id$ */
#ifndef __CM_LOG_H__
#define __CM_LOG_H__
void cm_log(const char* name, const char* log, ...);
#endif

9
Common/cm_string.h Normal file
View File

@ -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

55
Common/log.c Normal file
View File

@ -0,0 +1,55 @@
/* $Id$ */
#include "cm_log.h"
#include "cm_string.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
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);
}

View File

@ -1 +1,14 @@
/* $Id$ */ /* $Id$ */
#include <string.h>
#include <stdlib.h>
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, ""); }

View File

@ -2,6 +2,6 @@
CC = cc CC = cc
AR = ar AR = ar
CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common
LDFLAGS = LDFLAGS =
LIBS = LIBS =

View File

@ -5,12 +5,12 @@ include $(PWD)/Platform/$(PLATFORM).mk
.PHONY: all clean .PHONY: all clean
.SUFFIXES: .c .o .SUFFIXES: .c .o
OBJS = ../Common/common.a main.o OBJS = version.o main.o
all: tewi$(EXEC) all: tewi$(EXEC)
tewi$(EXEC): $(OBJS) tewi$(EXEC): $(OBJS) ../Common/common.a
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a
.c.o: .c.o:
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<

View File

@ -1,3 +1,31 @@
/* $Id$ */ /* $Id$ */
int main() {} #include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <cm_log.h>
#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");
}

8
Server/tw_version.h Normal file
View File

@ -0,0 +1,8 @@
/* $Id$ */
#ifndef __TW_VERSION_H__
#define __TW_VERSION_H__
const char* tw_get_version(void);
#endif

7
Server/version.c Normal file
View File

@ -0,0 +1,7 @@
/* $Id$ */
#include "tw_version.h"
const char* tw_version = "0.00";
const char* tw_get_version(void) { return tw_version; }