Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:37:48+00:00 2026-05-27T15:37:48+00:00

The function below basically emulates mkdir -p , recursively creating directories for a given

  • 0

The function below basically emulates mkdir -p, recursively creating directories for a given path. With Linux I have no issues, however running under OSX it always segfaults with the error pointer being freed was not allocated during the call to free(dir). Can anyone spot an error? When I step through execution in gdb I don’t see any obvious problems, dir is populated and the directory structure is created without error.

static int
mkpath(const char *path)
{
  int result = 0;
  struct stat st;
  char *p = NULL, *dir = strdup(path);
  char *tmp = g_malloc0(sizeof(char) * strlen(cache.path) + strlen(dir) + 1);

  dir = dirname(dir);
  tmp = strcpy(tmp, cache.path);
  p = strtok(dir, "/");
  while(p != NULL) {
    tmp = strncat(tmp, "/", 1);
    tmp = strncat(tmp, p, strlen(p));

    if(stat(tmp, &st) == 0) {
      if(S_ISDIR(st.st_mode)) {
        p = strtok(NULL, "/");
        continue;
      }

      result = -ENOTDIR;
      break;
    }

    if(mkdir(tmp, S_IRWXU) == -1) {
      result = -errno;
      break;
    }

    p = strtok(NULL, "/");
  }

  free(tmp);
  free(dir);

  return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:37:48+00:00Added an answer on May 27, 2026 at 3:37 pm

    Take a look at the man page for dirname: http://linux.die.net/man/3/dirname. “Both dirname() and basename() return pointers to null-terminated strings. (Do not pass these pointers to free(3).)” Also, you should probably not being doing dir = dirname(dir) as you’ve then lost the pointer to the memory allocated by strdup (strdup allocated memory should be passed to free).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have the following function below: public function setupHead($title){ $displayHead .='<!DOCTYPE html PUBLIC -//W3C//DTD
I have an example function below that reads in a date as a string
I have the function below that copies a file in a directory and recreate
I have the below function public static List<DateTime> GetOnlyFridays(DateTime endDate, int weeks, bool isIncludeBaseDate)
Basically I tried evaluating the function below but keep getting the following error: WARNING
I have the below jquery function $(function(){ $(select#rooms).change(function(){ $.getJSON(/admin/select.php,{id: $(this).val(), ajax: 'true'}, function(j){ var
Below I have some code which basically animates a div from the bottom of
I have a function below which is called to reload a recaptcha image. It
I have the jquery below which basically pre-populates a text box. (It actually populates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.