Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mkstemp instead of tempnam #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 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,25 @@ strchomp (char *str)
static char*
mktempfile()
{
char *filename = NULL;
int tries = 3;
while (! filename && tries-- > 0)
char *template = malloc(256);
char *tmpdir = getenv(MKTEMP_TEMPDIR_VAR);
if (tmpdir != NULL) {
strcpy(template, tmpdir);
} else {
strcpy(template, P_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