It's 2013.
[usmb.git] / options.c
blob3ada14e6009537edce65eaaa13c8b395e4d33011
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2013 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "config.h"
18 #include <assert.h>
19 #include <glib.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "options.h"
24 #include "utils.h"
25 #include "version.h"
28 static gboolean version = FALSE;
29 static gchar *conffile = NULL;
30 static gboolean debug = FALSE;
31 static gboolean nofork = FALSE;
32 static gboolean umount = FALSE;
33 static gchar **remaining = NULL;
36 static GOptionEntry entries[] = {
37 { .long_name = "version",
38 .short_name = 'v',
39 .flags = 0,
40 .arg = G_OPTION_ARG_NONE,
41 .arg_data = &version,
42 .description = "Show usmb, FUSE and Samba versions" },
44 { .long_name = "version",
45 .short_name = 'V',
46 .flags = G_OPTION_FLAG_HIDDEN,
47 .arg = G_OPTION_ARG_NONE,
48 .arg_data = &version,
49 .description = NULL },
51 { .long_name = "config",
52 .short_name = 'c',
53 .flags = 0,
54 .arg = G_OPTION_ARG_FILENAME,
55 .arg_data = &conffile,
56 .description = "usmb configuration file" },
58 { .long_name = "debug",
59 .short_name = 'd',
60 .arg = G_OPTION_ARG_NONE,
61 .arg_data = &debug,
62 .description = "Debug mode" },
64 { .long_name = "nofork",
65 .short_name = 'f',
66 .arg = G_OPTION_ARG_NONE,
67 .arg_data = &nofork,
68 .description = "Foreground operation" },
70 { .long_name = "unmount",
71 .short_name = 'u',
72 .arg = G_OPTION_ARG_NONE,
73 .arg_data = &umount,
74 .description = "Unmount the given filesystem" },
76 { .long_name = G_OPTION_REMAINING,
77 .short_name = 0,
78 .arg = G_OPTION_ARG_STRING_ARRAY,
79 .arg_data = &remaining,
80 .description = NULL },
82 { .long_name = NULL }
86 bool parse_args (int *argc, char ***argv,
87 const char **mountid, const char **out_conffile,
88 bool *out_umount)
90 GError *error = NULL;
92 GOptionContext *context =
93 g_option_context_new ("- mount SMB shares via FUSE and Samba");
94 g_option_context_add_main_entries (context, entries, NULL);
95 bool ret = g_option_context_parse (context, argc, argv, &error);
96 g_option_context_free (context);
98 if (false == ret)
100 fprintf (stderr, "Try `%s --help' for more information\n", (*argv)[0]);
101 return false;
104 if (version)
106 show_version (stdout);
107 exit (EXIT_SUCCESS);
110 if ((NULL == remaining) || (NULL == remaining[0]))
112 fputs ("No share ID given.\n", stderr);
113 return false;
116 if (NULL != remaining[1])
118 fputs ("Too many arguments.\n", stderr);
119 return false;
122 *out_umount = umount;
123 *mountid = remaining[0];
124 g_free (remaining);
125 DEBUG (fprintf (stderr, "Mount ID: %s\n", *mountid));
127 if (NULL != conffile)
128 *out_conffile = conffile;
130 return ret;
134 /* FUSE args are:
136 * argv[0]
137 * -s
138 * -d -- if debug mode requested
139 * -f -- if foreground mode requested
140 * -o ... -- if any mount options in the config file
141 * mount point
143 #define MAXARGS 12
144 void build_fuse_args (const char *options, const char *mountpoint,
145 int *out_argc, char ***out_argv)
147 static char USMB[] = "usmb";
148 static char MINUS_S[] = "-s";
149 static char MINUS_D[] = "-d";
150 static char MINUS_F[] = "-f";
151 static char MINUS_O[] = "-o";
152 static char MAX_READ[] = "max_read=32768";
154 assert (NULL != mountpoint);
155 static char *argv[MAXARGS];
157 int argc = 0;
159 argv[argc++] = USMB;
160 argv[argc++] = MINUS_S;
162 if (debug)
163 argv[argc++] = MINUS_D;
165 // force -f in debug mode
166 #ifndef DEBUGON
167 if (nofork)
168 #endif
169 argv[argc++] = MINUS_F;
171 argv[argc++] = MINUS_O;
172 argv[argc++] = MAX_READ;
174 if ((NULL != options) && ('\0' != options[0]))
176 argv[argc++] = MINUS_O;
177 argv[argc++] = (char *)options;
180 argv[argc++] = (char *)mountpoint;
181 argv[argc] = NULL; // for good measure...
183 assert (argc < MAXARGS);
184 *out_argc = argc;
185 *out_argv = argv;
187 #ifdef DEBUGON
188 for (int i = 0; i < argc; ++i)
189 fprintf (stderr, "%d: %s\n", i, argv[i]);
190 #endif