summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2005-01-23 15:16:47 +0000
committertron <tron@openttd.org>2005-01-23 15:16:47 +0000
commit53d1c3f117142dd3ecae23c5fcdc069f393f1703 (patch)
tree60b41a28cf18bba61bcff993ba254ea2000a083c
parent9a96f5b64e08ec6c8043e4e389f172e8bf5c460d (diff)
downloadopenttd-53d1c3f117142dd3ecae23c5fcdc069f393f1703.tar.xz
(svn r1616) Introduce ttd_strlcat() and use it to de-uglify some piece of code in misc_cmd.
While here rename the len parameter of ttd_strlcpy() to size, because it is a buffer size and not a string length. Also add -Wwrite-strings to the Makefile, because the above mentioned piece of code was the only part which triggered this warning.
-rw-r--r--Makefile1
-rw-r--r--functions.h3
-rw-r--r--misc_cmd.c6
-rw-r--r--ttd.c18
4 files changed, 18 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 9596a3efb..88bcf737f 100644
--- a/Makefile
+++ b/Makefile
@@ -300,6 +300,7 @@ CC_VERSION = $(shell $(CC) -dumpversion | cut -c 1,3)
# this is a workaround to test for >=
ifeq ($(shell if test $(CC_VERSION) -ge 29; then echo true; fi), true)
CFLAGS += -O -Wall -Wno-multichar -Wsign-compare -Wstrict-prototypes
+ CFLAGS += -Wwrite-strings
endif
ifeq ($(shell if test $(CC_VERSION) -ge 30; then echo true; fi), true)
CFLAGS += -W -Wno-unused-parameter
diff --git a/functions.h b/functions.h
index 12e2b9e20..6a5079aa4 100644
--- a/functions.h
+++ b/functions.h
@@ -271,7 +271,8 @@ enum {
};
void ShowSaveLoadDialog(int mode);
-void ttd_strlcpy(char *dst, const char *src, size_t len);
+void ttd_strlcpy(char *dst, const char *src, size_t size);
+void ttd_strlcat(char *dst, const char *src, size_t size);
// callback from drivers that is called if the game size changes dynamically
void GameSizeChanged(void);
diff --git a/misc_cmd.c b/misc_cmd.c
index 6d26c1d5c..865324acc 100644
--- a/misc_cmd.c
+++ b/misc_cmd.c
@@ -158,10 +158,8 @@ int32 CmdChangePresidentName(int x, int y, uint32 flags, uint32 p1, uint32 p2)
DeleteName(old_str);
if (p->name_1 == STR_SV_UNNAMED) {
- byte *s = " Transport";
- byte *d = (byte*)_decode_parameters, b;
- d--; do d++; while (*d);
- do *d++ = b = *s++; while(d != (byte*)endof(_decode_parameters) && b != 0);
+ ttd_strlcat(
+ (char*)_decode_parameters, " Transport", sizeof(_decode_parameters));
DoCommandByTile(0, p1, 0, DC_EXEC, CMD_CHANGE_COMPANY_NAME);
}
MarkWholeScreenDirty();
diff --git a/ttd.c b/ttd.c
index 68b423d1d..70e8e6722 100644
--- a/ttd.c
+++ b/ttd.c
@@ -203,12 +203,20 @@ static const DriverDesc *ChooseDefaultDriver(const DriverDesc *dd)
return best;
}
-void ttd_strlcpy(char *dst, const char *src, size_t len)
+void ttd_strlcpy(char *dst, const char *src, size_t size)
{
- assert(len > 0);
- while (--len && *src)
- *dst++=*src++;
- *dst = 0;
+ assert(size > 0);
+ while (--size > 0 && *src != '\0') *dst++ = *src++;
+ *dst = '\0';
+}
+
+void ttd_strlcat(char *dst, const char *src, size_t size)
+{
+ assert(size > 0);
+ for (; size > 0 && *dst != '\0'; --size, ++dst) {}
+ assert(size > 0);
+ while (--size > 0 && *src != '\0') *dst++ = *src++;
+ *dst = '\0';
}
static char *strecpy(char *dst, const char *src)