static char *mangle_filename(const char *orig_filename) {
/* TODO: handle GCOV_PREFIX_STRIP */
- const char *prefix;
char *filename = 0;
+ int prefix_len = 0;
+ const char *prefix = getenv("GCOV_PREFIX");
- prefix = getenv("GCOV_PREFIX");
-
- if (!prefix)
+ if (!prefix || prefix[0] != '/') /* Ignore non-absolute paths */
return strdup(orig_filename);
- filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
+ prefix_len = strlen(prefix);
+ filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
strcpy(filename, prefix);
- strcat(filename, "/");
+ if (prefix[prefix_len - 1] != '/')
+ strcat(filename, "/");
strcat(filename, orig_filename);
return filename;
}
-static void recursive_mkdir(const char *filename) {
- char *pathname;
- int i, e;
+static void recursive_mkdir(char *filename) {
+ int i;
- for (i = 1, e = strlen(filename); i != e; ++i) {
+ for (i = 1; filename[i] != '\0'; ++i) {
if (filename[i] != '/') continue;
- pathname = malloc(i + 1);
- strncpy(pathname, filename, i);
- pathname[i] = '\0';
+ filename[i] = '\0';
#ifdef _WIN32
- _mkdir(pathname);
+ _mkdir(filename);
#else
- mkdir(pathname, 0750); /* some of these will fail, ignore it. */
+ mkdir(filename, 0755); /* Some of these will fail, ignore it. */
#endif
- free(pathname);
+ filename[i] = '/';
}
}
* started at a time.
*/
void llvm_gcda_start_file(const char *orig_filename) {
- char *filename;
- filename = mangle_filename(orig_filename);
- recursive_mkdir(filename);
+ char *filename = mangle_filename(orig_filename);
output_file = fopen(filename, "w+b");
if (!output_file) {
int len = strlen(orig_filename) - 1;
+ recursive_mkdir(filename);
for (; len >= 0 && orig_filename[len] != '/'; --len)
/* empty */;