You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/09/28 17:57:23 UTC

[GitHub] [incubator-nuttx-apps] patacongo opened a new pull request #407: Add IPv4 Configuration File Access Helper

patacongo opened a new pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407


   ## Summary
   
   This commit adds support to access an IPv4 Configuration file similar to the Linux dhpc.client ipfcg file.  This version, tailored for deeply embedded systems supports several options to tailor the file and file access to different environments.  It supports:
   
   - Writable as well as read-only configuration files.
   - ASCII human readable files as well as smaller binary files.
   - It supports using character driver access to constrained media (such as EEPROM).
   
   ## Impact
   
   Since nothing uses the IPv4 Configuration file at present, there should be no impact from this addition.
   
   ## Testing
   
   To be provided after initial review.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496933158



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)

Review comment:
       fixed

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)

Review comment:
       fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700923263


   @patacongo 
   
   > @davids5 I final completed testing using examples/ipcfg. It is good to merge from my point of view.
   
   I tested the ASCII as well it is AOK.
   
   Once CI is happy I will merge it. 
   
   Greg, Thank you for taking the time to do this! It made it clear to me what was needed. Some time I just have to see it to understand. Than you again!
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496948971



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)

Review comment:
       Fixed by incorporating your change.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700921716


   @davids5 I final completed testing using examples/ipcfg.  It is good to merge from my point of view.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496948985



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)

Review comment:
       > I don't understand this comment. Can you add some more words? Which strcmp() are you referring to?
   
   Sorry for not being clear. The fgets leaves the '/n' So we need a trim() See patacongo@8e3d080




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700888282


   @davids5 I did add an example/ipcfg so that I can do testing as well.  But it sounds like you are ahead of me.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496933740



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)

Review comment:
       I don't understand this comment.  Can you add some more words?  Which strcmp() are you referring to?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 merged pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 merged pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700199282


   @patacongo - Thank you!


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700890189


   @patacongo yep. Here is the last change needed https://github.com/patacongo/incubator-nuttx-apps/commit/8e3d080ae149a164f8a1ea42eb1285db290b7ecd


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496932808



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_STATIC;
+                }
+              else if (strcmp(variable, "dhcp") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_DHCP;
+                }
+              else if (strcmp(variable, "fallback") == 0)

Review comment:
       fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496933849



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");

Review comment:
       fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 edited a comment on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 edited a comment on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700899372


   @patacongo - If you are done. We can merge this. Then I can fixup the 2 PR I had to use this.
   
   Go ahead and merge.  If I find other issues, I will create a new PR to fix them.  I will test ASCII mode since you are dealing with binary mode.  I am having a lot of problems today.  The simulator is not working for me at all; I think maybe it is broken under Cygwin.
   
   All of my hardware is older and intermittent (that is like in the tropics).  So I still don't have a good test platform.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 edited a comment on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 edited a comment on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700899372


   @patacongo - If you are done. We can merge this. Then I can fixup the 2 PR I had to use this.
   
   Go ahead and merge.  If I find other issues, I will create a new PR to fix them.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496917576



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)

Review comment:
       value

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");

Review comment:
       missing argument

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)

Review comment:
       The strcmp will fail as the the EOL is included. This needs to remove the EOL

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)

Review comment:
       "

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_STATIC;
+                }
+              else if (strcmp(variable, "dhcp") == 0)

Review comment:
       "

##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_STATIC;
+                }
+              else if (strcmp(variable, "dhcp") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_DHCP;
+                }
+              else if (strcmp(variable, "fallback") == 0)

Review comment:
       "




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo edited a comment on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700202368


   I just added another option to use character devices to support writing at an offset into the device.
   
   @davids5 So you are going to test it?  I was thinking of adding an examples/ipcfg to just do a simple read/write test.  But I am sure your testing would be better and we don't really need another just example.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700899372


   @patacongo - If you are done. We can merge this. Then I can fixup the 2 PR I had to use this.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700202368


   I just added another option to use character devices to support writing at an offset into the device.
   
   @davids5 So you are going to test it?  I was thinking of adding an examples/ipcfg to just to a simple read/write test.  But I am sure your testing would be better and we don't really need another just example.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] davids5 commented on pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
davids5 commented on pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#issuecomment-700210470


   @patacongo - yes see top post: 
   > I need to finish some other tasks and will test this ASAP (Please give me a day or 2).


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] patacongo commented on a change in pull request #407: Add IPv4 Configuration File Access Helper

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #407:
URL: https://github.com/apache/incubator-nuttx-apps/pull/407#discussion_r496932994



##########
File path: fsutils/ipcfg/ipcfg.c
##########
@@ -0,0 +1,562 @@
+/****************************************************************************
+ * apps/fsutils/ipcfg/ipcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fsutils/ipcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_LINESIZE  80
+#define MAX_BOOTPROTO BOOTPROTO_FALLBACK
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static const char *g_proto_name[] =
+{
+  "none",      /* BOOTPROTO_NONE */
+  "static",    /* BOOTPROTO_STATIC */
+  "dhcp",      /* BOOTPROTO_DHCP */
+  "fallback"   /* BOOTPROTO_FALLBACK */
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_allocpath
+ *
+ * Description:
+ *   Allocate memory for and construct the full path to the IPv4
+ *   Configuration file.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *
+ * Returned Value:
+ *   A pointer to the allocated path is returned.  NULL is returned only on
+ *  on a failure to allocate.
+ *
+ ****************************************************************************/
+
+static inline FAR char *ipcfg_allocpath(FAR const char *netdev)
+{
+#ifndef CONFIG_IPCFG_CHARDEV
+  FAR char *path = NULL;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device.
+   * the path of CONFIG_IPCFG_PATH returns to a directory containing
+   * multiple files of the form ipcfg-<dev> where <dev> is the network
+   * device name.  For example, ipcfg-eth0.
+   */
+
+  ret = asprintf(&path, CONFIG_IPCFG_PATH "/ipcfg-%s", netdev);
+  if (ret < 0 || path == NULL)
+    {
+      /* Assume that asprintf failed to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n",
+              ret);
+      return NULL;
+    }
+
+  return path;
+
+#else
+  /* In this case CONFIG_IPCFG_PATH is the full path to a character device
+   * that describes the configuration of a single network device.
+   *
+   * It is dup'ed to simplify typing (const vs non-const) and to make the
+   * free operation unconditional.
+   */
+
+  return strdup(CONFIG_IPCFG_PATH);
+#endif
+}
+
+/****************************************************************************
+ * Name: ipcfg_open (for ASCII mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   stream - Location to return the opened stream
+ *   mode   - File fopen mode
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, FAR FILE **stream,
+                      FAR const char *mode)
+{
+  FAR char *path;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  *stream = fopen(path, mode);
+  ret     = OK;
+
+  if (*stream == NULL)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+    }
+
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_open (for binary mode)
+ *
+ * Description:
+ *   Form the complete path to the ipcfg file and open it.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   oflags - File open flags
+ *   mode   - File creation mode
+ *
+ * Returned Value:
+ *   The open file descriptor is returned on success; a negated errno value
+ *   is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IPCFG_BINARY
+static int ipcfg_open(FAR const char *netdev, int oflags, mode_t mode)
+{
+  FAR char *path;
+  int fd;
+  int ret;
+
+  /* Create the full path to the ipcfg file for the network device */
+
+  path = ipcfg_allocpath(netdev);
+  if (path == NULL)
+    {
+      /* Assume failure to allocate memory */
+
+      fprintf(stderr, "ERROR: Failed to create path to ipcfg file: %d\n");
+      return -ENOMEM;
+    }
+
+  /* Now open the file */
+
+  fd  = open(path, oflags, mode);
+  ret = OK;
+
+  if (fd < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, ret);
+      goto errout_with_path;
+    }
+
+#if defined(CONFIG_IPCFG_OFFSET) && CONFIG_IPCFG_OFFSET > 0
+  /* If the binary file is accessed on a character device as a binary
+   * file, then there is also an option to seek to a location on the
+   * media before reading or writing the file.
+   */
+
+  ret = lseek(fd, CONFIG_IPCFG_OFFSET, SEEK_SET);
+  if (ret < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to seek to $ld: %d\n",
+              (long)CONFIG_IPCFG_OFFSET, ret);
+      close(fd);
+    }
+#endif
+
+errout_with_path:
+  free(path);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_skipspace
+ *
+ * Description:
+ *   Skip over any whitespace.
+ *
+ * Input Parameters:
+ *   line  - Pointer to line buffer
+ *   index - Current index into the line buffer
+ *
+ * Returned Value:
+ *   New value of index.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_IPCFG_BINARY
+static int ipcfg_skipspace(FAR const char *line, int index)
+{
+  while (line[index] != '\0' && isspace(line[index]))
+    {
+      index++;
+    }
+
+  return index;
+}
+#endif
+
+/****************************************************************************
+ * Name: ipcfg_putaddr
+ *
+ * Description:
+ *   Write a <variable>=<address> value pair to the stream.
+ *
+ * Input Parameters:
+ *   stream   - The output stream
+ *   variable - The variable namespace
+ *   address  - The IP address to write
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_IPCFG_WRITABLE) && !defined(CONFIG_IPCFG_BINARY)
+static void ipcfg_putaddr(FAR FILE *stream, FAR const char *variable,
+                          in_addr_t address)
+{
+  /* REVISIT:  inet_ntoa() is not thread safe. */
+
+  if (address != 0)
+    {
+      struct in_addr saddr =
+      {
+        address
+      };
+
+      fprintf(stream, "%s=%s\n", variable, inet_ntoa(saddr));
+    }
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ipcfg_read
+ *
+ * Description:
+ *   Read and parse the IP configuration file for the specified network
+ *   device.
+ *
+ * Input Parameters:
+ *   netdev - The network device.  For examplel "eth0"
+ *   ipcfg  - Pointer to a user provided location to receive the IP
+ *            configuration.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int ipcfg_read(FAR const char *netdev, FAR struct ipcfg_s *ipcfg)
+{
+#ifdef CONFIG_IPCFG_BINARY
+  ssize_t nread;
+  int fd;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  fd = ipcfg_open(netdev, O_RDONLY, 0666);
+  if (fd < 0)
+    {
+      return fd;
+    }
+
+  /* Read the file content */
+
+  nread = read(fd, ipcfg, sizeof(struct ipcfg_s));
+  if (nread < 0)
+    {
+      ret = -errno;
+      fprintf(stderr, "ERROR: Failed to read file: %d\n", ret);
+    }
+  else if (nread != sizeof(struct ipcfg_s))
+    {
+      ret = -EIO;
+      fprintf(stderr, "ERROR: Bad read size: %ld\n", (long)nread);
+    }
+  else
+    {
+      ret = OK;
+    }
+
+  close(fd);
+  return ret;
+
+#else
+  FAR FILE *stream;
+  char line[MAX_LINESIZE];
+  int index;
+  int ret;
+
+  DEBUGASSERT(netdev != NULL && ipcfg != NULL);
+
+  /* Open the file */
+
+  ret = ipcfg_open(netdev, &stream, "r");
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Process each line in the file */
+
+  memset(ipcfg, 0, sizeof(FAR struct ipcfg_s));
+
+  while (fgets(line, MAX_LINESIZE, stream) != NULL)
+    {
+      /* Skip any leading whitespace */
+
+      index = ipcfg_skipspace(line, 0);
+
+      /* Check for a blank line or a comment */
+
+      if (line[index] != '\0' && line[index] != '#')
+        {
+          FAR char *variable = &line[index];
+          FAR char *value;
+
+          /* Expect <variable>=<value> pair */
+
+          value = strchr(variable, '=');
+          if (value == NULL)
+            {
+              fprintf(stderr, "ERROR: Skipping malformed line in file: %s\n",
+                      line);
+              continue;
+            }
+
+          /* NUL-terminate the variable string */
+
+          *value++ = '\0';
+
+          /* Process the variable assignment */
+
+          if (strcmp(variable, "DEVICE") == 0)
+            {
+              /* Just assure that it matches the filename */
+
+              if (strcmp(value, netdev) != 0)
+                {
+                  fprintf(stderr, "ERROR: Bad device in file: %s=%s\n",
+                          variable, value);
+                }
+            }
+          else if (strcmp(variable, "BOOTPROTO") == 0)
+            {
+              if (strcmp(variable, "none") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_NONE;
+                }
+              else if (strcmp(variable, "static") == 0)
+                {
+                  ipcfg->proto = BOOTPROTO_STATIC;
+                }
+              else if (strcmp(variable, "dhcp") == 0)

Review comment:
       fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org