E
exallion.long
hello!i'm doning a program which can create the directory with
fonction mkdir,
all the things works,but strangely when i use I_ISDIR(st.mode) to
check each directory,some of them are not recognize as the
directory,and value of st.mode equal to 0.can someone explain it to me
please? i hope it's not my system's problem,i'm program on gcc
4.2.4,and i'm on a machine linux.here is my program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int my_mkdir(char * name)
{
int e;
if((e=mkdir(name,0765))==-1)
{
perror("error on mkdir");
return e;
}
return e;
}
int create_dest_rep(char *dest_rep)
{
int e,i;
char *rep_name=calloc(40,sizeof(char));
my_mkdir(dest_rep);
char * mrd="dir";
for(i=0;i<3;i++)
{
sprintf(rep_name,"%s/%s_%d",dest_rep,mrd,i);
my_mkdir(rep_name);
}
return 1; /** if all ok return 1 **/
}
int main(int argc,char ** argv){
create_dest_rep("dir");
DIR * rep;
if((rep = opendir("dir"))==NULL){
printf("probleme on open dir \n");
return 1;
}
struct dirent *entry;
struct stat *buf=malloc(sizeof(struct stat));
while((entry=readdir(rep))!=NULL)
{
stat(entry->d_name,buf); /** fetch the information of ino */
if(entry->d_name[0]!='.') /** no hiden files . .. */
{
printf("%s\n",entry->d_name);
printf("%d\n",buf->st_mode);
printf("%d\n",buf->st_ino);
printf("%d\n",buf->st_size);
if(S_ISDIR(buf->st_mode)) /** if it's a directory*/
{
printf("i'm a directory %s\n",entry->d_name);
}
if(S_ISREG(buf->st_mode))
{
printf("i'm a normal file%s\n",entry->d_name); /** normal file*/
}
}
}
closedir(rep);
return 0;
}
thank you!
fonction mkdir,
all the things works,but strangely when i use I_ISDIR(st.mode) to
check each directory,some of them are not recognize as the
directory,and value of st.mode equal to 0.can someone explain it to me
please? i hope it's not my system's problem,i'm program on gcc
4.2.4,and i'm on a machine linux.here is my program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int my_mkdir(char * name)
{
int e;
if((e=mkdir(name,0765))==-1)
{
perror("error on mkdir");
return e;
}
return e;
}
int create_dest_rep(char *dest_rep)
{
int e,i;
char *rep_name=calloc(40,sizeof(char));
my_mkdir(dest_rep);
char * mrd="dir";
for(i=0;i<3;i++)
{
sprintf(rep_name,"%s/%s_%d",dest_rep,mrd,i);
my_mkdir(rep_name);
}
return 1; /** if all ok return 1 **/
}
int main(int argc,char ** argv){
create_dest_rep("dir");
DIR * rep;
if((rep = opendir("dir"))==NULL){
printf("probleme on open dir \n");
return 1;
}
struct dirent *entry;
struct stat *buf=malloc(sizeof(struct stat));
while((entry=readdir(rep))!=NULL)
{
stat(entry->d_name,buf); /** fetch the information of ino */
if(entry->d_name[0]!='.') /** no hiden files . .. */
{
printf("%s\n",entry->d_name);
printf("%d\n",buf->st_mode);
printf("%d\n",buf->st_ino);
printf("%d\n",buf->st_size);
if(S_ISDIR(buf->st_mode)) /** if it's a directory*/
{
printf("i'm a directory %s\n",entry->d_name);
}
if(S_ISREG(buf->st_mode))
{
printf("i'm a normal file%s\n",entry->d_name); /** normal file*/
}
}
}
closedir(rep);
return 0;
}
thank you!