{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. */
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);
}
}
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');