diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-10-02 04:14:40 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-10-02 04:14:40 +0000 |
commit | 43e8d906bd836ae0b9b84ed8088434f7b7ab84f1 (patch) | |
tree | 103d44d1d89ef2ffb7687d41d38c284649ca0d9f /usr.bin/msgs | |
parent | 35076932ee996a9d81fd0f9396d3c81a11d6d9d5 (diff) |
ftello and fseek ok and such, ok otto
Diffstat (limited to 'usr.bin/msgs')
-rw-r--r-- | usr.bin/msgs/msgs.c | 286 |
1 files changed, 139 insertions, 147 deletions
diff --git a/usr.bin/msgs/msgs.c b/usr.bin/msgs/msgs.c index 163656cb449..66091e883cb 100644 --- a/usr.bin/msgs/msgs.c +++ b/usr.bin/msgs/msgs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: msgs.c,v 1.28 2003/10/04 21:55:12 jmc Exp $ */ +/* $OpenBSD: msgs.c,v 1.29 2004/10/02 04:14:39 deraadt Exp $ */ /* $NetBSD: msgs.c,v 1.7 1995/09/28 06:57:40 tls Exp $ */ /*- @@ -40,7 +40,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95"; #else -static char rcsid[] = "$OpenBSD: msgs.c,v 1.28 2003/10/04 21:55:12 jmc Exp $"; +static char rcsid[] = "$OpenBSD: msgs.c,v 1.29 2004/10/02 04:14:39 deraadt Exp $"; #endif #endif /* not lint */ @@ -183,64 +183,52 @@ main(int argc, char *argv[]) while (argc > 0) { if (isdigit(argv[0][0])) { /* starting message # */ rcfirst = atoi(argv[0]); - } - else if (isdigit(argv[0][1])) { /* backward offset */ - rcback = atoi( &( argv[0][1] ) ); - } - else { + } else if (isdigit(argv[0][1])) { /* backward offset */ + rcback = atoi(&(argv[0][1])); + } else { ptr = *argv; - while (*ptr) switch (*ptr++) { - - case '-': - break; - - case 'c': - if (uid != SUPERUSER && uid != DAEMON) { - fprintf(stderr, "Sorry\n"); + while (*ptr) { + switch (*ptr++) { + case '-': + break; + case 'c': + if (uid != SUPERUSER && uid != DAEMON) { + fprintf(stderr, "Sorry\n"); + exit(1); + } + clean = YES; + break; + case 'f': /* silently */ + hush = YES; + break; + case 'h': /* headers only */ + hdrs = YES; + break; + case 'l': /* local msgs only */ + locomode = YES; + break; + case 'o': /* option to save last message */ + lastcmd = YES; + break; + case 'p': /* pipe thru 'more' during long msgs */ + use_pager = YES; + break; + case 'q': /* query only */ + qopt = YES; + break; + case 'r': /* restricted */ + restricted = YES; + break; + case 's': /* sending TO msgs */ + send_msg = YES; + break; + default: + fprintf(stderr, + "usage: msgs [fhlopqr] [[-]number]\n" + " msgs [-s]\n" + " msgs [-c [-days]]\n"); exit(1); } - clean = YES; - break; - - case 'f': /* silently */ - hush = YES; - break; - - case 'h': /* headers only */ - hdrs = YES; - break; - - case 'l': /* local msgs only */ - locomode = YES; - break; - - case 'o': /* option to save last message */ - lastcmd = YES; - break; - - case 'p': /* pipe thru 'more' during long msgs */ - use_pager = YES; - break; - - case 'q': /* query only */ - qopt = YES; - break; - - case 'r': /* restricted */ - restricted = YES; - break; - - - case 's': /* sending TO msgs */ - send_msg = YES; - break; - - default: - fprintf(stderr, - "usage: msgs [fhlopqr] [[-]number]\n" - " msgs [-s]\n" - " msgs [-c [-days]]\n"); - exit(1); } } argc--, argv++; @@ -260,8 +248,7 @@ main(int argc, char *argv[]) } fprintf(bounds, "1 0\n"); rewind(bounds); - } - else { + } else { perror(fname); exit(1); } @@ -301,7 +288,7 @@ main(int argc, char *argv[]) if (clean) snprintf(inbuf, sizeof(inbuf), "%s/%s", - _PATH_MSGS, cp); + _PATH_MSGS, cp); while (isdigit(*cp)) i = i * 10 + *cp++ - '0'; @@ -311,8 +298,8 @@ main(int argc, char *argv[]) if (clean) { if (stat(inbuf, &stbuf) != 0) continue; - if (stbuf.st_mtime < keep - && stbuf.st_mode&S_IWRITE) { + if (stbuf.st_mtime < keep && + stbuf.st_mode&S_IWRITE) { unlink(inbuf); continue; } @@ -330,8 +317,7 @@ main(int argc, char *argv[]) if (blast != 0) /* never lower the upper bound! */ lastmsg = blast; firstmsg = lastmsg + 1; - } - else if (blast > lastmsg) + } else if (blast > lastmsg) lastmsg = blast; if (!send_msg) { @@ -375,23 +361,23 @@ main(int argc, char *argv[]) if (isatty(fileno(stdin))) { ptr = getpwuid(uid)->pw_name; printf("Message %d:\nFrom %s %sSubject: ", - nextmsg, ptr, ctime(&t)); + nextmsg, ptr, ctime(&t)); fflush(stdout); fgets(inbuf, sizeof inbuf, stdin); putchar('\n'); fflush(stdout); fprintf(newmsg, "From %s %sSubject: %s\n", - ptr, ctime(&t), inbuf); + ptr, ctime(&t), inbuf); blankline = seensubj = YES; - } - else + } else blankline = seensubj = NO; for (;;) { fgets(inbuf, sizeof inbuf, stdin); if (feof(stdin) || ferror(stdin)) break; blankline = (blankline || (inbuf[0] == '\n')); - seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); + seensubj = (seensubj || + (!blankline && (strncmp(inbuf, "Subj", 4) == 0))); fputs(inbuf, newmsg); } #ifdef OBJECT @@ -426,14 +412,12 @@ main(int argc, char *argv[]) fclose(msgsrc); if (nextmsg > lastmsg+1) { printf("Warning: bounds have been reset (%d, %d)\n", - firstmsg, lastmsg); + firstmsg, lastmsg); truncate(fname, (off_t)0); newrc = YES; - } - else if (!rcfirst) + } else if (!rcfirst) rcfirst = nextmsg - rcback; - } - else + } else newrc = YES; msgsrc = fopen(fname, "r+"); if (msgsrc == NULL) @@ -445,7 +429,7 @@ main(int argc, char *argv[]) if (rcfirst) { if (rcfirst > lastmsg+1) { printf("Warning: the last message is number %d.\n", - lastmsg); + lastmsg); rcfirst = nextmsg; } if (rcfirst > firstmsg) @@ -453,7 +437,7 @@ main(int argc, char *argv[]) } if (newrc) { nextmsg = firstmsg; - fseek(msgsrc, 0L, 0); + fseeko(msgsrc, (off_t)0, SEEK_SET); fprintf(msgsrc, "%d\n", nextmsg); fflush(msgsrc); } @@ -521,15 +505,14 @@ main(int argc, char *argv[]) if (seensubj) { printf("Subject: %s", subj); nlines++; - } - else { + } else { if (seenfrom) { putchar('\n'); nlines++; } - while (nlines < 6 - && fgets(inbuf, sizeof inbuf, newmsg) - && inbuf[0] != '\n') { + while (nlines < 6 && + fgets(inbuf, sizeof inbuf, newmsg) && + inbuf[0] != '\n') { fputs(inbuf, stdout); nlines++; } @@ -557,50 +540,50 @@ main(int argc, char *argv[]) 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 'x': + case 'X': + exit(0); - 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, sizeof inbuf); - sep = in; - break; - } + 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; - prmesg(nlines + lct + (seensubj? 1 : 0)); - prevmsg = msg; + 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, sizeof inbuf); + 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); + fseeko(msgsrc, (off_t)0, SEEK_SET); fprintf(msgsrc, "%d\n", nextmsg); fflush(msgsrc); } @@ -615,7 +598,7 @@ cmnd: */ if (--msg >= nextmsg) { nextmsg = msg + 1; - fseek(msgsrc, 0L, 0); + fseeko(msgsrc, (off_t)0, SEEK_SET); fprintf(msgsrc, "%d\n", nextmsg); fflush(msgsrc); } @@ -671,8 +654,7 @@ prmesg(int length) pclose(outf); signal(SIGPIPE, SIG_DFL); signal(SIGQUIT, SIG_DFL); - } - else { + } else { fflush(stdout); } @@ -680,34 +662,40 @@ prmesg(int length) tcdrain(fileno(stdout)); } +/* ARGSUSED */ void -onintr(int unused) +onintr(int signo) { + int save_errno = errno; + signal(SIGINT, onintr); if (mailing) unlink(fname); if (sending) { unlink(fname); - puts("--Killed--"); - exit(1); + write(STDOUT_FILENO, "--Killed--\n", strlen("--Killed--\n")); + _exit(1); } if (printing) { - putchar('\n'); + write(STDOUT_FILENO, "\n", 1); if (hdrs) - exit(0); + _exit(0); sep = "Interrupt"; if (newmsg) - fseek(newmsg, 0L, 2); + fseeko(newmsg, (off_t)0, SEEK_END); intrpflg = YES; } + errno = save_errno; } /* * We have just gotten a susp. Suspend and prepare to resume. */ +/* ARGSUSED */ void -onsusp(int unused) +onsusp(int signo) { + int save_errno = errno; sigset_t emptyset; signal(SIGTSTP, SIG_DFL); @@ -715,6 +703,8 @@ onsusp(int unused) sigprocmask(SIG_SETMASK, &emptyset, NULL); kill(0, SIGTSTP); signal(SIGTSTP, onsusp); + errno = save_errno; + if (!mailing) longjmp(tstpbuf, 1); } @@ -722,14 +712,15 @@ onsusp(int unused) int linecnt(FILE *f) { - off_t oldpos = ftell(f); + off_t oldpos = ftello(f); + int l = 0; char lbuf[BUFSIZ]; while (fgets(lbuf, sizeof lbuf, f)) l++; clearerr(f); - fseek(f, oldpos, 0); + fseeko(f, oldpos, SEEK_SET); return (l); } @@ -771,7 +762,7 @@ ask(char *prompt) cmsg = msg; snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg); - oldpos = ftell(newmsg); + oldpos = ftello(newmsg); cpfrom = fopen(fname, "r"); if (!cpfrom) { @@ -784,8 +775,8 @@ ask(char *prompt) in = nxtfld(inbuf); if (*in) { for (n=0; - in[n] > ' ' && n < sizeof fname - 1; - n++) { + in[n] > ' ' && n < sizeof fname - 1; + n++) { fname[n] = in[n]; } fname[n] = NULL; @@ -793,8 +784,7 @@ ask(char *prompt) else strlcpy(fname, "Messages", sizeof fname); fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND, 0666); - } - else { + } else { strlcpy(fname, _PATH_TMPFILE, sizeof fname); fd = mkstemp(fname); if (fd != -1) { @@ -807,7 +797,7 @@ ask(char *prompt) close(fd); perror(fname); mailing = NO; - fseek(newmsg, oldpos, 0); + fseeko(newmsg, oldpos, SEEK_SET); ask(prompt); return; } @@ -817,7 +807,7 @@ ask(char *prompt) fclose(cpfrom); fclose(cpto); - fseek(newmsg, oldpos, 0); /* reposition current message */ + fseeko(newmsg, oldpos, SEEK_SET); /* reposition current message */ if (inch == 's') printf("Message %d saved in \"%s\"\n", cmsg, fname); else { @@ -847,7 +837,7 @@ gfrsub(FILE *infile) * expected form starts with From */ seenfrom = YES; - frompos = ftell(infile); + frompos = ftello(infile); ptr = from; in = nxtfld(inbuf); if (*in) { @@ -865,16 +855,14 @@ gfrsub(FILE *infile) date[0] = '\n'; date[1] = NULL; } - } - else { + } else { /* * not the expected form */ - fseek(infile, 0L, 0); + fseeko(infile, (off_t)0, SEEK_SET); return; } - } - else + } else /* * empty file ? */ @@ -883,14 +871,14 @@ gfrsub(FILE *infile) /* * look for Subject line until EOF or a blank line */ - while (fgets(inbuf, sizeof inbuf, infile) - && !(blankline = (inbuf[0] == '\n'))) { + 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); + frompos = ftello(infile); strncpy(subj, nxtfld(inbuf), sizeof subj); } } @@ -898,7 +886,7 @@ gfrsub(FILE *infile) /* * ran into EOF */ - fseek(infile, frompos, 0); + fseeko(infile, frompos, SEEK_SET); if (!seensubj) /* @@ -910,7 +898,11 @@ gfrsub(FILE *infile) char * nxtfld(char *s) { - if (*s) while (*s && *s > ' ') s++; /* skip over this field */ - if (*s) while (*s && *s <= ' ') s++; /* find start of next field */ + if (*s) + while (*s && *s > ' ') + s++; /* skip over this field */ + if (*s) + while (*s && *s <= ' ') + s++; /* find start of next field */ return (s); } |