static char sccsid
[] = "@(#)comsat.c 4.14 (Berkeley) %G%";
#define dprintf if (debug) printf
struct sockaddr_in sin
= { AF_INET
};
struct utmp
*utmp
= NULL
;
unsigned utmpmtime
= 0; /* last modification time for utmp */
unsigned utmpsize
= 0; /* last malloced size for utmp */
char *malloc(), *realloc();
#define NAMLEN (sizeof (uts[0].ut_name) + 1)
/* verify proper invocation */
if (getsockname(0, &from
, &fromlen
) < 0) {
fprintf(stderr
, "%s: ", argv
[0]);
chdir("/usr/spool/mail");
if ((uf
= open("/etc/utmp",0)) < 0) {
syslog(LOG_ERR
, "/etc/utmp: %m");
(void) recv(0, msgbuf
, sizeof (msgbuf
) - 1, 0);
gethostname(hostname
, sizeof (hostname
));
signal(SIGTTOU
, SIG_IGN
);
signal(SIGCHLD
, reapchildren
);
cc
= recv(0, msgbuf
, sizeof (msgbuf
) - 1, 0);
while (wait3((struct wait
*)0, WNOHANG
, (struct rusage
*)0) > 0)
if (time(0) - lastmsgtime
>= MAXIDLE
)
if (statbf
.st_mtime
> utmpmtime
) {
utmpmtime
= statbf
.st_mtime
;
if (statbf
.st_size
> utmpsize
) {
utmpsize
= statbf
.st_size
+ 10 * sizeof(struct utmp
);
utmp
= (struct utmp
*)realloc(utmp
, utmpsize
);
utmp
= (struct utmp
*)malloc(utmpsize
);
dprintf("malloc failed\n");
nutmp
= read(uf
,utmp
,statbf
.st_size
)/sizeof(struct utmp
);
register struct utmp
*utp
= &utmp
[nutmp
];
dprintf("mailfor %s\n", name
);
while (*cp
&& *cp
!= '@')
if (!strncmp(utp
->ut_name
, name
, sizeof(utmp
[0].ut_name
)))
register struct utmp
*utp
;
char tty
[20], name
[sizeof (utmp
[0].ut_name
) + 1];
strncat(tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
dprintf("notify %s on %s\n", utp
->ut_name
, tty
);
if (stat(tty
, &stb
) == 0 && (stb
.st_mode
& 0100) == 0) {
signal(SIGALRM
, SIG_DFL
);
if ((tp
= fopen(tty
,"w")) == 0) {
dprintf("fopen failed\n");
ioctl(fileno(tp
), TIOCGETP
, >tybuf
);
cr
= (gttybuf
.sg_flags
& CRMOD
) ? "" : "\r";
strncpy(name
, utp
->ut_name
, sizeof (utp
->ut_name
));
name
[sizeof (name
) - 1] = '\0';
fprintf(tp
,"%s\n\007New mail for %s@%s\007 has arrived:%s\n",
fprintf(tp
,"----%s\n", cr
);
jkfprintf(tp
, name
, offset
);
jkfprintf(tp
, name
, offset
)
register int linecnt
, charcnt
;
dprintf("HERE %s's mail starting at %d\n",
if ((fi
= fopen(name
,"r")) == NULL
) {
dprintf("Cant read the mail\n");
fseek(fi
, offset
, L_SET
);
* Print the first 7 lines or 560 characters of the new mail
* (whichever comes first). Skip header crap other than
* From, Subject, To, and Date.
while (fgets(line
, sizeof (line
), fi
) != NULL
) {
if (linecnt
<= 0 || charcnt
<= 0) {
fprintf(tp
,"...more...%s\n", cr
);
if (strncmp(line
, "From ", 5) == 0)
if (inheader
&& (line
[0] == ' ' || line
[0] == '\t'))
if (cp
== 0 || (index(line
, ' ') && index(line
, ' ') < cp
))
strncmp(line
, "Date", cnt
) &&
strncmp(line
, "From", cnt
) &&
strncmp(line
, "Subject", cnt
) &&
strncmp(line
, "To", cnt
))
fprintf(tp
,"%s%s\n", line
, cr
);
linecnt
--, charcnt
-= strlen(line
);
fprintf(tp
,"----%s\n", cr
);