static char *sccsid
= "@(#)write.c 4.10 %G%";
#define NMAX sizeof(ubuf.ut_name)
#define LMAX sizeof(ubuf.ut_line)
int signum
[] = {SIGHUP
, SIGINT
, SIGQUIT
, 0};
char me
[NMAX
+ 1] = "???";
int suser
= getuid() == 0;
struct tm
*localclock
= localtime( &clock
);
fprintf(stderr
, "Usage: write user [ttyname]\n");
if ((uf
= fopen("/etc/utmp", "r")) == NULL
) {
perror("write: Can't open /etc/utmp");
fprintf(stderr
, "write: Can't find your tty\n");
if (stat(mytty
, &stbuf
) < 0) {
perror("write: Can't stat your tty");
if (!suser
&& (stbuf
.st_mode
&02) == 0) {
"write: You have write permission turned off\n");
mytty
= rindex(mytty
, '/') + 1;
while (fread((char *)&ubuf
, sizeof(ubuf
), 1, uf
) == 1) {
if (ubuf
.ut_name
[0] == '\0')
if (strcmp(ubuf
.ut_line
, mytty
)==0) {
if (him
[0] == '-' && him
[1] == 0)
if (c2
== 0 || c2
== ' ')
strcat(histty
, ubuf
.ut_line
);
if (logcnt
==0 && histty
[0]=='\0') {
fprintf(stderr
, "write: %s not logged in\n", him
);
if (histtya
==0 && logcnt
> 1) {
"write: %s logged in more than once ... writing to %s\n",
printf(" not on that tty\n"); else
printf(" not logged in\n");
if (access(histty
, 0) < 0) {
fprintf(stderr
, "write: No such tty\n");
if ((tf
= fopen(histty
, "w")) == NULL
)
if (fstat(fileno(tf
), &stbuf
) < 0)
if (!suser
&& (stbuf
.st_mode
&02) == 0)
gethostname(hostname
, sizeof (hostname
));
"\r\nMessage from %s@%s on %s at %d:%02d ...\r\n\007\007\007",
me
, hostname
, mytty
, localclock
->tm_hour
, localclock
->tm_min
);
i
= read(0, buf
, sizeof buf
);
for (bp
= buf
; --i
>= 0; bp
++) {
*bp
== ' ' || *bp
== '\t' || *bp
== '\n') {
if (ferror(tf
) || feof(tf
)) {
printf("\n\007Write failed (%s logged out?)\n",
fprintf(stderr
, "write: Permission denied\n");
fprintf(stderr
, "write: Timeout opening their tty\n");
getenv("SHELL") : "/bin/sh", "sh", "-c", bp
+1, 0);
while (wait((int *)NULL
) != i
)
for (i
=0; signum
[i
]; i
++)