1 | #include "sysincludes.h"
|
---|
2 | #include "msdos.h"
|
---|
3 | #include "mtools.h"
|
---|
4 | #include "partition.h"
|
---|
5 | #include "vfat.h"
|
---|
6 |
|
---|
7 | const char *progname;
|
---|
8 |
|
---|
9 | static const struct dispatch {
|
---|
10 | const char *cmd;
|
---|
11 | void (*fn)(int, char **, int);
|
---|
12 | int type;
|
---|
13 | } dispatch[] = {
|
---|
14 | {"attrib",mattrib, 0},
|
---|
15 | {"badblocks",mbadblocks, 0},
|
---|
16 | {"cat",mcat, 0},
|
---|
17 | {"cd",mcd, 0},
|
---|
18 | {"copy",mcopy, 0},
|
---|
19 | {"del",mdel, 0},
|
---|
20 | {"deltree",mdel, 2},
|
---|
21 | {"dir",mdir, 0},
|
---|
22 | {"doctorfat",mdoctorfat, 0},
|
---|
23 | {"du",mdu, 0},
|
---|
24 | {"format",mformat, 0},
|
---|
25 | {"info", minfo, 0},
|
---|
26 | {"label",mlabel, 0},
|
---|
27 | {"md",mmd, 0},
|
---|
28 | {"mkdir",mmd, 0},
|
---|
29 | #ifdef OS_linux
|
---|
30 | {"mount",mmount, 0},
|
---|
31 | #endif
|
---|
32 | {"partition",mpartition, 0},
|
---|
33 | {"rd",mdel, 1},
|
---|
34 | {"rmdir",mdel, 1},
|
---|
35 | {"read",mcopy, 0},
|
---|
36 | {"move",mmove, 0},
|
---|
37 | {"ren",mmove, 1},
|
---|
38 | {"showfat", mshowfat, 0},
|
---|
39 | #ifndef NO_CONFIG
|
---|
40 | {"toolstest", mtoolstest, 0},
|
---|
41 | #endif
|
---|
42 | {"type",mcopy, 1},
|
---|
43 | {"write",mcopy, 0},
|
---|
44 | #ifndef OS_Minix
|
---|
45 | {"zip", mzip, 0}
|
---|
46 | #endif
|
---|
47 | };
|
---|
48 | #define NDISPATCH (sizeof dispatch / sizeof dispatch[0])
|
---|
49 |
|
---|
50 | int main(int argc,char **argv)
|
---|
51 | {
|
---|
52 | const char *name;
|
---|
53 | int i;
|
---|
54 |
|
---|
55 | init_privs();
|
---|
56 | #ifdef __EMX__
|
---|
57 | _wildcard(&argc,&argv);
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | /*#define PRIV_TEST*/
|
---|
61 |
|
---|
62 | #ifdef PRIV_TEST
|
---|
63 | {
|
---|
64 | int euid;
|
---|
65 | char command[100];
|
---|
66 |
|
---|
67 | printf("INIT: %d %d\n", getuid(), geteuid());
|
---|
68 | drop_privs();
|
---|
69 | printf("DROP: %d %d\n", getuid(), geteuid());
|
---|
70 | reclaim_privs();
|
---|
71 | printf("RECLAIM: %d %d\n", getuid(), geteuid());
|
---|
72 | euid = geteuid();
|
---|
73 | if(argc & 1) {
|
---|
74 | drop_privs();
|
---|
75 | printf("DROP: %d %d\n", getuid(), geteuid());
|
---|
76 | }
|
---|
77 | if(!((argc-1) & 2)) {
|
---|
78 | destroy_privs();
|
---|
79 | printf("DESTROY: %d %d\n", getuid(), geteuid());
|
---|
80 | }
|
---|
81 | sprintf(command, "a.out %d", euid);
|
---|
82 | system(command);
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 |
|
---|
88 | #ifdef __EMX__
|
---|
89 | _wildcard(&argc,&argv);
|
---|
90 | #endif
|
---|
91 |
|
---|
92 |
|
---|
93 | /* check whether the compiler lays out structures in a sane way */
|
---|
94 | if(sizeof(struct partition) != 16 ||
|
---|
95 | sizeof(struct directory) != 32 ||
|
---|
96 | sizeof(struct vfat_subentry) !=32) {
|
---|
97 | fprintf(stderr,"Mtools has not been correctly compiled\n");
|
---|
98 | fprintf(stderr,"Recompile it using a more recent compiler\n");
|
---|
99 | return 137;
|
---|
100 | }
|
---|
101 |
|
---|
102 | #ifdef __EMX__
|
---|
103 | argv[0] = _getname(argv[0]); _remext(argv[0]); name = argv[0];
|
---|
104 | #else
|
---|
105 | name = _basename(argv[0]);
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | #if 0
|
---|
109 | /* this allows the different tools to be called as "mtools -c <command>"
|
---|
110 | ** where <command> is mdir, mdel, mcopy etcetera
|
---|
111 | ** Mainly done for the BeOS, which doesn't support links yet.
|
---|
112 | */
|
---|
113 |
|
---|
114 | if(argc >= 3 &&
|
---|
115 | !strcmp(argv[1], "-c") &&
|
---|
116 | !strcmp(name, "mtools")) {
|
---|
117 | argc-=2;
|
---|
118 | argv+=2;
|
---|
119 | name = argv[0];
|
---|
120 | }
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | /* print the version */
|
---|
124 | if(argc >= 2 &&
|
---|
125 | (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") ==0)) {
|
---|
126 | printf("%c%s version %s, dated %s\n",
|
---|
127 | toupper(name[0]), name+1,
|
---|
128 | mversion, mdate);
|
---|
129 | printf("configured with the following options: ");
|
---|
130 | #ifdef USE_XDF
|
---|
131 | printf("enable-xdf ");
|
---|
132 | #else
|
---|
133 | printf("disable-xdf ");
|
---|
134 | #endif
|
---|
135 | #ifdef USING_VOLD
|
---|
136 | printf("enable-vold ");
|
---|
137 | #else
|
---|
138 | printf("disable-vold ");
|
---|
139 | #endif
|
---|
140 | #ifdef USING_NEW_VOLD
|
---|
141 | printf("enable-new-vold ");
|
---|
142 | #else
|
---|
143 | printf("disable-new-vold ");
|
---|
144 | #endif
|
---|
145 | #ifdef DEBUG
|
---|
146 | printf("enable-debug ");
|
---|
147 | #else
|
---|
148 | printf("disable-debug ");
|
---|
149 | #endif
|
---|
150 | #ifdef USE_RAWTERM
|
---|
151 | printf("enable-raw-term ");
|
---|
152 | #else
|
---|
153 | printf("disable-raw-term ");
|
---|
154 | #endif
|
---|
155 | printf("\n");
|
---|
156 | return 0;
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (argc >= 2 && strcmp(name, "mtools") == 0) {
|
---|
160 | /* mtools command ... */
|
---|
161 | argc--;
|
---|
162 | argv++;
|
---|
163 | name = argv[0];
|
---|
164 | }
|
---|
165 | progname = argv[0];
|
---|
166 |
|
---|
167 | read_config();
|
---|
168 | setup_signal();
|
---|
169 | for (i = 0; i < NDISPATCH; i++) {
|
---|
170 | if (!strcmp(name,dispatch[i].cmd)
|
---|
171 | || (name[0] == 'm' && !strcmp(name+1,dispatch[i].cmd)))
|
---|
172 | dispatch[i].fn(argc, argv, dispatch[i].type);
|
---|
173 | }
|
---|
174 | if (strcmp(name,"mtools"))
|
---|
175 | fprintf(stderr,"Unknown mtools command '%s'\n",name);
|
---|
176 | fprintf(stderr,"Usage: mtools [-V] command [-options] arguments ...\n");
|
---|
177 | fprintf(stderr,"Supported commands:");
|
---|
178 | for (i = 0; i < NDISPATCH; i++) {
|
---|
179 | fprintf(stderr, i%8 == 0 ? "\n\t" : ", ");
|
---|
180 | fprintf(stderr, "%s", dispatch[i].cmd);
|
---|
181 | }
|
---|
182 | putc('\n', stderr);
|
---|
183 | fprintf(stderr, "Use 'mtools command -?' for help per command\n");
|
---|
184 |
|
---|
185 | return 1;
|
---|
186 | }
|
---|