[PATCH] git-local-fetch: Fix error checking and leak in setup_indices()
[git/gitweb.git] / ident.c
bloba2d241fba025f82fa4c77aeb345103db66168fa9
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
10 #include <pwd.h>
11 #include <time.h>
12 #include <ctype.h>
14 static char real_email[1000];
15 static char real_name[1000];
16 static char real_date[50];
18 int setup_ident(void)
20 int len;
21 struct passwd *pw = getpwuid(getuid());
23 if (!pw)
24 die("You don't exist. Go away!");
26 /* Get the name ("gecos") */
27 len = strlen(pw->pw_gecos);
28 if (len >= sizeof(real_name))
29 die("Your parents must have hated you!");
30 memcpy(real_name, pw->pw_gecos, len+1);
32 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
33 len = strlen(pw->pw_name);
34 if (len > sizeof(real_email)/2)
35 die("Your sysadmin must hate you!");
36 memcpy(real_email, pw->pw_name, len);
37 real_email[len++] = '@';
38 gethostname(real_email + len, sizeof(real_email) - len);
39 if (!strchr(real_email+len, '.')) {
40 len = strlen(real_email);
41 real_email[len++] = '.';
42 getdomainname(real_email+len, sizeof(real_email)-len);
44 /* And set the default date */
45 datestamp(real_date, sizeof(real_date));
46 return 0;
49 static int add_raw(char *buf, int size, int offset, const char *str)
51 int len = strlen(str);
52 if (offset + len > size)
53 return size;
54 memcpy(buf + offset, str, len);
55 return offset + len;
58 static int crud(unsigned char c)
60 static char crud_array[256];
61 static int crud_array_initialized = 0;
63 if (!crud_array_initialized) {
64 int k;
66 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
67 crud_array[' '] = 1;
68 crud_array['.'] = 1;
69 crud_array[','] = 1;
70 crud_array[':'] = 1;
71 crud_array[';'] = 1;
72 crud_array['<'] = 1;
73 crud_array['>'] = 1;
74 crud_array['"'] = 1;
75 crud_array['\''] = 1;
76 crud_array_initialized = 1;
78 return crud_array[c];
82 * Copy over a string to the destination, but avoid special
83 * characters ('\n', '<' and '>') and remove crud at the end
85 static int copy(char *buf, int size, int offset, const char *src)
87 int i, len;
88 unsigned char c;
90 /* Remove crud from the beginning.. */
91 while ((c = *src) != 0) {
92 if (!crud(c))
93 break;
94 src++;
97 /* Remove crud from the end.. */
98 len = strlen(src);
99 while (len > 0) {
100 c = src[len-1];
101 if (!crud(c))
102 break;
103 --len;
107 * Copy the rest to the buffer, but avoid the special
108 * characters '\n' '<' and '>' that act as delimeters on
109 * a identification line
111 for (i = 0; i < len; i++) {
112 c = *src++;
113 switch (c) {
114 case '\n': case '<': case '>':
115 continue;
117 if (offset >= size)
118 return size;
119 buf[offset++] = c;
121 return offset;
124 char *get_ident(const char *name, const char *email, const char *date_str)
126 static char buffer[1000];
127 char date[50];
128 int i;
130 if (!name)
131 name = real_name;
132 if (!email)
133 email = real_email;
134 strcpy(date, real_date);
135 if (date_str)
136 parse_date(date_str, date, sizeof(date));
138 i = copy(buffer, sizeof(buffer), 0, name);
139 i = add_raw(buffer, sizeof(buffer), i, " <");
140 i = copy(buffer, sizeof(buffer), i, email);
141 i = add_raw(buffer, sizeof(buffer), i, "> ");
142 i = copy(buffer, sizeof(buffer), i, date);
143 if (i >= sizeof(buffer))
144 die("Impossibly long personal identifier");
145 buffer[i] = 0;
146 return buffer;
149 char *git_author_info(void)
151 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
154 char *git_committer_info(void)
156 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));