/*****************************************************************************/

 #include <windows.h>
 #include <curl.h>
 
 
#define REMOTE_URL      "ftp://192.168.0.201"

#define FIND_CURRENT_DIR   "e:\\test\\*.*"
#define CURRENT_DIR        "e:\\test"

struct FtpFile {
  char *filename;
  FILE *stream;
};


static int MyDebug (CURL *curl,
					curl_infotype infotype,
					char *data,
					size_t datalen,
					void *clientp)
{
	char           *ptr = data;

	if (strncmp (data, "213 ", 4) == 0) {
		ptr += 4;
        // Get the file size here
        // .........
	}

	return 0;
}

/******************************************************************************/
/*            Write function: It is not used in this example                  */
/******************************************************************************/
int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  return (int)fwrite(buffer, size, nmemb, out->stream);
}



/******************************************************************************/
/*                      Get the size of the remote file                       */
/******************************************************************************/
static int CheckUpload (char *file_name, char * remote_directory, FtpFile* ftpfile)
{
	struct curl_slist *headerlist = NULL;
	char              cmd[_MAX_FNAME + 10], error[100];
	CURLcode          res;
    CURL *curl;

    curl = curl_easy_init();
    if(curl)
    {
        curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
        curl_easy_setopt (curl, CURLOPT_USERPWD, "myuser:mypass");

        //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        //curl_easy_setopt(curl, CURLOPT_FILE, ftpfile);

        sprintf (cmd, "CWD %s", remote_directory);
        headerlist = curl_slist_append (headerlist, cmd);

	    sprintf (cmd, "SIZE %s", file_name);
	    headerlist = curl_slist_append (headerlist, cmd);

	    curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE);
	    curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, MyDebug);

	    curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
        curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, error);

	    res = curl_easy_perform (curl);

	    curl_slist_free_all (headerlist);
	    curl_easy_setopt(curl, CURLOPT_QUOTE, NULL);

        curl_easy_cleanup(curl);
        return res;
   }
   return -1;
}

/******************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

  HANDLE   hFind = NULL;

  struct FtpFile ftpfile={
        "c:\\curl.txt",
        NULL};

  curl_global_init(CURL_GLOBAL_ALL);

  WIN32_FIND_DATA FindData;

  hFind = FindFirstFile (FIND_CURRENT_DIR, &FindData);
  if (hFind == INVALID_HANDLE_VALUE)  return 0;

  do {
	   if (strcmp (FindData.cFileName, ".")  &&
           strcmp (FindData.cFileName, ".."))
       {   if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
           continue;

            CheckUpload (FindData.cFileName, "folder1/folder2/folder3", &ftpfile);
       }
  } while (FindNextFile (hFind, &FindData));

  FindClose (hFind);

  curl_global_cleanup();
  return 0;
}

