示例#1
0
int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  char buffer[IOBUFFERSIZE];
  char *fullpath;
  int fd;
  int i;
  int ret = OK;

  /* Loop for each file name on the command line */

  for (i = 1; i < argc && ret == OK; i++)
    {
      /* Get the fullpath to the file */

      fullpath = nsh_getfullpath(vtbl, argv[i]);
      if (fullpath)
        {
          /* Open the file for reading */

          fd = open(fullpath, O_RDONLY);
          if (fd < 0)
            {
             nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
            }
          else
            {
              /* And just dump it byte for byte into stdout */

              for (;;)
                {
                  int nbytesread = read(fd, buffer, IOBUFFERSIZE);

                  /* Check for read errors */

                  if (nbytesread < 0)
                    {
                      /* EINTR is not an error (but will stop stop the cat) */

#ifndef CONFIG_DISABLE_SIGNALS
                      if (errno == EINTR)
                        {
                          nsh_output(vtbl, g_fmtsignalrecvd, argv[0]);
                        }
                      else
#endif
                        {
                          nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
                        }

                      ret = ERROR;
                      break;
                    }

                  /* Check for data successfully read */

                  else if (nbytesread > 0)
                    {
                      int nbyteswritten = 0;

                      while (nbyteswritten < nbytesread)
                        {
                          ssize_t n = nsh_write(vtbl, buffer, nbytesread);
                          if (n < 0)
                            {
                              /* EINTR is not an error (but will stop stop the cat) */

 #ifndef CONFIG_DISABLE_SIGNALS
                              if (errno == EINTR)
                                {
                                  nsh_output(vtbl, g_fmtsignalrecvd, argv[0]);
                                }
                              else
#endif
                                {
                                  nsh_output(vtbl, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO);
                                }
                              ret = ERROR;
                              break;
                            }
                          else
                            {
                              nbyteswritten += n;
                            }
                        }
                    }

                  /* Otherwise, it is the end of file */

                  else
                    {
                      break;
                    }
                }

              (void)close(fd);
            }

          /* Free the allocated full path */

          nsh_freefullpath(fullpath);
        }
    }
  return ret;
}
示例#2
0
static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
                      FAR const char *filename)
{
  char buffer[IOBUFFERSIZE];
  int fd;
  int ret = OK;

  /* Open the file for reading */

  fd = open(filename, O_RDONLY);
  if (fd < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
      return ERROR;
    }

  /* And just dump it byte for byte into stdout */

  for (;;)
    {
      int nbytesread = read(fd, buffer, IOBUFFERSIZE);

      /* Check for read errors */

      if (nbytesread < 0)
        {
          int errval = errno;

          /* EINTR is not an error (but will stop stop the cat) */

#ifndef CONFIG_DISABLE_SIGNALS
          if (errval == EINTR)
            {
              nsh_output(vtbl, g_fmtsignalrecvd, cmd);
            }
          else
#endif
            {
              nsh_output(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO_OF(errval));
            }

          ret = ERROR;
          break;
        }

      /* Check for data successfully read */

      else if (nbytesread > 0)
        {
          int nbyteswritten = 0;

          while (nbyteswritten < nbytesread)
            {
              ssize_t n = nsh_write(vtbl, buffer, nbytesread);
              if (n < 0)
                {
                  int errval = errno;

                  /* EINTR is not an error (but will stop stop the cat) */

 #ifndef CONFIG_DISABLE_SIGNALS
                  if (errval == EINTR)
                    {
                      nsh_output(vtbl, g_fmtsignalrecvd, cmd);
                    }
                  else
#endif
                    {
                      nsh_output(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO);
                    }

                  ret = ERROR;
                  break;
                }
              else
                {
                  nbyteswritten += n;
                }
            }
        }

      /* Otherwise, it is the end of file */

      else
        {
          break;
        }
    }

   (void)close(fd);
   return ret;
}
示例#3
0
static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
                      FAR const char *filename)
{
  FAR char *buffer;
  int fd;
  int ret = OK;

  /* Open the file for reading */

  fd = open(filename, O_RDONLY);
  if (fd < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
      return ERROR;
    }

  buffer = (FAR char *)malloc(IOBUFFERSIZE);
  if(buffer == NULL)
    {
      nsh_output(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO);
      return ERROR;
    }

  /* And just dump it byte for byte into stdout */

  for (;;)
    {
      int nbytesread = read(fd, buffer, IOBUFFERSIZE);

      /* Check for read errors */

      if (nbytesread < 0)
        {
          int errval = errno;

          /* EINTR is not an error (but will stop stop the cat) */

#ifndef CONFIG_DISABLE_SIGNALS
          if (errval == EINTR)
            {
              nsh_output(vtbl, g_fmtsignalrecvd, cmd);
            }
          else
#endif
            {
              nsh_output(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO_OF(errval));
            }

          ret = ERROR;
          break;
        }

      /* Check for data successfully read */

      else if (nbytesread > 0)
        {
          int nbyteswritten = 0;

          while (nbyteswritten < nbytesread)
            {
              ssize_t n = nsh_write(vtbl, buffer, nbytesread);
              if (n < 0)
                {
                  int errval = errno;

                  /* EINTR is not an error (but will stop stop the cat) */

 #ifndef CONFIG_DISABLE_SIGNALS
                  if (errval == EINTR)
                    {
                      nsh_output(vtbl, g_fmtsignalrecvd, cmd);
                    }
                  else
#endif
                    {
                      nsh_output(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO);
                    }

                  ret = ERROR;
                  break;
                }
              else
                {
                  nbyteswritten += n;
                }
            }
        }

      /* Otherwise, it is the end of file */

      else
        {
          break;
        }
    }

   /* Make sure that the following NSH prompt appears on a new line.  If the
    * file ends in a newline, then this will print an extra blank line
    * before the prompt, but that is preferable to the case where there is
    * no newline and the NSH prompt appears on the same line as the cat'ed
    * file.
    */

   nsh_output(vtbl, "\n");

   /* Close the input file and return the result */

   (void)close(fd);
   free(buffer);
   return ret;
}