xref: /aosp_15_r20/external/mtools/mmount.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1994,1996-2002,2005-2007,2009 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  * Mount an MSDOS disk
18*d5c9a868SElliott Hughes  *
19*d5c9a868SElliott Hughes  * written by:
20*d5c9a868SElliott Hughes  *
21*d5c9a868SElliott Hughes  * Alain L. Knaff
22*d5c9a868SElliott Hughes  * [email protected]
23*d5c9a868SElliott Hughes  *
24*d5c9a868SElliott Hughes  */
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes #include "sysincludes.h"
27*d5c9a868SElliott Hughes #include "msdos.h"
28*d5c9a868SElliott Hughes #include "mtools.h"
29*d5c9a868SElliott Hughes 
30*d5c9a868SElliott Hughes #ifdef OS_linux
31*d5c9a868SElliott Hughes #include <sys/wait.h>
32*d5c9a868SElliott Hughes #include "mainloop.h"
33*d5c9a868SElliott Hughes #include "fs.h"
34*d5c9a868SElliott Hughes 
35*d5c9a868SElliott Hughes void mmount(int argc, char **argv, int type UNUSEDP) NORETURN;
mmount(int argc,char ** argv,int type UNUSEDP)36*d5c9a868SElliott Hughes void mmount(int argc, char **argv, int type UNUSEDP)
37*d5c9a868SElliott Hughes {
38*d5c9a868SElliott Hughes 	char drive;
39*d5c9a868SElliott Hughes 	int pid;
40*d5c9a868SElliott Hughes 	int status;
41*d5c9a868SElliott Hughes 	struct device dev;
42*d5c9a868SElliott Hughes 	char name[EXPAND_BUF];
43*d5c9a868SElliott Hughes 	int media;
44*d5c9a868SElliott Hughes 	union bootsector boot;
45*d5c9a868SElliott Hughes 	Stream_t *Stream;
46*d5c9a868SElliott Hughes 
47*d5c9a868SElliott Hughes 	if (argc<2 || !argv[1][0]  || argv[1][1] != ':' || argv[1][2]){
48*d5c9a868SElliott Hughes 		fprintf(stderr,"Usage: %s -V drive:\n", argv[0]);
49*d5c9a868SElliott Hughes 		exit(1);
50*d5c9a868SElliott Hughes 	}
51*d5c9a868SElliott Hughes 	drive = ch_toupper(argv[1][0]);
52*d5c9a868SElliott Hughes 	Stream= find_device(drive, O_RDONLY, &dev, &boot, name, &media, 0, NULL);
53*d5c9a868SElliott Hughes 	if(!Stream)
54*d5c9a868SElliott Hughes 		exit(1);
55*d5c9a868SElliott Hughes 	FREE(&Stream);
56*d5c9a868SElliott Hughes 
57*d5c9a868SElliott Hughes 	destroy_privs();
58*d5c9a868SElliott Hughes 
59*d5c9a868SElliott Hughes 	if ( dev.partition ) {
60*d5c9a868SElliott Hughes 		char part_name[4];
61*d5c9a868SElliott Hughes 		sprintf(part_name, "%d", dev.partition %1000);
62*d5c9a868SElliott Hughes 		strcat(name, part_name);
63*d5c9a868SElliott Hughes 	}
64*d5c9a868SElliott Hughes 
65*d5c9a868SElliott Hughes 	/* and finally mount it */
66*d5c9a868SElliott Hughes 	switch((pid=fork())){
67*d5c9a868SElliott Hughes 	case -1:
68*d5c9a868SElliott Hughes 		fprintf(stderr,"fork failed\n");
69*d5c9a868SElliott Hughes 		exit(1);
70*d5c9a868SElliott Hughes 	case 0:
71*d5c9a868SElliott Hughes 		close(2);
72*d5c9a868SElliott Hughes 		open("/dev/null", O_RDWR | O_BINARY | O_LARGEFILE);
73*d5c9a868SElliott Hughes 		argv[1] = strdup("mount");
74*d5c9a868SElliott Hughes 		if ( argc > 2 )
75*d5c9a868SElliott Hughes 			execvp("mount", argv + 1 );
76*d5c9a868SElliott Hughes 		else
77*d5c9a868SElliott Hughes 			execlp("mount", "mount", name, NULL);
78*d5c9a868SElliott Hughes 		perror("exec mount");
79*d5c9a868SElliott Hughes 		exit(1);
80*d5c9a868SElliott Hughes 	default:
81*d5c9a868SElliott Hughes 		while ( wait(&status) != pid );
82*d5c9a868SElliott Hughes 	}
83*d5c9a868SElliott Hughes 	if ( WEXITSTATUS(status) == 0 )
84*d5c9a868SElliott Hughes 		exit(0);
85*d5c9a868SElliott Hughes 	argv[0] = strdup("mount");
86*d5c9a868SElliott Hughes 	argv[1] = strdup("-r");
87*d5c9a868SElliott Hughes 	if(!argv[0] || !argv[1]){
88*d5c9a868SElliott Hughes 		printOom();
89*d5c9a868SElliott Hughes 		exit(1);
90*d5c9a868SElliott Hughes 	}
91*d5c9a868SElliott Hughes 	if ( argc > 2 )
92*d5c9a868SElliott Hughes 		execvp("mount", argv);
93*d5c9a868SElliott Hughes 	else
94*d5c9a868SElliott Hughes 		execlp("mount", "mount","-r", name, NULL);
95*d5c9a868SElliott Hughes 	exit(1);
96*d5c9a868SElliott Hughes }
97*d5c9a868SElliott Hughes 
98*d5c9a868SElliott Hughes #else /* linux */
99*d5c9a868SElliott Hughes 
100*d5c9a868SElliott Hughes #include "msdos.h"
101*d5c9a868SElliott Hughes 
mmount(int argc UNUSEDP,char ** argv UNUSEDP,int type UNUSEDP)102*d5c9a868SElliott Hughes void mmount(int argc UNUSEDP, char **argv UNUSEDP, int type UNUSEDP)
103*d5c9a868SElliott Hughes {
104*d5c9a868SElliott Hughes   fprintf(stderr,"This command is only available for LINUX \n");
105*d5c9a868SElliott Hughes   exit(1);
106*d5c9a868SElliott Hughes }
107*d5c9a868SElliott Hughes #endif /* linux */
108*d5c9a868SElliott Hughes 
109