Skip to content

Commit

Permalink
Use mkstemp instead of tempnam
Browse files Browse the repository at this point in the history
Difference from 37bbe86: Set temporary directory path in runtime using
environment variable: "TMP" for Windows and "TMPDIR" for Unix-like.
This is because "P_tmpdir" does not expand correctly on Windows.

Temp filename examples:
- Windows 11: C:\Users\<username>\AppData\Local\Temp\epdfinfo_xxxxxx
- MSYS2 on Windows 11: <path_to_msys2>\tmp\epdfinfo_xxxxxx
- macOS Ventura:/var/folders/xx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/epdfinfo_xxxxxx

Closes vedang#110
  • Loading branch information
ywwry66 committed Sep 11, 2023
1 parent c69e765 commit e33f4e9
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions server/epdfinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ strchomp (char *str)
return str;
}

#ifdef _WIN32
#define MKTEMP_SEPARATOR "\\"
#define MKTEMP_TEMPDIR_VAR "TMP"
#else
#define MKTEMP_SEPARATOR "/"
#define MKTEMP_TEMPDIR_VAR "TMPDIR"
#endif
/**
* Create a new, temporary file and returns its name.
*
Expand All @@ -346,28 +353,21 @@ strchomp (char *str)
static char*
mktempfile()
{
char *filename = NULL;
int tries = 3;
while (! filename && tries-- > 0)
char *tmpdir = getenv(MKTEMP_TEMPDIR_VAR);
char *template = malloc(128);
strcpy(template, tmpdir);
strcat(template, MKTEMP_SEPARATOR "epdfinfo_XXXXXX");
int fd = mkstemp(template);
if (fd == -1)
{

filename = tempnam(NULL, "epdfinfo");
if (filename)
{
int fd = open(filename, O_CREAT | O_EXCL | O_RDONLY, S_IRUSR | S_IWUSR);
if (fd > 0)
close (fd);
else
{
free (filename);
filename = NULL;
}
}
fprintf (stderr, "Unable to create tempfile");
free(template);
template = NULL;
}
if (! filename)
fprintf (stderr, "Unable to create tempfile");
else
close(fd);

return filename;
return template;
}

/* Holds RGB, HSL, HSV, Lab, or Lch... but note that the order in memory for HSL
Expand Down

0 comments on commit e33f4e9

Please sign in to comment.