3 * Copyright (C) 2012 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File mymount.c: An example for mount function
23 * Author: S. Piccardi Aug. 2011
25 *****************************************************************************/
26 #include <sys/types.h> /* primitive system data types */
27 #include <sys/stat.h> /* file characteristics constants and functions */
28 #include <stdlib.h> /* C standard library */
29 #include <stdio.h> /* standard I/O library */
30 #include <unistd.h> /* unix standard library */
32 #include <sys/mount.h>
34 #ifdef DEBUG /* done only on debugging */
37 #define debug(fmt, arg...)
41 /* Adding undefined flags in glibc headers */
44 #define MS_DIRSYNC 128
56 #define MS_SILENT 32768
59 #define MS_UNBINDABLE (1<<17)
62 #define MS_PRIVATE (1<<18)
65 #define MS_SLAVE (1<<19)
68 #define MS_SHARED (1<<20)
71 #define MS_RELATIME (1<<21)
73 #ifndef MS_STRICTATIME
74 #define MS_STRICTATIME (1<<24)
81 /* Help printing routine */
90 int ms_flag_cmp(const void *c1, const void *c2)
92 struct ms_flag *v1 = (struct ms_flag *) c1;
93 struct ms_flag *v2 = (struct ms_flag *) c2;
94 return strcmp(v1->name, v2->name);
99 int main(int argc, char *argv[])
102 * Variables definition
105 char * mountflags=NULL, *data=NULL, *filesystemtype=NULL;
107 struct ms_flag ms_values[]=
110 {"dirsync",MS_DIRSYNC},
111 {"mandlock",MS_MANDLOCK},
113 {"noatime",MS_NOATIME},
115 {"nodiratime",MS_NODIRATIME},
116 {"noexec",MS_NOEXEC},
117 {"nosuid",MS_NOSUID},
118 {"private",MS_PRIVATE},
119 {"rdonly",MS_RDONLY},
121 {"relatime",MS_RELATIME},
122 {"remount",MS_REMOUNT},
124 {"silent",MS_SILENT},
126 {"strictatime",MS_STRICTATIME},
127 {"synchronous",MS_SYNCHRONOUS},
128 {"unbindable",MS_UNBINDABLE}
130 struct ms_flag key, *result;
131 int ms_count = sizeof(ms_values)/sizeof(struct ms_flag);
134 * Input section: decode command line parameters
135 * Use getopt function
137 opterr = 0; /* don't want writing to stderr */
138 while ( (i = getopt(argc, argv, "d:f:t:h")) != -1) {
143 case 'h': /* help option */
144 printf("Wrong -h option use\n");
148 case 'd': /* options data */
150 debug("passed option data: %s\n", data);
152 case 't': /* options data */
153 filesystemtype = optarg;
154 debug("passed filesystem type: %s\n", filesystemtype);
156 case 'f': /* options data */
158 debug("passed mount flags: %s\n", mountflags);
160 case '?': /* unrecognized options */
161 printf("Unrecognized options -%c\n",optopt);
163 default: /* should not reached */
167 /* ***********************************************************
169 * Options processing completed
171 * Main code beginning
173 * ***********************************************************/
174 if ((argc - optind) != 2) { /* There must be 0 remaing arguments */
175 printf("Wrong number of arguments %d\n", argc - optind);
178 debug("source %s, target %s\n",argv[optind], argv[optind+1]);
182 if ( token = strtok(mountflags, ",") ) {
185 result = bsearch(&key, ms_values, ms_count, sizeof(struct ms_flag),
188 debug("found option: %s\n",result->name);
189 flags |= result->flag;
191 printf("unknown flag: %s\n", key.name);
194 } while (token=strtok(NULL,","));
196 debug("flags value %X\n", flags);
197 if (mount(argv[optind], argv[optind + 1], filesystemtype, flags, data) == -1) {
198 perror("Mount call failed");
204 * routine to print usage info and exit
207 printf("Program mymount: \n");
209 printf("mymount [-h] [-d opt1[,...]] [-t type] [-f fl1[,...]] source target\n");
210 printf(" -h print this help\n");
211 printf(" -d options comma separated options data string\n");
212 printf(" -t fstype filesystemtype string\n");
213 printf(" -f flags comma separated mount flags string\n");