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

  1. /* History:
  2. 5/1/91 DJB baseline public domain
  3. */
  4.  
  5. /*
  6.  
  7. struct file *getfiletable() returns a pointer to a not necessarily
  8. atomic copy of the file table. myfile is the base of the file table in
  9. real kernel memory, i.e., _file. mynfile is the number of entries in the
  10. file table, i.e., _nfile.
  11.  
  12. filetableinit() does optional initialization to cooperate with other
  13. libraries.
  14.  
  15. filetablestrerr is a strerrfun for filetableinit() and getfiletable().
  16.  
  17. */
  18.  
  19. #include "mallocfree.h"
  20. #include "structfile.h"
  21. #include "filetable.h"
  22. #include "kmem.h"
  23. #include "nlistlist.h"
  24. #include "strerr.h"
  25.  
  26. #define FILENLIST "_file"
  27. #define NFILENLIST "_nfile"
  28.  
  29. static int finit = 0;
  30. static struct file *filetable = 0;
  31. static short ftype;
  32. static short nftype;
  33. static unsigned long fvalue;
  34. static unsigned long nfvalue;
  35. struct file *myfile;
  36. int mynfile;
  37. static int filetableerrno = 0;
  38.  
  39. static struct strerrtab e[] = {
  40.   { 0, "filetable error 0", 0 }
  41. #define FT_INIT 1
  42. , { FT_INIT, "cannot init filetable: ", nliststrerr }
  43. #define FT_NLIST 2
  44. , { FT_NLIST, "cannot nlist: ", nliststrerr }
  45. #define FT_NLFFOUND 3
  46. , { FT_NLFFOUND, FILENLIST, nlistnotin }
  47. #define FT_NLNFFOUND 4
  48. , { FT_NLNFFOUND, NFILENLIST, nlistnotin }
  49. #define FT_NLFREAD 5
  50. , { FT_NLFREAD, "cannot read file table pointer: ", kmemstrerr }
  51. #define FT_NLNFREAD 6
  52. , { FT_NLNFREAD, "cannot read nfile: ", kmemstrerr }
  53. #define FT_FTREAD 7
  54. , { FT_FTREAD, "cannot read file table: ", kmemstrerr }
  55. #define FT_ALLOC 8
  56. , { FT_ALLOC, "out of memory", 0 }
  57. } ;
  58.  
  59. char *filetablestrerr(ke)
  60. strerrfun *ke;
  61. {
  62.  return strerrtaberr(ke,filetableerrno,e,sizeof(e)/sizeof(*e),"unknown filetable error");
  63. }
  64.  
  65. int filetableinit()
  66. {
  67.  if (nlistadd(FILENLIST,&ftype,&fvalue) == -1)
  68.    RETERN(-1,filetableerrno,FT_INIT)
  69.  if (nlistadd(NFILENLIST,&nftype,&nfvalue) == -1)
  70.    RETERN(-1,filetableerrno,FT_INIT)
  71.  finit = 1;
  72.  return 0;
  73. }
  74.  
  75. struct file *getfiletable()
  76. {
  77.  if (!finit)
  78.    if (filetableinit() == -1)
  79.      return 0;
  80.  if (ftype == -1)
  81.    if (nlistdo() == -1)
  82.      RETERN(0,filetableerrno,FT_NLIST)
  83.  if (!ftype)
  84.    RETERN(0,filetableerrno,FT_NLFFOUND)
  85.  if (!nftype)
  86.    RETERN(0,filetableerrno,FT_NLNFFOUND)
  87.  if (kmemcpy((char *) &mynfile,(char *) nfvalue,sizeof(mynfile)) == -1)
  88.    RETERN(0,filetableerrno,FT_NLFREAD)
  89.  if (kmemcpy((char *) &myfile,(char *) fvalue,sizeof(myfile)) == -1)
  90.    RETERN(0,filetableerrno,FT_NLNFREAD)
  91.  
  92.  if (filetable)
  93.    free((char *) filetable);
  94.  filetable = (struct file *) malloc((unsigned) (sizeof(struct file) * mynfile));
  95.  if (!filetable)
  96.    RETERN(0,filetableerrno,FT_ALLOC)
  97.  
  98.  if (kmemcpy((char *) filetable,(char *) myfile,sizeof(struct file) * mynfile) == -1)
  99.    RETERN(0,filetableerrno,FT_FTREAD)
  100.  
  101.  return filetable;
  102. }
  103.