diff --git a/tools/list-sessions.c b/tools/list-sessions.c index 0a026c8..71a0278 100644 --- a/tools/list-sessions.c +++ b/tools/list-sessions.c @@ -149,7 +149,23 @@ get_real_name (uid_t uid) pwent = getpwuid (uid); if (pwent != NULL) { - realname = g_strdup (pwent->pw_gecos); + char *comma = strchr (pwent->pw_gecos, ','); + char *first = comma; + short count = 0; + + /* We look for 3 (or more) commas to know if we have a GECOS field */ + while (comma != NULL && count < 3) { + comma = strchr (comma + 1, ','); + count++; + } + + if (count < 3) { + /* This is not a GECOS field, we keep the whole field */ + realname = g_strdup (pwent->pw_gecos); + } else { + /* This is a GECOS field, we keep up to the first comma */ + realname = g_strndup (pwent->pw_gecos, first - pwent->pw_gecos); + } } return realname;