linux 创建多级目录

在linux在如果创建目录的中间路径不存在,则会创建失败,下面给一份创建多级目录的代码。


递归:

#include "stdio.h"
#include "string.h"
#include <sys/stat.h> 
#include <unistd.h> 

#define PATH	"ggyy1/ggyy2/ggyy3/ggyy4/ggyy5"
#define PATH1	"ggyy1/gg yy2/ggyy3/ggyy4/ggyy5/ggyy1/ggyy2/ggyy3/ggyy4/ggyy5"


#ifndef FALSE
  #define FALSE 0
#endif
#ifndef TRUE
  #define TRUE  1
#endif
typedef unsigned char ubyte;
typedef ubyte bool_t;


bool_t Logm_IsFileExist(const char *path);
int Logm_CheckAndCreateDir(const char* path);


int main()
{
  printf("ggyy\n");


  if(0 == Logm_CheckAndCreateDir(PATH1))
  {
    printf("create success\n");  
  }


  return 0;
}


bool_t Logm_IsFileExist(const char *path)
{
  return access(path, F_OK) == 0 ? TRUE : FALSE;
}


int Logm_CheckAndCreateDir(const char* path)
{
  char path_tmp[1024];
  char upper_path_temp[1024];
  char* p = NULL;


  memset(path_tmp, 0, sizeof(path_tmp));
  memset(upper_path_temp, 0, sizeof(upper_path_temp));


  /*check param*/
  if(NULL==path || strlen(path)<=0 || strlen(path)>=1024)
  {
      return -1;
  }


  strncpy(path_tmp, path, sizeof(path_tmp)-1);


  /*dir no exist*/
  if(!Logm_IsFileExist(path_tmp))
  {
    /*get upper dir*/
    strncpy(upper_path_temp, path_tmp, sizeof(upper_path_temp)-1);
    p = strrchr(upper_path_temp, '/');


    /*if upper dir exist*/
    if(NULL != p)
    {
      p[0] = 0;
      /*check and create upper dir*/
      if(0 != Logm_CheckAndCreateDir(upper_path_temp))
      {
        return -1;
      }
    }


    /*create current dir*/
    if (mkdir(path_tmp, ((S_IRWXU |S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))) == 0)
    {
      printf("create dir:%s successfully", path_tmp);
      return 0;
    }
    else
    {
      return -1;
    }
  }
  else
  {
    return 0;
  }
}

循环:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <sys/stat.h> 
#include <unistd.h> 


#define PATH	"ggyy1/ggyy2/ggyy3/ggyy4/ggyy5"
#define PATH1	"/ggyy1/gg yy2/ggyy3/ggyy4/ggyy5/ggyy1/ggyy2/ggyy3/ggyy4/ggyy5/"


#ifndef FALSE
  #define FALSE 0
#endif
#ifndef TRUE
  #define TRUE  1
#endif
typedef unsigned char ubyte;
typedef ubyte bool_t;


bool_t Logm_IsFileExist(const char *path);
int Logm_CheckAndCreateDir(const char* path);


int main()
{
  printf("ggyy\n");

  if(0 == Logm_CheckAndCreateDir(PATH1))
  {
    printf("create success\n");  
  }

  return 0;
}

bool_t Logm_IsFileExist(const char *path)
{
  return access(path, F_OK) == 0 ? TRUE : FALSE;
}
int Logm_CheckAndCreateDir(const char* path)
{
  const int buffLen = 1024;
  char pathTemp[buffLen];
  char upperPathTemp[buffLen];
  char* p = NULL;
  int i = 0;
  int cnt = 1;
  int result = -1;


  memset(pathTemp, 0, sizeof(pathTemp));
  memset(upperPathTemp, 0, sizeof(upperPathTemp));


  /*check param*/
  if(NULL==path || strlen(path)<=0 || strlen(path)>=buffLen)
  {
    result = -1;
  }
  else
  {
    while(1)
    {
      strncpy(pathTemp, path, sizeof(pathTemp)-1);
      p = pathTemp;
      for(i=0; i<cnt; i++)
      {
        p = strchr(p, '/');
        if(p)
        {
          p += 1;
        }
      }
      
      if(p)
      {
        p[0] = 0;
        if(!Logm_IsFileExist(pathTemp))
        {
          if (mkdir(pathTemp, ((S_IRWXU |S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))) == 0)
          {
            printf("create dir:%s successfully", pathTemp);
          }
          else
          {
            printf("mkdir create dir:%s failed", pathTemp);
            result = -1;
            break;
          }
        }
        cnt++;
      }
      else
      {
        if(!Logm_IsFileExist(pathTemp))
        {
          if (mkdir(pathTemp, ((S_IRWXU |S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))) == 0)
          {
            printf("create dir:%s successfully", pathTemp);
          }
          else
          {
            printf("mkdir create dir:%s failed", pathTemp);
            result = -1;
            break;
          }
        }     
        break;
      }
    }
  }


  return result;
}


猜你喜欢

转载自blog.csdn.net/qq229596421/article/details/80733816
今日推荐