C program demonstrating use of stat system call

#include<stdio.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <unistd.h>#include <string.h>#include <time.h>#include <stdlib.h>#define DIR1 "test/"const char dir[20]="test/";/* struct dirent {ino_t d_ino; // inode numberoff_t d_off; // offset to the next direntunsigned short d_reclen; // length of this recordunsigned char d_type; // type of file; not supportedby all file system typeschar d_name[256]; // filename};   struct stat {dev_t st_dev; // ID of device containing fileino_t st_ino; // inode numbermode_t st_mode; // protectionnlink_t st_nlink; // number of hard linksuid_t st_uid; // user ID of ownergid_t st_gid; // group ID of ownerdev_t st_rdev; // device ID (if special file)off_t st_size; // total size, in bytesblksize_t st_blksize; // blocksize for file system I/Oblkcnt_t st_blocks; // number of 512B blocks allocatedtime_t st_atime; // time of last accesstime_t st_mtime; // time of last modificationtime_t st_ctime; // time of last status change};  */ struct dirent *d1;struct stat buf;int main(){ DIR *d;d=opendir(dir);if(d==NULL){printf("nUnable to Open DIR n");exit(0);}else{while((d1=readdir(d))!=NULL){ char temp_name[30]=DIR1;strcat(temp_name,d1->d_name);//printf("nFile Name: %sn",temp_name);int a=stat(temp_name,&buf);if(a==-1){printf("nstat failedn");perror("stat");}else{printf("nFile Name: %sn",d1->d_name);printf("I-node number: %ldn", (long) buf.st_ino);printf("Mode: %lo (octal)n", (unsigned long) buf.st_mode);printf("Link count: %ldn", (long) buf.st_nlink);printf("Ownership: UID=%ld GID=%ldn",(long) buf.st_uid, (long) buf.st_gid);printf("Preferred I/O block size: %ld bytesn",(long) buf.st_blksize);printf("File size: %lld bytesn",(long long) buf.st_size);printf("Blocks allocated: %lldn",(long long) buf.st_blocks);printf("Last status change: %s", ctime(&buf.st_ctime));printf("Last file access: %s", ctime(&buf.st_atime));printf("Last file modification: %s", ctime(&buf.st_mtime));  } } }   }



Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+