A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.5 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. "\n"
  10. );
  11. exit(1);
  12. }
  13. struct item {
  14. unsigned num;
  15. char *name;
  16. char st;
  17. BTPDQ_ENTRY(item) entry;
  18. };
  19. struct items {
  20. int count;
  21. BTPDQ_HEAD(item_tq, item) hd;
  22. };
  23. void
  24. itm_insert(struct items *itms, struct item *itm)
  25. {
  26. struct item *p;
  27. BTPDQ_FOREACH(p, &itms->hd, entry)
  28. if (itm->num < p->num)
  29. break;
  30. if (p != NULL)
  31. BTPDQ_INSERT_BEFORE(p, itm, entry);
  32. else
  33. BTPDQ_INSERT_TAIL(&itms->hd, itm, entry);
  34. }
  35. static void
  36. list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
  37. {
  38. struct items *itms = arg;
  39. struct item *itm = calloc(1, sizeof(*itm));
  40. itms->count++;
  41. itm->num = (unsigned)res[IPC_TVAL_NUM].v.num;
  42. itm->st = tstate_char(res[IPC_TVAL_STATE].v.num);
  43. if (res[IPC_TVAL_NAME].type == IPC_TYPE_ERR)
  44. asprintf(&itm->name, "%s", ipc_strerror(res[IPC_TVAL_NAME].v.num));
  45. else
  46. asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l,
  47. res[IPC_TVAL_NAME].v.str.p);
  48. itm_insert(itms, itm);
  49. }
  50. void
  51. print_items(struct items* itms)
  52. {
  53. int n;
  54. struct item *p;
  55. BTPDQ_FOREACH(p, &itms->hd, entry) {
  56. n = printf("%u: ", p->num);
  57. while (n < 7) {
  58. putchar(' ');
  59. n++;
  60. }
  61. printf("%c. %s\n", p->st, p->name);
  62. }
  63. }
  64. static struct option list_opts [] = {
  65. { "help", no_argument, NULL, 'H' },
  66. {NULL, 0, NULL, 0}
  67. };
  68. void
  69. cmd_list(int argc, char **argv)
  70. {
  71. int ch, inactive = 0, active = 0;
  72. enum ipc_err code;
  73. enum ipc_twc twc;
  74. enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME };
  75. struct items itms;
  76. while ((ch = getopt_long(argc, argv, "ai", list_opts, NULL)) != -1) {
  77. switch (ch) {
  78. case 'a':
  79. active = 1;
  80. break;
  81. case 'i':
  82. inactive = 1;
  83. break;
  84. default:
  85. usage_list();
  86. }
  87. }
  88. if (inactive == active)
  89. twc = IPC_TWC_ALL;
  90. else if (inactive)
  91. twc = IPC_TWC_INACTIVE;
  92. else
  93. twc = IPC_TWC_ACTIVE;
  94. btpd_connect();
  95. printf("NUM ST NAME\n");
  96. itms.count = 0;
  97. BTPDQ_INIT(&itms.hd);
  98. if ((code = btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms)) != IPC_OK)
  99. errx(1, "%s", ipc_strerror(code));
  100. print_items(&itms);
  101. printf("Listed %d torrent%s.\n", itms.count, itms.count == 1 ? "" : "s");
  102. }