X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Fmymount.c;h=d419026efab0f3adad2d2795d1725859f67b9e13;hp=9c8da0d423d3eb81d9c0ddedb1ded75e44804943;hb=26f7a8bb19c6cb198c213757a97b6ac79e40db4b;hpb=4aa6c51696d2b11c572eccd37238db1691785573 diff --git a/sources/mymount.c b/sources/mymount.c index 9c8da0d..d419026 100644 --- a/sources/mymount.c +++ b/sources/mymount.c @@ -20,7 +20,7 @@ * * File mymount.c: An example for mount function * - * Author: S. Piccardi Aug. 2011 + * Author: S. Piccardi Jan. 2012 * *****************************************************************************/ #include /* primitive system data types */ @@ -31,15 +31,14 @@ #include #include -#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 @@ -81,12 +80,13 @@ /* 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; @@ -94,8 +94,7 @@ int ms_flag_cmp(const void *c1, const void *c2) return strcmp(v1->name, v2->name); } - - +/* main body */ int main(int argc, char *argv[]) { /* @@ -104,6 +103,7 @@ 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}, @@ -135,7 +135,7 @@ int main(int argc, char *argv[]) * 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 @@ -145,15 +145,15 @@ int main(int argc, char *argv[]) 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; @@ -171,14 +171,14 @@ int main(int argc, char *argv[]) * 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; @@ -194,6 +194,7 @@ int main(int argc, char *argv[]) } 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); @@ -206,10 +207,17 @@ int main(int argc, char *argv[]) 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"); + printf("you also need to specify options and flag, give an empty \n"); + printf("string for option if you don't want to set any, i.e. \n"); + printf(" ./mymount -t ext4 -o "" -f remount,rdonly /dev/sdb1 /mnt"); exit(1); }