int ensureDirectory (const char *path) { if (testDirectoryPath(path)) return 1; if (errno == EEXIST) { logMessage(LOG_ERR, "not a directory: %s", path); } else if (errno != ENOENT) { logMessage(LOG_ERR, "cannot access directory: %s: %s", path, strerror(errno)); } else { { char *directory = getPathDirectory(path); if (!directory) return 0; { int exists = ensureDirectory(directory); free(directory); if (!exists) return 0; } } if (createDirectory(path)) { logMessage(LOG_NOTICE, "directory created: %s", path); return 1; } } return 0; }
const char * getDeviceDirectory (void) { static const char *deviceDirectory = NULL; if (!deviceDirectory) { const char *directory = DEVICE_DIRECTORY; const size_t directoryLength = strlen(directory); static const char *const variables[] = {"DTDEVROOT", "UTDEVROOT", NULL}; const char *const *variable = variables; while (*variable) { const char *root = getenv(*variable); if (root && *root) { const size_t rootLength = strlen(root); char path[rootLength + directoryLength + 1]; snprintf(path, sizeof(path), "%s%s", root, directory); if (testDirectoryPath(path)) { if ((deviceDirectory = strdup(path))) goto found; logMallocError(); } else if (errno != ENOENT) { logMessage(LOG_ERR, "device directory error: %s (%s): %s", path, *variable, strerror(errno)); } } variable += 1; } deviceDirectory = directory; found: logMessage(LOG_DEBUG, "device directory: %s", deviceDirectory); } return deviceDirectory; }