scc

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

commit 61aee6d755efe40baee53f12242cb3199949fc8e
parent 720fe1d5f39c96fe6b8a334ba50132375d02658b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 Dec 2017 18:49:53 +0100

[lib/c] Add tmpnam()

Diffstat:
Mlib/c/src/Makefile | 1+
Mlib/c/src/syscall.h | 4++++
Alib/c/src/tmpnam.c | 32++++++++++++++++++++++++++++++++
3 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -5,6 +5,7 @@ include ../../../config.mk 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 \ diff --git a/lib/c/src/syscall.h b/lib/c/src/syscall.h @@ -5,6 +5,10 @@ extern int _read(int fd, void *buf, size_t n); 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); diff --git a/lib/c/src/tmpnam.c b/lib/c/src/tmpnam.c @@ -0,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; +}