chfn: add --trim option to remove trailing commas
PATCH 1/2 chfn: add --trim option to remove trailing commas
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.
--- login-utils/chfn.1 | 5 +++++ login-utils/chfn.c | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
login-utils/chfn.1 +5 -0
@@ -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
login-utils/chfn.c +10 -1
@@ -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 ;
Signature · 1 line
2.9.0
PATCH 2/2 chfn: stop adding trailing commas to gecos
The other field should not appear automatically, it causes not-automatically trimmed trailing commas to appear in gecos.
--- login-utils/chfn.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
login-utils/chfn.c +4 -3
@@ -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) {
Signature · 1 line
2.9.0
Karel Zak
maintainerRe: [PATCH 2/2] chfn: stop adding trailing commas to gecos
Quoted 2 lines · Sami Kerola
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
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com