czwartek, 30 maja 2019

Reading directory (class, C++, g++ Linux)


#include <iostream>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PP PATH_MAX+1
using namespace std;

class ls_Directory
{
private:
size_t size_path;
DIR *dir_function;
    const char *directory_path;
char main_path[PP];
struct dirent *dir_entr;
public:
ls_Directory()
{
;
}

void read_Path(const char *directory)
{
directory_path=directory;
strncpy(main_path,directory_path,sizeof(main_path));
size_path=strlen(directory_path);
char end_char='/';
size_t temp_s=size_path-1;
if(main_path[temp_s]!=end_char)
{
main_path[size_path]=end_char;
main_path[temp_s+2]='\0';
++size_path;
}
dir_function=opendir(directory_path);

}
void write_Path()
{
while(((dir_entr=readdir(dir_function))&& dir_entr!=NULL))
{
char *temp;
strncpy(main_path+size_path,dir_entr->d_name,sizeof(main_path)-size_path);
cout<<temp<<"  : "<<endl;
}
closedir(dir_function);
}
};


int main(int argc, char **argv)
{
string dir_unix[3]={"usr","lib","dev"};
ls_Directory *DIR_LINUX=new ls_Directory[3];
int i;
for(i=0;i<3;i++)
{
DIR_LINUX[i].read_Path(dir_unix[i].c_str());
DIR_LINUX[i].write_Path();
}
delete DIR_LINUX;
return 0;
}

środa, 22 maja 2019

Reading paths in C++ Builder




void __fastcall TForm1::Button1Click(TObject *Sender)
{
 char end='\0';
 char *char_v="C:";
 char *info_a="C:\\Program Files\\Embarcadero\\Studio\\18.0\\Path-After-Install.txt";
 char *info_b="C:\\Program Files\\Embarcadero\\Studio\\18.00\\Path-Before-Install.txt";
 FILE *before_path_file,*after_path_file;
 size_t bytes_val[2];
 char buf_after[512],buf_before[512];
 char *temp_after,*temp_before;
 char *info_after,*info_before;
 after_path_file=fopen(info_a,"r");
 bytes_val[0]=fread(buf_after,1,sizeof(buf_after),after_path_file);
 if(bytes_val[0]==0)
 {
  UnicodeString info; info=info_a;
  ShowMessage("Error! Can't open file"+info);
 }
 buf_after[bytes_val[0]]=end;
 temp_after=strstr(buf_after,char_v);
 sscanf(temp_after,char_v,"%s",info_after);
 before_path_file=fopen(info_b,"r");
 bytes_val[1]=fread(buf_before,1,sizeof(buf_before),before_path_file);
 if(bytes_val[1]==0)
 {
  UnicodeString info; info=info_b;
  ShowMessage("Error! Can't open file"+info);
 }
 buf_before[bytes_val[1]]=end;
 temp_before=strstr(buf_before,char_v);
 sscanf(temp_before,char_v,"%s",info_before);
 Edit1->Text=info_before;
 Edit2->Text=info_after;

}

czwartek, 9 maja 2019

table-StringGrid - C++ Builder


private: // User declarations
   bool __fastcall is_First(int value);
   int tab_First[1000];
   int date[9][9];
   void __fastcall init_First();
   void __fastcall init_Date();

bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
  ++sum;
if(sum==2)
return true;
else
     return false;
}
void __fastcall TForm1::init_First()
{
int i,j;
j=2;i=0;
do
{
  if(is_First(j))
  {
  tab_First[i]=j;
  ++i;
  }
  ++j;
}while(i<1000);
}
void __fastcall TForm1::init_Date()
{
int i,j,k,centr,left,right,s;
centr=4;
i=0;
right=i+1;
left=right+1;
do
{


  j=centr+1;
  s=1;
  do

  {


   date[i][j]=tab_First[s*(i+1)];
   ++s;
   ++j;
  }while(j<9);
  k=centr-1;
  s=1;
  do
  {
   date[i][k]=tab_First[s*(i+1)];
   ++s;
   --k;
  }while(k>-1);

  ++i;
}while(i<9);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_First();
 init_Date();
 int i,j;
 for(i=0;i<9;i++)
 {
for(j=0;j<9;j++)
{
StringGrid1->Cells[j][i]=IntToStr(date[i][j]);
     }
 }
}

środa, 8 maja 2019

FileOpen and FileSeek - C++ Builder

void __fastcall TForm1::Mtext_File(UnicodeString file_Name, TListBox *lBox,UnicodeString new_File)
{
UnicodeString result,temp_File;
ofstream n_File;
wchar_t *buff;
int i,handle,size;
temp_File=file_Name;
handle=FileOpen(temp_File.w_str(),fmOpenRead);
size=FileSeek(handle,0,2);
if(size==-1)
{
result="Error! Can't open file - "+temp_File;
ShowMessage(result);
}
else
{

FileSeek(handle,0,0);
buff=new wchar_t[size+1];
FileRead(handle,buff,size);
FileClose(handle);
result=buff;

delete buff;
lBox->Items->Add(result);
n_File.open(new_File.w_str());
n_File<<result.w_str();
n_File.close();

}

}
void __fastcall TForm1::Button1Click(TObject *Sender)
{

  Mtext_File("readme.txt",TForm1::ListBox1,"new_question.txt");
}

wtorek, 7 maja 2019

mmap/munmap gcc

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define size_Value 1000
#define size_F 0x100

int *first_Tab;
double *sqrt_Tab;
void *use_Func1;
void *use_Func2;
int file_Date;
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
 ++sum;
if(sum==2)
return 1;
else
return 0;
}
void init_File(char *name_File)
{
file_Date=open(name_File,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
lseek(file_Date,size_F+1,SEEK_SET);
write(file_Date,"",1);
lseek(file_Date,0,SEEK_SET);
}
void init_Tab()
{
int i,j;
srand(time(NULL));
first_Tab=malloc(size_Value*sizeof(int));
sqrt_Tab=malloc(size_Value*sizeof(double));
i=0;j=2;
do
{
if(is_First(j))
{
first_Tab[i]=j;
sqrt_Tab[i]=sqrt(1.0*j);
}
++j;
}while(i<size_Value);
}
int read_Int(int value)
{
return first_Tab[value];
}
int read_Double(int value)
{
return sqrt_Tab[value];
}
int random_Result()
{
int value;
value=rand()%size_Value;
}
void destroy_Tables()
{
free(sqrt_Tab);
free(first_Tab);
}

int main(int argc, char **argv)
{
int i;
char *name="test_mmap";
init_Tab();
i=random_Result();
init_File(name);
use_Func1=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
use_Func2=mmap(0,size_F,PROT_WRITE,MAP_SHARED,file_Date,0);
close(file_Date);
sprintf((char*) use_Func1,"%d\n",read_Int(i));
sprintf((char*) use_Func2,"%.2f\n",read_Double(i));
destroy_Tables();
munmap(use_Func2,size_F);
munmap(use_Func1,size_F);

return 0;
}

środa, 3 kwietnia 2019

The use of fork and exec together (gcc)

vim test_First.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int is_First(int value)
{
 int i,sum;
 sum=0;
 for(i=1;i<value+1;i++)
  if(value%i==0)
   ++sum;
 if(sum==2)
  return 1;
 else
  return 0;
}
int main(int argc, char *argv[])
{
 int i,j,k,result,sqrt_result;
 int how_many=atoi(argv[1]);
 i=0;
 j=1;
 do
 {
  if(is_First(j))
  {
   ++i;
   k=j;
  }
  ++j;
 }while(i<how_many);
 result=k;
 sqrt_result=sqrt(1.0*result);
 printf("%d,  (sqrt) = %f",result,sqrt(result));
 printf("\n");

 return 0;

}

gcc -o test_First test_First.c -lm

vim text_Opposite.c

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
 int i,j,size_Text;
 char *result;
 char *test;
 size_Text=strlen(argv[1]);
 test=malloc(sizeof(char)*size_Text);
 strcpy(test,argv[1]);
 test[size_Text+1]='\0';
 result=malloc(sizeof(char)*size_Text);

 result[size_Text]='\0';
 for(i=0;i<size_Text+1;i++)
  result[i]=test[i];
 for(i=0;i<size_Text+1;i++)
  result[i]=test[size_Text-(i+1)];
 printf("%s ",test);
 printf("\n"); 
 printf("%s ",result);
 printf("\n");
 free(test);
 free(result);
 printf("\n");


}

gcc -o text_Opposite text_Opposite.c

vim function.h

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>


int test_FE(char *text, char **arg)
{
 pid_t child_P;
 child_P=fork();
 if(child_P!=0)
  return child_P;
 else
  {
   execvp(text,arg);
   fprintf(stderr,"execvp error\n");
   abort();
  }

}

vim main_prog.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include "function.h"


int main()
{

 char *ARG1[]={
 "./test_First","10","/",NULL};
 char *ARG2[]={
 "./text_Opposite","Example","/",NULL};
 test_FE("./test_First",ARG1);
 test_FE("./text_Opposite",ARG2);
 return 0;

}

gcc -o main_prog main_prog.c
./main_prog

ś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;
}