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