]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: mount command -t option argument parsing
authorTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 07:47:48 +0000 (04:47 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 07:47:48 +0000 (04:47 -0300)
The mount command parsed device=argv[1] and mountpoint=argv[2]
BEFORE scanning for -t, so 'mount -t fat /dev/hdb /test' set
device='-t' and mountpoint='fat' instead of the correct values.

Rewrote argument parsing to scan for -t first, then collect
remaining positional arguments as device and mountpoint.
Added usage message for missing arguments.

user/mount.c

index 42cb54a15f04fa8c4df473c0e8e8442bb693a98e..fba0bf52b5caeaeb0c0cc415167a3a82cd672f37 100644 (file)
@@ -22,22 +22,32 @@ static void show_mounts(void) {
 }
 
 int main(int argc, char** argv) {
-    if (argc < 3) {
+    if (argc < 2) {
         show_mounts();
         return 0;
     }
 
-    const char* device = argv[1];
-    const char* mountpoint = argv[2];
     const char* fstype = "diskfs";
+    const char* device = NULL;
+    const char* mountpoint = NULL;
 
-    /* Parse -t fstype option */
-    for (int i = 1; i < argc - 2; i++) {
+    /* Parse options first, then collect positional args */
+    int i;
+    for (i = 1; i < argc; i++) {
         if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
             fstype = argv[++i];
+        } else if (!device) {
+            device = argv[i];
+        } else if (!mountpoint) {
+            mountpoint = argv[i];
         }
     }
 
+    if (!device || !mountpoint) {
+        fprintf(stderr, "usage: mount [-t fstype] device mountpoint\n");
+        return 1;
+    }
+
     int rc = __syscall_ret(_syscall3(SYS_MOUNT, (int)device, (int)mountpoint, (int)fstype));
     if (rc < 0) {
         fprintf(stderr, "mount: mounting %s on %s failed: %d\n", device, mountpoint, rc);