mirror of
https://github.com/coreutils/coreutils.git
synced 2025-01-11 10:33:49 +08:00
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.
This commit is contained in:
parent
0ff57399ed
commit
af735060cb
64
src/pinky.c
64
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');
|
||||
|
Loading…
Reference in New Issue
Block a user