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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * msgs - a user bulletin board program
53 * msgs [fhlopq] [[-]number] to read messages
54 * msgs -s to place messages
55 * msgs -c [-days] to clean up the bulletin board
57 * prompt commands are:
59 * n flush message, go to next message
60 * q flush message, quit
61 * p print message, turn on 'pipe thru more' mode
62 * P print message, turn off 'pipe thru more' mode
63 * - reprint last message
64 * s[-][<num>] [<filename>] save message
65 * m[-][<num>] mail with message in temp mbox
66 * x exit without flushing this message
67 * <num> print message number <num>
70 #define V7 /* will look for TERM in the environment */
71 #define OBJECT /* will object to messages without Subjects */
72 /* #define REJECT */ /* will reject messages without Subjects
73 (OBJECT must be defined also) */
74 /* #define UNBUFFERED *//* use unbuffered output */
76 #include <sys/param.h>
94 #include "pathnames.h"
96 #define CMODE 0644 /* bounds file creation mode */
99 #define SUPERUSER 0 /* superuser uid */
100 #define DAEMON 1 /* daemon uid */
101 #define NLINES 24 /* default number of lines/crt screen */
102 #define NDAYS 21 /* default keep time for messages */
103 #define DAYS *24*60*60 /* seconds/day */
104 #define MSGSRC ".msgsrc" /* user's rc file */
105 #define BOUNDS "bounds" /* message bounds file */
106 #define NEXT "Next message? [yq]"
107 #define MORE "More? [ynq]"
108 #define NOMORE "(No more) [q] ?"
114 const char *sep = "-";
116 char fname[MAXPATHLEN];
117 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
143 /* option initialization */
154 static void ask(const char *);
155 static void gfrsub(FILE *);
156 static int linecnt(FILE *);
157 static int next(char *);
158 static char *nxtfld(char *);
159 static void onsusp(int);
160 static void onintr(int);
161 static void prmesg(int);
162 static void usage(void);
165 main(int argc, char *argv[])
168 int rcfirst = 0; /* first message to print (from .rc) */
169 int rcback = 0; /* amount to back off of rcfirst */
170 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
172 struct stat buf; /* stat to check access of bounds */
177 setbuf(stdout, NULL);
179 setlocale(LC_ALL, "");
182 if (setuid(uid = getuid()) != 0)
183 err(1, "setuid failed");
184 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
186 signal(SIGINT, SIG_DFL);
190 if (isdigit(argv[0][0])) { /* starting message # */
191 rcfirst = atoi(argv[0]);
193 else if (isdigit(argv[0][1])) { /* backward offset */
194 rcback = atoi( &( argv[0][1] ) );
198 while (*ptr) switch (*ptr++) {
204 if (uid != SUPERUSER && uid != DAEMON)
206 "only the super-user can use the c flag");
210 case 'f': /* silently */
214 case 'h': /* headers only */
218 case 'l': /* local msgs only */
222 case 'o': /* option to save last message */
226 case 'p': /* pipe thru 'more' during long msgs */
230 case 'q': /* query only */
234 case 's': /* sending TO msgs */
246 * determine current message bounds
248 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
251 * Test access rights to the bounds file
252 * This can be a little tricky. if(send_msg), then
253 * we will create it. We assume that if(send_msg),
254 * then you have write permission there.
255 * Else, it better be there, or we bail.
257 if (send_msg != YES) {
258 if (stat(fname, &buf) < 0) {
260 err(errno, "%s", fname);
266 bounds = fopen(fname, "r");
268 if (bounds != NULL) {
269 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
271 blast = lastmsg; /* save upper bound */
275 keep = t - (rcback? rcback : NDAYS) DAYS;
277 if (clean || bounds == NULL) { /* relocate message bounds */
283 dirp = opendir(_PATH_MSGS);
285 err(errno, "%s", _PATH_MSGS);
290 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
296 if (dp->d_namlen == 0)
300 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
303 i = i * 10 + *cp++ - '0';
305 continue; /* not a message! */
308 if (stat(inbuf, &stbuf) != 0)
310 if (stbuf.st_mtime < keep
311 && stbuf.st_mode&S_IWRITE) {
326 if (blast != 0) /* never lower the upper bound! */
328 firstmsg = lastmsg + 1;
330 else if (blast > lastmsg)
334 bounds = fopen(fname, "w");
336 err(errno, "%s", fname);
338 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
345 * Send mode - place msgs in _PATH_MSGS
347 bounds = fopen(fname, "w");
349 err(errno, "%s", fname);
351 nextmsg = lastmsg + 1;
352 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
353 newmsg = fopen(fname, "w");
355 err(errno, "%s", fname);
358 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
363 signal(SIGINT, onintr);
365 if (isatty(fileno(stdin))) {
366 ptr = getpwuid(uid)->pw_name;
367 printf("Message %d:\nFrom %s %sSubject: ",
368 nextmsg, ptr, ctime(&t));
370 fgets(inbuf, sizeof inbuf, stdin);
373 fprintf(newmsg, "From %s %sSubject: %s\n",
374 ptr, ctime(&t), inbuf);
375 blankline = seensubj = YES;
378 blankline = seensubj = NO;
380 fgets(inbuf, sizeof inbuf, stdin);
381 if (feof(stdin) || ferror(stdin))
383 blankline = (blankline || (inbuf[0] == '\n'));
384 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
385 fputs(inbuf, newmsg);
389 printf("NOTICE: Messages should have a Subject field!\n");
402 * prepare to display messages
404 totty = (isatty(fileno(stdout)) != 0);
405 use_pager = use_pager && totty;
407 if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
408 fprintf(stderr, "Error, no home directory!\n");
411 snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
412 msgsrc = fopen(fname, "r");
415 fscanf(msgsrc, "%d\n", &nextmsg);
417 if (nextmsg > lastmsg+1) {
418 printf("Warning: bounds have been reset (%d, %d)\n",
420 truncate(fname, (off_t)0);
424 rcfirst = nextmsg - rcback;
428 msgsrc = fopen(fname, "r+");
430 msgsrc = fopen(fname, "w");
432 err(errno, "%s", fname);
434 if (rcfirst > lastmsg+1) {
435 printf("Warning: the last message is number %d.\n",
439 if (rcfirst > firstmsg)
440 firstmsg = rcfirst; /* don't set below first msg */
445 fprintf(msgsrc, "%d\n", nextmsg);
452 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
455 if (tgetent(inbuf, getenv("TERM")) <= 0
456 || (Lpp = tgetnum("li")) <= 0) {
462 Lpp -= 6; /* for headers, etc. */
468 signal(SIGINT, onintr);
473 for (msg = firstmsg; msg <= lastmsg; msg++) {
475 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
476 newmsg = fopen(fname, "r");
480 gfrsub(newmsg); /* get From and Subject fields */
481 if (locomode && !local) {
486 if (qopt) { /* This has to be located here */
487 printf("There are new messages.\n");
491 if (already && !hdrs)
498 signal(SIGTSTP, onsusp);
499 (void) setjmp(tstpbuf);
503 printf("Message %d:\nFrom %s %s", msg, from, date);
507 printf("Subject: %s", subj);
516 && fgets(inbuf, sizeof inbuf, newmsg)
517 && inbuf[0] != '\n') {
518 fputs(inbuf, stdout);
523 lct = linecnt(newmsg);
525 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
526 (lct == 1) ? "" : "s");
535 * Ask user for command
538 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
542 signal(SIGTSTP, SIG_DFL);
556 printf("--Postponed--\n");
563 if (msg >= nextmsg) sep = "Flushed";
570 use_pager = (*in++ == 'p');
587 prmesg(nlines + lct + (seensubj? 1 : 0));
592 printf("--%s--\n", sep);
594 if (msg >= nextmsg) {
597 fprintf(msgsrc, "%d\n", nextmsg);
607 * Make sure .rc file gets updated
609 if (--msg >= nextmsg) {
612 fprintf(msgsrc, "%d\n", nextmsg);
615 if (already && !quitit && lastcmd && totty) {
617 * save or reply to last message?
621 if (inbuf[0] == '-' || isdigit(inbuf[0]))
624 if (!(already || hush || qopt))
625 printf("No new messages.\n");
633 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
643 if (use_pager && length > Lpp) {
644 signal(SIGPIPE, SIG_IGN);
645 signal(SIGQUIT, SIG_IGN);
646 if ((env_pager = getenv("PAGER")) == NULL) {
647 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
649 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
651 outf = popen(cmdbuf, "w");
655 setbuf(outf, (char *)NULL);
663 while (fgets(inbuf, sizeof inbuf, newmsg)) {
671 if (outf != stdout) {
673 signal(SIGPIPE, SIG_DFL);
674 signal(SIGQUIT, SIG_DFL);
680 /* force wait on output */
681 tcdrain(fileno(stdout));
685 onintr(int unused __unused)
687 signal(SIGINT, onintr);
701 fseeko(newmsg, (off_t)0, SEEK_END);
707 * We have just gotten a susp. Suspend and prepare to resume.
710 onsusp(int unused __unused)
712 signal(SIGTSTP, SIG_DFL);
715 signal(SIGTSTP, onsusp);
723 off_t oldpos = ftello(f);
727 while (fgets(lbuf, sizeof lbuf, f))
730 fseeko(f, oldpos, SEEK_SET);
738 sscanf(buf, "%d", &i);
739 sprintf(buf, "Goto %d", i);
744 ask(const char *prompt)
751 printf("%s ", prompt);
754 (void) fgets(inbuf, sizeof inbuf, stdin);
755 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
761 * Handle 'mail' and 'save' here.
763 if ((inch = inbuf[0]) == 's' || inch == 'm') {
766 else if (isdigit(inbuf[1]))
767 cmsg = atoi(&inbuf[1]);
770 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
772 oldpos = ftello(newmsg);
774 cpfrom = fopen(fname, "r");
776 printf("Message %d not found\n", cmsg);
784 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
790 strcpy(fname, "Messages");
791 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
794 strcpy(fname, _PATH_TMP);
797 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
802 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
807 fseeko(newmsg, oldpos, SEEK_SET);
812 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
813 fwrite(inbuf, 1, n, cpto);
817 fseeko(newmsg, oldpos, SEEK_SET);/* reposition current message */
819 printf("Message %d saved in \"%s\"\n", cmsg, fname);
835 seensubj = seenfrom = NO;
837 subj[0] = from[0] = date[0] = '\0';
840 * Is this a normal message?
842 if (fgets(inbuf, sizeof inbuf, infile)) {
843 if (strncmp(inbuf, "From", 4)==0) {
845 * expected form starts with From
848 frompos = ftello(infile);
852 count = sizeof(from) - 1;
853 while (*in && *in > ' ' && count-- > 0) {
854 if (*in == ':' || *in == '@' ||
861 if (*(in = nxtfld(in)))
862 strncpy(date, in, sizeof date);
870 * not the expected form
883 * look for Subject line until EOF or a blank line
885 while (fgets(inbuf, sizeof inbuf, infile)
886 && !(blankline = (inbuf[0] == '\n'))) {
888 * extract Subject line
890 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
892 frompos = ftello(infile);
893 strncpy(subj, nxtfld(inbuf), sizeof subj);
900 fseeko(infile, frompos, SEEK_SET);
904 * for possible use with Mail
906 strncpy(subj, "(No Subject)\n", sizeof subj);
912 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
913 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */