xref: /haiku/src/bin/mount.c (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /**	Mounts a volume with the specified file system */
7 
8 
9 #include <fs_volume.h>
10 
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 
17 static void
18 usage(const char *programName)
19 {
20 
21 	printf("usage: %s [-ro] [-t fstype] [-p parameter] device directory\n"
22 		"\t-ro\tmounts the volume read-only\n"
23 		"\t-t\tspecifies the file system to use (defaults to automatic recognition)\n",programName);
24 	exit(0);
25 }
26 
27 
28 int
29 main(int argc, char **argv)
30 {
31 	const char *programName = argv[0];
32 	const char *device, *mountPoint;
33 	const char *parameter = NULL;
34 	const char *fs = NULL;
35 	struct stat mountStat;
36 	dev_t volume;
37 	uint32 flags = 0;
38 
39 	/* prettify the program name */
40 
41 	if (strrchr(programName, '/'))
42 		programName = strrchr(programName, '/') + 1;
43 
44 	/* get all options */
45 
46 	while (*++argv) {
47 		char *arg = *argv;
48 		if (*arg != '-')
49 			break;
50 
51 		if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
52 			flags |= B_MOUNT_READ_ONLY;
53 		else if (!strcmp(arg, "t") && fs == NULL)
54 			fs = *++argv;
55 		else if (!strcmp(arg, "p") && parameter == NULL)
56 			parameter = *++argv;
57 		else
58 			usage(programName);
59 	}
60 
61 	/* check the arguments */
62 
63 	device = argv[0];
64 	mountPoint = argv[1];
65 
66 	if (device == NULL || mountPoint == NULL)
67 		usage(programName);
68 
69 	if (stat(mountPoint, &mountStat) < 0) {
70 		fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
71 		return -1;
72 	}
73 	if (!S_ISDIR(mountStat.st_mode)) {
74 		fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
75 		return -1;
76 	}
77 
78 	/* do the work */
79 
80 	volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
81 	if (volume < B_OK) {
82 		fprintf(stderr, "%s: %s\n", programName, strerror(volume));
83 		return -1;
84 	}
85 	return 0;
86 }
87 
88