home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / mtools.c < prev    next >
C/C++ Source or Header  |  1997-11-13  |  3KB  |  131 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "patchlevel.h"
  5. #include "partition.h"
  6. #include "vfat.h"
  7.  
  8. const char *mversion = VERSION;
  9. const char *mdate = DATE;
  10. const char *progname;
  11.  
  12. static const struct dispatch {
  13.     const char *cmd;
  14.     void (*fn)(int, char **, int);
  15.     int type;
  16. } dispatch[] = {
  17.     {"mattrib",mattrib, 0},
  18.     {"mbadblocks",mbadblocks, 0},
  19.     {"mcd",mcd, 0},
  20.     {"mcopy",mcopy, 0},
  21.     {"mdel",mdel, 0},
  22.     {"mdeltree",mdel, 2},
  23.     {"mdir",mdir, 0},
  24.     {"mformat",mformat, 0},
  25.     {"minfo", minfo, 0},
  26.     {"mlabel",mlabel, 0},
  27.     {"mmd",mmd, 0},
  28. #ifndef _OSK
  29.     {"mmount",mmount, 0},
  30.     {"mpartition",mpartition, 0},
  31.     {"mzip", mzip, 0},
  32. #endif
  33.     {"mrd",mdel, 1},
  34.     {"mread",mcopy, 0},
  35.     {"mmove",mmove, 0},
  36.     {"mren",mmove, 1},
  37.     {"mtoolstest", mtoolstest, 0},
  38.     {"mtype",mcopy, 1},
  39.     {"mwrite",mcopy, 0}
  40. };
  41. #define NDISPATCH (sizeof dispatch / sizeof dispatch[0])
  42.  
  43. void main(int argc,char **argv)
  44. {
  45.     char *name;
  46.     int i;
  47.  
  48.     init_privs();
  49.  
  50. /*#define PRIV_TEST*/
  51.  
  52. #ifdef PRIV_TEST
  53.     { 
  54.         int euid;
  55.         char command[100];
  56.     
  57.         printf("INIT: %d %d\n", getuid(), geteuid());
  58.         drop_privs();
  59.         printf("DROP: %d %d\n", getuid(), geteuid());
  60.         reclaim_privs();
  61.         printf("RECLAIM: %d %d\n", getuid(), geteuid());
  62.         euid = geteuid();
  63.         if(argc & 1) {
  64.             drop_privs();
  65.             printf("DROP: %d %d\n", getuid(), geteuid());
  66.         }
  67.         if(!((argc-1) & 2)) {
  68.             destroy_privs();
  69.             printf("DESTROY: %d %d\n", getuid(), geteuid());
  70.         }
  71.         sprintf(command, "a.out %d", euid);
  72.         system(command);
  73.         exit(1);
  74.     }
  75. #endif
  76.  
  77.     /* print the version */
  78.     if(argc >= 2 && strcmp(argv[1], "-V") == 0) {
  79.         printf("Mtools version %s, dated %s\n", mversion, mdate);
  80.         exit(0);
  81.     }
  82.  
  83.     /* check whether the compiler lays out structures in a sane way */
  84.     if(sizeof(struct partition) != 16 ||
  85.        sizeof(struct directory) != 32 ||
  86.        sizeof(struct vfat_subentry) !=32) {
  87.         fprintf(stderr,"Mtools has not been correctly compiled\n");
  88.         fprintf(stderr,"Recompile it using a more recent compiler\n");
  89.         exit(137);
  90.     }
  91.  
  92.     if ((name = strrchr(argv[0],'/')))
  93.         name++;
  94.     else
  95.         name = argv[0];
  96.     progname = argv[0];
  97.  
  98.     /* this allows the different tools to be called as "mtools -c <command>"
  99.     ** where <command> is mdir, mdel, mcopy etcetera
  100.     ** Mainly done for the BeOS, which doesn't support links yet.
  101.     */
  102.  
  103.     if(argc >= 3 && 
  104.        !strcmp(argv[1], "-c") &&
  105.        !strcmp(name, "mtools")) {
  106.         argc-=2;
  107.         argv+=2;
  108.         name = argv[0];
  109.     }
  110.  
  111.     argv[0] = name;
  112.     
  113.     read_config();
  114.     setup_signal();
  115.     for (i = 0; i < NDISPATCH; i++) {
  116.         if (!strcmp(name,dispatch[i].cmd))
  117.             dispatch[i].fn(argc, argv, dispatch[i].type);
  118.     }
  119.     if (strcmp(name,"mtools"))
  120.         fprintf(stderr,"Unknown mtools command '%s'\n",name);
  121.     fprintf(stderr,"Supported commands:");
  122.     for (i = 0; i < NDISPATCH; i++) {
  123.         if (i%8 == 0) putc('\n', stderr);
  124.         else fprintf(stderr, ", ");
  125.         fprintf(stderr, "%s", dispatch[i].cmd);
  126.     }
  127.     putc('\n', stderr);
  128.  
  129.     exit(1);
  130. }
  131.