Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <dirent.h>
- #include <limits.h>
- void histogram(char** argv){
- int n=0;
- char s[7];
- strcpy(s, argv[2]);
- n=atoi(s);
- if(n!=2 && n!=4 && n!=8 && n!=1)
- printf("\nERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
- else {
- char path[50];
- strcpy(path, argv[3]);
- int fd=open(path, O_RDONLY);
- if(fd==-1){
- perror("Invalid path");
- return ;
- }
- char p1, p[1000];
- int nr, v[1000];
- for(int i=0;i<1000;++i) v[i]=0;
- int i=n, pu=1<<n, sum=0, nrb;
- pu--;
- while((nr=read(fd, &p1, 1))){
- int j=0;
- sum=0;
- while(j<8/nr){
- // if(p1=='1' && i){
- i--;
- pu=1;
- if(i && p1=='1'){
- int cop=i;
- while(cop) pu*=2, cop--;
- sum+=pu;
- }
- if(i==0){
- i=n;
- if(p1=='1') sum++;
- v[sum]++;
- sum=0; pu=1;
- }
- j++;
- p1>>=1;
- /* int x=(1<<n)-1;
- int y=p1 & x;
- v[y]++; */
- /* j++;
- nrb=p1&pu;
- p1=p1>>n;
- v[nrb]++;
- } */
- }
- }
- printf("SUCCESS\n");
- for(i=0;i<1000;++i)
- if(v[i]){
- for(int j=n-1;j>=0;--j){
- int k=i>>j;
- if(k & 1==1) printf("1");
- else printf("0");
- }
- printf(": %d\n", v[i]);
- }
- }
- }
- void runs(char** argv){
- char path[50];
- strcpy(path, argv[2]);
- int fd=open(path, O_RDONLY);
- if(fd==-1){
- perror("Invalid file path");
- return ;
- }
- int nr, i=0, f, prev=0, imax=0, byte=0, bit=0, j=0, k=0, rb1, rb2;
- char p;
- while((nr=read(fd, &p, 1))){
- bit++;
- if(bit%8==0) bit=0, byte++;
- if(p=='1') {
- i++;
- if(i>imax) {
- prev=imax;
- imax=i;
- rb1=byte;
- rb2=bit;
- f=j;
- }
- }
- else i=0;
- j++;
- }
- f=f-imax+1;
- rb2=f%8;
- rb1=f/8;
- printf("SUCCESS\nLength of the longest run: %d\n", imax);
- printf("Offset: %d bytes + %d bits\n", rb1, rb2);
- }
- void template_(char** argv){
- int n, nr=0, cr=3;
- n=atoi(argv[2]);
- if(argv[5]!=0) cr=4, nr=atoi(argv[3]);
- char t_path[50], path[50];
- strcpy(t_path, argv[cr]);
- strcpy(path, argv[++cr]);
- int fd=open(path, O_RDONLY);
- if(fd==-1){
- perror("ERROR\nInvalid file path");
- return ;
- }
- int fd1=open(t_path, O_RDONLY);
- if(fd1==-1){
- perror("ERROR\nInvalid template file");
- return ;
- }
- if(nr<0) printf("ERROR\nInvalid template number");
- if(n!=8 && n!=16 && n!=24 && n!=32)
- printf("ERROR\nInvalid bits number");
- else {
- char sablon[50], p[6];
- int vers=0, i=0, nrtmp=0, dim, nrcat=0;
- read(fd1, &vers, 2);
- //printf("%d", vers);
- if(vers<12345 || vers>54321){
- printf("ERROR\nWrong version\n");
- return ;
- }
- read(fd1, &nrcat, 2);
- int ok=0, ofs, cop=nrcat, t;
- i=0;
- while((t=read(fd1, &dim, 2))){
- i++;
- if(dim==n){
- read(fd1, &nrtmp, 2);
- read(fd1, &ofs, 4);
- // printf("%d ", nrtmp);
- break;
- }
- nrcat--;
- lseek(fd1, 6, SEEK_CUR);
- }
- if(!nrcat){
- printf("ERROR\nInvalid template category\n");
- return ;
- }
- // printf("%d\n", nrtmp);
- if(nrtmp<nr){
- printf("ERROR\nInvalid template number\n");
- return ;
- }
- nr--;
- char pattern[10000];
- char patt;
- int j, v[100], sz=n/8;
- ok=0;
- int nrc=nrtmp*i;
- if(ok){
- printf("ERROR\nOverlaying catgories");
- return ;
- }
- lseek(fd1, ofs, SEEK_SET);
- printf("\nSUCCESS\n");
- int nrp=0, val, tem, prez=0;
- if(nr!=-1){
- nr+=2;
- lseek(fd1, n*nr+ofs, SEEK_SET);
- int ss=lseek(fd, 0, SEEK_END);
- read(fd1, &val, sz);
- lseek(fd, 0, SEEK_SET);
- ss--;
- for(int k=ss;k>=sz;--k){
- read(fd, &tem, 2*sz);
- ok=0;
- nrp=0;
- int cop1=val, cop2=tem, q=1, cop11, cop22;
- for(int j=1;j<=n;++j){
- while(q<=n){
- cop11=cop1&1; cop22=cop2&1;
- if(cop11!=cop22){
- ok=1;
- break;
- }
- cop11=cop11>>1;
- cop22=cop22>>1;
- q++;
- }
- tem=tem>>1;
- cop11=val;
- cop22=tem;
- if(!ok) nrp++;
- }
- prez+=nrp;
- ss-=sz;
- lseek(fd, -sz, SEEK_CUR);
- }
- printf("Occurences of template %d of length %d: %d\n", nr+1, n, prez);
- }
- else {
- for(int i=1;i<=nrtmp;++i){
- int ss=lseek(fd, 0, SEEK_END);
- read(fd, &val, sz);
- lseek(fd, 0, SEEK_SET);
- nrp=0;
- int prez=0;
- ss--;
- for(int j=ss;j>=sz;j-=sz){
- read(fd, &tem, sz*2);
- nrp=0;
- for(int k=1;k<=n;++k){
- ok=0;
- int cop1=val, cop2=tem, q=1, cop11, cop22;
- while(q<=n){
- cop11=cop1&1; cop22=cop2&1;
- if(cop11!=cop22){
- ok=1;
- break;
- }
- cop11=cop11>>1;
- cop22=cop22>>1;
- q++;
- }
- tem=tem>>1;
- cop11=val;
- cop22=tem;
- if(!ok) nrp++;
- }
- prez+=nrp;
- lseek(fd, -sz, SEEK_CUR);
- }
- printf("Occurences of template %d of length %d: %d\n", i, n, prez);
- }
- }
- }
- }
- void list_(char** argv){
- int ok=0, cr=2;
- if(!strcmp("recursive", argv[2])) ok=1, cr=3;
- char op[256], path[256], crit[10];
- strcpy(op, argv[cr]);
- strcpy(path, argv[++cr]);
- DIR* dir;
- dir=opendir(path);
- if(dir==0){
- printf("Eroare deschidere fisier");
- return ;
- }
- struct dirent *de;
- struct stat st;
- for(int i=0;i<4;++i) crit[i]=op[i];
- crit[4]=NULL;
- ok=0;
- if(!strcmp("size", crit)){
- int k;
- char s[25];
- strcpy(s,op+13);
- k=atoi(s);
- char name[10000];
- while((de=readdir(dir))){
- char nume[256], num[256];
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- stat(nume, &st);
- int sz=st.st_size;
- if(sz>=k){
- ok=1;
- printf("%s\n", de->d_name);
- }
- }
- }
- else if(!strcmp("name", crit)){
- char eq[256];
- strcpy(eq, op+14);
- while((de=readdir(dir))){
- char nume[256];
- strcpy(nume, de->d_name);
- if(strstr(nume, eq)!=NULL){
- ok=1;
- printf("%s\n", de->d_name);
- }
- }
- }
- else if(!strcmp("temp", crit)){
- while((de=readdir(dir))){
- char nume[256], num[256];
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- lstat(nume, &st);
- if(S_ISREG(st.st_mode)){
- ok=1;
- printf("%s\n", nume);
- }
- }
- }
- else if(!strcmp("hist", crit)){
- char s[10];
- strcpy(s, op+12);
- int k=atoi(s);
- if(k!=1 && k!=2 && k!=4 && k!=8){
- printf("Invalid valu for N");
- return ;
- }
- while((de=readdir(dir))){
- char nume[256], num[256], p;
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- stat(nume, &st);
- int sz=st.st_size;
- sz*=8;
- sz/=100;
- int nr, nrb, val, v[100], minim=INT_MAX, maxim=0;
- nrb--;
- for(int i=0;i<100;++i) v[i]=0;
- int f=open(nume, O_RDONLY);
- if(f!=-1){
- while(read(f, &p, 1)>0){
- int i=0;
- while(i<8/k){
- nrb=1<<k;
- val=k & nrb;
- v[val]++;
- i++;
- p>>=k;
- }
- }
- for(int i=0;i<100;++i)
- if(v[i]){
- if(maxim<v[i]) maxim=v[i];
- if(minim>v[i]) minim=v[i];
- }
- if(maxim-minim<sz && minim!=INT_MAX) {
- ok=1;
- printf("%s\n", nume);
- }
- }
- }
- }
- if(!ok) printf("SUCCES");
- }
- int main(int argc, char** argv)
- {
- if(!strcmp("histogram", argv[1]))
- histogram(argv);
- else if(!strcmp("runs", argv[1]))
- runs(argv);
- else if(!strcmp("template", argv[1]))
- template_(argv);
- else if(!strcmp("list", argv[1]))
- list_(argv);
- return 0;
- }
- // gcc -Wall /home/laura/Desktop/Projects/t1/main.c -o s
- // ./s template 8 /home/laura/Desktop/Projects/t1/template_file_no1.bin /home/laura/Desktop/Projects/t1/random1k.bin
- // ./s list size_greater=1000 /home/laura/Desktop/Projects/t1
- // ./s list name_contains=an /home/laura/Desktop/Projects/t1
- // ./s list hist_random=1 /home/laura/Desktop/Projects/t1
- // ./s histogram 1 /home/laura/Desktop/Projects/t1/random1k.bin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement