home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / alt_os / mint / mfs6011 / source / csize.c < prev    next >
C/C++ Source or Header  |  1995-01-21  |  4KB  |  231 lines

  1. /* Minix Filesystem Binary Configuration Program
  2.  * Copyright 1995 S.N. Henson.
  3.  */
  4.  
  5. #include <mintbind.h>
  6. #include <nlist.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <st-out.h>
  12. #include <macros.h>
  13.  
  14. #ifdef __STDC__
  15. # define    P(s) s
  16. #else
  17. # define P(s) ()
  18. #endif
  19.  
  20.  
  21. /* csize.c */
  22. int main P((int argc , char **argv ));
  23. int usage P((void ));
  24. int get_binfo P((char *fpath ));
  25. int alert_err P((char *mess ));
  26. int load_bin P((char *fpath ));
  27. int save_bin P((char *fpath ));
  28. int get_var P((void *ptr , int sym , int len ));
  29. int put_var P((void *ptr , int sym , int len ));
  30.  
  31. #undef P
  32. /* Symbol list */
  33.  
  34. struct nlist nl[] = {
  35. {"_mfs_maj",0,0},
  36. {"_mfs_min",0,0},
  37. {"_mfs_plev",0,0},
  38. {"_mfs_magic",0,0},
  39. {"_ucache_size",0,0},
  40. {"_scache_size",0,0},
  41. {"_icache_size",0,0},
  42. {0,0,0}
  43. };
  44.  
  45. long ucache_size,scache_size,icache_size;
  46. long mfs_magic;
  47. unsigned short mfs_maj,mfs_min,mfs_plev;
  48.  
  49. FILE *mfs;
  50.  
  51. #define MFS_MAGIC 0x18970431
  52. #define MFS_VERIFY 0x100
  53. #define MFS_INFO   0x104
  54.  
  55. #define S_MAJ 0
  56. #define S_MIN 1
  57. #define S_PLEV 2
  58. #define S_MAGIC 3
  59. #define S_UCACHE 4
  60. #define S_SCACHE 5
  61. #define S_ICACHE 6
  62.  
  63.  
  64.  
  65.  
  66. int main(argc,argv)
  67. int argc;
  68. char **argv;
  69. {
  70.  
  71. if (argc != 2 && argc != 5) usage();
  72. if (get_binfo (argv[1])) exit (0);
  73. if (argc==2) {
  74.   printf ("Minixfs version %d.%d Patch level %d\n", mfs_maj, mfs_min, mfs_plev);
  75.   printf ("User   cache size %ldK\n", ucache_size);
  76.   printf ("System cache size %ldK\n", scache_size);
  77.   printf ("Inode  cache size %ldK\n", icache_size);
  78.   exit (0);
  79. }
  80.  
  81. ucache_size = atol (argv[2]);
  82. scache_size = atol (argv[3]);
  83. icache_size = atol (argv[4]);
  84. save_bin (argv[1]);
  85.  
  86. exit (0);
  87.  
  88. }
  89.  
  90. usage ()
  91. {
  92.   fprintf (stderr, "Usage: csize minix.xfs [ucache scache icache]\n");
  93. }
  94.  
  95. int get_binfo(fpath)
  96. char *fpath;
  97. {
  98.  
  99.     switch(nlist(fpath,nl))
  100.     {
  101.         case 0:            /* Got all symbols */
  102.         break;
  103.  
  104.         case 1:            /* One symbol not found */
  105.         if(nl[S_PLEV].n_type==0) break; /* OK if patchlevel */
  106.         default:        /* More than one */
  107.         alert_err("Can't Find All Symbols");
  108.         return 1;
  109.         
  110.         case -1:    /* Some other error */
  111.         switch(errno)
  112.         {
  113.  
  114.             case EDOM:
  115.             alert_err("Stripped Binary");
  116.             break;
  117.  
  118.             case ENOEXEC:
  119.             alert_err("Not Executable File");
  120.             break;
  121.  
  122.             default:
  123.             alert_err("Error Reading file");
  124.         }
  125.         return 1;
  126.     }        
  127.  
  128.     if(load_bin(fpath))
  129.     {
  130.         alert_err("Error Reading Symbols");    
  131.         return 1;
  132.     }
  133.  
  134.     return 0;
  135. }
  136.  
  137. alert_err (mess)
  138. char *mess;
  139. {
  140.     fprintf (stderr, "csize: %s\n", mess);
  141. }
  142.  
  143.  
  144. /* Load all symbols from binary into variables */
  145. #define HDR    sizeof(struct aexec)
  146. #define N_OFF(x) (nl[x].n_value+HDR)
  147. #define N_READ(x) (fread(&x,sizeof(x),1,mfs)!=1)
  148.  
  149. #define get_short(x,y) get_var(x,y,sizeof(short))
  150. #define get_long(x,y)  get_var(x,y,sizeof(long))
  151. #define put_short(x,y) put_var(x,y,sizeof(short))
  152. #define put_long(x,y)  put_var(x,y,sizeof(long))
  153.  
  154. int load_bin(fpath)
  155. char *fpath;
  156. {    if(! (mfs=fopen(fpath,"rb"))) return 1;
  157.  
  158.     if( get_long(&mfs_magic,S_MAGIC) ) return 1;
  159.  
  160.     if(mfs_magic!=MFS_MAGIC)
  161.     {
  162.         fclose(mfs);
  163.         return 1;
  164.     }
  165.  
  166.     if( get_short(&mfs_maj,S_MAJ) ) return 1;
  167.     if( get_short(&mfs_min,S_MIN) ) return 1;
  168.     if( get_short(&mfs_plev,S_PLEV) ) return 1;    
  169.  
  170.     if( get_long(&ucache_size,S_UCACHE) ) return 1;
  171.     if( get_long(&scache_size,S_SCACHE) ) return 1;
  172.     if( get_long(&icache_size,S_ICACHE) ) return 1;
  173.  
  174.     fclose(mfs);
  175.     return 0;
  176. }
  177.  
  178. int save_bin(fpath)
  179. char *fpath;
  180. {
  181.     if(! (mfs=fopen(fpath,"rb+"))) return 1;
  182.  
  183.     if( put_long(&ucache_size,S_UCACHE) ) return 1;
  184.     if( put_long(&scache_size,S_SCACHE) ) return 1;
  185.     if( put_long(&icache_size,S_ICACHE) ) return 1;
  186.  
  187.     fclose(mfs);
  188.     return 0;
  189. }
  190.  
  191.  
  192. int get_var(ptr,sym,len)
  193. void *ptr;
  194. int sym;
  195. int len;
  196. {
  197.     if(fseek(mfs,nl[sym].n_value+HDR,SEEK_SET))
  198.     {
  199.         fclose(mfs);
  200.         return 1;
  201.     }
  202.  
  203.     if(fread(ptr,len,1,mfs)!=1)
  204.     {
  205.         fclose(mfs);
  206.         return 1;
  207.     }
  208.     return 0;
  209. }
  210.  
  211. int put_var(ptr,sym,len)
  212. void *ptr;
  213. int sym;
  214. int len;
  215. {
  216.     if(fseek(mfs,nl[sym].n_value+HDR,SEEK_SET))
  217.     {
  218.         fclose(mfs);
  219.         return 1;
  220.     }
  221.  
  222.     if(fwrite(ptr,len,1,mfs)!=1)
  223.     {
  224.         fclose(mfs);
  225.         return 1;
  226.     }
  227.     return 0;
  228. }
  229.  
  230.  
  231.