xref: /aosp_15_r20/external/mtools/mtools.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1996-2004,2007-2010 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  */
17*d5c9a868SElliott Hughes 
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "msdos.h"
20*d5c9a868SElliott Hughes #include "mtools.h"
21*d5c9a868SElliott Hughes #include "partition.h"
22*d5c9a868SElliott Hughes #include "vfat.h"
23*d5c9a868SElliott Hughes 
24*d5c9a868SElliott Hughes const char *progname;
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes static const struct dispatch {
27*d5c9a868SElliott Hughes 	const char *cmd;
28*d5c9a868SElliott Hughes 	void (*fn)(int, char **, int);
29*d5c9a868SElliott Hughes 	int type;
30*d5c9a868SElliott Hughes } dispatch[] = {
31*d5c9a868SElliott Hughes 	{"mattrib",mattrib, 0},
32*d5c9a868SElliott Hughes 	{"mbadblocks",mbadblocks, 0},
33*d5c9a868SElliott Hughes 	{"mcat",mcat, 0},
34*d5c9a868SElliott Hughes 	{"mcd",mcd, 0},
35*d5c9a868SElliott Hughes 	{"mcopy",mcopy, 0},
36*d5c9a868SElliott Hughes 	{"mdel",mdel, 0},
37*d5c9a868SElliott Hughes 	{"mdeltree",mdel, 2},
38*d5c9a868SElliott Hughes 	{"mdir",mdir, 0},
39*d5c9a868SElliott Hughes 	{"mdoctorfat",mdoctorfat, 0},
40*d5c9a868SElliott Hughes 	{"mdu",mdu, 0},
41*d5c9a868SElliott Hughes 	{"mformat",mformat, 0},
42*d5c9a868SElliott Hughes 	{"minfo", minfo, 0},
43*d5c9a868SElliott Hughes 	{"mlabel",mlabel, 0},
44*d5c9a868SElliott Hughes 	{"mmd",mmd, 0},
45*d5c9a868SElliott Hughes 	{"mmount",mmount, 0},
46*d5c9a868SElliott Hughes 	{"mpartition",mpartition, 0},
47*d5c9a868SElliott Hughes 	{"mrd",mdel, 1},
48*d5c9a868SElliott Hughes 	{"mread",mcopy, 0},
49*d5c9a868SElliott Hughes 	{"mmove",mmove, 0},
50*d5c9a868SElliott Hughes 	{"mren",mmove, 1},
51*d5c9a868SElliott Hughes 	{"mshowfat", mshowfat, 0},
52*d5c9a868SElliott Hughes 	{"mshortname", mshortname, 0},
53*d5c9a868SElliott Hughes 	{"mtoolstest", mtoolstest, 0},
54*d5c9a868SElliott Hughes 	{"mtype",mcopy, 1},
55*d5c9a868SElliott Hughes 	{"mwrite",mcopy, 0},
56*d5c9a868SElliott Hughes 	{"mzip", mzip, 0}
57*d5c9a868SElliott Hughes };
58*d5c9a868SElliott Hughes #define NDISPATCH (sizeof dispatch / sizeof dispatch[0])
59*d5c9a868SElliott Hughes 
main(int argc,char ** argv)60*d5c9a868SElliott Hughes int main(int argc,char **argv)
61*d5c9a868SElliott Hughes {
62*d5c9a868SElliott Hughes 	unsigned int i;
63*d5c9a868SElliott Hughes 	const char *name;
64*d5c9a868SElliott Hughes 
65*d5c9a868SElliott Hughes #ifdef HAVE_SETLOCALE
66*d5c9a868SElliott Hughes 	char *locale;
67*d5c9a868SElliott Hughes 	locale=setlocale(LC_ALL, "");
68*d5c9a868SElliott Hughes 	if(locale == NULL || !strcmp(locale, "C"))
69*d5c9a868SElliott Hughes 		setlocale(LC_ALL, "en_US");
70*d5c9a868SElliott Hughes #endif
71*d5c9a868SElliott Hughes 
72*d5c9a868SElliott Hughes 	init_privs();
73*d5c9a868SElliott Hughes #ifdef __EMX__
74*d5c9a868SElliott Hughes 	_wildcard(&argc,&argv);
75*d5c9a868SElliott Hughes #endif
76*d5c9a868SElliott Hughes 
77*d5c9a868SElliott Hughes /*#define PRIV_TEST*/
78*d5c9a868SElliott Hughes 
79*d5c9a868SElliott Hughes #ifdef PRIV_TEST
80*d5c9a868SElliott Hughes 	{
81*d5c9a868SElliott Hughes 		int euid;
82*d5c9a868SElliott Hughes 		char command[100];
83*d5c9a868SElliott Hughes 
84*d5c9a868SElliott Hughes 		printf("INIT: %d %d\n", getuid(), geteuid());
85*d5c9a868SElliott Hughes 		drop_privs();
86*d5c9a868SElliott Hughes 		printf("DROP: %d %d\n", getuid(), geteuid());
87*d5c9a868SElliott Hughes 		reclaim_privs();
88*d5c9a868SElliott Hughes 		printf("RECLAIM: %d %d\n", getuid(), geteuid());
89*d5c9a868SElliott Hughes 		euid = geteuid();
90*d5c9a868SElliott Hughes 		if(argc & 1) {
91*d5c9a868SElliott Hughes 			drop_privs();
92*d5c9a868SElliott Hughes 			printf("DROP: %d %d\n", getuid(), geteuid());
93*d5c9a868SElliott Hughes 		}
94*d5c9a868SElliott Hughes 		if(!((argc-1) & 2)) {
95*d5c9a868SElliott Hughes 			destroy_privs();
96*d5c9a868SElliott Hughes 			printf("DESTROY: %d %d\n", getuid(), geteuid());
97*d5c9a868SElliott Hughes 		}
98*d5c9a868SElliott Hughes 		sprintf(command, "a.out %d", euid);
99*d5c9a868SElliott Hughes 		system(command);
100*d5c9a868SElliott Hughes 		return 1;
101*d5c9a868SElliott Hughes 	}
102*d5c9a868SElliott Hughes #endif
103*d5c9a868SElliott Hughes 
104*d5c9a868SElliott Hughes 
105*d5c9a868SElliott Hughes #ifdef __EMX__
106*d5c9a868SElliott Hughes        _wildcard(&argc,&argv);
107*d5c9a868SElliott Hughes #endif
108*d5c9a868SElliott Hughes 
109*d5c9a868SElliott Hughes 
110*d5c9a868SElliott Hughes 	/* check whether the compiler lays out structures in a sane way */
111*d5c9a868SElliott Hughes 	if(sizeof(struct partition) != 16 ||
112*d5c9a868SElliott Hughes 	   sizeof(struct directory) != 32 ||
113*d5c9a868SElliott Hughes 	   sizeof(struct vfat_subentry) !=32) {
114*d5c9a868SElliott Hughes 		fprintf(stderr,"Mtools has not been correctly compiled\n");
115*d5c9a868SElliott Hughes 		fprintf(stderr,"Recompile it using a more recent compiler\n");
116*d5c9a868SElliott Hughes 		return 137;
117*d5c9a868SElliott Hughes 	}
118*d5c9a868SElliott Hughes 
119*d5c9a868SElliott Hughes #ifdef __EMX__
120*d5c9a868SElliott Hughes        argv[0] = _getname(argv[0]); _remext(argv[0]); name = argv[0];
121*d5c9a868SElliott Hughes #else
122*d5c9a868SElliott Hughes #ifdef OS_mingw32msvc
123*d5c9a868SElliott Hughes 	_stripexe(argv[0]);
124*d5c9a868SElliott Hughes #endif
125*d5c9a868SElliott Hughes 	name = _basename(argv[0]);
126*d5c9a868SElliott Hughes #endif
127*d5c9a868SElliott Hughes 	progname = argv[0];
128*d5c9a868SElliott Hughes 
129*d5c9a868SElliott Hughes 	/* this allows the different tools to be called as "mtools -c <command>"
130*d5c9a868SElliott Hughes 	** where <command> is mdir, mdel, mcopy etcetera
131*d5c9a868SElliott Hughes 	** Mainly done for the BeOS, which doesn't support links yet.
132*d5c9a868SElliott Hughes 	*/
133*d5c9a868SElliott Hughes 
134*d5c9a868SElliott Hughes 	if(argc >= 3 &&
135*d5c9a868SElliott Hughes 	   !strcmp(argv[1], "-c") &&
136*d5c9a868SElliott Hughes 	   !strcmp(name, "mtools")) {
137*d5c9a868SElliott Hughes 		argc-=2;
138*d5c9a868SElliott Hughes 		argv+=2;
139*d5c9a868SElliott Hughes 		name = argv[0];
140*d5c9a868SElliott Hughes 	}
141*d5c9a868SElliott Hughes 
142*d5c9a868SElliott Hughes 
143*d5c9a868SElliott Hughes 
144*d5c9a868SElliott Hughes 	/* print the version */
145*d5c9a868SElliott Hughes 	if(argc >= 2 &&
146*d5c9a868SElliott Hughes 	   (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") ==0)) {
147*d5c9a868SElliott Hughes 		printf("%s (GNU mtools) %s\n",
148*d5c9a868SElliott Hughes 		       name, mversion);
149*d5c9a868SElliott Hughes 		printf("configured with the following options: ");
150*d5c9a868SElliott Hughes #ifdef USE_XDF
151*d5c9a868SElliott Hughes 		printf("enable-xdf ");
152*d5c9a868SElliott Hughes #else
153*d5c9a868SElliott Hughes 		printf("disable-xdf ");
154*d5c9a868SElliott Hughes #endif
155*d5c9a868SElliott Hughes #ifdef USING_VOLD
156*d5c9a868SElliott Hughes 		printf("enable-vold ");
157*d5c9a868SElliott Hughes #else
158*d5c9a868SElliott Hughes 		printf("disable-vold ");
159*d5c9a868SElliott Hughes #endif
160*d5c9a868SElliott Hughes #ifdef USING_NEW_VOLD
161*d5c9a868SElliott Hughes 		printf("enable-new-vold ");
162*d5c9a868SElliott Hughes #else
163*d5c9a868SElliott Hughes 		printf("disable-new-vold ");
164*d5c9a868SElliott Hughes #endif
165*d5c9a868SElliott Hughes #ifdef DEBUG
166*d5c9a868SElliott Hughes 		printf("enable-debug ");
167*d5c9a868SElliott Hughes #else
168*d5c9a868SElliott Hughes 		printf("disable-debug ");
169*d5c9a868SElliott Hughes #endif
170*d5c9a868SElliott Hughes #ifdef USE_RAWTERM
171*d5c9a868SElliott Hughes 		printf("enable-raw-term ");
172*d5c9a868SElliott Hughes #else
173*d5c9a868SElliott Hughes 		printf("disable-raw-term ");
174*d5c9a868SElliott Hughes #endif
175*d5c9a868SElliott Hughes 		printf("\n");
176*d5c9a868SElliott Hughes 		return 0;
177*d5c9a868SElliott Hughes 	}
178*d5c9a868SElliott Hughes 
179*d5c9a868SElliott Hughes 	read_config();
180*d5c9a868SElliott Hughes 	setup_signal();
181*d5c9a868SElliott Hughes 	for (i = 0; i < NDISPATCH; i++) {
182*d5c9a868SElliott Hughes 		if (!strcmp(name,dispatch[i].cmd))
183*d5c9a868SElliott Hughes 			dispatch[i].fn(argc, argv, dispatch[i].type);
184*d5c9a868SElliott Hughes 	}
185*d5c9a868SElliott Hughes 	if (strcmp(name,"mtools"))
186*d5c9a868SElliott Hughes 		fprintf(stderr,"Unknown mtools command '%s'\n",name);
187*d5c9a868SElliott Hughes 	fprintf(stderr,"Supported commands:");
188*d5c9a868SElliott Hughes 	for (i = 0; i < NDISPATCH; i++) {
189*d5c9a868SElliott Hughes 		if (i%8 == 0) putc('\n', stderr);
190*d5c9a868SElliott Hughes 		else fprintf(stderr, ", ");
191*d5c9a868SElliott Hughes 		fprintf(stderr, "%s", dispatch[i].cmd);
192*d5c9a868SElliott Hughes 	}
193*d5c9a868SElliott Hughes 	putc('\n', stderr);
194*d5c9a868SElliott Hughes 
195*d5c9a868SElliott Hughes 	return 1;
196*d5c9a868SElliott Hughes }
197*d5c9a868SElliott Hughes 
helpFlag(int argc,char ** argv)198*d5c9a868SElliott Hughes int helpFlag(int argc, char **argv) {
199*d5c9a868SElliott Hughes 	return (argc > 1 && !strcmp(argv[1], "--help"));
200*d5c9a868SElliott Hughes }
201