toaruos/libc/stdlib/mktemp.c

24 lines
531 B
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2018-11-22 10:27:07 +03:00
#include <fcntl.h>
char * mktemp(char * template) {
if (strstr(template + strlen(template)-6, "XXXXXX") != template + strlen(template) - 6) {
errno = EINVAL;
return NULL;
}
static int _i = 0;
char tmp[7] = {0};
sprintf(tmp,"%04d%02d", getpid(), _i++);
memcpy(template + strlen(template) - 6, tmp, 6);
return template;
}
2018-11-22 10:27:07 +03:00
int mkstemp(char * template) {
mktemp(template);
return open(template, O_RDWR | O_CREAT, 0600);
}