summaryrefslogtreecommitdiff
path: root/libc/misc/tmpnam.c
diff options
context:
space:
mode:
authorRobert de Bath <rdebath@poboxes.com>1998-02-01 11:26:21 +0100
committerLubomir Rintel <lkundrak@v3.sk>2013-10-23 23:40:14 +0200
commit48f0b3eb836162d41622cedc1eb5f5168168fb8e (patch)
treec53156383d4682a0a296f6611575dbc1d64d1881 /libc/misc/tmpnam.c
parent48798bf2eb93ec3b99720ac2e16093441156653d (diff)
downloaddev86-48f0b3eb836162d41622cedc1eb5f5168168fb8e.tar.gz
Import Dev86src-0.13.5.tar.gzv0.13.5
Diffstat (limited to 'libc/misc/tmpnam.c')
-rw-r--r--libc/misc/tmpnam.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/libc/misc/tmpnam.c b/libc/misc/tmpnam.c
new file mode 100644
index 0000000..5bb72c5
--- /dev/null
+++ b/libc/misc/tmpnam.c
@@ -0,0 +1,50 @@
+/*
+ * (C) Shane Kerr <kerr@wizard.net> under terms of LGPL
+ */
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#ifndef P_tmpdir
+#define P_tmpdir "/tmp"
+#endif
+
+#ifndef L_tmpnam
+#define L_tmpnam 20
+#endif
+
+char *tmpnam(s)
+char *s;
+{
+ static char ret_val[L_tmpnam];
+ static char c1 = 0;
+ static char c2 = 0;
+ static char c3 = 0;
+ static char uniq_ch[] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ struct stat stbuf;
+
+ do {
+ sprintf(ret_val, "%s/%05d%c%c%c", P_tmpdir, getpid(),
+ uniq_ch[c1], uniq_ch[c2], uniq_ch[c3]);
+ if (++c1 >= 62) {
+ c1 = 0;
+ if (++c2 >= 62) {
+ c2 = 0;
+ if (++c3 >= 62) {
+ errno = EEXIST;
+ return 0;
+ }
+ }
+ }
+ } while (stat(ret_val, &stbuf) == 0);
+
+ if (s != 0) {
+ strcpy(s, ret_val);
+ return s;
+ } else {
+ return ret_val;
+ }
+}
+