home *** CD-ROM | disk | FTP | other *** search
- /* History:
- 5/1/91 DJB baseline public domain
- */
-
- #include "mallocfree.h"
- #include "proctable.h"
- #include "structproc.h"
- #include "kmem.h"
- #include "nlistlist.h"
- #include "strerr.h"
-
- #define PROCNLIST "_proc"
- #define NPROCNLIST "_nproc"
-
- static int pinit = 0;
- static struct proc *proctable = 0;
- static short ptype;
- static short nptype;
- static unsigned long pvalue;
- static unsigned long npvalue;
- struct proc *myproc;
- int mynproc;
- static int proctableerrno = 0;
-
- char *proctablestrerr(ke)
- strerrfun *ke;
- {
- *ke = 0;
- switch(proctableerrno)
- {
- case 0:
- return "proctable error 0";
- #define PT_INIT 1
- case PT_INIT:
- *ke = nliststrerr;
- return "cannot init proctable: ";
- #define PT_NLIST 2
- case PT_NLIST:
- *ke = nliststrerr;
- return "cannot nlist: ";
- #define PT_NLPFOUND 3
- case PT_NLPFOUND:
- *ke = nlistnotin;
- return PROCNLIST;
- #define PT_NLNPFOUND 4
- case PT_NLNPFOUND:
- *ke = nlistnotin;
- return NPROCNLIST;
- #define PT_NLPREAD 5
- case PT_NLPREAD:
- *ke = kmemstrerr;
- return "cannot read proc table pointer: ";
- #define PT_NLNPREAD 6
- case PT_NLNPREAD:
- *ke = kmemstrerr;
- return "cannot read nproc: ";
- #define PT_FTREAD 7
- case PT_FTREAD:
- *ke = kmemstrerr;
- return "cannot read proc table: ";
- #define PT_ALLOC 8
- case PT_ALLOC:
- *ke = 0;
- return "out of memory";
- default:
- return "unknown proctable error";
- }
- }
-
- int proctableinit()
- {
- if (nlistadd(PROCNLIST,&ptype,&pvalue) == -1)
- RETERN(-1,proctableerrno,PT_INIT)
- if (nlistadd(NPROCNLIST,&nptype,&npvalue) == -1)
- RETERN(-1,proctableerrno,PT_INIT)
- pinit = 1;
- return 0;
- }
-
- struct proc *getproctable()
- {
- if (!pinit)
- if (proctableinit() == -1)
- return 0;
- if (ptype == -1)
- if (nlistdo() == -1)
- RETERN(0,proctableerrno,PT_NLIST)
- if (!ptype)
- RETERN(0,proctableerrno,PT_NLPFOUND)
- if (!nptype)
- RETERN(0,proctableerrno,PT_NLNPFOUND)
- if (kmemcpy((char *) &mynproc,(char *) npvalue,sizeof(mynproc)) == -1)
- RETERN(0,proctableerrno,PT_NLPREAD)
- if (kmemcpy((char *) &myproc,(char *) pvalue,sizeof(myproc)) == -1)
- RETERN(0,proctableerrno,PT_NLNPREAD)
-
- if (proctable)
- free((char *) proctable);
- proctable = (struct proc *) malloc((unsigned) (sizeof(struct proc) * mynproc));
- if (!proctable)
- RETERN(0,proctableerrno,PT_ALLOC)
-
- if (kmemcpy((char *) proctable,(char *) myproc,sizeof(struct proc) * mynproc) == -1)
- RETERN(0,proctableerrno,PT_FTREAD)
-
- return proctable;
- }
-