diff options
| author | Karel Zak <kzak@redhat.com> | 2021-08-05 16:05:58 +0200 |
|---|---|---|
| committer | Karel Zak <kzak@redhat.com> | 2021-08-05 16:06:12 +0200 |
| commit | f60dc888489a0b8dcb24fe64659d3b7e8ebc778f (patch) | |
| tree | 5bbf650985c434ba450ef651f241a13df8d68cbf | |
| parent | 2e03758dc56ccfdec4ca3401136d2701a219b943 (diff) | |
| download | util-linux-f60dc888489a0b8dcb24fe64659d3b7e8ebc778f.tar.gz | |
lib/buffer: retun size of the buffer and data
Signed-off-by: Karel Zak <kzak@redhat.com>
| -rw-r--r-- | include/buffer.h | 3 | ||||
| -rw-r--r-- | lib/buffer.c | 24 | ||||
| -rw-r--r-- | libmount/src/optstr.c | 14 | ||||
| -rw-r--r-- | misc-utils/lsblk.c | 4 |
4 files changed, 28 insertions, 17 deletions
diff --git a/include/buffer.h b/include/buffer.h index 5bc7037f3d..4163e0d752 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -23,6 +23,7 @@ int ul_buffer_append_data(struct ul_buffer *buf, const char *data, size_t sz); int ul_buffer_append_string(struct ul_buffer *buf, const char *str); int ul_buffer_append_ntimes(struct ul_buffer *buf, size_t n, const char *str); int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz); -char *ul_buffer_get_data(struct ul_buffer *buf); +char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz); +size_t ul_buffer_get_bufsiz(struct ul_buffer *buf); #endif /* UTIL_LINUX_BUFFER */ diff --git a/lib/buffer.c b/lib/buffer.c index 26582119c2..80374e5a89 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -119,16 +119,26 @@ int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz) return ul_buffer_append_data(buf, data, sz); } -char *ul_buffer_get_data(struct ul_buffer *buf) +char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz) { + if (sz) + *sz = buf->end - buf->begin; return buf->begin; } +/* size of allocated area (!= size of stored data */ +size_t ul_buffer_get_bufsiz(struct ul_buffer *buf) +{ + return buf->sz; +} + + #ifdef TEST_PROGRAM_BUFFER int main(void) { struct ul_buffer buf = UL_INIT_BUFFER; char *str; + size_t sz = 0; ul_buffer_set_chunksize(&buf, 16); @@ -140,22 +150,22 @@ int main(void) ul_buffer_append_string(&buf, "="); ul_buffer_append_string(&buf, "bbb"); - str = ul_buffer_get_data(&buf); - printf("data '%s'\n", str); + str = ul_buffer_get_data(&buf, &sz); + printf("data [%zu] '%s'\n", sz, str); ul_buffer_reset_data(&buf); ul_buffer_append_string(&buf, "This is really long string to test the buffer function."); ul_buffer_append_string(&buf, " YES!"); - str = ul_buffer_get_data(&buf); - printf("data '%s'\n", str); + str = ul_buffer_get_data(&buf, &sz); + printf("data [%zu] '%s'\n", sz, str); ul_buffer_free_data(&buf); str = strdup("foo"); ul_buffer_refer_string(&buf, str); ul_buffer_append_data(&buf, ",", 1); ul_buffer_append_string(&buf, "bar"); - str = ul_buffer_get_data(&buf); - printf("data '%s'\n", str); + str = ul_buffer_get_data(&buf, &sz); + printf("data [%zu] '%s'\n", sz, str); ul_buffer_free_data(&buf); } diff --git a/libmount/src/optstr.c b/libmount/src/optstr.c index 921b9318e7..d37c7bcd42 100644 --- a/libmount/src/optstr.c +++ b/libmount/src/optstr.c @@ -224,7 +224,7 @@ int mnt_optstr_append_option(char **optstr, const char *name, const char *value) rc = __buffer_append_option(&buf, name, nsz, value, vsz); - *optstr = ul_buffer_get_data(&buf); + *optstr = ul_buffer_get_data(&buf, NULL); return rc; } /** @@ -261,7 +261,7 @@ int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value free(*optstr); } - *optstr = ul_buffer_get_data(&buf); + *optstr = ul_buffer_get_data(&buf, NULL); return rc; } @@ -558,11 +558,11 @@ int mnt_split_optstr(const char *optstr, char **user, char **vfs, } if (vfs) - *vfs = rc ? NULL : ul_buffer_get_data(&xvfs); + *vfs = rc ? NULL : ul_buffer_get_data(&xvfs, NULL); if (fs) - *fs = rc ? NULL : ul_buffer_get_data(&xfs); + *fs = rc ? NULL : ul_buffer_get_data(&xfs, NULL); if (user) - *user = rc ? NULL : ul_buffer_get_data(&xuser); + *user = rc ? NULL : ul_buffer_get_data(&xuser, NULL); if (rc) { ul_buffer_free_data(&xvfs); ul_buffer_free_data(&xfs); @@ -626,7 +626,7 @@ int mnt_optstr_get_options(const char *optstr, char **subset, break; } - *subset = rc ? NULL : ul_buffer_get_data(&buf); + *subset = rc ? NULL : ul_buffer_get_data(&buf, NULL); if (rc) ul_buffer_free_data(&buf); return rc; @@ -834,7 +834,7 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags, goto err; } - *optstr = ul_buffer_get_data(&buf); + *optstr = ul_buffer_get_data(&buf, NULL); } DBG(CXT, ul_debug("new optstr '%s'", *optstr)); diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 754081d077..4a501eda09 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -820,7 +820,7 @@ static char *device_get_data( if (i + 1 < n) ul_buffer_append_data(&buf, "\n", 1); } - str = ul_buffer_get_data(&buf); + str = ul_buffer_get_data(&buf, NULL); break; } case COL_FSROOTS: @@ -838,7 +838,7 @@ static char *device_get_data( if (i + 1 < n) ul_buffer_append_data(&buf, "\n", 1); } - str = ul_buffer_get_data(&buf); + str = ul_buffer_get_data(&buf, NULL); break; } case COL_LABEL: |
