Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / ksysguard / ksysguardd / NetBSD / logfile.c
blob47798d53daa710d25efe7fbc55b385831b51d553
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of version 2 of the GNU General Public
8 License as published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <config-workspace.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "Command.h"
28 #include "ccont.h"
29 #include "conf.h"
30 #include "ksysguardd.h"
31 #include "logfile.h"
33 static CONTAINER LogFiles = 0;
34 static unsigned long counter = 1;
36 typedef struct {
37 char name[256];
38 FILE* fh;
39 unsigned long id;
40 } LogFileEntry;
42 extern CONTAINER LogFileList;
45 ================================ public part =================================
48 void initLogFile(struct SensorModul* sm)
50 char monitor[1024];
51 ConfigLogFile *entry;
53 registerCommand("logfile_register", registerLogFile);
54 registerCommand("logfile_unregister", unregisterLogFile);
55 registerCommand("logfile_registered", printRegistered);
57 for (entry = first_ctnr(LogFileList); entry; entry = next_ctnr(LogFileList))
59 FILE* fp;
61 /* register the log file if we can actually read the file. */
62 if ((fp = fopen(entry->path, "r")) != NULL)
64 fclose(fp);
65 snprintf(monitor, 1024, "logfiles/%s", entry->name);
66 registerMonitor(monitor, "logfile", printLogFile,
67 printLogFileInfo, sm);
71 LogFiles = new_ctnr();
74 void exitLogFile(void)
76 destr_ctnr(LogFiles, free);
79 void printLogFile(const char* cmd)
81 char line[1024];
82 unsigned long id;
83 int i;
84 char ch;
85 LogFileEntry *entry;
87 sscanf(cmd, "%*s %lu", &id);
89 for (entry = first_ctnr(LogFiles); entry; entry = next_ctnr(LogFiles)) {
90 if (entry->id == id) {
91 while (fgets(line, sizeof(line), entry->fh) != NULL) {
92 fprintf(CurrentClient, "%s", line);
94 clearerr(entry->fh);
98 fprintf(CurrentClient, "\n");
101 void printLogFileInfo(const char* cmd)
103 fprintf(CurrentClient, "LogFile\n");
106 void registerLogFile(const char* cmd)
108 char name[257];
109 FILE* file;
110 LogFileEntry *entry;
111 ConfigLogFile *conf;
113 memset(name, 0, sizeof(name));
114 sscanf(cmd, "%*s %256s", name);
116 for (conf = first_ctnr(LogFileList); conf; conf = next_ctnr(LogFileList)) {
117 if (!strcmp(conf->name, name)) {
118 if ((file = fopen(conf->path, "r")) == NULL) {
119 print_error("fopen()");
120 fprintf(CurrentClient, "0\n");
121 return;
124 fseek(file, 0, SEEK_END);
126 if ((entry = (LogFileEntry *)malloc(sizeof(LogFileEntry))) == NULL) {
127 print_error("malloc()");
128 fprintf(CurrentClient, "0\n");
129 return;
132 entry->fh = file;
133 strlcpy(entry->name, conf->name, sizeof(entry->name));
134 entry->id = counter;
136 push_ctnr(LogFiles, entry);
138 fprintf(CurrentClient, "%lu\n", counter);
139 counter++;
141 return;
145 fprintf(CurrentClient, "0\n");
148 void unregisterLogFile(const char* cmd)
150 unsigned long id;
151 LogFileEntry *entry;
153 sscanf(cmd, "%*s %lu", &id);
155 for (entry = first_ctnr(LogFiles); entry; entry = next_ctnr(LogFiles)) {
156 if (entry->id == id) {
157 fclose(entry->fh);
158 free(remove_ctnr(LogFiles));
159 fprintf(CurrentClient, "\n");
160 return;
164 fprintf(CurrentClient, "\n");
167 void printRegistered(const char* cmd)
169 LogFileEntry *entry;
171 for (entry = first_ctnr(LogFiles); entry; entry = next_ctnr(LogFiles))
172 fprintf(CurrentClient, "%s:%lu\n", entry->name, entry->id);
174 fprintf(CurrentClient, "\n");