/*****************************************************************************
 *                                  _   _ ____  _     
 *  Project                     ___| | | |  _ \| |    
 *                             / __| | | | |_) | |    
 *                            | (__| |_| |  _ <| |___ 
 *                             \___|\___/|_| \_\_____|
 *
 * $Id: telnet.c,v 1.1 2001/03/27 09:00:18 bagder Exp $
 */

#include <stdio.h>

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

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

static CURL *curl;

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
 int fd, bytes;
 static char buflast;
 static int madewrite = 0;

 if ( size*nmemb == 0 )
    return(0);

  printf("%c", *(char *)buffer);
  if ( *(char *)buffer == ' ' && buflast == ':' ) {
    if ( !madewrite ) {
       madewrite=1;
       bytes = write(3,"CogcoG\r\r",strlen("CogcoG\r\r"));
       printf("wrote: %d bytes sock: %d\n",bytes,3);
    } else {
       exit(0);
    }
  }
  buflast = *(char *)buffer;
  /* printf("bytes in: %d\n",size*nmemb); */
  return(size*nmemb);
}

int main(int argc, char **argv)
{
  CURLcode res;
  FILE *hd_src;

  curl = curl_easy_init();

    /* Get curl 7.7 from sunet.se's FTP site: */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "telnet://localhost");

    curl_easy_setopt(curl, CURLOPT_USERPWD, "COG:CogcoG"); 

    curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE); 

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

    res = curl_easy_perform(curl);

    printf("res is: %d\n",res);

    /* always cleanup */
    curl_easy_cleanup(curl);

  /* fclose(ftpfile);  close the local file */

  return 0;
}
