1583263314841378d6da22f01292e2cbcb6fd381
[openafs.git] / src / util / dirpath.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <limits.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include "assert.h"
23 #include "afsutil.h"
24 #include "fileutil.h"
25 #ifdef AFS_PTHREAD_ENV
26 #include <pthread.h>
27 static pthread_once_t dirInit_once = PTHREAD_ONCE_INIT;
28 #endif
29 #ifdef AFS_NT40_ENV
30 #include <windows.h>
31 #include <WINNT\afssw.h>
32 #endif
33 #ifdef AFS_DARWIN_ENV
34 #include <unistd.h>
35 #endif
36
37 /* local vars */
38 /* static storage for path strings */
39 static char dirPathArray[AFSDIR_PATHSTRING_MAX][AFSDIR_PATH_MAX];
40
41 /* indicate if and how the dirpath module initialized. */
42 static int initFlag = 0;
43 static unsigned int initStatus = 0;
44
45
46 /* storage for dynamically-determined install dir (NT only; long and short) */
47 #ifdef AFS_NT40_ENV
48 static char ntServerInstallDirLong[AFSDIR_PATH_MAX];
49 static char ntServerInstallDirShort[AFSDIR_PATH_MAX];
50 static char ntClientConfigDirLong[AFSDIR_PATH_MAX];
51 static char ntClientConfigDirShort[AFSDIR_PATH_MAX];
52 #endif
53
54 /* storage for local afs server/client paths (top-level) */
55 static char afsSrvDirPath[AFSDIR_PATH_MAX];
56 static char afsClntDirPath[AFSDIR_PATH_MAX];
57
58 /* internal array init function */
59 static void initDirPathArray(void);
60
61 /* Additional macros for ease of use */
62 /* buf is expected to be atleast AFS_PATH_MAX bytes long */
63 #define AFSDIR_SERVER_DIRPATH(buf, dir)  \
64             strcompose(buf, AFSDIR_PATH_MAX, dirPathArray[AFSDIR_SERVER_AFS_DIRPATH_ID], "/", dir, NULL)
65
66 #define AFSDIR_SERVER_FILEPATH(buf, dir, file)  \
67             strcompose(buf, AFSDIR_PATH_MAX, dirPathArray[AFSDIR_SERVER_AFS_DIRPATH_ID], "/", dir, "/", file,  NULL)
68
69 #define AFSDIR_CLIENT_DIRPATH(buf, dir)  \
70             strcompose(buf, AFSDIR_PATH_MAX, dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID], "/", dir, NULL)
71
72 #define AFSDIR_CLIENT_FILEPATH(buf, dir, file)  \
73             strcompose(buf, AFSDIR_PATH_MAX, dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID], "/", dir, "/", file,  NULL)
74
75
76 /* initAFSDirPath() -- External users call this function to initialize
77  * the dirpath module and/or to determine the initialization status.
78  */
79 unsigned int initAFSDirPath(void)
80 {
81     if (initFlag == 0) { /* not yet init'ed, so initialize */
82 #ifdef AFS_PTHREAD_ENV
83         pthread_once(&dirInit_once, initDirPathArray);
84 #else
85         initDirPathArray();
86 #endif
87     }
88     return initStatus;
89 }
90
91
92 /* initDirPathArray() -- Initializes the afs dir paths for the 
93  *     server and client installations.
94  *
95  *     For NT these are determined dynamically; for Unix they are static.
96  *
97  *     NT NOTE: If a particular component (client/server) is not installed
98  *              then we may not be able to initialize the paths to anything
99  *              meaningful.  In this case the paths are set to the local
100  *              temp directory to avoid later reference to an uninitialized
101  *              variable.  The initStatus flag is set to indicate which
102  *              paths (client/server) initialized properly for callers of
103  *              initAFSDirPath() who would like to know this information.
104  */
105 static void initDirPathArray(void)
106
107     char *pathp;
108
109 #ifdef AFS_NT40_ENV
110     char *buf;
111     int status;
112
113     /* get the afs server software installation dir from the registry */
114     if (afssw_GetServerInstallDir(&buf)) {
115         /* failed; server not installed; use temp directory */
116         strcpy(ntServerInstallDirLong, gettmpdir());
117     } else {
118         strcpy(ntServerInstallDirLong, buf);
119         free(buf);
120         initStatus |= AFSDIR_SERVER_PATHS_OK;
121     }
122     FilepathNormalize(ntServerInstallDirLong);
123     status = GetShortPathName(ntServerInstallDirLong,
124                               ntServerInstallDirShort, AFSDIR_PATH_MAX);
125     if (status == 0 || status > AFSDIR_PATH_MAX) {
126         /* can't convert path to short version; just use long version */
127         strcpy(ntServerInstallDirShort, ntServerInstallDirLong);
128     }
129     FilepathNormalize(ntServerInstallDirShort);
130
131     /* get the afs client configuration directory (/usr/vice/etc equivalent) */
132     status = GetWindowsDirectory(ntClientConfigDirLong, AFSDIR_PATH_MAX);
133     if (status == 0 || status > AFSDIR_PATH_MAX) {
134         /* failed to get canonical Windows directory; use temp directory */
135         strcpy(ntClientConfigDirLong, gettmpdir());
136     } else {
137         initStatus |= AFSDIR_CLIENT_PATHS_OK;
138     }
139     FilepathNormalize(ntClientConfigDirLong);
140
141     status = GetShortPathName(ntClientConfigDirLong,
142                               ntClientConfigDirShort, AFSDIR_PATH_MAX);
143     if (status == 0 || status > AFSDIR_PATH_MAX) {
144         /* can't convert path to short version; just use long version */
145         strcpy(ntClientConfigDirShort, ntClientConfigDirLong);
146     }
147     FilepathNormalize(ntClientConfigDirShort);
148
149     /* setup the root server directory path (/usr/afs equivalent) */
150     strcpy(afsSrvDirPath, ntServerInstallDirShort);
151     strcat(afsSrvDirPath, AFSDIR_CANONICAL_SERVER_AFS_DIRPATH);
152
153     /* there is no root client directory path (/usr/vice equivalent) */
154     afsClntDirPath[0] = '\0';
155
156     /* setup top level dirpath (/usr equivalent); valid for server ONLY */
157     strcpy(dirPathArray[AFSDIR_USR_DIRPATH_ID], ntServerInstallDirShort);
158     strcat(dirPathArray[AFSDIR_USR_DIRPATH_ID], AFSDIR_CANONICAL_USR_DIRPATH);
159
160 #else /* AFS_NT40_ENV */
161     /* setup the root server directory path */
162     strcpy(afsSrvDirPath, AFSDIR_CANONICAL_SERVER_AFS_DIRPATH);
163
164     /* setup the root client directory path */
165 #ifdef AFS_DARWIN_ENV
166     if (access(AFSDIR_ALTERNATE_CLIENT_VICE_DIRPATH, F_OK) == 0)
167         strcpy(afsClntDirPath, AFSDIR_ALTERNATE_CLIENT_VICE_DIRPATH);
168     else 
169 #endif
170     strcpy(afsClntDirPath, AFSDIR_CANONICAL_CLIENT_VICE_DIRPATH);
171
172     /* setup top level dirpath; valid for both server and client */
173     strcpy(dirPathArray[AFSDIR_USR_DIRPATH_ID], AFSDIR_CANONICAL_USR_DIRPATH);
174
175     initStatus |= (AFSDIR_CLIENT_PATHS_OK | AFSDIR_SERVER_PATHS_OK);
176 #endif /* AFS_NT40_ENV */
177
178   /* now initialize various dir and file paths exported by dirpath module */
179
180   /* server dir paths */
181  
182   strcpy(dirPathArray[AFSDIR_SERVER_AFS_DIRPATH_ID], afsSrvDirPath);
183
184   pathp = dirPathArray[AFSDIR_SERVER_ETC_DIRPATH_ID];
185   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_ETC_DIR);
186
187   pathp = dirPathArray[AFSDIR_SERVER_BIN_DIRPATH_ID];
188   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_BIN_DIR);
189
190   pathp = dirPathArray[AFSDIR_SERVER_CORES_DIRPATH_ID];
191   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_CORES_DIR);
192
193   pathp = dirPathArray[AFSDIR_SERVER_DB_DIRPATH_ID];
194   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_DB_DIR);
195
196   pathp = dirPathArray[AFSDIR_SERVER_LOGS_DIRPATH_ID];
197   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_LOGS_DIR);
198
199   pathp = dirPathArray[AFSDIR_SERVER_LOCAL_DIRPATH_ID];
200   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_LOCAL_DIR);
201
202   pathp = dirPathArray[AFSDIR_SERVER_BACKUP_DIRPATH_ID];
203   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_BACKUP_DIR);
204
205   pathp = dirPathArray[AFSDIR_SERVER_MIGRATE_DIRPATH_ID];
206   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_MIGR_DIR);
207
208   pathp = dirPathArray[AFSDIR_SERVER_BIN_FILE_DIRPATH_ID];
209   AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_BIN_FILE_DIR);
210
211   /* client dir path */
212
213 #ifdef AFS_NT40_ENV
214   strcpy(dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID],
215          "/NoUsrViceDirectoryOnWindows");
216   strcpy(dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID], ntClientConfigDirShort);
217 #else
218   strcpy(dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID], afsClntDirPath);
219
220   pathp = dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID];
221   AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_ETC_DIR);
222 #endif /* AFS_NT40_ENV */
223
224   /* server file paths */
225
226   pathp = dirPathArray[AFSDIR_SERVER_THISCELL_FILEPATH_ID];
227   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_THISCELL_FILE);
228
229   pathp = dirPathArray[AFSDIR_SERVER_CELLSERVDB_FILEPATH_ID];
230   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_CELLSERVDB_FILE);
231
232   pathp = dirPathArray[AFSDIR_SERVER_NOAUTH_FILEPATH_ID];
233   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NOAUTH_FILE);
234
235   pathp = dirPathArray[AFSDIR_SERVER_BUDBLOG_FILEPATH_ID];
236   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_BUDBLOG_FILE);
237
238   pathp = dirPathArray[AFSDIR_SERVER_TAPECONFIG_FILEPATH_ID];
239   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BACKUP_DIR, AFSDIR_TAPECONFIG_FILE);
240
241   pathp = dirPathArray[AFSDIR_SERVER_KALOGDB_FILEPATH_ID];
242   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_KALOGDB_FILE);
243
244   pathp = dirPathArray[AFSDIR_SERVER_KALOG_FILEPATH_ID];
245   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_KALOG_FILE);
246
247   pathp = dirPathArray[AFSDIR_SERVER_KADB_FILEPATH_ID];
248   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_KADB_FILE);
249
250   pathp = dirPathArray[AFSDIR_SERVER_NTPD_FILEPATH_ID];
251   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BIN_DIR, AFSDIR_NTPD_FILE);
252
253   pathp = dirPathArray[AFSDIR_SERVER_PRDB_FILEPATH_ID];
254   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_PRDB_FILE);
255
256   pathp = dirPathArray[AFSDIR_SERVER_PTLOG_FILEPATH_ID];
257   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_PTLOG_FILE);
258
259   pathp = dirPathArray[AFSDIR_SERVER_KCONF_FILEPATH_ID];
260   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_KCONF_FILE);
261
262   pathp = dirPathArray[AFSDIR_SERVER_VLDB_FILEPATH_ID];
263   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_VLDB_FILE);
264
265   pathp = dirPathArray[AFSDIR_SERVER_VLOG_FILEPATH_ID];
266   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_VLOG_FILE);
267
268   pathp = dirPathArray[AFSDIR_SERVER_CORELOG_FILEPATH_ID];
269   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_CORE_FILE);
270
271   pathp = dirPathArray[AFSDIR_SERVER_SLVGLOG_FILEPATH_ID];
272   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_SLVGLOG_FILE);
273
274   pathp = dirPathArray[AFSDIR_SERVER_SALVAGER_FILEPATH_ID];
275   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BIN_DIR, AFSDIR_SALVAGER_FILE);
276
277   pathp = dirPathArray[AFSDIR_SERVER_SLVGLOCK_FILEPATH_ID];
278   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_SLVGLOCK_FILE);
279
280   pathp = dirPathArray[AFSDIR_SERVER_KEY_FILEPATH_ID];
281   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_KEY_FILE);
282
283   pathp = dirPathArray[AFSDIR_SERVER_ULIST_FILEPATH_ID];
284   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_ULIST_FILE);
285
286   pathp = dirPathArray[AFSDIR_SERVER_BOZCONF_FILEPATH_ID];
287   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_BOZCONF_FILE);
288
289   pathp = dirPathArray[AFSDIR_SERVER_BOZCONFNEW_FILEPATH_ID];
290   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_BOZCONFNEW_FILE);
291
292   pathp = dirPathArray[AFSDIR_SERVER_BOZLOG_FILEPATH_ID];
293   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_BOZLOG_FILE);
294
295   pathp = dirPathArray[AFSDIR_SERVER_BOZINIT_FILEPATH_ID];
296   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_BOZINIT_FILE);
297
298   pathp = dirPathArray[AFSDIR_SERVER_BOSVR_FILEPATH_ID];
299   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BIN_DIR, AFSDIR_BOSVR_FILE);
300
301   pathp = dirPathArray[AFSDIR_SERVER_VOLSERLOG_FILEPATH_ID];
302   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_VOLSERLOG_FILE);
303
304   pathp = dirPathArray[AFSDIR_SERVER_ROOTVOL_FILEPATH_ID];
305   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_ROOTVOL_FILE);
306
307   pathp = dirPathArray[AFSDIR_SERVER_HOSTDUMP_FILEPATH_ID];
308   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_HOSTDUMP_FILE);
309
310   pathp = dirPathArray[AFSDIR_SERVER_CLNTDUMP_FILEPATH_ID];
311   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_CLNTDUMP_FILE);
312
313   pathp = dirPathArray[AFSDIR_SERVER_CBKDUMP_FILEPATH_ID];
314   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_CBKDUMP_FILE);
315
316   pathp = dirPathArray[AFSDIR_SERVER_OLDSYSID_FILEPATH_ID];
317   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_OLDSYSID_FILE);
318
319   pathp = dirPathArray[AFSDIR_SERVER_SYSID_FILEPATH_ID];
320   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_SYSID_FILE);
321
322   pathp = dirPathArray[AFSDIR_SERVER_FILELOG_FILEPATH_ID];
323   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_FILELOG_FILE);
324
325   pathp = dirPathArray[AFSDIR_SERVER_AUDIT_FILEPATH_ID];
326   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_AUDIT_FILE);
327
328   pathp = dirPathArray[AFSDIR_SERVER_NETINFO_FILEPATH_ID];
329   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NETINFO_FILE);
330
331   pathp = dirPathArray[AFSDIR_SERVER_NETRESTRICT_FILEPATH_ID];
332   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NETRESTRICT_FILE);
333
334   pathp = dirPathArray[AFSDIR_SERVER_WEIGHTING_CONSTANTS_FILEPATH_ID];
335   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR, AFSDIR_WEIGHTINGCONST_FILE);
336
337   pathp = dirPathArray[AFSDIR_SERVER_THRESHOLD_CONSTANTS_FILEPATH_ID];
338   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR, AFSDIR_THRESHOLDCONST_FILE);
339
340   pathp = dirPathArray[AFSDIR_SERVER_MIGRATELOG_FILEPATH_ID];
341   AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR, AFSDIR_MIGRATE_LOGNAME);
342  
343
344   /* client file paths */
345
346 #ifdef AFS_NT40_ENV
347   strcpy(dirPathArray[AFSDIR_CLIENT_THISCELL_FILEPATH_ID],
348          "/NoUsrViceEtcThisCellFileOnWindows");
349   sprintf(dirPathArray[AFSDIR_CLIENT_CELLSERVDB_FILEPATH_ID], "%s/%s",
350           ntClientConfigDirShort, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
351 #else
352   pathp = dirPathArray[AFSDIR_CLIENT_THISCELL_FILEPATH_ID];
353   AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_THISCELL_FILE);
354
355   pathp = dirPathArray[AFSDIR_CLIENT_CELLSERVDB_FILEPATH_ID]; 
356   AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_CELLSERVDB_FILE);
357 #endif /* AFS_NT40_ENV */
358
359   pathp = dirPathArray[AFSDIR_CLIENT_NETINFO_FILEPATH_ID];
360   AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_NETINFO_FILE);
361
362   pathp = dirPathArray[AFSDIR_CLIENT_NETRESTRICT_FILEPATH_ID];
363   AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_ETC_DIR, AFSDIR_NETRESTRICT_FILE);
364
365   initFlag = 1;  /* finished dirpath initialization */
366   return;
367 }
368
369 /* getDirPath - returns a const char pointer to the requested string
370  * from the internal path array.
371  * string_id - index into the path array 
372  */
373 const char *getDirPath(afsdir_id_t string_id)
374 {
375     /* check if the array has been initialized */
376     if (initFlag == 0) { /* no it's not, so initialize */
377 #ifdef AFS_PTHREAD_ENV
378         pthread_once(&dirInit_once, initDirPathArray);
379 #else
380         initDirPathArray();
381 #endif
382     }
383     return (const char *)dirPathArray[string_id];
384 }
385
386
387 #ifdef AFS_NT40_ENV
388 /* NT version of ConstructLocalPath() */
389
390 /*
391  * ConstructLocalPath() --  Convert a canonical (wire-format) path to a fully
392  *     specified local path.  Upon successful completion, *fullPathBufp is
393  *     set to an allocated buffer containing the fully specified local path
394  *     constructed from the cpath argument.
395  *
396  *     On NT, path construction proceeds as follows:
397  *         1) If cpath is fully qualified (i.e., starts with 'X:/') then the
398  *            path returned is equivalent to cpath.
399  *         2) If cpath begins with a drive letter but is not fully qualified,
400  *            i.e., it is drive relative, then the function fails with EINVAL.
401  *         3) If cpath begins with '/' (or '\') then the path returned is the
402  *            concatenation  AFS-server-install-dir + cpath.
403  *         4) Otherwise the path returned is the concatenation
404  *            AFS-server-install-dir + relativeTo + cpath.
405  *
406  *     Leading whitespace in cpath is ignored; the constructed path is
407  *     normalized (FilepathNormalize()).
408  *
409  * RETURN CODES: 0 if successful; errno code otherwise.
410  */
411 int
412 ConstructLocalPath(const char *cpath,
413                    const char *relativeTo,
414                    char **fullPathBufp)
415 {
416     int status = 0;
417     char *newPath = NULL;
418
419     if (initFlag == 0) { /* dirpath module not yet initialized */
420 #ifdef AFS_PTHREAD_ENV
421         pthread_once(&dirInit_once, initDirPathArray);
422 #else
423         initDirPathArray();
424 #endif
425     }
426
427     *fullPathBufp = NULL;
428
429     while (isspace(*cpath)) {
430         cpath++;
431     }
432
433     if ((((*cpath >= 'a') && (*cpath <= 'z')) ||
434          ((*cpath >= 'A') && (*cpath <= 'Z'))) &&
435         (*(cpath+1) == ':')) {
436
437         /* cpath has a leading drive letter */
438         if ((*(cpath+2) != '/') && (*(cpath+2) != '\\')) {
439             /* drive letter relative path; this is not allowed */
440             status = EINVAL;
441         } else {
442             /* fully qualified path; just make a copy */
443             newPath = (char *)malloc(strlen(cpath)+1);
444             if (!newPath) {
445                 status = ENOMEM;
446             } else {
447                 (void)strcpy(newPath, cpath);
448             }
449         }
450
451     } else {
452         /* cpath has NO leading drive letter; make relative to install dir */
453         size_t pathSize = strlen(ntServerInstallDirShort) + 2;
454
455         if ((*cpath == '/') || (*cpath == '\\')) {
456             /* construct path relative to install directory only */
457             pathSize += strlen(cpath);
458
459             newPath = (char *)malloc(pathSize);
460             if (!newPath) {
461                 status = ENOMEM;
462             } else {
463                 sprintf(newPath, "%s/%s", ntServerInstallDirShort, cpath);
464             }
465         } else {
466             /* construct path relative to 'relativeTo' (and install dir) */
467             pathSize += strlen(relativeTo) + 1 + strlen(cpath);
468
469             newPath = (char *)malloc(pathSize);
470             if (!newPath) {
471                 status = ENOMEM;
472             } else {
473                 sprintf(newPath, "%s/%s/%s",
474                         ntServerInstallDirShort, relativeTo, cpath);
475             }
476         }
477     }
478
479     if (status == 0) {
480         FilepathNormalize(newPath);
481
482         /* return buffer containing fully specified path */
483         *fullPathBufp = newPath;
484     }
485
486     return status;
487 }
488
489 #else
490 /* Unix version of ConstructLocalPath() */
491
492 /*
493  * ConstructLocalPath() --  Convert a canonical (wire-format) path to a fully
494  *     specified local path.  Upon successful completion, *fullPathBufp is
495  *     set to an allocated buffer containing the fully specified local path
496  *     constructed from the cpath argument.
497  *
498  *     On Unix, path construction proceeds as follows:
499  *         1) If cpath begins with '/' then the path returned is equivalent
500  *            to cpath.
501  *         2) Otherwise the path returned is the concatenation
502  *            relativeTo + cpath.
503  *
504  *     Leading whitespace in cpath is ignored; the constructed path is
505  *     normalized (FilepathNormalize()).
506  *
507  * RETURN CODES: 0 if successful; errno code otherwise.
508  */
509 int
510 ConstructLocalPath(const char *cpath,
511                    const char *relativeTo,
512                    char **fullPathBufp)
513 {
514     int status = 0;
515     char *newPath = NULL;
516
517     if (initFlag == 0) { /* dirpath module not yet initialized */
518 #ifdef AFS_PTHREAD_ENV
519         pthread_once(&dirInit_once, initDirPathArray);
520 #else
521         initDirPathArray();
522 #endif
523     }
524
525     *fullPathBufp = NULL;
526
527     while (isspace(*cpath)) {
528         cpath++;
529     }
530
531     if (*cpath == '/') {
532         newPath = (char *)malloc(strlen(cpath) + 1);
533         if (!newPath) {
534             status = ENOMEM;
535         } else {
536             strcpy(newPath, cpath);
537         }
538     } else {
539         newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1);
540         if (!newPath) {
541             status = ENOMEM;
542         } else {
543             sprintf(newPath, "%s/%s", relativeTo, cpath);
544         }
545     }
546
547     if (status == 0) {
548         FilepathNormalize(newPath);
549
550         /* return buffer containing fully specified path */
551         *fullPathBufp = newPath;
552     }
553
554     return status;
555 }
556 #endif /* AFS_NT40_ENV */
557
558
559 /*
560  * ConstructLocalBinPath() -- A convenience wrapper for ConstructLocalPath()
561  *     that specifies the canonical AFS server binary directory as the relative
562  *     directory.
563  */
564 int
565 ConstructLocalBinPath(const char *cpath,
566                       char **fullPathBufp)
567 {
568     return ConstructLocalPath(cpath,
569                               AFSDIR_CANONICAL_SERVER_BIN_DIRPATH,
570                               fullPathBufp);
571 }
572
573
574 /*
575  * ConstructLocalLogPath() -- A convenience wrapper for ConstructLocalPath()
576  *     that specifies the canonical AFS server logs directory as the relative
577  *     directory.
578  */
579 int
580 ConstructLocalLogPath(const char *cpath,
581                       char **fullPathBufp)
582 {
583     return ConstructLocalPath(cpath,
584                               AFSDIR_CANONICAL_SERVER_LOGS_DIRPATH,
585                               fullPathBufp);
586 }