home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part02 / wall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  1.7 KB  |  87 lines

  1. /* Public domain. */
  2.  
  3. #include <sys/types.h>
  4. #include <sys/file.h>
  5. #ifdef BSD
  6. #include <limits.h>
  7. #endif
  8. #include <stdio.h>
  9. #include <strings.h>
  10. #include <utmp.h>
  11. #include <pwd.h>
  12. #include <time.h>
  13. #include <ctype.h>
  14. #include "fmt.h"
  15. #include "config/utmpfile.h"
  16. extern unsigned short getuid();
  17. extern char *ttyname();
  18. extern long time();
  19.  
  20. main()
  21.  register FILE *fi;
  22.  struct utmp ut;
  23.  char fntty[30];
  24.  int fd;
  25.  char buf[10000];
  26.  char *username;
  27.  struct passwd *pw;
  28.  char hostname[64];
  29.  char *ttyn;
  30.  long t;
  31.  struct tm *tm;
  32.  int r;
  33.  int pos;
  34.  
  35.  if (!(pw = getpwuid((int) getuid())))
  36.   {
  37.    fprintf(stderr,"write: who are you?\n");
  38.    exit(1);
  39.   }
  40.  username = pw->pw_name;
  41.  
  42.  gethostname(hostname,sizeof(hostname));
  43.  
  44.  if (!(ttyn = ttyname(2)))
  45.   {
  46.    fprintf(stderr,"wall: Can't find your tty\n");
  47.    exit(1);
  48.   }
  49.  
  50.  t = time((long *) 0);
  51.  tm = localtime(&t);
  52.  
  53.  sprintf(buf,"\nBroadcast message from %s@%s on %s at %d:%02d ...%c\n\n",
  54.         username,hostname,ttyn + 5,tm->tm_hour,tm->tm_min,7);
  55.  pos = strlen(buf);
  56.   {
  57.    static char pre[] =
  58. "End your message with the EOF character. This is how it will show up:";
  59.    write(1,pre,strlen(pre));
  60.   }
  61.  write(1,buf,pos);
  62.  while ((pos < 10000) && ((r = read(0,buf + pos,10000 - pos)) > 0))
  63.    pos += r;
  64.  if (fmt_nvis(FMT_LEN,buf,pos) != pos + 1) /* +1 for extra beep */
  65.   {
  66.    fprintf(stderr,"wall: sorry, that message has control characters\n");
  67.    exit(1);
  68.   }
  69.  
  70.  if (fi = fopen(UTMP_FILE,"r"))
  71.    while (fread((char *) &ut,sizeof(ut),1,fi))
  72.      if (ut.ut_name[0])
  73.       {
  74.        sprintf(fntty,"/dev/%.8s",ut.ut_line);
  75.        if ((fd = open(fntty,O_WRONLY)) == -1)
  76.          fprintf(stderr,"wall: cannot write to %.8s\n",ut.ut_line);
  77.        else
  78.         {
  79.      write(fd,buf,pos);
  80.          close(fd);
  81.         }
  82.       }
  83.  
  84.  exit(0);
  85. }
  86.