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