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