Nowe posty

xx Dystrybucja pod HP Omen (6)
Wczoraj o 23:30:08
xx [Poradnik] Wyszukiwanie Sterowników (2)
Wczoraj o 21:08:23
lamp Problem z Linux Lite po instalacji (0)
Wczoraj o 19:50:30
xx Ile pingwinów? (1)
Wczoraj o 08:59:24
xx konfiguracja pale moon (0)
2024-03-24, 21:53:42
xx Plasma 6 w Neonie ssie trochę mniej ... (10)
2024-03-23, 02:38:11
xx problem z instalacja sterowników do karty sieciowej (3)
2024-03-18, 18:10:16
xx Plik abc.001 (1)
2024-03-17, 17:48:27
xx Zlecę dopracowanie programu w MatLab (0)
2024-03-13, 15:28:40
xx Linux Mint 21.3 XFCE brak dźwieku po paru minutach (karta muzyczna zintegrowana) (5)
2024-03-12, 23:07:01

Autor Wątek: Własny wget problem z http i ftp.  (Przeczytany 2445 razy)

megotpelka

  • Gość
Własny wget problem z http i ftp.
« dnia: 2009-12-28, 15:22:01 »
Kod:
#include 
#include
#include
#include
#include
#include


void pomoc()
{
system("clear");
puts("Pomoc dla uzytkownieka");
puts("");
}

void wersja()
{
system("clear");
puts("Wersja oprogramowania");
puts("");
}
 
int test_wejscia()
{
fseek(stdin, 0, SEEK_END) ;

if(ftell(stdin) == -1)
return 1;
else
return 0;
}

void http( char **argv )
{
FILE *plik;


if((plik = fopen(strcat(argv[1],".html"), "w"))==NULL)
puts("Blad przetwarzania pliku");

CURL *curl;
        CURLcode res;
 
  curl = curl_easy_init();
 
if(curl)
{
   curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
   res = curl_easy_perform(curl);

   curl_easy_cleanup(curl);
  }
fclose(plik);
}

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

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream)
{
   out->stream=fopen(out->filename, "wb");
   if(!out->stream)
   return -1;
        }
  return fwrite(buffer, size, nmemb, out->stream);
}

void ftp( char **argv )
{

 

  CURL *curl;
  CURLcode res;
  struct FtpFile ftpfile={"pub",NULL};
 
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,argv[1]);
   
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
   
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
 
   
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
 
    res = curl_easy_perform(curl);
 
    curl_easy_cleanup(curl);
 
    if(CURLE_OK != res) {
      /* we failed */
      fprintf(stderr, "curl told us %d\\n", res);
    }
  }
 
  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */
 
  curl_global_cleanup();
 
}
/**********************************************************************/

int main( int argc, char **argv )
{

if (argc < 2)
{
if(test_wejscia())
{
puts("Niepoprawnie wywolany kod.");
puts("Sprobuj ./kodC -h");
}
}
else if (argc == 2)
{
if( !strcmp(argv[1], "-?") || !strcmp(argv[1], "-help") || !strcmp(argv[1], "-h")) {
pomoc();
return 0;
}
else if( !strcmp(argv[1], "-version") || !strcmp(argv[1], "-v"))
{
wersja();
return 0;
}
else
{
if(!strstr(argv[1],"http://") || !strstr(argv[1],"www")) http(argv);

if(!strstr(argv[1],"ftp://")) ftp(argv);

return 0;
}
}
return 0;
}
Pytania co do http:
1. Jak przerobić kod żeby zawartość zapisywał do pliku ?
Pytania co do ftp:
2. Dlaczego nie działa ?
Korzystam z tutoriala do culr