shithub: scc

Download patch

ref: 95552e35a889ea98c511ffc72ebc62d9fd48121a
parent: fb8dbbcf81b73f635d9269048a6a1e09c029c1ed
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri May 27 19:56:15 EDT 2022

libc: Add tmpfile() for posix

This version is not fully portable because it relays in the
fact that a process can unllink an open file that will be
removed from the hard disk once the process finishes.

--- a/include/bits/darwin/sys.h
+++ b/include/bits/darwin/sys.h
@@ -3,9 +3,11 @@
 #define O_RDWR    0x00000002
 #define O_ACCMODE 0x00000003
 
+#define O_CLOEXEC 0x00400000
+#define O_EXCL    0x00000800
 #define O_TRUNC   0x00000400
-#define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
+#define O_APPEND  0x00000008
 
 #define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 1000000)
--- a/include/bits/dragonfly/sys.h
+++ b/include/bits/dragonfly/sys.h
@@ -3,9 +3,11 @@
 #define O_RDWR    0x00000002
 #define O_ACCMODE 0x00000003
 
+#define O_CLOEXEC 0x00400000
+#define O_EXCL    0x00000800
 #define O_TRUNC   0x00000400
-#define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
+#define O_APPEND  0x00000008
 
 #define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 128)
--- a/include/bits/linux/sys.h
+++ b/include/bits/linux/sys.h
@@ -3,8 +3,10 @@
 #define O_RDWR    0x00000002
 #define O_ACCMODE 0x00000003
 
+#define O_CLOEXEC 0x00080000
 #define O_TRUNC   0x00000200
 #define O_APPEND  0x00000400
+#define O_EXCL    0x00000080
 #define O_CREAT   0x00000040
 
 #define AT_FDCWD  -100
--- a/include/bits/netbsd/sys.h
+++ b/include/bits/netbsd/sys.h
@@ -3,9 +3,11 @@
 #define O_RDWR    0x00000002
 #define O_ACCMODE 0x00000003
 
+#define O_CLOEXEC 0x00400000
+#define O_EXCL    0x00000800
 #define O_TRUNC   0x00000400
-#define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
+#define O_APPEND  0x00000008
 
 #define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 100)
--- a/include/bits/openbsd/sys.h
+++ b/include/bits/openbsd/sys.h
@@ -3,9 +3,11 @@
 #define O_RDWR    0x00000002
 #define O_ACCMODE 0x00000003
 
+#define O_CLOEXEC 0x00010000
+#define O_EXCL    0x00000800
 #define O_TRUNC   0x00000400
-#define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
+#define O_APPEND  0x00000008
 
 #define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 100)
--- a/src/libc/arch/posix/Makefile
+++ b/src/libc/arch/posix/Makefile
@@ -15,6 +15,7 @@
 	signal.$O\
 	system.$O\
 	time.$O\
+	tmpfile.$O\
 
 all: $(OBJS)
 
--- /dev/null
+++ b/src/libc/arch/posix/tmpfile.c
@@ -1,0 +1,21 @@
+#include <stdio.h>
+
+#include "../../syscall.h"
+
+#undef tmpfile
+
+FILE *
+tmpfile(void)
+{
+	char *fname;
+	FILE *fp;
+
+	for (;;) {
+		if ((fname = tmpnam(NULL)) == NULL)
+			return NULL;
+		if ((fp = fopen(fname, "wt+")) == NULL)
+			continue;
+		_unlink(fname);
+		return fp;
+	}
+}
--- a/src/libc/objs/amd64-linux.mk
+++ b/src/libc/objs/amd64-linux.mk
@@ -47,4 +47,5 @@
 	arch/posix/signal.$O\
 	arch/posix/system.$O\
 	arch/posix/time.$O\
+	arch/posix/tmpfile.$O\
 	string/strlen.$O\
--- a/src/libc/objs/amd64-netbsd.mk
+++ b/src/libc/objs/amd64-netbsd.mk
@@ -39,4 +39,5 @@
 	arch/posix/signal.$O\
 	arch/posix/system.$O\
 	arch/posix/time.$O\
+	arch/posix/tmpfile.$O\
 	string/strlen.$O\
--- a/src/libc/objs/amd64-openbsd.mk
+++ b/src/libc/objs/amd64-openbsd.mk
@@ -43,4 +43,5 @@
 	arch/posix/signal.$O\
 	arch/posix/system.$O\
 	arch/posix/time.$O\
+	arch/posix/tmpfile.$O\
 	string/strlen.$O\
--- a/src/libc/stdio/_fpopen.c
+++ b/src/libc/stdio/_fpopen.c
@@ -12,9 +12,10 @@
         const char *restrict mode,
         FILE * restrict fp)
 {
-	int i, flags, fd, rw, bin;
+	int i, flags, fd, rw, bin, rights;;
 
 	flags = rw = bin = 0;
+	rights = 0666;
 
 	if (mode[0] == '\0')
 		goto einval;
@@ -31,6 +32,10 @@
 				goto einval;
 			bin = 1;
 			break;
+		case 't':
+			flags |= O_EXCL | O_CLOEXEC;
+			rights = 0600;
+			break;
 		default:
 			goto einval;
 		}
@@ -46,7 +51,7 @@
 		flags |= (rw) ? O_RDWR : O_WRONLY;
 		break;
 	case 'r':
-		flags = (rw) ? O_RDWR : O_RDONLY;
+		flags |= (rw) ? O_RDWR : O_RDONLY;
 		break;
 	default:
 	einval:
@@ -54,7 +59,7 @@
 		return NULL;
 	}
 
-	if ((fd = _open(fname, flags, 0666)) < 0)
+	if ((fd = _open(fname, flags, rights)) < 0)
 		return NULL;
 
 	fp->buf = NULL;