fetch.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2010 Martin Willi
  3. * Copyright (C) 2010 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <library.h>
  18. #include <utils/debug.h>
  19. static int count = 0;
  20. static bool cb(void *userdata, chunk_t chunk)
  21. {
  22. if (write(1, chunk.ptr, chunk.len) == chunk.len)
  23. {
  24. count++;
  25. return TRUE;
  26. }
  27. return FALSE;
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. chunk_t res;
  32. library_init(NULL, "fetch");
  33. atexit(library_deinit);
  34. lib->plugins->load(lib->plugins, PLUGINS);
  35. if (argc != 3 || (!streq(argv[1], "a") && !streq(argv[1], "s")))
  36. {
  37. fprintf(stderr, "usage: %s a|s <url>\n", argv[0]);
  38. return 1;
  39. }
  40. if (streq(argv[1], "a"))
  41. {
  42. if (lib->fetcher->fetch(lib->fetcher, argv[2], &res,
  43. FETCH_END) == SUCCESS)
  44. {
  45. ignore_result(write(1, res.ptr, res.len));
  46. free(res.ptr);
  47. return 0;
  48. }
  49. }
  50. else
  51. {
  52. if (lib->fetcher->fetch(lib->fetcher, argv[2], NULL,
  53. FETCH_CALLBACK, cb, FETCH_END) == SUCCESS)
  54. {
  55. fprintf(stderr, "received %d chunks\n", count);
  56. return 0;
  57. }
  58. }
  59. return 1;
  60. }