static int pam_conv_func(int nmsg, const struct pam_message** p_msg, struct pam_response** p_reply, void* p_addata) { int i; struct pam_response* p_resps = 0; (void) p_addata; if (nmsg < 0) { bug("dodgy nmsg in pam_conv_func"); } p_resps = vsf_sysutil_malloc(sizeof(struct pam_response) * nmsg); for (i=0; i<nmsg; i++) { switch (p_msg[i]->msg_style) { case PAM_PROMPT_ECHO_OFF: p_resps[i].resp_retcode = PAM_SUCCESS; p_resps[i].resp = (char*) str_strdup(&s_pword_str); break; case PAM_TEXT_INFO: case PAM_ERROR_MSG: p_resps[i].resp_retcode = PAM_SUCCESS; p_resps[i].resp = 0; break; case PAM_PROMPT_ECHO_ON: default: vsf_sysutil_free(p_resps); return PAM_CONV_ERR; break; } } *p_reply = p_resps; return PAM_SUCCESS; }
ship *new_ship(const ship_type * stype, region * r, const struct locale *lang) { static char buffer[32]; ship *sh = (ship *)calloc(1, sizeof(ship)); const char *sname = 0; if (!sh) abort(); assert(stype); sh->no = newcontainerid(); sh->coast = NODIRECTION; sh->type = stype; sh->region = r; if (lang) { sname = LOC(lang, stype->_name); if (!sname) { sname = LOC(lang, parameters[P_SHIP]); } } if (!sname) { sname = parameters[P_SHIP]; } assert(sname); snprintf(buffer, sizeof(buffer), "%s %s", sname, itoa36(sh->no)); sh->name = str_strdup(buffer); shash(sh); if (r) { addlist(&r->ships, sh); } return sh; }
void faction_setemail(faction * self, const char *email) { free(self->email); if (email) self->email = str_strdup(email); else self->email = NULL; }
faction *addfaction(const char *email, const char *password, const struct race * frace, const struct locale * loc) { faction *f = calloc(1, sizeof(faction)); char buf[128]; if (!f) abort(); if (check_email(email) == 0) { faction_setemail(f, email); } else { log_info("Invalid email address for faction %s: %s\n", itoa36(f->no), email?email:""); faction_setemail(f, NULL); } f->alliance_joindate = turn; f->lastorders = 0; f->_alive = true; f->password_id = 0; f->age = 0; f->race = frace; f->magiegebiet = 0; f->locale = loc; f->uid = 0; f->flags = FFL_ISNEW|FFL_PWMSG; if (password) { faction_setpassword(f, password_hash(password, PASSWORD_DEFAULT)); ADDMSG(&f->msgs, msg_message("changepasswd", "value", password)); } f->options = WANT_OPTION(O_REPORT) | WANT_OPTION(O_ZUGVORLAGE) | WANT_OPTION(O_COMPUTER) | WANT_OPTION(O_COMPRESS) | WANT_OPTION(O_ADRESSEN) | WANT_OPTION(O_STATISTICS); f->no = unused_faction_id(); if (rule_region_owners()) { alliance *al = makealliance(f->no, NULL); setalliance(f, al); } addlist(&factions, f); fhash(f); slprintf(buf, sizeof(buf), "%s %s", LOC(loc, "factiondefault"), itoa36(f->no)); f->name = str_strdup(buf); if (!f->race) { log_warning("creating a faction that has no race", itoa36(f->no)); } return f; }
ship_type *st_get_or_create(const char * name) { ship_type * st = st_find_i(name); assert(!snames); if (!st) { st = (ship_type *)calloc(1, sizeof(ship_type)); if (!st) abort(); st->_name = str_strdup(name); st->storm = 1.0; st->tac_bonus = 1.0; st_register(st); } return st; }
/** Specifies automatic alliance modes. * If this returns a value then the bits set are immutable between alliance * partners (faction::alliance) and cannot be changed with the HELP command. */ int AllianceAuto(void) { int value; const char *str = config_get("alliance.auto"); value = 0; if (str != NULL) { char *sstr = str_strdup(str); char *tok = strtok(sstr, " "); while (tok) { value |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } return value & HelpMask(); }
static int AllianceRestricted(void) { static int config, rule; if (config_changed(&config)) { const char *str = config_get("alliance.restricted"); rule = 0; if (str != NULL) { char *sstr = str_strdup(str); char *tok = strtok(sstr, " "); while (tok) { rule |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } rule &= HelpMask(); } return rule; }
static void test_buildingtype(CuTest *tc) { building_type *btype; test_setup(); btype = test_create_buildingtype("hodor"); CuAssertPtrNotNull(tc, btype->stages); CuAssertPtrEquals(tc, NULL, btype->stages->name); CuAssertPtrNotNull(tc, btype->stages->construction); CuAssertStrEquals(tc, "hodor", buildingtype(btype, NULL, 1)); btype->stages->name = str_strdup("castle"); CuAssertStrEquals(tc, "castle", buildingtype(btype, NULL, 1)); btype = bt_get_or_create("portal"); CuAssertPtrEquals(tc, NULL, btype->stages); CuAssertStrEquals(tc, "portal", buildingtype(btype, NULL, 1)); test_teardown(); }
/** Limits the available help modes * The bitfield returned by this function specifies the available help modes * in this game (so you can, for example, disable HELP GIVE globally). * Disabling a status will disable the command sequence entirely (order parsing * uses this function). */ int HelpMask(void) { static int config, rule; if (config_changed(&config)) { const char *str = config_get("rules.help.mask"); if (str != NULL) { char *sstr = str_strdup(str); char *tok = strtok(sstr, " "); rule = 0; while (tok) { rule |= ally_flag(tok, -1); tok = strtok(NULL, " "); } free(sstr); } else { rule = HELP_ALL; } } return rule; }
int str_strarr_add(char ***arr, const char *entry) { int i; for (i = 0; NULL != (*arr)[i]; i++) ; *arr = realloc(*arr, sizeof(char **) * (i + 2)); if (NULL == *arr) { return FAIL; } (*arr)[i] = str_strdup(entry); if (NULL == (*arr)[i]) { return FAIL; } (*arr)[++i] = NULL; return SUCCEED; }
void ship_setname(ship * self, const char *name) { free(self->name); self->name = name ? str_strdup(name) : 0; }
void vsf_parseconf_load_setting(const char* p_setting, int errs_fatal) { static struct mystr s_setting_str; static struct mystr s_value_str; while (vsf_sysutil_isspace(*p_setting)) { p_setting++; } str_alloc_text(&s_setting_str, p_setting); str_split_char(&s_setting_str, &s_value_str, '='); /* Is it a string setting? */ { const struct parseconf_str_setting* p_str_setting = parseconf_str_array; while (p_str_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_str_setting->p_setting_name)) { /* Got it */ const char** p_curr_setting = p_str_setting->p_variable; if (*p_curr_setting) { vsf_sysutil_free((char*) *p_curr_setting); } if (str_isempty(&s_value_str)) { *p_curr_setting = 0; } else { *p_curr_setting = str_strdup(&s_value_str); } return; } p_str_setting++; } } if (str_isempty(&s_value_str)) { if (errs_fatal) { die2("missing value in config file for: ", str_getbuf(&s_setting_str)); } else { return; } } /* Is it a boolean value? */ { const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array; while (p_bool_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_bool_setting->p_setting_name)) { /* Got it */ str_upper(&s_value_str); if (str_equal_text(&s_value_str, "YES") || str_equal_text(&s_value_str, "TRUE") || str_equal_text(&s_value_str, "1")) { *(p_bool_setting->p_variable) = 1; } else if (str_equal_text(&s_value_str, "NO") || str_equal_text(&s_value_str, "FALSE") || str_equal_text(&s_value_str, "0")) { *(p_bool_setting->p_variable) = 0; } else if (errs_fatal) { die2("bad bool value in config file for: ", str_getbuf(&s_setting_str)); } return; } p_bool_setting++; } } /* Is it an unsigned integer setting? */ { const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array; while (p_uint_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_uint_setting->p_setting_name)) { /* Got it */ /* If the value starts with 0, assume it's an octal value */ if (!str_isempty(&s_value_str) && str_get_char_at(&s_value_str, 0) == '0') { *(p_uint_setting->p_variable) = str_octal_to_uint(&s_value_str); } else { /* TODO: we could reject negatives instead of converting them? */ *(p_uint_setting->p_variable) = (unsigned int) str_atoi(&s_value_str); } return; } p_uint_setting++; } } if (errs_fatal) { die2("unrecognised variable in config file: ", str_getbuf(&s_setting_str)); } }
void faction_setname(faction * self, const char *name) { free(self->name); if (name) self->name = str_strdup(name); }
static void handle_config_setting(struct mystr* p_setting_str, struct mystr* p_value_str) { if (str_isempty(p_value_str)) { die("missing value in config file"); } /* Is it a boolean value? */ { const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array; while (p_bool_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_bool_setting->p_setting_name)) { /* Got it */ str_upper(p_value_str); if (str_equal_text(p_value_str, "YES") || str_equal_text(p_value_str, "TRUE") || str_equal_text(p_value_str, "1")) { *(p_bool_setting->p_variable) = 1; } else if (str_equal_text(p_value_str, "NO") || str_equal_text(p_value_str, "FALSE") || str_equal_text(p_value_str, "0")) { *(p_bool_setting->p_variable) = 0; } else { die("bad bool value in config file"); } return; } p_bool_setting++; } } /* Is it an unsigned integer setting? */ { const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array; while (p_uint_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_uint_setting->p_setting_name)) { /* Got it */ /* If the value starts with 0, assume it's an octal value */ if (!str_isempty(p_value_str) && str_get_char_at(p_value_str, 0) == '0') { *(p_uint_setting->p_variable) = str_octal_to_uint(p_value_str); } else { *(p_uint_setting->p_variable) = str_atoi(p_value_str); } return; } p_uint_setting++; } } /* Is it a string setting? */ { const struct parseconf_str_setting* p_str_setting = parseconf_str_array; while (p_str_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_str_setting->p_setting_name)) { /* Got it */ const char** p_curr_setting = p_str_setting->p_variable; if (*p_curr_setting) { vsf_sysutil_free((char*)*p_curr_setting); } *p_curr_setting = str_strdup(p_value_str); return; } p_str_setting++; } } die("unrecognised variable in config file"); }