2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)msgs.c 8.2 (Berkeley) 4/28/95
35 * $FreeBSD: src/usr.bin/msgs/msgs.c,v 1.15.2.2 2003/02/11 21:31:56 mike Exp $
36 * $DragonFly: src/usr.bin/msgs/msgs.c,v 1.2 2003/06/17 04:29:29 dillon Exp $
40 * msgs - a user bulletin board program
43 * msgs [fhlopq] [[-]number] to read messages
44 * msgs -s to place messages
45 * msgs -c [-days] to clean up the bulletin board
47 * prompt commands are:
49 * n flush message, go to next message
50 * q flush message, quit
51 * p print message, turn on 'pipe thru more' mode
52 * P print message, turn off 'pipe thru more' mode
53 * - reprint last message
54 * s[-][<num>] [<filename>] save message
55 * m[-][<num>] mail with message in temp mbox
56 * x exit without flushing this message
57 * <num> print message number <num>
60 #define V7 /* will look for TERM in the environment */
61 #define OBJECT /* will object to messages without Subjects */
62 /* #define REJECT */ /* will reject messages without Subjects
63 (OBJECT must be defined also) */
64 /* #define UNBUFFERED *//* use unbuffered output */
66 #include <sys/param.h>
84 #include "pathnames.h"
86 #define CMODE 0644 /* bounds file creation mode */
89 #define SUPERUSER 0 /* superuser uid */
90 #define DAEMON 1 /* daemon uid */
91 #define NLINES 24 /* default number of lines/crt screen */
92 #define NDAYS 21 /* default keep time for messages */
93 #define DAYS *24*60*60 /* seconds/day */
94 #define MSGSRC ".msgsrc" /* user's rc file */
95 #define BOUNDS "bounds" /* message bounds file */
96 #define NEXT "Next message? [yq]"
97 #define MORE "More? [ynq]"
98 #define NOMORE "(No more) [q] ?"
106 char fname[MAXPATHLEN];
107 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
133 /* option initialization */
145 void ask __P((char *));
146 void gfrsub __P((FILE *));
147 int linecnt __P((FILE *));
148 int next __P((char *));
149 char *nxtfld __P((unsigned char *));
150 void onsusp __P((int));
151 void onintr __P((int));
152 void prmesg __P((int));
153 static void usage __P((void));
157 int argc; char *argv[];
160 int rcfirst = 0; /* first message to print (from .rc) */
161 int rcback = 0; /* amount to back off of rcfirst */
162 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
164 struct stat buf; /* stat to check access of bounds */
168 setbuf(stdout, NULL);
170 setlocale(LC_ALL, "");
173 setuid(uid = getuid());
174 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
176 signal(SIGINT, SIG_DFL);
180 if (isdigit(argv[0][0])) { /* starting message # */
181 rcfirst = atoi(argv[0]);
183 else if (isdigit(argv[0][1])) { /* backward offset */
184 rcback = atoi( &( argv[0][1] ) );
188 while (*ptr) switch (*ptr++) {
194 if (uid != SUPERUSER && uid != DAEMON) {
195 fprintf(stderr, "Sorry\n");
201 case 'f': /* silently */
205 case 'h': /* headers only */
209 case 'l': /* local msgs only */
213 case 'o': /* option to save last message */
217 case 'p': /* pipe thru 'more' during long msgs */
221 case 'q': /* query only */
225 case 's': /* sending TO msgs */
237 * determine current message bounds
239 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
242 * Test access rights to the bounds file
243 * This can be a little tricky. if(send_msg), then
244 * we will create it. We assume that if(send_msg),
245 * then you have write permission there.
246 * Else, it better be there, or we bail.
248 if (send_msg != YES) {
249 if (stat(fname, &buf) < 0) {
251 err(errno, "%s", fname);
257 bounds = fopen(fname, "r");
259 if (bounds != NULL) {
260 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
262 blast = lastmsg; /* save upper bound */
266 keep = t - (rcback? rcback : NDAYS) DAYS;
268 if (clean || bounds == NULL) { /* relocate message bounds */
274 dirp = opendir(_PATH_MSGS);
276 err(errno, "%s", _PATH_MSGS);
281 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
282 register char *cp = dp->d_name;
287 if (dp->d_namlen == 0)
291 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
294 i = i * 10 + *cp++ - '0';
296 continue; /* not a message! */
299 if (stat(inbuf, &stbuf) != 0)
301 if (stbuf.st_mtime < keep
302 && stbuf.st_mode&S_IWRITE) {
317 if (blast != 0) /* never lower the upper bound! */
319 firstmsg = lastmsg + 1;
321 else if (blast > lastmsg)
325 bounds = fopen(fname, "w");
327 err(errno, "%s", fname);
329 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
336 * Send mode - place msgs in _PATH_MSGS
338 bounds = fopen(fname, "w");
340 err(errno, "%s", fname);
342 nextmsg = lastmsg + 1;
343 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
344 newmsg = fopen(fname, "w");
346 err(errno, "%s", fname);
349 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
354 signal(SIGINT, onintr);
356 if (isatty(fileno(stdin))) {
357 ptr = getpwuid(uid)->pw_name;
358 printf("Message %d:\nFrom %s %sSubject: ",
359 nextmsg, ptr, ctime(&t));
361 fgets(inbuf, sizeof inbuf, stdin);
364 fprintf(newmsg, "From %s %sSubject: %s\n",
365 ptr, ctime(&t), inbuf);
366 blankline = seensubj = YES;
369 blankline = seensubj = NO;
371 fgets(inbuf, sizeof inbuf, stdin);
372 if (feof(stdin) || ferror(stdin))
374 blankline = (blankline || (inbuf[0] == '\n'));
375 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
376 fputs(inbuf, newmsg);
380 printf("NOTICE: Messages should have a Subject field!\n");
393 * prepare to display messages
395 totty = (isatty(fileno(stdout)) != 0);
396 use_pager = use_pager && totty;
398 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
399 msgsrc = fopen(fname, "r");
402 fscanf(msgsrc, "%d\n", &nextmsg);
404 if (nextmsg > lastmsg+1) {
405 printf("Warning: bounds have been reset (%d, %d)\n",
407 truncate(fname, (off_t)0);
411 rcfirst = nextmsg - rcback;
415 msgsrc = fopen(fname, "r+");
417 msgsrc = fopen(fname, "w");
419 err(errno, "%s", fname);
421 if (rcfirst > lastmsg+1) {
422 printf("Warning: the last message is number %d.\n",
426 if (rcfirst > firstmsg)
427 firstmsg = rcfirst; /* don't set below first msg */
431 fseek(msgsrc, 0L, 0);
432 fprintf(msgsrc, "%d\n", nextmsg);
439 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
442 if (tgetent(inbuf, getenv("TERM")) <= 0
443 || (Lpp = tgetnum("li")) <= 0) {
449 Lpp -= 6; /* for headers, etc. */
455 signal(SIGINT, onintr);
460 for (msg = firstmsg; msg <= lastmsg; msg++) {
462 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
463 newmsg = fopen(fname, "r");
467 gfrsub(newmsg); /* get From and Subject fields */
468 if (locomode && !local) {
473 if (qopt) { /* This has to be located here */
474 printf("There are new messages.\n");
478 if (already && !hdrs)
485 signal(SIGTSTP, onsusp);
486 (void) setjmp(tstpbuf);
490 printf("Message %d:\nFrom %s %s", msg, from, date);
494 printf("Subject: %s", subj);
503 && fgets(inbuf, sizeof inbuf, newmsg)
504 && inbuf[0] != '\n') {
505 fputs(inbuf, stdout);
510 lct = linecnt(newmsg);
512 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
513 (lct == 1) ? "" : "s");
522 * Ask user for command
525 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
529 signal(SIGTSTP, SIG_DFL);
540 printf("--Postponed--\n");
542 /* intentional fall-thru */
545 if (msg >= nextmsg) sep = "Flushed";
551 use_pager = (*in++ == 'p');
552 /* intentional fallthru */
567 prmesg(nlines + lct + (seensubj? 1 : 0));
572 printf("--%s--\n", sep);
574 if (msg >= nextmsg) {
576 fseek(msgsrc, 0L, 0);
577 fprintf(msgsrc, "%d\n", nextmsg);
587 * Make sure .rc file gets updated
589 if (--msg >= nextmsg) {
591 fseek(msgsrc, 0L, 0);
592 fprintf(msgsrc, "%d\n", nextmsg);
595 if (already && !quitit && lastcmd && totty) {
597 * save or reply to last message?
601 if (inbuf[0] == '-' || isdigit(inbuf[0]))
604 if (!(already || hush || qopt))
605 printf("No new messages.\n");
612 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
623 if (use_pager && length > Lpp) {
624 signal(SIGPIPE, SIG_IGN);
625 signal(SIGQUIT, SIG_IGN);
626 if ((env_pager = getenv("PAGER")) == NULL) {
627 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
629 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
631 outf = popen(cmdbuf, "w");
635 setbuf(outf, (char *)NULL);
643 while (fgets(inbuf, sizeof inbuf, newmsg)) {
651 if (outf != stdout) {
653 signal(SIGPIPE, SIG_DFL);
654 signal(SIGQUIT, SIG_DFL);
660 /* force wait on output */
661 tcdrain(fileno(stdout));
668 signal(SIGINT, onintr);
682 fseek(newmsg, 0L, 2);
688 * We have just gotten a susp. Suspend and prepare to resume.
694 signal(SIGTSTP, SIG_DFL);
697 signal(SIGTSTP, onsusp);
706 off_t oldpos = ftell(f);
710 while (fgets(lbuf, sizeof lbuf, f))
722 sscanf(buf, "%d", &i);
723 sprintf(buf, "Goto %d", i);
736 printf("%s ", prompt);
739 (void) fgets(inbuf, sizeof inbuf, stdin);
740 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
746 * Handle 'mail' and 'save' here.
748 if ((inch = inbuf[0]) == 's' || inch == 'm') {
751 else if (isdigit(inbuf[1]))
752 cmsg = atoi(&inbuf[1]);
755 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
757 oldpos = ftell(newmsg);
759 cpfrom = fopen(fname, "r");
761 printf("Message %d not found\n", cmsg);
769 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
775 strcpy(fname, "Messages");
776 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
779 strcpy(fname, _PATH_TMP);
782 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
787 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
792 fseek(newmsg, oldpos, 0);
797 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
798 fwrite(inbuf, 1, n, cpto);
802 fseek(newmsg, oldpos, 0); /* reposition current message */
804 printf("Message %d saved in \"%s\"\n", cmsg, fname);
821 seensubj = seenfrom = NO;
823 subj[0] = from[0] = date[0] = NULL;
826 * Is this a normal message?
828 if (fgets(inbuf, sizeof inbuf, infile)) {
829 if (strncmp(inbuf, "From", 4)==0) {
831 * expected form starts with From
834 frompos = ftell(infile);
838 count = sizeof(from) - 1;
839 while (*in && *in > ' ' && count-- > 0) {
840 if (*in == ':' || *in == '@' ||
847 if (*(in = nxtfld(in)))
848 strncpy(date, in, sizeof date);
856 * not the expected form
858 fseek(infile, 0L, 0);
869 * look for Subject line until EOF or a blank line
871 while (fgets(inbuf, sizeof inbuf, infile)
872 && !(blankline = (inbuf[0] == '\n'))) {
874 * extract Subject line
876 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
878 frompos = ftell(infile);
879 strncpy(subj, nxtfld(inbuf), sizeof subj);
886 fseek(infile, frompos, 0);
890 * for possible use with Mail
892 strncpy(subj, "(No Subject)\n", sizeof subj);
899 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
900 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */