home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3298 / proctable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  2.4 KB  |  108 lines

  1. /* History:
  2. 5/1/91 DJB baseline public domain
  3. */
  4.  
  5. #include "mallocfree.h"
  6. #include "proctable.h"
  7. #include "structproc.h"
  8. #include "kmem.h"
  9. #include "nlistlist.h"
  10. #include "strerr.h"
  11.  
  12. #define PROCNLIST "_proc"
  13. #define NPROCNLIST "_nproc"
  14.  
  15. static int pinit = 0;
  16. static struct proc *proctable = 0;
  17. static short ptype;
  18. static short nptype;
  19. static unsigned long pvalue;
  20. static unsigned long npvalue;
  21. struct proc *myproc;
  22. int mynproc;
  23. static int proctableerrno = 0;
  24.  
  25. char *proctablestrerr(ke)
  26. strerrfun *ke;
  27. {
  28.  *ke = 0;
  29.  switch(proctableerrno)
  30.   {
  31.    case 0:
  32.      return "proctable error 0";
  33. #define PT_INIT 1
  34.    case PT_INIT:
  35.      *ke = nliststrerr;
  36.      return "cannot init proctable: ";
  37. #define PT_NLIST 2
  38.    case PT_NLIST:
  39.      *ke = nliststrerr;
  40.      return "cannot nlist: ";
  41. #define PT_NLPFOUND 3
  42.    case PT_NLPFOUND:
  43.      *ke = nlistnotin;
  44.      return PROCNLIST;
  45. #define PT_NLNPFOUND 4
  46.    case PT_NLNPFOUND:
  47.      *ke = nlistnotin;
  48.      return NPROCNLIST;
  49. #define PT_NLPREAD 5
  50.    case PT_NLPREAD:
  51.      *ke = kmemstrerr;
  52.      return "cannot read proc table pointer: ";
  53. #define PT_NLNPREAD 6
  54.    case PT_NLNPREAD:
  55.      *ke = kmemstrerr;
  56.      return "cannot read nproc: ";
  57. #define PT_FTREAD 7
  58.    case PT_FTREAD:
  59.      *ke = kmemstrerr;
  60.      return "cannot read proc table: ";
  61. #define PT_ALLOC 8
  62.    case PT_ALLOC:
  63.      *ke = 0;
  64.      return "out of memory";
  65.    default:
  66.      return "unknown proctable error";
  67.   }
  68. }
  69.  
  70. int proctableinit()
  71. {
  72.  if (nlistadd(PROCNLIST,&ptype,&pvalue) == -1)
  73.    RETERN(-1,proctableerrno,PT_INIT)
  74.  if (nlistadd(NPROCNLIST,&nptype,&npvalue) == -1)
  75.    RETERN(-1,proctableerrno,PT_INIT)
  76.  pinit = 1;
  77.  return 0;
  78. }
  79.  
  80. struct proc *getproctable()
  81. {
  82.  if (!pinit)
  83.    if (proctableinit() == -1)
  84.      return 0;
  85.  if (ptype == -1)
  86.    if (nlistdo() == -1)
  87.      RETERN(0,proctableerrno,PT_NLIST)
  88.  if (!ptype)
  89.    RETERN(0,proctableerrno,PT_NLPFOUND)
  90.  if (!nptype)
  91.    RETERN(0,proctableerrno,PT_NLNPFOUND)
  92.  if (kmemcpy((char *) &mynproc,(char *) npvalue,sizeof(mynproc)) == -1)
  93.    RETERN(0,proctableerrno,PT_NLPREAD)
  94.  if (kmemcpy((char *) &myproc,(char *) pvalue,sizeof(myproc)) == -1)
  95.    RETERN(0,proctableerrno,PT_NLNPREAD)
  96.  
  97.  if (proctable)
  98.    free((char *) proctable);
  99.  proctable = (struct proc *) malloc((unsigned) (sizeof(struct proc) * mynproc));
  100.  if (!proctable)
  101.    RETERN(0,proctableerrno,PT_ALLOC)
  102.  
  103.  if (kmemcpy((char *) proctable,(char *) myproc,sizeof(struct proc) * mynproc) == -1)
  104.    RETERN(0,proctableerrno,PT_FTREAD)
  105.  
  106.  return proctable;
  107. }
  108.