sts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Nov 2010 04:05:14 +0100
changeset 153 b223089872b6
parent 145 63a5269fb113
child 156 2898369b1cdc
permissions -rw-r--r--
ipv6 support added committer: Markus Bröker <mbroeker@largo.homelinux.org>
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 <time.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
#include <errno.h>
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
void usage (char *name)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
    printf ("Usage: %s STAMP [BOOT-STAMP]\n", name);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    15
    exit (EXIT_FAILURE);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    16
}
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
time_t parse (char *buf)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    19
{
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
    time_t sec = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    21
    time_t milli = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    22
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
    if (sscanf (buf, "%ld.%ld", &sec, &milli) != 2) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    24
        fprintf (stderr, "  sec: %ld, ", sec);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    25
        fprintf (stderr, "milli: %ld\n", milli);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    26
    }
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    27
145
63a5269fb113 removed code without effect
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 144
diff changeset
    28
    return sec;
144
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
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    31
time_t getboottime ()
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
    char buf[80];
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    34
    time_t now = time (NULL);
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
    FILE *f = fopen ("/proc/uptime", "r");
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    37
    if (fgets (buf, sizeof (buf), f) == NULL) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
        perror ("FOPEN");
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    39
        exit (errno);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    40
    }
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
    fclose (f);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    43
    return now - parse (buf);
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
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    46
int main (int argc, char **argv)
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
    time_t boot;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    49
    time_t stamp = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    50
    time_t diff;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    51
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    52
    if (argc < 2)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    53
        usage (argv[0]);
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
    stamp = parse (argv[1]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    56
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    57
    if (argc == 3) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    58
        boot = parse (argv[2]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    59
    } else
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    60
        boot = getboottime ();
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    61
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    62
    diff = boot + stamp;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    63
    printf ("%s: %s", argv[1], ctime (&diff));
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
    return EXIT_SUCCESS;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    66
}