A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

155 lignes
3.9 KiB

  1. #include "btcli.h"
  2. void
  3. usage_list(void)
  4. {
  5. printf(
  6. "List torrents.\n"
  7. "\n"
  8. "Usage: list [-a] [-i]\n"
  9. " list torrent ...\n"
  10. "\n"
  11. "Arguments:\n"
  12. "torrent ...\n"
  13. "\tThe torrents to list. Running 'btcli list' without any arguments\n"
  14. "\tor options is equivalent to running 'btcli list -ai'.\n"
  15. "\n"
  16. "Options:\n"
  17. "-a\n"
  18. "\tList active torrents.\n"
  19. "\n"
  20. "-i\n"
  21. "\tList inactive torrents.\n"
  22. "\n"
  23. );
  24. exit(1);
  25. }
  26. struct item {
  27. unsigned num;
  28. char *name;
  29. char st;
  30. long long cgot, csize, totup;
  31. BTPDQ_ENTRY(item) entry;
  32. };
  33. struct items {
  34. int count;
  35. char **argv;
  36. int ntps;
  37. struct ipc_torrent *tps;
  38. BTPDQ_HEAD(item_tq, item) hd;
  39. };
  40. void
  41. itm_insert(struct items *itms, struct item *itm)
  42. {
  43. struct item *p;
  44. BTPDQ_FOREACH(p, &itms->hd, entry)
  45. if (strcmp(itm->name, p->name) < 0)
  46. break;
  47. if (p != NULL)
  48. BTPDQ_INSERT_BEFORE(p, itm, entry);
  49. else
  50. BTPDQ_INSERT_TAIL(&itms->hd, itm, entry);
  51. }
  52. static void
  53. list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
  54. {
  55. struct items *itms = arg;
  56. struct item *itm = calloc(1, sizeof(*itm));
  57. if (objerr != IPC_OK)
  58. errx(1, "list failed for '%s' (%s)", itms->argv[obji],
  59. ipc_strerror(objerr));
  60. itms->count++;
  61. itm->num = (unsigned)res[IPC_TVAL_NUM].v.num;
  62. itm->st = tstate_char(res[IPC_TVAL_STATE].v.num);
  63. if (res[IPC_TVAL_NAME].type == IPC_TYPE_ERR)
  64. asprintf(&itm->name, "%s", ipc_strerror(res[IPC_TVAL_NAME].v.num));
  65. else
  66. asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l,
  67. res[IPC_TVAL_NAME].v.str.p);
  68. itm->totup = res[IPC_TVAL_TOTUP].v.num;
  69. itm->cgot = res[IPC_TVAL_CGOT].v.num;
  70. itm->csize = res[IPC_TVAL_CSIZE].v.num;
  71. itm_insert(itms, itm);
  72. }
  73. void
  74. print_items(struct items* itms)
  75. {
  76. struct item *p;
  77. BTPDQ_FOREACH(p, &itms->hd, entry) {
  78. printf("%-40.40s %4u %c. ", p->name, p->num, p->st);
  79. print_percent(p->cgot, p->csize);
  80. print_size(p->csize);
  81. print_ratio(p->totup, p->csize);
  82. printf("\n");
  83. }
  84. }
  85. static struct option list_opts [] = {
  86. { "help", no_argument, NULL, 'H' },
  87. {NULL, 0, NULL, 0}
  88. };
  89. void
  90. cmd_list(int argc, char **argv)
  91. {
  92. int ch, inactive = 0, active = 0;
  93. enum ipc_err code;
  94. enum ipc_twc twc;
  95. enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME,
  96. IPC_TVAL_TOTUP, IPC_TVAL_CSIZE, IPC_TVAL_CGOT };
  97. size_t nkeys = sizeof(keys) / sizeof(keys[0]);
  98. struct items itms;
  99. while ((ch = getopt_long(argc, argv, "ai", list_opts, NULL)) != -1) {
  100. switch (ch) {
  101. case 'a':
  102. active = 1;
  103. break;
  104. case 'i':
  105. inactive = 1;
  106. break;
  107. default:
  108. usage_list();
  109. }
  110. }
  111. argc -= optind;
  112. argv += optind;
  113. if (argc > 0) {
  114. if (inactive || active)
  115. usage_list();
  116. itms.tps = malloc(argc * sizeof(*itms.tps));
  117. for (itms.ntps = 0; itms.ntps < argc; itms.ntps++) {
  118. if (!torrent_spec(argv[itms.ntps], &itms.tps[itms.ntps]))
  119. exit(1);
  120. }
  121. } else {
  122. itms.ntps = 0;
  123. itms.tps = NULL;
  124. }
  125. if (inactive == active)
  126. twc = IPC_TWC_ALL;
  127. else if (inactive)
  128. twc = IPC_TWC_INACTIVE;
  129. else
  130. twc = IPC_TWC_ACTIVE;
  131. btpd_connect();
  132. itms.count = 0;
  133. itms.argv = argv;
  134. BTPDQ_INIT(&itms.hd);
  135. if (itms.tps == NULL)
  136. code = btpd_tget_wc(ipc, twc, keys, nkeys, list_cb, &itms);
  137. else
  138. code = btpd_tget(ipc, itms.tps, itms.ntps, keys, nkeys, list_cb, &itms);
  139. if (code != IPC_OK)
  140. errx(1, "%s", ipc_strerror(code));
  141. printf("%-40.40s NUM ST HAVE SIZE RATIO\n", "NAME");
  142. print_items(&itms);
  143. }