aboutsummaryrefslogtreecommitdiffstats
path: root/profile.c
diff options
context:
space:
mode:
Diffstat (limited to 'profile.c')
-rw-r--r--profile.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/profile.c b/profile.c
index fe411e43..74fc4f9c 100644
--- a/profile.c
+++ b/profile.c
@@ -65,12 +65,29 @@ static long indent_level = 0;
void
set_prof_file(const char *file)
{
+ int fd;
+
assert(file != NULL);
- if (strcmp(file, "-") == 0)
+ fd = devopen_simple(file, "w", true);
+ if (fd == INVALID_HANDLE)
+ prof_fp = NULL;
+ else if (fd == fileno(stdout))
prof_fp = stdout;
+ else if (fd == fileno(stderr))
+ prof_fp = stderr;
else
- prof_fp = fopen(file, "w");
+ prof_fp = fdopen(fd, "w");
+
if (prof_fp == NULL) {
+ /* don't leak file descriptors */
+ int e = errno;
+
+ if ( fd != INVALID_HANDLE
+ && fd != fileno(stdout)
+ && fd != fileno(stderr))
+ (void) close(fd);
+
+ errno = e;
warning(_("could not open `%s' for writing: %s"),
file, strerror(errno));
warning(_("sending profile to standard error"));