1 kumpf 1.2 /*
|
2 martin 1.4 //%LICENSE////////////////////////////////////////////////////////////////
|
3 martin 1.5 //
|
4 martin 1.4 // Licensed to The Open Group (TOG) under one or more contributor license
5 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
6 // this work for additional information regarding copyright ownership.
7 // Each contributor licenses this file to you under the OpenPegasus Open
8 // Source License; you may not use this file except in compliance with the
9 // License.
|
10 martin 1.5 //
|
11 martin 1.4 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
|
17 martin 1.5 //
|
18 martin 1.4 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
|
20 martin 1.5 //
|
21 martin 1.4 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
22 martin 1.5 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
23 martin 1.4 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
28 martin 1.5 //
|
29 martin 1.4 //////////////////////////////////////////////////////////////////////////
|
30 kumpf 1.2 */
|
31 martin 1.4
|
32 kumpf 1.2 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include "Path.h"
38 #include "Strlcpy.h"
39 #include "Strlcat.h"
40 #include "Config.h"
41 #include "Defines.h"
42
43 /*
44 **==============================================================================
45 **
46 ** DirName()
47 **
48 ** Remove the trailing component from the path (like the Unix dirname
49 ** command).
50 **
51 ** /a => /
52 ** /a/ => /
53 kumpf 1.2 ** /a/b => /a
54 ** /a/b/foo.conf => /a/b
55 **
56 **==============================================================================
57 */
58
59 void DirName(const char* path1, char path2[EXECUTOR_BUFFER_SIZE])
60 {
61 char* p;
62
63 /* Copy path1 to path2. */
64
65 Strlcpy(path2, path1, EXECUTOR_BUFFER_SIZE);
66
67 /* Find last slash. */
68
69 p = strrchr(path2, '/');
70
71 /* Handle "." case (empty string or no slashes). */
72
73 if (*path2 == '\0' || p == NULL)
74 kumpf 1.2 {
75 Strlcpy(path2, ".", EXECUTOR_BUFFER_SIZE);
76 return;
77 }
78
79 /* Remove trailing slashes. */
80
81 if (p[1] == '\0')
82 {
83 while (p != path2 && *p == '/')
84 *p-- = '\0';
85 }
86
87 /* Remove trailing component. */
88
89 p = strrchr(path2, '/');
90
91 if (p)
92 {
93 if (p == path2)
94 p[1] = '\0';
95 kumpf 1.2
96 while (p != path2 && *p == '/')
97 *p-- = '\0';
98 }
99 else
100 Strlcpy(path2, ".", EXECUTOR_BUFFER_SIZE);
101 }
102
103 /*
104 **==============================================================================
105 **
106 ** GetHomedPath()
107 **
108 ** Get the absolute path of the given named file or directory. If already
109 ** absolute it just returns. Otherwise, it prepends the PEGASUS_HOME
110 ** environment variable.
111 **
112 **==============================================================================
113 */
114
115 int GetHomedPath(
116 kumpf 1.2 const char* name,
117 char path[EXECUTOR_BUFFER_SIZE])
118 {
119 const char* home;
120
121 /* If absolute, then use the name as is. */
122
123 if (name && name[0] == '/')
124 {
125 Strlcpy(path, name, EXECUTOR_BUFFER_SIZE);
126 return 0;
127 }
128
129 /* Use PEGASUS_HOME to form path. */
130
131 /* Flawfinder: ignore */
132 if ((home = getenv("PEGASUS_HOME")) == NULL)
133 return -1;
134
135 Strlcpy(path, home, EXECUTOR_BUFFER_SIZE);
136
137 kumpf 1.2 if (name)
138 {
139 Strlcat(path, "/", EXECUTOR_BUFFER_SIZE);
140 Strlcat(path, name, EXECUTOR_BUFFER_SIZE);
141 }
142
143 return 0;
144 }
145
146 /*
147 **==============================================================================
148 **
149 ** GetPegasusInternalBinDir()
150 **
151 ** Get the Pegasus "lbin" directory. This is the directory that contains
152 ** internal Pegasus programs. Note that administrative tools are contained
153 ** in the "sbin" directory.
154 **
155 **==============================================================================
156 */
157
158 kumpf 1.2 int GetPegasusInternalBinDir(char path[EXECUTOR_BUFFER_SIZE])
159 {
160 char* p;
161 struct stat st;
162
163 /* Make a copy of PEGASUS_PROVIDER_AGENT_PROC_NAME: */
164
165 char buffer[EXECUTOR_BUFFER_SIZE];
166 Strlcpy(buffer, PEGASUS_PROVIDER_AGENT_PROC_NAME, sizeof(buffer));
167
168 /* Remove CIMPROVAGT suffix. */
169
170 p = strrchr(buffer, '/');
171
172 if (!p)
173 p = buffer;
174
175 *p = '\0';
176
177 /* If buffer path absolute, use this. */
178
179 kumpf 1.2 if (buffer[0] == '/')
180 {
|
181 kumpf 1.3 Strlcpy(path, buffer, EXECUTOR_BUFFER_SIZE);
|
182 kumpf 1.2 }
183 else
184 {
185 /* Prefix with PEGASUS_HOME environment variable. */
186
187 /* Flawfinder: ignore */
188 const char* home = getenv("PEGASUS_HOME");
189
190 if (!home)
191 return -1;
192
193 Strlcpy(path, home, EXECUTOR_BUFFER_SIZE);
194 Strlcat(path, "/", EXECUTOR_BUFFER_SIZE);
195 Strlcat(path, buffer, EXECUTOR_BUFFER_SIZE);
196 }
197
198 /* Fail if no such directory. */
199
200 if (stat(path, &st) != 0)
201 return -1;
202
203 kumpf 1.2 if (!S_ISDIR(st.st_mode))
204 return -1;
205
206 return 0;
207 }
|