From 8cc4c6cb0349bcc7d17f9f5b92b27f49aa714fa8 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 29 Mar 2013 14:29:11 +0100 Subject: [PATCH] Add tmpfile(3). --- libc/Makefile | 1 + libc/include/stdio.h | 2 +- libc/tmpfile.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 libc/tmpfile.cpp diff --git a/libc/Makefile b/libc/Makefile index 5b8e02b4..02a5b458 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -325,6 +325,7 @@ sys/socket/socketpair.o \ system.o \ tfork.o \ time.o \ +tmpfile.o \ truncateat.o \ truncate.o \ ttyname.o \ diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 871586d4..d1a4b86a 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -117,6 +117,7 @@ extern int sortix_puts(const char* str); extern int sprintf(char* restrict s, const char* restrict format, ...); extern int scanf(const char* restrict format, ...); extern int sscanf(const char* restrict s, const char* restrict format, ...); +extern FILE* tmpfile(void); extern int ungetc(int c, FILE* stream); extern int vfprintf(FILE* restrict stream, const char* restrict format, __gnuc_va_list ap); extern int vfscanf(FILE* restrict stream, const char* restrict format, __gnuc_va_list arg); @@ -131,7 +132,6 @@ extern int vsscanf(const char* restrict s, const char* restrict format, __gnuc_v extern char* ctermid(char* s); extern FILE *fmemopen(void* restrict buf, size_t size, const char* restrict mode); extern FILE* open_memstream(char** bufp, size_t* sizep); -extern FILE* tmpfile(void); extern int dprintf(int fildes, const char* restrict format, ...); extern int fgetpos(FILE* restrict stream, fpos_t* restrict pos); extern int fsetpos(FILE* stream, const fpos_t* pos); diff --git a/libc/tmpfile.cpp b/libc/tmpfile.cpp new file mode 100644 index 00000000..749e7d49 --- /dev/null +++ b/libc/tmpfile.cpp @@ -0,0 +1,39 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + tmpfile.cpp + Opens an unnamed temporary file. + +*******************************************************************************/ + +#include + +#include +#include + +extern "C" FILE* tmpfile() +{ + char name[5 + sizeof(pid_t) * 3]; + snprintf(name, sizeof(name), "/tmp/%ju", (uintmax_t) getpid()); + FILE* ret = fopen(name, "w+"); + if ( !ret ) + return NULL; + unlink(name); + return ret; +}