Friday, 22 November 2013

DISPLAY FILE ATTRIBUTES

#include<sys/stat.h>
#include<pwd.h>
#include<time.h>
#include<stdio.h>
int main()
{
    int s;
    char source[30],flag='y';
    struct stat filestat;
    struct passwd *pwd;
    printf("\nEnter the file name:- ");
    gets(source);
    s=stat(source,&filestat);
    if(s!=-1)
    {
        printf("\nOwner id: %d",filestat.st_uid);
      if((pwd = getpwuid(filestat.st_uid)) != NULL)
  {
  printf("\tname: %s", pwd->pw_name);
}
    printf("\nFile Size: %ld bytes",filestat.st_size);
    printf("\nFile Permission (Octal Notation): %o", filestat.st_mode);
   
printf("\nFile Permissions (Symbolic Notation): \t");
printf( (S_ISDIR(filestat.st_mode))      ?    "d"  : "-" );
printf( (filestat.st_mode & S_IRUSR)  ?    "r"   : "-" );
printf( (filestat.st_mode & S_IWUSR) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXUSR)  ?    "x"  : "-" );
printf( (filestat.st_mode & S_IRGRP)  ?    "r"   : "-" );
printf( (filestat.st_mode & S_IWGRP) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXGRP)  ?    "x"  : "-" );
printf( (filestat.st_mode & S_IROTH)  ?    "r"  : "-" );
printf( (filestat.st_mode & S_IWOTH) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXOTH)  ?   "x"  : "-" );

    printf("\nDate of creation: %s",ctime(&filestat.st_ctime));
    printf("Last Modified Date: %s",ctime(&filestat.st_mtime));
  printf("\n");
    }
    else
        printf("\nError!!!");
    return 0;
}

No comments:

Post a Comment