prepare for the release of rrdtool-1.2.9
[rrdtool.git] / src / rrd_first.c
1 /*****************************************************************************
2  * RRDtool 1.2.9  Copyright by Tobi Oetiker, 1997-2005
3  *****************************************************************************
4  * rrd_first Return
5  *****************************************************************************
6  * Initial version by Burton Strauss, ntopSupport.com - 3/2005
7  *****************************************************************************/
8
9 #include "rrd_tool.h"
10
11
12 time_t
13 rrd_first(int argc, char **argv)
14 {
15     int target_rraindex=0;
16     char *endptr;
17     optind = 0; opterr = 0;  /* initialize getopt */
18
19     while (1){
20         static struct option long_options[] =
21         {
22             {"rraindex", required_argument, 0, 129},
23             {0,0,0,0}
24         };
25         int option_index = 0;
26         int opt;
27         optind = 0; opterr = 0;  /* initialize getopt */
28         opt = getopt_long(argc, argv, "", long_options, &option_index);
29
30         if(opt == EOF)
31             break;
32
33         switch(opt) {
34           case 129:
35             target_rraindex=strtol(optarg,&endptr,0);
36             if(target_rraindex < 0) {
37                 rrd_set_error("invalid rraindex number");
38                 return(-1);
39             }
40             break;
41           default:
42             rrd_set_error("usage rrdtool %s [--rraindex number] file.rrd", argv[0]);
43             return(-1);
44         }
45     }
46
47     if(optind >= argc){
48         rrd_set_error("not enough arguments");
49         return -1;       
50     }     
51
52     return(rrd_first_r(argv[optind], target_rraindex));
53 }
54
55
56 time_t
57 rrd_first_r(const char *filename, const int rraindex)
58 {
59     long rra_start,
60          timer;
61     time_t then;
62     FILE *in_file;
63     rrd_t rrd;
64
65     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1){
66         rrd_set_error("could not open RRD");
67         return(-1);
68     }
69
70     if((rraindex < 0) || (rraindex >= (int)rrd.stat_head->rra_cnt)) {
71         rrd_set_error("invalid rraindex number");
72         return(-1);
73     }
74
75     rra_start = ftell(in_file);    
76     fseek(in_file,
77           (rra_start +
78            (rrd.rra_ptr[rraindex].cur_row+1) *
79            rrd.stat_head->ds_cnt *
80            sizeof(rrd_value_t)),
81           SEEK_SET);
82     timer = - (rrd.rra_def[rraindex].row_cnt-1);
83     if (rrd.rra_ptr[rraindex].cur_row + 1 > rrd.rra_def[rraindex].row_cnt) {
84       fseek(in_file,rra_start,SEEK_SET);
85     }
86     then = (rrd.live_head->last_up -
87             rrd.live_head->last_up %
88             (rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step)) +
89            (timer * 
90             rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step);
91
92     rrd_free(&rrd);
93     fclose(in_file);
94     return(then);
95 }
96
97
98
99