scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

tmpnam.c (479B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include "../syscall.h"
      4 #undef tmpnam
      5 
      6 char *
      7 tmpnam(char *s)
      8 {
      9 	static char *tmpl, buf[L_tmpnam];
     10 	char *p;
     11 
     12 	if (*buf == '\0') {
     13 		for (tmpl = buf, p = _TMPNAME; *tmpl++ = *p++; )
     14 			;
     15 		for (p = tmpl; p < &buf[L_tmpnam-1]; ++p)
     16 			*p = '0';
     17 		*p = '\0';
     18 	}
     19 	for (;;) {
     20 		for (p = tmpl; *p && *p != '9'; ++p)
     21 			;
     22 		if (*p == '\0')
     23 			return NULL;
     24 		++*p;
     25 		if (_access(buf, 0) != 0)
     26 			break;
     27 	}
     28 	if (s)
     29 		strcpy(s, buf);
     30 	return buf;
     31 }