From b8915c9cd49800455a0b6872ac10a8a10fc75bbf Mon Sep 17 00:00:00 2001 From: gdr-ftp Date: Fri, 10 Apr 1998 20:03:55 +0000 Subject: [PATCH] This is write version 2.1 from 8 March 1994. The manual page has been converted from preformatted ascii to nroff source. Leslie's email address was also updated. --- usr.bin/write/README | 25 ++++++++ usr.bin/write/write.1 | 42 +++++++++++++ usr.bin/write/write.c | 143 +++++++++++++++++++++++------------------- 3 files changed, 145 insertions(+), 65 deletions(-) create mode 100644 usr.bin/write/README create mode 100644 usr.bin/write/write.1 diff --git a/usr.bin/write/README b/usr.bin/write/README new file mode 100644 index 0000000..49d4249 --- /dev/null +++ b/usr.bin/write/README @@ -0,0 +1,25 @@ +write v2.1 README file. + +write v2.1 _requires_ at least GNO v2.0.5 or the multi-user update. + +What's changed: + - changed error messages to go to stderr (previously to stdout). + - added several checks on ut_type to insure validation of utmp entry and + not write to entries marked UT_INVISIBLE. + - fixed bug which would have caused problems in an fputs() command iff + the length of the line name was the maximum allowed. + - have it on authority that the CRs appended to the ends of the output + lines will be correctly translated. This was questionable when 2.0 was + sent out. + +Special thanks to Phil Vandry again for suggestions - he sent me source after +the MU upgrade, but it got corrupted. He sent me a new copy, and I used it to +modify the new code. Thanks :-) + +Please send all requests to: +America On-Line: WDPhoenix (@aol.com) +Delphi: WDPHOENIX (@delphi.com) +US Snail: Les Barstow + 275 Route 10 East + Suite 220-136 + Succasunna, NJ 07876 diff --git a/usr.bin/write/write.1 b/usr.bin/write/write.1 new file mode 100644 index 0000000..e0c41db --- /dev/null +++ b/usr.bin/write/write.1 @@ -0,0 +1,42 @@ +.\" Man page converted from formatted ascii to nroff source by +.\" Devin Reade, 10 April 1998. +.\" +.\" $Id: write.1,v 1.1 1998/04/10 20:03:53 gdr-ftp Exp $ +.\" +.TH WRITE 1 "8 March 1994" GNO "Commands and Applications" +.SH NAME +write \- write messages to a user's terminal +.SH SYNOPSIS +.B write +.IR username " [" message ] +.SH DESCRIPTION +.BR write +writes messages to all terminals belonging to +.IR username . +If an optional +.I message +is included on the command line, then that message is the one sent to the +user. If no message is included on the command line, +.BR write +takes input from standard input until a blank line is encountered. +It sends each line of input as it is received. +.SH ERRORS +.BR write +returns in error if the user does not exist or if the user is not +currently signed on. Executing +.BR write +with no arguments or with an option argument returns a usage message. +.SH BUGS +Old versions of the GNO multiuser package are incompatible with this version of +.BR write ; +use +.BR write +version 1.0 if you have not updated the multiuser package. +.SH OTHER +This program is Copyright 1993, 1994 by Leslie M. Barstow III. +It may be distributed freely provided it is accompanied by this manpage. +write uses routines from the ORCA Libraries, Copyright by ByteWorks, Inc. +.SH AUTHOR +Leslie Barstow, . +.SH FILES +/var/adm/utmp diff --git a/usr.bin/write/write.c b/usr.bin/write/write.c index 1123461..676e140 100644 --- a/usr.bin/write/write.c +++ b/usr.bin/write/write.c @@ -1,11 +1,18 @@ /* write.cc - UNIX-like write utility. Send message to terminal. */ -/* Copyright 1993 by Leslie M. Barstow III */ -/* Placed in the Public Domain with the following condition: - Please retain original Copyright in source, program, and manpage. */ +/* Copyright 1993, 1994 by Leslie M. Barstow III */ +/* This program is Freeware: Distribute and modify as you like, but + please retain original Copyright in source, program, and manpage. */ + +/* v1.0 - original release */ +/* v1.1 - 2/2/94 updated for new Multi-User package using wtmp */ +/* v2.0 - 2/21/94 fixed several bugs, added date to message. */ +/* v2.1 - 3/8/94 changed errors to stderr, fixed some errors, added TZ. + changes taken in part from Phil Vandry's sample code - + Thanks, Phil :-) */ #pragma optimize -1 #pragma debug 0 -#pragma stacksize 1024 +#pragma stacksize 768 #define O_WRONLY 0x0002 @@ -17,26 +24,31 @@ #include #include #include +#include +#include +#include +#include void Usage(void) {fputs("usage: write username [message]\n",stdout); - fputs("GS write: Copyright 1993 by Leslie M. Barstow III.\n", stdout); + fputs("GS write v2.1: Copyright 1994 by Leslie M. Barstow III.\n", stdout); } int userTTY(char *user, int **list) {int fd, tmp; - struct utmp uentry; + static struct utmp uentry; + static char tmpline[UT_LINESIZE+1]; int count = 0; int *newlist; fd=open(_PATH_UTMP, 257); if (fd < 1) - {fputs("write - unable to retrieve current users.\n", stdout); + {fputs("write - unable to retrieve current users.\n", stderr); exit(-1); } do {tmp = read(fd, (void *)(&uentry), sizeof(uentry)); - if (tmp) - {if (!strncmp(uentry.ut_name,user,strlen(user))) + if (tmp && uentry.ut_type && !(uentry.ut_type & UT_INVISIBLE)) + {if (!strncmp(uentry.ut_name,user,UT_NAMESIZE)) {count++; newlist = (int *)realloc(*list, count *2); if (newlist == (int *)0L) @@ -44,15 +56,17 @@ int userTTY(char *user, int **list) close((*list)[tmp-1]); free(*list); close(fd); - fputs("write - unable to allocate memory.\n", stdout); + fputs("write - unable to allocate memory.\n", stderr); exit(-1); } *list = newlist; (*list)[count-1] = open(uentry.ut_line, 0x0002); if ((*list)[count-1] < 1) - {fputs("write - unable to open terminal ",stdout); - fputs(uentry.ut_line, stdout); - fputs(".\n", stdout); + {fputs("write - unable to open terminal ",stderr); + strncpy(tmpline, uentry.ut_line, UT_LINESIZE); + tmpline[UT_LINESIZE] = '\0'; + fputs(tmpline, stderr); + fputs(".\n", stderr); count--; } } @@ -68,64 +82,63 @@ int main(int argc, char **argv) {int count; int i; int j = 2; - char *line, *uname; + time_t Time; + char *cTime; + char *uname; int *fdlist = (int *)0L; + uid_t uid; + int doEcho; + struct passwd *pwent; + static char line [512]; -if (argc > 3) + +if ((argc < 2) || (argc > 3) || (argv[1][0] == '-')) {Usage(); exit(-1); } + +uid = getuid(); +pwent = getpwuid(uid); +uname = pwent->pw_name; +Time = time(0L); +cTime = ctime(&Time); + +count = userTTY(argv[1], &fdlist); +if (count < 1) + {fputs("write - no active terminals for user.\n", stderr); + exit(-1); + } +for (i = 0; i < count; i++) + {write(fdlist[i], "\r\aMessage from ", 15); + write(fdlist[i], uname, strlen(uname)); + write(fdlist[i], " at ", 4); + write(fdlist[i], cTime, strlen(cTime)); + write(fdlist[i], "\r", 1); + } + +/* message on command line - write it and exit */ + if (argc == 3) - {count = userTTY(argv[1], &fdlist); - if (count < 1) - {fputs("write - no active terminals for user.\n", stdout); - exit(-1); + {for (i = 0; i < count; i++) + {write(fdlist[i], argv[2], strlen(argv[2])); + write(fdlist[i], "\r", 1); + close(fdlist[i]); } + exit(0); + } - uname = getenv("user"); - for (i = 0; i < count; i++) - {write(fdlist[i], "\n\aMessage from ", 15); - write(fdlist[i], uname, strlen(uname)); - write(fdlist[i], ".\r\n", 3); - write(fdlist[i], argv[2], strlen(argv[2])); - write(fdlist[i], "\r\n", 2); - } - for (i = 0; i < count; i++) - close(fdlist[i]); - exit(0); - } -if ((argc == 2) && (argv[1][0] != '-')) /* enter read loop */ - {line = (char *)malloc(256); - if (line == (char *)0L) - {fputs("write - error allocating buffer.\n", stdout); - exit(-1); - } - count = userTTY(argv[1], &fdlist); - if (count < 1) - {fputs("write - no active terminals for user.\n", stdout); - free(line); - exit(-1); - } - uname = getenv("user"); - for(i = 0; i < count; i++) - {write(fdlist[i], "\n\aMessage from ", 15); - write(fdlist[i], uname, strlen(uname)); - write(fdlist[i], ".\r\n", 3); - } - while (j > 1) - {fgets(line, 256, stdin); - j = strlen(line); - for (i = 0; i< count; i++) - write(fdlist[i], line, j); - - } - free(line); - for (i = 0; i < count; i++) - close(fdlist[i]); - exit(0); - } -else - {Usage(); - exit(-1); - } +/* No message on command line - read from stdin */ + + while (j = ReadLine(line, 512, '\r', 0)) + {for (i = 0; i< count; i++) + {write(fdlist[i], line, j); + write(fdlist[i], "\r", 1); + } + } + for (i = 0; i < count; i++) + write(fdlist[i], "\r\r", 7); + free(line); + for (i = 0; i < count; i++) + close(fdlist[i]); + exit(0); }