tmpfile.c raw

   1  #include <stdio.h>
   2  #include <fcntl.h>
   3  #include <stdlib.h>
   4  #include "stdio_impl.h"
   5  
   6  #define MAXTRIES 100
   7  
   8  FILE *tmpfile(void)
   9  {
  10  	char s[] = "/tmp/tmpfile_XXXXXX";
  11  	int fd;
  12  	FILE *f;
  13  	int try;
  14  	for (try=0; try<MAXTRIES; try++) {
  15  		__randname(s+13);
  16  		fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
  17  		if (fd >= 0) {
  18  #ifdef SYS_unlink
  19  			__syscall(SYS_unlink, s);
  20  #else
  21  			__syscall(SYS_unlinkat, AT_FDCWD, s, 0);
  22  #endif
  23  			f = __fdopen(fd, "w+");
  24  			if (!f) __syscall(SYS_close, fd);
  25  			return f;
  26  		}
  27  	}
  28  	return 0;
  29  }
  30  
  31  weak_alias(tmpfile, tmpfile64);
  32