Skip to content

Commit a0d8100

Browse files
committed
CRT_degreeSign code shrink
Make "CRT_degreeSign" a writable buffer that will be updated after the initDegreeSign() function. This avoids needing a static buffer in the function. Also use MB_LEN_MAX for the buffer size, which is more appropriate than the magic number 4. One side effect of this change is that "CRT_degreeSign" will lose the const qualifier in the header (CRT.h). Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent ab455a2 commit a0d8100

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

CRT.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include <errno.h>
1313
#include <fcntl.h>
1414
#include <langinfo.h>
15+
#include <limits.h>
1516
#include <signal.h>
1617
#include <stdarg.h>
1718
#include <stdio.h>
@@ -94,21 +95,25 @@ const char* const* CRT_treeStr = CRT_treeStrAscii;
9495

9596
static const Settings* CRT_settings;
9697

97-
const char* CRT_degreeSign;
98+
#ifdef HAVE_LIBNCURSESW
99+
char CRT_degreeSign[MB_LEN_MAX] = "\xc2\xb0";
100+
#else
101+
char CRT_degreeSign[] = "";
102+
#endif
98103

99-
static const char* initDegreeSign(void) {
104+
static void initDegreeSign(void) {
100105
#ifdef HAVE_LIBNCURSESW
101106
if (CRT_utf8)
102-
return "\xc2\xb0";
107+
return;
103108

104-
static char buffer[4];
105109
// this might fail if the current locale does not support wide characters
106-
int r = snprintf(buffer, sizeof(buffer), "%lc", 176);
107-
if (r > 0)
108-
return buffer;
110+
int r = snprintf(CRT_degreeSign, sizeof(CRT_degreeSign), "%lc", 176);
111+
if (r <= 0)
112+
CRT_degreeSign[0] = '\0';
109113
#endif
110114

111-
return "";
115+
// No-op
116+
return;
112117
}
113118

114119
const int* CRT_colors;
@@ -1145,7 +1150,7 @@ IGNORE_WCASTQUAL_END
11451150

11461151
CRT_setMouse(settings->enableMouse);
11471152

1148-
CRT_degreeSign = initDegreeSign();
1153+
initDegreeSign();
11491154
}
11501155

11511156
void CRT_done(void) {

CRT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void CRT_handleSIGSEGV(int signal) ATTR_NORETURN;
176176
#define KEY_FOCUS_IN (KEY_MAX + 'I')
177177
#define KEY_FOCUS_OUT (KEY_MAX + 'O')
178178

179-
extern const char* CRT_degreeSign;
179+
extern char CRT_degreeSign[];
180180

181181
#ifdef HAVE_LIBNCURSESW
182182

0 commit comments

Comments
 (0)