mkdtemp.c raw

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