- if (getenv ("TMP"))
- tmpdir = getenv ("TMP");
- else if (getenv ("TEMP"))
- tmpdir = getenv ("TEMP");
- else if (getenv ("TMPDIR"))
- tmpdir = getenv ("TMPDIR");
- if (tmpdir)
- {
- char *name = tempnam (tmpdir, "sdcc");
- if (name)
- {
- FILE *fp = fopen (name, "w+b");
- if (fp)
- {
- addSetHead (&tmpfileNameSet, name);
- }
- return fp;
- }
- return NULL;
+ int fd;
+
+ if ((tmpdir = getenv ("TMP")) == NULL)
+ if ((tmpdir = getenv ("TEMP")) == NULL)
+ tmpdir = getenv ("TMPDIR");
+
+#ifndef _WIN32
+ {
+ /* try with /usr/tmp and /tmp on Un*x systems */
+ struct stat statbuf;
+
+ if (tmpdir == NULL) {
+ if (stat("/usr/tmp", &statbuf) != -1)
+ tmpdir = "/usr/tmp";
+ else if (stat("/tmp", &statbuf) != -1)
+ tmpdir = "/tmp";
+ }
+ }
+#endif
+
+#ifdef HAVE_MKSTEMP
+ {
+ char fnamebuf[PATH_MAX];
+ size_t name_len;
+
+ if (fname == NULL || len == 0) {
+ fname = fnamebuf;
+ len = sizeof fnamebuf;
+ }
+
+ if (tmpdir) {
+ name_len = strlen(tmpdir) + 1 + TEMPLATE_LEN;
+
+ assert(name_len < len);
+ if (!(name_len < len)) /* in NDEBUG is defined */
+ return -1; /* buffer too small, temporary file can not be created */
+
+ sprintf(fname, "%s" DIR_SEPARATOR_STRING TEMPLATE, tmpdir);
+ }
+ else {
+ name_len = TEMPLATE_LEN;
+
+ assert(name_len < len);
+ if (!(name_len < len)) /* in NDEBUG is defined */
+ return -1; /* buffer too small, temporary file can not be created */
+
+ strcpy(fname, TEMPLATE);
+ }
+
+ fd = mkstemp(fname);
+ }
+#else
+ {
+ char *name = tempnam(tmpdir, "sdcc");
+
+ if (name == NULL) {
+ perror("Can't create temporary file name");
+ exit(1);