2 * $Id: mktemp.c,v 1.3 2001-06-29 14:14:46 rufustfirefly Exp $
4 * Copyright (c) 1987 Regents of the University of California.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that: (1) source distributions retain this entire copyright
9 * notice and comment, and (2) distributions including binaries display
10 * the following acknowledgement: ``This product includes software
11 * developed by the University of California, Berkeley and its contributors''
12 * in the documentation or other materials provided with the distribution
13 * and in all advertising materials mentioning features or use of this
14 * software. Neither the name of the University nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #endif /* HAVE_CONFIG_H */
26 #if defined(LIBC_SCCS) && !defined(lint)
27 static char sccsid[] = "@(#)mktemp.c 5.9 (Berkeley) 6/1/90";
28 #endif /* LIBC_SCCS and not lint */
30 static int _mktemp_dummy;
34 #include <sys/types.h>
46 return (_gettemp(path, &fd) ? fd : -1);
53 return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
57 _gettemp(path, doopen)
62 register char *start, *trv;
67 for (trv = path; *trv; ++trv); /* extra X's get set to 0's */
68 while (*--trv == 'X') {
69 *trv = (pid % 10) + '0';
74 * check the target directory; if you have six X's and it
75 * doesn't exist this runs for a *very* long time.
77 for (start = trv + 1;; --trv) {
82 if (stat(path, &sbuf))
84 if (!S_ISDIR(sbuf.st_mode)) {
96 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
101 else if (stat(path, &sbuf))
102 return(errno == ENOENT ? 1 : 0);
104 /* tricky little algorithm for backward compatibility */
105 for (trv = start;;) {