xref: /titanic_41/usr/src/lib/libast/common/comp/mktemp.c (revision bfc032a14cc866ab7f34ca6fd86c240a5ebede9d)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * mktemp,mkstemp implementation
25  */
26 
27 #define mktemp		______mktemp
28 #define mkstemp		______mkstemp
29 
30 #include <ast.h>
31 #include <stdio.h>
32 
33 #undef	mktemp
34 #undef	mkstemp
35 
36 #undef	_def_map_ast
37 #include <ast_map.h>
38 
39 #if defined(__EXPORT__)
40 #define extern	__EXPORT__
41 #endif
42 
43 static char*
44 temp(char* buf, int* fdp)
45 {
46 	char*	s;
47 	char*	d;
48 	int	n;
49 	size_t	len;
50 
51 	len = strlen(buf);
52 	if (s = strrchr(buf, '/'))
53 	{
54 		*s++ = 0;
55 		d = buf;
56 	}
57 	else
58 	{
59 		s = buf;
60 		d = "";
61 	}
62 	if ((n = strlen(s)) < 6 || strcmp(s + n - 6, "XXXXXX"))
63 		*buf = 0;
64 	else
65 	{
66 		*(s + n - 6) = 0;
67 		if (!pathtemp(buf, len, d, s, fdp))
68 			*buf = 0;
69 	}
70 	return buf;
71 }
72 
73 extern char*
74 mktemp(char* buf)
75 {
76 	return temp(buf, NiL);
77 }
78 
79 extern int
80 mkstemp(char* buf)
81 {
82 	int	fd;
83 
84 	return *temp(buf, &fd) ? fd : -1;
85 }
86