@@ -50,6 +50,54 @@ static void copy_gecos(const struct passwd *w, char *name, size_t sz)
5050
5151}
5252
53+ static int add_mailname_host (char * buf , size_t len )
54+ {
55+ FILE * mailname ;
56+
57+ mailname = fopen ("/etc/mailname" , "r" );
58+ if (!mailname ) {
59+ if (errno != ENOENT )
60+ warning ("cannot open /etc/mailname: %s" ,
61+ strerror (errno ));
62+ return -1 ;
63+ }
64+ if (!fgets (buf , len , mailname )) {
65+ if (ferror (mailname ))
66+ warning ("cannot read /etc/mailname: %s" ,
67+ strerror (errno ));
68+ fclose (mailname );
69+ return -1 ;
70+ }
71+ /* success! */
72+ fclose (mailname );
73+ return 0 ;
74+ }
75+
76+ static void add_domainname (char * buf , size_t len )
77+ {
78+ struct hostent * he ;
79+ size_t namelen ;
80+ const char * domainname ;
81+
82+ if (gethostname (buf , len )) {
83+ warning ("cannot get host name: %s" , strerror (errno ));
84+ strlcpy (buf , "(none)" , len );
85+ return ;
86+ }
87+ namelen = strlen (buf );
88+ if (memchr (buf , '.' , namelen ))
89+ return ;
90+
91+ he = gethostbyname (buf );
92+ buf [namelen ++ ] = '.' ;
93+ buf += namelen ;
94+ len -= namelen ;
95+ if (he && (domainname = strchr (he -> h_name , '.' )))
96+ strlcpy (buf , domainname + 1 , len );
97+ else
98+ strlcpy (buf , "(none)" , len );
99+ }
100+
53101static void copy_email (const struct passwd * pw )
54102{
55103 /*
@@ -61,35 +109,29 @@ static void copy_email(const struct passwd *pw)
61109 die ("Your sysadmin must hate you!" );
62110 memcpy (git_default_email , pw -> pw_name , len );
63111 git_default_email [len ++ ] = '@' ;
64- gethostname (git_default_email + len , sizeof (git_default_email ) - len );
65- if (!strchr (git_default_email + len , '.' )) {
66- struct hostent * he = gethostbyname (git_default_email + len );
67- char * domainname ;
68-
69- len = strlen (git_default_email );
70- git_default_email [len ++ ] = '.' ;
71- if (he && (domainname = strchr (he -> h_name , '.' )))
72- strlcpy (git_default_email + len , domainname + 1 ,
73- sizeof (git_default_email ) - len );
74- else
75- strlcpy (git_default_email + len , "(none)" ,
76- sizeof (git_default_email ) - len );
77- }
112+
113+ if (!add_mailname_host (git_default_email + len ,
114+ sizeof (git_default_email ) - len ))
115+ return ; /* read from "/etc/mailname" (Debian) */
116+ add_domainname (git_default_email + len ,
117+ sizeof (git_default_email ) - len );
78118}
79119
80- static void setup_ident (void )
120+ static void setup_ident (const char * * name , const char * * emailp )
81121{
82122 struct passwd * pw = NULL ;
83123
84124 /* Get the name ("gecos") */
85- if (!git_default_name [0 ]) {
125+ if (!* name && ! git_default_name [0 ]) {
86126 pw = getpwuid (getuid ());
87127 if (!pw )
88128 die ("You don't exist. Go away!" );
89129 copy_gecos (pw , git_default_name , sizeof (git_default_name ));
90130 }
131+ if (!* name )
132+ * name = git_default_name ;
91133
92- if (!git_default_email [0 ]) {
134+ if (!* emailp && ! git_default_email [0 ]) {
93135 const char * email = getenv ("EMAIL" );
94136
95137 if (email && email [0 ]) {
@@ -104,6 +146,8 @@ static void setup_ident(void)
104146 copy_email (pw );
105147 }
106148 }
149+ if (!* emailp )
150+ * emailp = git_default_email ;
107151
108152 /* And set the default date */
109153 if (!git_default_date [0 ])
@@ -199,11 +243,7 @@ const char *fmt_ident(const char *name, const char *email,
199243 int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME );
200244 int name_addr_only = (flag & IDENT_NO_DATE );
201245
202- setup_ident ();
203- if (!name )
204- name = git_default_name ;
205- if (!email )
206- email = git_default_email ;
246+ setup_ident (& name , & email );
207247
208248 if (!* name ) {
209249 struct passwd * pw ;
0 commit comments