home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part03 / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  2.7 KB  |  129 lines

  1. /* Public domain. */
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/file.h>
  6. #ifdef BSD
  7. #include <limits.h>
  8. #endif
  9. #include <stdio.h>
  10. #include <strings.h>
  11. #include <utmp.h>
  12. #include <pwd.h>
  13. #include <time.h>
  14. #include "config/utmpfile.h"
  15. #include "fmt.h"
  16. extern unsigned short getuid();
  17. extern char *ttyname();
  18. extern long time();
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23.  register FILE *fi;
  24.  struct utmp ut;
  25.  char line[9];
  26.  int lines = 0;
  27.  char fntty[30];
  28.  int fd;
  29.  struct stat st;
  30.  char buf[500];
  31.  char outbuf[3000];
  32.  int offset;
  33.  char *username;
  34.  struct passwd *pw;
  35.  char hostname[64];
  36.  char *ttyn;
  37.  long t;
  38.  struct tm *tm;
  39.  char *s;
  40.  
  41.  if (argc < 2)
  42.   {
  43.    fputs("Usage: write user [ttyname]\n",stderr);
  44.    exit(1);
  45.   }
  46.  
  47.  if (!(pw = getpwuid((int) getuid())))
  48.   {
  49.    fputs("write: who are you?\n",stderr);
  50.    exit(1);
  51.   }
  52.  username = pw->pw_name;
  53.  
  54.  gethostname(hostname,sizeof(hostname));
  55.  hostname[sizeof(hostname) - 1] = 0;
  56.  
  57.  if (!(ttyn = ttyname(2)))
  58.   {
  59.    fputs("write: Can't find your tty\n",stderr);
  60.    exit(1);
  61.   }
  62.  if ((fstat(2,&st) == -1) || !(st.st_mode & 0020))
  63.    fputs("write: warning: your messages are off, recipient will not be able to respond\n",stderr);
  64.  
  65.  t = time((long *) 0);
  66.  tm = localtime(&t);
  67.  
  68.  if (fi = fopen(UTMP_FILE,"r"))
  69.    while (fread((char *) &ut,sizeof(ut),1,fi))
  70.      if (!strncmp(ut.ut_name,argv[1],8))
  71.        if ((argc == 2) || (!strncmp(ut.ut_line,argv[2],8)))
  72.      if (!lines)
  73.       {
  74.        fmt_strncpy(line,ut.ut_line,8);
  75.        line[8] = '\0';
  76.        lines = 1;
  77.       }
  78.      else
  79.        lines++;
  80.  if (!lines)
  81.   {
  82.    if (argc == 2)
  83.      (void) fprintf(stderr,"write: %s not logged in\n",argv[1]);
  84.    else
  85.      (void) fprintf(stderr,"write: %s not logged in on tty %s\n",
  86.             argv[1],argv[2]);
  87.    (void) exit(1);
  88.   }
  89.  if (lines > 1)
  90.    (void) fprintf(stderr,
  91.    "write: %s logged in more than once ... writing to %s\n",
  92.    argv[1],line);
  93.  
  94.  (void) sprintf(fntty,"/dev/%s",line);
  95.  if ((fd = open(fntty,O_WRONLY)) == -1)
  96.   {
  97.    fputs("write: Permission denied\n",stderr);
  98.    exit(1);
  99.   }
  100.  
  101.  (void) sprintf(buf,"\nMessage from %s@%s on %s at %d:%02d ...%c\n",
  102.         username,hostname,ttyn + 5,tm->tm_hour,tm->tm_min,7);
  103.  (void) write(fd,buf,strlen(buf));
  104.  
  105.  (void) sprintf(buf,"%s: ",username);
  106.  offset = strlen(buf);
  107.  
  108.  while (fgets(buf + offset,sizeof(buf) - offset,stdin))
  109.   {
  110.    if ((fstat(fd,&st) == -1) || !(st.st_mode & 0020))
  111.     {
  112.      fprintf(stderr,"write: Permission denied\n");
  113.      exit(1);
  114.     }
  115.    s = outbuf + fmt_nvis(outbuf,buf,strlen(buf));
  116.    write(fd,outbuf,s - outbuf); /*XXX*/
  117.    sleep(1);
  118.   }
  119.  
  120.  t = time((long *) 0);
  121.  tm = localtime(&t);
  122.  (void) sprintf(buf,"End of message from %s@%s on %s at %d:%02d\n",
  123.         username,hostname,ttyn + 5,tm->tm_hour,tm->tm_min);
  124.  (void) write(fd,buf,strlen(buf));
  125.  
  126.  exit(0);
  127. }
  128.