xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/mkstemp.c (revision 4a634bb80136cc001d14ab96addd9915105e5223)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #pragma ident	"%Z%%M%	%I%	%E% SMI"
8 
9 #include <sys/file.h>
10 
11 int
12 mkstemp(char *as)
13 {
14 	char *s;
15 	unsigned int pid;
16 	int fd, i;
17 
18 	pid = getpid();
19 	s = as;
20 	while (*s++)
21 		/* void */;
22 	s--;
23 	while (*--s == 'X') {
24 		*s = (pid % 10) + '0';
25 		pid /= 10;
26 	}
27 	s++;
28 	i = 'a';
29 	while ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) == -1) {
30 		if (i == 'z')
31 			return (-1);
32 		*s = i++;
33 	}
34 	return (fd);
35 }
36