/** * Copyright (C) 2008 Seapeak.Xu / xvhfeng@gmail.com * * FastLib may be copied only under the terms of the GNU General * Public License V3, which may be found in the FastLib source kit. * Please visit the FastLib Home Page http://www.csource.org/ for more detail. **/ #include #include #include #include #include #include #include "io_opt.h" int mkdir_by_cascading(const char *path,mode_t mode) { int length,pointer_postion = 0; char *postion; char *path_temp = path; char cwd[MAX_PATH_SIZE]; char *subfolder; int is_error = 0; int result = 0; if(NULL == getcwd(cwd,sizeof(cwd))) { return -1; } if(*path_temp == '/') { if(-1 == chdir("/")) { return -2; } pointer_postion ++; } while(pointer_postion != strlen(path_temp)) { postion = strchr(path_temp+pointer_postion,'/'); if(0 == postion) { length = strlen(path_temp) - pointer_postion; } else { length = postion - path_temp - pointer_postion; } do { subfolder = (char *)calloc(length,sizeof(char)); if(NULL == subfolder) { result = -3; break; } memcpy(subfolder,path_temp+pointer_postion,length); if(is_dir(subfolder)) { if(-1 == chdir(subfolder)) { result = -2; break; } } if(-1 == mkdir(subfolder,mode)) { result = -4; break; } if(-1 == chdir(subfolder)) { result = -2; break; } }while(0); if(NULL != subfolder) { free(subfolder); subfolder = NULL; } pointer_postion += 0 == postion ? length : length + 1; } return result; } int is_dir(const char *dir_path) { struct stat buf; if (0 != stat(dir_path, &buf)) { return 0; } return S_ISDIR(buf.st_mode); }