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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
33 #include "text_object.h"
41 #define MAX_HEADTAIL_LINES 30
42 #define DEFAULT_MAX_HEADTAIL_USES 2
53 static void tailstring(char *string, int endofstring, int wantedlines) {
54 int i, linescounted = 0;
56 string[endofstring] = 0;
58 if(string[endofstring-1] == '\n') { //work with or without \n at end of file
59 string[endofstring-1] = 0;
61 for(i = endofstring - 1; i >= 0 && linescounted < wantedlines; i--) {
62 if(string[i] == '\n') {
72 void free_tailhead(struct text_object *obj)
74 struct headtail *ht = obj->data.opaque;
81 free(obj->data.opaque);
82 obj->data.opaque = NULL;
85 void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
89 ht = malloc(sizeof(struct headtail));
90 memset(ht, 0, sizeof(struct headtail));
92 ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
93 memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
95 ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
97 args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
98 if (args < 2 || args > 3) {
100 CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
102 if (ht->max_uses < 1) {
104 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
106 if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
107 to_real_path(ht->logfile, ht->logfile);
112 CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
114 obj->data.opaque = ht;
117 void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
118 int fd, i, endofstring = 0, linescounted = 0;
121 struct headtail *ht = obj->data.opaque;
126 //empty the buffer and reset the counter if we used it the max number of times
127 if(ht->buffer && ht->current_use >= ht->max_uses - 1) {
132 //use the buffer if possible
134 strcpy(p, ht->buffer);
136 }else{ //otherwise find the needed data
137 if(stat(ht->logfile, &st) == 0) {
138 if (S_ISFIFO(st.st_mode)) {
139 fd = open_fifo(ht->logfile, &ht->reported);
141 if(strcmp(type, "head") == 0) {
142 for(i = 0; linescounted < ht->wantedlines; i++) {
149 } else if(strcmp(type, "tail") == 0) {
150 i = read(fd, p, p_max_size - 1);
151 tailstring(p, i, ht->wantedlines);
153 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
158 fp = open_file(ht->logfile, &ht->reported);
160 if(strcmp(type, "head") == 0) {
161 for(i = 0; i < ht->wantedlines; i++) {
162 fgets(p + endofstring, p_max_size - endofstring, fp);
163 endofstring = strlen(p);
165 } else if(strcmp(type, "tail") == 0) {
166 fseek(fp, - p_max_size, SEEK_END);
167 i = fread(p, 1, p_max_size - 1, fp);
168 tailstring(p, i, ht->wantedlines);
170 CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
175 ht->buffer = strdup(p);
177 CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);
183 /* FIXME: use something more general (see also tail.c, head.c */
186 void print_lines(struct text_object *obj, char *p, int p_max_size)
189 FILE *fp = open_file(obj->data.s, &rep);
194 snprintf(p, p_max_size, "File Unreadable");
199 while(fgets(buf, BUFSZ, fp) != NULL){
200 for(j = 0; buf[j] != 0; j++) {
206 snprintf(p, p_max_size, "%d", lines);
210 void print_words(struct text_object *obj, char *p, int p_max_size)
213 FILE *fp = open_file(obj->data.s, &rep);
219 snprintf(p, p_max_size, "File Unreadable");
224 while(fgets(buf, BUFSZ, fp) != NULL){
225 for(j = 0; buf[j] != 0; j++) {
226 if(!isspace(buf[j])) {
236 snprintf(p, p_max_size, "%d", words);