char * tmpnam(char *s) { static char name_buffer[L_tmpnam] = "/tmp/tmp."; static unsigned long count = 0; static char *name = NULL; if (!name) { name = name_buffer + strlen(name_buffer); name = _i_compute((unsigned long)getpid(), 10, name, 5); *name++ = '.'; *name = '\0'; } if (++count > TMP_MAX) count = 1; /* wrap-around */ *_i_compute(count, 10, name, 3) = '\0'; if (s) return strcpy(s, name_buffer); else return name_buffer; }
char * _i_compute(unsigned long val, int base, char *s, int nrdigits) { int c; c= val % base ; val /= base ; if (val || nrdigits > 1) s = _i_compute(val, base, s, nrdigits - 1); *s++ = (c>9 ? c-10+'a' : c+'0'); return s; }
char * _i_compute(printval_u_t val, int base, char *s, int nrdigits) { int c; div_u64_rem(val, base, (u32*)&c); // val % base ; val = div_u64(val, base); // val /= base if (val || nrdigits > 1) s = _i_compute(val, base, s, nrdigits - 1); *s++ = (c>9 ? c-10+'a' : c+'0'); return s; }
FILE * tmpfile(void) { static char name_buffer[L_tmpnam] = "/tmp/tmp." ; static char *name = NULL; FILE *file; if (!name) { name = name_buffer + strlen(name_buffer); name = _i_compute(getpid(), 10, name, 5); *name = '\0'; } file = fopen(name_buffer,"wb+"); if (!file) return (FILE *)NULL; (void) remove(name_buffer); return file; }