Initial import package mtools: Programs for accessing MS-DOS disks without mounting...
[profile/ivi/mtools.git] / mmount.c
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
35 void 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         union bootsector boot;
44         Stream_t *Stream;
45         
46         if (argc<2 || !argv[1][0]  || argv[1][1] != ':' || argv[1][2]){
47                 fprintf(stderr,"Usage: %s -V drive:\n", argv[0]);
48                 exit(1);
49         }
50         drive = toupper(argv[1][0]);
51         Stream= find_device(drive, O_RDONLY, &dev, &boot, name, &media, 0, NULL);
52         if(!Stream)
53                 exit(1);
54         FREE(&Stream);
55
56         destroy_privs();
57
58         if ( dev.partition ) {
59                 char part_name[4];
60                 sprintf(part_name, "%d", dev.partition %1000);
61                 strcat(name, part_name); 
62         }
63
64         /* and finally mount it */
65         switch((pid=fork())){
66         case -1:
67                 fprintf(stderr,"fork failed\n");
68                 exit(1);
69         case 0:
70                 close(2);
71                 open("/dev/null", O_RDWR | O_BINARY | O_LARGEFILE);
72                 argv[1] = strdup("mount");
73                 if ( argc > 2 )
74                         execvp("mount", argv + 1 );
75                 else
76                         execlp("mount", "mount", name, NULL);
77                 perror("exec mount");
78                 exit(1);
79         default:
80                 while ( wait(&status) != pid );
81         }       
82         if ( WEXITSTATUS(status) == 0 )
83                 exit(0);
84         argv[0] = strdup("mount");
85         argv[1] = strdup("-r");
86         if(!argv[0] || !argv[1]){
87                 printOom();
88                 exit(1);
89         }
90         if ( argc > 2 )
91                 execvp("mount", argv);
92         else
93                 execlp("mount", "mount","-r", name, NULL);
94         exit(1);
95 }
96
97 #else /* linux */
98
99 #include "msdos.h"
100
101 void mmount(int argc, char **argv, int type)
102 {
103   fprintf(stderr,"This command is only available for LINUX \n");
104   exit(1);
105 }
106 #endif /* linux */
107