sts.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 156 2898369b1cdc
permissions -rw-r--r--
Proper Datatypes to leave the 80's
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");
156
2898369b1cdc check for the linux-specific proc filesystem...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 145
diff changeset
    37
    if (f == NULL) {
2898369b1cdc check for the linux-specific proc filesystem...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 145
diff changeset
    38
        perror ("FOPEN");
2898369b1cdc check for the linux-specific proc filesystem...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 145
diff changeset
    39
        return EXIT_FAILURE;
2898369b1cdc check for the linux-specific proc filesystem...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 145
diff changeset
    40
    }
144
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
    if (fgets (buf, sizeof (buf), f) == NULL) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
        perror ("FOPEN");
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    43
        exit (errno);
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
    fclose (f);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    47
    return now - parse (buf);
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
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    50
int main (int argc, char **argv)
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
    time_t boot;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    53
    time_t stamp = 0;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    54
    time_t diff;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    55
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    56
    if (argc < 2)
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    57
        usage (argv[0]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    58
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    59
    stamp = parse (argv[1]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    60
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    61
    if (argc == 3) {
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    62
        boot = parse (argv[2]);
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    63
    } else
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    64
        boot = getboottime ();
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    65
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    66
    diff = boot + stamp;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    67
    printf ("%s: %s", argv[1], ctime (&diff));
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    68
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    69
    return EXIT_SUCCESS;
f064cd793f8d sts: show time stamps added
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
diff changeset
    70
}