mkostemps.c raw

   1  #define _BSD_SOURCE
   2  #include <stdlib.h>
   3  #include <string.h>
   4  #include <fcntl.h>
   5  #include <unistd.h>
   6  #include <errno.h>
   7  
   8  int __mkostemps(char *template, int len, int flags)
   9  {
  10  	size_t l = strlen(template);
  11  	if (l<6 || len>l-6 || memcmp(template+l-len-6, "XXXXXX", 6)) {
  12  		errno = EINVAL;
  13  		return -1;
  14  	}
  15  
  16  	flags -= flags & O_ACCMODE;
  17  	int fd, retries = 100;
  18  	do {
  19  		__randname(template+l-len-6);
  20  		if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)
  21  			return fd;
  22  	} while (--retries && errno == EEXIST);
  23  
  24  	memcpy(template+l-len-6, "XXXXXX", 6);
  25  	return -1;
  26  }
  27  
  28  weak_alias(__mkostemps, mkostemps);
  29  weak_alias(__mkostemps, mkostemps64);
  30