[PATCH 17/17] fileio: Add command "com" to write to files

Joel Sherrill joel at rtems.org
Wed Mar 9 22:25:02 UTC 2016


This was used to open, write, and close the VGA console and
/dev/pcicom1 for testing purposes.
---
 testsuites/samples/fileio/Makefile.am |   2 +-
 testsuites/samples/fileio/init.c      |   2 +
 testsuites/samples/fileio/main_com.c  | 129 ++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 testsuites/samples/fileio/main_com.c

diff --git a/testsuites/samples/fileio/Makefile.am b/testsuites/samples/fileio/Makefile.am
index 6e67e42..060fca8 100644
--- a/testsuites/samples/fileio/Makefile.am
+++ b/testsuites/samples/fileio/Makefile.am
@@ -1,5 +1,5 @@
 rtems_tests_PROGRAMS = fileio
-fileio_SOURCES = init.c system.h main_lspci.c
+fileio_SOURCES = init.c system.h main_com.c main_lspci.c
 
 dist_rtems_tests_DATA = fileio.doc
 
diff --git a/testsuites/samples/fileio/init.c b/testsuites/samples/fileio/init.c
index ca3527d..05a7157 100644
--- a/testsuites/samples/fileio/init.c
+++ b/testsuites/samples/fileio/init.c
@@ -1291,10 +1291,12 @@ static rtems_shell_alias_t Shell_USERECHO_Alias = {
   "userecho"                 /* alias */
 };
 
+extern rtems_shell_cmd_t Shell_COM_Command;
 extern rtems_shell_cmd_t Shell_LSPCI_Command;
 
 #define CONFIGURE_SHELL_USER_COMMANDS \
     &Shell_USERCMD_Command, \
+    &Shell_COM_Command, \
     &Shell_LSPCI_Command
 #define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
 #define CONFIGURE_SHELL_COMMANDS_INIT
diff --git a/testsuites/samples/fileio/main_com.c b/testsuites/samples/fileio/main_com.c
new file mode 100644
index 0000000..fda482d
--- /dev/null
+++ b/testsuites/samples/fileio/main_com.c
@@ -0,0 +1,129 @@
+/**
+ * @brief com command
+ *
+ * This file contains a command to perform individual system call operations
+ * on a specified file.
+ */
+
+/*  COPYRIGHT (c) 2016.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.org/license/LICENSE.
+ */
+
+#define __need_getopt_newlib
+#include <rtems.h>
+#include <rtems/shell.h>
+
+#include <unistd.h> /* for getopt */
+#include <getopt.h>
+
+#include <sys/types.h>  /* for open() */
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <errno.h>
+
+
+int main_com(
+  int    argc,
+  char **argv
+);
+
+int main_com(
+  int    argc,
+  char **argv
+)
+{
+  int opt;
+  struct getopt_data getopt_reent;
+  #define optarg getopt_reent.optarg
+  #define optind getopt_reent.optind
+  #define opterr getopt.reent.opterr
+  #define optopt getopt.reent.optopt
+  static int fd = -1;
+  int action_taken = 0;
+
+  memset(&getopt_reent, 0, sizeof(getopt_reent));
+  while ((opt = getopt_r(argc, argv, "o:r:w:c", &getopt_reent )) != -1 ) {
+    switch (opt) {
+      /* o - Open */
+      case 'o':  {
+        char *filename;
+
+        filename = optarg;
+        fd = open( filename, O_RDWR );
+        if ( fd == -1 ) {
+          fprintf( stderr, "Unable to open %s: %s\n", filename, strerror( errno ) );
+          return -1;
+        }
+
+        fprintf( stderr, "Opened %s: fd=%d\n", filename, fd );
+        action_taken = 1;
+        break;
+      }
+      /* r - Read */
+      case 'r':  {
+        fprintf( stderr, "Read is not yet supported\n" );
+        return -1;
+        break;
+      }
+      /* w - Write */
+      case 'w': {
+        char    *p;
+        ssize_t  rc;
+
+        for ( p = optarg ; *p ; p++ ) {
+          rc = write( fd, p, 1 );
+          if ( rc != 1 ) {
+            fprintf( stderr, "write() returned %s\n", strerror( errno ) );
+            return -1;
+          }
+        }
+        fprintf( stderr, "\nwrite() OK\n" );
+        action_taken = 1;
+        break;
+      }
+      /* c - Close */
+      case 'c':  {
+        int rc;
+
+        rc = close( fd );
+        if ( rc == -1 ) {
+          fprintf( stderr, "Unable to close: %s\n", strerror( errno ) );
+          return -1;
+        }
+
+        fprintf( stderr, "Closed\n" );
+        fd = -1;
+        action_taken = 1;
+        break;
+      }
+      default:
+        fprintf( stderr, "%c is not a valid options\n", opt );
+        break;
+    }
+  }
+
+  if (optind < argc) {
+    fprintf( stderr, "Unparsed arguments remain\n" );
+    return -1;
+  }
+
+  if ( action_taken == 0 ) {
+    fprintf( stderr, "No action(s) specified\n" );
+    return -1;
+  }
+  return 0;
+}
+
+rtems_shell_cmd_t Shell_COM_Command = {
+  "com",                                         /* name */
+  "com [-o file] [-rc] [-w string]",             /* usage */
+  "user",                                        /* topic */
+  main_com,                                      /* command */
+  NULL,                                          /* alias */
+  NULL                                           /* next */
+};
-- 
1.8.3.1




More information about the devel mailing list