static char sccsid
[] = "@(#)msgs.c 4.2 %G%";
* msgs - a user bulletin board program
* msgs [fhlopq] [[-]number] to read messages
* msgs -s to place messages
* msgs -c [-days] to clean up the bulletin board
* n flush message, go to next message
* p print message, turn on 'pipe thru more' mode
* P print message, turn off 'pipe thru more' mode
* s[-][<num>] [<filename>] save message
* m[-][<num>] mail with message in temp mbox
* x exit without flushing this message
#define V7 /* will look for TERM in the environment */
#define OBJECT /* will object to messages without Subjects */
/* #define REJECT /* will reject messages without Subjects
(OBJECT must be defined also) */
/* #define UNBUFFERED /* use unbuffered output */
#define CMODE 0666 /* bounds file creation mode */
#define SUPERUSER 0 /* superuser uid */
#define DAEMON 1 /* daemon uid */
#define NLINES 24 /* default number of lines/crt screen */
#define NDAYS 21 /* default keep time for messages */
#define DAYS *24*60*60 /* seconds/day */
#define TEMP "/tmp/msgXXXXXX"
#define MSGSRC ".msgsrc" /* user's rc file */
#define BOUNDS "bounds" /* message bounds file */
#define NEXT "Next message? [yq]"
#define MORE "More? [ynq]"
#define NOMORE "(No more) [q] ?"
struct passwd
*getpwuid();
/* option initialization */
int rcfirst
= 0; /* first message to print (from .rc) */
int rcback
= 0; /* amount to back off of rcfirst*/
int firstmsg
, nextmsg
, lastmsg
= 0;
gtty(fileno(stdout
), &otty
);
ruptible
= (signal(SIGINT
, SIG_IGN
) == SIG_DFL
);
if (isdigit(argv
[0][0])) { /* starting message # */
else if (isdigit(argv
[0][1])) { /* backward offset */
rcback
= atoi( &( argv
[0][1] ) );
while (*ptr
) switch (*ptr
++) {
if (uid
!= SUPERUSER
&& uid
!= DAEMON
) {
fprintf(stderr
, "Sorry\n");
case 'h': /* headers only */
case 'l': /* local msgs only */
case 'o': /* option to save last message */
case 'p': /* pipe thru 'more' during long msgs */
case 'q': /* query only */
case 's': /* sending TO msgs */
"usage: msgs [fhlopq] [[-]number]\n");
* determine current message bounds
sprintf(fname
, "%s/%s", USRMSGS
, BOUNDS
);
bounds
= fopen(fname
, "r");
fscanf(bounds
, "%d %d\n", &firstmsg
, &lastmsg
);
blast
= lastmsg
; /* save upper bound */
keep
= t
- (rcback
? rcback
: NDAYS
) DAYS
;
if (clean
|| bounds
== NULL
) { /* relocate message bounds */
for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)){
register char *cp
= dp
->d_name
;
sprintf(inbuf
, "%s/%s", USRMSGS
, cp
);
i
= i
* 10 + *cp
++ - '0';
continue; /* not a message! */
if (stat(inbuf
, &stbuf
) != 0)
if (stbuf
.st_mtime
< keep
&& stbuf
.st_mode
&S_IWRITE
) {
if (blast
!= 0) /* never lower the upper bound! */
else if (blast
> lastmsg
)
bounds
= fopen(fname
, "w");
fprintf(bounds
, "%d %d\n", firstmsg
, lastmsg
);
* Send mode - place msgs in USRMSGS
bounds
= fopen(fname
, "w");
sprintf(fname
, "%s/%d", USRMSGS
, nextmsg
);
newmsg
= fopen(fname
, "w");
fprintf(bounds
, "%d %d\n", firstmsg
, nextmsg
);
if (isatty(fileno(stdin
))) {
ptr
= getpwuid(uid
)->pw_name
;
printf("Message %d:\nFrom %s %sSubject: ",
nextmsg
, ptr
, ctime(&t
));
fgets(inbuf
, sizeof inbuf
, stdin
);
fprintf(newmsg
, "From %s %sSubject: %s\n",
blankline
= seensubj
= YES
;
blankline
= seensubj
= NO
;
fgets(inbuf
, sizeof inbuf
, stdin
);
if (feof(stdin
) || ferror(stdin
))
blankline
= (blankline
|| (inbuf
[0] == '\n'));
seensubj
= (seensubj
|| (!blankline
&& (strncmp(inbuf
, "Subj", 4) == 0)));
printf("NOTICE: Messages should have a Subject field!\n");
* prepare to display messages
totty
= (isatty(fileno(stdout
)) != 0);
sprintf(fname
, "%s/%s", getenv("HOME"), MSGSRC
);
msgsrc
= fopen(fname
, "r");
fscanf(msgsrc
, "%d\n", &nextmsg
);
rcfirst
= nextmsg
- rcback
;
msgsrc
= fopen(fname
, "a");
fprintf(msgsrc
, "%d\n", nextmsg
);
if (tgetent(inbuf
, getenv("TERM")) <= 0
|| (Lpp
= tgetnum("li")) <= 0) {
Lpp
-= 6; /* for headers, etc. */
for (msg
= firstmsg
; msg
<= lastmsg
; msg
++) {
sprintf(fname
, "%s/%d", USRMSGS
, msg
);
newmsg
= fopen(fname
, "r");
gfrsub(newmsg
); /* get From and Subject fields */
if (locomode
&& !local
) {
if (qopt
) { /* This has to be located here */
printf("There are new messages.\n");
printf("Message %d:\nFrom %s %s", msg
, from
, date
);
printf("Subject: %s", subj
);
&& fgets(inbuf
, sizeof inbuf
, newmsg
)
printf("(%d%slines) ", lct
, seensubj
? " " : " more ");
ask(lct
? MORE
: (msg
==lastmsg
? NOMORE
: NEXT
));
printf("--Postponed--\n");
/* intentional fall-thru */
if (msg
>= nextmsg
) sep
= "Flushed";
/* intentional fallthru */
prmesg(nlines
+ lct
+ (seensubj
? 1 : 0));
fprintf(msgsrc
, "%d\n", nextmsg
);
if (already
&& !quitit
&& lastcmd
&& totty
) {
* save or reply to last message?
if (inbuf
[0] == '-' || isdigit(inbuf
[0]))
if (!(already
|| hush
|| qopt
))
printf("No new messages.\n");
if (pause
&& length
> Lpp
) {
sprintf(cmdbuf
, PAGE
, Lpp
);
outf
= popen(cmdbuf
, "w");
while (fgets(inbuf
, sizeof inbuf
, newmsg
))
/* trick to force wait on output */
stty(fileno(stdout
), &otty
);
while (fgets(lbuf
, sizeof lbuf
, f
))
sprintf(buf
, "Goto %d", i
);
* Handle 'mail' and 'save' here.
if ((inch
= inbuf
[0]) == 's' || inch
== 'm') {
else if (isdigit(inbuf
[1]))
sprintf(fname
, "%s/%d", USRMSGS
, cmsg
);
cpfrom
= fopen(fname
, "r");
printf("Message %d not found\n", cmsg
);
for (n
=0; in
[n
] > ' '; n
++) { /* sizeof fname? */
strcpy(fname
, "Messages");
sprintf(cmdbuf
, MAIL
, fname
);
cpto
= fopen(fname
, "a");
fseek(newmsg
, oldpos
, 0);
while (n
= fread(inbuf
, 1, sizeof inbuf
, cpfrom
))
fwrite(inbuf
, 1, n
, cpto
);
fseek(newmsg
, oldpos
, 0); /* reposition current message */
printf("Message %d saved in \"%s\"\n", cmsg
, fname
);
seensubj
= seenfrom
= NO
;
subj
[0] = from
[0] = date
[0] = NULL
;
* Is this a normal message?
if (fgets(inbuf
, sizeof inbuf
, infile
)) {
if (strncmp(inbuf
, "From", 4)==0) {
* expected form starts with From
if (*in
) while (*in
&& *in
> ' ') {
/* what about sizeof from ? */
strncpy(date
, in
, sizeof date
);
* look for Subject line until EOF or a blank line
while (fgets(inbuf
, sizeof inbuf
, infile
)
&& !(blankline
= (inbuf
[0] == '\n'))) {
if (!seensubj
&& strncmp(inbuf
, "Subj", 4)==0) {
strncpy(subj
, nxtfld(inbuf
), sizeof subj
);
fseek(infile
, frompos
, 0);
* for possible use with Mail
strncpy(subj
, "(No Subject)\n", sizeof subj
);
if (*s
) while (*s
&& *s
> ' ') s
++; /* skip over this field */
if (*s
) while (*s
&& *s
<= ' ') s
++; /* find start of next field */