problem on fonction mkdir

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!
 
I

Ian Collins

hello!i'm doning a program which can create the directory with
fonction mkdir,

You're asking in the wrong place, try a linux group or comp.unix.programmer.
 
A

Antoninus Twink

There's your problem. You're wanting to stat() the files from
the directory "dir", but the above actually stat()'s the files
from the current working directory.

Hmm? I don't see why it should.

FWIW, when I run the OP's code, everything seems to work fine:

$ ./foo
dir_0
16832
3473443
204800
i'm a directory dir_0
dir_1
16832
3473443
204800
i'm a directory dir_1
dir_2
16832
3473443
204800
i'm a directory dir_2
$
 
N

Nate Eldredge

Han from China said:
There's your problem. You're wanting to stat() the files from
the directory "dir", but the above actually stat()'s the files
from the current working directory.

Incidentally, this would have been easier to notice if you'd checked the
return value from `stat', and called `perror' in case of error.
 
F

Flash Gordon

hello!i'm doning a program which can create the directory with
fonction mkdir,

<snip>

Now for the problems which are actually to do with C.
int my_mkdir(char * name)
{
int e;
if((e=mkdir(name,0765))==-1)
{
perror("error on mkdir");
return e;

Why bother with the return in here? It does the same thing after the if
anyway.
}
return e;

}

int create_dest_rep(char *dest_rep)
{
int e,i;
char *rep_name=calloc(40,sizeof(char));

calloc initialises the allocated memory. You don't need this as you
immediately overwrite it anyway.
my_mkdir(dest_rep);
char * mrd="dir";

Why bother having mrd at all? You only use it once, and you could have
just put "dir" directly in the string simplifying things!
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 **/

Oopd. You have just returned without having freed rep_name. In fact, as
you always allocate the same amount, and it is small, you might just as
well have declared rep_name with
char rep_name[40];
Then no need to calloc/malloc and free!
Finally (on this function), in your real program, is that guaranteed to
be enough space?
}

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));

<snip>

Another pointless malloc followed by a memory leak. See comments about
the earlier one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top