sts.c
author Markus Brökers <mbroeker@largo.homelinux.org>
Sun, 29 Aug 2010 15:29:22 +0200
changeset 144 f064cd793f8d
child 145 63a5269fb113
permissions -rw-r--r--
sts: show time stamps added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
144
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/**
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 * sts.c: show time stamp
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 * Copyright (C) 2010 Markus Broeker
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
#include <stdio.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
#include <stdlib.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
#include <string.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
#include <math.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
#include <time.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
#include <errno.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
void usage (char *name)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    15
    printf ("Usage: %s STAMP [BOOT-STAMP]\n", name);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    16
    exit (EXIT_FAILURE);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    17
}
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    18
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    19
time_t parse (char *buf)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    21
    time_t sec = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    22
    time_t milli = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
    double value;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    24
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    25
    if (sscanf (buf, "%ld.%ld", &sec, &milli) != 2) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    26
        fprintf (stderr, "  sec: %ld, ", sec);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    27
        fprintf (stderr, "milli: %ld\n", milli);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    28
    }
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    29
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    30
    value = (milli / 1000000);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    31
    return sec + (time_t) (floor (value));
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    32
}
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    33
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    34
time_t getboottime ()
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    35
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    36
    char buf[80];
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    37
    time_t now = time (NULL);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    39
    FILE *f = fopen ("/proc/uptime", "r");
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    40
    if (fgets (buf, sizeof (buf), f) == NULL) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
        perror ("FOPEN");
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
        exit (errno);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    43
    }
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    44
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    45
    fclose (f);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    46
    return now - parse (buf);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    47
}
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    48
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    49
int main (int argc, char **argv)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    50
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    51
    time_t boot;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    52
    time_t stamp = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    53
    time_t diff;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    54
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    55
    if (argc < 2)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    56
        usage (argv[0]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    57
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    58
    stamp = parse (argv[1]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    59
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    60
    if (argc == 3) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    61
        boot = parse (argv[2]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    62
    } else
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    63
        boot = getboottime ();
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    64
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    65
    diff = boot + stamp;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    66
    printf ("%s: %s", argv[1], ctime (&diff));
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    67
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    68
    return EXIT_SUCCESS;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    69
}