summaryrefslogtreecommitdiff
path: root/mmount.c
blob: 3499824630d8f152838a589ae2e88ad31c2a1cfb (plain)
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
/*  Copyright 1994,1996-2002,2005-2007,2009 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/>.
 *
 * Mount an MSDOS disk
 *
 * written by:
 *
 * Alain L. Knaff
 * alain@knaff.lu
 *
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"

#ifdef OS_linux
#include <sys/wait.h>
#include "mainloop.h"
#include "fs.h"

void mmount(int argc, char **argv, int type UNUSEDP) NORETURN;
void mmount(int argc, char **argv, int type UNUSEDP)
{
	char drive;
	int pid;
	int status;
	struct device dev;
	char name[EXPAND_BUF];
	int media;
	union bootsector boot;
	Stream_t *Stream;

	if (argc<2 || !argv[1][0]  || argv[1][1] != ':' || argv[1][2]){
		fprintf(stderr,"Usage: %s -V drive:\n", argv[0]);
		exit(1);
	}
	drive = ch_toupper(argv[1][0]);
	Stream= find_device(drive, O_RDONLY, &dev, &boot, name, &media, 0, NULL);
	if(!Stream)
		exit(1);
	FREE(&Stream);

	destroy_privs();

	if ( dev.partition ) {
		char part_name[4];
		sprintf(part_name, "%d", dev.partition %1000);
		strcat(name, part_name);
	}

	/* and finally mount it */
	switch((pid=fork())){
	case -1:
		fprintf(stderr,"fork failed\n");
		exit(1);
	case 0:
		close(2);
		open("/dev/null", O_RDWR | O_BINARY | O_LARGEFILE);
		argv[1] = strdup("mount");
		if ( argc > 2 )
			execvp("mount", argv + 1 );
		else
			execlp("mount", "mount", name, NULL);
		perror("exec mount");
		exit(1);
	default:
		while ( wait(&status) != pid );
	}
	if ( WEXITSTATUS(status) == 0 )
		exit(0);
	argv[0] = strdup("mount");
	argv[1] = strdup("-r");
	if(!argv[0] || !argv[1]){
		printOom();
		exit(1);
	}
	if ( argc > 2 )
		execvp("mount", argv);
	else
		execlp("mount", "mount","-r", name, NULL);
	exit(1);
}

#else /* linux */

#include "msdos.h"

void mmount(int argc UNUSEDP, char **argv UNUSEDP, int type UNUSEDP)
{
  fprintf(stderr,"This command is only available for LINUX \n");
  exit(1);
}
#endif /* linux */