6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details. You should have received a
15 * copy of the GNU General Public License along with this program; if
16 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17 * Suite 330, Boston, MA 02111-1307, USA.
21 #include <sys/select.h>
23 #include <sys/types.h>
32 #include "howmuchuseit.h"
41 struct context *g_ctx = NULL;
43 void sig_handler(int sig);
44 void now_not_working(struct context *ctx);
46 void now_working(struct context *ctx)
48 if (ctx->is_working == 0) {
49 ctx->start = time(NULL);
50 if (ctx->is_verbose) {
51 printf("now is working\n");
57 void now_not_working(struct context *ctx)
61 if (ctx->is_working == 1) {
63 fprintf(ctx->fout, "%ld:%ld\n", (long int)curtime, (long int)(curtime - ctx->start));
65 if (ctx->is_verbose) {
66 printf("now not is working\n");
73 void sig_handler(int sig)
78 now_not_working(g_ctx);
80 fprintf(g_ctx->fout, "# end at %ld\n", (long int)curtime);
87 int main(int argc, char *argv[])
91 int inactive_time = 10;
92 int argd, fd[MAX_FD], fd_n;
93 char fd_name[MAX_FD][PATH_MAX+1];
96 char fd_max = -1, fd_path[PATH_MAX+1], bf[1024];
105 for (argd = 1 ; argd < argc ; argd++) {
106 if (strcmp(argv[argd], "-i") == 0) {
107 if ((argd+1) < argc) {
108 inactive_time = atoi(argv[argd+1]);
112 else if (strcmp(argv[argd], "-d") == 0) {
115 else if (strcmp(argv[argd], "-v") == 0) {
128 if (daemon(0, 0) != 0) {
133 for (fd_n = 0; argd < argc ; argd++, fd_n++) {
134 sprintf(fd_path, "%s/%s", EVENT_PATH, argv[argd]);
135 strcpy(fd_name[fd_n], argv[argd]);
136 if ((fd[fd_n] = open(fd_path, O_RDONLY)) == -1) {
140 printf("[%s] opened for read\n", fd_path);
142 if (fd_max < fd[fd_n]) {
147 if ((ctx.fout = fopen(OUTPUT_FILE, "a")) == NULL) {
151 curtime = time(NULL);
152 fprintf(ctx.fout, "# start at %ld\n", (long int)curtime);
155 ctx.is_verbose = is_verbose;
158 signal(SIGINT, sig_handler);
159 signal(SIGTERM, sig_handler);
162 printf("[%s] opened for append\n", OUTPUT_FILE);
167 for (i = 0 ; i < fd_n ; i++) {
168 FD_SET(fd[i], &rfds);
170 tv.tv_sec = inactive_time;
173 retval = select(fd_max+1, &rfds, NULL, NULL, &tv);
174 /* Don't rely on the value of tv now! */
177 for (i = 0 ; i < fd_n ; i++) {
178 if(FD_ISSET(fd[i], &rfds)) {
180 printf("data from [%s]\n", fd_name[i]);
182 read(fd[i], bf, 1024);
187 else if (retval == 0) {
188 now_not_working(&ctx);