Replace C99 array initializers with code.
[git] / gitenv.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5
6 /*
7  * This array must be sorted by its canonical name, because
8  * we do look-up by binary search.
9  */
10 static struct backward_compatible_env {
11         const char *canonical;
12         const char *old;
13 } bc_name[] = {
14         { "GIT_ALTERNATE_OBJECT_DIRECTORIES", "SHA1_FILE_DIRECTORIES" },
15         { "GIT_AUTHOR_DATE", "AUTHOR_DATE" },
16         { "GIT_AUTHOR_EMAIL", "AUTHOR_EMAIL" },
17         { "GIT_AUTHOR_NAME", "AUTHOR_NAME" }, 
18         { "GIT_COMMITTER_EMAIL", "COMMIT_AUTHOR_EMAIL" },
19         { "GIT_COMMITTER_NAME", "COMMIT_AUTHOR_NAME" },
20         { "GIT_OBJECT_DIRECTORY", "SHA1_FILE_DIRECTORY" },
21 };
22
23 static void warn_old_environment(int pos)
24 {
25         int i;
26         static int warned = 0;
27         if (warned)
28                 return;
29
30         warned = 1;
31         fprintf(stderr,
32                 "warning: Attempting to use %s\n",
33                 bc_name[pos].old);
34         fprintf(stderr,
35                 "warning: GIT environment variables have been renamed.\n"
36                 "warning: Please adjust your scripts and environment.\n");
37         for (i = 0; i < sizeof(bc_name) / sizeof(bc_name[0]); i++) {
38                 /* warning is needed only when old name is there and
39                  * new name is not.
40                  */
41                 if (!getenv(bc_name[i].canonical) && getenv(bc_name[i].old))
42                         fprintf(stderr, "warning: old %s => new %s\n",
43                                 bc_name[i].old, bc_name[i].canonical);
44         }
45 }
46
47 char *gitenv_bc(const char *e)
48 {
49         int first, last;
50         char *val = getenv(e);
51         if (val)
52                 die("gitenv_bc called on existing %s; fix the caller.", e);
53
54         first = 0;
55         last = sizeof(bc_name) / sizeof(bc_name[0]);
56         while (last > first) {
57                 int next = (last + first) >> 1;
58                 int cmp = strcmp(e, bc_name[next].canonical);
59                 if (!cmp) {
60                         val = getenv(bc_name[next].old);
61                         /* If the user has only old name, warn.
62                          * otherwise stay silent.
63                          */
64                         if (val)
65                                 warn_old_environment(next);
66                         return val;
67                 }
68                 if (cmp < 0) {
69                         last = next;
70                         continue;
71                 }
72                 first = next+1;
73         }
74         return NULL;
75 }