]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - hack/hack.objnam.c
Remove extra semicolon.
[bsdgames-darwin.git] / hack / hack.objnam.c
index f78579183fcbc84db6fa51c03991ded39d2f515a..eb0421b2ff07b20029e1cea5111f66cdca98c0cc 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: hack.objnam.c,v 1.8 2009/06/07 18:30:39 dholland Exp $ */
+/*     $NetBSD: hack.objnam.c,v 1.11 2011/08/07 06:03:45 dholland Exp $        */
 
 /*
  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: hack.objnam.c,v 1.8 2009/06/07 18:30:39 dholland Exp $");
+__RCSID("$NetBSD: hack.objnam.c,v 1.11 2011/08/07 06:03:45 dholland Exp $");
 #endif                         /* not lint */
 
 #include <stdlib.h>
 #include "hack.h"
 #include "extern.h"
-#define Sprintf (void) sprintf
+#define Snprintf (void) snprintf
 #define Strcat  (void) strcat
 #define        Strcpy  (void) strcpy
 #define        PREFIX  15
 
-char           *
+static char *strprepend(char *, char *);
+static char *sitoa(int);
+
+static char *
 strprepend(char *s, char *pref)
 {
        int             i = strlen(pref);
@@ -87,11 +90,11 @@ strprepend(char *s, char *pref)
        return (s);
 }
 
-char           *
+static char *
 sitoa(int a)
 {
        static char     buf[13];
-       Sprintf(buf, (a < 0) ? "%d" : "+%d", a);
+       Snprintf(buf, sizeof(buf), (a < 0) ? "%d" : "+%d", a);
        return (buf);
 }
 
@@ -99,6 +102,7 @@ char           *
 typename(int otyp)
 {
        static char     buf[BUFSZ];
+       size_t bufpos;
        struct objclass *ocl = &objects[otyp];
        const char     *an = ocl->oc_name;
        const char     *dn = ocl->oc_descr;
@@ -122,26 +126,42 @@ typename(int otyp)
                        Strcpy(buf, an);
                        if (otyp >= TURQUOISE && otyp <= JADE)
                                Strcat(buf, " stone");
-                       if (un)
-                               Sprintf(eos(buf), " called %s", un);
-                       if (dn)
-                               Sprintf(eos(buf), " (%s)", dn);
+                       if (un) {
+                               bufpos = strlen(buf);
+                               Snprintf(buf+bufpos, sizeof(buf)-bufpos,
+                                       " called %s", un);
+                       }
+                       if (dn) {
+                               bufpos = strlen(buf);
+                               Snprintf(buf+bufpos, sizeof(buf)-bufpos,
+                                       " (%s)", dn);
+                       }
                } else {
-                       Strcpy(buf, dn ? dn : an);
-                       if (ocl->oc_olet == GEM_SYM)
-                               Strcat(buf, " gem");
-                       if (un)
-                               Sprintf(eos(buf), " called %s", un);
+                       strlcpy(buf, dn ? dn : an, sizeof(buf));
+                       if (ocl->oc_olet == GEM_SYM) {
+                               strlcat(buf, " gem", sizeof(buf));
+                       }
+                       if (un) {
+                               bufpos = strlen(buf);
+                               Snprintf(buf+bufpos, sizeof(buf)-bufpos,
+                                       " called %s", un);
+                       }
                }
                return (buf);
        }
        /* here for ring/scroll/potion/wand */
-       if (nn)
-               Sprintf(eos(buf), " of %s", an);
-       if (un)
-               Sprintf(eos(buf), " called %s", un);
-       if (dn)
-               Sprintf(eos(buf), " (%s)", dn);
+       if (nn) {
+               bufpos = strlen(buf);
+               Snprintf(buf+bufpos, sizeof(buf)-bufpos, " of %s", an);
+       }
+       if (un) {
+               bufpos = strlen(buf);
+               Snprintf(buf+bufpos, sizeof(buf)-bufpos, " called %s", un);
+       }
+       if (dn) {
+               bufpos = strlen(buf);
+               Snprintf(buf+bufpos, sizeof(buf)-bufpos, " (%s)", dn);
+       }
        return (buf);
 }
 
@@ -149,12 +169,15 @@ char           *
 xname(struct obj *obj)
 {
        static char     bufr[BUFSZ];
+       /* caution: doname() and aobjnam() below "know" these sizes */
        char           *buf = &(bufr[PREFIX]);  /* leave room for "17 -3 " */
+       size_t          bufmax = sizeof(bufr) - PREFIX;
        int             nn = objects[obj->otyp].oc_name_known;
        const char     *an = objects[obj->otyp].oc_name;
        const char     *dn = objects[obj->otyp].oc_descr;
        char           *un = objects[obj->otyp].oc_uname;
        int             pl = (obj->quan != 1);
+
        if (!obj->dknown && !Blind)
                obj->dknown = 1;/* %% doesnt belong here */
        switch (obj->olet) {
@@ -165,10 +188,10 @@ xname(struct obj *obj)
                break;
        case TOOL_SYM:
                if (!nn) {
-                       Strcpy(buf, dn);
+                       strlcpy(buf, dn, bufmax);
                        break;
                }
-               Strcpy(buf, an);
+               strlcpy(buf, an, bufmax);
                break;
        case FOOD_SYM:
                if (obj->otyp == DEAD_HOMUNCULUS && pl) {
@@ -177,7 +200,7 @@ xname(struct obj *obj)
                        break;
                }
                /* fungis ? */
-               /* fall into next case */
+               /* FALLTHROUGH */
        case WEAPON_SYM:
                if (obj->otyp == WORM_TOOTH && pl) {
                        pl = 0;
@@ -189,14 +212,14 @@ xname(struct obj *obj)
                        Strcpy(buf, "crysknives");
                        break;
                }
-               /* fall into next case */
+               /* FALLTHROUGH */
        case ARMOR_SYM:
        case CHAIN_SYM:
        case ROCK_SYM:
-               Strcpy(buf, an);
+               strlcpy(buf, an, bufmax);
                break;
        case BALL_SYM:
-               Sprintf(buf, "%sheavy iron ball",
+               Snprintf(buf, bufmax, "%sheavy iron ball",
                  (obj->owt > objects[obj->otyp].oc_weight) ? "very " : "");
                break;
        case POTION_SYM:
@@ -210,14 +233,14 @@ xname(struct obj *obj)
                                break;
                        if (un) {
                                Strcat(buf, " called ");
-                               Strcat(buf, un);
+                               strlcat(buf, un, bufmax);
                        } else {
                                Strcat(buf, " of ");
-                               Strcat(buf, an);
+                               strlcat(buf, an, bufmax);
                        }
                } else {
-                       Strcpy(buf, dn);
-                       Strcat(buf, " potion");
+                       strlcpy(buf, dn, bufmax);
+                       strlcat(buf, " potion", bufmax);
                }
                break;
        case SCROLL_SYM:
@@ -230,34 +253,34 @@ xname(struct obj *obj)
                        break;
                if (nn) {
                        Strcat(buf, " of ");
-                       Strcat(buf, an);
+                       strlcat(buf, an, bufmax);
                } else if (un) {
                        Strcat(buf, " called ");
-                       Strcat(buf, un);
+                       strlcat(buf, un, bufmax);
                } else {
                        Strcat(buf, " labeled ");
-                       Strcat(buf, dn);
+                       strlcat(buf, dn, bufmax);
                }
                break;
        case WAND_SYM:
                if (!obj->dknown)
-                       Sprintf(buf, "wand");
+                       Snprintf(buf, bufmax, "wand");
                else if (nn)
-                       Sprintf(buf, "wand of %s", an);
+                       Snprintf(buf, bufmax, "wand of %s", an);
                else if (un)
-                       Sprintf(buf, "wand called %s", un);
+                       Snprintf(buf, bufmax, "wand called %s", un);
                else
-                       Sprintf(buf, "%s wand", dn);
+                       Snprintf(buf, bufmax, "%s wand", dn);
                break;
        case RING_SYM:
                if (!obj->dknown)
-                       Sprintf(buf, "ring");
+                       Snprintf(buf, bufmax, "ring");
                else if (nn)
-                       Sprintf(buf, "ring of %s", an);
+                       Snprintf(buf, bufmax, "ring of %s", an);
                else if (un)
-                       Sprintf(buf, "ring called %s", un);
+                       Snprintf(buf, bufmax, "ring called %s", un);
                else
-                       Sprintf(buf, "%s ring", dn);
+                       Snprintf(buf, bufmax, "%s ring", dn);
                break;
        case GEM_SYM:
                if (!obj->dknown) {
@@ -265,15 +288,15 @@ xname(struct obj *obj)
                        break;
                }
                if (!nn) {
-                       Sprintf(buf, "%s gem", dn);
+                       Snprintf(buf, bufmax, "%s gem", dn);
                        break;
                }
-               Strcpy(buf, an);
+               strlcpy(buf, an, bufmax);
                if (obj->otyp >= TURQUOISE && obj->otyp <= JADE)
-                       Strcat(buf, " stone");
+                       strlcat(buf, " stone", bufmax);
                break;
        default:
-               Sprintf(buf, "glorkum %c (0%o) %u %d",
+               Snprintf(buf, bufmax, "glorkum %c (0%o) %u %d",
                        obj->olet, obj->olet, obj->otyp, obj->spe);
        }
        if (pl) {
@@ -294,17 +317,21 @@ xname(struct obj *obj)
                }
                p = eos(buf) - 1;
                if (*p == 's' || *p == 'z' || *p == 'x' ||
-                   (*p == 'h' && p[-1] == 's'))
-                       Strcat(buf, "es");      /* boxes */
-               else if (*p == 'y' && !strchr(vowels, p[-1]))
-                       Strcpy(p, "ies");       /* rubies, zruties */
-               else
-                       Strcat(buf, "s");
+                   (*p == 'h' && p[-1] == 's')) {
+                       /* boxes */
+                       strlcat(buf, "es", bufmax);
+               } else if (*p == 'y' && !strchr(vowels, p[-1])) {
+                       /* rubies, zruties */
+                       *p = '\0';
+                       strlcat(buf, "ies", bufmax);
+               } else {
+                       strlcat(buf, "s", bufmax);
+               }
        }
 nopl:
        if (obj->onamelth) {
-               Strcat(buf, " named ");
-               Strcat(buf, ONAME(obj));
+               strlcat(buf, " named ", bufmax);
+               strlcat(buf, ONAME(obj), bufmax);
        }
        return (buf);
 }
@@ -314,8 +341,13 @@ doname(struct obj *obj)
 {
        char            prefix[PREFIX];
        char           *bp = xname(obj);
+       size_t          bppos, bpmax;
+
+       /* XXX do this better somehow w/o knowing internals of xname() */
+       bpmax = BUFSZ - PREFIX;
+
        if (obj->quan != 1)
-               Sprintf(prefix, "%u ", obj->quan);
+               Snprintf(prefix, sizeof(prefix), "%u ", obj->quan);
        else
                Strcpy(prefix, "a ");
        switch (obj->olet) {
@@ -325,33 +357,35 @@ doname(struct obj *obj)
                break;
        case ARMOR_SYM:
                if (obj->owornmask & W_ARMOR)
-                       Strcat(bp, " (being worn)");
-               /* fall into next case */
+                       strlcat(bp, " (being worn)", bpmax);
+               /* FALLTHROUGH */
        case WEAPON_SYM:
                if (obj->known) {
-                       Strcat(prefix, sitoa(obj->spe));
-                       Strcat(prefix, " ");
+                       strlcat(prefix, sitoa(obj->spe), sizeof(prefix));
+                       strlcat(prefix, " ", sizeof(prefix));
                }
                break;
        case WAND_SYM:
-               if (obj->known)
-                       Sprintf(eos(bp), " (%d)", obj->spe);
+               if (obj->known) {
+                       bppos = strlen(bp);
+                       Snprintf(bp+bppos, bpmax-bppos, " (%d)", obj->spe);
+               }
                break;
        case RING_SYM:
                if (obj->owornmask & W_RINGR)
-                       Strcat(bp, " (on right hand)");
+                       strlcat(bp, " (on right hand)", bpmax);
                if (obj->owornmask & W_RINGL)
-                       Strcat(bp, " (on left hand)");
+                       strlcat(bp, " (on left hand)", bpmax);
                if (obj->known && (objects[obj->otyp].bits & SPEC)) {
-                       Strcat(prefix, sitoa(obj->spe));
-                       Strcat(prefix, " ");
+                       strlcat(prefix, sitoa(obj->spe), sizeof(prefix));
+                       strlcat(prefix, " ", sizeof(prefix));
                }
                break;
        }
        if (obj->owornmask & W_WEP)
-               Strcat(bp, " (weapon in hand)");
+               strlcat(bp, " (weapon in hand)", bpmax);
        if (obj->unpaid)
-               Strcat(bp, " (unpaid)");
+               strlcat(bp, " (unpaid)", bpmax);
        if (!strcmp(prefix, "a ") && strchr(vowels, *bp))
                Strcpy(prefix, "an ");
        bp = strprepend(bp, prefix);
@@ -360,12 +394,12 @@ doname(struct obj *obj)
 
 /* used only in hack.fight.c (thitu) */
 void
-setan(const char *str, char *buf)
+setan(const char *str, char *buf, size_t bufmax)
 {
        if (strchr(vowels, *str))
-               Sprintf(buf, "an %s", str);
+               Snprintf(buf, bufmax, "an %s", str);
        else
-               Sprintf(buf, "a %s", str);
+               Snprintf(buf, bufmax, "a %s", str);
 }
 
 char           *
@@ -373,20 +407,25 @@ aobjnam(struct obj *otmp, const char *verb)
 {
        char           *bp = xname(otmp);
        char            prefix[PREFIX];
+       size_t          bpmax;
+
+       /* XXX do this better somehow w/o knowing internals of xname() */
+       bpmax = BUFSZ - PREFIX;
+
        if (otmp->quan != 1) {
-               Sprintf(prefix, "%u ", otmp->quan);
+               Snprintf(prefix, sizeof(prefix), "%u ", otmp->quan);
                bp = strprepend(bp, prefix);
        }
        if (verb) {
                /* verb is given in plural (i.e., without trailing s) */
-               Strcat(bp, " ");
+               strlcat(bp, " ", bpmax);
                if (otmp->quan != 1)
-                       Strcat(bp, verb);
+                       strlcat(bp, verb, bpmax);
                else if (!strcmp(verb, "are"))
-                       Strcat(bp, "is");
+                       strlcat(bp, "is", bpmax);
                else {
-                       Strcat(bp, verb);
-                       Strcat(bp, "s");
+                       strlcat(bp, verb, bpmax);
+                       strlcat(bp, "s", bpmax);
                }
        }
        return (bp);
@@ -402,8 +441,8 @@ Doname(struct obj *obj)
        return (s);
 }
 
-const char *const wrp[] = {"wand", "ring", "potion", "scroll", "gem"};
-const char wrpsym[] = {WAND_SYM, RING_SYM, POTION_SYM, SCROLL_SYM, GEM_SYM};
+static const char *const wrp[] = {"wand", "ring", "potion", "scroll", "gem"};
+static const char wrpsym[] = {WAND_SYM, RING_SYM, POTION_SYM, SCROLL_SYM, GEM_SYM};
 
 struct obj     *
 readobjnam(char *bp)