Nowe posty

xx Problem ze sterownikami. (5)
2024-04-13, 21:25:16
xx Instalacja xfce4 (2)
2024-04-13, 16:20:17
xx Serie kompilacji bez instalacji dla “emerge” w Gentoo (2)
2024-04-08, 18:40:04
xx Plasma 6 w Neonie ssie trochę mniej ... (17)
2024-04-05, 10:03:46
xx Problem z Linux Lite po instalacji (3)
2024-04-03, 14:23:40
xx Jak właczyć num locka przy starcie systemu debian 12? (12)
2024-04-02, 17:43:54
xx Brak dźwieku w systemie. (5)
2024-04-02, 16:13:41
xx Dystrybucja pod HP Omen (7)
2024-03-29, 11:33:05
xx [Poradnik] Wyszukiwanie Sterowników (2)
2024-03-27, 21:08:23
xx Ile pingwinów? (1)
2024-03-27, 08:59:24

Autor Wątek: Własny wget problem z http i ftp.  (Przeczytany 2452 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