xref: /haiku/src/bin/mount.c (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
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 <stdlib.h>
15 #include <string.h>
16 
17 
18 static void
19 usage(const char *programName)
20 {
21 
22 	printf("usage: %s [-ro] [-t fstype] [-p parameter] [device] directory\n"
23 		"\t-ro\tmounts the volume read-only\n"
24 		"\t-t\tspecifies the file system to use (defaults to automatic recognition)\n"
25 		"\t-p\tspecifies parameters to pass to the file system (-o also accepted)\n"
26 		"\tif device is not specified, NULL is passed (for in-memory filesystems)\n",programName);
27 	exit(1);
28 }
29 
30 
31 int
32 main(int argc, char **argv)
33 {
34 	const char *programName = argv[0];
35 	const char *device = NULL;
36 	const char *mountPoint;
37 	const char *parameter = NULL;
38 	const char *fs = NULL;
39 	struct stat mountStat;
40 	dev_t volume;
41 	uint32 flags = 0;
42 
43 	/* prettify the program name */
44 
45 	if (strrchr(programName, '/'))
46 		programName = strrchr(programName, '/') + 1;
47 
48 	/* get all options */
49 
50 	while (*++argv) {
51 		char *arg = *argv;
52 		argc--;
53 		if (*arg != '-')
54 			break;
55 
56 		if (!strcmp(++arg, "ro") && (flags & B_MOUNT_READ_ONLY) == 0)
57 			flags |= B_MOUNT_READ_ONLY;
58 		else if (!strcmp(arg, "t") && fs == NULL) {
59 			if (argc <= 1)
60 				break;
61 			fs = *++argv;
62 			argc--;
63 		} else if ((!strcmp(arg, "p") || !strcmp(arg, "o")) && parameter == NULL) {
64 			if (argc <= 1)
65 				break;
66 			parameter = *++argv;
67 			argc--;
68 		} else
69 			usage(programName);
70 	}
71 
72 	/* check the arguments */
73 
74 	if (argc > 1) {
75 		device = argv[0];
76 		argv++;
77 		argc--;
78 	}
79 	mountPoint = argv[0];
80 
81 	if (mountPoint == NULL)
82 		usage(programName);
83 
84 	if (stat(mountPoint, &mountStat) < 0) {
85 		fprintf(stderr, "%s: The mount point '%s' is not accessible\n", programName, mountPoint);
86 		return 1;
87 	}
88 	if (!S_ISDIR(mountStat.st_mode)) {
89 		fprintf(stderr, "%s: The mount point '%s' is not a directory\n", programName, mountPoint);
90 		return 1;
91 	}
92 
93 	/* do the work */
94 
95 	volume = fs_mount_volume(mountPoint, device, fs, flags, parameter);
96 	if (volume < B_OK) {
97 		fprintf(stderr, "%s: %s\n", programName, strerror(volume));
98 		return 1;
99 	}
100 	return 0;
101 }
102 
103