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