Browse Source

clish_get__format_username()

Serj Kalichev 3 years ago
parent
commit
9b1dde752d
4 changed files with 37 additions and 28 deletions
  1. 1 0
      clish/shell.h
  2. 33 0
      clish/shell/shell_misc.c
  3. 1 19
      clish/shell/shell_var.c
  4. 2 9
      plugins/clish/hook_log.c

+ 1 - 0
clish/shell.h

@@ -192,6 +192,7 @@ int clish_shell_wdog(clish_shell_t *instance);
 int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
 int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
 void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
+char *clish_shell_format_username(const clish_shell_t *instance);
 
 /* Plugin functions */
 clish_plugin_t * clish_shell_create_plugin(clish_shell_t *instance,

+ 33 - 0
clish/shell/shell_misc.c

@@ -4,6 +4,10 @@
 
 #include <stdlib.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <string.h>
 
 #include "private.h"
 
@@ -41,3 +45,32 @@ void clish_shell_set_human_interface(clish_shell_t *shell)
 	if (shell->tinyrl)
 		tinyrl_set_human_interface(shell->tinyrl);
 }
+
+
+/* Get user name.
+ * Return value must be freed after using.
+ */
+char *clish_shell_format_username(const clish_shell_t *shell)
+{
+	char *uname = NULL;
+
+	/* Try to get username from environment variables
+	 * USER and LOGNAME and then from /etc/passwd. Else use UID.
+	 */
+	if (!(uname = getenv("USER"))) {
+		if (!(uname = getenv("LOGNAME"))) {
+			struct passwd *user = NULL;
+			user = clish_shell__get_user(shell);
+			if (user) {
+				uname = user->pw_name;
+			} else {
+				char tmp[10] = {};
+				snprintf(tmp, sizeof(tmp), "%u", getuid());
+				tmp[sizeof(tmp) - 1] = '\0';
+				uname = tmp;
+			}
+		}
+	}
+
+	return strdup(uname);
+}

+ 1 - 19
clish/shell/shell_var.c

@@ -119,25 +119,7 @@ static char *find_context_var(const char *name, clish_context_t *this)
 		result = strdup(tmp);
 
 	} else if (!lub_string_nocasecmp(name, "_user")) {
-		char *uname = NULL;
-		/* Try to get username from environment variables
-		 * USER and LOGNAME and then from /etc/passwd. Else use UID.
-		 */
-		if (!(uname = getenv("USER"))) {
-			if (!(uname = getenv("LOGNAME"))) {
-				struct passwd *user = NULL;
-				user = clish_shell__get_user(shell);
-				if (user) {
-					uname = user->pw_name;
-				} else {
-					char tmp[10] = {};
-					snprintf(tmp, sizeof(tmp), "%u", getuid());
-					tmp[sizeof(tmp) - 1] = '\0';
-					uname = tmp;
-				}
-			}
-		}
-		result = strdup(uname);
+		result = clish_shell_format_username(shell);
 
 	/* The vars dependent on command. Code supposes this->cmd is valid.
 	 * Don't put command-independent variables after that line.

+ 2 - 9
plugins/clish/hook_log.c

@@ -28,17 +28,10 @@ CLISH_HOOK_LOG(clish_hook_log)
 		return 0;
 	}
 
-	/* Log the given line */
-	/* Try to get username from environment variables
-	 * USER and LOGNAME and then from /etc/passwd.
-	 */
-	user = clish_shell__get_user(this);
-	if (!(uname = getenv("USER"))) {
-		if (!(uname = getenv("LOGNAME")))
-			uname = user ? user->pw_name : "unknown";
-	}
+	uname = clish_shell_format_username(this);
 	syslog(LOG_INFO, "%u(%s) %s : %d",
 		user ? user->pw_uid : getuid(), uname, line, retcode);
+	free(uname);
 
 	return 0;
 }