variables should be defined at the bein of the block ... else we loose portability!
[rrdtool.git] / src / rrd_first.c
1 /*****************************************************************************
2  * RRDtool 1.2.12  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         opt = getopt_long(argc, argv, "", long_options, &option_index);
28
29         if(opt == EOF)
30             break;
31
32         switch(opt) {
33           case 129:
34             target_rraindex=strtol(optarg,&endptr,0);
35             if(target_rraindex < 0) {
36                 rrd_set_error("invalid rraindex number");
37                 return(-1);
38             }
39             break;
40           default:
41             rrd_set_error("usage rrdtool %s [--rraindex number] file.rrd", argv[0]);
42             return(-1);
43         }
44     }
45
46     if(optind >= argc){
47         rrd_set_error("not enough arguments");
48         return -1;       
49     }     
50
51     return(rrd_first_r(argv[optind], target_rraindex));
52 }
53
54
55 time_t
56 rrd_first_r(const char *filename, const int rraindex)
57 {
58     long rra_start,
59          timer;
60     time_t then;
61     FILE *in_file;
62     rrd_t rrd;
63
64     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1){
65         rrd_set_error("could not open RRD");
66         return(-1);
67     }
68
69     if((rraindex < 0) || (rraindex >= (int)rrd.stat_head->rra_cnt)) {
70         rrd_set_error("invalid rraindex number");
71         return(-1);
72     }
73
74     rra_start = ftell(in_file);    
75     fseek(in_file,
76           (rra_start +
77            (rrd.rra_ptr[rraindex].cur_row+1) *
78            rrd.stat_head->ds_cnt *
79            sizeof(rrd_value_t)),
80           SEEK_SET);
81     timer = - (rrd.rra_def[rraindex].row_cnt-1);
82     if (rrd.rra_ptr[rraindex].cur_row + 1 > rrd.rra_def[rraindex].row_cnt) {
83       fseek(in_file,rra_start,SEEK_SET);
84     }
85     then = (rrd.live_head->last_up -
86             rrd.live_head->last_up %
87             (rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step)) +
88            (timer * 
89             rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step);
90
91     rrd_free(&rrd);
92     fclose(in_file);
93     return(then);
94 }
95
96
97
98