]> git.pond.sub.org Git - empserver/commitdiff
Add standard checks to the assigning of a country name
authorRon Koenderink <rkoenderink@yahoo.ca>
Tue, 6 Jan 2009 23:22:26 +0000 (17:22 -0600)
committerRon Koenderink <rkoenderink@yahoo.ca>
Tue, 6 Jan 2009 23:22:26 +0000 (17:22 -0600)
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.

include/nat.h
src/lib/commands/add.c
src/lib/commands/chan.c
src/lib/commands/edit.c
src/lib/subs/natsub.c

index 1e15fab5b55e561daedede36ba14ac7751f52cb7..c35401ceb2ddad152248871e9bd51f2ad08e114c 100644 (file)
@@ -177,6 +177,7 @@ extern void agecontact(struct natstr *np);
 extern int influx(struct natstr *np);
 
 extern struct natstr *nat_reset(struct natstr *, enum nat_status, coord, coord);
 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 );
 
 
 extern int grant_btus(struct natstr *, int );
 
index a0add70016cc6f15ee8a0dae49c21c3b42991435..3ba304bb7f96c4cd37fc5687c7ca6fcd7c66edb7 100644 (file)
@@ -84,12 +84,10 @@ add(void)
     }
     natp = getnatp(coun);
     p = getstarg(player->argp[2], "Country Name? ", buf);
     }
     natp = getnatp(coun);
     p = getstarg(player->argp[2], "Country Name? ", buf);
-    if (p == 0 || *p == 0)
+    if (p ==0)
        return RET_SYN;
        return RET_SYN;
-    if (strlen(p) >= sizeof(cntryname)) {
-       pr("Country name too long\n");
+    if (!check_nat_name(p))
        return RET_FAIL;
        return RET_FAIL;
-    }
     strcpy(cntryname, p);
     p = getstarg(player->argp[3], "Representative? ", buf);
     if (p == 0 || *p == 0)
     strcpy(cntryname, p);
     p = getstarg(player->argp[3], "Representative? ", buf);
     if (p == 0 || *p == 0)
index 03da08d01573768d7fb8e39a5b6c0f99cd2fa769..176b4238027424dd4fa6190f8102a1129acb63cf 100644 (file)
@@ -33,7 +33,6 @@
 
 #include <config.h>
 
 
 #include <config.h>
 
-#include <ctype.h>
 #include "commands.h"
 #include "news.h"
 #include "optlist.h"
 #include "commands.h"
 #include "news.h"
 #include "optlist.h"
 int
 chan(void)
 {
 int
 chan(void)
 {
-    char *temp;
-    struct natstr *natp;
     char *p;
     char *p;
-    natid cn;
     int charge;
     int charge;
-    int nonb;
     int btucost;
     char buf[1024];
     struct natstr *us;
     int btucost;
     char buf[1024];
     struct natstr *us;
@@ -84,25 +79,8 @@ chan(void)
        if ((p =
             getstarg(player->argp[2], "New country name -- ", buf)) == 0)
            return RET_SYN;
        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;
            return RET_FAIL;
-       }
        player->dolcost += charge;
        player->btused += btucost;
        strcpy(us->nat_cnam, p);
        player->dolcost += charge;
        player->btused += btucost;
        strcpy(us->nat_cnam, p);
index e28b262c66439d4193ffa894e60787cf4432d8d9..2ad13dac0487ada20f10afd4618644f4af9bef26 100644 (file)
@@ -655,8 +655,10 @@ docountry(char op, int arg, char *p, struct natstr *np)
 
     switch (op) {
     case 'n':
 
     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);
        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",
        break;
     case 'r':
        pr("Country representative changed from %s to %s\n",
index 04c8e8dc980ac135b92231a6b38a6331799855b2..8374ccb9135faecb5d54bdd285dd9673ff805343 100644 (file)
@@ -34,6 +34,7 @@
 #include <config.h>
 
 #include <string.h>
 #include <config.h>
 
 #include <string.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -41,6 +42,7 @@
 #include "game.h"
 #include "nat.h"
 #include "optlist.h"
 #include "game.h"
 #include "nat.h"
 #include "optlist.h"
+#include "prototypes.h"
 #include "tel.h"
 #include "xy.h"
 
 #include "tel.h"
 #include "xy.h"
 
@@ -112,3 +114,38 @@ nat_reset(struct natstr *natp, enum nat_status stat, coord x, coord y)
 
     return natp;
 }
 
     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;
+}