-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanually-umount.c
107 lines (84 loc) · 2.18 KB
/
manually-umount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* manually-umount - Umounts a resource
* Bruno Mondelo Giaramita
* 2017-05-04 Escola Del Treball De Barcelona
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <errno.h>
#include <string.h>
#define PROGRAM_NAME "manually-umount"
/* The program full name */
#define AUTHORS \
proper_name("Bruno Mondelo")
#define EXIT_BAD_UMOUNT 1
/* Failed umounting */
#define EXIT_BAD_ARGUMENT 147
/* Unknown argument */
#define EXIT_BAD_NUMBER_ARGUMENTS 148
/* Bad number of arguments */
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf(stderr, "Try '%s --help' for more information\n", PROGRAM_NAME);
else
printf("Usage: manually-umount RESOURCE\n\
Mounts a resource to the specified destination\n\
\n\
Options:\n\
-h, --help Shows this help and exits\n\
\n\
Examples:\n\
manually-umount /mnt\n");
exit(status);
}
int
strstart (const char * phrase, const char * prefix)
{
/*
* Function to check if a string starts with a prefix
* Input: char *, char *
* Output: Int
1 -> Contains
0 -> Not contains
*/
if (strlen(phrase) < strlen(prefix))
return 0;
/* The phrase is greater than the prefix */
if (strncmp(prefix, phrase, strlen(prefix)) == 0)
return 1;
/* Check if the prefix is there */
return 0;
}
int
main (int argc, char ** argv)
{
char * resource;
if (argc != 2)
{
fprintf(stderr, "%s: Bad usage\n", PROGRAM_NAME);
usage(EXIT_BAD_NUMBER_ARGUMENTS);
}
/* Check maximum arguments */
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
usage(EXIT_SUCCESS);
else if (strstart(argv[1], "-"))
{
fprintf(stderr, "%s: Unknown option %s\n", PROGRAM_NAME, argv[1]);
usage(EXIT_BAD_ARGUMENT);
}
/* In case the argument is '-h' or '--help' print
help info, if it's other option is a error */
resource = argv[1];
if (umount(resource) != 0) {
fprintf(stderr, "%s: Failed umounting resource %s\n", PROGRAM_NAME,
resource);
fprintf(stderr, "Error (%d) info: %s\n", errno, strerror(errno));
return EXIT_BAD_UMOUNT;
}
/* Umount resource */
return 0;
/* No news good news */
}