Some patches from Gennady Feldman. Fixed a glob problem such that
[oweals/busybox.git] / mount.c
diff --git a/mount.c b/mount.c
index 57dc73e55aec7abc6e720f1bb0906a51ce31ffa0..90c1cc723beec79ceae2f6565e32bed99b0cc808 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -84,11 +84,7 @@ extern int mount (__const char *__special_file, __const char *__dir,
 extern int umount (__const char *__special_file);
 extern int umount2 (__const char *__special_file, int __flags);
 
-#include <sys/syscall.h>
-#include <linux/unistd.h>
-static int sysfs( int option, unsigned int fs_index, char * buf);
-_syscall3(int, sysfs, int, option, unsigned int, fs_index, char *, buf);
-
+extern int sysfs( int option, unsigned int fs_index, char * buf);
 
 extern const char mtab_file[]; /* Defined in utility.c */
 
@@ -277,6 +273,64 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
        return (TRUE);
 }
 
+void show_mounts()
+{
+#if defined BB_FEATURE_USE_DEVPS_PATCH
+       int fd, i, numfilesystems;
+       char device[] = "/dev/mtab";
+       struct k_mntent *mntentlist;
+
+       /* open device */ 
+       fd = open(device, O_RDONLY);
+       if (fd < 0)
+               perror_msg_and_die("open failed for `%s'", device);
+
+       /* How many mounted filesystems?  We need to know to 
+        * allocate enough space for later... */
+       numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS);
+       if (numfilesystems<0)
+               perror_msg_and_die( "\nDEVMTAB_COUNT_MOUNTS");
+       mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent));
+               
+       /* Grab the list of mounted filesystems */
+       if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0)
+               perror_msg_and_die( "\nDEVMTAB_GET_MOUNTS");
+
+       for( i = 0 ; i < numfilesystems ; i++) {
+               printf( "%s %s %s %s %d %d\n", mntentlist[i].mnt_fsname,
+                               mntentlist[i].mnt_dir, mntentlist[i].mnt_type, 
+                               mntentlist[i].mnt_opts, mntentlist[i].mnt_freq, 
+                               mntentlist[i].mnt_passno);
+       }
+#ifdef BB_FEATURE_CLEAN_UP
+       /* Don't bother to close files or free memory.  Exit 
+        * does that automagically, so we can save a few bytes */
+       free( mntentlist);
+       close(fd);
+#endif
+       exit(EXIT_SUCCESS);
+#else
+       FILE *mountTable = setmntent(mtab_file, "r");
+
+       if (mountTable) {
+               struct mntent *m;
+
+               while ((m = getmntent(mountTable)) != 0) {
+                       char *blockDevice = m->mnt_fsname;
+                       if (strcmp(blockDevice, "/dev/root") == 0) {
+                               find_real_root_device_name( blockDevice);
+                       }
+                       printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
+                                  m->mnt_type, m->mnt_opts);
+               }
+               endmntent(mountTable);
+       } else {
+               perror_msg_and_die("%s", mtab_file);
+       }
+       exit(EXIT_SUCCESS);
+#endif
+}
+
 extern int mount_main(int argc, char **argv)
 {
        char string_flags_buf[1024] = "";
@@ -289,126 +343,49 @@ extern int mount_main(int argc, char **argv)
        int all = FALSE;
        int fakeIt = FALSE;
        int useMtab = TRUE;
-       int i;
        int rc = EXIT_FAILURE;
        int fstabmount = FALSE; 
-
-#if defined BB_FEATURE_USE_DEVPS_PATCH
-       if (argc == 1) {
-               int fd, i, numfilesystems;
-               char device[] = "/dev/mtab";
-               struct k_mntent *mntentlist;
-
-               /* open device */ 
-               fd = open(device, O_RDONLY);
-               if (fd < 0)
-                       perror_msg_and_die("open failed for `%s'", device);
-
-               /* How many mounted filesystems?  We need to know to 
-                * allocate enough space for later... */
-               numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS);
-               if (numfilesystems<0)
-                       perror_msg_and_die( "\nDEVMTAB_COUNT_MOUNTS");
-               mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent));
-               
-               /* Grab the list of mounted filesystems */
-               if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0)
-                       perror_msg_and_die( "\nDEVMTAB_GET_MOUNTS");
-
-               for( i = 0 ; i < numfilesystems ; i++) {
-                       printf( "%s %s %s %s %d %d\n", mntentlist[i].mnt_fsname,
-                                       mntentlist[i].mnt_dir, mntentlist[i].mnt_type, 
-                                       mntentlist[i].mnt_opts, mntentlist[i].mnt_freq, 
-                                       mntentlist[i].mnt_passno);
-               }
-#ifdef BB_FEATURE_CLEAN_UP
-               /* Don't bother to close files or free memory.  Exit 
-                * does that automagically, so we can save a few bytes */
-               free( mntentlist);
-               close(fd);
-#endif
-               return EXIT_SUCCESS;
-       }
-#else
-       if (argc == 1) {
-               FILE *mountTable = setmntent(mtab_file, "r");
-
-               if (mountTable) {
-                       struct mntent *m;
-
-                       while ((m = getmntent(mountTable)) != 0) {
-                               char *blockDevice = m->mnt_fsname;
-                               if (strcmp(blockDevice, "/dev/root") == 0) {
-                                       find_real_root_device_name( blockDevice);
-                               }
-                               printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
-                                          m->mnt_type, m->mnt_opts);
-                       }
-                       endmntent(mountTable);
-               } else {
-                       perror_msg_and_die("%s", mtab_file);
-               }
-               return EXIT_SUCCESS;
-       }
-#endif
+       int opt;
 
        /* Parse options */
-       i = --argc;
-       argv++;
-       while (i > 0 && **argv) {
-               if (**argv == '-') {
-                       char *opt = *argv;
-
-                       while (i > 0 && *++opt)
-                               switch (*opt) {
-                               case 'o':
-                                       if (--i == 0) {
-                                               goto goodbye;
-                                       }
-                                       parse_mount_options(*(++argv), &flags, string_flags);
-                                       break;
-                               case 'r':
-                                       flags |= MS_RDONLY;
-                                       break;
-                               case 't':
-                                       if (--i == 0) {
-                                               goto goodbye;
-                                       }
-                                       filesystemType = *(++argv);
-                                       break;
-                               case 'w':
-                                       flags &= ~MS_RDONLY;
-                                       break;
-                               case 'a':
-                                       all = TRUE;
-                                       break;
-                               case 'f':
-                                       fakeIt = TRUE;
-                                       break;
+       while ((opt = getopt(argc, argv, "o:rt:wafnv")) > 0) {
+               switch (opt) {
+               case 'o':
+                       parse_mount_options(optarg, &flags, string_flags);
+                       break;
+               case 'r':
+                       flags |= MS_RDONLY;
+                       break;
+               case 't':
+                       filesystemType = optarg;
+                       break;
+               case 'w':
+                       flags &= ~MS_RDONLY;
+                       break;
+               case 'a':
+                       all = TRUE;
+                       break;
+               case 'f':
+                       fakeIt = TRUE;
+                       break;
 #ifdef BB_FEATURE_MTAB_SUPPORT
-                               case 'n':
-                                       useMtab = FALSE;
-                                       break;
+               case 'n':
+                       useMtab = FALSE;
+                       break;
 #endif
-                               case 'v':
-                                       break; /* ignore -v */
-                               case 'h':
-                               case '-':
-                                       goto goodbye;
-                               }
-               } else {
-                       if (device == NULL)
-                               device = *argv;
-                       else if (directory == NULL)
-                               directory = *argv;
-                       else {
-                               goto goodbye;
-                       }
+               case 'v':
+                       break; /* ignore -v */
                }
-               i--;
-               argv++;
        }
 
+       if (argv[optind] != NULL) {
+               device = argv[optind];
+               directory = argv[optind + 1];
+       }
+
+       if (device == NULL && !all)
+               show_mounts();
+
        if (all == TRUE || directory == NULL) {
                struct mntent *m;
                FILE *f = setmntent("/etc/fstab", "r");
@@ -471,7 +448,4 @@ singlemount:
        }
        
        goto singlemount;
-       
-goodbye:
-       show_usage();
 }