summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Gerwitz <mike@mikegerwitz.com>2013-12-11 10:46:33 -0500
committerAmadeusz Sławiński <amade@asmblr.net>2015-06-26 15:17:18 +0200
commit78dc153b6111b487cc51e924868cce8e8b87c742 (patch)
tree19721de459b7d512296e7b80ea2e8f099d2b8a1b /src
parentf32ca1040b689dc105655d100d2cc4e738ee9351 (diff)
downloadscreen-78dc153b6111b487cc51e924868cce8e8b87c742.tar.gz
Added wmbc_str{,n}cpy and replaced existing refactored strncpy's
All remaining calls in MakeWinMsgEv will be replaced as they are refactored into their own functions. Until then, they will continue to work as before, but will not expand the buffer.
Diffstat (limited to 'src')
-rw-r--r--src/winmsg.c33
-rw-r--r--src/winmsgbuf.c44
-rw-r--r--src/winmsgbuf.h6
3 files changed, 54 insertions, 29 deletions
diff --git a/src/winmsg.c b/src/winmsg.c
index 1a90c71..743a5ad 100644
--- a/src/winmsg.c
+++ b/src/winmsg.c
@@ -326,15 +326,8 @@ winmsg_esc_ex(Focus, Window *win, Event *ev)
winmsg_esc(HostName)
{
- size_t max = wmbc_bytesleft(wmbc);
-
- *wmbc->p = '\0';
- if (strlen(HostName) < max) {
- strncpy(wmbc->p, HostName, max);
- if (*wmbc->p)
- wmc_set(cond);
- }
- wmbc_fastfw(wmbc);
+ if (*wmbc_strcpy(wmbc, HostName))
+ wmc_set(cond);
return s;
}
@@ -376,16 +369,10 @@ winmsg_esc(Rend)
winmsg_esc(SessName)
{
char *session_name = strchr(SocketName, '.') + 1;
- size_t max = wmbc_bytesleft(wmbc);
- *wmbc->p = '\0';
- if (strlen(session_name) < max) {
- strncpy(wmbc->p, session_name, max);
- if (*wmbc->p)
- wmc_set(cond);
- }
+ if (*wmbc_strcpy(wmbc, session_name))
+ wmc_set(cond);
- wmbc_fastfw(wmbc);
return s;
}
@@ -440,16 +427,12 @@ winmsg_esc_ex(WinArgv, Window *win)
winmsg_esc_ex(WinTitle, Window *win)
{
- size_t max = wmbc_bytesleft(wmbc);
+ if (!win)
+ return s;
- *wmbc->p = '\0';
- if (win && strlen(win->w_title) < max) {
- strncpy(wmbc->p, win->w_title, max);
- if (*wmbc->p)
- wmc_set(cond);
- }
+ if (*wmbc_strcpy(wmbc, win->w_title))
+ wmc_set(cond);
- wmbc_fastfw(wmbc);
return s;
}
diff --git a/src/winmsgbuf.c b/src/winmsgbuf.c
index 5c45829..43fd8c6 100644
--- a/src/winmsgbuf.c
+++ b/src/winmsgbuf.c
@@ -126,13 +126,53 @@ inline void wmbc_fastfw_end(WinMsgBufContext *wmbc)
}
/* Sets a character at the current buffer position and increments the pointer.
- * The terminating null character is not retained. */
+ * The terminating null character is not retained. The buffer will be
+ * dynamically resized as needed. */
inline void wmbc_putchar(WinMsgBufContext *wmbc, char c)
{
- assert(wmbc);
+ /* attempt to accomodate this character, but bail out silenty if it cannot
+ * fit */
+ if (!wmbc_bytesleft(wmbc)) {
+ size_t size = wmbc->buf->size + 1;
+ if (wmb_expand(wmbc->buf, size) < size) {
+ return;
+ }
+ }
+
*wmbc->p++ = c;
}
+/* Copies a string into the buffer, dynamically resizing the buffer as needed to
+ * accomodate length N. If S is shorter than N characters in length, the
+ * remaining bytes are filled will nulls. The pointer is adjusted to the last
+ * character before the first terminating null byte. */
+inline char *wmbc_strncpy(WinMsgBufContext *wmbc, const char *s, size_t n)
+{
+ size_t l = wmbc_bytesleft(wmbc);
+
+ /* silently fail in the event that we cannot accomodate */
+ if (l < n) {
+ size_t size = wmbc->buf->size + (n - l);
+ if (wmb_expand(wmbc->buf, size) < size) {
+ return wmbc->p;
+ }
+ }
+
+ char *p = wmbc->p;
+ strncpy(wmbc->p, s, n);
+ wmbc_fastfw(wmbc);
+ return p;
+}
+
+/* Copies a string into the buffer, dynamically resizing the buffer as needed to
+ * accomodate the length of the string plus its terminating null byte. The
+ * pointer is adjusted to the last character before the terminiating null byte.
+ * */
+inline char *wmbc_strcpy(WinMsgBufContext *wmbc, const char *s)
+{
+ return wmbc_strncpy(wmbc, s, strlen(s) + 1);
+}
+
/* Write data to the buffer using a printf-style format string. If needed, the
* buffer will be automatically expanded to accomodate the resulting string and
* is therefore protected against overflows. */
diff --git a/src/winmsgbuf.h b/src/winmsgbuf.h
index c1be733..5649598 100644
--- a/src/winmsgbuf.h
+++ b/src/winmsgbuf.h
@@ -55,8 +55,10 @@ WinMsgBufContext *wmbc_create(WinMsgBuf *);
inline void wmbc_fastfw(WinMsgBufContext *);
inline void wmbc_fastfw0(WinMsgBufContext *);
inline void wmbc_putchar(WinMsgBufContext *, char);
- int wmbc_printf(WinMsgBufContext *wmbc, const char *fmt, ...)
- __attribute__((format(printf,2,3)));
+inline char *wmbc_strncpy(WinMsgBufContext *wmbc, const char *s, size_t n);
+inline char *wmbc_strcpy(WinMsgBufContext *wmbc, const char *s);
+int wmbc_printf(WinMsgBufContext *wmbc, const char *fmt, ...)
+ __attribute__((format(printf,2,3)));
inline size_t wmbc_bytesleft(WinMsgBufContext *wmbc);
void wmbc_free(WinMsgBufContext *);