static int ftpc_recvbinary(FAR struct ftpc_session_s *session,
                           FAR FILE *rinstream, FAR FILE *loutstream)
{
  ssize_t nread;
  ssize_t nwritten;

  /* Loop until the entire file is received */

  for (;;)
    {
      /* Read the data from the socket */

      nread = fread(session->buffer, sizeof(char), CONFIG_FTP_BUFSIZE, rinstream);
      if (nread <= 0)
        {
          /* nread < 0 is an error */

          if (nread < 0)
            {
              /* errno should already be set by fread */

              (void)ftpc_xfrabort(session, rinstream);
              return ERROR;
            }

          /* nread == 0 means end of file. Return success */

          return OK;
        }

      /* Write the data to the file */

      nwritten = fwrite(session->buffer, sizeof(char), nread, loutstream);
      if (nwritten != nread)
        {
          (void)ftpc_xfrabort(session, loutstream);

          /* If nwritten < 0 errno should already be set by fwrite.
           * What would a short write mean?
           */

         return ERROR;
        }

      /* Increment the size of the file written */

      session->size += nwritten;
    }
}
int ftpc_recvtext(FAR struct ftpc_session_s *session,
                  FAR FILE *rinstream, FAR FILE *loutstream)
{
  int ch;

  /* Read the next character from the incoming data stream */

  while ((ch = fgetc(rinstream)) != EOF)
    {
      /* Is it a carriage return? Compress \r\n to \n */

      if (ch == '\r')
        {
          /* Get the next character */

          ch = fgetc(rinstream);
          if (ch == EOF)
            {
              /* Ooops... */

              (void)ftpc_xfrabort(session, rinstream);
              return ERROR;
            }

          /* If its not a newline, then keep the carriage return */

          if (ch != '\n')
            {
              ungetc(ch, rinstream);
              ch = '\r';
            }
        }

    /* Then write the character to the output file */

    if (fputc(ch, loutstream) == EOF)
      {
        (void)ftpc_xfrabort(session, loutstream);
        return ERROR;
      }

    /* Increase the actual size of the file by one */

    session->size++;
  }

  return OK;
}
Esempio n. 3
0
static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
                           FAR FILE *linstream, FILE *routstream)
{
  ssize_t nread;
  ssize_t nwritten;

  /* Loop until the entire file is sent */

  for (;;)
    {
      /* Read data from the file */

      nread = fread(session->buffer, sizeof(char), CONFIG_FTP_BUFSIZE, linstream);
      if (nread <= 0)
        {
          /* nread == 0 is just EOF */

          if (nread < 0)
            {
              (void)ftpc_xfrabort(session, linstream);
              return ERROR;
            }

          /* Return success */

          return OK;
        }

      /* Send the data */

      nwritten = fwrite(session->buffer, sizeof(char), nread, routstream);
      if (nwritten != nread)
        {
          (void)ftpc_xfrabort(session, routstream);

          /* Return failue */

          return ERROR;
        }

      /* Increment the size of the file sent */

      session->size += nread;
    }
}
Esempio n. 4
0
static int ftpc_sendtext(FAR struct ftpc_session_s *session,
                         FAR FILE *linstream, FAR FILE *routstream)
{
  int ch;
  int ret = OK;

  /* Write characters one at a time. */

  while ((ch = fgetc(linstream)) != EOF)
    {
      /* If it is a newline, send a carriage return too */

      if (ch == '\n')
        {
          if (fputc('\r', routstream) == EOF)
            {
              (void)ftpc_xfrabort(session, routstream);
              ret = ERROR;
              break;
            }

          /* Increment the size of the file sent */

          session->size++;
        }

      /* Send the character */

      if (fputc(ch, routstream) == EOF)
        {
          (void)ftpc_xfrabort(session, routstream);
          ret = ERROR;
          break;
        }

      /* Increment the size of the file sent */

      session->size++;
    }

  return ret;
}