changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: Add mkstemp to stdlib.h

changeset 7378: b22ea0ca9175
parent 7377: f6b32f6f4b93
child 7379: 072514e7cf5a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon, 09 Sep 2019 15:58:39 +0100
files: sys/include/ape/stdlib.h sys/src/ape/lib/ap/gen/mkstemp.c
description: Add mkstemp to stdlib.h
q
     1.1--- a/sys/include/ape/stdlib.h
     1.2+++ b/sys/include/ape/stdlib.h
     1.3@@ -49,6 +49,10 @@ extern int wctomb(char *, wchar_t);
     1.4 extern size_t mbstowcs(wchar_t *, const char *, size_t);
     1.5 extern size_t wcstombs(char *, const wchar_t *, size_t);
     1.6 
     1.7+#ifdef _POSIX_C_SOURCE
     1.8+extern int mkstemp(char *template);
     1.9+#endif
    1.10+
    1.11 #ifdef __cplusplus
    1.12 }
    1.13 #endif
     2.1new file mode 100644
     2.2--- /dev/null
     2.3+++ b/sys/src/ape/lib/ap/gen/mkstemp.c
     2.4@@ -0,0 +1,25 @@
     2.5+#include <stdlib.h>
     2.6+#include <string.h>
     2.7+#include <unistd.h>
     2.8+
     2.9+int
    2.10+mkstemp(char *template)
    2.11+{
    2.12+   char *s;
    2.13+   int i, fd;
    2.14+
    2.15+   s = strdup(template);
    2.16+   if(s == NULL)
    2.17+       return -1;
    2.18+   for(i=0; i<20; i++){
    2.19+       strcpy(s, template);
    2.20+       mktemp(s);
    2.21+       if((fd = creat(s, 0666)) >= 0){
    2.22+           strcpy(template, s);
    2.23+           free(s);
    2.24+           return fd;
    2.25+       }
    2.26+   }
    2.27+   free(s);
    2.28+   return -1;
    2.29+}