-#ifdef TRACE
-static bool been_here = FALSE;
-static char my_name[] = "trace";
-
- _nc_tracing = tracelevel;
- if (! been_here && tracelevel) {
- been_here = TRUE;
-
- if (_nc_access(my_name, W_OK) < 0
- || (tracefp = fopen(my_name, "w")) == 0) {
- perror("curses: Can't open 'trace' file: ");
- exit(EXIT_FAILURE);
- }
- /* Try to set line-buffered mode, or (failing that) unbuffered,
- * so that the trace-output gets flushed automatically at the
- * end of each line. This is useful in case the program dies.
- */
-#if HAVE_SETVBUF /* ANSI */
- (void) setvbuf(tracefp, (char *)0, _IOLBF, 0);
-#elif HAVE_SETBUF /* POSIX */
- (void) setbuffer(tracefp, (char *)0);
-#endif
- _tracef("TRACING NCURSES version %s (%d)",
- NCURSES_VERSION, NCURSES_VERSION_PATCH);
+ if ((TraceFP == 0) && tracelevel) {
+ const char *mode = _nc_globals.init_trace ? "ab" : "wb";
+
+ if (TracePath[0] == '\0') {
+ if (getcwd(TracePath, sizeof(TracePath) - 12) == 0) {
+ perror("curses: Can't get working directory");
+ exit(EXIT_FAILURE);
+ }
+ strcat(TracePath, "/trace");
+ if (_nc_is_dir_path(TracePath)) {
+ strcat(TracePath, ".log");
+ }
+ }
+
+ _nc_globals.init_trace = TRUE;
+ _nc_tracing = tracelevel;
+ if (_nc_access(TracePath, W_OK) < 0
+ || (TraceFP = fopen(TracePath, mode)) == 0) {
+ perror("curses: Can't open 'trace' file");
+ exit(EXIT_FAILURE);