środa, 13 marca 2019

Wriiting class with an argument - writev (Linux/g++)

#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
using namespace std;



class WRITE_ARGS
{
private:
char *file_args;
char *if_Exists;
char *if_Readable;
char *which_Type;
char *now_Date;
time_t temp_Time;
public:
WRITE_ARGS(char *file_test, char *save_Info)
{
int temp_rval;


temp_Time=time(NULL);
now_Date=asctime(localtime(&temp_Time));
file_args=save_Info;
temp_rval=access(file_test,R_OK);
if(temp_rval==0)
if_Readable="writable";
else if(errno==EACCES)
if_Readable="not access";
else if(errno==EROFS)
if_Readable="read only";
temp_rval=access(file_test,F_OK);
if(temp_rval==0)
{
if_Exists="file exists";
struct stat stat_Value;
lstat(file_test,&stat_Value);
if(S_ISDIR(stat_Value.st_mode))
 which_Type="directory";
else if(S_ISSOCK(stat_Value.st_mode))
 which_Type="socket";
else if(S_ISFIFO(stat_Value.st_mode))
 which_Type="queue FIFO";
else if(S_ISCHR(stat_Value.st_mode))
 which_Type="character device";
else if(S_ISBLK(stat_Value.st_mode))
 which_Type="block device";
else if(S_ISREG(stat_Value.st_mode))
 which_Type="normal file";
else
 which_Type="unknown type";

}
else
{
if(errno==EACCES)
if_Exists="is not access";
if(errno==ENOENT)
{
if_Exists="not exists";
which_Type="not exists";
  } 
}


}  
void save_Values()
{
int i,write_file;
char *info_Date[4];
struct iovec* io_value;
struct iovec* io_next;
char end='\n';
info_Date[0]=now_Date;
info_Date[1]=if_Exists;
info_Date[2]=if_Readable;
info_Date[3]=which_Type;
io_value=(struct iovec*)malloc(8*sizeof(struct iovec));
io_next=io_value;
for(i=0;i<4;i++)
{
io_next->iov_base=info_Date[i];
io_next->iov_len=strlen(info_Date[i]);
++io_next;
io_next->iov_base=&end;
io_next->iov_len=1;
++io_next;
}
write_file=open(file_args,O_WRONLY | O_CREAT);
writev(write_file,io_value,8);
close(write_file);
free(io_value);

}


};

int main(int argc, char **argv)
{
WRITE_ARGS wr_ARGS("/proc/version","my_report");
WRITE_ARGS wr_ARGS2("mm_cpu.c","report_cpu");
wr_ARGS.save_Values();
wr_ARGS2.save_Values();
return 0;
}