summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libcruft/tempnam.c
blob: 01c20b4902a48304babd0a1daff1c8d52d0909ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <dietwarning.h>

link_warning("tempnam","\e[1;33;41m>>> tempnam stinks! NEVER ! NEVER USE IT ! <<<\e[0m");

char* tempnam(char* dir,char* template) {
  char buf[1024];
  int len=sizeof(buf)-1,fd;
  buf[len]=0;
  if ((dir)&&(*dir)) {
    memccpy(buf,dir,0,len);
    strncat(buf,"/",1);
  }
  else
    strncpy(buf,"/tmp/",len);
  len=(sizeof(buf)-1)-strlen(buf);
  if (template)
    strncat(buf,template, --len);
  else
    strncat(buf,"temp_", --len);
  len=(sizeof(buf)-1)-strlen(buf);
  strncat(buf,"XXXXXX",len);
  if ((fd=mkstemp(buf))<0) return 0;
  close(fd);
  unlink(buf);
  return strdup(buf);
}