- }
-#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);
- }
-
- assert(strlen(name) < len);
- if (!(strlen(name) < len)) /* in NDEBUG is defined */
- return -1; /* buffer too small, temporary file can not be created */
-
- strcpy(fname, name);
-#ifdef _WIN32
- fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
-#else
- fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
-#endif
- }
-#endif
-
- if (fd == -1) {
- perror("Can't create temporary file");
- exit(1);
- }
-
- return fd;
-}
-
-
-/** Create a temporary file name
-*/
-char *
-tempfilename(void)
-{
- int fd;
- static char fnamebuf[PATH_MAX];
-
- if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
- fprintf(stderr, "Can't create temporary file name!");
- exit(1);
- }
-
- fd = close(fd);
- assert(fd != -1);
-
- return fnamebuf;
-}
-
-
-/** Create a temporary file and add it to tmpfileNameSet,
- so that it is removed explicitly by rm_tmpfiles()
- or implicitly at program extit.
-*/
-FILE *
-tempfile(void)
-{
- int fd;
- char *tmp;
- FILE *fp;
- char fnamebuf[PATH_MAX];
-
- if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
- fprintf(stderr, "Can't create temporary file!");
- exit(1);
- }
-
- tmp = Safe_strdup(fnamebuf);
- if (tmp)
- addSetHead(&tmpfileNameSet, tmp);
-
- if ((fp = fdopen(fd, "w+b")) == NULL) {
- perror("Can't create temporary file!");
- exit(1);
- }
-
- return fp;