home
/
code
/
khatus.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Move config defaults from global into main
[khatus.git]
/
x5
/
khatus_sensor_time.c
diff --git
a/x5/khatus_sensor_time.c
b/x5/khatus_sensor_time.c
index
f156f45
..
01483b9
100644
(file)
--- a/
x5/khatus_sensor_time.c
+++ b/
x5/khatus_sensor_time.c
@@
-1,5
+1,7
@@
+#include <assert.h>
+#include <ctype.h>
#include <errno.h>
#include <errno.h>
-#include <s
tdarg
.h>
+#include <s
ignal
.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@
-9,10
+11,18
@@
#include "khatus_lib_log.h"
#include "khatus_lib_time.h"
#include "khatus_lib_log.h"
#include "khatus_lib_time.h"
-char *argv0;
+#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
+
+#define MAX_LEN 20
+#define END_OF_MESSAGE '\n'
+
+char *argv0 = NULL;
+
+double opt_interval = 1.0;
+char *opt_fmt = "%a %b %d %H:%M:%S";
void
void
-usage()
+
print_
usage()
{
printf(
"%s: [OPT ...]\n"
{
printf(
"%s: [OPT ...]\n"
@@
-21,23
+31,13
@@
usage()
" | -f string # format string\n"
" | -h # help message (i.e. what you're reading now :) )\n",
argv0);
" | -f string # format string\n"
" | -h # help message (i.e. what you're reading now :) )\n",
argv0);
- fatal("usage\n");
}
}
-int
-
main
(int argc, char **argv)
+void
+
opt_parse
(int argc, char **argv)
{
{
- argv0 = argv[0];
-
- double opt_interval = 1.0;
- char *opt_fmt = "%a %b %d %H:%M:%S";
-
- time_t t;
- struct timespec ti;
- char buf[128];
char c;
char c;
- memset(buf, '\0', 128);
while ((c = getopt(argc, argv, "f:i:h")) != -1)
switch (c) {
case 'f':
while ((c = getopt(argc, argv, "f:i:h")) != -1)
switch (c) {
case 'f':
@@
-48,15
+48,47
@@
main(int argc, char **argv)
opt_interval = atof(optarg);
break;
case 'h':
opt_interval = atof(optarg);
break;
case 'h':
- usage();
- break;
+ print_usage();
+ exit(EXIT_SUCCESS);
+ case '?':
+ if (optopt == 'f' || optopt == 'i')
+ fprintf(stderr,
+ "Option -%c requires an argument.\n",
+ optopt);
+ else if (isprint(optopt))
+ fprintf (stderr,
+ "Unknown option `-%c'.\n",
+ optopt);
+ else
+ fprintf(stderr,
+ "Unknown option character `\\x%x'.\n",
+ optopt);
+ exit(EXIT_FAILURE);
default:
default:
-
usage(
);
+
assert(0
);
}
}
+}
+
+int
+main(int argc, char **argv)
+{
+ argv0 = argv[0];
+
+ time_t t;
+ struct timespec ti;
+ char buf[MAX_LEN];
+
+ opt_parse(argc, argv);
ti = timespec_of_float(opt_interval);
ti = timespec_of_float(opt_interval);
+ debug("opt_fmt: \"%s\"\n", opt_fmt);
+ debug("opt_interval: %f\n", opt_interval);
+ debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n", ti.tv_sec, ti.tv_nsec);
+ memset(buf, '\0', MAX_LEN);
+ signal(SIGPIPE, SIG_IGN);
+
for (;;) {
t = time(NULL);
for (;;) {
t = time(NULL);
- strftime(buf,
sizeof(buf)
, opt_fmt, localtime(&t));
+ strftime(buf,
MAX_LEN
, opt_fmt, localtime(&t));
puts(buf);
fflush(stdout);
snooze(&ti);
puts(buf);
fflush(stdout);
snooze(&ti);
This page took
0.042928 seconds
and
4
git commands to generate.