-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
49 lines (44 loc) · 824 Bytes
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "hardv.h"
void
syserr()
{
perror(progname);
exit(-1);
}
void
err(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
va_end(ap);
exit(-1);
}
time_t
elapsecs(char *buf)
{
unsigned hr, mi;
char sg, *s;
struct tm tm;
time_t ck;
s = buf;
if (!s) return 0;
while (isspace((unsigned char)*s)) s++;
if (!(s=strptime(s, "%Y-%m-%d %H:%M:%S", &tm)))
err("invalid time: %s", buf);
if (sscanf(s," %c%2u%2u",&sg,&hr,&mi) != 3)
err("invalid time: %s", buf);
ck = timegm(&tm);
if (sg=='+') ck -= mi*60+hr*3600LU;
else if (sg=='-') ck += mi*60+hr*3600LU;
else err("invalid time: %s", buf);
return ck;
}