Skip to content

Commit

Permalink
Revert to snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
orestisfl committed May 8, 2024
1 parent 6efb409 commit b1f0b60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/i3status.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void print_separator(const char *separator);
char *color(const char *colorstr);
char *endcolor() __attribute__((pure));
void reset_cursor(void);
char *maybe_escape_markup(char *text);
void maybe_escape_markup(char *text, char *buffer, size_t size);

char *rtrim(const char *s);
char *ltrim(const char *s);
Expand Down
36 changes: 17 additions & 19 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,49 +88,47 @@ void reset_cursor(void) {
* https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142
*
*/
char *maybe_escape_markup(char *text) {
void maybe_escape_markup(char *text, char *buffer, size_t size) {
size--; /* Leave a byte for NUL termination. */

if (markup_format == M_NONE) {
return strdup(text);
*buffer += snprintf(buffer, size, "%s", text);
return;
}

size_t idx = 0;
size_t size = 32;
char *buffer = malloc(size);
for (; *text != '\0'; text++) {
if (idx + 10 > size) {
size *= 2;
buffer = realloc(buffer, size);
for (size_t i = 0; *text != '\0'; text++) {
if (i >= size) {
/* trim buffer and leave */
return;
}
switch (*text) {
case '&':
idx += sprintf(&buffer[idx], "%s", "&");
i += snprintf(&buffer[i], size - i, "%s", "&");
break;
case '<':
idx += sprintf(&buffer[idx], "%s", "&lt;");
i += snprintf(&buffer[i], size - i, "%s", "&lt;");
break;
case '>':
idx += sprintf(&buffer[idx], "%s", "&gt;");
i += snprintf(&buffer[i], size - i, "%s", "&gt;");
break;
case '\'':
idx += sprintf(&buffer[idx], "%s", "&apos;");
i += snprintf(&buffer[i], size - i, "%s", "&apos;");
break;
case '"':
idx += sprintf(&buffer[idx], "%s", "&quot;");
i += snprintf(&buffer[i], size - i, "%s", "&quot;");
break;
default:
if ((0x1 <= *text && *text <= 0x8) ||
(0xb <= *text && *text <= 0xc) ||
(0xe <= *text && *text <= 0x1f)) {
idx += sprintf(&buffer[idx], "&#x%x;", *text);
i += snprintf(&buffer[i], size - i, "&#x%x;", *text);
} else {
buffer[idx] = *text;
idx++;
buffer[i] = *text;
i++;
}
break;
}
}
buffer[idx] = 0;
return buffer;
}

/*
Expand Down
13 changes: 6 additions & 7 deletions src/print_wireless_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

#include "i3status.h"

#define STRING_SIZE 30
#define STRING_SIZE 60

#define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
#define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
Expand Down Expand Up @@ -569,6 +569,7 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
char string_quality[STRING_SIZE] = {'\0'};
char string_signal[STRING_SIZE] = {'\0'};
char string_noise[STRING_SIZE] = {'\0'};
char string_essid[STRING_SIZE] = {'\0'};
char string_frequency[STRING_SIZE] = {'\0'};
char string_ip[STRING_SIZE] = {'\0'};
char string_bitrate[STRING_SIZE] = {'\0'};
Expand Down Expand Up @@ -600,13 +601,12 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
snprintf(string_noise, STRING_SIZE, "?");
}

char *string_essid_tmp = NULL; /* Dynamic allocation of ESSID */
char *string_essid = "?";
#ifdef IW_ESSID_MAX_SIZE
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) {
string_essid = string_essid_tmp = maybe_escape_markup(info.essid);
}
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
maybe_escape_markup(info.essid, string_essid, STRING_SIZE);
else
#endif
snprintf(string_essid, STRING_SIZE, "?");

if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9);
Expand All @@ -632,7 +632,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
char *formatted = format_placeholders(walk, &placeholders[0], num);
OUTPUT_FORMATTED;
free(formatted);
free(string_essid_tmp);

END_COLOR;
free(ipv4_address);
Expand Down

0 comments on commit b1f0b60

Please sign in to comment.