[PATCH] Add barebones mktemp(1)

From: sin <sin_AT_2f30.org>
Date: Tue, 12 Nov 2013 11:52:28 +0000

---
 Makefile |  1 +
 mktemp.1 | 20 ++++++++++++++++++++
 mktemp.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 mktemp.1
 create mode 100644 mktemp.c
diff --git a/Makefile b/Makefile
index 326cae8..2a72a1c 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -57,6 +57,7 @@ SRC = \
 	mkdir.c    \
 	mkfifo.c   \
 	mknod.c    \
+	mktemp.c   \
 	mv.c       \
 	nice.c     \
 	nl.c       \
diff --git a/mktemp.1 b/mktemp.1
new file mode 100644
index 0000000..65c2ff5
--- /dev/null
+++ b/mktemp.1
_AT_@ -0,0 +1,20 @@
+	.TH MKTEMP 1 sbase\-VERSION
+.SH NAME
+mktemp \- make temporary filename
+.SH SYNOPSIS
+.B mktemp
+.RB [ \-d ]
+.RB [ template ]
+.SH DESCRIPTION
+.B mktemp
+takes the given filename template and overwrites a portion of it
+to create a unique filename.  The template may be any filename with at least
+six `Xs' appended to it.  If no template is specified a default of
+`tmp.XXXXXXXXXX' is used and the tmpdir is currently fixed to `/tmp'.
+.SH OPTIONS
+.TP
+.B \-d
+Make a directory instead of a file
+.SH SEE ALSO
+.IR mkdtemp (3),
+.IR mkstemp (3)
diff --git a/mktemp.c b/mktemp.c
new file mode 100644
index 0000000..0989931
--- /dev/null
+++ b/mktemp.c
_AT_@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "util.h"
+
+static void
+usage(void)
+{
+	eprintf("usage: %s [-d] [template]\n", argv0);
+}
+
+static int dflag = 0;
+
+int
+main(int argc, char *argv[])
+{
+	char *template = "tmp.XXXXXXXXXX";
+	char *tmpdir = "/tmp";
+	char tmppath[PATH_MAX];
+	int fd;
+
+	ARGBEGIN {
+	case 'd':
+		dflag = 1;
+		break;
+	default:
+		usage();
+	} ARGEND;
+
+	if (argc > 1)
+		usage();
+	else if (argc == 1)
+		template = argv[0];
+
+	snprintf(tmppath, sizeof(tmppath), "%s/%s", tmpdir, template);
+	if (dflag) {
+		if (!mkdtemp(tmppath))
+			eprintf("mkdtemp %s:", tmppath);
+	} else {
+		if ((fd = mkstemp(tmppath)) < 0)
+			eprintf("mkstemp %s:", tmppath);
+		close(fd);
+	}
+	puts(tmppath);
+	return EXIT_SUCCESS;
+}
-- 
1.8.3.4
--1yeeQ81UyVL57Vl7--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Nov 12 2013 - 13:12:07 CET