From 43d39a14330ad16b2347d39b979ec8efff63a1ec Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Thu, 19 Nov 2009 19:53:42 +0330 Subject: [PATCH] let page command take mail number --- mailx.c | 25 ++++++++++++++++++++++--- mbox.c | 1 + mbox.h | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mailx.c b/mailx.c index 025320d..7cae513 100644 --- a/mailx.c +++ b/mailx.c @@ -7,6 +7,7 @@ #include "mbox.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAXLINE (1 << 7) #define BUFSIZE (1 << 12) #define PAGER "pgmail" @@ -45,12 +46,30 @@ static void till_eol(char *s) write(1, "\n", 1); } +static char *cut_int(char *dst, char *s) +{ + while (isspace(*s)) + s++; + while (isdigit(*s)) + *dst++ = *s++; + *dst = '\0'; + return s; +} + static void cmd_page(struct mbox *mbox, char *args) { struct mail *mail; pid_t pid; int fds[2]; - mail = &mbox->mails[0]; + char num[MAXLINE]; + int n = mbox->c; + args = cut_int(num, args); + if (isdigit(*num)) + n = atoi(num); + if (n < 0 && n >= mbox->n) + return; + mail = &mbox->mails[n]; + mbox->c = n; pipe(fds); if (!(pid = fork())) { char *args[] = {PAGER, NULL}; @@ -85,8 +104,8 @@ static void cmd_head(struct mbox *mbox, char *args) static void loop(struct mbox *mbox) { - char line[128]; - char cmd[128]; + char line[MAXLINE]; + char cmd[MAXLINE]; while (read_line(line, sizeof(line)) > 0) { char *args = cut_cmd(cmd, line); int len = strlen(cmd); diff --git a/mbox.c b/mbox.c index 108ddad..7380913 100644 --- a/mbox.c +++ b/mbox.c @@ -75,6 +75,7 @@ struct mbox *mbox_alloc(char *filename) if (fd == -1) return NULL; mbox = malloc(sizeof(*mbox)); + memset(mbox, 0, sizeof(*mbox)); len = file_size(fd); mbox->mbox = malloc(len + 1); mbox->len = read_file(fd, mbox->mbox, len); diff --git a/mbox.h b/mbox.h index f6dac4d..5a30236 100644 --- a/mbox.h +++ b/mbox.h @@ -11,6 +11,7 @@ struct mbox { int n; char *mbox; int len; + int c; }; struct mbox *mbox_alloc(char *filename); -- 2.11.4.GIT