home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / PS / PROCPS-0.000 / PROCPS-0 / procps-0.97 / devname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-25  |  3.2 KB  |  129 lines

  1. /*
  2.  * devname.c
  3.  *
  4.  * modified by Michael K. Johnson, johnsonm@sunsite.unc.edu for YAPPS
  5.  * 
  6.  * I am changing this significantly to provide a more reasonable
  7.  * output.  Two characters are not enough to easily grok all possible
  8.  * tty's.
  9.  * 
  10.  * $Log: devname.c,v $
  11.  * Revision 1.8  1994/07/27  06:35:20  cb
  12.  * decided that snap.c was the place to do the minor device no
  13.  * conversion after all.  At least for now.  re-writing the
  14.  * tty_to_dev() routine is what made me think that it was just much easier
  15.  * to use only the minor number while doing ps stuff.
  16.  *
  17.  * Revision 1.7  1994/07/27  05:38:34  cb
  18.  * added a macro TTY_FULL_DEVNO to correct for the presence of the major bits
  19.  * for the only current tty major device.  Later this may need to be generalized.
  20.  * If a kernel past 1.1.?? (not sure which patch level) is used then defining
  21.  * this macro should be uncommented out of the Makefile.
  22.  *
  23.  * Revision 1.6  1994/01/01  12:43:47  johnsonm
  24.  * Fixed dev3().
  25.  *
  26.  * Revision 1.5  1993/12/31  20:22:13  johnsonm
  27.  * Removed devline perversion from my wrong-ended attempts to fix w.
  28.  *
  29.  */
  30.  
  31. #include <linux/fs.h>
  32. #include <sys/stat.h>
  33. #include <sys/dir.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36.  
  37. static char rcsid[]="$Id: devname.c,v 1.8 1994/07/27 06:35:20 cb Exp $";
  38.  
  39. /*
  40.  * ttynames:
  41.  *    console: con              console
  42.  *       vc00: v01 v02...       virtual consoles
  43.  *      tty00: s00 s01...       serial lines
  44.  *      ttyp0: p00 p01...       pty's
  45.  */
  46.  
  47. static char *ttgrp = "    StuvPQRWpqrs";
  48. static char *ttsub = "0123456789abcdef";
  49.  
  50. void dev_to_tty(char *tty, int dev)
  51. {
  52.   if (dev == -1)
  53.     strcpy(tty," ? ");
  54.   else if (dev == 0)
  55.     strcpy(tty,"con");
  56.   else if (dev < 64) {
  57.     sprintf(tty, "v%02d", dev);
  58.   } else {
  59.     if (dev < 128) {
  60.       sprintf(tty, "s%02d", (dev - 64));
  61.     } else {
  62.       tty[0] = 'p';
  63.       tty[1] = ttgrp[(dev >> 4) & 017];
  64.       tty[2] = ttsub[dev & 017];
  65.     }
  66.   }
  67.   tty[3] = 0;
  68. }
  69.  
  70. int tty_to_dev(char *tty)
  71. {
  72.     char *p, *q;
  73.     int i;
  74.  
  75.     if (tty == (char*)0) {
  76.         fprintf(stderr, "devname.c: tty_to_dev() called with null argument\n");
  77.         exit(1);
  78.     }
  79.     if (*tty == '\0') {        /* empty string: controlling tty */
  80.     struct stat buf;
  81.     if (fstat(0, &buf) != -1)
  82.         return(buf.st_rdev & 0xff);
  83.     else
  84.         return -1;
  85.     }
  86.     if (tty[0] == 'v') {
  87.         sscanf(&tty[1], "%d", &i);
  88.     return(i);
  89.     }
  90.     if (tty[0] == 's') {
  91.         sscanf(&tty[1], "%d", &i);
  92.     return(i+64);
  93.     }
  94.     if (tty[0] == 'p') {
  95.         p = strchr(ttgrp, tty[1]);
  96.     q = strchr(ttsub, tty[2]);
  97.     return(((p - ttgrp) << 4) | (q - ttsub));
  98.     }
  99.     if ((strcmp(tty, "con") == 0) || (strcmp(tty, "co") == 0))
  100.     return(0);
  101.     /* The rest are for compatibility with old releases */
  102.     if (tty[1] == '\0' && *tty >= '0' && *tty <= '9')
  103.     return(*tty - '0');
  104.     if ((p = strchr(ttgrp, *tty)) != NULL &&
  105.     (q = strchr(ttsub, tty[1])) != NULL)
  106.     return(((p - ttgrp) << 4) | (q - ttsub));
  107.     else
  108.     return -1;
  109. }
  110.  
  111. char *dev3(char *ttyname)
  112. {
  113.     static char ftname[256]; /* holds filename, then ttyname */
  114.     struct stat sb;
  115.  
  116.     strcpy(ftname, "/dev/");
  117.     strcat(ftname, ttyname);
  118.     stat(ftname, &sb);
  119.     if (S_ISCHR(sb.st_mode))
  120.     {
  121.         dev_to_tty(ftname, MINOR(sb.st_rdev));
  122.     }
  123.     else
  124.     {
  125.         strcpy(ftname, " ? ");
  126.     }
  127.     return(ftname);
  128. }
  129.