tmpnam.c raw

   1  #include <stdio.h>
   2  #include <fcntl.h>
   3  #include <errno.h>
   4  #include <sys/stat.h>
   5  #include <string.h>
   6  #include <stdlib.h>
   7  #include "syscall.h"
   8  #include "kstat.h"
   9  
  10  #define MAXTRIES 100
  11  
  12  char *tmpnam(char *buf)
  13  {
  14  	static char internal[L_tmpnam];
  15  	char s[] = "/tmp/tmpnam_XXXXXX";
  16  	int try;
  17  	int r;
  18  	for (try=0; try<MAXTRIES; try++) {
  19  		__randname(s+12);
  20  #ifdef SYS_lstat
  21  		r = __syscall(SYS_lstat, s, &(struct kstat){0});
  22  #else
  23  		r = __syscall(SYS_fstatat, AT_FDCWD, s,
  24  			&(struct kstat){0}, AT_SYMLINK_NOFOLLOW);
  25  #endif
  26  		if (r == -ENOENT) return strcpy(buf ? buf : internal, s);
  27  	}
  28  	return 0;
  29  }
  30