示例#1
0
文件: pwdb.c 项目: druzac/pwdb
int
main(int argc, char **argv)
{
    struct arguments args;
    int rc;

    rc = -1;
    memset(&args, 0, sizeof(args));
    argp_parse(&argp, argc, argv, 0, 0, &args);
    switch (args.cmd) {
    case CMD_INIT:
        rc = cmd_init(&args);
        break;
    case CMD_LIST:
        rc = cmd_list(&args);
        break;
    case CMD_INSERT:
        rc = cmd_insert(&args);
        break;
    case CMD_RETRIEVE:
        rc = cmd_retrieve(&args);
        break;
    case CMD_KILL:
        rc = cmd_kill(&args);
        break;
    case CMD_INTERACTIVE:
        rc = cmd_interactive(&args);
        break;
    default:
        fprintf(stderr, "invalid command\n");
        goto out;
    }

 out:
    return rc;
}
示例#2
0
文件: cmd.c 项目: ehalls/usbpcap
int __cdecl main(int argc, CHAR **argv)
{
    void *options;
    const char *tmp;
    struct thread_data data;
    HANDLE thread;
    DWORD thread_id;
    BOOL interactive;

    /* Too bad Microsoft compiler does not support C99...
    options = gopt_sort(&argc, argv, gopt_start(
        gopt_option('h', 0, gopt_shorts('h, '?'), gopt_longs("help")),
        gopt_option('d', GOPT_ARG, gopt_shorts('d'), gopt_longs("device")),
        gopt_option('o', GOPT_ARG, gopt_shorts('o'), gopt_longs("output")),
        gopt_option('s', GOPT_ARG, gopt_shorts('s'), gopt_longs("snaplen")),
        gopt_option('b', GOPT_ARG, gopt_shorts('b'), gopt_longs("bufferlen")),
        gopt_option('I', 0, gopt_shorts('I'), gopt_longs("init-non-standard-hwids"))));
    */

    const char *const h_long[] = {"help", NULL};
    const char *const d_long[] = {"device", NULL};
    const char *const o_long[] = {"output", NULL};
    const char *const s_long[] = {"snaplen", NULL};
    const char *const b_long[] = {"bufferlen", NULL};
    const char *const I_long[] = {"init-non-standard-hwids", NULL};
    opt_spec_t opt_specs[] = {
        {'h', 0, "h?", h_long},
        {'d', GOPT_ARG, "d", d_long},
        {'o', GOPT_ARG, "o", o_long},
        {'s', GOPT_ARG, "s", s_long},
        {'b', GOPT_ARG, "b", b_long},
        {'I', 0, "I", I_long},
    };

    options = gopt_sort(&argc, argv, (const void*)opt_specs);


    data.filename = NULL;
    data.device = NULL;
    data.snaplen = 65535;
    data.bufferlen = DEFAULT_INTERNAL_KERNEL_BUFFER_SIZE;

    if (gopt(options, 'h'))
    {
        printf("Usage: USBPcapCMD.exe [options]\n"
               "  -h, -?, --help\n"
               "    Prints this help.\n"
               "  -d <device>, --device <device>\n"
               "    USBPcap control device to open. Example: -d \\\\.\\USBPcap1.\n"
               "  -o <file>, --output <file>\n"
               "    Output .pcap file name.\n"
               "  -s <len>, --snaplen <len>\n"
               "    Sets snapshot length.\n"
               "  -b <len>, --bufferlen <len>\n"
               "    Sets internal capture buffer length. Valid range <4096,134217728>.\n"
               "  -I,  --init-non-standard-hwids\n"
               "    Initializes NonStandardHWIDs registry key used by USBPcapDriver.\n"
               "    This registry key is needed for USB 3.0 capture.\n");
        return 0;
    }

    if (gopt(options, 'I'))
    {
        init_non_standard_roothub_hwid();
        return 0;
    }

    if (gopt_arg(options, 'd', &tmp))
    {
        data.device = _strdup(tmp);
    }

    if (gopt_arg(options, 'o', &tmp))
    {
        data.filename = _strdup(tmp);
    }

    if (gopt_arg(options, 's', &tmp))
    {
        data.snaplen = atol(tmp);
        if (data.snaplen == 0)
        {
            printf("Invalid snapshot length!\n");
            return -1;
        }
    }

    if (gopt_arg(options, 'b', &tmp))
    {
        data.bufferlen = atol(tmp);
        /* Minimum buffer size if 4 KiB, maximum 128 MiB */
        if (data.bufferlen < 4096 || data.bufferlen > 134217728)
        {
            printf("Invalid buffer length! "
                   "Valid range <4096,134217728>.\n");
            return -1;
        }
    }

    if (data.filename != NULL && data.device != NULL)
    {
        interactive = FALSE;
    }
    else
    {
        interactive = TRUE;
        if (data.filename != NULL)
        {
            free(data.filename);
            data.filename = NULL;
        }

        if (data.device != NULL)
        {
            free(data.device);
            data.device = NULL;
        }

        if (cmd_interactive(&data) < 0)
        {
            return 0;
        }
    }

    data.process = TRUE;

    thread = CreateThread(NULL, /* default security attributes */
                          0,    /* use default stack size */
                          read_thread,
                          &data,
                          0,    /* use default creation flag */
                          &thread_id);

    if (thread == NULL)
    {
        if (interactive == TRUE)
        {
            printf("Failed to create thread\n");
        }
    }
    else
    {
        for (;;)
        {
            int c = getchar();
            if (c == (int)'q')
            {
                data.process = FALSE;
                break;
            }
        }

        WaitForSingleObject(thread, INFINITE);
    }

    filters_free();

    if (data.device != NULL)
    {
        free(data.device);
    }

    if (data.filename != NULL)
    {
        free(data.filename);
    }

    return 0;
}