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