#include <string.h>
#include <sys/mount.h>
-#ifdef DEBUG /* done only on debugging */
+/* pass -D DEBUG to gcc to enable debug printing */
+#ifdef DEBUG
#define debug printf
#else
#define debug(fmt, arg...)
#endif /* DEBUG */
-
/* Adding undefined flags in glibc headers */
-
#ifndef MS_DIRSYNC
#define MS_DIRSYNC 128
#endif
/* Help printing routine */
void usage(void);
+/* struct for name<->flag correspondence */
struct ms_flag {
const char *name;
int flag;
};
-/* compare funtion */
+/* compare funtion to find a flag name */
int ms_flag_cmp(const void *c1, const void *c2)
{
struct ms_flag *v1 = (struct ms_flag *) c1;
return strcmp(v1->name, v2->name);
}
-
-
+/* main body */
int main(int argc, char *argv[])
{
/*
int i, flags=0;
char * mountflags=NULL, *data=NULL, *filesystemtype=NULL;
char *token;
+ /* mount flag table, must be alphabetically ordered */
struct ms_flag ms_values[]=
{
{"bind", MS_BIND},
* Use getopt function
*/
opterr = 0; /* don't want writing to stderr */
- while ( (i = getopt(argc, argv, "d:f:t:h")) != -1) {
+ while ( (i = getopt(argc, argv, "o:f:t:h")) != -1) {
switch (i) {
/*
* Handling options
usage();
return -1;
break;
- case 'd': /* options data */
+ case 'o': /* options data */
data = optarg;
debug("passed option data: %s\n", data);
break;
- case 't': /* options data */
+ case 't': /* filesystem type */
filesystemtype = optarg;
debug("passed filesystem type: %s\n", filesystemtype);
break;
- case 'f': /* options data */
+ case 'f': /* mount flags */
mountflags = optarg;
debug("passed mount flags: %s\n", mountflags);
break;
* Main code beginning
*
* ***********************************************************/
- if ((argc - optind) != 2) { /* There must be 0 remaing arguments */
+ /* There must be 2 arguments, source and target */
+ if ((argc - optind) != 2) {
printf("Wrong number of arguments %d\n", argc - optind);
usage();
}
debug("source %s, target %s\n",argv[optind], argv[optind+1]);
-
-// printf("\n",);
+ /* parse -f option parameter, and build mount flags argument */
if ( token = strtok(mountflags, ",") ) {
do {
key.name = token;
} while (token=strtok(NULL,","));
}
debug("flags value %X\n", flags);
+ /* do mount */
if (mount(argv[optind], argv[optind + 1], filesystemtype, flags, data) == -1) {
perror("Mount call failed");
exit(EXIT_FAILURE);
void usage(void) {
printf("Program mymount: \n");
printf("Usage:\n");
- printf("mymount [-h] [-d opt1[,...]] [-t type] [-f fl1[,...]] source target\n");
+ printf("mymount [-h] [-o opt1[,...]] [-t type] [-f fl1[,...]] source target\n");
printf(" -h print this help\n");
- printf(" -d options comma separated options data string\n");
+ printf(" -o options comma separated options data string\n");
printf(" -t fstype filesystemtype string\n");
- printf(" -f flags comma separated mount flags string\n");
+ printf(" -f flags comma separated mount flags string\n");
+ printf("\n");
+ printf("you always need to specify two argument, source and target,\n");
+ printf("matching the system call ones, if one of them is not needed\n");
+ printf("just pass an empty string\n");
exit(1);
}