home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum16.lzh / SOFTWARE / C / DEVPRC / devprc.c < prev    next >
C/C++ Source or Header  |  1991-01-06  |  6KB  |  225 lines

  1. /*
  2.  *        This software is copyrighted (c)1990 by Hellmuth Michaelis
  3.  *
  4.  *        Permission is granted to reproduce and distribute
  5.  *        this package by any means as long as no fee is charged
  6.  *        and as long as this notice is always included in the copies.
  7.  *        Commerical use or incorporation into commercial software
  8.  *        is prohibited without the written permission of the author.
  9.  *
  10.  *        Other rights are reserved except as explicitly granted
  11.  *        by written permission of the author.
  12.  *
  13.  *        Hellmuth Michaelis
  14.  *        Eggerstedtstr.28
  15.  *        2000 Hamburg 50
  16.  *        Tel: +49 40 384298
  17.  *        uucp(subnet): helle@ktshh
  18.  *
  19.  *    I would like to thank Martin Gasthuber (martin@odinhh) and
  20.  *    Heino Peters (heino@hphal) for their suggestions and helpful
  21.  *    support ! (and also Reimer Mellin for his Copyright text ...)
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <module.h>
  26. #include <procid.h>
  27. #include <sgstat.h>
  28. #include <sg_codes.h>
  29. #include <strings.h>
  30.  
  31. #define MAXPRC    63        /* max no of pid's (better get it from init ?)*/
  32. #define MAXDNL    32        /* device name length */
  33. #define MAXPATH 32        /* no of paths per process */
  34. #define VERSION "V.1.01"    /* version info */
  35.  
  36. typedef procid *p_procid;    /* ptr to procid structure */
  37.  
  38. typedef struct            /* got back from _get_process_table() */
  39. {
  40.     unsigned short maxblock;    /* max no of blocks */
  41.     unsigned short blocksize;    /* size of block */
  42.     p_procid ptr_pids[MAXPRC];    /* pointers to process id structs */
  43. }gpt_ret;
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.     int c;            /* character */
  50.     extern char *optarg;    /* for getopt() */
  51.     int aflag,dflag,hflag;    /* option flags */
  52.     int errflag;        /* error flag */
  53.     char *devnam;        /* name of device to search for */        
  54.  
  55.     errflag = aflag = dflag = hflag = 0;
  56.     while((c = getopt(argc,argv,"?ad:h")) != EOF)
  57.     {
  58.         switch(c)
  59.         {
  60.             case 'a':
  61.                 if(dflag || hflag)
  62.                     errflag++;
  63.                 else
  64.                     aflag++;
  65.                 break;
  66.             case 'd':
  67.                 if(aflag)
  68.                     errflag++;
  69.                 else
  70.                     dflag++;
  71.                     devnam = optarg;
  72.                 break;
  73.             case 'h':
  74.                 if(aflag)
  75.                     errflag++;
  76.                 else
  77.                     hflag++;
  78.                 break;
  79.             default:
  80.                 errflag++;
  81.                 break;
  82.         }
  83.         if((errflag) || ((!aflag) && (!dflag)))
  84.             usage(argv[0]);        /* no return.. */
  85.     }
  86.     if(dflag)
  87.         do_d(devnam,hflag);        /* display procs for dev */    
  88.     else
  89.         do_a();                /* display all */
  90.     return(0);
  91. }
  92.  
  93. usage(name)
  94. char *name;    /* program name */
  95. {
  96.     fprintf(stderr,"%s: display device(s) belonging to process(es), ",name);
  97.     fprintf(stderr,VERSION);
  98.     fprintf(stderr,"\noptions:     -a       display all devices belonging to all processes");
  99.     fprintf(stderr,"\n             -d [dev] display process having device [dev] open");
  100.     fprintf(stderr,"\n             -h       print header (with -d option)\n");
  101.  
  102.     exit(0);
  103. }
  104.  
  105. error(string)
  106. char *string;
  107. {
  108.     perror(string);
  109.     exit(101);
  110. }
  111.  
  112. do_a()    /* all option */
  113. {
  114.     procid *pp;        /* pointer to one process table struct */
  115.     mod_exec *pmodul;    /* pointer to module */
  116.     int i,j;        /* counters */
  117.     char name[32];        /* device name buffer */
  118.     struct sgbuf temp;    /* device option section buffer */
  119.     unsigned short *ptp;    /* path table ptr */
  120.     int devid;        /* system device path no */
  121.     gpt_ret proc_buff;    /* actual buffer for _get_process_table() */
  122.     
  123.     if ((_get_process_table( &proc_buff, sizeof(proc_buff))) < 1)
  124.         error("can't get process table");
  125.  
  126.     for (i=0;i<MAXPRC;i++)    /* every process .. */
  127.     {
  128.         if(((pp = proc_buff.ptr_pids[i]) !=0)    && \
  129.                  ((pmodul = pp->_pmodul) !=0)    && \
  130.               (pmodul->_mh._msync == MODSYNC))    /* valid descriptor ? */
  131.         {
  132.             printf("\nPID=%d, NAME=%s",pp->_id,(char *) \
  133.                 ((unsigned int)pmodul->_mh._mname + \
  134.                  (unsigned int)pmodul)); /* print pid & name */
  135.             
  136.             ptp = pp->_path;    /* get ptr to path table */
  137.             for(j=0; j < MAXPATH; j++) /* for every possible path */
  138.             {
  139.                 devid = (int)*ptp; /* get system path no */
  140.                 if(!devid)       /* valid ?? */
  141.                     continue;  /* no, continue .. */
  142.                 printf("\n\tPROCESSPATH=%d, ",j); /* yes */
  143.  
  144.                 if((_gs_sdevn(devid,name)) == -1) 
  145.                     error("cannot get device name");    
  146.  
  147.                 printf("SYSPATH=%d, DEVICE=/%s",devid,name);    
  148.  
  149.                 if((_gs_sopt(devid,&temp)) == -1)
  150.                     error("cannot get system path options");    
  151.  
  152.                 if(temp.sg_class == DT_RBF)
  153.                     printf(", FILENAME=%s",temp.sg_fname);
  154.  
  155.                 ptp++;    /* next path no */
  156.             }
  157.         }
  158.     }
  159.     printf("\n");    
  160. }
  161.  
  162. do_d(name,hflag)
  163. char *name;    /* ptr to devicename-string */
  164. int hflag;    /* print headerline flag */
  165. {
  166.     procid *pp;        /* pointer to one process table struct */
  167.     mod_exec *pmodul;    /* pointer to module */
  168.     int i,j;        /* counters */
  169.     unsigned short *ptp;    /* path table ptr */
  170.     char srcdev[MAXDNL];    /* devicename to search for */
  171.     char tstdev[MAXDNL];    /* devicename to test */
  172.     char gsdev[MAXDNL];    /* buffer for _gs_sdevn */
  173.     char *sptr,*dptr;    /* character pointers */
  174.     gpt_ret proc_buff;    /* actual buffer for _get_process_table() */
  175.     int devid;        /* system path no */
  176.     
  177.     if(strlen(name) > MAXDNL)    /* supplied devname too long */
  178.         error("device name too long");
  179.             
  180.     dptr = srcdev;
  181.     if(*name == '/')    /* allow "/dev" as well as "dev" */
  182.         name++;
  183.     while(*name)        /* uppercase devicename */
  184.         *dptr++ = toupper(*name++);
  185.     *dptr = '\0';        /* terminate .. */
  186.  
  187.     if ((_get_process_table( &proc_buff, sizeof(proc_buff))) < 1)
  188.         error("can't get process table");
  189.  
  190.     if(hflag)    /* header printing requested ? */
  191.         printf("PID\tNAME\n---\t----------------------------\n");
  192.             
  193.     for (i=0;i<MAXPRC;i++)
  194.     {
  195.         if(((pp = proc_buff.ptr_pids[i]) !=0)    && \
  196.                  ((pmodul = pp->_pmodul) !=0)    && \
  197.               (pmodul->_mh._msync == MODSYNC))
  198.         {
  199.             ptp = pp->_path;
  200.             for(j=0; j < MAXPATH; j++)
  201.             {
  202.                 devid = (int)*ptp;
  203.                 if(!devid)
  204.                     continue;
  205.  
  206.                 if((_gs_sdevn(devid,gsdev)) == -1)
  207.                     error("cannot get device name");    
  208.                 dptr = tstdev;
  209.                 sptr = gsdev;    /* uppercase devicename */
  210.                 while(*sptr)
  211.                     *dptr++ = toupper(*sptr++);
  212.                 *dptr = '\0';
  213.                 if(!strcmp(srcdev,tstdev)) /* searched for ?? */
  214.                 {
  215.                     printf("%d\t%s\n",pp->_id,(char *) \
  216.                        ((unsigned int)pmodul->_mh._mname + \
  217.                         (unsigned int)pmodul));
  218.                     break;    /* next process */
  219.                 }
  220.                 ptp++;
  221.             }
  222.         }
  223.     }
  224. }
  225.