dirpath: fix macos alternate client etc dir support
[openafs.git] / src / util / dirpath.c
index ca116b1..55bf251 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
+#include <afs/opr.h>
 
 #include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
 #include <ctype.h>
 #include <limits.h>
-#include <errno.h>
-#include <stdio.h>
-#include "assert.h"
+
 #include "afsutil.h"
 #include "fileutil.h"
+
+#include <afs/opr.h>
+
 #ifdef AFS_PTHREAD_ENV
 #include <pthread.h>
 static pthread_once_t dirInit_once = PTHREAD_ONCE_INIT;
 #endif
+
 #ifdef AFS_NT40_ENV
-#include <windows.h>
 #include <WINNT\afssw.h>
 #endif
-#ifdef AFS_DARWIN_ENV
-#include <unistd.h>
-#endif
 
 /* local vars */
 /* static storage for path strings */
@@ -60,16 +58,16 @@ static void initDirPathArray(void);
 /* Additional macros for ease of use */
 /* buf is expected to be atleast AFS_PATH_MAX bytes long */
 #define AFSDIR_SERVER_DIRPATH(buf, dir)  \
-            (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, NULL)
+            (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, (char *)NULL)
 
 #define AFSDIR_SERVER_FILEPATH(buf, dir, file)  \
-            (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, "/", file,  NULL)
+            (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, "/", file,  (char *)NULL)
 
 #define AFSDIR_CLIENT_DIRPATH(buf, dir)  \
-            (void) strcompose(buf, AFSDIR_PATH_MAX, clientPrefix, dir, NULL)
+            (void) strcompose(buf, AFSDIR_PATH_MAX, clientPrefix, dir, (char *)NULL)
 
 #define AFSDIR_CLIENT_FILEPATH(buf, dir, file)  \
-            (void) strcompose(buf, AFSDIR_PATH_MAX,  clientPrefix, dir, "/", file,  NULL)
+            (void) strcompose(buf, AFSDIR_PATH_MAX,  clientPrefix, dir, "/", file,  (char *)NULL)
 
 
 /* initAFSDirPath() -- External users call this function to initialize
@@ -89,7 +87,7 @@ initAFSDirPath(void)
 }
 
 
-/* initDirPathArray() -- Initializes the afs dir paths for the 
+/* initDirPathArray() -- Initializes the afs dir paths for the
  *     server and client installations.
  *
  *     For NT these are determined dynamically; for Unix they are static.
@@ -105,7 +103,7 @@ initAFSDirPath(void)
 static void
 initDirPathArray(void)
 {
-    char *pathp;
+    char *pathp, *clntEtcDir;
     const char *clientPrefix = "";
     const char *serverPrefix = "";
 
@@ -230,7 +228,7 @@ initDirPathArray(void)
 #else
     strcpy(dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID], afsClntDirPath);
 
-    pathp = dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID];
+    clntEtcDir = pathp = dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID];
 #ifdef AFS_DARWIN_ENV
     if (access(AFSDIR_ALTERNATE_CLIENT_ETC_DIR, F_OK) == 0)
        AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_ALTERNATE_CLIENT_ETC_DIR);
@@ -241,7 +239,12 @@ initDirPathArray(void)
 
 #ifndef AFS_NT40_ENV
     pathp = dirPathArray[AFSDIR_CLIENT_DATA_DIRPATH_ID];
-    AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_DATA_DIR);
+#ifdef AFS_DARWIN_ENV
+    if (access(AFSDIR_ALTERNATE_CLIENT_DATA_DIR, F_OK) == 0)
+       AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_ALTERNATE_CLIENT_DATA_DIR);
+    else
+#endif
+       AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_DATA_DIR);
 #endif
 
     /* server file paths */
@@ -328,6 +331,9 @@ initDirPathArray(void)
     pathp = dirPathArray[AFSDIR_SERVER_BOZINIT_FILEPATH_ID];
     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSCONFIG_DIR, AFSDIR_BOZINIT_FILE);
 
+    pathp = dirPathArray[AFSDIR_SERVER_BOZRXBIND_FILEPATH_ID];
+    AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSCONFIG_DIR, AFSDIR_BOZRXBIND_FILE);
+
     pathp = dirPathArray[AFSDIR_SERVER_BOSVR_FILEPATH_ID];
     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSSERVER_DIR, AFSDIR_BOSVR_FILE);
 
@@ -391,24 +397,20 @@ initDirPathArray(void)
           "/NoCellAliasOnWindows");
 #else
     pathp = dirPathArray[AFSDIR_CLIENT_THISCELL_FILEPATH_ID];
-    AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
-                          AFSDIR_THISCELL_FILE);
+    AFSDIR_CLIENT_FILEPATH(pathp, clntEtcDir, AFSDIR_THISCELL_FILE);
 
     pathp = dirPathArray[AFSDIR_CLIENT_CELLSERVDB_FILEPATH_ID];
-    AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
-                          AFSDIR_CELLSERVDB_FILE);
+    AFSDIR_CLIENT_FILEPATH(pathp, clntEtcDir, AFSDIR_CELLSERVDB_FILE);
 
     pathp = dirPathArray[AFSDIR_CLIENT_CELLALIAS_FILEPATH_ID];
-    AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
-                          AFSDIR_CELLALIAS_FILE);
+    AFSDIR_CLIENT_FILEPATH(pathp, clntEtcDir, AFSDIR_CELLALIAS_FILE);
 #endif /* AFS_NT40_ENV */
 
     pathp = dirPathArray[AFSDIR_CLIENT_NETINFO_FILEPATH_ID];
-    AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR, AFSDIR_NETINFO_FILE);
+    AFSDIR_CLIENT_FILEPATH(pathp, clntEtcDir, AFSDIR_NETINFO_FILE);
 
     pathp = dirPathArray[AFSDIR_CLIENT_NETRESTRICT_FILEPATH_ID];
-    AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
-                          AFSDIR_NETRESTRICT_FILE);
+    AFSDIR_CLIENT_FILEPATH(pathp, clntEtcDir, AFSDIR_NETRESTRICT_FILE);
 
     initFlag = 1;              /* finished dirpath initialization */
     return;
@@ -416,7 +418,7 @@ initDirPathArray(void)
 
 /* getDirPath - returns a const char pointer to the requested string
  * from the internal path array.
- * string_id - index into the path array 
+ * string_id - index into the path array
  */
 const char *
 getDirPath(afsdir_id_t string_id)
@@ -472,7 +474,7 @@ static void
 LocalizePathHead(const char **path, const char **relativeTo)
 {
      struct canonmapping *map;
+
      if (**path == '/') {
         for (map = CanonicalTranslations; map->local != NULL; map++) {
             int canonlength = strlen(map->canonical);
@@ -550,12 +552,9 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
            status = EINVAL;
        } else {
            /* fully qualified path; just make a copy */
-           newPath = (char *)malloc(strlen(cpath) + 1);
-           if (!newPath) {
+           newPath = strdup(cpath);
+           if (!newPath)
                status = ENOMEM;
-           } else {
-               (void)strcpy(newPath, cpath);
-           }
        }
 
     } else {
@@ -566,7 +565,7 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
            /* construct path relative to install directory only */
            pathSize += strlen(cpath);
 
-           newPath = (char *)malloc(pathSize);
+           newPath = malloc(pathSize);
            if (!newPath) {
                status = ENOMEM;
            } else {
@@ -576,7 +575,7 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
            /* construct path relative to 'relativeTo' (and install dir) */
            pathSize += strlen(relativeTo) + 1 + strlen(cpath);
 
-           newPath = (char *)malloc(pathSize);
+           newPath = malloc(pathSize);
            if (!newPath) {
                status = ENOMEM;
            } else {
@@ -639,20 +638,12 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
 
     LocalizePathHead(&cpath, &relativeTo);
     if (*cpath == '/') {
-       newPath = (char *)malloc(strlen(cpath) + 1);
-       if (!newPath) {
-           status = ENOMEM;
-       } else {
-           strcpy(newPath, cpath);
-       }
+       newPath = strdup(cpath);
     } else {
-       newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1);
-       if (!newPath) {
-           status = ENOMEM;
-       } else {
-           sprintf(newPath, "%s/%s", relativeTo, cpath);
-       }
+       asprintf(&newPath, "%s/%s", relativeTo, cpath);
     }
+    if (newPath == NULL)
+       status = ENOMEM;
 
     if (status == 0) {
        FilepathNormalize(newPath);