all: add format strings
This commit is contained in:
10
README.md
10
README.md
@@ -14,13 +14,15 @@ If you still want to, `make install` will install this utility to
|
||||
## Usage
|
||||
|
||||
```
|
||||
Usage: bin/n [options]
|
||||
Usage: n [options]
|
||||
Options:
|
||||
-h HOST, --host=HOST NTP server to use
|
||||
-h HOST, --host=HOST NTP server to use
|
||||
defaults to 'europe.pool.ntp.org'
|
||||
-p PORT, --port=PORT NTP server port to use (must be numerical)
|
||||
-p PORT, --port=PORT NTP server port to use (must be numerical)
|
||||
defaults to '123'
|
||||
--help Display this message and exit
|
||||
-f FORMAT, --format=FORMAT Date format string (passed to strftime)
|
||||
defaults to standard of ctime
|
||||
--help Display this message and exit
|
||||
```
|
||||
|
||||
<hr/>
|
||||
|
38
main.c
38
main.c
@@ -45,23 +45,37 @@ typedef struct {
|
||||
typedef struct {
|
||||
const char* host;
|
||||
int port;
|
||||
const char* format;
|
||||
} ntp_info;
|
||||
|
||||
ntp_info NDEFAULT = {.host="europe.pool.ntp.org", .port = 123};
|
||||
ntp_info NDEFAULT = {.host="europe.pool.ntp.org", .port = 123, .format = NULL};
|
||||
|
||||
void err(char* msg) {
|
||||
char* nstrftime(const char* fmt, const struct tm* t) {
|
||||
int size = 40;
|
||||
char* res = malloc(size);
|
||||
int n;
|
||||
do {
|
||||
n = strftime(res, size-1, fmt, t);
|
||||
size *= 2;
|
||||
} while (!n);
|
||||
return res;
|
||||
}
|
||||
|
||||
void err(const char* msg) {
|
||||
fprintf(stderr, "Error %s.\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void usage(char* program, int code) {
|
||||
void usage(const char* program, int code) {
|
||||
fprintf(stderr, "Usage: %s [options]\n", program);
|
||||
fputs("Options:\n", stderr);
|
||||
fputs(" -h HOST, --host=HOST NTP server to use\n", stderr);
|
||||
fputs(" -h HOST, --host=HOST NTP server to use\n", stderr);
|
||||
fputs(" defaults to 'europe.pool.ntp.org'\n", stderr);
|
||||
fputs(" -p PORT, --port=PORT NTP server port to use (must be numerical)\n", stderr);
|
||||
fputs(" -p PORT, --port=PORT NTP server port to use (must be numerical)\n", stderr);
|
||||
fputs(" defaults to '123'\n", stderr);
|
||||
fputs(" --help Display this message and exit\n", stderr);
|
||||
fputs(" -f FORMAT, --format=FORMAT Date format string (passed to strftime)\n", stderr);
|
||||
fputs(" defaults to standard of ctime\n", stderr);
|
||||
fputs(" --help Display this message and exit\n", stderr);
|
||||
exit(code);
|
||||
}
|
||||
|
||||
@@ -71,12 +85,14 @@ ntp_info args(int argc, char** argv) {
|
||||
static struct option long_options[] = {
|
||||
{"host", required_argument, 0, 'h'},
|
||||
{"port", required_argument, 0, 'p'},
|
||||
{"format", required_argument, 0, 'f'},
|
||||
{"help", no_argument, 0, 'u'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
while ((c = getopt_long(argc, argv, "t:a:", long_options, 0)) != -1) {
|
||||
while ((c = getopt_long(argc, argv, "t:a:f:", long_options, 0)) != -1) {
|
||||
switch (c) {
|
||||
case 'h': res.host = optarg; break;
|
||||
case 'f': res.format = optarg; break;
|
||||
case 'p': {
|
||||
char* end;
|
||||
res.port = strtol(optarg, &end, 10);
|
||||
@@ -123,7 +139,13 @@ int main(int argc, char** argv) {
|
||||
|
||||
time_t tm = n.ttms - EPOCH;
|
||||
|
||||
fputs(ctime(&tm), stdout);
|
||||
if (!i.format) {
|
||||
fputs(ctime(&tm), stdout);
|
||||
} else {
|
||||
char* fmt = nstrftime(i.format, localtime(&tm));
|
||||
puts(fmt);
|
||||
free(fmt);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user