Fix bug with SF id 2808272 (tail and head)
[monky] / src / tailhead.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "text_object.h"
29 #include "logging.h"
30
31 #define MAX_HEADTAIL_LINES 30
32 #define DEFAULT_MAX_HEADTAIL_USES 2
33
34 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
35         unsigned int args;
36
37         if(arg) {
38                 obj->data.headtail.logfile=malloc(strlen(arg));
39                 obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
40                 args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
41                 if(args == 2 || args == 3) {
42                         if(obj->data.headtail.max_uses < 1) {
43                                 free(obj->data.headtail.logfile);
44                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
45                         }
46                         if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
47                                 to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
48                                 obj->data.headtail.buffer = NULL;
49                                 obj->data.headtail.current_use = 0;
50                         }else{
51                                 free(obj->data.headtail.logfile);
52                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
53                         }
54                 } else {
55                         free(obj->data.headtail.logfile);
56                         CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
57                 }
58         } else {
59                 CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
60         }
61 }
62
63 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
64         int i, endofstring = 0, linescounted = 0;
65         FILE *fp;
66
67         if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
68                 free(obj->data.headtail.buffer);
69                 obj->data.headtail.buffer = NULL;
70                 obj->data.headtail.current_use = 0;
71         }
72         if(obj->data.headtail.buffer) {
73                 strcpy(p, obj->data.headtail.buffer);
74                 obj->data.headtail.current_use++;
75         }else{
76                 fp = open_file(obj->data.headtail.logfile, &obj->a);
77                 if(fp != NULL) {
78                         if(strcmp(type,"head") == 0) {
79                                 for(i = 0; i < obj->data.headtail.wantedlines; i++) {
80                                         fgets(p + endofstring, p_max_size - endofstring, fp);
81                                         endofstring = strlen(p);
82                                 }
83                         } else if(strcmp(type,"tail") == 0) {
84                                 fseek(fp, - p_max_size, SEEK_END);
85                                 fread(p, 1, p_max_size, fp);
86                                 p[p_max_size - 1] = 0;
87                                 if(p[strlen(p)-1] == '\n') {    //work with or without \n at end of file
88                                         p[strlen(p)-1] = 0;
89                                 }
90                                 for(i = strlen(p); i >= 0 && linescounted < obj->data.headtail.wantedlines; i--) {
91                                         if(p[i] == '\n') {
92                                                 linescounted++;
93                                         }
94                                 }
95                                 if(i > 0) {
96                                         strfold(p, i+2);
97                                 }
98                         } else {
99                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
100                         }
101                         fclose(fp);
102                         obj->data.headtail.buffer = strdup(p);
103                 }
104         }
105         return;
106 }