xref: /titanic_44/usr/src/cmd/svr4pkg/libinst/setup_temporary_directory.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <assert.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <pwd.h>
40 
41 /*
42  * consolidation pkg command library includes
43  */
44 
45 #include <pkglib.h>
46 
47 /*
48  * local pkg command library includes
49  */
50 
51 #include "install.h"
52 #include "libinst.h"
53 #include "libadm.h"
54 #include "messages.h"
55 
56 /*
57  * Name:	setup_temporary_directory
58  * Description:	create a temporary directory from specified components
59  *		and return full path to the directory created
60  * Arguments:	r_dirname - pointer to handle to string - on success,
61  *			the full path to the temporary directory created
62  *			is returned in this handle
63  *		a_tmpdir - pointer to string representing the directory into
64  *			which the new temporary directory should be created
65  *		a_suffix - pointer to string representing the 5-character
66  *			suffix to be used as the first part of the temporary
67  *			directory name invented
68  * Returns:	boolean_t
69  *			== B_TRUE - temporary directory created, path returned
70  *			== B_FALSE - failed to create temporary directory
71  *				'errno' is set to the failure reason
72  * NOTE:    	Any path returned is placed in new storage for the
73  *		calling function. The caller must use 'free' to dispose
74  *		of the storage once the path is no longer needed.
75  */
76 
77 boolean_t
setup_temporary_directory(char ** r_dirname,char * a_tmpdir,char * a_suffix)78 setup_temporary_directory(char **r_dirname, char *a_tmpdir, char *a_suffix)
79 {
80 	char	*dirname;
81 
82 	/* entry assertions */
83 
84 	assert(a_tmpdir != (char *)NULL);
85 
86 	/* error if no pointer provided to return temporary name in */
87 
88 	if (r_dirname == (char **)NULL) {
89 		errno = EFAULT;			/* bad address */
90 		return (B_FALSE);
91 	}
92 
93 	/* generate temporary directory name */
94 
95 	dirname = tempnam(a_tmpdir, a_suffix);
96 	if (dirname == (char *)NULL) {
97 		return (B_FALSE);
98 	}
99 
100 	/* create the temporary directory */
101 
102 	if (mkdir(dirname, 0755) != 0) {
103 		return (B_FALSE);
104 	}
105 
106 	echoDebug(DBG_SETUP_TEMPDIR, dirname);
107 
108 	*r_dirname = dirname;
109 
110 	return (B_TRUE);
111 }
112