📄 msgs.c
字号:
Lpp = NLINES; } } }#endif Lpp -= 6; /* for headers, etc. */ already = NO; prevmsg = firstmsg; printing = YES; if (ruptible) signal(SIGINT, onintr); /* * Main program loop */ for (msg = firstmsg; msg <= lastmsg; msg++) { sprintf(fname, "%s/%d", _PATH_MSGS, msg); newmsg = fopen(fname, "r"); if (newmsg == NULL) continue; gfrsub(newmsg); /* get From and Subject fields */ if (locomode && !local) { fclose(newmsg); continue; } if (qopt) { /* This has to be located here */ printf("There are new messages.\n"); exit(0); } if (already && !hdrs) putchar('\n'); /* * Print header */ if (totty) signal(SIGTSTP, onsusp); (void) setjmp(tstpbuf); already = YES; nlines = 2; if (seenfrom) { printf("Message %d:\nFrom %s %s", msg, from, date); nlines++; } if (seensubj) { printf("Subject: %s", subj); nlines++; } else { if (seenfrom) { putchar('\n'); nlines++; } while (nlines < 6 && fgets(inbuf, sizeof inbuf, newmsg) && inbuf[0] != '\n') { fputs(inbuf, stdout); nlines++; } } lct = linecnt(newmsg); if (lct) printf("(%d%slines) ", lct, seensubj? " " : " more "); if (hdrs) { printf("\n-----\n"); fclose(newmsg); continue; } /* * Ask user for command */ if (totty) ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT)); else inbuf[0] = 'y'; if (totty) signal(SIGTSTP, SIG_DFL);cmnd: in = inbuf; switch (*in) { case 'x': case 'X': exit(0); case 'q': case 'Q': quitit = YES; printf("--Postponed--\n"); exit(0); /* intentional fall-thru */ case 'n': case 'N': if (msg >= nextmsg) sep = "Flushed"; prevmsg = msg; break; case 'p': case 'P': use_pager = (*in++ == 'p'); /* intentional fallthru */ case '\n': case 'y': default: if (*in == '-') { msg = prevmsg-1; sep = "replay"; break; } if (isdigit(*in)) { msg = next(in); sep = in; break; } prmesg(nlines + lct + (seensubj? 1 : 0)); prevmsg = msg; } printf("--%s--\n", sep); sep = "-"; if (msg >= nextmsg) { nextmsg = msg + 1; fseek(msgsrc, 0L, 0); fprintf(msgsrc, "%d\n", nextmsg); fflush(msgsrc); } if (newmsg) fclose(newmsg); if (quitit) break; } /* * Make sure .rc file gets updated */ if (--msg >= nextmsg) { nextmsg = msg + 1; fseek(msgsrc, 0L, 0); fprintf(msgsrc, "%d\n", nextmsg); fflush(msgsrc); } if (already && !quitit && lastcmd && totty) { /* * save or reply to last message? */ msg = prevmsg; ask(NOMORE); if (inbuf[0] == '-' || isdigit(inbuf[0])) goto cmnd; } if (!(already || hush || qopt)) printf("No new messages.\n"); exit(0);}prmesg(length)int length;{ FILE *outf; if (use_pager && length > Lpp) { signal(SIGPIPE, SIG_IGN); signal(SIGQUIT, SIG_IGN); sprintf(cmdbuf, _PATH_PAGER, Lpp); outf = popen(cmdbuf, "w"); if (!outf) outf = stdout; else setbuf(outf, (char *)NULL); } else outf = stdout; if (seensubj) putc('\n', outf); while (fgets(inbuf, sizeof inbuf, newmsg)) { fputs(inbuf, outf); if (ferror(outf)) { clearerr(outf); break; } } if (outf != stdout) { pclose(outf); signal(SIGPIPE, SIG_DFL); signal(SIGQUIT, SIG_DFL); } else { fflush(stdout); } /* trick to force wait on output */ stty(fileno(stdout), &otty);}voidonintr(){ signal(SIGINT, onintr); if (mailing) unlink(fname); if (sending) { unlink(fname); puts("--Killed--"); exit(1); } if (printing) { putchar('\n'); if (hdrs) exit(0); sep = "Interrupt"; if (newmsg) fseek(newmsg, 0L, 2); intrpflg = YES; }}/* * We have just gotten a susp. Suspend and prepare to resume. */voidonsusp(){ signal(SIGTSTP, SIG_DFL); sigsetmask(0); kill(0, SIGTSTP); signal(SIGTSTP, onsusp); if (!mailing) longjmp(tstpbuf, 0);}linecnt(f)FILE *f;{ off_t oldpos = ftell(f); int l = 0; char lbuf[BUFSIZ]; while (fgets(lbuf, sizeof lbuf, f)) l++; clearerr(f); fseek(f, oldpos, 0); return (l);}next(buf)char *buf;{ int i; sscanf(buf, "%d", &i); sprintf(buf, "Goto %d", i); return(--i);}ask(prompt)char *prompt;{ char inch; int n, cmsg; off_t oldpos; FILE *cpfrom, *cpto; printf("%s ", prompt); fflush(stdout); intrpflg = NO; (void) fgets(inbuf, sizeof inbuf, stdin); if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n') inbuf[n - 1] = '\0'; if (intrpflg) inbuf[0] = 'x'; /* * Handle 'mail' and 'save' here. */ if ((inch = inbuf[0]) == 's' || inch == 'm') { if (inbuf[1] == '-') cmsg = prevmsg; else if (isdigit(inbuf[1])) cmsg = atoi(&inbuf[1]); else cmsg = msg; sprintf(fname, "%s/%d", _PATH_MSGS, cmsg); oldpos = ftell(newmsg); cpfrom = fopen(fname, "r"); if (!cpfrom) { printf("Message %d not found\n", cmsg); ask (prompt); return; } if (inch == 's') { in = nxtfld(inbuf); if (*in) { for (n=0; in[n] > ' '; n++) { /* sizeof fname? */ fname[n] = in[n]; } fname[n] = NULL; } else strcpy(fname, "Messages"); } else { strcpy(fname, _PATH_TMP); mktemp(fname); sprintf(cmdbuf, _PATH_MAIL, fname); mailing = YES; } cpto = fopen(fname, "a"); if (!cpto) { perror(fname); mailing = NO; fseek(newmsg, oldpos, 0); ask(prompt); return; } while (n = fread(inbuf, 1, sizeof inbuf, cpfrom)) fwrite(inbuf, 1, n, cpto); fclose(cpfrom); fclose(cpto); fseek(newmsg, oldpos, 0); /* reposition current message */ if (inch == 's') printf("Message %d saved in \"%s\"\n", cmsg, fname); else { system(cmdbuf); unlink(fname); mailing = NO; } ask(prompt); }}gfrsub(infile)FILE *infile;{ off_t frompos; seensubj = seenfrom = NO; local = YES; subj[0] = from[0] = date[0] = NULL; /* * Is this a normal message? */ if (fgets(inbuf, sizeof inbuf, infile)) { if (strncmp(inbuf, "From", 4)==0) { /* * expected form starts with From */ seenfrom = YES; frompos = ftell(infile); ptr = from; in = nxtfld(inbuf); if (*in) while (*in && *in > ' ') { if (*in == ':' || *in == '@' || *in == '!') local = NO; *ptr++ = *in++; /* what about sizeof from ? */ } *ptr = NULL; if (*(in = nxtfld(in))) strncpy(date, in, sizeof date); else { date[0] = '\n'; date[1] = NULL; } } else { /* * not the expected form */ fseek(infile, 0L, 0); return; } } else /* * empty file ? */ return; /* * look for Subject line until EOF or a blank line */ while (fgets(inbuf, sizeof inbuf, infile) && !(blankline = (inbuf[0] == '\n'))) { /* * extract Subject line */ if (!seensubj && strncmp(inbuf, "Subj", 4)==0) { seensubj = YES; frompos = ftell(infile); strncpy(subj, nxtfld(inbuf), sizeof subj); } } if (!blankline) /* * ran into EOF */ fseek(infile, frompos, 0); if (!seensubj) /* * for possible use with Mail */ strncpy(subj, "(No Subject)\n", sizeof subj);}char *nxtfld(s)char *s;{ if (*s) while (*s && *s > ' ') s++; /* skip over this field */ if (*s) while (*s && *s <= ' ') s++; /* find start of next field */ return (s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -