summary refs log tree commit diff
path: root/cli/stop.c
blob: 031b00e9e88e81d45672e5ca60ba09a9bdea27a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "btcli.h"

void
usage_stop(void)
{
    printf(
        "Deactivate torrents.\n"
        "\n"
        "Usage: stop -a\n"
        "       stop torrent ...\n"
        "\n"
        "Arguments:\n"
        "torrent ...\n"
        "\tThe torrents to deactivate.\n"
        "\n"
        "Options:\n"
        "-a\n"
        "\tDeactivate all active torrents.\n"
        "\n"
        );
    exit(1);
}

static struct option stop_opts [] = {
    { "help", no_argument, NULL, 'H' },
    {NULL, 0, NULL, 0}
};

void
cmd_stop(int argc, char **argv)
{
    int ch, all = 0;
    struct ipc_torrent t;

    while ((ch = getopt_long(argc, argv, "a", stop_opts, NULL)) != -1) {
        switch (ch) {
        case 'a':
            all = 1;
            break;
        default:
            usage_stop();
        }
    }
    argc -= optind;
    argv += optind;

    if ((argc == 0 && !all) || (all && argc != 0))
        usage_stop();

    btpd_connect();
    if (all) {
        enum ipc_err code = btpd_stop_all(ipc);
        if (code != IPC_OK)
            diemsg("command failed (%s).\n", ipc_strerror(code));
    } else {
        for (int i = 0; i < argc; i++)
            if (torrent_spec(argv[i], &t))
                handle_ipc_res(btpd_stop(ipc, &t), "stop", argv[i]);
    }
}