Location * form_location_select(char *input) { int id; Location *location; Client *client; location = location_malloc(); do { printf("Digite [1] para pesquisar por ID ou [2] para pesquisar por cliente: "); read_string(input); switch (*input) { case '1': // Verifica se é um ID válido id = check_by_id_location(input); if (id == NON_EXIST) { location->id = NON_EXIST; return location; } // Procura o location pelo ID location = search_location_by_id(id); *input = '1'; break; case '2': // Pesquisa todo as locações relacionados ao cliente informado printf("\n=> Pesquisando cliente <=\n"); client = form_client_select(input); // Verifica se é um filme válido if (client->id == NON_EXIST) { free(client); location->id = NON_EXIST; return location; } // Lista todos as locações com o nome do cliente pesquisado. puts_all_locations_by_client(client); free(client); // Pede para o usuário escolher por ID agora. do { printf("> Qual deles? Informe o valor da coluna ID: "); read_string(input); } while (!validate_id(input)); id = atoi(input); // Procura o location pelo ID agora location = search_location_by_id(id); *input = '2'; break; default: printf("Opcao invalida!\n"); } // Caso não ache, retorna com ID = NON_EXIST if (location->id == NON_EXIST) { if (*input == '1') printf(ID_NOT_FOUND_ERROR, __FILE__, "locacao"); else if (*input == '2') printf(NAME_NOT_FOUND_ERROR, __FILE__, "locacao"); location->id = NON_EXIST; return location; } } while (*input != '1' && *input != '2'); return location; }
int cmd_renew(int argc, char **argv) { long long ts = renew_timeout(); off_t offset = 0; int opt, fd, r; optind = 0; while ((opt = getopt(argc, argv, "+hdr:o:t:")) != -1) { switch (opt) { case 'h': usage(); break; case 'd': debug++; break; case 'r': request = optarg; break; case 'o': offset = strtoul(optarg, 0, 0); break; case 't': ts = strtoll(optarg, 0, 0); break; } } if (argc - optind < 4) usage(); path = argv[optind++]; validate_path(path); id = argv[optind++]; validate_id(id); lease_ms = strtoul(argv[optind++], 0, 0); op_max_ms = strtoul(argv[optind++], 0, 0); validate_lease_params(lease_ms, op_max_ms); DEBUG("path '%s' offset %ld id '%s' lease_ms %ld op_max_ms %ld", path, offset, id, lease_ms, op_max_ms); if ((fd = open(path, O_RDWR | O_DIRECT)) < 0) panic("can't open '%s'", path); r = renew(fd, offset, id, &ts); close(fd); /* print out the last successful renewal timestamp, or zero for don't renew */ printf("%lld\n", ts); if (r == 1) { DEBUG("Succeeded"); return 0; } DEBUG("%s (%s)", "Failed", strerror(r)); return 1; }
int cmd_acquire(int argc, char **argv) { int opt, fd, r, b = 0; off_t offset = 0; long long ts; optind = 0; while ((opt = getopt(argc, argv, "+hdr:bo:")) != -1) { switch (opt) { case 'h': usage(); break; case 'd': debug++; break; case 'r': request = optarg; break; case 'b': b = 1; break; case 'o': offset = strtoul(optarg, 0, 0); break; } } if (argc - optind < 4) usage(); path = argv[optind++]; validate_path(path); id = argv[optind++]; validate_id(id); lease_ms = strtoul(argv[optind++], 0, 0); op_max_ms = strtoul(argv[optind++], 0, 0); validate_lease_params(lease_ms, op_max_ms); DEBUG("path '%s' offset %ld id '%s' lease_ms %ld op_max_ms %ld", path, offset, id, lease_ms, op_max_ms); if ((fd = open(path, O_RDWR | O_DIRECT)) < 0) panic("can't open '%s'", path); r = acquire(fd, offset, id, b, &ts); close(fd); if (r == 1) { /* print last successful timestamp == aquire time */ printf("%lld", ts); DEBUG("Succeeded"); return 0; } else DEBUG("%s (%s)", "Failed", strerror(r)); return 1; }
static int8_t do_query(char *query, char *address) { uint32_t ip; if(!inet_pton(AF_INET, address, &ip)) { return ERROR_INTERNAL; } char *name = strstr(query, "name="), *id = strstr(query, "id="), *c; uint32_t len; uint8_t name_length; if(name && id) { name += 5; id += 3; c = name; while(*c != '&'&& *c) { if(*c >= 'A' && *c <= 'Z') { *c = *c - 'A' + 'a'; c++; continue; } if(!((*c >= 'a' && *c <= 'z') || (*c >= '0' && *c <= '9'))) { return INVALID_CHARS_NAME; } c++; } len = c - name; if(len >= MAX_NAME_LENGTH) { len = MAX_NAME_LENGTH; } name_length = len; c = id; while(*c != '&'&& *c){c++;} len = c - id; if(len != TOX_ID_SIZE * 2) { return INVALID_LENGTH; } uint8_t _id[TOX_ID_SIZE]; if(!string_to_id(_id, (uint8_t*)id)) { return INVALID_CHARS; } if(!validate_id(_id)) { return INVALID_CHECKSUM; } return database_write(_id, (uint8_t*)name, name_length, ip); } else { return 1; } }
static ngx_int_t nchan_process_legacy_channel_id(ngx_http_request_t *r, nchan_loc_conf_t *cf, ngx_str_t **ret_id) { static ngx_str_t channel_id_var_name = ngx_string("push_channel_id"); ngx_uint_t key = ngx_hash_key(channel_id_var_name.data, channel_id_var_name.len); ngx_http_variable_value_t *vv = NULL; ngx_str_t *group = &cf->channel_group; ngx_str_t tmpid; ngx_str_t *id; size_t sz; u_char *cur; nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, nchan_module); ctx->channel_id_count = 0; vv = ngx_http_get_variable(r, &channel_id_var_name, key); if (vv == NULL || vv->not_found || vv->len == 0) { //ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "nchan: the legacy $push_channel_id variable is not set"); return NGX_ABORT; } else { tmpid.len = vv->len; tmpid.data = vv->data; } if(validate_id(r, &tmpid, cf) != NGX_OK) { *ret_id = NULL; return NGX_DECLINED; } sz = group->len + 1 + tmpid.len; if((id = ngx_palloc(r->pool, sizeof(*id) + sz)) == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "nchan: can't allocate space for legacy channel id"); *ret_id = NULL; return NGX_ERROR; } id->len = sz; id->data = (u_char *)&id[1]; cur = id->data; ngx_memcpy(cur, group->data, group->len); cur += group->len; cur[0]='/'; cur++; ngx_memcpy(cur, tmpid.data, tmpid.len); ctx->channel_id_count = 1; ctx->channel_id[0] = *id; *ret_id = id; return NGX_OK; }
int cmd_release(int argc, char **argv) { int opt, fd, r; int force = 0; off_t offset = 0; optind = 0; while ((opt = getopt(argc, argv, "+hdfo:")) != -1) { switch (opt) { case 'h': usage(); break; case 'd': debug++; break; case 'f': force++; break; case 'o': offset = strtoul(optarg, 0, 0); break; } } if (argc - optind < 2) usage(); path = argv[optind++]; validate_path(path); id = argv[optind++]; validate_id(id); DEBUG("path '%s' offset %ld id '%s' force %d", path, offset, id, force); if ((fd = open(path, O_RDWR | O_DIRECT)) < 0) panic("can't open '%s'", path); r = release(fd, offset, id, force); close(fd); if (r == 1) { DEBUG("Succeeded"); return 0; } else DEBUG("%s (%s)", "Failed", strerror(r)); return 1; }
int check_by_id_location(char *input) { int id; do { printf("Qual ID? "); read_string(input); } while (!validate_id(input)); id = atoi(input); // Verificar se o ID existe if (id > 0 && location_index_exist(id)) { return id; } else { printf(ID_NOT_FOUND_ERROR, __FILE__, "filme"); return NON_EXIST; } }
static ngx_int_t nchan_process_multi_channel_id(ngx_http_request_t *r, nchan_chid_loc_conf_t *idcf, nchan_loc_conf_t *cf, ngx_str_t **ret_id) { ngx_int_t i, n; ngx_str_t id[NCHAN_MEMSTORE_MULTI_MAX]; ngx_str_t *id_out; ngx_str_t *group = &cf->channel_group; size_t sz = 0, grouplen = group->len; u_char *cur; static ngx_str_t empty_string = ngx_string(""); nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, nchan_module); n = idcf->n; if(n>1) { sz += 3 + n; //space for null-separators and "m/<SEP>" prefix for multi-chid } for(i=0; i < n; i++) { ngx_http_complex_value(r, idcf->id[i], &id[i]); if(validate_id(r, &id[i], cf) != NGX_OK) { *ret_id = NULL; return NGX_DECLINED; } sz += id[i].len; sz += 1 + grouplen; // "group/" ctx->channel_id[i] = id[i]; } if(ctx) { ctx->channel_id_count = i; for(; i < NCHAN_MULTITAG_MAX; i++) { ctx->channel_id[i] = empty_string; } } if((id_out = ngx_palloc(r->pool, sizeof(*id_out) + sz)) == NULL) { ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "nchan: can't allocate space for channel id"); *ret_id = NULL; return NGX_ERROR; } id_out->len = sz; id_out->data = (u_char *)&id_out[1]; cur = id_out->data; if(n > 1) { cur[0]='m'; cur[1]='/'; cur[2]=NCHAN_MULTI_SEP_CHR; cur+=3; } for(i = 0; i < n; i++) { ngx_memcpy(cur, group->data, grouplen); cur += grouplen; cur[0] = '/'; cur++; ngx_memcpy(cur, id[i].data, id[i].len); cur += id[i].len; if(n>1) { cur[0] = NCHAN_MULTI_SEP_CHR; cur++; } } *ret_id = id_out; return NGX_OK; }
static ngx_int_t nchan_process_multi_channel_id(ngx_http_request_t *r, nchan_complex_value_arr_t *idcf, nchan_loc_conf_t *cf, ngx_str_t **ret_id) { ngx_int_t i, n = idcf->n, n_out = 0; ngx_str_t id[NCHAN_MULTITAG_MAX]; ngx_str_t *id_out; ngx_str_t *group = &cf->channel_group; size_t sz = 0, grouplen = group->len; u_char *cur; //static ngx_str_t empty_string = ngx_string(""); nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, nchan_module); for(i=0; i < n && n_out < NCHAN_MULTITAG_MAX; i++) { ngx_http_complex_value(r, idcf->cv[i], &id[n_out]); if(validate_id(r, &id[n_out], cf) != NGX_OK) { *ret_id = NULL; return NGX_DECLINED; } if(cf->channel_id_split_delimiter.len > 0) { ngx_str_t *delim = &cf->channel_id_split_delimiter; u_char *cur_last, *last; cur = id[n_out].data; last = cur + id[n_out].len; u_char *cur_first = cur; while ((cur_last = nchan_strsplit(&cur, delim, last)) != NULL) { id[n_out].data = cur_first; id[n_out].len = cur_last - cur_first; cur_first = cur; sz += id[n_out].len + 1 + grouplen; // "group/<channel-id>" if(n_out < NCHAN_MULTITAG_REQUEST_CTX_MAX) { ctx->channel_id[n_out] = id[n_out]; } n_out++; } } else { sz += id[n_out].len + 1 + grouplen; // "group/<channel-id>" if(n_out < NCHAN_MULTITAG_REQUEST_CTX_MAX) { ctx->channel_id[n_out] = id[n_out]; } n_out++; } } if(n_out>1) { sz += 3 + n_out; //space for null-separators and "m/<SEP>" prefix for multi-chid } if(ctx) { ctx->channel_id_count = n_out; //for(; i < NCHAN_MULTITAG_REQUEST_CTX_MAX; i++) { // ctx->channel_id[i] = empty_string; //} } if((id_out = ngx_palloc(r->pool, sizeof(*id_out) + sz)) == NULL) { ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "nchan: can't allocate space for channel id"); *ret_id = NULL; return NGX_ERROR; } id_out->len = sz; id_out->data = (u_char *)&id_out[1]; cur = id_out->data; if(n_out > 1) { cur[0]='m'; cur[1]='/'; cur[2]=NCHAN_MULTI_SEP_CHR; cur+=3; } for(i = 0; i < n_out; i++) { ngx_memcpy(cur, group->data, grouplen); cur += grouplen; cur[0] = '/'; cur++; ngx_memcpy(cur, id[i].data, id[i].len); cur += id[i].len; if(n_out>1) { cur[0] = NCHAN_MULTI_SEP_CHR; cur++; } } *ret_id = id_out; return NGX_OK; }