xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/mkstemp.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
37c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
47c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
7*5d54f3d8Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*5d54f3d8Smuffin 
97c478bd9Sstevel@tonic-gate #include <sys/file.h>
107c478bd9Sstevel@tonic-gate 
11*5d54f3d8Smuffin int
mkstemp(char * as)12*5d54f3d8Smuffin mkstemp(char *as)
137c478bd9Sstevel@tonic-gate {
14*5d54f3d8Smuffin 	char *s;
15*5d54f3d8Smuffin 	unsigned int pid;
16*5d54f3d8Smuffin 	int fd, i;
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate 	pid = getpid();
197c478bd9Sstevel@tonic-gate 	s = as;
207c478bd9Sstevel@tonic-gate 	while (*s++)
217c478bd9Sstevel@tonic-gate 		/* void */;
227c478bd9Sstevel@tonic-gate 	s--;
237c478bd9Sstevel@tonic-gate 	while (*--s == 'X') {
247c478bd9Sstevel@tonic-gate 		*s = (pid % 10) + '0';
257c478bd9Sstevel@tonic-gate 		pid /= 10;
267c478bd9Sstevel@tonic-gate 	}
277c478bd9Sstevel@tonic-gate 	s++;
287c478bd9Sstevel@tonic-gate 	i = 'a';
297c478bd9Sstevel@tonic-gate 	while ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) == -1) {
307c478bd9Sstevel@tonic-gate 		if (i == 'z')
317c478bd9Sstevel@tonic-gate 			return (-1);
327c478bd9Sstevel@tonic-gate 		*s = i++;
337c478bd9Sstevel@tonic-gate 	}
347c478bd9Sstevel@tonic-gate 	return (fd);
357c478bd9Sstevel@tonic-gate }
36