A clone of btpd with my configuration changes.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

207 Zeilen
4.3 KiB

  1. #include <stdarg.h>
  2. #include "btcli.h"
  3. const char *btpd_dir;
  4. struct ipc *ipc;
  5. void
  6. diemsg(const char *fmt, ...)
  7. {
  8. va_list ap;
  9. va_start(ap, fmt);
  10. vfprintf(stderr, fmt, ap);
  11. va_end(ap);
  12. exit(1);
  13. }
  14. void
  15. btpd_connect(void)
  16. {
  17. if ((errno = ipc_open(btpd_dir, &ipc)) != 0)
  18. diemsg("cannot open connection to btpd in %s (%s).\n", btpd_dir,
  19. strerror(errno));
  20. }
  21. enum ipc_err
  22. handle_ipc_res(enum ipc_err code, const char *cmd, const char *target)
  23. {
  24. switch (code) {
  25. case IPC_OK:
  26. break;
  27. case IPC_COMMERR:
  28. diemsg("error in communication with btpd.\n");
  29. default:
  30. fprintf(stderr, "btcli %s '%s': %s.\n", cmd, target,
  31. ipc_strerror(code));
  32. }
  33. return code;
  34. }
  35. void
  36. print_percent(long long part, long long whole)
  37. {
  38. printf("%5.1f%% ", floor(1000.0 * part / whole) / 10);
  39. }
  40. void
  41. print_rate(long long rate)
  42. {
  43. if (rate >= 999.995 * (1 << 10))
  44. printf("%6.2fMB/s ", (double)rate / (1 << 20));
  45. else
  46. printf("%6.2fkB/s ", (double)rate / (1 << 10));
  47. }
  48. void
  49. print_size(long long size)
  50. {
  51. if (size >= 999.995 * (1 << 20))
  52. printf("%6.2fG ", (double)size / (1 << 30));
  53. else
  54. printf("%6.2fM ", (double)size / (1 << 20));
  55. }
  56. void
  57. print_ratio(long long part, long long whole)
  58. {
  59. printf("%7.2f ", (double)part / whole);
  60. }
  61. char
  62. tstate_char(enum ipc_tstate ts)
  63. {
  64. switch (ts) {
  65. case IPC_TSTATE_INACTIVE:
  66. return 'I';
  67. case IPC_TSTATE_START:
  68. return '+';
  69. case IPC_TSTATE_STOP:
  70. return '-';
  71. case IPC_TSTATE_LEECH:
  72. return 'L';
  73. case IPC_TSTATE_SEED:
  74. return 'S';
  75. }
  76. diemsg("unrecognized torrent state.\n");
  77. }
  78. int
  79. torrent_spec(char *arg, struct ipc_torrent *tp)
  80. {
  81. char *p;
  82. tp->u.num = strtoul(arg, &p, 10);
  83. if (*p == '\0') {
  84. tp->by_hash = 0;
  85. return 1;
  86. }
  87. if ((p = mi_load(arg, NULL)) == NULL) {
  88. fprintf(stderr, "btcli: bad torrent '%s' (%s).\n", arg,
  89. strerror(errno));
  90. return 0;
  91. }
  92. tp->by_hash = 1;
  93. mi_info_hash(p, tp->u.hash);
  94. free(p);
  95. return 1;
  96. }
  97. static struct {
  98. const char *name;
  99. void (*fun)(int, char **);
  100. void (*help)(void);
  101. } cmd_table[] = {
  102. { "add", cmd_add, usage_add },
  103. { "del", cmd_del, usage_del },
  104. { "kill", cmd_kill, usage_kill },
  105. { "list", cmd_list, usage_list },
  106. { "start", cmd_start, usage_start },
  107. { "stop", cmd_stop, usage_stop },
  108. { "stat", cmd_stat, usage_stat }
  109. };
  110. int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);
  111. static void
  112. usage(void)
  113. {
  114. printf(
  115. "btcli is the btpd command line interface.\n"
  116. "\n"
  117. "Usage: btcli [main options] command [command options]\n"
  118. "\n"
  119. "Main options:\n"
  120. "-d dir\n"
  121. "\tThe btpd directory.\n"
  122. "\n"
  123. "--help [command]\n"
  124. "\tShow this text or help for the specified command.\n"
  125. "\n"
  126. "Commands:\n"
  127. "add\t- Add torrents to btpd.\n"
  128. "del\t- Remove torrents from btpd.\n"
  129. "kill\t- Shut down btpd.\n"
  130. "list\t- List torrents.\n"
  131. "start\t- Activate torrents.\n"
  132. "stat\t- Display stats for active torrents.\n"
  133. "stop\t- Deactivate torrents.\n"
  134. "\n"
  135. "Note:\n"
  136. "Torrents can be specified either with its number or its file.\n"
  137. "\n"
  138. );
  139. exit(1);
  140. }
  141. static struct option base_opts [] = {
  142. { "help", no_argument, NULL, 'H' },
  143. {NULL, 0, NULL, 0}
  144. };
  145. int
  146. main(int argc, char **argv)
  147. {
  148. int ch, help = 0;
  149. if (argc < 2)
  150. usage();
  151. while ((ch = getopt_long(argc, argv, "+d:", base_opts, NULL)) != -1) {
  152. switch (ch) {
  153. case 'd':
  154. btpd_dir = optarg;
  155. break;
  156. case 'H':
  157. help = 1;
  158. break;
  159. default:
  160. usage();
  161. }
  162. }
  163. argc -= optind;
  164. argv += optind;
  165. if (argc == 0)
  166. usage();
  167. if (btpd_dir == NULL)
  168. if ((btpd_dir = find_btpd_dir()) == NULL)
  169. diemsg("cannot find the btpd directory.\n");
  170. optind = 0;
  171. int found = 0;
  172. for (int i = 0; !found && i < ncmds; i++) {
  173. if (strcmp(argv[0], cmd_table[i].name) == 0) {
  174. found = 1;
  175. if (help)
  176. cmd_table[i].help();
  177. else
  178. cmd_table[i].fun(argc, argv);
  179. }
  180. }
  181. if (!found)
  182. usage();
  183. return 0;
  184. }