środa, 7 listopada 2018

Class that check file (g++ Linux)

 vim f_access.cxx

#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;

class FILE_ACCESS
{
    private:
    struct stat st_file;
    char *file_name;
    int read_value;
    int write_value;
    int exist_value;
    bool is_True(int x);
    public:
    FILE_ACCESS(char *f_name);
    void Report1();
    void Report2();
};
bool FILE_ACCESS::is_True(int x)
{
    if(x==0)
     return true;
    else
     return false;
}
FILE_ACCESS::FILE_ACCESS(char *f_name)
{
    file_name=f_name;
    lstat(file_name,&st_file);
   
}
void FILE_ACCESS::Report1()
{
    exist_value=access(file_name,F_OK);
    if(is_True(exist_value))
    {
     cout<<file_name<<"  exists "<<endl;
    }
    else
    {
        if(errno==ENOENT)
        {
            cout<<file_name<<"  not exists "<<endl;
           
        }
        else if(errno==EACCES)
        {
            cout<<file_name<<"  is unavailable "<<endl;
           
        }
    }
    read_value=access(file_name,R_OK);
    if(is_True(read_value))
    {
        cout<<file_name<<" is readable "<<endl;
        cout<<"process "<< (int)getpid<<endl;
    }
    else
    {
        cout<<file_name<<" is not readable "<<endl;
    }
    write_value=access(file_name,W_OK);
    if(is_True(write_value))
    {
        cout<<file_name<<" is writetable "<<endl;
    }
    else if(errno==EACCES)
    {
        cout<<file_name<<" is not writetable  (access denied)"<<endl;
    }
    else if(errno==EROFS)
    {
        cout<<file_name<<" is not writetable (only read-only)"<<endl;
    }
}
    void FILE_ACCESS::Report2()
    {
        if(S_ISLNK(st_file.st_mode))
        {
            cout<<" symbolic link "<<endl;
        }
        else if(S_ISDIR(st_file.st_mode))
        {
            cout<<" directory "<<endl;
        }
        else if(S_ISCHR(st_file.st_mode))
        {
            cout<<" character device "<<endl;
        }
        else if(S_ISBLK(st_file.st_mode))
        {
            cout<<" block device "<<endl;
        }
        else if(S_ISFIFO(st_file.st_mode))
        {
            cout<<" FIFO queue "<<endl;
        }
        else if(S_ISSOCK(st_file.st_mode))
        {
            cout <<" socket "<<endl;
        }
        else if(S_ISREG(st_file.st_mode))
        {
            cout<<" plain file"<<endl;
        }
       
    }
   
   

int main(int argc, char **argv)
{
    FILE_ACCESS my_ACCESS1("/bin/bash"),my_ACCESS2("f_access.cxx");
    my_ACCESS1.Report1();
    my_ACCESS1.Report2();
    my_ACCESS2.Report1();
    my_ACCESS2.Report2();
   
   
   
   
    return 0;
}

Brak komentarzy:

Prześlij komentarz