[Balug-talk] function to get file attribute

Aaron T Porter atporter at primate.net
Wed Jun 11 13:26:02 PDT 2003


On Wed, Jun 11, 2003 at 01:15:53PM -0700, John Dave wrote:
> Is there a function in linux to get file attribute?
> Need to use it in C source code.

	John, do you even try using search engines before posting to the
list?

---
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

void main(int argc, char * argv[] ) {

/* Open file given in  argv[1] */
/* Display information from i-node  */
/* Then close file using file descriptor */

int fd ;  /* file descriptor */
struct stat buf ;
if (( fd=open(argv[1], O_RDONLY ))==-1 )
{ fprintf ( stderr, "%s: can not open %s\n", argv[0], argv[1]) ;
perror("could not open file") ;
exit (1) ;
}
printf( "Success: Opened file: %s\n", argv[1] ) ;

fstat( fd, & buf ) ;

printf( "Device of i-node: %u\n", buf.st_dev ) ;
printf( "This i-node's number: %u\n", buf.st_ino ) ;
printf( "File type and mode: %o\n",buf.st_mode ) ;
printf( "No of hardlinks: %d\n", buf.st_nlink ) ;
printf( "User id: %d\n", buf.st_uid ) ;
printf( "Group id: %d\n", buf.st_gid ) ;
printf( "Device type: %d\n", buf.st_rdev ) ;
printf( "Total size of file: %u\n", buf.st_size ) ;
printf( "File last access time: %u\n", buf.st_atime ) ;
printf( "File last modify time: %u\n", buf.st_mtime ) ;
printf( "File last status change time: %u\n", buf.st_ctime ) ;
printf( "Optimal block size: %d\n", buf.st_blksize ) ;
printf( "Actual number of blocks: %d\n", buf.st_blocks ) ;
close ( fd ) ;
}



More information about the Balug-talk-balug.org mailing list