2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * msgs - a user bulletin board program
51 * msgs [fhlopq] [[-]number] to read messages
52 * msgs -s to place messages
53 * msgs -c [-days] to clean up the bulletin board
55 * prompt commands are:
57 * n flush message, go to next message
58 * q flush message, quit
59 * p print message, turn on 'pipe thru more' mode
60 * P print message, turn off 'pipe thru more' mode
61 * - reprint last message
62 * s[-][<num>] [<filename>] save message
63 * m[-][<num>] mail with message in temp mbox
64 * x exit without flushing this message
65 * <num> print message number <num>
68 #define V7 /* will look for TERM in the environment */
69 #define OBJECT /* will object to messages without Subjects */
70 /* #define REJECT */ /* will reject messages without Subjects
71 (OBJECT must be defined also) */
72 /* #define UNBUFFERED *//* use unbuffered output */
74 #include <sys/param.h>
92 #include "pathnames.h"
94 #define CMODE 0644 /* bounds file creation mode */
97 #define SUPERUSER 0 /* superuser uid */
98 #define DAEMON 1 /* daemon uid */
99 #define NLINES 24 /* default number of lines/crt screen */
100 #define NDAYS 21 /* default keep time for messages */
101 #define DAYS *24*60*60 /* seconds/day */
102 #define MSGSRC ".msgsrc" /* user's rc file */
103 #define BOUNDS "bounds" /* message bounds file */
104 #define NEXT "Next message? [yq]"
105 #define MORE "More? [ynq]"
106 #define NOMORE "(No more) [q] ?"
112 static const char *sep = "-";
113 static char inbuf[BUFSIZ];
114 static char fname[MAXPATHLEN];
115 static char cmdbuf[MAXPATHLEN + MAXPATHLEN];
116 static char subj[128];
117 static char from[128];
118 static char date[128];
122 static bool ruptible;
124 static bool seenfrom;
125 static bool seensubj;
126 static bool blankline;
127 static bool printing = NO;
128 static bool mailing = NO;
129 static bool quitit = NO;
130 static bool sending = NO;
131 static bool intrpflg = NO;
141 /* option initialization */
142 static bool hdrs = NO;
143 static bool qopt = NO;
144 static bool hush = NO;
145 static bool send_msg = NO;
146 static bool locomode = NO;
147 static bool use_pager = NO;
148 static bool clean = NO;
149 static bool lastcmd = NO;
150 static jmp_buf tstpbuf;
152 static void ask(const char *);
153 static void gfrsub(FILE *);
154 static int linecnt(FILE *);
155 static int next(char *);
156 static char *nxtfld(char *);
157 static void onsusp(int);
158 static void onintr(int);
159 static void prmesg(int);
160 static void usage(void);
163 main(int argc, char *argv[])
166 int rcfirst = 0; /* first message to print (from .rc) */
167 int rcback = 0; /* amount to back off of rcfirst */
168 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
170 struct stat buf; /* stat to check access of bounds */
175 setbuf(stdout, NULL);
177 setlocale(LC_ALL, "");
180 if (setuid(uid = getuid()) != 0)
181 err(1, "setuid failed");
182 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
184 signal(SIGINT, SIG_DFL);
188 if (isdigit(argv[0][0])) { /* starting message # */
189 rcfirst = atoi(argv[0]);
191 else if (isdigit(argv[0][1])) { /* backward offset */
192 rcback = atoi( &( argv[0][1] ) );
196 while (*ptr) switch (*ptr++) {
202 if (uid != SUPERUSER && uid != DAEMON)
204 "only the super-user can use the c flag");
208 case 'f': /* silently */
212 case 'h': /* headers only */
216 case 'l': /* local msgs only */
220 case 'o': /* option to save last message */
224 case 'p': /* pipe thru 'more' during long msgs */
228 case 'q': /* query only */
232 case 's': /* sending TO msgs */
244 * determine current message bounds
246 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
249 * Test access rights to the bounds file
250 * This can be a little tricky. if(send_msg), then
251 * we will create it. We assume that if(send_msg),
252 * then you have write permission there.
253 * Else, it better be there, or we bail.
255 if (send_msg != YES) {
256 if (stat(fname, &buf) < 0) {
258 err(errno, "%s", fname);
264 bounds = fopen(fname, "r");
266 if (bounds != NULL) {
267 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
269 blast = lastmsg; /* save upper bound */
273 keep = t - (rcback? rcback : NDAYS) DAYS;
275 if (clean || bounds == NULL) { /* relocate message bounds */
281 dirp = opendir(_PATH_MSGS);
283 err(errno, "%s", _PATH_MSGS);
288 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
294 if (dp->d_namlen == 0)
298 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
301 i = i * 10 + *cp++ - '0';
303 continue; /* not a message! */
306 if (stat(inbuf, &stbuf) != 0)
308 if (stbuf.st_mtime < keep
309 && stbuf.st_mode&S_IWRITE) {
324 if (blast != 0) /* never lower the upper bound! */
326 firstmsg = lastmsg + 1;
328 else if (blast > lastmsg)
332 bounds = fopen(fname, "w");
334 err(errno, "%s", fname);
336 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
343 * Send mode - place msgs in _PATH_MSGS
345 bounds = fopen(fname, "w");
347 err(errno, "%s", fname);
349 nextmsg = lastmsg + 1;
350 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
351 newmsg = fopen(fname, "w");
353 err(errno, "%s", fname);
356 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
361 signal(SIGINT, onintr);
363 if (isatty(fileno(stdin))) {
364 ptr = getpwuid(uid)->pw_name;
365 printf("Message %d:\nFrom %s %sSubject: ",
366 nextmsg, ptr, ctime(&t));
368 fgets(inbuf, sizeof inbuf, stdin);
371 fprintf(newmsg, "From %s %sSubject: %s\n",
372 ptr, ctime(&t), inbuf);
373 blankline = seensubj = YES;
376 blankline = seensubj = NO;
378 fgets(inbuf, sizeof inbuf, stdin);
379 if (feof(stdin) || ferror(stdin))
381 blankline = (blankline || (inbuf[0] == '\n'));
382 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
383 fputs(inbuf, newmsg);
387 printf("NOTICE: Messages should have a Subject field!\n");
400 * prepare to display messages
402 totty = (isatty(fileno(stdout)) != 0);
403 use_pager = use_pager && totty;
405 if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
406 fprintf(stderr, "Error, no home directory!\n");
409 snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
410 msgsrc = fopen(fname, "r");
413 fscanf(msgsrc, "%d\n", &nextmsg);
415 if (nextmsg > lastmsg+1) {
416 printf("Warning: bounds have been reset (%d, %d)\n",
418 truncate(fname, (off_t)0);
422 rcfirst = nextmsg - rcback;
426 msgsrc = fopen(fname, "r+");
428 msgsrc = fopen(fname, "w");
430 err(errno, "%s", fname);
432 if (rcfirst > lastmsg+1) {
433 printf("Warning: the last message is number %d.\n",
437 if (rcfirst > firstmsg)
438 firstmsg = rcfirst; /* don't set below first msg */
443 fprintf(msgsrc, "%d\n", nextmsg);
450 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
453 if (tgetent(inbuf, getenv("TERM")) <= 0
454 || (Lpp = tgetnum("li")) <= 0) {
460 Lpp -= 6; /* for headers, etc. */
466 signal(SIGINT, onintr);
471 for (msg = firstmsg; msg <= lastmsg; msg++) {
473 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
474 newmsg = fopen(fname, "r");
478 gfrsub(newmsg); /* get From and Subject fields */
479 if (locomode && !local) {
484 if (qopt) { /* This has to be located here */
485 printf("There are new messages.\n");
489 if (already && !hdrs)
496 signal(SIGTSTP, onsusp);
497 (void) setjmp(tstpbuf);
501 printf("Message %d:\nFrom %s %s", msg, from, date);
505 printf("Subject: %s", subj);
514 && fgets(inbuf, sizeof inbuf, newmsg)
515 && inbuf[0] != '\n') {
516 fputs(inbuf, stdout);
521 lct = linecnt(newmsg);
523 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
524 (lct == 1) ? "" : "s");
533 * Ask user for command
536 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
540 signal(SIGTSTP, SIG_DFL);
554 printf("--Postponed--\n");
561 if (msg >= nextmsg) sep = "Flushed";
568 use_pager = (*in++ == 'p');
585 prmesg(nlines + lct + (seensubj? 1 : 0));
590 printf("--%s--\n", sep);
592 if (msg >= nextmsg) {
595 fprintf(msgsrc, "%d\n", nextmsg);
605 * Make sure .rc file gets updated
607 if (--msg >= nextmsg) {
610 fprintf(msgsrc, "%d\n", nextmsg);
613 if (already && !quitit && lastcmd && totty) {
615 * save or reply to last message?
619 if (inbuf[0] == '-' || isdigit(inbuf[0]))
622 if (!(already || hush || qopt))
623 printf("No new messages.\n");
631 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
641 if (use_pager && length > Lpp) {
642 signal(SIGPIPE, SIG_IGN);
643 signal(SIGQUIT, SIG_IGN);
644 if ((env_pager = getenv("PAGER")) == NULL) {
645 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
647 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
649 outf = popen(cmdbuf, "w");
653 setbuf(outf, (char *)NULL);
661 while (fgets(inbuf, sizeof inbuf, newmsg)) {
669 if (outf != stdout) {
671 signal(SIGPIPE, SIG_DFL);
672 signal(SIGQUIT, SIG_DFL);
678 /* force wait on output */
679 tcdrain(fileno(stdout));
683 onintr(int unused __unused)
685 signal(SIGINT, onintr);
699 fseeko(newmsg, (off_t)0, SEEK_END);
705 * We have just gotten a susp. Suspend and prepare to resume.
708 onsusp(int unused __unused)
710 signal(SIGTSTP, SIG_DFL);
713 signal(SIGTSTP, onsusp);
721 off_t oldpos = ftello(f);
725 while (fgets(lbuf, sizeof lbuf, f))
728 fseeko(f, oldpos, SEEK_SET);
736 sscanf(buf, "%d", &i);
737 sprintf(buf, "Goto %d", i);
742 ask(const char *prompt)
749 printf("%s ", prompt);
752 (void) fgets(inbuf, sizeof inbuf, stdin);
753 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
759 * Handle 'mail' and 'save' here.
761 if ((inch = inbuf[0]) == 's' || inch == 'm') {
764 else if (isdigit(inbuf[1]))
765 cmsg = atoi(&inbuf[1]);
768 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
770 oldpos = ftello(newmsg);
772 cpfrom = fopen(fname, "r");
774 printf("Message %d not found\n", cmsg);
782 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
788 strcpy(fname, "Messages");
789 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
792 strcpy(fname, _PATH_TMP);
795 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
800 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
805 fseeko(newmsg, oldpos, SEEK_SET);
811 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
812 fwrite(inbuf, 1, n, cpto);
816 fseeko(newmsg, oldpos, SEEK_SET);/* reposition current message */
818 printf("Message %d saved in \"%s\"\n", cmsg, fname);
834 seensubj = seenfrom = NO;
836 subj[0] = from[0] = date[0] = '\0';
839 * Is this a normal message?
841 if (fgets(inbuf, sizeof inbuf, infile)) {
842 if (strncmp(inbuf, "From", 4)==0) {
844 * expected form starts with From
847 frompos = ftello(infile);
851 count = sizeof(from) - 1;
852 while (*in && *in > ' ' && count-- > 0) {
853 if (*in == ':' || *in == '@' ||
860 if (*(in = nxtfld(in)))
861 strlcpy(date, in, sizeof date);
869 * not the expected form
882 * look for Subject line until EOF or a blank line
884 while (fgets(inbuf, sizeof inbuf, infile)
885 && !(blankline = (inbuf[0] == '\n'))) {
887 * extract Subject line
889 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
891 frompos = ftello(infile);
892 strlcpy(subj, nxtfld(inbuf), sizeof subj);
899 fseeko(infile, frompos, SEEK_SET);
903 * for possible use with Mail
905 strlcpy(subj, "(No Subject)\n", sizeof subj);
911 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
912 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */