1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif /*HAVE_CONFIG_H*/
4 
5 #include <stdio.h>
6 #include <ctype.h>
7 
8 #ifdef HAVE_STRING_H
9 #include <string.h>
10 #endif
11 
12 #ifdef HAVE_STRINGS_H
13 #include <strings.h>
14 #endif
15 
16 
17 #ifdef HAVE_STDLIB_H
18 #include <stdlib.h>
19 #endif
20 
21 #include "minc_config.h"
22 
23 #ifdef _MSC_VER
24 #define snprintf _snprintf
25 #define vsnprintf _vsnprintf
26 #define strcasecmp _stricmp
27 #define strncasecmp _strnicmp
28 #endif
29 
30 
31 static const char *_CONFIG_VAR[]=
32   {
33       "MINC_FORCE_V2",
34       "MINC_COMPRESS",
35       "MINC_CHUNKING",
36       "MINC_LOGFILE",
37       "MINC_LOGLEVEL",
38       "MINC_MAX_FILE_BUFFER_KB",
39       "MINC_MAX_MEMORY_KB",
40       "MINC_FILE_CACHE_MB",
41       "MINC_CHECKSUM",
42       "MINC_PREFER_V2_API"
43   };
44 
45 enum {
46  _MICFG_MAX_STRING_LENGTH=256
47 };
48 
49 /*settings cache*/
50 static char        _CONFIG_VAL[MICFG_COUNT][_MICFG_MAX_STRING_LENGTH];
51 static int         _CONFIG_PRESENT[MICFG_COUNT]={0};
52 static int         _CONFIG_INIT[MICFG_COUNT]={0};
53 
54 /** Simple function to read a user's .mincrc file, if present.
55  */
56 static int
miread_cfg(const char * name,char * buffer,int maxlen)57 miread_cfg(const char *name, char *buffer, int maxlen)
58 {
59     FILE *fp;
60     int result = 0;
61     char *home_ptr = getenv("HOME");
62     char path[256];
63 
64     if (home_ptr != NULL) {
65       strncpy(path, home_ptr, sizeof(path) - 1);
66     }
67     else {
68       path[0] = '\0';
69     }
70     strcat(path, "/.mincrc");
71 
72     if ((fp = fopen(path, "r")) != NULL) {
73         while (fgets(buffer, maxlen, fp)) {
74             if (buffer[0] == '#') {
75                 continue;
76             }
77             if (!strncasecmp(buffer, name, strlen(name))) {
78                 char *tmp = strchr(buffer, '=');
79                 if (tmp != NULL) {
80                     tmp++;
81                     while (isspace(*tmp)) {
82                         tmp++;
83                     }
84                     strncpy(buffer, tmp, maxlen);
85                     result = 1;
86                     break;
87                 }
88             }
89         }
90         fclose(fp);
91     }
92     return (result);
93 }
94 
miget_cfg_str(int id)95 const char * miget_cfg_str(int id)
96 {
97   if(id<0 || id>=MICFG_COUNT) return "";
98 
99   if(!_CONFIG_INIT[id])
100   {
101     const char *name=_CONFIG_VAR[id];
102     char buffer[_MICFG_MAX_STRING_LENGTH];
103     char *var_ptr;
104 
105     if ((var_ptr = getenv(name)) != NULL) {
106         strncpy(buffer, var_ptr, _MICFG_MAX_STRING_LENGTH - 1);
107         _CONFIG_PRESENT[id]=1;
108     }  else {
109         if (!miread_cfg(name, buffer, _MICFG_MAX_STRING_LENGTH-1)) {
110           _CONFIG_PRESENT[id]=0;
111           buffer[0]='\0';
112         } else {
113           _CONFIG_PRESENT[id]=1;
114         }
115     }
116     strncpy(_CONFIG_VAL[id],buffer,_MICFG_MAX_STRING_LENGTH-1);
117     _CONFIG_VAL[id][_MICFG_MAX_STRING_LENGTH-1]='\0';
118     _CONFIG_INIT[id]=1;
119   }
120   return _CONFIG_VAL[id];
121 }
122 
miget_cfg_present(int id)123 int miget_cfg_present(int id)
124 {
125   if(id <0 || id>=MICFG_COUNT) return 0;
126 
127   miget_cfg_str(id);
128   return _CONFIG_PRESENT[id];
129 }
130 
131 
miget_cfg_bool(int id)132 int miget_cfg_bool(int id)
133 {
134   const char *_var=miget_cfg_str(id);
135   return atoi(_var) != 0;
136 }
137 
miget_cfg_int(int id)138 int miget_cfg_int(int id)
139 {
140   const char *_var=miget_cfg_str(id);
141   return atoi(_var);
142 }
143 
miget_cfg_double(int id)144 double miget_cfg_double(int id)
145 {
146   const char *_var=miget_cfg_str(id);
147   return atof(_var);
148 }
149