]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/buffer: retun size of the buffer and data
authorKarel Zak <kzak@redhat.com>
Thu, 5 Aug 2021 14:05:58 +0000 (16:05 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 5 Aug 2021 14:06:12 +0000 (16:06 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/buffer.h
lib/buffer.c
libmount/src/optstr.c
misc-utils/lsblk.c

index 5bc7037f3d059dabf6b11bc59e5e8c30fb38ea73..4163e0d752439cf082f24dd8707a7aa4d6c8dcc6 100644 (file)
@@ -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 */
index 26582119c282216e3d35dd2b38128950d4cc80fa..80374e5a89e0584e0b1598d849c4642079df90f1 100644 (file)
@@ -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);
 }
index 921b9318e76fe6b974534f73ded8154499297fd0..d37c7bcd42b95b9adaf7803808dc222293c3c50c 100644 (file)
@@ -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));
index 754081d0777c8eca9e97f6c84589ebc7732e73bc..4a501eda0990112458cd3e754b3e666eeefe0d16 100644 (file)
@@ -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: