(file) Return to Path.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Executor

  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 <stdlib.h>
 34               #include <sys/types.h>
 35               #include <sys/stat.h>
 36               #include <unistd.h>
 37               #include <string.h>
 38               #include "Path.h"
 39               #include "Strlcpy.h"
 40               #include "Strlcat.h"
 41               #include "Config.h"
 42               #include "Defines.h"
 43 mike  1.1.2.1 
 44               /*
 45               **==============================================================================
 46               **
 47 mike  1.1.2.6 ** DirName()
 48               **
 49               **     Remove the trailing component from the path (like the Unix dirname 
 50               **     command).
 51               **
 52               **         /a => /
 53               **         /a/ => /
 54               **         /a/b => /a
 55               **         /a/b/foo.conf => /a/b
 56               **
 57               **==============================================================================
 58               */
 59               
 60               void DirName(const char* path1, char path2[EXECUTOR_BUFFER_SIZE])
 61               {
 62                   char* p;
 63               
 64                   /* Copy path1 to path2. */
 65               
 66                   Strlcpy(path2, path1, EXECUTOR_BUFFER_SIZE);
 67               
 68 mike  1.1.2.6     /* Find last slash. */
 69               
 70                   p = strrchr(path2, '/');
 71               
 72                   /* Handle "." case (empty string or no slashes). */
 73               
 74                   if (*path2 == '\0' || p == NULL)
 75                   {
 76                       Strlcpy(path2, ".", EXECUTOR_BUFFER_SIZE);
 77                       return;
 78                   }
 79               
 80                   /* Remove trailing slashes. */
 81               
 82                   if (p[1] == '\0')
 83                   {
 84                       while (p != path2 && *p == '/')
 85                           *p-- = '\0';
 86                   }
 87               
 88                   /* Remove trailing component. */
 89 mike  1.1.2.6 
 90                   p = strrchr(path2, '/');
 91               
 92                   if (p)
 93                   {
 94                       if (p == path2)
 95                           p[1] = '\0';
 96               
 97                       while (p != path2 && *p == '/')
 98                           *p-- = '\0';
 99                   }
100                   else
101                       Strlcpy(path2, ".", EXECUTOR_BUFFER_SIZE);
102               }
103               
104               /*
105               **==============================================================================
106               **
107 mike  1.1.2.1 ** GetHomedPath()
108               **
109               **     Get the absolute path of the given named file or directory. If already
110               **     absolute it just returns. Otherwise, it prepends the PEGASUS_HOME
111               **     environment variable.
112               **
113               **==============================================================================
114               */
115               
116               int GetHomedPath(
117                   const char* name,
118                   char path[EXECUTOR_BUFFER_SIZE])
119               {
120                   const char* home;
121               
122                   /* If absolute, then use the name as is. */
123               
124 mike  1.1.2.4     if (name && name[0] == '/')
125 mike  1.1.2.1     {
126                       Strlcpy(path, name, EXECUTOR_BUFFER_SIZE);
127                       return 0;
128                   }
129               
130                   /* Use PEGASUS_HOME to form path. */
131               
132                   /* Flawfinder: ignore */
133                   if ((home = getenv("PEGASUS_HOME")) == NULL)
134                       return -1;
135               
136                   Strlcpy(path, home, EXECUTOR_BUFFER_SIZE);
137 mike  1.1.2.4 
138                   if (name)
139                   {
140                       Strlcat(path, "/", EXECUTOR_BUFFER_SIZE);
141                       Strlcat(path, name, EXECUTOR_BUFFER_SIZE);
142                   }
143 mike  1.1.2.1 
144                   return 0;
145               }
146               
147               /*
148               **==============================================================================
149               **
150               ** GetPegasusInternalBinDir()
151               **
152               **     Get the Pegasus "lbin" directory. This is the directory that contains
153               **     internal Pegasus programs. Note that administrative tools are contained
154               **     in the "sbin" directory.
155               **
156               **==============================================================================
157               */
158               
159               int GetPegasusInternalBinDir(char path[EXECUTOR_BUFFER_SIZE])
160               {
161                   char* p;
162                   struct stat st;
163               
164 mike  1.1.2.1     /* Make a copy of PEGASUS_PROVIDER_AGENT_PROC_NAME: */
165               
166                   char buffer[EXECUTOR_BUFFER_SIZE];
167                   Strlcpy(buffer, PEGASUS_PROVIDER_AGENT_PROC_NAME, sizeof(buffer));
168               
169 mike  1.1.2.5     /* Remove CIMPROVAGT suffix. */
170 mike  1.1.2.1 
171                   p = strrchr(buffer, '/');
172               
173                   if (!p)
174                       p = buffer;
175               
176                   *p = '\0';
177               
178                   /* If buffer path absolute, use this. */
179               
180                   if (buffer[0] == '/')
181                   {
182                       Strlcat(path, buffer, EXECUTOR_BUFFER_SIZE);
183                   }
184                   else
185                   {
186                       /* Prefix with PEGASUS_HOME environment variable. */
187               
188                       /* Flawfinder: ignore */
189                       const char* home = getenv("PEGASUS_HOME");
190               
191 mike  1.1.2.1         if (!home)
192                           return -1;
193               
194                       Strlcpy(path, home, EXECUTOR_BUFFER_SIZE);
195                       Strlcat(path, "/", EXECUTOR_BUFFER_SIZE);
196                       Strlcat(path, buffer, EXECUTOR_BUFFER_SIZE);
197                   }
198               
199                   /* Fail if no such directory. */
200               
201                   if (stat(path, &st) != 0)
202                       return -1;
203               
204                   if (!S_ISDIR(st.st_mode))
205                       return -1;
206               
207                   return 0;
208               }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2