Move vi modelines closer to the beginning, so they're more likely to be actually...
[monky] / src / tailhead.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "text_object.h"
32 #include "logging.h"
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #define MAX_HEADTAIL_LINES 30
38 #define DEFAULT_MAX_HEADTAIL_USES 2
39
40 void tailstring(char *string, int endofstring, int wantedlines) {
41         int i, linescounted = 0;
42
43         string[endofstring] = 0;
44         if(endofstring > 0) {
45                 if(string[endofstring-1] == '\n') {     //work with or without \n at end of file
46                         string[endofstring-1] = 0;
47                 }
48                 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
49                         if(string[i] == '\n') {
50                                 linescounted++;
51                         }
52                 }
53                 if(i > 0) {
54                         strfold(string, i+2);
55                 }
56         }
57 }
58
59 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
60         unsigned int args;
61
62         if(arg) {
63                 obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
64                 obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
65                 args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
66                 if(args == 2 || args == 3) {
67                         if(obj->data.headtail.max_uses < 1) {
68                                 free(obj->data.headtail.logfile);
69                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
70                         }
71                         if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
72                                 to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
73                                 obj->data.headtail.buffer = NULL;
74                                 obj->data.headtail.current_use = 0;
75                         }else{
76                                 free(obj->data.headtail.logfile);
77                                 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
78                         }
79                 } else {
80                         free(obj->data.headtail.logfile);
81                         CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
82                 }
83         } else {
84                 CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
85         }
86 }
87
88 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
89         int fd, i, endofstring = 0, linescounted = 0;
90         FILE *fp;
91         struct stat st;
92
93         //empty the buffer and reset the counter if we used it the max number of times
94         if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
95                 free(obj->data.headtail.buffer);
96                 obj->data.headtail.buffer = NULL;
97                 obj->data.headtail.current_use = 0;
98         }
99         //use the buffer if possible
100         if(obj->data.headtail.buffer) {
101                 strcpy(p, obj->data.headtail.buffer);
102                 obj->data.headtail.current_use++;
103         }else{  //otherwise find the needed data
104                 if(stat(obj->data.headtail.logfile, &st) == 0) {
105                         if (S_ISFIFO(st.st_mode)) {
106                                 fd = open_fifo(obj->data.headtail.logfile, &obj->a);
107                                 if(fd != -1) {
108                                         if(strcmp(type, "head") == 0) {
109                                                 for(i = 0; linescounted < obj->data.headtail.wantedlines; i++) {
110                                                         read(fd, p + i, 1);
111                                                         if(p[i] == '\n') {
112                                                                 linescounted++;
113                                                         }
114                                                 }
115                                                 p[i] = 0;
116                                         } else if(strcmp(type, "tail") == 0) {
117                                                 i = read(fd, p, p_max_size - 1);
118                                                 tailstring(p, i, obj->data.headtail.wantedlines);
119                                         } else {
120                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
121                                         }
122                                 }
123                                 close(fd);
124                         } else {
125                                 fp = open_file(obj->data.headtail.logfile, &obj->a);
126                                 if(fp != NULL) {
127                                         if(strcmp(type, "head") == 0) {
128                                                 for(i = 0; i < obj->data.headtail.wantedlines; i++) {
129                                                         fgets(p + endofstring, p_max_size - endofstring, fp);
130                                                         endofstring = strlen(p);
131                                                 }
132                                         } else if(strcmp(type, "tail") == 0) {
133                                                 fseek(fp, - p_max_size, SEEK_END);
134                                                 i = fread(p, 1, p_max_size - 1, fp);
135                                                 tailstring(p, i, obj->data.headtail.wantedlines);
136                                         } else {
137                                                 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
138                                         }
139                                         fclose(fp);
140                                 }
141                         }
142                         obj->data.headtail.buffer = strdup(p);
143                 } else {
144                         CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, obj->data.headtail.logfile);
145                 }
146         }
147         return;
148 }