[PATCH] Exit with status code 2 if we get an exception.
[git/jnareb-git/bp-gitweb.git] / ident.c
blob0df2d979242bd6ee246bd280ffebdf8299408a8c
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 #ifndef NO_GETDOMAINNAME
40 if (!strchr(real_email+len, '.')) {
41 len = strlen(real_email);
42 real_email[len++] = '.';
43 getdomainname(real_email+len, sizeof(real_email)-len);
45 #endif
46 /* And set the default date */
47 datestamp(real_date, sizeof(real_date));
48 return 0;
51 static int add_raw(char *buf, int size, int offset, const char *str)
53 int len = strlen(str);
54 if (offset + len > size)
55 return size;
56 memcpy(buf + offset, str, len);
57 return offset + len;
60 static int crud(unsigned char c)
62 static char crud_array[256];
63 static int crud_array_initialized = 0;
65 if (!crud_array_initialized) {
66 int k;
68 for (k = 0; k <= 31; ++k) crud_array[k] = 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['"'] = 1;
77 crud_array['\''] = 1;
78 crud_array_initialized = 1;
80 return crud_array[c];
84 * Copy over a string to the destination, but avoid special
85 * characters ('\n', '<' and '>') and remove crud at the end
87 static int copy(char *buf, int size, int offset, const char *src)
89 int i, len;
90 unsigned char c;
92 /* Remove crud from the beginning.. */
93 while ((c = *src) != 0) {
94 if (!crud(c))
95 break;
96 src++;
99 /* Remove crud from the end.. */
100 len = strlen(src);
101 while (len > 0) {
102 c = src[len-1];
103 if (!crud(c))
104 break;
105 --len;
109 * Copy the rest to the buffer, but avoid the special
110 * characters '\n' '<' and '>' that act as delimeters on
111 * a identification line
113 for (i = 0; i < len; i++) {
114 c = *src++;
115 switch (c) {
116 case '\n': case '<': case '>':
117 continue;
119 if (offset >= size)
120 return size;
121 buf[offset++] = c;
123 return offset;
126 char *get_ident(const char *name, const char *email, const char *date_str)
128 static char buffer[1000];
129 char date[50];
130 int i;
132 if (!name)
133 name = real_name;
134 if (!email)
135 email = real_email;
136 strcpy(date, real_date);
137 if (date_str)
138 parse_date(date_str, date, sizeof(date));
140 i = copy(buffer, sizeof(buffer), 0, name);
141 i = add_raw(buffer, sizeof(buffer), i, " <");
142 i = copy(buffer, sizeof(buffer), i, email);
143 i = add_raw(buffer, sizeof(buffer), i, "> ");
144 i = copy(buffer, sizeof(buffer), i, date);
145 if (i >= sizeof(buffer))
146 die("Impossibly long personal identifier");
147 buffer[i] = 0;
148 return buffer;
151 char *git_author_info(void)
153 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
156 char *git_committer_info(void)
158 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));