prepare for the release of rrdtool-1.2.17
[rrdtool.git] / src / rrd_first.c
1 /*****************************************************************************
2  * RRDtool 1.2.17  Copyright by Tobi Oetiker, 1997-2006
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         rrd_free(&rrd);
72         fclose(in_file);
73         return(-1);
74     }
75
76     rra_start = ftell(in_file);    
77     fseek(in_file,
78           (rra_start +
79            (rrd.rra_ptr[rraindex].cur_row+1) *
80            rrd.stat_head->ds_cnt *
81            sizeof(rrd_value_t)),
82           SEEK_SET);
83     timer = - (rrd.rra_def[rraindex].row_cnt-1);
84     if (rrd.rra_ptr[rraindex].cur_row + 1 > rrd.rra_def[rraindex].row_cnt) {
85       fseek(in_file,rra_start,SEEK_SET);
86     }
87     then = (rrd.live_head->last_up -
88             rrd.live_head->last_up %
89             (rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step)) +
90            (timer * 
91             rrd.rra_def[rraindex].pdp_cnt*rrd.stat_head->pdp_step);
92
93     rrd_free(&rrd);
94     fclose(in_file);
95     return(then);
96 }
97
98
99
100