Wednesday 8 July 2015

LINUX3

Image description
/*3)display given  file  metadata using stat system call & st_mode structure

THEORY:
stat strcture:

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* inode number */
               mode_t    st_mode;        /* protection */
               nlink_t   st_nlink;       /* number of hard links */
               uid_t     st_uid;         /* user ID of owner */
               gid_t     st_gid;         /* group ID of owner */
               dev_t     st_rdev;        /* device ID (if special file) */
               off_t     st_size;        /* total size, in bytes */
               blksize_t st_blksize;     /* blocksize for filesystem I/O */
               blkcnt_t  st_blocks;      /* number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */
               struct timespec st_atim;  /* time of last access */
               struct timespec st_mtim;  /* time of last modification */
               struct timespec st_ctim;  /* time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };


st_mode structure:

st_mode to be written more concisely:

           S_ISREG(m)  is it a regular file?

           S_ISDIR(m)  directory?

           S_ISCHR(m)  character device?

           S_ISBLK(m)  block device?

           S_ISFIFO(m) FIFO (named pipe)?

           S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)

           S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)
              
PRE_WORK:
  create a file


PROGRAM:
*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<time.h>

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

      struct stat sb;
      int s;
      if(argc!=2){
            printf("PASS ARGUMENTS TO PROGRAM");     
           
      }
      if((s=stat(argv[1],&sb))1=-1){
            if(s_ISDIR(sb.st_mode)){
                  printf("%s  IS A DIRECTORY",argv(1));
                 
            }else if(S_ISBLK(sb.st_mode)){
                  printf("char.specialfile",argv[1]);
                             
            }else if(S_ISBLK(sb.st_mode)){
                  printf("BLOCK sp.file",argv[1]);
                             
            }else if(S_ISREG(sb.st_mode)){
                  printf("THE FILE IS A REGULAR FILE");    

            }else{
                  printf("UNKOWN FILE\n");           
            }
            printf("Device no %d",(int)sb.st_dev);
            printf("inode no %  %ld \n");
            printf("MODE : %l \n",(unsigned long)sb.st_mode);
            printf("no of lines :%d \n GID =%ld\n",(long)sb.st_uid,(long)sb.st_gid);
            printf("PREFFERED BLOCK SIZE %lld BYTES \n"(long long)sb.st_blocks);
            printf("file size %lld BYTES \n",ctime(&sb.st_ctime));
            printf("LAST FILE ACCESS %s",ctime(&sb.st_atime));
            printf("LAST  FILE  MODIFICATION  %s ",sb.st_mtime);
           
      }else{
            printf("CANNOT  READ PROPERTIES %s",argv[1]);
            exit(2);
      }
}

/*
COMPILATION:
gcc 3.c

EXECUTION:
./a.out file
( file is name of the file created using cat command in pre_work )


OUTPUT:











1 comment:

  1. few errors are there . . . here is an final code
    #include
    #include
    #include
    #include
    #include
    main(int argc,char *argv[]){
    struct stat sb;
    int s;
    if(argc!=2){
    printf("PASS ARGUMENTS TO PROGRAM");
    }
    if((s=stat(argv[1],&sb))!=-1){
    if(S_ISDIR(sb.st_mode)){
    printf("%s  IS A DIRECTORY \n",argv[1]);
    }else if(S_ISBLK(sb.st_mode)){
    printf("char.specialfile \n",argv[1]);
    }else if(S_ISBLK(sb.st_mode)){
    printf("BLOCK sp.file \n ",argv[1]);
    }else if(S_ISREG(sb.st_mode)){
    printf("THE FILE IS A REGULAR FILE \n");
    }else{
    printf("UNKOWN FILE\n");
    }
    printf("Device no %d \n",(int)sb.st_dev);
    printf("inode no %ld \n");
    printf("MODE : %ld \n",(unsigned long)sb.st_mode);
    printf("no of lines :%d \n GID =%ld \n",(long)sb.st_uid,(long)sb.st_gid);
    //printf("PREFFERED BLOCK SIZE %lld BYTES \n" long long sb.st_blocks);
    printf("file size %lld BYTES \n",ctime(&sb.st_ctime));
    printf("LAST FILE ACCESS %s \n",ctime(&sb.st_atime));
    printf("LAST  FILE  MODIFICATION  %s \n",sb.st_mtime);
    }else{
    printf("CANNOT  READ PROPERTIES %s \n",argv[1]);
    exit(2);
    }
    }

    output:
    [prasad@localhost ~]$ vi lablinux13.c
    [prasad@localhost ~]$ cc lablinux13.c
    [prasad@localhost ~]$ ./a.out file
    THE FILE IS A REGULAR FILE
    Device no 64769
    inode no 64769
    MODE : 33204
    no of lines :1000
    GID =1000
    file size 4298045562176 BYTES
    LAST FILE ACCESS Mon Aug 10 10:13:11 2015

    Segmentation fault (core dumped)

    ReplyDelete