* A bit of cleanup of the 'recursive_mkdir'. No functionality change.
authorBill Wendling <isanbard@gmail.com>
Mon, 28 May 2012 09:41:48 +0000 (09:41 +0000)
committerBill Wendling <isanbard@gmail.com>
Mon, 28 May 2012 09:41:48 +0000 (09:41 +0000)
* Check for absolute paths before using the GCOV_PREFIX.
* Don't add an ending path separator if there's already one.

llvm-svn: 157571

compiler-rt/lib/profile/GCDAProfiling.c

index d4a720c..1126787 100644 (file)
@@ -68,37 +68,35 @@ static void write_string(const char *s) {
 
 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] = '/';
   }
 }
 
@@ -111,13 +109,12 @@ static void recursive_mkdir(const char *filename) {
  * 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 */;