Commit 56509055 authored by Patrick Monnerat's avatar Patrick Monnerat
Browse files

cli tool: use file2memory() to buffer stdin in -F option.

parent 665b3e48
Loading
Loading
Loading
Loading
+14 −37
Changes for src/tool_formparse.c: 14 added lines, 37 removed lines.
Original line number Original line Diff line number Diff line
@@ -32,12 +32,12 @@
#include "tool_convert.h"
#include "tool_convert.h"
#include "tool_msgs.h"
#include "tool_msgs.h"
#include "tool_binmode.h"
#include "tool_binmode.h"
#include "tool_getparam.h"
#include "tool_paramhlp.h"
#include "tool_formparse.h"
#include "tool_formparse.h"


#include "memdebug.h" /* keep this as LAST include */
#include "memdebug.h" /* keep this as LAST include */


#define STDIN_BUFSIZE 0x4000   /* Always have 16K to read more stdin data. */

/* Stdin parameters. */
/* Stdin parameters. */
typedef struct {
typedef struct {
  char *data;  /* Memory data. */
  char *data;  /* Memory data. */
@@ -474,46 +474,23 @@ static CURLcode file_or_stdin(curl_mimepart *part, const char *file)
    if(sip->size < 0)
    if(sip->size < 0)
      sip->size = 0;
      sip->size = 0;
  }
  }
  else {
  else {  /* Not suitable for direct use, buffer stdin data. */
    /* Not suitable for direct use, buffer stdin data. */
    size_t stdinsize = 0;
    sip->origin = 0;
    sip->size = 0;
    sip->curpos = STDIN_BUFSIZE;
    sip->data = malloc(STDIN_BUFSIZE);
    if(!sip->data) {
      stdin_free(sip);
      return CURLE_OUT_OF_MEMORY;
    }
    for(;;) {
      size_t wantbytes = (size_t) (sip->curpos - sip->size);
      size_t havebytes = fread(sip->data + (size_t) sip->size, 1, wantbytes,
                               stdin);
      char *p;


      sip->size += havebytes;
    sip->origin = 0;
      if(ferror(stdin)) {
    if(file2memory(&sip->data, &stdinsize, stdin) != PARAM_OK)
        stdin_free(sip);
      result = CURLE_OUT_OF_MEMORY;
        return CURLE_READ_ERROR;
    else {
      }
      if(!stdinsize)
      if(!havebytes || feof(stdin))
        sip->data = NULL;  /* Has been freed if no data. */
        break;
      sip->size = stdinsize;

      if(ferror(stdin))
      /* Enlarge data buffer. */
        result = CURLE_READ_ERROR;
      p = realloc(sip->data, (size_t) sip->size + STDIN_BUFSIZE);
      if(!p) {
        stdin_free(sip);
        return CURLE_OUT_OF_MEMORY;
      }
      sip->data = p;
      sip->curpos = sip->size + STDIN_BUFSIZE;
    }
    }
    /* Shrink buffer to spare memory resources. */
    if(sip->size < sip->curpos)
      sip->data = realloc(sip->data, (size_t) sip->size);
  }
  }
  sip->curpos = 0;  /* Rewind. */


  /* Set remote file name. */
  /* Set remote file name. */
  if(!result)
    result = curl_mime_filename(part, file);
    result = curl_mime_filename(part, file);


  /* Set part's data from callback. */
  /* Set part's data from callback. */