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.

list.c 2.9 KiB

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