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 * 4. 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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)msgs.c 8.2 (Berkeley) 4/28/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * msgs - a user bulletin board program
49 * msgs [fhlopq] [[-]number] to read messages
50 * msgs -s to place messages
51 * msgs -c [-days] to clean up the bulletin board
53 * prompt commands are:
55 * n flush message, go to next message
56 * q flush message, quit
57 * p print message, turn on 'pipe thru more' mode
58 * P print message, turn off 'pipe thru more' mode
59 * - reprint last message
60 * s[-][<num>] [<filename>] save message
61 * m[-][<num>] mail with message in temp mbox
62 * x exit without flushing this message
63 * <num> print message number <num>
66 #define V7 /* will look for TERM in the environment */
67 #define OBJECT /* will object to messages without Subjects */
68 /* #define REJECT */ /* will reject messages without Subjects
69 (OBJECT must be defined also) */
70 /* #define UNBUFFERED *//* use unbuffered output */
72 #include <sys/param.h>
90 #include "pathnames.h"
92 #define CMODE 0644 /* bounds file creation mode */
95 #define SUPERUSER 0 /* superuser uid */
96 #define DAEMON 1 /* daemon uid */
97 #define NLINES 24 /* default number of lines/crt screen */
98 #define NDAYS 21 /* default keep time for messages */
99 #define DAYS *24*60*60 /* seconds/day */
100 #define MSGSRC ".msgsrc" /* user's rc file */
101 #define BOUNDS "bounds" /* message bounds file */
102 #define NEXT "Next message? [yq]"
103 #define MORE "More? [ynq]"
104 #define NOMORE "(No more) [q] ?"
110 const char *sep = "-";
112 char fname[MAXPATHLEN];
113 char cmdbuf[MAXPATHLEN + MAXPATHLEN];
139 /* option initialization */
150 static void ask(const char *);
151 static void gfrsub(FILE *);
152 static int linecnt(FILE *);
153 static int next(char *);
154 static char *nxtfld(char *);
155 static void onsusp(int);
156 static void onintr(int);
157 static void prmesg(int);
158 static void usage(void);
161 main(int argc, char *argv[])
164 int rcfirst = 0; /* first message to print (from .rc) */
165 int rcback = 0; /* amount to back off of rcfirst */
166 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
168 struct stat buf; /* stat to check access of bounds */
173 setbuf(stdout, NULL);
175 setlocale(LC_ALL, "");
178 setuid(uid = getuid());
179 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
181 signal(SIGINT, SIG_DFL);
185 if (isdigit(argv[0][0])) { /* starting message # */
186 rcfirst = atoi(argv[0]);
188 else if (isdigit(argv[0][1])) { /* backward offset */
189 rcback = atoi( &( argv[0][1] ) );
193 while (*ptr) switch (*ptr++) {
199 if (uid != SUPERUSER && uid != DAEMON)
201 "only the super-user can use the c flag");
205 case 'f': /* silently */
209 case 'h': /* headers only */
213 case 'l': /* local msgs only */
217 case 'o': /* option to save last message */
221 case 'p': /* pipe thru 'more' during long msgs */
225 case 'q': /* query only */
229 case 's': /* sending TO msgs */
241 * determine current message bounds
243 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
246 * Test access rights to the bounds file
247 * This can be a little tricky. if(send_msg), then
248 * we will create it. We assume that if(send_msg),
249 * then you have write permission there.
250 * Else, it better be there, or we bail.
252 if (send_msg != YES) {
253 if (stat(fname, &buf) < 0) {
255 err(errno, "%s", fname);
261 bounds = fopen(fname, "r");
263 if (bounds != NULL) {
264 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
266 blast = lastmsg; /* save upper bound */
270 keep = t - (rcback? rcback : NDAYS) DAYS;
272 if (clean || bounds == NULL) { /* relocate message bounds */
278 dirp = opendir(_PATH_MSGS);
280 err(errno, "%s", _PATH_MSGS);
285 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
291 if (dp->d_namlen == 0)
295 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
298 i = i * 10 + *cp++ - '0';
300 continue; /* not a message! */
303 if (stat(inbuf, &stbuf) != 0)
305 if (stbuf.st_mtime < keep
306 && stbuf.st_mode&S_IWRITE) {
321 if (blast != 0) /* never lower the upper bound! */
323 firstmsg = lastmsg + 1;
325 else if (blast > lastmsg)
329 bounds = fopen(fname, "w");
331 err(errno, "%s", fname);
333 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
340 * Send mode - place msgs in _PATH_MSGS
342 bounds = fopen(fname, "w");
344 err(errno, "%s", fname);
346 nextmsg = lastmsg + 1;
347 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
348 newmsg = fopen(fname, "w");
350 err(errno, "%s", fname);
353 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
358 signal(SIGINT, onintr);
360 if (isatty(fileno(stdin))) {
361 ptr = getpwuid(uid)->pw_name;
362 printf("Message %d:\nFrom %s %sSubject: ",
363 nextmsg, ptr, ctime(&t));
365 fgets(inbuf, sizeof inbuf, stdin);
368 fprintf(newmsg, "From %s %sSubject: %s\n",
369 ptr, ctime(&t), inbuf);
370 blankline = seensubj = YES;
373 blankline = seensubj = NO;
375 fgets(inbuf, sizeof inbuf, stdin);
376 if (feof(stdin) || ferror(stdin))
378 blankline = (blankline || (inbuf[0] == '\n'));
379 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
380 fputs(inbuf, newmsg);
384 printf("NOTICE: Messages should have a Subject field!\n");
397 * prepare to display messages
399 totty = (isatty(fileno(stdout)) != 0);
400 use_pager = use_pager && totty;
402 if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
403 fprintf(stderr, "Error, no home directory!\n");
406 snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
407 msgsrc = fopen(fname, "r");
410 fscanf(msgsrc, "%d\n", &nextmsg);
412 if (nextmsg > lastmsg+1) {
413 printf("Warning: bounds have been reset (%d, %d)\n",
415 truncate(fname, (off_t)0);
419 rcfirst = nextmsg - rcback;
423 msgsrc = fopen(fname, "r+");
425 msgsrc = fopen(fname, "w");
427 err(errno, "%s", fname);
429 if (rcfirst > lastmsg+1) {
430 printf("Warning: the last message is number %d.\n",
434 if (rcfirst > firstmsg)
435 firstmsg = rcfirst; /* don't set below first msg */
440 fprintf(msgsrc, "%d\n", nextmsg);
447 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
450 if (tgetent(inbuf, getenv("TERM")) <= 0
451 || (Lpp = tgetnum("li")) <= 0) {
457 Lpp -= 6; /* for headers, etc. */
463 signal(SIGINT, onintr);
468 for (msg = firstmsg; msg <= lastmsg; msg++) {
470 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
471 newmsg = fopen(fname, "r");
475 gfrsub(newmsg); /* get From and Subject fields */
476 if (locomode && !local) {
481 if (qopt) { /* This has to be located here */
482 printf("There are new messages.\n");
486 if (already && !hdrs)
493 signal(SIGTSTP, onsusp);
494 (void) setjmp(tstpbuf);
498 printf("Message %d:\nFrom %s %s", msg, from, date);
502 printf("Subject: %s", subj);
511 && fgets(inbuf, sizeof inbuf, newmsg)
512 && inbuf[0] != '\n') {
513 fputs(inbuf, stdout);
518 lct = linecnt(newmsg);
520 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
521 (lct == 1) ? "" : "s");
530 * Ask user for command
533 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
537 signal(SIGTSTP, SIG_DFL);
551 printf("--Postponed--\n");
558 if (msg >= nextmsg) sep = "Flushed";
565 use_pager = (*in++ == 'p');
582 prmesg(nlines + lct + (seensubj? 1 : 0));
587 printf("--%s--\n", sep);
589 if (msg >= nextmsg) {
592 fprintf(msgsrc, "%d\n", nextmsg);
602 * Make sure .rc file gets updated
604 if (--msg >= nextmsg) {
607 fprintf(msgsrc, "%d\n", nextmsg);
610 if (already && !quitit && lastcmd && totty) {
612 * save or reply to last message?
616 if (inbuf[0] == '-' || isdigit(inbuf[0]))
619 if (!(already || hush || qopt))
620 printf("No new messages.\n");
628 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
638 if (use_pager && length > Lpp) {
639 signal(SIGPIPE, SIG_IGN);
640 signal(SIGQUIT, SIG_IGN);
641 if ((env_pager = getenv("PAGER")) == NULL) {
642 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
644 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
646 outf = popen(cmdbuf, "w");
650 setbuf(outf, (char *)NULL);
658 while (fgets(inbuf, sizeof inbuf, newmsg)) {
666 if (outf != stdout) {
668 signal(SIGPIPE, SIG_DFL);
669 signal(SIGQUIT, SIG_DFL);
675 /* force wait on output */
676 tcdrain(fileno(stdout));
680 onintr(int unused __unused)
682 signal(SIGINT, onintr);
696 fseeko(newmsg, (off_t)0, SEEK_END);
702 * We have just gotten a susp. Suspend and prepare to resume.
705 onsusp(int unused __unused)
707 signal(SIGTSTP, SIG_DFL);
710 signal(SIGTSTP, onsusp);
718 off_t oldpos = ftello(f);
722 while (fgets(lbuf, sizeof lbuf, f))
725 fseeko(f, oldpos, SEEK_SET);
733 sscanf(buf, "%d", &i);
734 sprintf(buf, "Goto %d", i);
739 ask(const char *prompt)
746 printf("%s ", prompt);
749 (void) fgets(inbuf, sizeof inbuf, stdin);
750 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
756 * Handle 'mail' and 'save' here.
758 if ((inch = inbuf[0]) == 's' || inch == 'm') {
761 else if (isdigit(inbuf[1]))
762 cmsg = atoi(&inbuf[1]);
765 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
767 oldpos = ftello(newmsg);
769 cpfrom = fopen(fname, "r");
771 printf("Message %d not found\n", cmsg);
779 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
785 strcpy(fname, "Messages");
786 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
789 strcpy(fname, _PATH_TMP);
792 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
797 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
802 fseeko(newmsg, oldpos, SEEK_SET);
807 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
808 fwrite(inbuf, 1, n, cpto);
812 fseeko(newmsg, oldpos, SEEK_SET);/* reposition current message */
814 printf("Message %d saved in \"%s\"\n", cmsg, fname);
830 seensubj = seenfrom = NO;
832 subj[0] = from[0] = date[0] = '\0';
835 * Is this a normal message?
837 if (fgets(inbuf, sizeof inbuf, infile)) {
838 if (strncmp(inbuf, "From", 4)==0) {
840 * expected form starts with From
843 frompos = ftello(infile);
847 count = sizeof(from) - 1;
848 while (*in && *in > ' ' && count-- > 0) {
849 if (*in == ':' || *in == '@' ||
856 if (*(in = nxtfld(in)))
857 strncpy(date, in, sizeof date);
865 * not the expected form
878 * look for Subject line until EOF or a blank line
880 while (fgets(inbuf, sizeof inbuf, infile)
881 && !(blankline = (inbuf[0] == '\n'))) {
883 * extract Subject line
885 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
887 frompos = ftello(infile);
888 strncpy(subj, nxtfld(inbuf), sizeof subj);
895 fseeko(infile, frompos, SEEK_SET);
899 * for possible use with Mail
901 strncpy(subj, "(No Subject)\n", sizeof subj);
907 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
908 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */