From b705164771b54ac510eafc550d8925756b645f10 Mon Sep 17 00:00:00 2001 From: llsth Date: Wed, 15 Apr 2015 23:52:40 +0200 Subject: [PATCH] Bugfixing and cleanup --- main_amiga_client.c | 36 ++++++++++++++++++++++++++---------- main_amiga_poll.c | 16 ++++++++++------ main_amiga_sim.c | 3 ++- mem.c | 25 +++++++++++-------------- mem.h | 3 +++ ntp_peer.c | 9 +++------ ntp_peerset.c | 5 ++--- ocx_stdio.c | 23 +++++++++++------------ 8 files changed, 68 insertions(+), 52 deletions(-) diff --git a/main_amiga_client.c b/main_amiga_client.c index 79ab3c0..febedce 100644 --- a/main_amiga_client.c +++ b/main_amiga_client.c @@ -31,8 +31,8 @@ #include "ntp.h" #include "udp.h" -#define ARGSFORMAT "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,QUIET/S" -const char *vers = "\0$VER: ntimed-client 1.0a"; +#define ARGSFORMAT "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,SHOW/S,QUIET/S" +const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)"; #define PARAM_CLIENT PARAM_INSTANCE #define PARAM_TABLE_NAME client_param_table @@ -60,6 +60,7 @@ struct ntimedargs { char **servers; long synchronize; long save; + long show; long quiet; }; @@ -71,22 +72,37 @@ int main(int argc, char **argv) { struct ntimedargs args = { NULL, NULL, NULL, NULL, - FALSE, FALSE, FALSE + FALSE, FALSE, FALSE, FALSE }; - started_from_wb = (BOOL)argc; - + started_from_wb = (argc == 0); atexit(clean_exit); - rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); - if (!rdargs) - { - PrintFault(IoErr(), (ARGPTR)argv[0]); - exit(2); + if(started_from_wb) { + args.servers = (char *[]) { + "0.pool.ntp.org", + "1.pool.ntp.org", + "2.pool.ntp.org", + "3.pool.ntp.org" + }; + args.synchronize = TRUE; + args.save = TRUE; + args.quiet = TRUE; + } else { + rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + if (!rdargs) + { + PrintFault(IoErr(), (ARGPTR)argv[0]); + exit(2); + } } if (!args.quiet) { EnableDebug(); + + if (args.show) { + EnableTraceDebug(); + } } init_libs(&args); diff --git a/main_amiga_poll.c b/main_amiga_poll.c index de3f153..623acef 100644 --- a/main_amiga_poll.c +++ b/main_amiga_poll.c @@ -31,8 +31,8 @@ #include "ntp.h" #include "udp.h" -#define ARGSFORMAT "D=DURATION/N,M=MONITOR/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,QUIET/S" -const char *vers = "\0$VER: ntimed-poll 1.0a"; +#define ARGSFORMAT "D=DURATION/N,M=MONITOR/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SHOW/S,QUIET/S" +const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)"; void Time_Amiga_Passive(void); @@ -50,6 +50,7 @@ struct ntimedargs { char *tracefile; char *timezone; char **servers; + long show; long quiet; }; @@ -59,7 +60,7 @@ static void clean_exit(); int main(int argc, char **argv) { - struct ntimedargs args = { NULL, NULL, "-", NULL, NULL, FALSE }; + struct ntimedargs args = { NULL, NULL, NULL, NULL, NULL, FALSE, FALSE }; started_from_wb = (BOOL)argc; @@ -74,7 +75,10 @@ int main(int argc, char **argv) if (!args.quiet) { EnableDebug(); - EnableTraceDebug(); + + if (args.show) { + EnableTraceDebug(); + } } init_libs(&args); @@ -98,13 +102,13 @@ static void init_libs(struct ntimedargs *args) #ifdef AROS if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) { Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n"); - exit(2); + exit(5); } if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno, SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) { Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n"); - exit(2); + exit(5); } #endif } diff --git a/main_amiga_sim.c b/main_amiga_sim.c index 7fcf227..7997e87 100644 --- a/main_amiga_sim.c +++ b/main_amiga_sim.c @@ -26,12 +26,13 @@ */ #include "ntimed_platform.h" +#include "mem.h" #include "ntimed.h" #include "ntp.h" #include "udp.h" #define ARGSFORMAT "P=PARAM/K,B=BUMP/K,T=TRACEFILE/K,SIMFILE/A,QUIET/S" -const char *vers = "\0$VER: ntimed-sim 1.0a"; +const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)"; #define PARAM_CLIENT PARAM_INSTANCE #define PARAM_TABLE_NAME client_param_table diff --git a/mem.c b/mem.c index d4f8705..8d646bf 100644 --- a/mem.c +++ b/mem.c @@ -184,20 +184,17 @@ void deallocerror(char *descr, void *p) Debug(NULL, "Mememory deallocation error (%s) address (%x)\n", descr, p); } -#ifdef __c_plus -inline void* operator new (size_t size) { - return allocmem(size); -} +char *strdup(const char *s1) +{ + char *s2; + size_t len = strlen(s1); + s2 = allocmem(++len); -inline void* operator new[] (size_t size) { - return allocmem(size); -} + if(s2 == NULL) + { + return NULL; + } -inline void operator delete (void* ptr) { - freemem(ptr); + memcpy(s2, s1, --len); + return s2; } - -inline void operator delete[] (void* ptr) { - freemem(ptr); -} -#endif diff --git a/mem.h b/mem.h index bbb42b8..8d41b58 100644 --- a/mem.h +++ b/mem.h @@ -33,4 +33,7 @@ void* allocmem(size_t); void freemem(void*); void freeall(); +#define strdup(s) managed_strdup(s) +char *strdup(const char *s1); + #endif diff --git a/ntp_peer.c b/ntp_peer.c index 534c1ae..e7e00b7 100644 --- a/ntp_peer.c +++ b/ntp_peer.c @@ -32,6 +32,7 @@ #include #include "ntimed.h" +#include "mem.h" #include "udp.h" #include "ntp.h" #include "net_getaddrinfo.h" @@ -53,13 +54,9 @@ NTP_Peer_New(const char *hostname, const void *sa, unsigned salen) AN(np->sa); memcpy(np->sa, sa, np->sa_len); -// np->hostname = strdup(hostname); - np->hostname = allocmem(strlen(hostname) + 1); - memcpy(np->hostname, hostname, strlen(hostname) + 1); + np->hostname = strdup(hostname); AN(np->hostname); -// np->ip = strdup(hbuf); - np->ip = allocmem(strlen(hbuf) + 1); - memcpy(np->ip, hbuf, strlen(hbuf) + 1); + np->ip = strdup(hbuf); AN(np->ip); ALLOC_OBJ(np->tx_pkt, NTP_PACKET_MAGIC); diff --git a/ntp_peerset.c b/ntp_peerset.c index 42ac0ee..f7e99f0 100644 --- a/ntp_peerset.c +++ b/ntp_peerset.c @@ -45,6 +45,7 @@ #include #include "ntimed.h" +#include "mem.h" #include "ntp.h" #include "net_getaddrinfo.h" @@ -168,9 +169,7 @@ ntp_peerset_add_group(struct ntp_peerset *nps, const char *name) ALLOC_OBJ(ng, NTP_GROUP_MAGIC); AN(ng); -// ng->hostname = strdup(name); - ng->hostname = allocmem(strlen(name) + 1); - memcpy(ng->hostname, name, strlen(name) + 1); + ng->hostname = strdup(name); AN(ng->hostname); TAILQ_INSERT_TAIL(&nps->group, ng, list); nps->ngroup++; diff --git a/ocx_stdio.c b/ocx_stdio.c index 4a0c76f..63eea9e 100644 --- a/ocx_stdio.c +++ b/ocx_stdio.c @@ -65,8 +65,10 @@ #include #include +#include "ntimed_platform.h" #include "ntimed.h" +extern BOOL started_from_wb; int repeat_trace = 0; static FILE *debugfile = NULL; @@ -92,7 +94,6 @@ getdst(enum ocx_chan chan) void ArgTracefile(const char *fn) { - if (tracefile != NULL && tracefile != stdout) { AZ(fclose(tracefile)); tracefile = NULL; @@ -101,11 +102,6 @@ ArgTracefile(const char *fn) if (fn == NULL) return; - if (!strcmp(fn, "-")) { - tracefile = stdout; - return; - } - tracefile = fopen(fn, "w"); if (tracefile == NULL) Fail(NULL, 1, "Could not open '%s' for writing", fn); @@ -138,15 +134,18 @@ Put(struct ocx *ocx, enum ocx_chan chan, const char *fmt, ...) va_start(ap, fmt); dst = getdst(chan); - if (dst != NULL) { + if (dst != NULL && !(started_from_wb && dst == stdout)) { (void)vfprintf(dst, fmt, ap); fflush(dst); + } - if (repeat_trace && chan == OCX_TRACE) { - dst = getdst(OCX_DEBUG); - (void)vfprintf(dst, fmt, ap); - fflush(dst); - } + dst = getdst(OCX_DEBUG); + if ( + dst != NULL && + !(started_from_wb && dst == stdout) && + (repeat_trace && chan == OCX_TRACE)) { + (void)vfprintf(dst, fmt, ap); + fflush(dst); } va_end(ap);