|
a |
|
b/HRV.src/seconds.c |
|
|
1 |
/* seconds.c Joe Mietus Oct 7 2008 */ |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
seconds : |
|
|
5 |
Usage: seconds [[hh]:mm]:ss | - |
|
|
6 |
Convert string in [[hh:]mm:]ss to seconds |
|
|
7 |
`-' to read stdin |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
#include <stdio.h> |
|
|
11 |
#include <stdlib.h> |
|
|
12 |
|
|
|
13 |
main(argc, argv) |
|
|
14 |
int argc; |
|
|
15 |
char *argv[]; |
|
|
16 |
{ |
|
|
17 |
char *time[12]; |
|
|
18 |
long strtim(); |
|
|
19 |
|
|
|
20 |
if (argc < 2) { |
|
|
21 |
usage(argv[0]); |
|
|
22 |
exit(1); |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
if (*argv[1] == '-') |
|
|
26 |
switch (argv[1][1]) { |
|
|
27 |
case '\0' : while (scanf("%s", time) == 1) |
|
|
28 |
printf("%ld\n", strtim(time)); |
|
|
29 |
exit(0); |
|
|
30 |
default : usage(argv[0]); |
|
|
31 |
exit(1); |
|
|
32 |
} |
|
|
33 |
else |
|
|
34 |
printf("%ld\n", strtim(argv[1])); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
long strtim(buf) /* convert string in [[hh:]mm:]ss to seconds */ |
|
|
39 |
char *buf; |
|
|
40 |
{ |
|
|
41 |
long x, y, z; |
|
|
42 |
|
|
|
43 |
switch (sscanf(buf, "%ld:%ld:%ld", &x, &y, &z)) { |
|
|
44 |
case 1: return (x); |
|
|
45 |
case 2: return (60*x + y); |
|
|
46 |
case 3: return (3600*x + 60*y + z); |
|
|
47 |
default: return (-1L); |
|
|
48 |
} |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
usage(prog) |
|
|
53 |
char *prog; |
|
|
54 |
{ |
|
|
55 |
fprintf(stderr, "Usage: %s [[hh]:mm]:ss | -\n", prog); |
|
|
56 |
fprintf(stderr, " Convert string in [[hh:]mm:]ss to seconds\n"); |
|
|
57 |
fprintf(stderr, " `-' to read stdin\n"); |
|
|
58 |
} |