home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume4 / bridge / part02 / get_names.c < prev    next >
C/C++ Source or Header  |  1988-05-31  |  2KB  |  106 lines

  1. #ifndef lint
  2. static    char sccsid[] = "@(#)get_names.c 1.1 86/02/05 SMI"; /* from UCB 1.2 83/03/26 */
  3. #endif
  4.  
  5. #include "talk.h"
  6. #include "ctl.h"
  7.  
  8. char *getlogin(), *ttyname(), *rindex();
  9.  
  10. extern CTL_MSG msg;
  11.  
  12. /*
  13.  * Determine the local and remote user, tty, and machines
  14.  */
  15.  
  16. struct hostent *gethostbyname();
  17.  
  18. get_names(argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     char hostname[HOST_NAME_LENGTH];
  23.     char *his_name;
  24.     char *my_name;
  25.     char *my_machine_name;
  26.     char *his_machine_name;
  27.     char *my_tty;
  28.     char *his_tty;
  29.     char *ptr;
  30.     int name_length;
  31.  
  32.     if (argc < 2 ) {
  33.     printf("Usage:  talk user [ttyname]\n");
  34.     exit(-1);
  35.     }
  36.     if ( !isatty(0) ) {
  37.     printf("Standard input must be a tty, not a pipe or a file\n");
  38.     exit(-1);
  39.     }
  40.  
  41.     my_name = getlogin();
  42.     if (my_name == NULL) {
  43.     printf("You don't exist. Go away.\n");
  44.     exit(-1);
  45.     }
  46.  
  47.     name_length = HOST_NAME_LENGTH;
  48.     gethostname(hostname, &name_length);
  49.     my_machine_name = hostname;
  50.  
  51.     my_tty = rindex(ttyname(0), '/') + 1;
  52.  
  53.     /* check for, and strip out, the machine name 
  54.         of the target */
  55.  
  56.     for (ptr = argv[1]; *ptr != '\0' &&
  57.              *ptr != '@' &&
  58.              *ptr != ':' &&
  59.              *ptr != '!' &&
  60.              *ptr != '.'     ; ptr++) {
  61.     }
  62.  
  63.     if (*ptr == '\0') {
  64.  
  65.         /* this is a local to local talk */
  66.  
  67.     his_name = argv[1];
  68.     his_machine_name = my_machine_name;
  69.  
  70.     } else {
  71.  
  72.     if (*ptr == '@') {
  73.         /* user@host */
  74.         his_name = argv[1];
  75.         his_machine_name = ptr + 1;
  76.     } else {
  77.         /* host.user or host!user or host:user */
  78.         his_name = ptr + 1;
  79.         his_machine_name = argv[1];
  80.     }
  81.     *ptr = '\0';
  82.     }
  83.  
  84.  
  85.     if (argc > 2) {
  86.     his_tty = argv[2];    /* tty name is arg 2 */
  87.     } else {
  88.     his_tty = "";
  89.     }
  90.  
  91.     get_addrs(my_machine_name, his_machine_name);
  92.  
  93.     /* Load these useful values into the standard message header */
  94.  
  95.     msg.id_num = 0;
  96.  
  97.     strncpy(msg.l_name, my_name, NAME_SIZE);
  98.     msg.l_name[NAME_SIZE - 1] = '\0';
  99.  
  100.     strncpy(msg.r_name, his_name, NAME_SIZE);
  101.     msg.r_name[NAME_SIZE - 1] = '\0';
  102.  
  103.     strncpy(msg.r_tty, his_tty, TTY_SIZE);
  104.     msg.r_tty[TTY_SIZE - 1] = '\0';
  105. }
  106.