#include <stdio.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#if LIBCURL_VERSION_NUM < 0x070900
#error "curl_formadd() is not introduced until libcurl 7.9 and later"
#endif

int main(void)
{
  	CURL *curl;
  	CURLcode res;
  	FILE *file,*mfile,*jar;
  	struct HttpPost* post=NULL;
	struct HttpPost* last=NULL;
	char *san;
	int i;

		file = fopen("./proc.html","w");
		mfile = fopen("./cook.html","w");
		jar = fopen("./jar.html","w");

		san = "simple.c";		//First Attachment

			curl_formadd(&post, &last, CURLFORM_COPYNAME, "m_file", 
									   CURLFORM_FILE, san, 
									   CURLFORM_END);
		 	curl_formadd(&post, &last, CURLFORM_COPYNAME, "attSub", 
									   CURLFORM_COPYCONTENTS, "Attach", 
									   CURLFORM_END);
			printf("FILE:: %s\n",post->contents);	// Printed "simple.c"

  		curl = curl_easy_init();
		
  		if(curl) {
			curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
    		curl_easy_setopt(curl, CURLOPT_URL, "http://www.yyy.com/qqq/formspage.phtml");
			curl_easy_setopt(curl, CURLOPT_FILE, file);
			curl_easy_setopt(curl, CURLOPT_COOKIEFILE, mfile);
			curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "uid=san&pass=san2");
			// Strangely! in the above statement, i dont need to add submit=Submit
			// but it is submitting for me automatically.
			//
    		res = curl_easy_perform(curl);

//			curl_easy_setopt(curl, CURLOPT_HTTPGET, TRUE);
					// it is working fine for me without the above statement!
					// 
			curl_easy_setopt(curl, CURLOPT_COOKIEJAR, jar);

		 	for(i=0;i<2;i++) {
					
		 		curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
		 		res = curl_easy_perform(curl);
				printf("SIZE:: %lu \n",strlen(post->contents));
				
				if(i<1) {
						
					post->contents = "/tmp/FileCache/Default/b/b/9/bb999fc89329270f9d479160b1d67bc00fc15170";
					
					printf("FILE1:: %s\n",post->next->contents); // To check whether the "Attach" has been over
																// written due to the above (looong) statement.
				}	// end of if(i<1)

			}	// end of for(i=0;i<2;i++)
			
			curl_easy_setopt(curl, CURLOPT_POSTFIELDS, 
							"m_tpc=firstAttachment&m_desc=thisIsFirstSUbmission&m_class=1&frmnum=1&stat=1&continue=Continue");
		 	res = curl_easy_perform(curl);
    		curl_easy_cleanup(curl);
//			curl_formfree(post);

  		}	// end of if(curl)
		
		fclose(file);
		fclose(mfile);
  return 0;
}
