All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] chfn: add --trim option to remove trailing commas
@ 2016-07-26 18:06 Sami Kerola
  2016-07-26 18:06 ` [PATCH 2/2] chfn: stop adding trailing commas to gecos Sami Kerola
  0 siblings, 1 reply; 3+ messages in thread
From: Sami Kerola @ 2016-07-26 18:06 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

When running chfn(1) will add empty 'other' field, that causes trailing
commas to persist.

-- snip
$ chfn
Changing finger information for kerolasa.
Password:
Office []: foo
Office Phone []:
Home Phone []:
Finger information changed.
$ grep Sami /etc/passwd
kerolasa:x:1000:1000:Sami Kerola,foo,,,:/home/kerolasa:/bin/bash
-- snip

New --trim option will allow user to remove such pointless commas.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/chfn.1 |  5 +++++
 login-utils/chfn.c | 11 ++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/login-utils/chfn.1 b/login-utils/chfn.1
index 0cb5f28..8fb5e4b 100644
--- a/login-utils/chfn.1
+++ b/login-utils/chfn.1
@@ -64,6 +64,11 @@ Specify your office phone number.
 .BR \-h , " \-\-home\-phone " \fIhome-phone
 Specify your home phone number.
 .TP
+.B \-\-trim
+Remove trailing commas from gecos.  This option will remove empty fields
+also in the case they are not managed with
+.BR chfn (1).
+.TP
 .BR \-u , " \-\-help"
 Display help text and exit.
 .TP
diff --git a/login-utils/chfn.c b/login-utils/chfn.c
index 89e6bd7..2d58fc3 100644
--- a/login-utils/chfn.c
+++ b/login-utils/chfn.c
@@ -77,6 +77,7 @@ struct chfn_control {
 		allow_room:1,		   /* see: man login.defs(5) */
 		allow_work:1,		   /* and look for CHFN_RESTRICT */
 		allow_home:1,		   /* keyword for these four. */
+		trim:1,			/* remove trailing commas from gecos */
 		changed:1,		/* is change requested */
 		interactive:1;		/* whether to prompt for fields or not */
 };
@@ -97,6 +98,7 @@ static void __attribute__((__noreturn__)) usage(FILE *fp)
 	fputs(_(" -o, --office <office>        office number\n"), fp);
 	fputs(_(" -p, --office-phone <phone>   office phone number\n"), fp);
 	fputs(_(" -h, --home-phone <phone>     home phone number\n"), fp);
+	fputs(_("     --trim                   remove trailing commas\n"), fp);
 	fputs(USAGE_SEPARATOR, fp);
 	fputs(_(" -u, --help     display this help and exit\n"), fp);
 	fputs(_(" -v, --version  output version information and exit\n"), fp);
@@ -131,12 +133,16 @@ static int check_gecos_string(const char *msg, char *gecos)
  */
 static void parse_argv(struct chfn_control *ctl, int argc, char **argv)
 {
+	enum {
+		OPT_TRIM = CHAR_MAX + 1
+	};
 	int index, c, status = 0;
 	static const struct option long_options[] = {
 		{"full-name", required_argument, 0, 'f'},
 		{"office", required_argument, 0, 'o'},
 		{"office-phone", required_argument, 0, 'p'},
 		{"home-phone", required_argument, 0, 'h'},
+		{"trim", no_argument, 0, OPT_TRIM},
 		{"help", no_argument, 0, 'u'},
 		{"version", no_argument, 0, 'v'},
 		{NULL, no_argument, 0, '0'},
@@ -169,6 +175,9 @@ static void parse_argv(struct chfn_control *ctl, int argc, char **argv)
 			ctl->newf.home_phone = optarg;
 			status += check_gecos_string(_("Home Phone"), optarg);
 			break;
+		case OPT_TRIM:
+			ctl->trim = 1;
+			break;
 		case 'v':
 			printf(UTIL_LINUX_VERSION);
 			exit(EXIT_SUCCESS);
@@ -360,7 +369,7 @@ static int save_new_data(struct chfn_control *ctl)
 			ctl->newf.other);
 
 	/* remove trailing empty fields (but not subfields of ctl->newf.other) */
-	if (!ctl->newf.other) {
+	if (!ctl->newf.other || ctl->trim) {
 		while (len > 0 && gecos[len - 1] == ',')
 			len--;
 		gecos[len] = 0;
-- 
2.9.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] chfn: stop adding trailing commas to gecos
  2016-07-26 18:06 [PATCH 1/2] chfn: add --trim option to remove trailing commas Sami Kerola
@ 2016-07-26 18:06 ` Sami Kerola
  2016-08-02 14:07   ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Sami Kerola @ 2016-07-26 18:06 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

The other field should not appear automatically, it causes not-automatically
trimmed trailing commas to appear in gecos.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/chfn.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/login-utils/chfn.c b/login-utils/chfn.c
index 2d58fc3..c56f510 100644
--- a/login-utils/chfn.c
+++ b/login-utils/chfn.c
@@ -346,7 +346,7 @@ static void add_missing(struct chfn_control *ctl)
 	ctl->newf.office = find_field(ctl->newf.office, ctl->oldf.office);
 	ctl->newf.office_phone = find_field(ctl->newf.office_phone, ctl->oldf.office_phone);
 	ctl->newf.home_phone = find_field(ctl->newf.home_phone, ctl->oldf.home_phone);
-	ctl->newf.other = find_field(ctl->newf.other, ctl->oldf.other);
+	ctl->newf.other = ctl->oldf.other ? ctl->oldf.other : NULL;
 	printf("\n");
 }
 
@@ -361,12 +361,13 @@ static int save_new_data(struct chfn_control *ctl)
 	int len;
 
 	/* create the new gecos string */
-	len = xasprintf(&gecos, "%s,%s,%s,%s,%s",
+	len = xasprintf(&gecos, "%s,%s,%s,%s%s%s",
 			ctl->newf.full_name,
 			ctl->newf.office,
 			ctl->newf.office_phone,
 			ctl->newf.home_phone,
-			ctl->newf.other);
+			ctl->newf.other ? "," : "",
+			ctl->newf.other ? ctl->newf.other : "");
 
 	/* remove trailing empty fields (but not subfields of ctl->newf.other) */
 	if (!ctl->newf.other || ctl->trim) {
-- 
2.9.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] chfn: stop adding trailing commas to gecos
  2016-07-26 18:06 ` [PATCH 2/2] chfn: stop adding trailing commas to gecos Sami Kerola
@ 2016-08-02 14:07   ` Karel Zak
  0 siblings, 0 replies; 3+ messages in thread
From: Karel Zak @ 2016-08-02 14:07 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On Tue, Jul 26, 2016 at 07:06:02PM +0100, Sami Kerola wrote:
> The other field should not appear automatically, it causes not-automatically
> trimmed trailing commas to appear in gecos.

Hmmm.. I'm really not sure if this is a good idea. I'm almost sure
that somewhere in our universe is a program or a custom script that
parses GECOS and expects fixed number of commas.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-08-02 14:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-26 18:06 [PATCH 1/2] chfn: add --trim option to remove trailing commas Sami Kerola
2016-07-26 18:06 ` [PATCH 2/2] chfn: stop adding trailing commas to gecos Sami Kerola
2016-08-02 14:07   ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.