copy_gecos: fix not adding nlen to len when processing "&"
[git/git.git] / ident.c
CommitLineData
6aa33f40
LT
1/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
9
e1b10391 10static char git_default_date[50];
6aa33f40 11
b0732115 12static void copy_gecos(const struct passwd *w, char *name, size_t sz)
e9bacb4f
JH
13{
14 char *src, *dst;
b0732115 15 size_t len, nlen;
e9bacb4f
JH
16
17 nlen = strlen(w->pw_name);
18
19 /* Traditionally GECOS field had office phone numbers etc, separated
20 * with commas. Also & stands for capitalized form of the login name.
21 */
22
23 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
24 int ch = *src;
25 if (ch != '&') {
26 *dst++ = ch;
27 if (ch == 0 || ch == ',')
28 break;
29 len++;
30 continue;
31 }
32 if (len + nlen < sz) {
33 /* Sorry, Mr. McDonald... */
34 *dst++ = toupper(*w->pw_name);
35 memcpy(dst, w->pw_name + 1, nlen - 1);
36 dst += nlen - 1;
c0336ff2 37 len += nlen;
e9bacb4f
JH
38 }
39 }
40 if (len < sz)
41 name[len] = 0;
42 else
43 die("Your parents must have hated you!");
44
45}
46
0b952a98 47static void copy_email(const struct passwd *pw)
6aa33f40 48{
01754769
JH
49 /*
50 * Make up a fake email address
51 * (name + '@' + hostname [+ '.' + domainname])
52 */
b0732115 53 size_t len = strlen(pw->pw_name);
e1b10391 54 if (len > sizeof(git_default_email)/2)
7a868a84 55 die("Your sysadmin must hate you!");
e1b10391
LT
56 memcpy(git_default_email, pw->pw_name, len);
57 git_default_email[len++] = '@';
58 gethostname(git_default_email + len, sizeof(git_default_email) - len);
59 if (!strchr(git_default_email+len, '.')) {
adc3dbca
PB
60 struct hostent *he = gethostbyname(git_default_email + len);
61 char *domainname;
62
e1b10391
LT
63 len = strlen(git_default_email);
64 git_default_email[len++] = '.';
adc3dbca 65 if (he && (domainname = strchr(he->h_name, '.')))
01754769
JH
66 strlcpy(git_default_email + len, domainname + 1,
67 sizeof(git_default_email) - len);
adc3dbca 68 else
01754769
JH
69 strlcpy(git_default_email + len, "(none)",
70 sizeof(git_default_email) - len);
6aa33f40 71 }
01754769
JH
72}
73
74static void setup_ident(void)
75{
76 struct passwd *pw = NULL;
77
78 /* Get the name ("gecos") */
79 if (!git_default_name[0]) {
80 pw = getpwuid(getuid());
81 if (!pw)
82 die("You don't exist. Go away!");
83 copy_gecos(pw, git_default_name, sizeof(git_default_name));
84 }
85
86 if (!git_default_email[0]) {
46f74f00
MK
87 const char *email = getenv("EMAIL");
88
99178c83 89 if (email && email[0]) {
46f74f00
MK
90 strlcpy(git_default_email, email,
91 sizeof(git_default_email));
99178c83
JH
92 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
93 } else {
46f74f00
MK
94 if (!pw)
95 pw = getpwuid(getuid());
96 if (!pw)
97 die("You don't exist. Go away!");
98 copy_email(pw);
99 }
01754769
JH
100 }
101
6aa33f40 102 /* And set the default date */
01754769
JH
103 if (!git_default_date[0])
104 datestamp(git_default_date, sizeof(git_default_date));
6aa33f40
LT
105}
106
b0732115 107static int add_raw(char *buf, size_t size, int offset, const char *str)
6aa33f40 108{
b0732115 109 size_t len = strlen(str);
6aa33f40
LT
110 if (offset + len > size)
111 return size;
112 memcpy(buf + offset, str, len);
113 return offset + len;
114}
115
116static int crud(unsigned char c)
117{
f64c81d4
AR
118 return c <= 32 ||
119 c == '.' ||
120 c == ',' ||
121 c == ':' ||
122 c == ';' ||
123 c == '<' ||
124 c == '>' ||
125 c == '"' ||
d404bf02 126 c == '\\' ||
f64c81d4 127 c == '\'';
6aa33f40
LT
128}
129
130/*
131 * Copy over a string to the destination, but avoid special
132 * characters ('\n', '<' and '>') and remove crud at the end
133 */
b0732115 134static int copy(char *buf, size_t size, int offset, const char *src)
6aa33f40 135{
b0732115 136 size_t i, len;
6aa33f40
LT
137 unsigned char c;
138
139 /* Remove crud from the beginning.. */
140 while ((c = *src) != 0) {
141 if (!crud(c))
142 break;
143 src++;
144 }
145
146 /* Remove crud from the end.. */
147 len = strlen(src);
148 while (len > 0) {
149 c = src[len-1];
150 if (!crud(c))
151 break;
152 --len;
153 }
154
155 /*
156 * Copy the rest to the buffer, but avoid the special
82f9d58a 157 * characters '\n' '<' and '>' that act as delimiters on
790296fd 158 * an identification line
6aa33f40
LT
159 */
160 for (i = 0; i < len; i++) {
161 c = *src++;
162 switch (c) {
163 case '\n': case '<': case '>':
164 continue;
165 }
166 if (offset >= size)
167 return size;
168 buf[offset++] = c;
169 }
170 return offset;
171}
172
749be728 173static const char *env_hint =
d5cc2de9 174"\n"
6c293d40 175"*** Please tell me who you are.\n"
d5cc2de9
HWN
176"\n"
177"Run\n"
178"\n"
8e7425da 179" git config --global user.email \"you@example.com\"\n"
180787c4 180" git config --global user.name \"Your Name\"\n"
d5cc2de9 181"\n"
180787c4
SP
182"to set your account\'s default identity.\n"
183"Omit --global to set the identity only in this repository.\n"
d5cc2de9 184"\n";
749be728 185
774751a8
JH
186const char *fmt_ident(const char *name, const char *email,
187 const char *date_str, int flag)
6aa33f40
LT
188{
189 static char buffer[1000];
190 char date[50];
191 int i;
774751a8
JH
192 int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
193 int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
194 int name_addr_only = (flag & IDENT_NO_DATE);
6aa33f40 195
01754769 196 setup_ident();
6aa33f40 197 if (!name)
e1b10391 198 name = git_default_name;
6aa33f40 199 if (!email)
e1b10391 200 email = git_default_email;
dfdd309e 201
749be728 202 if (!*name) {
cb280e10
JH
203 struct passwd *pw;
204
774751a8 205 if ((warn_on_no_name || error_on_no_name) &&
cb280e10 206 name == git_default_name && env_hint) {
8b770a2a 207 fputs(env_hint, stderr);
c27b3925 208 env_hint = NULL; /* warn only once */
749be728 209 }
774751a8 210 if (error_on_no_name)
749be728 211 die("empty ident %s <%s> not allowed", name, email);
cb280e10
JH
212 pw = getpwuid(getuid());
213 if (!pw)
214 die("You don't exist. Go away!");
215 strlcpy(git_default_name, pw->pw_name,
216 sizeof(git_default_name));
217 name = git_default_name;
749be728 218 }
dfdd309e 219
e1b10391 220 strcpy(date, git_default_date);
d9ccfe77 221 if (!name_addr_only && date_str)
6aa33f40
LT
222 parse_date(date_str, date, sizeof(date));
223
224 i = copy(buffer, sizeof(buffer), 0, name);
225 i = add_raw(buffer, sizeof(buffer), i, " <");
226 i = copy(buffer, sizeof(buffer), i, email);
d9ccfe77
JH
227 if (!name_addr_only) {
228 i = add_raw(buffer, sizeof(buffer), i, "> ");
229 i = copy(buffer, sizeof(buffer), i, date);
230 } else {
231 i = add_raw(buffer, sizeof(buffer), i, ">");
232 }
6aa33f40
LT
233 if (i >= sizeof(buffer))
234 die("Impossibly long personal identifier");
235 buffer[i] = 0;
236 return buffer;
237}
d289d136 238
d9ccfe77
JH
239const char *fmt_name(const char *name, const char *email)
240{
774751a8 241 return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
d9ccfe77
JH
242}
243
774751a8 244const char *git_author_info(int flag)
d289d136 245{
798123af 246 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 247 getenv("GIT_AUTHOR_EMAIL"),
749be728 248 getenv("GIT_AUTHOR_DATE"),
774751a8 249 flag);
d289d136
EB
250}
251
774751a8 252const char *git_committer_info(int flag)
d289d136 253{
91c38a21
JH
254 if (getenv("GIT_COMMITTER_NAME"))
255 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
256 if (getenv("GIT_COMMITTER_EMAIL"))
257 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 258 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 259 getenv("GIT_COMMITTER_EMAIL"),
749be728 260 getenv("GIT_COMMITTER_DATE"),
774751a8 261 flag);
d289d136 262}
5aeb3a3a
JH
263
264int user_ident_sufficiently_given(void)
265{
266#ifndef WINDOWS
267 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
268#else
269 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
270#endif
271}