grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC][PATCH v1 1/4] Add grub_env_append function.


From: Denis 'GNUtoo' Carikli
Subject: [RFC][PATCH v1 1/4] Add grub_env_append function.
Date: Sun, 30 Jun 2024 18:25:16 +0200

If the given environment variable doesn't exist, grub_env_append will
have the same effect than grub_env_set. But if the variable do exist,
using grub_env_append will append the given content to the variable
content.

This can be used to build a command that can append data to an
existing variable.

The goal here is to more easily add --set=VARNAME arguments to current
commands like it is done in the probe command for instance.

This is because in the code of some commands (like ls) GRUB start
printing information directly to the output instead of building a big
string and only printing the information when done building it.

And so having something like grub_env_append that is closer to this
behavior helps adding --set=VARNAME to various commands (like ls).

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
---
 grub-core/kern/env.c | 38 ++++++++++++++++++++++++++++++++++++++
 include/grub/env.h   |  1 +
 2 files changed, 39 insertions(+)

diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
index 764068896..24ba42bb8 100644
--- a/grub-core/kern/env.c
+++ b/grub-core/kern/env.c
@@ -129,6 +129,44 @@ grub_env_set (const char *name, const char *val)
   return grub_errno;
 }
 
+grub_err_t
+grub_env_append (const char *name, const char *val)
+{
+  struct grub_env_var *var;
+
+  /* If the variable does already exist, append val to the variable content.  
*/
+  var = grub_env_find (name);
+  if (var)
+    {
+      char *old = var->value;
+      char *new;
+
+      new = grub_zalloc (grub_strlen(old) + grub_strlen(val) + 1);
+      if (!new)
+        return grub_errno;
+
+      grub_strcpy (new, old);
+      grub_strcpy (new + grub_strlen(new), val);
+
+      if (var->write_hook)
+        var->value = var->write_hook (var, new);
+      else
+        var->value = grub_strdup (new);
+
+      if (! var->value)
+        {
+          var->value = old;
+          grub_free (new);
+          return grub_errno;
+        }
+
+      grub_free (old);
+      return GRUB_ERR_NONE;
+    }
+
+  return grub_env_set (name, val);
+}
+
 const char *
 grub_env_get (const char *name)
 {
diff --git a/include/grub/env.h b/include/grub/env.h
index 6b9379a30..e62786006 100644
--- a/include/grub/env.h
+++ b/include/grub/env.h
@@ -44,6 +44,7 @@ struct grub_env_var
 };
 
 grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char *val);
+grub_err_t EXPORT_FUNC(grub_env_append) (const char *name, const char *val);
 const char *EXPORT_FUNC(grub_env_get) (const char *name);
 bool EXPORT_FUNC(grub_env_get_bool) (const char *name, bool if_unset);
 void EXPORT_FUNC(grub_env_unset) (const char *name);
-- 
2.45.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]