Add check to ensure a country by that name does not exist.
Ensure the length is not too long. Note this is a change
behaviour for edit and change commands which used to silently
truncate long names. Enforce that a country name can not have
control characters in it. Ensure that a country name is not
blank or just spaces.
extern int influx(struct natstr *np);
extern struct natstr *nat_reset(struct natstr *, enum nat_status, coord, coord);
+extern int check_nat_name(char *cname);
extern int grant_btus(struct natstr *, int );
}
natp = getnatp(coun);
p = getstarg(player->argp[2], "Country Name? ", buf);
- if (p == 0 || *p == 0)
+ if (p ==0)
return RET_SYN;
- if (strlen(p) >= sizeof(cntryname)) {
- pr("Country name too long\n");
+ if (!check_nat_name(p))
return RET_FAIL;
- }
strcpy(cntryname, p);
p = getstarg(player->argp[3], "Representative? ", buf);
if (p == 0 || *p == 0)
#include <config.h>
-#include <ctype.h>
#include "commands.h"
#include "news.h"
#include "optlist.h"
int
chan(void)
{
- char *temp;
- struct natstr *natp;
char *p;
- natid cn;
int charge;
- int nonb;
int btucost;
char buf[1024];
struct natstr *us;
if ((p =
getstarg(player->argp[2], "New country name -- ", buf)) == 0)
return RET_SYN;
- p[sizeof(us->nat_cnam) - 1] = 0;
- for (cn = 0; NULL != (natp = getnatp(cn)); cn++) {
- if (!strcmp(p, natp->nat_cnam)) {
- pr("Country #%d is already called `%s'!\n", cn, p);
- return RET_FAIL;
- }
- }
- nonb = 0;
- for (temp = p; *temp != '\0'; temp++) {
- if (iscntrl(*temp)) {
- pr("No control characters allowed in country names!\n");
- return RET_FAIL;
- } else if (*temp != ' ')
- nonb = 1;
- }
- if (!nonb) {
- pr("Must have a non-blank name!\n");
+ if (!check_nat_name(p))
return RET_FAIL;
- }
player->dolcost += charge;
player->btused += btucost;
strcpy(us->nat_cnam, p);
switch (op) {
case 'n':
+ if (!check_nat_name(p))
+ return RET_SYN;
pr("Country name changed from %s to %s\n", np->nat_cnam, p);
- strncpy(np->nat_cnam, p, sizeof(np->nat_cnam) - 1);
+ strcpy(np->nat_cnam, p);
break;
case 'r':
pr("Country representative changed from %s to %s\n",
#include <config.h>
#include <string.h>
+#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "game.h"
#include "nat.h"
#include "optlist.h"
+#include "prototypes.h"
#include "tel.h"
#include "xy.h"
return natp;
}
+
+int
+check_nat_name(char *cname)
+{
+ struct natstr *natp;
+ natid cn;
+ int nonb;
+ char *temp;
+
+ if (strlen(cname) >= sizeof(natp->nat_cnam)) {
+ pr("Country name too long\n");
+ return 0;
+ }
+
+ nonb = 0;
+ for (temp = cname; *temp != '\0'; temp++) {
+ if (iscntrl(*temp)) {
+ pr("No control characters allowed in country names!\n");
+ return 0;
+ } else if (!isspace(*temp))
+ nonb = 1;
+ }
+ if (!nonb) {
+ pr("Country name can't be all blank\n");
+ return 0;
+ }
+
+ for (cn = 0; NULL != (natp = getnatp(cn)); cn++) {
+ if (!strcmp(cname, natp->nat_cnam)) {
+ pr("Country #%d is already called `%s'\n", cn, cname);
+ return 0;
+ }
+ }
+ return 1;
+}