io_opt.c bug fix

pull/5/head
lichhaosysu 2016-01-29 14:42:19 +08:00
parent 0165a3357b
commit e1d140c7e5
1 changed files with 17 additions and 11 deletions

View File

@ -19,10 +19,10 @@ int mkdir_by_cascading(const char *path,mode_t mode)
{ {
int length,pointer_postion = 0; int length,pointer_postion = 0;
char *postion; char *postion;
char *path_temp = path; // some compiler reports warning without(char *)
char *path_temp = (char *)path;
char cwd[MAX_PATH_SIZE]; char cwd[MAX_PATH_SIZE];
char *subfolder; char *subfolder;
int is_error = 0;
int result = 0; int result = 0;
if(NULL == getcwd(cwd,sizeof(cwd))) if(NULL == getcwd(cwd,sizeof(cwd)))
{ {
@ -41,7 +41,7 @@ int mkdir_by_cascading(const char *path,mode_t mode)
while(pointer_postion != strlen(path_temp)) while(pointer_postion != strlen(path_temp))
{ {
postion = strchr(path_temp+pointer_postion,'/'); postion = strchr(path_temp+pointer_postion,'/');
if(0 == postion) if(NULL == postion)
{ {
length = strlen(path_temp) - pointer_postion; length = strlen(path_temp) - pointer_postion;
} }
@ -61,12 +61,16 @@ int mkdir_by_cascading(const char *path,mode_t mode)
memcpy(subfolder,path_temp+pointer_postion,length); memcpy(subfolder,path_temp+pointer_postion,length);
if(is_dir(subfolder)) if(is_dir(subfolder))
{ {
// if subfolder exists, do not create, just chdir to it
if(-1 == chdir(subfolder)) if(-1 == chdir(subfolder))
{ {
result = -2; result = -2;
break; break;
} }
} }
else
{
// if subfolder not exists, creates it
if(-1 == mkdir(subfolder,mode)) if(-1 == mkdir(subfolder,mode))
{ {
result = -4; result = -4;
@ -77,6 +81,8 @@ int mkdir_by_cascading(const char *path,mode_t mode)
result = -2; result = -2;
break; break;
} }
}
}while(0); }while(0);
if(NULL != subfolder) if(NULL != subfolder)