1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
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.
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/>.
28 #include "text_object.h"
34 #define MAX_HEADTAIL_LINES 30
35 #define DEFAULT_MAX_HEADTAIL_USES 2
37 void tailstring(char *string, int endofstring, int wantedlines) {
38 int i, linescounted = 0;
40 string[endofstring] = 0;
42 if(string[endofstring-1] == '\n') { //work with or without \n at end of file
43 string[endofstring-1] = 0;
45 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
46 if(string[i] == '\n') {
56 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
60 obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
61 obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
62 args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
63 if(args == 2 || args == 3) {
64 if(obj->data.headtail.max_uses < 1) {
65 free(obj->data.headtail.logfile);
66 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
68 if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
69 to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
70 obj->data.headtail.buffer = NULL;
71 obj->data.headtail.current_use = 0;
73 free(obj->data.headtail.logfile);
74 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
77 free(obj->data.headtail.logfile);
78 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
81 CRIT_ERR(obj, free_at_crash, "%s needs arguments", type);
85 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
86 int fd, i, endofstring = 0, linescounted = 0;
90 //empty the buffer and reset the counter if we used it the max number of times
91 if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
92 free(obj->data.headtail.buffer);
93 obj->data.headtail.buffer = NULL;
94 obj->data.headtail.current_use = 0;
96 //use the buffer if possible
97 if(obj->data.headtail.buffer) {
98 strcpy(p, obj->data.headtail.buffer);
99 obj->data.headtail.current_use++;
100 }else{ //otherwise find the needed data
101 if(stat(obj->data.headtail.logfile, &st) == 0) {
102 if (S_ISFIFO(st.st_mode)) {
103 fd = open_fifo(obj->data.headtail.logfile, &obj->a);
105 if(strcmp(type, "head") == 0) {
106 for(i = 0; linescounted < obj->data.headtail.wantedlines; i++) {
113 } else if(strcmp(type, "tail") == 0) {
114 i = read(fd, p, p_max_size - 1);
115 tailstring(p, i, obj->data.headtail.wantedlines);
117 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
122 fp = open_file(obj->data.headtail.logfile, &obj->a);
124 if(strcmp(type, "head") == 0) {
125 for(i = 0; i < obj->data.headtail.wantedlines; i++) {
126 fgets(p + endofstring, p_max_size - endofstring, fp);
127 endofstring = strlen(p);
129 } else if(strcmp(type, "tail") == 0) {
130 fseek(fp, - p_max_size, SEEK_END);
131 i = fread(p, 1, p_max_size - 1, fp);
132 tailstring(p, i, obj->data.headtail.wantedlines);
134 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
139 obj->data.headtail.buffer = strdup(p);
141 CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, obj->data.headtail.logfile);