More stuff
[oweals/busybox.git] / mount.c
diff --git a/mount.c b/mount.c
index 9a1accc88acbf8a6462ac67b13b98a3a3ec75a65..a7d5aed2d46b196b8d76f3fa2e097b0c24b0c0b5 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -22,8 +22,8 @@
  *             will try mounting stuff with all fses when passed -t auto
  *
  * 1999-04-17  Dave Cinege...Rewrote -t auto. Fixed ro mtab.
- * 1999-10-07  Erik Andersen.  Removed mtab usage, major adjustments,
- *              and some serious dieting all around.
+ * 1999-10-07  Erik Andersen.  Rewrote of a lot of code. Removed mtab 
+ *              usage, major adjustments, and some serious dieting all around.
 */
 
 #include "internal.h"
@@ -37,7 +37,7 @@
 #include <ctype.h>
 #include <fstab.h>
 
-const char mount_usage[] = "Usage:\tmount [flags]\n"
+static const char mount_usage[] = "Usage:\tmount [flags]\n"
     "\tmount [flags] device directory [-o options,more-options]\n"
     "\n"
     "Flags:\n"
@@ -81,36 +81,42 @@ static const struct mount_options mount_options[] = {
 };
 
 
+/* Seperate standard mount options from the nonstandard string options */
 static void
-parse_mount_options ( char *options, unsigned long *flags, char *data)
+parse_mount_options ( char *options, unsigned long *flags, char *strflags)
 {
-    printf("option=%s\n", options);
-    while (*options) {
+    while (options) {
+       int gotone=FALSE;
        char *comma = strchr (options, ',');
        const struct mount_options* f = mount_options;
        if (comma)
            *comma = '\0';
 
-       printf("checking option=%s vs %s\n", options, f->name);
        while (f->name != 0) {
-           printf("checking option=%s vs %s\n", options, f->name);
            if (strcasecmp (f->name, options) == 0) {
                *flags &= f->and;
                *flags |= f->or;
-               return;
+               gotone=TRUE;
+               break;
            }
            f++;
        }
-       if (*data) {
-           data += strlen (data);
-           *data++ = ',';
+       if (*strflags && strflags!= '\0' && gotone==FALSE) {
+           char *temp=strflags;
+           temp += strlen (strflags);
+           *temp++ = ',';
+           *temp++ = '\0';
+       }
+       if (gotone==FALSE) {
+           strcat (strflags, options);
+           gotone=FALSE;
        }
-       strcpy (data, options);
        if (comma) {
            *comma = ',';
            options = ++comma;
-       } else
+       } else {
            break;
+       }
     }
 }
 
@@ -163,53 +169,66 @@ mount_one (
 
 extern int mount_main (int argc, char **argv)
 {
-    char string_flags[1024]="\0";
+    char string_flags[1024]="";
     unsigned long flags = 0;
     char *filesystemType = "auto";
+    char *device = NULL;
+    char *directory = NULL;
+    struct stat statBuf;
     int all = 0;
-    int i = argc;
+    int i;
+
+    if (stat("/etc/fstab", &statBuf) < 0) 
+       fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
 
     if (argc == 1) {
        FILE *mountTable;
        if ((mountTable = setmntent ("/proc/mounts", "r"))) {
            struct mntent *m;
            while ((m = getmntent (mountTable)) != 0) {
+               struct fstab* fstabItem;
                char *blockDevice = m->mnt_fsname;
-               if (strcmp (blockDevice, "/dev/root") == 0)
-                   blockDevice = (getfsfile ("/"))->fs_spec;
+               /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
+               if (strcmp (blockDevice, "/dev/root") == 0) {
+                   fstabItem = getfsfile ("/");
+                   if (fstabItem != NULL)
+                       blockDevice = fstabItem->fs_spec;
+               }
                printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
                        m->mnt_type, m->mnt_opts);
            }
            endmntent (mountTable);
        }
-       return( TRUE);
+       exit( TRUE);
     }
 
 
     /* Parse options */
-    while (**argv) {
+    i = --argc;
+    argv++;
+    while (i > 0 && **argv) {
        if (**argv == '-') {
-           switch (**argv) {
+           while (i>0 && *++(*argv)) switch (**argv) {
            case 'o':
-               if (++argv == 0) {
+               if (--i == 0) {
                    fprintf (stderr, "%s\n", mount_usage);
-                   return( FALSE);
+                   exit( FALSE);
                }
-               parse_mount_options (*argv, &flags, string_flags);
-               argc--;
-               argv++;
+               parse_mount_options (*(++argv), &flags, string_flags);
+               --i;
+               ++argv;
                break;
            case 'r':
                flags |= MS_RDONLY;
                break;
            case 't':
-               if (++argv == 0) {
+               if (--i == 0) {
                    fprintf (stderr, "%s\n", mount_usage);
-                   return( FALSE);
+                   exit( FALSE);
                }
-               filesystemType = *argv;
-               argc--;
-               argv++;
+               filesystemType = *(++argv);
+               --i;
+               ++argv;
                break;
            case 'w':
                flags &= ~MS_RDONLY;
@@ -221,7 +240,17 @@ extern int mount_main (int argc, char **argv)
            case 'h':
            case '-':
                fprintf (stderr, "%s\n", mount_usage);
-               return( TRUE);
+               exit( TRUE);
+               break;
+           }
+       } else {
+           if (device == NULL)
+               device=*argv;
+           else if (directory == NULL)
+               directory=*argv;
+           else {
+               fprintf (stderr, "%s\n", mount_usage);
+               exit( TRUE);
            }
        }
        i--;
@@ -234,11 +263,8 @@ extern int mount_main (int argc, char **argv)
 
        if (f == NULL) {
            perror("/etc/fstab");
-           return( FALSE); 
+           exit( FALSE); 
        }
-       // FIXME: Combine read routine (make new function) with unmount_all
-       // to save space.
-
        while ((m = getmntent (f)) != NULL) {
            // If the file system isn't noauto, and isn't mounted on /, mount 
            // it
@@ -250,23 +276,15 @@ extern int mount_main (int argc, char **argv)
                           m->mnt_opts);
            }
        }
-
        endmntent (f);
     } else {
-       if (argc >= 3) {
-           while (i < argc)
-               argv--;
-           while (**argv == '-')
-               argv++;
-           if (mount_one
-               (*argv, *(argv+1), filesystemType, flags,
-                string_flags) == 0) return 0;
-           else
-               return( FALSE);
+       if (device && directory) {
+           exit (mount_one (device, directory, filesystemType, 
+                       flags, string_flags));
        } else {
            fprintf (stderr, "%s\n", mount_usage);
-           return( FALSE);
+           exit( FALSE);
        }
     }
-    return( TRUE);
+    exit( TRUE);
 }