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.3 2003/10/04 20:36:49 hmp 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 */
149 char *nxtfld(unsigned char *);
153 static void usage(void);
156 main(int argc, char **argv)
159 int rcfirst = 0; /* first message to print (from .rc) */
160 int rcback = 0; /* amount to back off of rcfirst */
161 int firstmsg = 0, nextmsg = 0, lastmsg = 0;
163 struct stat buf; /* stat to check access of bounds */
167 setbuf(stdout, NULL);
169 setlocale(LC_ALL, "");
172 setuid(uid = getuid());
173 ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
175 signal(SIGINT, SIG_DFL);
179 if (isdigit(argv[0][0])) { /* starting message # */
180 rcfirst = atoi(argv[0]);
182 else if (isdigit(argv[0][1])) { /* backward offset */
183 rcback = atoi( &( argv[0][1] ) );
187 while (*ptr) switch (*ptr++) {
193 if (uid != SUPERUSER && uid != DAEMON) {
194 fprintf(stderr, "Sorry\n");
200 case 'f': /* silently */
204 case 'h': /* headers only */
208 case 'l': /* local msgs only */
212 case 'o': /* option to save last message */
216 case 'p': /* pipe thru 'more' during long msgs */
220 case 'q': /* query only */
224 case 's': /* sending TO msgs */
236 * determine current message bounds
238 snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
241 * Test access rights to the bounds file
242 * This can be a little tricky. if(send_msg), then
243 * we will create it. We assume that if(send_msg),
244 * then you have write permission there.
245 * Else, it better be there, or we bail.
247 if (send_msg != YES) {
248 if (stat(fname, &buf) < 0) {
250 err(errno, "%s", fname);
256 bounds = fopen(fname, "r");
258 if (bounds != NULL) {
259 fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
261 blast = lastmsg; /* save upper bound */
265 keep = t - (rcback? rcback : NDAYS) DAYS;
267 if (clean || bounds == NULL) { /* relocate message bounds */
273 dirp = opendir(_PATH_MSGS);
275 err(errno, "%s", _PATH_MSGS);
280 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
281 register char *cp = dp->d_name;
286 if (dp->d_namlen == 0)
290 snprintf(inbuf, sizeof(inbuf), "%s/%s", _PATH_MSGS, cp);
293 i = i * 10 + *cp++ - '0';
295 continue; /* not a message! */
298 if (stat(inbuf, &stbuf) != 0)
300 if (stbuf.st_mtime < keep
301 && stbuf.st_mode&S_IWRITE) {
316 if (blast != 0) /* never lower the upper bound! */
318 firstmsg = lastmsg + 1;
320 else if (blast > lastmsg)
324 bounds = fopen(fname, "w");
326 err(errno, "%s", fname);
328 fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
335 * Send mode - place msgs in _PATH_MSGS
337 bounds = fopen(fname, "w");
339 err(errno, "%s", fname);
341 nextmsg = lastmsg + 1;
342 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
343 newmsg = fopen(fname, "w");
345 err(errno, "%s", fname);
348 fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
353 signal(SIGINT, onintr);
355 if (isatty(fileno(stdin))) {
356 ptr = getpwuid(uid)->pw_name;
357 printf("Message %d:\nFrom %s %sSubject: ",
358 nextmsg, ptr, ctime(&t));
360 fgets(inbuf, sizeof inbuf, stdin);
363 fprintf(newmsg, "From %s %sSubject: %s\n",
364 ptr, ctime(&t), inbuf);
365 blankline = seensubj = YES;
368 blankline = seensubj = NO;
370 fgets(inbuf, sizeof inbuf, stdin);
371 if (feof(stdin) || ferror(stdin))
373 blankline = (blankline || (inbuf[0] == '\n'));
374 seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
375 fputs(inbuf, newmsg);
379 printf("NOTICE: Messages should have a Subject field!\n");
392 * prepare to display messages
394 totty = (isatty(fileno(stdout)) != 0);
395 use_pager = use_pager && totty;
397 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
398 msgsrc = fopen(fname, "r");
401 fscanf(msgsrc, "%d\n", &nextmsg);
403 if (nextmsg > lastmsg+1) {
404 printf("Warning: bounds have been reset (%d, %d)\n",
406 truncate(fname, (off_t)0);
410 rcfirst = nextmsg - rcback;
414 msgsrc = fopen(fname, "r+");
416 msgsrc = fopen(fname, "w");
418 err(errno, "%s", fname);
420 if (rcfirst > lastmsg+1) {
421 printf("Warning: the last message is number %d.\n",
425 if (rcfirst > firstmsg)
426 firstmsg = rcfirst; /* don't set below first msg */
430 fseek(msgsrc, 0L, 0);
431 fprintf(msgsrc, "%d\n", nextmsg);
438 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
441 if (tgetent(inbuf, getenv("TERM")) <= 0
442 || (Lpp = tgetnum("li")) <= 0) {
448 Lpp -= 6; /* for headers, etc. */
454 signal(SIGINT, onintr);
459 for (msg = firstmsg; msg <= lastmsg; msg++) {
461 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
462 newmsg = fopen(fname, "r");
466 gfrsub(newmsg); /* get From and Subject fields */
467 if (locomode && !local) {
472 if (qopt) { /* This has to be located here */
473 printf("There are new messages.\n");
477 if (already && !hdrs)
484 signal(SIGTSTP, onsusp);
485 (void) setjmp(tstpbuf);
489 printf("Message %d:\nFrom %s %s", msg, from, date);
493 printf("Subject: %s", subj);
502 && fgets(inbuf, sizeof inbuf, newmsg)
503 && inbuf[0] != '\n') {
504 fputs(inbuf, stdout);
509 lct = linecnt(newmsg);
511 printf("(%d%sline%s) ", lct, seensubj? " " : " more ",
512 (lct == 1) ? "" : "s");
521 * Ask user for command
524 ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
528 signal(SIGTSTP, SIG_DFL);
539 printf("--Postponed--\n");
541 /* intentional fall-thru */
544 if (msg >= nextmsg) sep = "Flushed";
550 use_pager = (*in++ == 'p');
551 /* intentional fallthru */
566 prmesg(nlines + lct + (seensubj? 1 : 0));
571 printf("--%s--\n", sep);
573 if (msg >= nextmsg) {
575 fseek(msgsrc, 0L, 0);
576 fprintf(msgsrc, "%d\n", nextmsg);
586 * Make sure .rc file gets updated
588 if (--msg >= nextmsg) {
590 fseek(msgsrc, 0L, 0);
591 fprintf(msgsrc, "%d\n", nextmsg);
594 if (already && !quitit && lastcmd && totty) {
596 * save or reply to last message?
600 if (inbuf[0] == '-' || isdigit(inbuf[0]))
603 if (!(already || hush || qopt))
604 printf("No new messages.\n");
611 fprintf(stderr, "usage: msgs [fhlopq] [[-]number]\n");
621 if (use_pager && length > Lpp) {
622 signal(SIGPIPE, SIG_IGN);
623 signal(SIGQUIT, SIG_IGN);
624 if ((env_pager = getenv("PAGER")) == NULL) {
625 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
627 snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
629 outf = popen(cmdbuf, "w");
633 setbuf(outf, (char *)NULL);
641 while (fgets(inbuf, sizeof inbuf, newmsg)) {
649 if (outf != stdout) {
651 signal(SIGPIPE, SIG_DFL);
652 signal(SIGQUIT, SIG_DFL);
658 /* force wait on output */
659 tcdrain(fileno(stdout));
665 signal(SIGINT, onintr);
679 fseek(newmsg, 0L, 2);
685 * We have just gotten a susp. Suspend and prepare to resume.
690 signal(SIGTSTP, SIG_DFL);
693 signal(SIGTSTP, onsusp);
701 off_t oldpos = ftell(f);
705 while (fgets(lbuf, sizeof lbuf, f))
716 sscanf(buf, "%d", &i);
717 sprintf(buf, "Goto %d", i);
729 printf("%s ", prompt);
732 (void) fgets(inbuf, sizeof inbuf, stdin);
733 if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
739 * Handle 'mail' and 'save' here.
741 if ((inch = inbuf[0]) == 's' || inch == 'm') {
744 else if (isdigit(inbuf[1]))
745 cmsg = atoi(&inbuf[1]);
748 snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
750 oldpos = ftell(newmsg);
752 cpfrom = fopen(fname, "r");
754 printf("Message %d not found\n", cmsg);
762 for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
768 strcpy(fname, "Messages");
769 fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND);
772 strcpy(fname, _PATH_TMP);
775 snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL,
780 if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
785 fseek(newmsg, oldpos, 0);
790 while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
791 fwrite(inbuf, 1, n, cpto);
795 fseek(newmsg, oldpos, 0); /* reposition current message */
797 printf("Message %d saved in \"%s\"\n", cmsg, fname);
813 seensubj = seenfrom = NO;
815 subj[0] = from[0] = date[0] = NULL;
818 * Is this a normal message?
820 if (fgets(inbuf, sizeof inbuf, infile)) {
821 if (strncmp(inbuf, "From", 4)==0) {
823 * expected form starts with From
826 frompos = ftell(infile);
830 count = sizeof(from) - 1;
831 while (*in && *in > ' ' && count-- > 0) {
832 if (*in == ':' || *in == '@' ||
839 if (*(in = nxtfld(in)))
840 strncpy(date, in, sizeof date);
848 * not the expected form
850 fseek(infile, 0L, 0);
861 * look for Subject line until EOF or a blank line
863 while (fgets(inbuf, sizeof inbuf, infile)
864 && !(blankline = (inbuf[0] == '\n'))) {
866 * extract Subject line
868 if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
870 frompos = ftell(infile);
871 strncpy(subj, nxtfld(inbuf), sizeof subj);
878 fseek(infile, frompos, 0);
882 * for possible use with Mail
884 strncpy(subj, "(No Subject)\n", sizeof subj);
888 nxtfld(unsigned char *s)
890 if (*s) while (*s && !isspace(*s)) s++; /* skip over this field */
891 if (*s) while (*s && isspace(*s)) s++; /* find start of next field */