Bugfixing and cleanup

This commit is contained in:
llsth 2015-04-15 23:52:40 +02:00
parent 620823b985
commit b705164771
8 changed files with 68 additions and 52 deletions

View File

@ -31,8 +31,8 @@
#include "ntp.h" #include "ntp.h"
#include "udp.h" #include "udp.h"
#define ARGSFORMAT "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,QUIET/S" #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-client 1.0a"; const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)";
#define PARAM_CLIENT PARAM_INSTANCE #define PARAM_CLIENT PARAM_INSTANCE
#define PARAM_TABLE_NAME client_param_table #define PARAM_TABLE_NAME client_param_table
@ -60,6 +60,7 @@ struct ntimedargs {
char **servers; char **servers;
long synchronize; long synchronize;
long save; long save;
long show;
long quiet; long quiet;
}; };
@ -71,22 +72,37 @@ int main(int argc, char **argv)
{ {
struct ntimedargs args = { struct ntimedargs args = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
FALSE, FALSE, FALSE FALSE, FALSE, FALSE, FALSE
}; };
started_from_wb = (BOOL)argc; started_from_wb = (argc == 0);
atexit(clean_exit); atexit(clean_exit);
rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); if(started_from_wb) {
if (!rdargs) args.servers = (char *[]) {
{ "0.pool.ntp.org",
PrintFault(IoErr(), (ARGPTR)argv[0]); "1.pool.ntp.org",
exit(2); "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) { if (!args.quiet) {
EnableDebug(); EnableDebug();
if (args.show) {
EnableTraceDebug();
}
} }
init_libs(&args); init_libs(&args);

View File

@ -31,8 +31,8 @@
#include "ntp.h" #include "ntp.h"
#include "udp.h" #include "udp.h"
#define ARGSFORMAT "D=DURATION/N,M=MONITOR/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,QUIET/S" #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 1.0a"; const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)";
void Time_Amiga_Passive(void); void Time_Amiga_Passive(void);
@ -50,6 +50,7 @@ struct ntimedargs {
char *tracefile; char *tracefile;
char *timezone; char *timezone;
char **servers; char **servers;
long show;
long quiet; long quiet;
}; };
@ -59,7 +60,7 @@ static void clean_exit();
int main(int argc, char **argv) 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; started_from_wb = (BOOL)argc;
@ -74,7 +75,10 @@ int main(int argc, char **argv)
if (!args.quiet) { if (!args.quiet) {
EnableDebug(); EnableDebug();
EnableTraceDebug();
if (args.show) {
EnableTraceDebug();
}
} }
init_libs(&args); init_libs(&args);
@ -98,13 +102,13 @@ static void init_libs(struct ntimedargs *args)
#ifdef AROS #ifdef AROS
if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) { if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) {
Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n"); Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n");
exit(2); exit(5);
} }
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno, if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno,
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) { SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) {
Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n"); Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n");
exit(2); exit(5);
} }
#endif #endif
} }

View File

@ -26,12 +26,13 @@
*/ */
#include "ntimed_platform.h" #include "ntimed_platform.h"
#include "mem.h"
#include "ntimed.h" #include "ntimed.h"
#include "ntp.h" #include "ntp.h"
#include "udp.h" #include "udp.h"
#define ARGSFORMAT "P=PARAM/K,B=BUMP/K,T=TRACEFILE/K,SIMFILE/A,QUIET/S" #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_CLIENT PARAM_INSTANCE
#define PARAM_TABLE_NAME client_param_table #define PARAM_TABLE_NAME client_param_table

25
mem.c
View File

@ -184,20 +184,17 @@ void deallocerror(char *descr, void *p)
Debug(NULL, "Mememory deallocation error (%s) address (%x)\n", descr, p); Debug(NULL, "Mememory deallocation error (%s) address (%x)\n", descr, p);
} }
#ifdef __c_plus char *strdup(const char *s1)
inline void* operator new (size_t size) { {
return allocmem(size); char *s2;
} size_t len = strlen(s1);
s2 = allocmem(++len);
inline void* operator new[] (size_t size) { if(s2 == NULL)
return allocmem(size); {
} return NULL;
}
inline void operator delete (void* ptr) { memcpy(s2, s1, --len);
freemem(ptr); return s2;
} }
inline void operator delete[] (void* ptr) {
freemem(ptr);
}
#endif

3
mem.h
View File

@ -33,4 +33,7 @@ void* allocmem(size_t);
void freemem(void*); void freemem(void*);
void freeall(); void freeall();
#define strdup(s) managed_strdup(s)
char *strdup(const char *s1);
#endif #endif

View File

@ -32,6 +32,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include "ntimed.h" #include "ntimed.h"
#include "mem.h"
#include "udp.h" #include "udp.h"
#include "ntp.h" #include "ntp.h"
#include "net_getaddrinfo.h" #include "net_getaddrinfo.h"
@ -53,13 +54,9 @@ NTP_Peer_New(const char *hostname, const void *sa, unsigned salen)
AN(np->sa); AN(np->sa);
memcpy(np->sa, sa, np->sa_len); memcpy(np->sa, sa, np->sa_len);
// np->hostname = strdup(hostname); np->hostname = strdup(hostname);
np->hostname = allocmem(strlen(hostname) + 1);
memcpy(np->hostname, hostname, strlen(hostname) + 1);
AN(np->hostname); AN(np->hostname);
// np->ip = strdup(hbuf); np->ip = strdup(hbuf);
np->ip = allocmem(strlen(hbuf) + 1);
memcpy(np->ip, hbuf, strlen(hbuf) + 1);
AN(np->ip); AN(np->ip);
ALLOC_OBJ(np->tx_pkt, NTP_PACKET_MAGIC); ALLOC_OBJ(np->tx_pkt, NTP_PACKET_MAGIC);

View File

@ -45,6 +45,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include "ntimed.h" #include "ntimed.h"
#include "mem.h"
#include "ntp.h" #include "ntp.h"
#include "net_getaddrinfo.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); ALLOC_OBJ(ng, NTP_GROUP_MAGIC);
AN(ng); AN(ng);
// ng->hostname = strdup(name); ng->hostname = strdup(name);
ng->hostname = allocmem(strlen(name) + 1);
memcpy(ng->hostname, name, strlen(name) + 1);
AN(ng->hostname); AN(ng->hostname);
TAILQ_INSERT_TAIL(&nps->group, ng, list); TAILQ_INSERT_TAIL(&nps->group, ng, list);
nps->ngroup++; nps->ngroup++;

View File

@ -65,8 +65,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "ntimed_platform.h"
#include "ntimed.h" #include "ntimed.h"
extern BOOL started_from_wb;
int repeat_trace = 0; int repeat_trace = 0;
static FILE *debugfile = NULL; static FILE *debugfile = NULL;
@ -92,7 +94,6 @@ getdst(enum ocx_chan chan)
void void
ArgTracefile(const char *fn) ArgTracefile(const char *fn)
{ {
if (tracefile != NULL && tracefile != stdout) { if (tracefile != NULL && tracefile != stdout) {
AZ(fclose(tracefile)); AZ(fclose(tracefile));
tracefile = NULL; tracefile = NULL;
@ -101,11 +102,6 @@ ArgTracefile(const char *fn)
if (fn == NULL) if (fn == NULL)
return; return;
if (!strcmp(fn, "-")) {
tracefile = stdout;
return;
}
tracefile = fopen(fn, "w"); tracefile = fopen(fn, "w");
if (tracefile == NULL) if (tracefile == NULL)
Fail(NULL, 1, "Could not open '%s' for writing", fn); 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); va_start(ap, fmt);
dst = getdst(chan); dst = getdst(chan);
if (dst != NULL) { if (dst != NULL && !(started_from_wb && dst == stdout)) {
(void)vfprintf(dst, fmt, ap); (void)vfprintf(dst, fmt, ap);
fflush(dst); fflush(dst);
}
if (repeat_trace && chan == OCX_TRACE) { dst = getdst(OCX_DEBUG);
dst = getdst(OCX_DEBUG); if (
(void)vfprintf(dst, fmt, ap); dst != NULL &&
fflush(dst); !(started_from_wb && dst == stdout) &&
} (repeat_trace && chan == OCX_TRACE)) {
(void)vfprintf(dst, fmt, ap);
fflush(dst);
} }
va_end(ap); va_end(ap);