rrdtool is assuming that rrd_xport will always return -1 on failure;
[rrdtool.git] / src / rrd_last.c
1 /*****************************************************************************
2  * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
3  *****************************************************************************
4  * rrd_last.c
5  *****************************************************************************
6  * Initial version by Russ Wright, @Home Network, 9/28/98
7  *****************************************************************************/
8
9 #include "rrd_tool.h"
10 #include "rrd_client.h"
11
12 time_t rrd_last(
13     int argc,
14     char **argv)
15 {
16     char *opt_daemon = NULL;
17     time_t lastupdate;
18
19     optind = 0;
20     opterr = 0;         /* initialize getopt */
21
22     while (42) {
23         int       opt;
24         int       option_index = 0;
25         static struct option long_options[] = {
26             {"daemon", required_argument, 0, 'd'},
27             {0, 0, 0, 0}
28         };
29
30         opt = getopt_long(argc, argv, "d:", long_options, &option_index);
31
32         if (opt == EOF)
33             break;
34
35         switch (opt) {
36         case 'd':
37             if (opt_daemon != NULL)
38                     free (opt_daemon);
39             opt_daemon = strdup (optarg);
40             if (opt_daemon == NULL)
41             {
42                 rrd_set_error ("strdup failed.");
43                 return (-1);
44             }
45             break;
46
47         default:
48             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
49                     argv[0]);
50             return (-1);
51             break;
52         }
53     }                   /* while (42) */
54
55     if ((argc - optind) != 1) {
56         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
57                 argv[0]);
58         return (-1);
59     }
60
61     rrdc_connect (opt_daemon);
62     if (rrdc_is_connected (opt_daemon))
63         lastupdate = rrdc_last (argv[optind]);
64
65     else
66         lastupdate = rrd_last_r(argv[optind]);
67
68     if (opt_daemon) free(opt_daemon);
69     return (lastupdate);
70 }
71
72 time_t rrd_last_r(
73     const char *filename)
74 {
75     time_t    lastup = -1;
76     rrd_file_t *rrd_file;
77
78     rrd_t     rrd;
79
80     rrd_init(&rrd);
81     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
82     if (rrd_file != NULL) {
83         lastup = rrd.live_head->last_up;
84         rrd_close(rrd_file);
85     }
86     rrd_free(&rrd);
87     return (lastup);
88 }