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