summaryrefslogtreecommitdiff
path: root/src/pinky.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-05-10 14:17:09 +0000
committerJim Meyering <jim@meyering.net>1999-05-10 14:17:09 +0000
commitaf735060cbf853f58663a52dce783887732763b4 (patch)
tree5d7c68ba310a287957fdb64d8c8ee1160c1023d1 /src/pinky.c
parent0ff57399eda272d303866c78daa726bd05175209 (diff)
downloadcoreutils-af735060cbf853f58663a52dce783887732763b4.tar.xz
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.
Diffstat (limited to 'src/pinky.c')
-rw-r--r--src/pinky.c64
1 files changed, 60 insertions, 4 deletions
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');