From f7192b57b6e7b9622bfadae163af6a6711c8d245 Mon Sep 17 00:00:00 2001 From: axis6404 Date: Wed, 2 Jul 2025 00:46:25 +0900 Subject: [PATCH] Add default server If no ntp server address is specified in the argument, the hard-coded ntp default server is used. --- ntpc.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ntpc.c b/ntpc.c index c5caf28..cb777ed 100644 --- a/ntpc.c +++ b/ntpc.c @@ -155,13 +155,14 @@ int main(int argc, char **argv) struct in_addr ia; const char *s; int i; + const char *cDefaultNtpServer = "1.jp.pool.ntp.org"; for (i = 1; i < argc; ++i) { if ((he = gethostbyname(argv[i])) != NULL) { memcpy(&ia, he->h_addr_list[0], sizeof(ia)); s = inet_ntoa(ia); - if (strcmp(s, argv[i]) == 0) printf("Trying %s: ", s); - else printf("Trying %s [%s]:", argv[i], s); + if (strcmp(s, argv[i]) == 0) printf("Trying %s: \n", s); + else printf("Trying %s [%s]:\n", argv[i], s); if (ntpc(ia) == 0) return 0; } else { @@ -171,7 +172,19 @@ int main(int argc, char **argv) if (argc < 2) { printf("Usage: ntpc \n"); - printf("Example: ntpc 0.asia.pool.ntp.org 1.asia.pool.ntp.org 2.asia.pool.ntp.org\n"); + printf("Example: ntpc 0.asia.pool.ntp.org 1.asia.pool.ntp.org 2.asia.pool.ntp.org\n\n"); + printf("Use default ntp server: %s\n\n", cDefaultNtpServer); + + if ((he = gethostbyname(cDefaultNtpServer)) != NULL) { + memcpy(&ia, he->h_addr_list[0], sizeof(ia)); + s = inet_ntoa(ia); + if (strcmp(s, cDefaultNtpServer) == 0) printf("Trying %s: \n", s); + else printf("Trying %s [%s]:\n", cDefaultNtpServer, s); + if (ntpc(ia) == 0) return 0; + } + else { + printf("Unable to resolve: %s\n", cDefaultNtpServer); + } } return 1;