mktemp.c raw

   1  #define _GNU_SOURCE
   2  #include <string.h>
   3  #include <stdlib.h>
   4  #include <errno.h>
   5  #include <sys/stat.h>
   6  
   7  char *mktemp(char *template)
   8  {
   9  	size_t l = strlen(template);
  10  	int retries = 100;
  11  	struct stat st;
  12  
  13  	if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
  14  		errno = EINVAL;
  15  		*template = 0;
  16  		return template;
  17  	}
  18  
  19  	do {
  20  		__randname(template+l-6);
  21  		if (stat(template, &st)) {
  22  			if (errno != ENOENT) *template = 0;
  23  			return template;
  24  		}
  25  	} while (--retries);
  26  
  27  	*template = 0;
  28  	errno = EEXIST;
  29  	return template;
  30  }
  31