source: mainline/uspace/app/bdsh/cmds/modules/mount/mount.c@ 72fda53

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 72fda53 was c08fb04, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

Print the mtab list when executing the mount command without any argument.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2009 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <vfs/vfs.h>
32#include <adt/list.h>
33#include <errno.h>
34#include <getopt.h>
35#include "config.h"
36#include "util.h"
37#include "errors.h"
38#include "entry.h"
39#include "mount.h"
40#include "cmds.h"
41
42static const char *cmdname = "mount";
43
44static struct option const long_options[] = {
45 { "help", no_argument, 0, 'h' },
46 { "instance", required_argument, 0, 'i' },
47 { 0, 0, 0, 0 }
48};
49
50
51/* Displays help for mount in various levels */
52void help_cmd_mount(unsigned int level)
53{
54 static char helpfmt[] =
55 "Usage: %s <fstype> <mp> [dev] [<moptions>]\n";
56 if (level == HELP_SHORT) {
57 printf("'%s' mounts a file system.\n", cmdname);
58 } else {
59 help_cmd_mount(HELP_SHORT);
60 printf(helpfmt, cmdname);
61 }
62 return;
63}
64
65static void print_mtab_list(void)
66{
67 LIST_INITIALIZE(mtab_list);
68 get_mtab_list(&mtab_list);
69
70 mtab_list_ent_t *old_ent = NULL;
71
72 list_foreach(mtab_list, cur) {
73 mtab_list_ent_t *ent = list_get_instance(cur, mtab_list_ent_t,
74 link);
75
76 mtab_ent_t *mtab_ent = &ent->mtab_ent;
77
78 if (old_ent)
79 free(old_ent);
80
81 old_ent = ent;
82
83 printf("%s on %s ", mtab_ent->fs_name, mtab_ent->mp);
84
85 if (str_size(mtab_ent->opts) > 0)
86 printf("opts=%s ", mtab_ent->opts);
87
88 printf("(instance=%d, flags=%d, fs_handle=%d)\n",
89 mtab_ent->instance, mtab_ent->flags, mtab_ent->fs_handle);
90 }
91
92 if (old_ent)
93 free(old_ent);
94}
95
96/* Main entry point for mount, accepts an array of arguments */
97int cmd_mount(char **argv)
98{
99 unsigned int argc;
100 const char *mopts = "";
101 const char *dev = "";
102 int rc, c, opt_ind;
103 unsigned int instance = 0;
104 bool instance_set = false;
105 char **t_argv;
106
107 argc = cli_count_args(argv);
108
109 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
110 c = getopt_long(argc, argv, "i:h", long_options, &opt_ind);
111 switch (c) {
112 case 'h':
113 help_cmd_mount(HELP_LONG);
114 return CMD_SUCCESS;
115 case 'i':
116 instance = (unsigned int) strtol(optarg, NULL, 10);
117 instance_set = true;
118 break;
119 }
120 }
121
122 if (instance_set) {
123 argc -= 2;
124 t_argv = &argv[2];
125 } else
126 t_argv = &argv[0];
127
128 if ((argc == 2) || (argc > 5)) {
129 printf("%s: invalid number of arguments. Try `mount --help'\n",
130 cmdname);
131 return CMD_FAILURE;
132 }
133 if (argc == 1) {
134 print_mtab_list();
135 return CMD_SUCCESS;
136 }
137 if (argc > 3)
138 dev = t_argv[3];
139 if (argc == 5)
140 mopts = t_argv[4];
141
142 rc = mount(t_argv[1], t_argv[2], dev, mopts, 0, instance);
143 if (rc != EOK) {
144 printf("Unable to mount %s filesystem to %s on %s (rc=%d)\n",
145 t_argv[1], t_argv[2], t_argv[3], rc);
146 return CMD_FAILURE;
147 }
148
149 return CMD_SUCCESS;
150}
151
Note: See TracBrowser for help on using the repository browser.