[gnome-terminal] util: Add utility to parse /etc/shells



commit 183e5f07b1b5dbfdfadf4a2ddf1f41e6c11f2ba0
Author: Christian Persch <chpe gnome org>
Date:   Tue Jul 14 19:37:28 2015 +0200

    util: Add utility to parse /etc/shells

 src/terminal-util.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/terminal-util.h |    4 +++
 2 files changed, 62 insertions(+), 0 deletions(-)
---
diff --git a/src/terminal-util.c b/src/terminal-util.c
index 3b4ff27..2c4e0ff 100644
--- a/src/terminal-util.c
+++ b/src/terminal-util.c
@@ -718,6 +718,64 @@ terminal_util_get_screen_by_display_name (const char *display_name,
   return screen;
 }
 
+/**
+ * terminal_util_get_etc_shells:
+ *
+ * Returns: (transfer full) the contents of /etc/shells
+ */
+char **
+terminal_util_get_etc_shells (void)
+{
+  GError *err = NULL;
+  gsize len;
+  gs_free char *contents = NULL;
+  char *str, *nl, *end;
+  GPtrArray *arr;
+
+  if (!g_file_get_contents ("/etc/shells", &contents, &len, &err) || len == 0)
+    return NULL;
+
+  arr = g_ptr_array_new ();
+  str = contents;
+  end = contents + len;
+  while (str < end && (nl = strchr (str, '\n')) != NULL) {
+    if (str != nl) /* non-empty? */
+      g_ptr_array_add (arr, g_strndup (str, nl - str));
+    str = nl + 1;
+  }
+  /* Anything non-empty left? */
+  if (str < end && str[0])
+    g_ptr_array_add (arr, g_strdup (str));
+
+  g_ptr_array_add (arr, NULL);
+  return (char **) g_ptr_array_free (arr, FALSE);
+}
+
+/**
+ * terminal_util_get_is_shell:
+ * @command: a string
+ *
+ * Returns wether @command is a valid shell as defined by the contents of /etc/shells.
+ *
+ * Returns: whether @command is a shell
+ */
+gboolean
+terminal_util_get_is_shell (const char *command)
+{
+  gs_strfreev char **shells;
+  guint i;
+
+  shells = terminal_util_get_etc_shells ();
+  if (shells == NULL)
+    return FALSE;
+
+  for (i = 0; shells[i]; i++)
+    if (g_str_equal (command, shells[i]))
+      return TRUE;
+
+  return FALSE;
+}
+
 static gboolean
 s_to_rgba (GVariant *variant,
            gpointer *result,
diff --git a/src/terminal-util.h b/src/terminal-util.h
index 043fb2b..18b033d 100644
--- a/src/terminal-util.h
+++ b/src/terminal-util.h
@@ -68,6 +68,10 @@ void terminal_util_add_proxy_env (GHashTable *env_table);
 GdkScreen *terminal_util_get_screen_by_display_name (const char *display_name,
                                                      int screen_number);
 
+char **terminal_util_get_etc_shells (void);
+
+gboolean terminal_util_get_is_shell (const char *command);
+
 const GdkRGBA *terminal_g_settings_get_rgba (GSettings  *settings,
                                              const char *key,
                                              GdkRGBA    *rgba);


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