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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)msgs.c 8.2 (Berkeley) 4/28/95
31 * $FreeBSD: src/usr.bin/msgs/msgs.c,v 1.15.2.2 2003/02/11 21:31:56 mike Exp $
35 * msgs - a user bulletin board program
38 * msgs [fhlopq] [[-]number] to read messages
39 * msgs -s to place messages
40 * msgs -c [-days] to clean up the bulletin board
42 * prompt commands are:
44 * n flush message, go to next message
45 * q flush message, quit
46 * p print message, turn on 'pipe thru more' mode
47 * P print message, turn off 'pipe thru more' mode
48 * - reprint last message
49 * s[-][<num>] [<filename>] save message
50 * m[-][<num>] mail with message in temp mbox
51 * x exit without flushing this message
52 * <num> print message number <num>
55 #define V7 /* will look for TERM in the environment */
56 #define OBJECT /* will object to messages without Subjects */
57 /* #define REJECT */ /* will reject messages without Subjects
58 (OBJECT must be defined also) */
59 /* #define UNBUFFERED *//* use unbuffered output */
61 #include <sys/param.h>
80 #include "pathnames.h"
82 #define CMODE 0644 /* bounds file creation mode */
85 #define SUPERUSER 0 /* superuser uid */
86 #define DAEMON 1 /* daemon uid */
87 #define NLINES 24 /* default number of lines/crt screen */
88 #define NDAYS 21 /* default keep time for messages */
89 #define DAYS *24*60*60 /* seconds/day */
90 #define MSGSRC ".msgsrc" /* user's rc file */
91 #define BOUNDS "bounds" /* message bounds file */
92 #define NEXT "Next message? [yq]"
93 #define MORE "More? [ynq]"
94 #define NOMORE "(No more) [q] ?"
98 const char *sep = "-";
100 char fname[MAXPATHLEN];
101 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
127 /* option initialization */
139 void ask(const char *);
143 char *nxtfld(unsigned char *);
147 static void usage(void);
150 main(int argc, char **argv)
153 int rcfirst = 0; /* first message to print (from .rc) */
154 int rcback = 0; /* amount to back off of rcfirst */
155 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
157 struct stat buf; /* stat to check access of bounds */
161 setbuf(stdout, NULL);
163 setlocale(LC_ALL, "");
166 setuid(uid = getuid());
167 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
169 signal(SIGINT, SIG_DFL);
173 if (isdigit(argv[0][0])) { /* starting message # */
174 rcfirst = atoi(argv[0]);
176 else if (isdigit(argv[0][1])) { /* backward offset */
177 rcback = atoi( &( argv[0][1] ) );
181 while (*ptr) switch (*ptr++) {
187 if (uid != SUPERUSER && uid != DAEMON) {
188 fprintf(stderr, "Sorry\n");
194 case 'f': /* silently */
198 case 'h': /* headers only */
202 case 'l': /* local msgs only */
206 case 'o': /* option to save last message */
210 case 'p': /* pipe thru 'more' during long msgs */
214 case 'q': /* query only */
218 case 's': /* sending TO msgs */
230 * determine current message bounds
232 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
235 * Test access rights to the bounds file
236 * This can be a little tricky. if(send_msg), then
237 * we will create it. We assume that if(send_msg),
238 * then you have write permission there.
239 * Else, it better be there, or we bail.
241 if (send_msg != YES) {
242 if (stat(fname, &buf) < 0) {
244 err(errno, "%s", fname);
250 bounds = fopen(fname, "r");
252 if (bounds != NULL) {
253 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
255 blast = lastmsg; /* save upper bound */
259 keep = t - (rcback? rcback : NDAYS) DAYS;
261 if (clean || bounds == NULL) { /* relocate message bounds */
267 dirp = opendir(_PATH_MSGS);
269 err(errno, "%s", _PATH_MSGS);
274 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
275 char *cp = dp->d_name;
282 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
285 i = i * 10 + *cp++ - '0';
287 continue; /* not a message! */
290 if (stat(inbuf, &stbuf) != 0)
292 if (stbuf.st_mtime < keep
293 && stbuf.st_mode&S_IWRITE) {
308 if (blast != 0) /* never lower the upper bound! */
310 firstmsg = lastmsg + 1;
312 else if (blast > lastmsg)
316 bounds = fopen(fname, "w");
318 err(errno, "%s", fname);
320 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
327 * Send mode - place msgs in _PATH_MSGS
329 bounds = fopen(fname, "w");
331 err(errno, "%s", fname);
333 nextmsg = lastmsg + 1;
334 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
335 newmsg = fopen(fname, "w");
337 err(errno, "%s", fname);
340 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
345 signal(SIGINT, onintr);
347 if (isatty(fileno(stdin))) {
348 ptr = getpwuid(uid)->pw_name;
349 printf("Message %d:\nFrom %s %sSubject: ",
350 nextmsg, ptr, ctime(&t));
352 fgets(inbuf, sizeof inbuf, stdin);
355 fprintf(newmsg, "From %s %sSubject: %s\n",
356 ptr, ctime(&t), inbuf);
357 blankline = seensubj = YES;
360 blankline = seensubj = NO;
362 fgets(inbuf, sizeof inbuf, stdin);
363 if (feof(stdin) || ferror(stdin))
365 blankline = (blankline || (inbuf[0] == '\n'));
366 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
367 fputs(inbuf, newmsg);
371 printf("NOTICE: Messages should have a Subject field!\n");
384 * prepare to display messages
386 totty = (isatty(fileno(stdout)) != 0);
387 use_pager = use_pager && totty;
389 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
390 msgsrc = fopen(fname, "r");
393 fscanf(msgsrc, "%d\n", &nextmsg);
395 if (nextmsg > lastmsg+1) {
396 printf("Warning: bounds have been reset (%d, %d)\n",
398 truncate(fname, (off_t)0);
402 rcfirst = nextmsg - rcback;
406 msgsrc = fopen(fname, "r+");
408 msgsrc = fopen(fname, "w");
410 err(errno, "%s", fname);
412 if (rcfirst > lastmsg+1) {
413 printf("Warning: the last message is number %d.\n",
417 if (rcfirst > firstmsg)
418 firstmsg = rcfirst; /* don't set below first msg */
422 fseek(msgsrc, 0L, 0);
423 fprintf(msgsrc, "%d\n", nextmsg);
430 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
433 if (tgetent(inbuf, getenv("TERM")) <= 0
434 || (Lpp = tgetnum("li")) <= 0) {
440 Lpp -= 6; /* for headers, etc. */
446 signal(SIGINT, onintr);
451 for (msg = firstmsg; msg <= lastmsg; msg++) {
453 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
454 newmsg = fopen(fname, "r");
458 gfrsub(newmsg); /* get From and Subject fields */
459 if (locomode && !local) {
464 if (qopt) { /* This has to be located here */
465 printf("There are new messages.\n");
469 if (already && !hdrs)
476 signal(SIGTSTP, onsusp);
477 (void) setjmp(tstpbuf);
481 printf("Message %d:\nFrom %s %s", msg, from, date);
485 printf("Subject: %s", subj);
494 && fgets(inbuf, sizeof inbuf, newmsg)
495 && inbuf[0] != '\n') {
496 fputs(inbuf, stdout);
501 lct = linecnt(newmsg);
503 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
504 (lct == 1) ? "" : "s");
513 * Ask user for command
516 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
520 signal(SIGTSTP, SIG_DFL);
531 printf("--Postponed--\n");
533 /* intentional fall-thru */
536 if (msg >= nextmsg) sep = "Flushed";
542 use_pager = (*in++ == 'p');
543 /* intentional fallthru */
558 prmesg(nlines + lct + (seensubj? 1 : 0));
563 printf("--%s--\n", sep);
565 if (msg >= nextmsg) {
567 fseek(msgsrc, 0L, 0);
568 fprintf(msgsrc, "%d\n", nextmsg);
578 * Make sure .rc file gets updated
580 if (--msg >= nextmsg) {
582 fseek(msgsrc, 0L, 0);
583 fprintf(msgsrc, "%d\n", nextmsg);
586 if (already && !quitit && lastcmd && totty) {
588 * save or reply to last message?
592 if (inbuf[0] == '-' || isdigit(inbuf[0]))
595 if (!(already || hush || qopt))
596 printf("No new messages.\n");
603 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
613 if (use_pager && length > Lpp) {
614 signal(SIGPIPE, SIG_IGN);
615 signal(SIGQUIT, SIG_IGN);
616 if ((env_pager = getenv("PAGER")) == NULL) {
617 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
619 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
621 outf = popen(cmdbuf, "w");
633 while (fgets(inbuf, sizeof inbuf, newmsg)) {
641 if (outf != stdout) {
643 signal(SIGPIPE, SIG_DFL);
644 signal(SIGQUIT, SIG_DFL);
650 /* force wait on output */
651 tcdrain(fileno(stdout));
655 onintr(int unused __unused)
657 signal(SIGINT, onintr);
671 fseek(newmsg, 0L, 2);
677 * We have just gotten a susp. Suspend and prepare to resume.
680 onsusp(int unused __unused)
682 signal(SIGTSTP, SIG_DFL);
685 signal(SIGTSTP, onsusp);
693 off_t oldpos = ftell(f);
697 while (fgets(lbuf, sizeof lbuf, f))
708 sscanf(buf, "%d", &i);
709 sprintf(buf, "Goto %d", i);
714 ask(const char *prompt)
721 printf("%s ", prompt);
724 (void) fgets(inbuf, sizeof inbuf, stdin);
725 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
731 * Handle 'mail' and 'save' here.
733 if ((inch = inbuf[0]) == 's' || inch == 'm') {
736 else if (isdigit(inbuf[1]))
737 cmsg = atoi(&inbuf[1]);
740 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
742 oldpos = ftell(newmsg);
744 cpfrom = fopen(fname, "r");
746 printf("Message %d not found\n", cmsg);
754 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
760 strcpy(fname, "Messages");
761 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
764 strcpy(fname, _PATH_TMP);
767 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
772 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
777 fseek(newmsg, oldpos, 0);
782 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
783 fwrite(inbuf, 1, n, cpto);
787 fseek(newmsg, oldpos, 0); /* reposition current message */
789 printf("Message %d saved in \"%s\"\n", cmsg, fname);
805 seensubj = seenfrom = NO;
807 subj[0] = from[0] = date[0] = '\0';
810 * Is this a normal message?
812 if (fgets(inbuf, sizeof inbuf, infile)) {
813 if (strncmp(inbuf, "From", 4)==0) {
815 * expected form starts with From
818 frompos = ftell(infile);
822 count = sizeof(from) - 1;
823 while (*in && *in > ' ' && count-- > 0) {
824 if (*in == ':' || *in == '@' ||
831 if (*(in = nxtfld(in)))
832 strncpy(date, in, sizeof date);
840 * not the expected form
842 fseek(infile, 0L, 0);
853 * look for Subject line until EOF or a blank line
855 while (fgets(inbuf, sizeof inbuf, infile)
856 && !(blankline = (inbuf[0] == '\n'))) {
858 * extract Subject line
860 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
862 frompos = ftell(infile);
863 strncpy(subj, nxtfld(inbuf), sizeof subj);
870 fseek(infile, frompos, 0);
874 * for possible use with Mail
876 strncpy(subj, "(No Subject)\n", sizeof subj);
880 nxtfld(unsigned char *s)
882 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
883 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */