1*0Sstevel@tonic-gate /*- 2*0Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Copyright (c) 1998 5*0Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*0Sstevel@tonic-gate */ 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate #include "config.h" 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate #ifndef lint 13*0Sstevel@tonic-gate static const char sccsid[] = "@(#)os_tmpdir.c 10.3 (Sleepycat) 10/13/98"; 14*0Sstevel@tonic-gate #endif /* not lint */ 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 17*0Sstevel@tonic-gate #include <sys/types.h> 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate #include <errno.h> 20*0Sstevel@tonic-gate #include <stdlib.h> 21*0Sstevel@tonic-gate #endif 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate #include "db_int.h" 24*0Sstevel@tonic-gate #include "common_ext.h" 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate #ifdef macintosh 27*0Sstevel@tonic-gate #include <TFileSpec.h> 28*0Sstevel@tonic-gate #endif 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * __os_tmpdir -- 32*0Sstevel@tonic-gate * Set the temporary directory path. 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate * The order of items in the list structure and the order of checks in 35*0Sstevel@tonic-gate * the environment are documented. 36*0Sstevel@tonic-gate * 37*0Sstevel@tonic-gate * PUBLIC: int __os_tmpdir __P((DB_ENV *, u_int32_t)); 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate int 40*0Sstevel@tonic-gate __os_tmpdir(dbenv, flags) 41*0Sstevel@tonic-gate DB_ENV *dbenv; 42*0Sstevel@tonic-gate u_int32_t flags; 43*0Sstevel@tonic-gate { 44*0Sstevel@tonic-gate /* 45*0Sstevel@tonic-gate * !!! 46*0Sstevel@tonic-gate * Don't change this to: 47*0Sstevel@tonic-gate * 48*0Sstevel@tonic-gate * static const char * const list[] 49*0Sstevel@tonic-gate * 50*0Sstevel@tonic-gate * because it creates a text relocation in position independent code. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate static const char * list[] = { 53*0Sstevel@tonic-gate "/var/tmp", 54*0Sstevel@tonic-gate "/usr/tmp", 55*0Sstevel@tonic-gate "/temp", /* Windows. */ 56*0Sstevel@tonic-gate "/tmp", 57*0Sstevel@tonic-gate "C:/temp", /* Windows. */ 58*0Sstevel@tonic-gate "C:/tmp", /* Windows. */ 59*0Sstevel@tonic-gate NULL 60*0Sstevel@tonic-gate }; 61*0Sstevel@tonic-gate const char * const *lp, *p; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* Use the environment if it's permitted and initialized. */ 64*0Sstevel@tonic-gate p = NULL; 65*0Sstevel@tonic-gate #ifdef HAVE_GETEUID 66*0Sstevel@tonic-gate if (LF_ISSET(DB_USE_ENVIRON) || 67*0Sstevel@tonic-gate (LF_ISSET(DB_USE_ENVIRON_ROOT) && getuid() == 0)) 68*0Sstevel@tonic-gate #else 69*0Sstevel@tonic-gate if (LF_ISSET(DB_USE_ENVIRON)) 70*0Sstevel@tonic-gate #endif 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate if ((p = getenv("TMPDIR")) != NULL && p[0] == '\0') { 73*0Sstevel@tonic-gate __db_err(dbenv, "illegal TMPDIR environment variable"); 74*0Sstevel@tonic-gate return (EINVAL); 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate /* Windows */ 77*0Sstevel@tonic-gate if (p == NULL && (p = getenv("TEMP")) != NULL && p[0] == '\0') { 78*0Sstevel@tonic-gate __db_err(dbenv, "illegal TEMP environment variable"); 79*0Sstevel@tonic-gate return (EINVAL); 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate /* Windows */ 82*0Sstevel@tonic-gate if (p == NULL && (p = getenv("TMP")) != NULL && p[0] == '\0') { 83*0Sstevel@tonic-gate __db_err(dbenv, "illegal TMP environment variable"); 84*0Sstevel@tonic-gate return (EINVAL); 85*0Sstevel@tonic-gate } 86*0Sstevel@tonic-gate /* Macintosh */ 87*0Sstevel@tonic-gate if (p == NULL && 88*0Sstevel@tonic-gate (p = getenv("TempFolder")) != NULL && p[0] == '\0') { 89*0Sstevel@tonic-gate __db_err(dbenv, 90*0Sstevel@tonic-gate "illegal TempFolder environment variable"); 91*0Sstevel@tonic-gate return (EINVAL); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate #ifdef macintosh 96*0Sstevel@tonic-gate /* Get the path to the temporary folder. */ 97*0Sstevel@tonic-gate if (p == NULL) { 98*0Sstevel@tonic-gate FSSpec spec; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate if (!Special2FSSpec(kTemporaryFolderType, 101*0Sstevel@tonic-gate kOnSystemDisk, 0, &spec)) 102*0Sstevel@tonic-gate (void)__os_strdup(FSp2FullPath(&spec), &p); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate #endif 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* Step through the list looking for a possibility. */ 107*0Sstevel@tonic-gate if (p == NULL) 108*0Sstevel@tonic-gate for (lp = list; *lp != NULL; ++lp) 109*0Sstevel@tonic-gate if (__os_exists(p = *lp, NULL) == 0) 110*0Sstevel@tonic-gate break; 111*0Sstevel@tonic-gate if (p == NULL) 112*0Sstevel@tonic-gate return (0); 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate return (__os_strdup(p, &dbenv->db_tmp_dir)); 115*0Sstevel@tonic-gate } 116