shithub: scc

Download patch

ref: eccc5be2a0755da4d604a9e3079426253ee7a980
parent: 34bc68e7ade75c1627c7de6cbaa605601f2479fe
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Dec 10 13:49:53 EST 2017

[lib/c] Add tmpnam()

--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -5,6 +5,7 @@
 OBJ = bsearch.o qsort.o \
       abs.o __abs.o labs.o __labs.o llabs.o __labs.o \
       perror.o strerror.o \
+      tmpnam.o \
       sprintf.o snprintf.o vsprintf.o vsnprintf.o \
       printf.o fprintf.o vfprintf.o \
       fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \
--- a/lib/c/src/syscall.h
+++ b/lib/c/src/syscall.h
@@ -5,6 +5,10 @@
 extern int _write(int fd, void *buf, size_t n);
 extern int _lseek(int fd, long off, int whence);
 extern void _Exit(int status);
+extern void _access(char *path, int mode);
+
 extern int raise(int sig);
 extern void (*signal(int sig, void (*func)(int)))(int);
 extern getenv(const char *var);
+extern int rename(const char *from, const char *to);
+extern int remove(const char *path);
--- /dev/null
+++ b/lib/c/src/tmpnam.c
@@ -1,0 +1,32 @@
+
+#include <stdio.h>
+#include <string.h>
+#include "syscall.h"
+#undef tmpnam
+
+char *
+tmpnam(char *s)
+{
+	static char *tmpl, buf[L_tmpnam];
+	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)
+			;
+		if (*p == '\0')
+			return NULL;
+		++*p;
+		if (_access(buf, 0) != 0)
+			break;
+	}
+	if (s)
+		strcpy(s, buf);
+	return buf;
+}