home *** CD-ROM | disk | FTP | other *** search
- /* History:
- 5/1/91 DJB baseline public domain
- */
-
- /*
-
- struct file *getfiletable() returns a pointer to a not necessarily
- atomic copy of the file table. myfile is the base of the file table in
- real kernel memory, i.e., _file. mynfile is the number of entries in the
- file table, i.e., _nfile.
-
- filetableinit() does optional initialization to cooperate with other
- libraries.
-
- filetablestrerr is a strerrfun for filetableinit() and getfiletable().
-
- */
-
- #include "mallocfree.h"
- #include "structfile.h"
- #include "filetable.h"
- #include "kmem.h"
- #include "nlistlist.h"
- #include "strerr.h"
-
- #define FILENLIST "_file"
- #define NFILENLIST "_nfile"
-
- static int finit = 0;
- static struct file *filetable = 0;
- static short ftype;
- static short nftype;
- static unsigned long fvalue;
- static unsigned long nfvalue;
- struct file *myfile;
- int mynfile;
- static int filetableerrno = 0;
-
- static struct strerrtab e[] = {
- { 0, "filetable error 0", 0 }
- #define FT_INIT 1
- , { FT_INIT, "cannot init filetable: ", nliststrerr }
- #define FT_NLIST 2
- , { FT_NLIST, "cannot nlist: ", nliststrerr }
- #define FT_NLFFOUND 3
- , { FT_NLFFOUND, FILENLIST, nlistnotin }
- #define FT_NLNFFOUND 4
- , { FT_NLNFFOUND, NFILENLIST, nlistnotin }
- #define FT_NLFREAD 5
- , { FT_NLFREAD, "cannot read file table pointer: ", kmemstrerr }
- #define FT_NLNFREAD 6
- , { FT_NLNFREAD, "cannot read nfile: ", kmemstrerr }
- #define FT_FTREAD 7
- , { FT_FTREAD, "cannot read file table: ", kmemstrerr }
- #define FT_ALLOC 8
- , { FT_ALLOC, "out of memory", 0 }
- } ;
-
- char *filetablestrerr(ke)
- strerrfun *ke;
- {
- return strerrtaberr(ke,filetableerrno,e,sizeof(e)/sizeof(*e),"unknown filetable error");
- }
-
- int filetableinit()
- {
- if (nlistadd(FILENLIST,&ftype,&fvalue) == -1)
- RETERN(-1,filetableerrno,FT_INIT)
- if (nlistadd(NFILENLIST,&nftype,&nfvalue) == -1)
- RETERN(-1,filetableerrno,FT_INIT)
- finit = 1;
- return 0;
- }
-
- struct file *getfiletable()
- {
- if (!finit)
- if (filetableinit() == -1)
- return 0;
- if (ftype == -1)
- if (nlistdo() == -1)
- RETERN(0,filetableerrno,FT_NLIST)
- if (!ftype)
- RETERN(0,filetableerrno,FT_NLFFOUND)
- if (!nftype)
- RETERN(0,filetableerrno,FT_NLNFFOUND)
- if (kmemcpy((char *) &mynfile,(char *) nfvalue,sizeof(mynfile)) == -1)
- RETERN(0,filetableerrno,FT_NLFREAD)
- if (kmemcpy((char *) &myfile,(char *) fvalue,sizeof(myfile)) == -1)
- RETERN(0,filetableerrno,FT_NLNFREAD)
-
- if (filetable)
- free((char *) filetable);
- filetable = (struct file *) malloc((unsigned) (sizeof(struct file) * mynfile));
- if (!filetable)
- RETERN(0,filetableerrno,FT_ALLOC)
-
- if (kmemcpy((char *) filetable,(char *) myfile,sizeof(struct file) * mynfile) == -1)
- RETERN(0,filetableerrno,FT_FTREAD)
-
- return filetable;
- }
-