ident.con commit Replace C99 array initializers with code. (fb2af03)
   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
  14static char real_email[1000];
  15static char real_name[1000];
  16static char real_date[50];
  17
  18int 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
  45        /* And set the default date */
  46        datestamp(real_date, sizeof(real_date));
  47        return 0;
  48}
  49
  50static int add_raw(char *buf, int size, int offset, const char *str)
  51{
  52        int len = strlen(str);
  53        if (offset + len > size)
  54                return size;
  55        memcpy(buf + offset, str, len);
  56        return offset + len;
  57}
  58
  59static int crud(unsigned char c)
  60{
  61        static char crud_array[256];
  62        static int crud_array_initialized = 0;
  63
  64        if (!crud_array_initialized) {
  65                int k;
  66
  67                for (k = 0; k <= 31; ++k) crud_array[k] = 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['\''] = 1;
  77                crud_array_initialized = 1;
  78        }
  79        return crud_array[c];
  80}
  81
  82/*
  83 * Copy over a string to the destination, but avoid special
  84 * characters ('\n', '<' and '>') and remove crud at the end
  85 */
  86static int copy(char *buf, int size, int offset, const char *src)
  87{
  88        int i, len;
  89        unsigned char c;
  90
  91        /* Remove crud from the beginning.. */
  92        while ((c = *src) != 0) {
  93                if (!crud(c))
  94                        break;
  95                src++;
  96        }
  97
  98        /* Remove crud from the end.. */
  99        len = strlen(src);
 100        while (len > 0) {
 101                c = src[len-1];
 102                if (!crud(c))
 103                        break;
 104                --len;
 105        }
 106
 107        /*
 108         * Copy the rest to the buffer, but avoid the special
 109         * characters '\n' '<' and '>' that act as delimeters on
 110         * a identification line
 111         */
 112        for (i = 0; i < len; i++) {
 113                c = *src++;
 114                switch (c) {
 115                case '\n': case '<': case '>':
 116                        continue;
 117                }
 118                if (offset >= size)
 119                        return size;
 120                buf[offset++] = c;
 121        }
 122        return offset;
 123}
 124
 125char *get_ident(const char *name, const char *email, const char *date_str)
 126{
 127        static char buffer[1000];
 128        char date[50];
 129        int i;
 130
 131        if (!name)
 132                name = real_name;
 133        if (!email)
 134                email = real_email;
 135        strcpy(date, real_date);
 136        if (date_str)
 137                parse_date(date_str, date, sizeof(date));
 138
 139        i = copy(buffer, sizeof(buffer), 0, name);
 140        i = add_raw(buffer, sizeof(buffer), i, " <");
 141        i = copy(buffer, sizeof(buffer), i, email);
 142        i = add_raw(buffer, sizeof(buffer), i, "> ");
 143        i = copy(buffer, sizeof(buffer), i, date);
 144        if (i >= sizeof(buffer))
 145                die("Impossibly long personal identifier");
 146        buffer[i] = 0;
 147        return buffer;
 148}
 149
 150char *git_author_info(void)
 151{
 152        return get_ident(gitenv("GIT_AUTHOR_NAME"), gitenv("GIT_AUTHOR_EMAIL"), gitenv("GIT_AUTHOR_DATE"));
 153}
 154
 155char *git_committer_info(void)
 156{
 157        return get_ident(gitenv("GIT_COMMITTER_NAME"), gitenv("GIT_COMMITTER_EMAIL"), gitenv("GIT_COMMITTER_DATE"));
 158}