1 mike 1.1.2.1 /*
2 //%2006////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 // IBM Corp.; EMC Corporation, The Open Group.
8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 // EMC Corporation; Symantec Corporation; The Open Group.
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to
17 // deal in the Software without restriction, including without limitation the
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 // sell copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 mike 1.1.2.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32 */
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include "Config.h"
37 #include "Strlcpy.h"
38 #include "Strlcat.h"
39 #include "Path.h"
40 #include "Defines.h"
41
42 /*
43 mike 1.1.2.1 **==============================================================================
44 **
|
70 mike 1.1.2.1 ** GetConfigParamFromCommandLine()
71 **
72 ** Attempt to find a command line configuratin parameter of the form
73 ** name=value. For example:
74 ** cimservermain repositoryDir=/opt/pegasus/repository.
75 ** Return 0 if found.
76 **
77 **==============================================================================
78 */
79
80 int GetConfigParamFromCommandLine(
81 int argc,
82 char** argv,
83 const char* name,
84 char value[EXECUTOR_BUFFER_SIZE])
85 {
86 size_t n = strlen(name);
87 int i;
88
89 for (i = 1; i < argc; i++)
90 {
91 mike 1.1.2.1 if (strncmp(argv[i], name, n) == 0 && argv[i][n] == '=')
92 {
93 const char* p = argv[i] + n + 1;
94 Strlcpy(value, argv[i] + n + 1, EXECUTOR_BUFFER_SIZE);
95 return 0;
96 }
97 }
98
99 return -1;
100 }
101
102 /*
103 **==============================================================================
104 **
105 ** GetConfigParamFromFile()
106 **
107 ** Attempt to find the named option in the configuration file. If found,
108 ** set value and return 0.
109 **
110 **==============================================================================
111 */
112 mike 1.1.2.1
113 int GetConfigParamFromFile(
114 const char* path,
115 const char* name,
116 char value[EXECUTOR_BUFFER_SIZE])
117 {
118 char buffer[EXECUTOR_BUFFER_SIZE];
119 FILE* is;
120 size_t n;
121
122 if ((is = fopen(path, "r")) == NULL)
123 return -1;
124
125 n = strlen(name);
126
127 while (fgets(buffer, sizeof(buffer), is) != 0)
128 {
129 size_t r;
130
131 /* Skip comments. */
132
133 mike 1.1.2.1 if (buffer[0] == '#')
134 continue;
135
136 /* Remove trailing whitespace. */
137
138 r = strlen(buffer);
139
140 while (r--)
141 {
142 if (isspace(buffer[r]))
143 buffer[r] = '\0';
144 }
145
146 /* Skip blank lines. */
147
148 if (buffer[0] == '\0')
149 continue;
150
151 /* Check option. */
152
153 if (strncmp(buffer, name, n) == 0 && buffer[n] == '=')
154 mike 1.1.2.1 {
155 Strlcpy(value, buffer + n + 1, EXECUTOR_BUFFER_SIZE);
156 fclose(is);
157 return 0;
158 }
159 }
160
161 /* Not found! */
162 fclose(is);
163 return -1;
164 }
165
166 /*
167 **==============================================================================
168 **
169 ** GetConfigParam()
170 **
171 ** Attempt to find a configuration setting for the given name. First,
172 ** search the command line and then the config file.
173 **
174 **==============================================================================
175 mike 1.1.2.1 */
176
177 int GetConfigParam(
178 int argc,
179 char** argv,
180 const char* name,
181 char value[EXECUTOR_BUFFER_SIZE])
182 {
183 char path[EXECUTOR_BUFFER_SIZE];
|
197 mike 1.1.2.2 /* (3) Finally check the default configuration table. */
198
199 for (i = 0; i < _configSize; i++)
200 {
201 if (strcmp(_config[i].name, name) == 0)
202 {
203 Strlcpy(value, _config[i].value, EXECUTOR_BUFFER_SIZE);
204 return 0;
205 }
206 }
207
|