Add vim modelines.
[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  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29
30 #include "text_object.h"
31 #include "logging.h"
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #define MAX_HEADTAIL_LINES 30
37 #define DEFAULT_MAX_HEADTAIL_USES 2
38
39 void tailstring(char *string, int endofstring, int wantedlines) {
40         int i, linescounted = 0;
41
42         string[endofstring] = 0;
43         if(endofstring > 0) {
44                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
45                         string[endofstring-1] = 0;
46                 }
47                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
48                         if(string[i] == '\n') {
49                                 linescounted++;
50                         }
51                 }
52                 if(i > 0) {
53                         strfold(string, i+2);
54                 }
55         }
56 }
57
58 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
59         unsigned int args;
60
61         if(arg) {
62                 obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
63                 obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
64                 args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
65                 if(args == 2 || args == 3) {
66                         if(obj->data.headtail.max_uses < 1) {
67                                 free(obj->data.headtail.logfile);
68                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
69                         }
70                         if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
71                                 to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
72                                 obj->data.headtail.buffer = NULL;
73                                 obj->data.headtail.current_use = 0;
74                         }else{
75                                 free(obj->data.headtail.logfile);
76                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
77                         }
78                 } else {
79                         free(obj->data.headtail.logfile);
80                         CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
81                 }
82         } else {
83                 CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
84         }
85 }
86
87 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
88         int fd, i, endofstring = 0, linescounted = 0;
89         FILE *fp;
90         struct stat st;
91
92         //empty the buffer and reset the counter if we used it the max number of times
93         if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
94                 free(obj->data.headtail.buffer);
95                 obj->data.headtail.buffer = NULL;
96                 obj->data.headtail.current_use = 0;
97         }
98         //use the buffer if possible
99         if(obj->data.headtail.buffer) {
100                 strcpy(p, obj->data.headtail.buffer);
101                 obj->data.headtail.current_use++;
102         }else{  //otherwise find the needed data
103                 if(stat(obj->data.headtail.logfile, &st) == 0) {
104                         if (S_ISFIFO(st.st_mode)) {
105                                 fd = open_fifo(obj->data.headtail.logfile, &obj->a);
106                                 if(fd != -1) {
107                                         if(strcmp(type, "head") == 0) {
108                                                 for(i = 0; linescounted < obj->data.headtail.wantedlines; i++) {
109                                                         read(fd, p + i, 1);
110                                                         if(p[i] == '\n') {
111                                                                 linescounted++;
112                                                         }
113                                                 }
114                                                 p[i] = 0;
115                                         } else if(strcmp(type, "tail") == 0) {
116                                                 i = read(fd, p, p_max_size - 1);
117                                                 tailstring(p, i, obj->data.headtail.wantedlines);
118                                         } else {
119                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
120                                         }
121                                 }
122                                 close(fd);
123                         } else {
124                                 fp = open_file(obj->data.headtail.logfile, &obj->a);
125                                 if(fp != NULL) {
126                                         if(strcmp(type, "head") == 0) {
127                                                 for(i = 0; i < obj->data.headtail.wantedlines; i++) {
128                                                         fgets(p + endofstring, p_max_size - endofstring, fp);
129                                                         endofstring = strlen(p);
130                                                 }
131                                         } else if(strcmp(type, "tail") == 0) {
132                                                 fseek(fp, - p_max_size, SEEK_END);
133                                                 i = fread(p, 1, p_max_size - 1, fp);
134                                                 tailstring(p, i, obj->data.headtail.wantedlines);
135                                         } else {
136                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
137                                         }
138                                         fclose(fp);
139                                 }
140                         }
141                         obj->data.headtail.buffer = strdup(p);
142                 } else {
143                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, obj->data.headtail.logfile);
144                 }
145         }
146         return;
147 }