home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / unaxcess / part1 / Utilities / uwho.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.0 KB  |  102 lines

  1. /*
  2.  * %W% %E% %U% ncoast!bsa %Z%
  3.  * %Z% Copyright (C) 1986 by Brandon S. Allbery, All Rights Reserved %Z%
  4.  */
  5.  
  6. #ifndef lint
  7. static char _SccsId[] = "%W% %E% %U% ncoast!bsa %Z%";
  8. static char _CopyRt[] = "%Z% Copyright (C) 1985 by Brandon S. Allbery %Z%";
  9. #endif  lint
  10.  
  11. #include <stdio.h>
  12. #include <pwd.h>
  13.  
  14. struct passwd *getpwuid();
  15.  
  16. #ifdef SYS3
  17. #define TTYLIST "/etc/inittab"
  18. #else
  19. #define TTYLIST    "/etc/ttys"
  20. #endif
  21.  
  22. main(argc, argv)
  23. char **argv;
  24. {
  25.     FILE *tfp;
  26.     int cnt, flg;
  27.     char line[1024];
  28. #ifdef SYS3
  29.     int state;
  30.     char tty[18];
  31.     char mode[20];
  32. #endif
  33.  
  34.     if ((tfp = fopen(TTYLIST, "r")) == NULL)
  35.     {
  36.         fprintf(stderr, "%s: can't open %s\n", argv[0], TTYLIST);
  37.         exit(1);
  38.     }
  39.     flg = 0;
  40.     while (fgets(line, 1024, tfp) != NULL)
  41.     {
  42.         line[strlen(line) - 1] = '\0';    /* kill trailing newline */
  43. #ifdef SYS3
  44.         sscanf(line, "%d:%[^:]:%[^:]:", &state, tty, mode);
  45.         if (strcmp(tty, "co") == 0)
  46.             strcpy(tty, "console");
  47.         else
  48.             sprintf(tty, "tty%d", atoi(tty));    /* I hope!!! */
  49.             /* The above will be a parameter. */
  50.         if ((state != 2 && state != 7) || strchr(mode, 'o') != 0)
  51.             continue;    /* disabled tty */
  52. #else  SYS3
  53.         if (line[0] == '0')        /* disabled tty */
  54.             continue;
  55. #endif SYS3
  56.         if (argc > 1)            /* list specific ttys */
  57.         {
  58.             for (cnt = 1; cnt < argc; cnt++)
  59. #ifdef SYS3
  60.                 if (strcpy(argv[cnt], tty) == 0)
  61. #else  SYS3
  62.                     if (strcmp(argv[cnt], &line[2]) == 0)
  63. #endif SYS3
  64.                     {
  65. #ifdef SYS3
  66.                         showme(tty);
  67. #else  SYS3
  68.                         showme(&line[2]);
  69. #endif SYS3
  70.                         flg++;
  71.                     }
  72.         }
  73.         else                /* list all enabled ttys */
  74. #ifdef SYS3
  75.             showme(tty);
  76. #else  SYS3
  77.             showme(&line[2]);
  78. #endif SYS3
  79.     }
  80.     fclose(tfp);
  81.     if (argc > 1 && flg != argc - 1)
  82.     {
  83.         fprintf(stderr, "%s: bad tty name(s)\n", argv[0]);
  84.         exit(1);
  85.     }
  86.     exit(0);
  87. }
  88.  
  89. showme(ttyf)
  90. char *ttyf;
  91. {
  92.     FILE *fp;
  93.     char line[1024];
  94.  
  95.     sprintf(line, "%s/%s", getpwuid(geteuid())->pw_dir, ttyf);
  96.     if ((fp = fopen(line, "r")) == NULL)    /* not in use on this tty */
  97.         return;
  98.     fgets(line, 1024, fp);
  99.     printf("%s: %s", ttyf, line);        /* line already has newline */
  100.     fclose(fp);
  101. }
  102.