#include "libmail.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utmpx.h>
#include <syslog.h>
#if !defined(__cplusplus) && !defined(c_plusplus)
typedef void (*SIG_PF) (int);
#endif
#include <unistd.h>
#include <signal.h>
static void
catcher(int arg __unused)
{
}
void
notify(char *user, char *msg, int check_mesg_y, char *etcdir)
{
SIG_PF old;
unsigned int oldalarm;
struct utmpx utmpx, *putmpx = &utmpx;
setutxent();
while ((putmpx = getutxent()) != NULL) {
if (strncmp(user, utmpx.ut_name,
sizeof (utmpx.ut_name)) == 0) {
char tty[sizeof (utmpx.ut_line)+1];
char dev[MAXFILENAME];
FILE *port;
size_t i;
int fd;
for (i = 0; i < sizeof (utmpx.ut_line); i++)
tty[i] = utmpx.ut_line[i];
tty[i] = '\0';
(void) sprintf(dev, "%s/dev/%s", etcdir, tty);
old = (SIG_PF)signal(SIGALRM, catcher);
oldalarm = alarm(300);
if ((fd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
(void) fprintf(stderr,
"Cannot open %s.\n", dev);
continue;
} else {
if (!isatty(fd)) {
(void) fprintf(stderr, "%s in utmpx is "
"not a tty\n", tty);
openlog("mail", 0, LOG_AUTH);
syslog(LOG_CRIT, "%s in utmp is "
"not a tty\n", tty);
closelog();
(void) close(fd);
continue;
}
}
(void) close(fd);
port = fopen(dev, "w");
if (port != 0) {
(void) fprintf(port, "\r\n%s\r\n", msg);
(void) fclose(port);
}
(void) alarm(0);
(void) signal(SIGALRM, old);
(void) alarm(oldalarm);
}
}
endutxent();
}