xref: /haiku/src/bin/mount.c (revision 302f62604763c95777d6d04cca456e876f471c4f)
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(1);
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 		argc--;
49 		if (*arg != '-')
50 			break;
51 
52 		if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
53 			flags |= B_MOUNT_READ_ONLY;
54 		else if (!strcmp(arg, "t") && fs == NULL) {
55 			if (argc <= 1)
56 				break;
57 			fs = *++argv;
58 			argc--;
59 		} else if (!strcmp(arg, "p") && parameter == NULL) {
60 			if (argc <= 1)
61 				break;
62 			parameter = *++argv;
63 			argc--;
64 		} else
65 			usage(programName);
66 	}
67 
68 	/* check the arguments */
69 
70 	device = argv[0];
71 	mountPoint = argv[1];
72 
73 	if (device == NULL || mountPoint == NULL)
74 		usage(programName);
75 
76 	if (stat(mountPoint, &mountStat) < 0) {
77 		fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
78 		return 1;
79 	}
80 	if (!S_ISDIR(mountStat.st_mode)) {
81 		fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
82 		return 1;
83 	}
84 
85 	/* do the work */
86 
87 	volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
88 	if (volume < B_OK) {
89 		fprintf(stderr, "%s: %s\n", programName, strerror(volume));
90 		return 1;
91 	}
92 	return 0;
93 }
94 
95