+static char*
+new_filename (const char* filename)
+{
+ time_t t = time (NULL);
+ int pid = process_id ();
+ char pid_buf [16];
+ char time_buf [16];
+ char *res, *d;
+ const char *p;
+ int count_dates = 0;
+ int count_pids = 0;
+ int s_date, s_pid;
+ struct tm *ts;
+ for (p = filename; *p; p++) {
+ if (*p != '%')
+ continue;
+ p++;
+ if (*p == 't')
+ count_dates++;
+ else if (*p == 'p')
+ count_pids++;
+ else if (*p == 0)
+ break;
+ }
+ if (!count_dates && !count_pids)
+ return pstrdup (filename);
+ snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
+ ts = gmtime (&t);
+ snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
+ 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
+ s_date = strlen (time_buf);
+ s_pid = strlen (pid_buf);
+ d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
+ for (p = filename; *p; p++) {
+ if (*p != '%') {
+ *d++ = *p;
+ continue;
+ }
+ p++;
+ if (*p == 't') {
+ strcpy (d, time_buf);
+ d += s_date;
+ continue;
+ } else if (*p == 'p') {
+ strcpy (d, pid_buf);
+ d += s_pid;
+ continue;
+ } else if (*p == '%') {
+ *d++ = '%';
+ continue;
+ } else if (*p == 0)
+ break;
+ *d++ = '%';
+ *d++ = *p;
+ }
+ *d = 0;
+ return res;
+}
+