Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /**
0002  * @file
0003  * 
0004  * @brief Shell Command Implmentation
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
0009  *
0010  *  The license and distribution terms for this file may be
0011  *  found in the file LICENSE in this distribution or at
0012  *  http://www.rtems.org/license/LICENSE.
0013  */
0014 
0015 #ifdef HAVE_CONFIG_H
0016 #include "config.h"
0017 #endif
0018 
0019 #include <stdio.h>
0020 #include <unistd.h>
0021 #include <string.h>
0022 #include <errno.h>
0023 
0024 #include <rtems.h>
0025 #include <rtems/shell.h>
0026 #include <rtems/shellconfig.h>
0027 #include <rtems/libio.h>
0028 #include "internal.h"
0029 
0030 static bool print_filesystem(const rtems_filesystem_table_t *entry, void *arg)
0031 {
0032   printf("%s ", entry->type);
0033 
0034   return false;
0035 }
0036 
0037 static int rtems_shell_main_mount(
0038   int   argc,
0039   char *argv[]
0040 )
0041 {
0042   rtems_filesystem_options_t options = RTEMS_FILESYSTEM_READ_WRITE;
0043   char*                      type = NULL;
0044   char*                      source = NULL;
0045   char*                      target = NULL;
0046   char*                      fsoptions = NULL;
0047   int                        arg;
0048 
0049   for (arg = 1; arg < argc; arg++) {
0050     if (argv[arg][0] == '-') {
0051       if (argv[arg][1] == 't') {
0052         arg++;
0053         if (arg == argc) {
0054           fprintf(
0055             stderr,
0056             "%s: -t needs a type of file-system; see -L.\n",
0057             argv[0]
0058           );
0059           return 1;
0060         }
0061         type = argv[arg];
0062       } else if (argv[arg][1] == 'r') {
0063         options = RTEMS_FILESYSTEM_READ_ONLY;
0064       } else if (argv[arg][1] == 'L') {
0065         printf ("File systems: ");
0066         rtems_filesystem_iterate(print_filesystem, NULL);
0067         printf ("\n");
0068         return 0;
0069       } else if (argv[arg][1] == 'o') {
0070         arg++;
0071         if (arg == argc) {
0072           fprintf(
0073             stderr,
0074             "%s: -o needs a list of filesystem options.\n",
0075             argv[0]
0076           );
0077           return 1;
0078         }
0079         fsoptions = argv[arg];
0080       } else {
0081         fprintf (stderr, "unknown option: %s\n", argv[arg]);
0082         return 1;
0083       }
0084     } else {
0085       if (!source)
0086         source = argv[arg];
0087       else if (!target)
0088         target = argv[arg];
0089       else {
0090         fprintf (
0091           stderr, "mount: source and mount only require: %s\n", argv[arg]);
0092         return 1;
0093       }
0094     }
0095   }
0096 
0097   if (!type) {
0098     fprintf (stderr, "mount: no file-system; see the -L option\n");
0099     return 1;
0100   }
0101 
0102   if (!source) {
0103     fprintf (stderr, "mount: no source\n");
0104     return 1;
0105   }
0106 
0107   if (!target) {
0108     fprintf (stderr, "mount: no mount point\n");
0109     return 1;
0110   }
0111 
0112   /*
0113    * Mount the disk.
0114    */
0115 
0116   if (mount (source, target, type, options, fsoptions) < 0) {
0117     fprintf (stderr, "error: %s\n", strerror(errno));
0118     return 1;
0119   }
0120 
0121   printf ("mounted %s -> %s\n", source, target);
0122 
0123   return 0;
0124 }
0125 
0126 rtems_shell_cmd_t rtems_shell_MOUNT_Command = {
0127   "mount",                                                  /* name */
0128   "mount [-t type] [-r] [-L] [-o options] source target",   /* usage */
0129   "files",                                                  /* topic */
0130   rtems_shell_main_mount,                                   /* command */
0131   NULL,                                                     /* alias */
0132   NULL                                                      /* next */
0133 };