void parse_turn (int argc, char * arg1, char * arg2, gameType * game) { /* Decide on an action to take with player input: make a move, save a file or exit */ int a, b; char * fname; /* Save */ if (argc == 1) { /* Check filename given */ /*if (strlen(arg1) == 1) { sysMessage(9, game); }*/ /* Strip out first character for filename & save */ fname = (char *) malloc(strlen(arg1)); fname = strncpy(fname, &arg1[1], strlen(arg1) - 1); fname[strlen(arg1)] = '\0'; game_save(fname, game); free(fname); } /* Place piece */ else if (argc == 2) { /* Check positive integers given */ string_strip_nondigit(arg2); if (!( string_is_numeric(arg1) && string_is_numeric(arg2) )) { return; } /* Check integers did not overflow */ a = atoi(arg1); b = atoi(arg2); if ((a == INT_MAX) || (b == INT_MAX) || (a < 0) || (b < 0)) { return; } /* Perform the move */ player_try_move(a, b, game); } }
void parse_setup (int argc, char * argv[], gameType * game) { /* Process arguments from main() for starting a game */ int a, b = 0, c = 0, i; /* Strip non-integer characters after the last argument */ if (argc == 4) { string_strip_nondigit(argv[3]); } else if (argc == 5) { string_strip_nondigit(argv[4]); } /* Check arguments are positive integers */ for (i = 2; i < argc; i++) { if (!string_is_numeric(argv[i])) { if (i == 2) { sysMessage(5, game); } else { sysMessage(6, game); } } } /* get parameters */ if (argc >= 4) { b = atoi(argv[3]); if (argc == 5) { c = atoi(argv[4]); } } a = atoi(argv[2]); if (a <= 3) { /* Boardsize is invalid */ sysMessage(5, game); } else if ( (b > 2) || (c > 2) || (b < 0) || (c < 0) ) { /* Player selection is invalid */ sysMessage(6, game); } else { /* Valid game start condition! */ board_ini(&game->board, a); board_ini(&game->validMove, a); game->pTypeX = b; game->pTypeO = c; play(game); } }
int skill_lookup(char *skill) { if (string_is_numeric(skill)) { int d; sscanf(skill, "%i", &d); return d; } else { int i; for (i = 0; i < n_skills; i++) { if (string_compare((char *) skills[i], skill, FALSE)) { return i; } } return -1; } }
static int watched_accounts_parser(struct nv_pair *nv, int line, prelude_conf_t *config) { char *str = (char *)nv->value; do { char *ptr = strchr(str, '-'); if (ptr) { char *user1, *user2; int start, end, i; user1 = str; *ptr = 0; user2 = ptr+1; if (string_is_numeric(user1)) { start = strtoul(user1, NULL, 10); } else { struct passwd *pw; pw = getpwnam(user1); if (pw == NULL) { syslog(LOG_ERR, "user %s is invalid - line %d, skipping", user1, line); continue; } start = pw->pw_uid; } i = strlen(user2); if (i>0 && user2[i-1] == ',') user2[i-1] = 0; if (string_is_numeric(user2)) { end = strtoul(user2, NULL, 10); } else { struct passwd *pw; pw = getpwnam(user2); if (pw == NULL) { syslog(LOG_ERR, "user %s is invalid - line %d, skipping", user2, line); continue; } end = pw->pw_uid; } if (start >= end) { syslog(LOG_ERR, "%s is larger or equal to %s, please fix, skipping", user1, user2); continue; } for (i=start; i<=end; i++) { ilist_add_if_uniq( &config->watched_accounts, i); } } else { int acct; if (string_is_numeric(str)) acct = strtoul(str, NULL, 10); else { struct passwd *pw; pw = getpwnam(str); if (pw == NULL) { syslog(LOG_ERR, "user %s is invalid - line %d, skipping", str, line); continue; } acct = pw->pw_uid; } ilist_add_if_uniq(&config->watched_accounts, acct); } str = strtok(NULL, ", "); } while(str); return 0; }