Пример #1
0
int main(int argc, string argv[])
{
    // bad input
    if (!is_input_valid(argc, argv))
    {
        printf(COLOR_RED);
        printf("Usage: nthfib <Fibonacci number (1 - %i)>\n",
            MAX_FIB);
        printf(COLOR_RESET);
        // failure
        return 1;
    }
    // good input
    int n = atoi(argv[1]);
    // add one extra index to more easily count from 1
    long long memoized[n + 1];
    // memoize first two values
    memoized[0] = -1;
    memoized[1] = 0;
    memoized[2] = 1;
    memoized_count = 3;
    long long output = get_nth_fib(n, memoized);
    printf("\nFinal result: Fibonacci Number #%i = %lli \n\n",
        n, output);

    // success
    return 0;
}
Пример #2
0
Файл: Vcs.c Проект: marciof/show
static void Plugin_run(
        Plugin* plugin, Array* inputs, Array* outputs, Error* error) {

    Array argv = {NULL};

    for (size_t i = 0; i < inputs->length; ++i) {
        Input* input = (Input*) inputs->data[i];

        if ((input == NULL) || (input->fd != IO_INVALID_FD)) {
            continue;
        }

        bool is_valid = is_input_valid(input->name, error);

        if (ERROR_HAS(error)) {
            Error_add(error, "`" EXTERNAL_BINARY "` error");
            return;
        }
        if (!is_valid) {
            continue;
        }

        if (argv.data == NULL) {
            init_argv(&argv, &plugin->options, error);

            if (ERROR_HAS(error)) {
                return;
            }
        }

        argv.data[argv.length - 1 - 1] = (intptr_t) input->name;

        int child_pid;
        int fd = fork_exec_fd(
            (char*) argv.data[0], (char**) argv.data, &child_pid, error);

        if (ERROR_HAS(error)) {
            Error_add(error, "`" EXTERNAL_BINARY "` error");
            return;
        }

        input->fd = fd;
        input->arg = child_pid;
        input->close = Input_close_subprocess;
    }

    if (argv.data != NULL) {
        Array_deinit(&argv);
    }
}
Пример #3
0
/*  @brief Creates a wifidirect_service_discovery request/response and
 *         sends to the driver
 *
 *  Usage: "Usage : wifidirect_discovery_request/response [CONFIG_FILE]"
 *
 *  @param argc     Number of arguments
 *  @param argv     Pointer to the arguments
 *  @return         SUCCESS or FAILURE
 **/
void
wifidisplaycmd_service_discovery(int argc, char *argv[])
{
    wifidisplay_discovery_request *req_buf = NULL;
    wifidisplay_discovery_response *resp_buf = NULL;
    char *line = NULL;
    FILE *config_file = NULL;
    int i, opt, li = 0, arg_num = 0, ret = 0, wifidirect_level = 0;
    char *args[30], *pos = NULL, wifidisplay_mac[20], wifidisplay_cmd[32];
    t_u8 dev_address[ETH_ALEN], cmd_found = 0;
    t_u8 *buffer = NULL, *buf = NULL, *tmp_buffer = NULL;
    t_u8 req_resp = 0;          /* req = 0, resp = 1 */
    t_u16 cmd_len = 0, query_len = 0, vendor_len = 0, service_len = 0;
    t_u16 ie_len_wifidisplay = 0;

    strncpy(wifidisplay_cmd, argv[2], sizeof(wifidisplay_cmd) - 1);
    while ((opt = getopt_long(argc, argv, "+", cmd_options, NULL)) != -1) {
        switch (opt) {
        default:
            print_wifidisplay_discovery_usage();
            return;
        }
    }
    argc -= optind;
    argv += optind;

    /* Check arguments */
    if (argc != 3) {
        printf("ERR:Incorrect number of arguments.\n");
        print_wifidisplay_discovery_usage();
        return;
    }

    /* Check if file exists */
    config_file = fopen(argv[2], "r");
    if (config_file == NULL) {
        printf("\nERR:Config file can not open.\n");
        return;
    }
    line = (char *) malloc(MAX_CONFIG_LINE);
    if (!line) {
        printf("ERR:Cannot allocate memory for line\n");
        goto done;
    }
    memset(line, 0, MAX_CONFIG_LINE);
    buf = (t_u8 *) malloc(MRVDRV_SIZE_OF_CMD_BUFFER);
    if (!buf) {
        printf("ERR:Cannot allocate memory!\n");
        goto done;
    }
    if (strcmp(args[0], "wifidisplay_discovery_response") == 0) {
        t_u8 wfd_oui_header = 6;
        wifidisplay_file_params_config(argv[2], NULL, buf, &ie_len_wifidisplay);
        ie_len_wifidisplay += wfd_oui_header;
        buf += wfd_oui_header;
    } else {
        buf[0] = 0x03;
        buf[1] = TLV_TYPE_WIFIDISPLAY_DEVICE_INFO;
        buf[2] = TLV_TYPE_SESSION_INFO_SUBELEM;
        buf[3] = TLV_TYPE_WIFIDISPLAY_COUPLED_SINK;
        ie_len_wifidisplay = 4;
    }

    /* Parse file and process */
    while (config_get_line(line, MAX_CONFIG_LINE, config_file, &li, &pos)) {
        arg_num = parse_line(line, args);
        if (!cmd_found && strncmp(args[0], wifidisplay_cmd, strlen(args[0])))
            continue;
        cmd_found = 1;
        if (strcmp(args[0], "wifidisplay_discovery_request") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_REQUEST_RESPONSE;
            /* For wifidirect_service_discovery, basic initialization here */
            cmd_len = sizeof(wifidisplay_discovery_request);
            buffer = (t_u8 *) malloc(cmd_len);
            if (!buffer) {
                printf("ERR:Cannot allocate memory!\n");
                goto done;
            }
            req_buf = (wifidisplay_discovery_request *) buffer;
            req_buf->cmd_code = HostCmd_CMD_WIFIDIRECT_SERVICE_DISCOVERY;
            req_buf->size = cmd_len;
            req_buf->seq_num = 0;
            req_buf->result = 0;
        } else if (strcmp(args[0], "wifidisplay_discovery_response") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_REQUEST_RESPONSE;
            req_resp = 1;
            /* For wifidirect_service_discovery, basic initialization here */
            cmd_len = sizeof(wifidisplay_discovery_response);
            buffer = (t_u8 *) malloc(cmd_len);
            if (!buffer) {
                printf("ERR:Cannot allocate memory!\n");
                goto done;
            }
            resp_buf = (wifidisplay_discovery_response *) buffer;
            resp_buf->cmd_code = HostCmd_CMD_WIFIDIRECT_SERVICE_DISCOVERY;
            resp_buf->size = cmd_len;
            resp_buf->seq_num = 0;
            resp_buf->result = 0;

        } else if (strcmp(args[0], "PeerAddr") == 0) {
            strncpy(wifidisplay_mac, args[1], 20 - 1);
            if ((ret = mac2raw(wifidisplay_mac, dev_address)) != SUCCESS) {
                printf("ERR: %s Address \n",
                       ret == FAILURE ? "Invalid MAC" : ret ==
                       WIFIDIRECT_RET_MAC_BROADCAST ? "Broadcast" :
                       "Multicast");
                goto done;
            }
            (!req_resp) ? memcpy(req_buf->peer_mac_addr, dev_address,
                                 ETH_ALEN) : memcpy(resp_buf->peer_mac_addr,
                                                    dev_address, ETH_ALEN);

        } else if (strcmp(args[0], "Category") == 0) {
            if (is_input_valid(WIFIDIRECT_CATEGORY, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;

            }
            (!req_resp) ? (req_buf->category = (t_u8) atoi(args[1])) :
                (resp_buf->category = (t_u8) atoi(args[1]));
        } else if (strcmp(args[0], "Action") == 0) {
            if (is_input_valid(WIFIDIRECT_ACTION, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            (!req_resp) ? (req_buf->action = (t_u8) A2HEXDECIMAL(args[1])) :
                (resp_buf->action = (t_u8) A2HEXDECIMAL(args[1]));
        } else if (strcmp(args[0], "DialogToken") == 0) {
            if (is_input_valid(WIFIDIRECT_DIALOGTOKEN, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            (!req_resp) ? (req_buf->dialog_taken = (t_u8) atoi(args[1])) :
                (resp_buf->dialog_taken = (t_u8) atoi(args[1]));
        } else if (strcmp(args[0], "StatusCode") == 0) {
            resp_buf->status_code = (t_u8) atoi(args[1]);
        } else if (strcmp(args[0], "GasComebackDelay") == 0) {
            if (is_input_valid
                (WIFIDIRECT_GAS_COMEBACK_DELAY, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            resp_buf->gas_reply = (t_u16) A2HEXDECIMAL(args[1]);
            resp_buf->gas_reply = cpu_to_le16(resp_buf->gas_reply);
        } else if (strcmp(args[0], "AdvertizementProtocolIE") == 0) {
            if (is_input_valid(WIFIDIRECT_DISC_ADPROTOIE, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            if (!req_resp) {
                for (i = 0; i < arg_num - 1; i++)
                    req_buf->advertize_protocol_ie[i] =
                        (t_u8) A2HEXDECIMAL(args[i + 1]);
            } else {
                for (i = 0; i < arg_num - 1; i++)
                    resp_buf->advertize_protocol_ie[i] =
                        (t_u8) A2HEXDECIMAL(args[i + 1]);
            }
        } else if (strcmp(args[0], "InfoId") == 0) {
            if (is_input_valid(WIFIDIRECT_DISC_INFOID, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            if (!req_resp) {
                for (i = 0; i < arg_num - 1; i++)
                    req_buf->info_id[i] = (t_u8) A2HEXDECIMAL(args[i + 1]);
            } else {
                for (i = 0; i < arg_num - 1; i++)
                    resp_buf->info_id[i] = (t_u8) A2HEXDECIMAL(args[i + 1]);
            }
            query_len += arg_num - 1;
        } else if (strcmp(args[0], "OUI") == 0) {
            if (is_input_valid(WIFIDIRECT_OUI, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            if (!req_resp) {
                for (i = 0; i < arg_num - 1; i++)
                    req_buf->oui[i] = (t_u8) A2HEXDECIMAL(args[i + 1]);
            } else {
                for (i = 0; i < arg_num - 1; i++)
                    resp_buf->oui[i] = (t_u8) A2HEXDECIMAL(args[i + 1]);
            }
            service_len += arg_num - 1;
            query_len += arg_num - 1;
        } else if (strcmp(args[0], "OUISubType") == 0) {
            if (is_input_valid(WIFIDIRECT_OUISUBTYPE, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            (!req_resp) ? (req_buf->oui_sub_type = (t_u8) atoi(args[1])) :
                (resp_buf->oui_sub_type = (t_u8) atoi(args[1]));
            service_len++;
            query_len++;
        } else if (strcmp(args[0], "QueryRequestLen") == 0 ||
                   strcmp(args[0], "QueryResponseLen") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_QUERY;
        } else if (strcmp(args[0], "RequestLen") == 0 ||
                   strcmp(args[0], "ResponseLen") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_SERVICE;
            query_len += 2;
        } else if (strcmp(args[0], "VendorLen") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_VENDOR;
            service_len += 2;
            query_len += 2;
        } else if (strcmp(args[0], "QueryData") == 0 ||
                   strcmp(args[0], "ResponseData") == 0) {
            wifidirect_level = WIFIDIRECT_DISCOVERY_QUERY_RESPONSE_PER_PROTOCOL;
            tmp_buffer = realloc(buffer, cmd_len + ie_len_wifidisplay);
            if (!tmp_buffer) {
                printf("ERR:Cannot add DNS name to buffer!\n");
                goto done;
            } else {
                buffer = tmp_buffer;
                tmp_buffer = NULL;
            }
            if (!req_resp) {
                for (i = 0; i < ie_len_wifidisplay; i++)
                    req_buf->disc_query[i] = (t_u8) buf[i];
            } else {
                resp_buf = (wifidisplay_discovery_response *) buffer;
                for (i = 0; i < ie_len_wifidisplay; i++)
                    buffer[i + cmd_len] = (t_u8) buf[i];
            }
            cmd_len += (ie_len_wifidisplay);
            vendor_len += (ie_len_wifidisplay);
            service_len += (ie_len_wifidisplay);
            query_len += (ie_len_wifidisplay);
        } else if (strcmp(args[0], "ServiceProtocol") == 0) {
            if (!req_resp) {
                req_buf->service_protocol = (t_u8) atoi(args[1]);
                /* 
                 * For uPnP, due to union allocation, a extra byte
                 * is allocated reduce it here for uPnP
                 */
                if (req_buf->service_protocol == 2)
                    cmd_len--;
            } else {
                resp_buf->service_protocol = (t_u8) atoi(args[1]);
                if (resp_buf->service_protocol == 2)
                    cmd_len--;
            }
            vendor_len++;
            service_len++;
            query_len++;
        } else if (strcmp(args[0], "ServiceUpdateIndicator") == 0) {
            if (is_input_valid
                (WIFIDIRECT_SERVICEUPDATE_INDICATOR, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            (!req_resp) ? (req_buf->service_update_indicator =
                           cpu_to_le16((t_u16) atoi(args[1]))) :
                (resp_buf->service_update_indicator =
                 cpu_to_le16((t_u16) atoi(args[1])));
            service_len += 2;
            query_len += 2;
        } else if (strcmp(args[0], "ServiceTransactionId") == 0) {
            if (is_input_valid
                (WIFIDIRECT_DISC_SERVICETRANSACID, arg_num - 1, args + 1)
                != SUCCESS) {
                goto done;
            }
            (!req_resp) ? (req_buf->service_transaction_id =
                           (t_u8) atoi(args[1])) : (resp_buf->
                                                    service_transaction_id =
                                                    (t_u8) atoi(args[1]));
            vendor_len++;
            service_len++;
            query_len++;
        } else if (strcmp(args[0], "}") == 0) {
            switch (wifidirect_level) {
            case WIFIDIRECT_DISCOVERY_QUERY:
                (!req_resp) ? (req_buf->query_len = cpu_to_le16(query_len)) :
                    (resp_buf->query_len = cpu_to_le16(query_len));
                break;
            case WIFIDIRECT_DISCOVERY_SERVICE:
                (!req_resp) ? (req_buf->request_len =
                               cpu_to_le16(service_len)) : (resp_buf->
                                                            response_len =
                                                            cpu_to_le16
                                                            (service_len));
                break;
            case WIFIDIRECT_DISCOVERY_VENDOR:
                (!req_resp) ? (req_buf->vendor_len = cpu_to_le16(vendor_len)) :
                    (resp_buf->vendor_len = cpu_to_le16(vendor_len));
                break;
            default:
                break;
            }
            if (wifidirect_level) {
                if (wifidirect_level == WIFIDIRECT_DISCOVERY_REQUEST_RESPONSE)
                    break;
                wifidirect_level--;
            }
        }
    }
    /* Send collective command */
    wifidirect_ioctl((t_u8 *) buffer, &cmd_len, cmd_len);
  done:
    fclose(config_file);
    if (buffer)
        free(buffer);
    if (line)
        free(line);
}