From 2c3acdb84e2d05d5a445ab50200200b80c9f978d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 14 Aug 2004 17:09:55 +0000 Subject: major update: version 0.5 remove malloc stuff add itimer --- watch.c | 228 ++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 122 insertions(+), 106 deletions(-) diff --git a/watch.c b/watch.c index 4549096..89a7d72 100644 --- a/watch.c +++ b/watch.c @@ -5,7 +5,7 @@ * (with mods and corrections by Francois Pinard) */ /* - * Copyright (c) 2003 demon + * Copyright (c) 2003, 2004 demon * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,15 +22,22 @@ #include #include +#include #include #include #include #include #include +#include #include +#include -static char *copyright = "(c) 2003 demon "; -static char *version = "0.4"; +#define MAXBUF 1024 + +static char buffer[MAXBUF]; + +static char *copyright = "(c) 2003, 2004 demon "; +static char *version = "0.5"; extern char *__progname; unsigned int period = 2; @@ -38,36 +45,27 @@ unsigned short die_flag = 0; unsigned short n_flag = 0; unsigned short color_flag = 0; -void loop(char *); -void title(char *); +static int lines, cols; + +int readargs(char **); +void display(); +int title(); void resize(); +void settimer(int); void die(); void usage(); -struct buf { - char *b_val; - unsigned int b_size; -}; - int main(int argc, char **argv) { - unsigned int a_len; - unsigned int c_len; int hold_curs; char ch; - struct buf cmd; - - signal(SIGINT, die); - signal(SIGTERM, die); - signal(SIGHUP, die); - signal(SIGWINCH, resize); while ((ch = getopt(argc, argv, "s:vn")) != -1) switch (ch) { case 'v': - (void) fprintf(stderr, "%s %s %s\n", - __progname, version, copyright); + (void) fprintf(stderr, "watch %s %s\n", + version, copyright); exit(1); break; case 's': @@ -85,140 +83,158 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (*argv) { - cmd.b_size = strlen(*argv) + 1; - if (!(cmd.b_val = (char *) malloc(cmd.b_size))) - perror("malloc"); - memcpy(cmd.b_val, *argv, strlen(*argv)); - - while (*++argv) { - a_len = strlen(*argv); - cmd.b_size += a_len + 1; - if (!(cmd.b_val = (char *) realloc(cmd.b_val, - cmd.b_size))) - perror("realloc"); - c_len = strlen(cmd.b_val); - cmd.b_val[c_len] = ' '; - memcpy(cmd.b_val + c_len + 1, *argv, a_len); - } + if (readargs(argv) == -1) + usage(); /* does not return */ - initscr(); - hold_curs = curs_set(0); - if (has_colors()) - color_flag = 1; + signal(SIGINT, die); + signal(SIGTERM, die); + signal(SIGHUP, die); - loop(cmd.b_val); + initscr(); + lines = LINES; + cols = COLS; + signal(SIGWINCH, resize); - curs_set(hold_curs); - endwin(); + hold_curs = curs_set(0); + if (has_colors()) + color_flag = 1; - free(cmd.b_val); - cmd.b_size = 0; + signal(SIGALRM, display); + settimer(period); + display(); - } else - usage(); + while (die_flag == 0) + pause(); + curs_set(hold_curs); + endwin(); exit(0); } +int +readargs(char **argv) +{ + int alen, blen; + + if (*argv == NULL) + return -1; + + alen = strlen(*argv); + if (alen + 1 >= MAXBUF) + return -1; + memcpy(buffer, *argv, alen); + + while (*++argv != NULL) { + alen = strlen(*argv); + blen = strlen(buffer); + if (alen + blen + 1 >= MAXBUF) + return -1; + buffer[blen] = ' '; + memcpy(buffer + blen + 1, *argv, alen); + buffer[blen + alen + 1] = '\0'; + } + return 0; +} + void -loop(char *cmd) +display() { - unsigned int char_count; - unsigned int line_count; char ch; FILE *pipe; + int char_count = 0; + int line_count = 0; - while (!die_flag) { - char_count = 0; - line_count = 0; + clear(); - clear(); + if (n_flag == 0) { + title(); + line_count = 2; + } + pipe = popen(buffer, "r"); - if (!n_flag) { - title(cmd); - line_count += 2; + while ((ch = fgetc(pipe)) != EOF) { + if ((ch == '\0') || (ch == '\n')) { + line_count++; + char_count = 0; } - pipe = popen(cmd, "r"); - - while ((ch = fgetc(pipe)) != EOF) { - if ((ch == '\0') || (ch == '\n')) { - line_count++; - char_count = 0; - } - if (line_count >= LINES) - break; - - if (char_count < COLS) { - printw("%c", ch); - char_count++; - } + if (line_count >= lines) + break; + if (char_count < cols) { + printw("%c", ch); + char_count++; } - - pclose(pipe); - refresh(); - sleep(period); } + + pclose(pipe); + refresh(); } -void -title(char *cmd) +int +title() { - unsigned int t_len; - unsigned int t_len2; + char title[MAXBUF]; + int tlen, tlen2; time_t tval = time(NULL); - - struct buf title; struct tm *tm = localtime(&tval); - title.b_size = COLS + 1; - if (!(title.b_val = (char *) malloc(title.b_size))) - perror("malloc"); + if (cols + 1 >= MAXBUF) + return -1; - snprintf(title.b_val, COLS, " Every %ds : %s", period, cmd); + snprintf(title, cols, " Every %ds : %s", period, buffer); - t_len = strlen(title.b_val); - t_len2 = COLS - t_len; + tlen = strlen(title); + tlen2 = cols - tlen; - if (t_len2 > 0) - memset(title.b_val + t_len, ' ', t_len2); + if (tlen2 > 0) + memset(title + tlen, ' ', tlen2); - if (t_len2 < 12) - title.b_val[COLS - 12] = '>'; + if (tlen2 < 12) + title[cols - 12] = '>'; - t_len = strlen(title.b_val); - - snprintf(title.b_val + t_len - 11, 12, " %.2d:%.2d:%.2d ", + snprintf(title + cols - 11, 12, " %.2d:%.2d:%.2d ", tm->tm_hour, tm->tm_min, tm->tm_sec); - title.b_val[COLS] = '\0'; + title[cols] = '\0'; - if (color_flag) + if (color_flag == 1) attron(A_REVERSE); - printw("%s", title.b_val); + printw("%s", title); - if (color_flag) + if (color_flag == 1) attroff(A_REVERSE); - free(title.b_val); - title.b_size = 0; - move(2, 0); + return 0; } void resize() { + int save_errno = errno; struct winsize ws; - if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *) &ws)) { - LINES = ws.ws_row; - COLS = ws.ws_col; - resizeterm(LINES, COLS); - clear(); + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { + lines = ws.ws_row; + cols = ws.ws_col; + resizeterm(lines, cols); } + raise(SIGALRM); + errno = save_errno; +} + +void +settimer(int wait) +{ + int save_errno = errno; + struct itimerval itv; + + itv.it_value.tv_sec = wait; + itv.it_value.tv_usec = 0; + itv.it_interval = itv.it_value; + setitimer(ITIMER_REAL, &itv, NULL); + errno = save_errno; } void -- cgit v1.2.3