[PATCH] getdomainname should be usable on SunOS with -lnsl
[git.git] / ident.c
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
10 #include <pwd.h>
11 #include <time.h>
12 #include <ctype.h>
13
14 static char real_email[1000];
15 static char real_name[1000];
16 static char real_date[50];
17
18 int setup_ident(void)
19 {
20         int len;
21         struct passwd *pw = getpwuid(getuid());
22
23         if (!pw)
24                 die("You don't exist. Go away!");
25
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);
31
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);
43         }
44         /* And set the default date */
45         datestamp(real_date, sizeof(real_date));
46         return 0;
47 }
48
49 static int add_raw(char *buf, int size, int offset, const char *str)
50 {
51         int len = strlen(str);
52         if (offset + len > size)
53                 return size;
54         memcpy(buf + offset, str, len);
55         return offset + len;
56 }
57
58 static int crud(unsigned char c)
59 {
60         static char crud_array[256];
61         static int crud_array_initialized = 0;
62
63         if (!crud_array_initialized) {
64                 int k;
65
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;
77         }
78         return crud_array[c];
79 }
80
81 /*
82  * Copy over a string to the destination, but avoid special
83  * characters ('\n', '<' and '>') and remove crud at the end
84  */
85 static int copy(char *buf, int size, int offset, const char *src)
86 {
87         int i, len;
88         unsigned char c;
89
90         /* Remove crud from the beginning.. */
91         while ((c = *src) != 0) {
92                 if (!crud(c))
93                         break;
94                 src++;
95         }
96
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;
104         }
105
106         /*
107          * Copy the rest to the buffer, but avoid the special
108          * characters '\n' '<' and '>' that act as delimeters on
109          * a identification line
110          */
111         for (i = 0; i < len; i++) {
112                 c = *src++;
113                 switch (c) {
114                 case '\n': case '<': case '>':
115                         continue;
116                 }
117                 if (offset >= size)
118                         return size;
119                 buf[offset++] = c;
120         }
121         return offset;
122 }
123
124 char *get_ident(const char *name, const char *email, const char *date_str)
125 {
126         static char buffer[1000];
127         char date[50];
128         int i;
129
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));
137
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;
147 }
148
149 char *git_author_info(void)
150 {
151         return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
152 }
153
154 char *git_committer_info(void)
155 {
156         return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));
157 }