static apr_status_t Fixed_apr_temp_dir_get(const char **temp_dir, apr_pool_t *p) { apr_status_t apr_err; const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" }; const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" }; char *cwd; size_t i; /* Our goal is to find a temporary directory suitable for writing into. We'll only pay the price once if we're successful -- we cache our successful find. Here's the order in which we'll try various paths: $TMP $TEMP $TMPDIR "/tmp" "/var/tmp" "/usr/tmp" `pwd` NOTE: This algorithm is basically the same one used by Python 2.2's tempfile.py module. */ /* Try the environment first. */ for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) { char *value; apr_err = apr_env_get(&value, try_envs[i], p); if ((apr_err == APR_SUCCESS) && value) { apr_size_t len = strlen(value); if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) { memcpy(global_temp_dir, value, len + 1); goto end; } } } /* Next, try a set of hard-coded paths. */ for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) { if (test_tempdir(try_dirs[i], p)) { memcpy(global_temp_dir, try_dirs[i], strlen(try_dirs[i]) + 1); goto end; } } /* Finally, try the current working directory. */ if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) { if (test_tempdir(cwd, p)) { memcpy(global_temp_dir, cwd, strlen(cwd) + 1); goto end; } } end: if (global_temp_dir[0]) { *temp_dir = apr_pstrdup(p, global_temp_dir); return APR_SUCCESS; } return APR_EGENERAL; }
APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir, apr_pool_t *p) { apr_status_t apr_err; const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" }; const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" }; char *cwd; int i; /* Our goal is to find a temporary directory suitable for writing into. We'll only pay the price once if we're successful -- we cache our successful find. Here's the order in which we'll try various paths: $TMP $TEMP $TMPDIR "C:\TEMP" (windows only) "SYS:\TMP" (netware only) "/tmp" "/var/tmp" "/usr/tmp" P_tmpdir (POSIX define) `pwd` NOTE: This algorithm is basically the same one used by Python 2.2's tempfile.py module. */ /* Try the environment first. */ for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) { char *value; apr_err = apr_env_get(&value, try_envs[i], p); if ((apr_err == APR_SUCCESS) && value) { apr_size_t len = strlen(value); if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) { memcpy(global_temp_dir, value, len + 1); goto end; } } } #ifdef WIN32 /* Next, on Win32, try the C:\TEMP directory. */ if (test_tempdir("C:\\TEMP", p)) { memcpy(global_temp_dir, "C:\\TEMP", 7 + 1); goto end; } #endif #ifdef NETWARE /* Next, on NetWare, try the SYS:/TMP directory. */ if (test_tempdir("SYS:/TMP", p)) { memcpy(global_temp_dir, "SYS:/TMP", 8 + 1); goto end; } #endif /* Next, try a set of hard-coded paths. */ for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) { if (test_tempdir(try_dirs[i], p)) { memcpy(global_temp_dir, try_dirs[i], strlen(try_dirs[i]) + 1); goto end; } } #ifdef P_tmpdir /* * If we have it, use the POSIX definition of where * the tmpdir should be */ if (test_tempdir(P_tmpdir, p)) { memcpy(global_temp_dir, P_tmpdir, strlen(P_tmpdir) +1); goto end; } #endif /* Finally, try the current working directory. */ if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) { if (test_tempdir(cwd, p)) { memcpy(global_temp_dir, cwd, strlen(cwd) + 1); goto end; } } end: if (global_temp_dir[0]) { *temp_dir = apr_pstrdup(p, global_temp_dir); return APR_SUCCESS; } return APR_EGENERAL; }
virgo_error_t* virgo__temp_dir_get(char **temp_dir) { const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" }; const char *try_envs[] = { "TMPDIR", "TMP", "TEMP"}; const char *dir; size_t i; /* Our goal is to find a temporary directory suitable for writing into. Here's the order in which we'll try various paths: $TMPDIR $TMP $TEMP "C:\TEMP" (windows only) "SYS:\TMP" (netware only) "/tmp" "/var/tmp" "/usr/tmp" P_tmpdir (POSIX define) `pwd` NOTE: This algorithm is basically the same one used by Python 2.2's tempfile.py module. */ /* Try the environment first. */ for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) { char *value; value = getenv(try_envs[i]); if (value) { size_t len = strlen(value); if (len && (len < VIRGO_PATH_MAX) && !test_tempdir(value)) { dir = value; goto end; } } } #ifdef WIN32 /* Next, on Win32, try the C:\TEMP directory. */ if (!test_tempdir("C:\\TEMP")) { dir = "C:\\TEMP"; goto end; } #endif #ifdef NETWARE /* Next, on NetWare, try the SYS:/TMP directory. */ if (!test_tempdir("SYS:/TMP")) { dir = "SYS:/TMP"; goto end; } #endif /* Next, try a set of hard-coded paths. */ for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) { if (!test_tempdir(try_dirs[i])) { dir = try_dirs[i]; goto end; } } #ifdef P_tmpdir /* * If we have it, use the POSIX definition of where * the tmpdir should be */ if (!test_tempdir(P_tmpdir)) { dir = P_tmpdir; goto end; } #endif return virgo_error_create(VIRGO_EINVAL, "Unable to detect temporary directory."); end: *temp_dir = strdup(dir); return VIRGO_SUCCESS; }