mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
Improve tmpfile api (#114)
This commit is contained in:
parent
2bd1e72d5a
commit
816b0e1851
3 changed files with 56 additions and 8 deletions
|
@ -16,10 +16,14 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/stdio/temp.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Creates a temporary file.
|
||||
|
@ -28,7 +32,13 @@
|
|||
*/
|
||||
FILE *tmpfile(void) {
|
||||
int fd;
|
||||
char template[] = "/tmp/tmp.XXXXXX";
|
||||
if ((fd = mkostemps(template, 0, 0)) == -1) return NULL;
|
||||
return fdopen(fd, "w+");
|
||||
char *tmp, *sep, tpl[PATH_MAX];
|
||||
tmp = firstnonnull(getenv("TMPDIR"), kTmpPath);
|
||||
sep = !isempty(tmp) && !endswith(tmp, "/") ? "/" : "";
|
||||
if (snprintf(tpl, PATH_MAX, "%s%stmp.XXXXXX", tmp, sep) < PATH_MAX) {
|
||||
if ((fd = mkostemps(tpl, 0, 0)) != -1) {
|
||||
return fdopen(fd, "w+");
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
40
test/libc/stdio/tmpfile_test.c
Normal file
40
test/libc/stdio/tmpfile_test.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/stdio/temp.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
TEST(tmpfile, test) {
|
||||
FILE *f;
|
||||
mkdir("doge", 0755);
|
||||
setenv("TMPDIR", "doge", true);
|
||||
f = tmpfile();
|
||||
EXPECT_NE(-1, fputc('t', f));
|
||||
EXPECT_NE(-1, fflush(f));
|
||||
rewind(f);
|
||||
EXPECT_EQ('t', fgetc(f));
|
||||
EXPECT_NE(-1, fclose(f));
|
||||
/* EXPECT_EQ(-1, rmdir("doge")); */
|
||||
/* EXPECT_EQ(ENOTEMPTY, errno); */
|
||||
}
|
8
third_party/lua/loslib.c
vendored
8
third_party/lua/loslib.c
vendored
|
@ -10,6 +10,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/weirdtypes.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/temp.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
#include "libc/time/struct/tm.h"
|
||||
|
@ -114,12 +115,9 @@
|
|||
|
||||
#define LUA_TMPNAMBUFSIZE 32
|
||||
|
||||
#if !defined(LUA_TMPNAMTEMPLATE)
|
||||
#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
|
||||
#endif
|
||||
|
||||
#define lua_tmpnam(b,e) { \
|
||||
strcpy(b, LUA_TMPNAMTEMPLATE); \
|
||||
strcpy(b, kTmpPath); \
|
||||
strcat(b, "lua_XXXXXX"); \
|
||||
e = mkstemp(b); \
|
||||
if (e != -1) close(e); \
|
||||
e = (e == -1); }
|
||||
|
|
Loading…
Reference in a new issue