From af735060cbf853f58663a52dce783887732763b4 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 10 May 1999 14:17:09 +0000 Subject: Expand each `&' character in the gecos field. (count_ampersands): New function. (create_fullname): New function. (print_entry): Use create_fullname here. (print_long_entry): Use it here, too. From Kaveh Ghazi. --- src/pinky.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) (limited to 'src/pinky.c') diff --git a/src/pinky.c b/src/pinky.c index ac15f7e1d..65cec90a4 100644 --- a/src/pinky.c +++ b/src/pinky.c @@ -81,6 +81,56 @@ static struct option const longopts[] = {NULL, 0, NULL, 0} }; +/* Count and return the number of ampersands in STR. */ + +static int +count_ampersands (const char *str) +{ + int count = 0; + do + { + if (*str == '&') + count++; + } while (*str++); + return count; +} + +/* Create a string (via xmalloc) which contains a full name by substituting + for each ampersand in GECOS_NAME the USER_NAME string with its first + character capitalized. The caller must ensure that GECOS_NAME contains + no `,'s. The caller also is responsible for free'ing the return value of + this function. */ + +static char * +create_fullname (const char *gecos_name, const char *user_name) +{ + const int result_len = strlen (gecos_name) + + count_ampersands (gecos_name) * (strlen (user_name) - 1) + 1; + char *const result = xmalloc (result_len); + char *r = result; + + while (*gecos_name) + { + if (*gecos_name == '&') + { + const char *uname = user_name; + if (ISLOWER (*uname)) + *r++ = TOUPPER (*uname++); + while (*uname) + *r++ = *uname++; + } + else + { + *r++ = *gecos_name; + } + + gecos_name++; + } + *r = 0; + + return result; +} + /* Return a string representing the time between WHEN and the time that this function is first run. */ @@ -164,11 +214,14 @@ print_entry (const STRUCT_UTMP *utmp_ent) else { char *const comma = strchr (pw->pw_gecos, ','); + char *result; + if (comma) *comma = '\0'; - /* FIXME: we don't yet convert '&' to username capitalized. */ - printf (" %-19.19s", pw->pw_gecos); + result = create_fullname (pw->pw_gecos, pw->pw_name); + printf (" %-19.19s", result); + free (result); } } @@ -245,11 +298,14 @@ print_long_entry (const char name[]) else { char *const comma = strchr (pw->pw_gecos, ','); + char *result; + if (comma) *comma = '\0'; - /* FIXME: we don't yet convert '&' to username capitalized. */ - printf (" %s", pw->pw_gecos); + result = create_fullname (pw->pw_gecos, pw->pw_name); + printf (" %s", result); + free (result); } putchar ('\n'); -- cgit v1.2.3-54-g00ecf