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