summaryrefslogblamecommitdiff
path: root/src/temp/mktemp.c
blob: 67130e1677e65b92fe13d4da3314eb4e8e864aa3 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   


                   
                     
 

                         
                            
 
                                    

                          
 
                                                         
                               

                                
         

            
                                         





                                                           
                      
                       
                        
 
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>

char *__randname(char *);

char *mktemp(char *template)
{
	size_t l = strlen(template);
	int retries = 100;
	struct stat st;

	if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
		errno = EINVAL;
		*template = 0;
		return template;
	}

	do {
		__randname(template+l-6);
		if (stat(template, &st)) {
			if (errno != ENOENT) *template = 0;
			return template;
		}
	} while (--retries);

	*template = 0;
	errno = EEXIST;
	return template;
}