ret_t cherokee_downloader_async_init (cherokee_downloader_async_t *adownloader) { ret_t ret; ret = cherokee_downloader_init (DOWNLOADER(adownloader)); if (ret != ret_ok) return ret; adownloader->fd_added = -1; adownloader->fdpoll_ref = NULL; return ret_ok; }
int main (int argc, char **argv) { int re; ret_t ret; cint_t val; cint_t param_num; cint_t long_index; cherokee_downloader_t *downloader; cherokee_buffer_t proxy = CHEROKEE_BUF_INIT; cuint_t proxy_port; struct option long_options[] = { /* Options without arguments */ {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"quiet", no_argument, NULL, 'q'}, {"save-headers", no_argument, NULL, 's'}, {"header", required_argument, NULL, 0 }, {NULL, 0, NULL, 0} }; /* Parse known parameters */ while ((val = getopt_long (argc, argv, "VshqO:", long_options, &long_index)) != -1) { switch (val) { case 'V': printf ("Cherokee Downloader %s\n" "Written by Alvaro Lopez Ortega <*****@*****.**>\n\n" "Copyright (C) 2001-2014 Alvaro Lopez Ortega.\n" "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", PACKAGE_VERSION); return EXIT_OK; case 'O': if (global_fd != UNSET_FD) { close (global_fd); } if ((strlen(optarg) == 1) && (optarg[0] == '-')) { global_fd = fileno(stdout); } else { global_fd = open (optarg, O_WRONLY | O_CREAT, 0644); } if (global_fd < 0) { PRINT_MSG ("ERROR: Can not open %s\n", optarg); return EXIT_ERROR; } break; case 0: break; case 'q': quiet = true; break; case 's': save_headers = true; break; case 'h': case '?': default: print_help(); return EXIT_OK; } } /* The rest.. */ param_num = argc - optind; if (param_num <= 0) { print_usage(); return EXIT_OK; } /* Tracing and proxy discovering.. */ cherokee_init(); cget_find_proxy (&proxy, &proxy_port); for (val=optind; val<optind+param_num; val++) { cherokee_buffer_t url = CHEROKEE_BUF_INIT; /* Build the url buffer */ ret = cherokee_buffer_add_va (&url, "%s", argv[val]); if (ret != ret_ok) exit (EXIT_ERROR); /* Create the downloader object.. */ ret = cherokee_downloader_new (&downloader); if (ret != ret_ok) exit (EXIT_ERROR); ret = cherokee_downloader_init(downloader); if (ret != ret_ok) exit (EXIT_ERROR); if (! cherokee_buffer_is_empty (&proxy)) { ret = cherokee_downloader_set_proxy (downloader, &proxy, proxy_port); if (ret != ret_ok) exit (EXIT_ERROR); } ret = cherokee_downloader_set_url (downloader, &url); if (ret != ret_ok) exit (EXIT_ERROR); ret = cherokee_downloader_connect (downloader); if (ret != ret_ok) exit (EXIT_ERROR); /* Download it! */ ret = do_download (downloader); if ((ret != ret_ok) && (ret != ret_eof)) { exit (EXIT_ERROR); } /* Free the objects.. */ cherokee_buffer_mrproper (&url); cherokee_downloader_free (downloader); } /* Close the output file */ re = close (output_fd); if (re != 0) exit (EXIT_ERROR); cherokee_mrproper(); return EXIT_OK; }