xref: /haiku/src/bin/unmount.c (revision 2ae568931fcac7deb9f1e6ff4e47213fbfe4029b)
1 /*
2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
5 
6 /**	Unmounts a volume */
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 [-f] <path to volume>\n"
22 		"\t-f\tforces unmounting in case of open files left\n", programName);
23 	exit(0);
24 }
25 
26 
27 int
28 main(int argc, char **argv)
29 {
30 	const char *programName = argv[0];
31 	struct stat pathStat;
32 	const char *path;
33 	status_t status;
34 	uint32 flags = 0;
35 
36 	/* prettify the program name */
37 
38 	if (strrchr(programName, '/'))
39 		programName = strrchr(programName, '/') + 1;
40 
41 	/* get all options */
42 
43 	while (*++argv) {
44 		char *arg = *argv;
45 		if (*arg != '-')
46 			break;
47 
48 		if (!strcmp(++arg, "f"))
49 			flags |= B_FORCE_UNMOUNT;
50 		else
51 			usage(programName);
52 	}
53 
54 	path = argv[0];
55 
56 	/* check the arguments */
57 
58 	if (path == NULL)
59 		usage(programName);
60 
61 	if (stat(path, &pathStat) < 0) {
62 		fprintf(stderr, "%s: The path \"%s\" is not accessible\n", programName, path);
63 		return -1;
64 	}
65 
66 	/* do the work */
67 
68 	status = fs_unmount_volume(path, flags);
69 	if (status != B_OK) {
70 		fprintf(stderr, "%s: unmounting failed: %s\n", programName, strerror(status));
71 		return -1;
72 	}
73 
74 	return 0;
75 }
76 
77