ref: 10561bd468a51e001fea28922b18d172d832d72b
parent: fb8dbbcf81b73f635d9269048a6a1e09c029c1ed
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri May 27 07:05:20 EDT 2022
libc: Simplify tmpnam() Tmpnam() was doing a runtime initialization that was not needed anymore because we can customize _TMPNAME now if it is needed, because we have its definition in a system specific header.
--- a/include/bits/darwin/sys/stdio.h
+++ b/include/bits/darwin/sys/stdio.h
@@ -3,6 +3,5 @@
#define FOPEN_MAX 16
#define TMP_MAX 25
-#define L_tmpnam 256
-
#define _TMPNAME "/tmp/tmp.0000000"
+#define L_tmpnam sizeof(_TMPNAME)
--- a/include/bits/dragonfly/sys/stdio.h
+++ b/include/bits/dragonfly/sys/stdio.h
@@ -3,6 +3,5 @@
#define FOPEN_MAX 16
#define TMP_MAX 25
-#define L_tmpnam 256
-
#define _TMPNAME "/tmp/tmp.0000000"
+#define L_tmpnam sizeof(_TMPNAME)
--- a/include/bits/linux/sys/stdio.h
+++ b/include/bits/linux/sys/stdio.h
@@ -3,6 +3,5 @@
#define FOPEN_MAX 16
#define TMP_MAX 25
-#define L_tmpnam 256
-
#define _TMPNAME "/tmp/tmp.0000000"
+#define L_tmpnam sizeof(_TMPNAME)
--- a/include/bits/netbsd/sys/stdio.h
+++ b/include/bits/netbsd/sys/stdio.h
@@ -3,6 +3,5 @@
#define FOPEN_MAX 16
#define TMP_MAX 25
-#define L_tmpnam 256
-
#define _TMPNAME "/tmp/tmp.0000000"
+#define L_tmpnam sizeof(_TMPNAME)
--- a/include/bits/openbsd/sys/stdio.h
+++ b/include/bits/openbsd/sys/stdio.h
@@ -3,6 +3,5 @@
#define FOPEN_MAX 16
#define TMP_MAX 25
-#define L_tmpnam 256
-
#define _TMPNAME "/tmp/tmp.0000000"
+#define L_tmpnam sizeof(_TMPNAME)
--- a/src/libc/stdio/tmpnam.c
+++ b/src/libc/stdio/tmpnam.c
@@ -10,16 +10,9 @@
char *
tmpnam(char *s)
{
- static char *tmpl, buf[L_tmpnam];
+ static char tmpl[] = _TMPNAME;
char *p;
- if (*buf == '\0') {
- for (tmpl = buf, p = _TMPNAME; *tmpl++ = *p++; )
- ;
- for (p = tmpl; p < &buf[L_tmpnam-1]; ++p)
- *p = '0';
- *p = '\0';
- }
for (;;) {
for (p = tmpl; *p && *p != '9'; ++p)
;
@@ -27,10 +20,10 @@
return NULL;
++*p;
- if (_access(buf, F_OK) != 0)
+ if (_access(tmpl, F_OK) != 0)
break;
}
if (s)
- strcpy(s, buf);
- return buf;
+ strcpy(s, tmpl);
+ return tmpl;
}