I'm studying c programming in linux,and I wrote this to output information about the files and directories like the standard tool "ls" with "-l",everything works fine except the macro S_ISDIR,Here is my code. Additionally,my os is mint 14 x86_64.
#include<sys/types.h>
#include<time.h>
#include<string.h>
#include<dirent.h>
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
void do_ls(char []);
void show_file_info(struct stat *t){
printf("mode: %o\n",t->st_mode);
if(S_ISDIR(t->st_mode)==0)
printf("Is a dir\n");
else
printf("Is not a dir\n");
printf("links: %d\n",t->st_nlink);
printf("group: %d\n",t->st_gid);
printf("user: %d\n",t->st_uid);
printf("size: %d\n",t->st_size);
printf("modtime: %s\n",ctime(&t->st_mtime));
}
int main(int num,char *a[]){
if(num==1){
do_ls(".");
}
else{
while(--num){
printf("%s :\n",*++a);
do_ls(*a);
}
}
}
void do_ls(char dirname[]){
DIR *tem=opendir(dirname);
struct dirent *direntp;
struct stat *buf;
char t[256];
if(tem==NULL){
fprintf(stderr,"ls: cannot open %s\n",dirname);
}
else{
while((direntp=readdir(tem))!=NULL){
strcpy(t,dirname);
printf("%s\n",direntp->d_name);
strcat(t,"/");
if(stat(t,buf)==-1){
perror("");
break;
}
else{
show_file_info(buf);
}
}
closedir(tem);
}
}
The corrections of user1198331 about stat
was right. It is a good practice to generally check all syscalls return values in order to prevent errors.
Still, in your original code, I think this part was wrong:
if(S_ISDIR(t->st_mode)==0)
printf("Is a dir\n");
else
printf("Is not a dir\n");
You consider that if S_ISDIR(t->st_mode) returns 0, it is a directory, but actually, S_ISDIR(t->st_mode) returns 0 if the file pointed to by t is not a directory. You, thus, have to do the reverse check.