static char *sccsid
= "@(#)write.c 4.13 %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 ((stbuf
.st_mode
&020) == 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
== ' ')
if (histtya
&& strncmp(histtya
, ubuf
.ut_line
,
if (histty
[0]==0 || nomesg
&& histtya
== 0) {
strcat(ttybuf
, ubuf
.ut_line
);
if (access(ttybuf
, 0) < 0 || stat(ttybuf
, &stbuf
) < 0 ||
(stbuf
.st_mode
&020) == 0)
fprintf(stderr
, "write: %s not logged in%s\n", him
,
histtya
? " on that tty" : "");
if (histtya
==0 && logcnt
> 1) {
"write: %s logged in more than once ... writing to %s\n",
if (access(histty
, 0) < 0) {
fprintf(stderr
, "write: No such tty\n");
if ((tf
= fopen(histty
, "w")) == NULL
) {
fprintf(stderr
, "write: Permission denied\n");
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: Timeout opening their tty\n");
fclose(tf
); /* Close his terminal */
setgid(getgid()); /* Give up effective group privs */
getenv("SHELL") : "/bin/sh", "sh", "-c", bp
+1, 0);
while (wait((int *)NULL
) != i
)
for (i
=0; signum
[i
]; i
++)