Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <dirent.h>
  9. #include <limits.h>
  10.  
  11. void histogram(char** argv){
  12. int n=0;
  13. char s[7];
  14. strcpy(s, argv[2]);
  15. n=atoi(s);
  16.  
  17. if(n!=2 && n!=4 && n!=8 && n!=1)
  18. printf("\nERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
  19. else {
  20.  
  21. char path[50];
  22. strcpy(path, argv[3]);
  23.  
  24. int fd=open(path, O_RDONLY);
  25. if(fd==-1){
  26. perror("Invalid path");
  27. return ;
  28. }
  29.  
  30. char p1, p[1000];
  31. int nr, v[1000];
  32. for(int i=0;i<1000;++i) v[i]=0;
  33. int i=n, pu=1<<n, sum=0, nrb;
  34. pu--;
  35. while((nr=read(fd, &p1, 1))){
  36. int j=0;
  37. sum=0;
  38. while(j<8/nr){
  39. // if(p1=='1' && i){
  40. i--;
  41. pu=1;
  42. if(i && p1=='1'){
  43. int cop=i;
  44. while(cop) pu*=2, cop--;
  45. sum+=pu;
  46. }
  47. if(i==0){
  48. i=n;
  49. if(p1=='1') sum++;
  50. v[sum]++;
  51. sum=0; pu=1;
  52. }
  53. j++;
  54. p1>>=1;
  55. /* int x=(1<<n)-1;
  56. int y=p1 & x;
  57. v[y]++; */
  58. /* j++;
  59. nrb=p1&pu;
  60. p1=p1>>n;
  61. v[nrb]++;
  62. } */
  63. }
  64. }
  65. printf("SUCCESS\n");
  66. for(i=0;i<1000;++i)
  67. if(v[i]){
  68. for(int j=n-1;j>=0;--j){
  69. int k=i>>j;
  70. if(k & 1==1) printf("1");
  71. else printf("0");
  72. }
  73. printf(": %d\n", v[i]);
  74. }
  75. }
  76. }
  77.  
  78. void runs(char** argv){
  79. char path[50];
  80. strcpy(path, argv[2]);
  81. int fd=open(path, O_RDONLY);
  82. if(fd==-1){
  83. perror("Invalid file path");
  84. return ;
  85. }
  86. int nr, i=0, f, prev=0, imax=0, byte=0, bit=0, j=0, k=0, rb1, rb2;
  87. char p;
  88. while((nr=read(fd, &p, 1))){
  89. bit++;
  90. if(bit%8==0) bit=0, byte++;
  91. if(p=='1') {
  92. i++;
  93. if(i>imax) {
  94. prev=imax;
  95. imax=i;
  96. rb1=byte;
  97. rb2=bit;
  98. f=j;
  99. }
  100. }
  101. else i=0;
  102. j++;
  103. }
  104. f=f-imax+1;
  105. rb2=f%8;
  106. rb1=f/8;
  107. printf("SUCCESS\nLength of the longest run: %d\n", imax);
  108. printf("Offset: %d bytes + %d bits\n", rb1, rb2);
  109. }
  110.  
  111. void template_(char** argv){
  112. int n, nr=0, cr=3;
  113. n=atoi(argv[2]);
  114. if(argv[5]!=0) cr=4, nr=atoi(argv[3]);
  115. char t_path[50], path[50];
  116. strcpy(t_path, argv[cr]);
  117. strcpy(path, argv[++cr]);
  118. int fd=open(path, O_RDONLY);
  119. if(fd==-1){
  120. perror("ERROR\nInvalid file path");
  121. return ;
  122. }
  123.  
  124. int fd1=open(t_path, O_RDONLY);
  125. if(fd1==-1){
  126. perror("ERROR\nInvalid template file");
  127. return ;
  128. }
  129. if(nr<0) printf("ERROR\nInvalid template number");
  130. if(n!=8 && n!=16 && n!=24 && n!=32)
  131. printf("ERROR\nInvalid bits number");
  132. else {
  133. char sablon[50], p[6];
  134. int vers=0, i=0, nrtmp=0, dim, nrcat=0;
  135. read(fd1, &vers, 2);
  136. //printf("%d", vers);
  137. if(vers<12345 || vers>54321){
  138. printf("ERROR\nWrong version\n");
  139. return ;
  140. }
  141.  
  142. read(fd1, &nrcat, 2);
  143. int ok=0, ofs, cop=nrcat, t;
  144. i=0;
  145. while((t=read(fd1, &dim, 2))){
  146. i++;
  147. if(dim==n){
  148. read(fd1, &nrtmp, 2);
  149. read(fd1, &ofs, 4);
  150. // printf("%d ", nrtmp);
  151. break;
  152. }
  153. nrcat--;
  154. lseek(fd1, 6, SEEK_CUR);
  155. }
  156.  
  157. if(!nrcat){
  158. printf("ERROR\nInvalid template category\n");
  159. return ;
  160. }
  161. // printf("%d\n", nrtmp);
  162. if(nrtmp<nr){
  163. printf("ERROR\nInvalid template number\n");
  164. return ;
  165. }
  166.  
  167. nr--;
  168. char pattern[10000];
  169. char patt;
  170. int j, v[100], sz=n/8;
  171. ok=0;
  172. int nrc=nrtmp*i;
  173. if(ok){
  174. printf("ERROR\nOverlaying catgories");
  175. return ;
  176. }
  177. lseek(fd1, ofs, SEEK_SET);
  178. printf("\nSUCCESS\n");
  179. int nrp=0, val, tem, prez=0;
  180. if(nr!=-1){
  181. nr+=2;
  182. lseek(fd1, n*nr+ofs, SEEK_SET);
  183. int ss=lseek(fd, 0, SEEK_END);
  184. read(fd1, &val, sz);
  185. lseek(fd, 0, SEEK_SET);
  186. ss--;
  187. for(int k=ss;k>=sz;--k){
  188. read(fd, &tem, 2*sz);
  189. ok=0;
  190. nrp=0;
  191. int cop1=val, cop2=tem, q=1, cop11, cop22;
  192. for(int j=1;j<=n;++j){
  193. while(q<=n){
  194. cop11=cop1&1; cop22=cop2&1;
  195. if(cop11!=cop22){
  196. ok=1;
  197. break;
  198. }
  199. cop11=cop11>>1;
  200. cop22=cop22>>1;
  201. q++;
  202. }
  203. tem=tem>>1;
  204. cop11=val;
  205. cop22=tem;
  206. if(!ok) nrp++;
  207. }
  208. prez+=nrp;
  209. ss-=sz;
  210. lseek(fd, -sz, SEEK_CUR);
  211. }
  212. printf("Occurences of template %d of length %d: %d\n", nr+1, n, prez);
  213. }
  214. else {
  215. for(int i=1;i<=nrtmp;++i){
  216. int ss=lseek(fd, 0, SEEK_END);
  217. read(fd, &val, sz);
  218. lseek(fd, 0, SEEK_SET);
  219. nrp=0;
  220. int prez=0;
  221. ss--;
  222. for(int j=ss;j>=sz;j-=sz){
  223. read(fd, &tem, sz*2);
  224. nrp=0;
  225. for(int k=1;k<=n;++k){
  226. ok=0;
  227. int cop1=val, cop2=tem, q=1, cop11, cop22;
  228. while(q<=n){
  229. cop11=cop1&1; cop22=cop2&1;
  230. if(cop11!=cop22){
  231. ok=1;
  232. break;
  233. }
  234. cop11=cop11>>1;
  235. cop22=cop22>>1;
  236. q++;
  237. }
  238. tem=tem>>1;
  239. cop11=val;
  240. cop22=tem;
  241. if(!ok) nrp++;
  242. }
  243. prez+=nrp;
  244. lseek(fd, -sz, SEEK_CUR);
  245. }
  246. printf("Occurences of template %d of length %d: %d\n", i, n, prez);
  247. }
  248. }
  249. }
  250. }
  251.  
  252. void list_(char** argv){
  253. int ok=0, cr=2;
  254. if(!strcmp("recursive", argv[2])) ok=1, cr=3;
  255. char op[256], path[256], crit[10];
  256. strcpy(op, argv[cr]);
  257. strcpy(path, argv[++cr]);
  258.  
  259. DIR* dir;
  260. dir=opendir(path);
  261. if(dir==0){
  262. printf("Eroare deschidere fisier");
  263. return ;
  264. }
  265. struct dirent *de;
  266. struct stat st;
  267. for(int i=0;i<4;++i) crit[i]=op[i];
  268. crit[4]=NULL;
  269. ok=0;
  270. if(!strcmp("size", crit)){
  271. int k;
  272. char s[25];
  273. strcpy(s,op+13);
  274. k=atoi(s);
  275. char name[10000];
  276. while((de=readdir(dir))){
  277. char nume[256], num[256];
  278. strcpy(nume, path);
  279. strcpy(num, "/");
  280. strcat(num, de->d_name);
  281. strcat(nume, num);
  282. stat(nume, &st);
  283. int sz=st.st_size;
  284. if(sz>=k){
  285. ok=1;
  286. printf("%s\n", de->d_name);
  287. }
  288. }
  289. }
  290. else if(!strcmp("name", crit)){
  291. char eq[256];
  292. strcpy(eq, op+14);
  293. while((de=readdir(dir))){
  294. char nume[256];
  295. strcpy(nume, de->d_name);
  296. if(strstr(nume, eq)!=NULL){
  297. ok=1;
  298. printf("%s\n", de->d_name);
  299. }
  300. }
  301. }
  302. else if(!strcmp("temp", crit)){
  303. while((de=readdir(dir))){
  304. char nume[256], num[256];
  305. strcpy(nume, path);
  306. strcpy(num, "/");
  307. strcat(num, de->d_name);
  308. strcat(nume, num);
  309. lstat(nume, &st);
  310. if(S_ISREG(st.st_mode)){
  311. ok=1;
  312. printf("%s\n", nume);
  313. }
  314. }
  315. }
  316. else if(!strcmp("hist", crit)){
  317. char s[10];
  318. strcpy(s, op+12);
  319. int k=atoi(s);
  320. if(k!=1 && k!=2 && k!=4 && k!=8){
  321. printf("Invalid valu for N");
  322. return ;
  323. }
  324. while((de=readdir(dir))){
  325. char nume[256], num[256], p;
  326. strcpy(nume, path);
  327. strcpy(num, "/");
  328. strcat(num, de->d_name);
  329. strcat(nume, num);
  330. stat(nume, &st);
  331. int sz=st.st_size;
  332. sz*=8;
  333. sz/=100;
  334. int nr, nrb, val, v[100], minim=INT_MAX, maxim=0;
  335. nrb--;
  336. for(int i=0;i<100;++i) v[i]=0;
  337. int f=open(nume, O_RDONLY);
  338. if(f!=-1){
  339. while(read(f, &p, 1)>0){
  340. int i=0;
  341. while(i<8/k){
  342. nrb=1<<k;
  343. val=k & nrb;
  344. v[val]++;
  345. i++;
  346. p>>=k;
  347. }
  348. }
  349. for(int i=0;i<100;++i)
  350. if(v[i]){
  351. if(maxim<v[i]) maxim=v[i];
  352. if(minim>v[i]) minim=v[i];
  353. }
  354. if(maxim-minim<sz && minim!=INT_MAX) {
  355. ok=1;
  356. printf("%s\n", nume);
  357. }
  358. }
  359. }
  360. }
  361. if(!ok) printf("SUCCES");
  362. }
  363.  
  364. int main(int argc, char** argv)
  365. {
  366. if(!strcmp("histogram", argv[1]))
  367. histogram(argv);
  368. else if(!strcmp("runs", argv[1]))
  369. runs(argv);
  370. else if(!strcmp("template", argv[1]))
  371. template_(argv);
  372. else if(!strcmp("list", argv[1]))
  373. list_(argv);
  374. return 0;
  375. }
  376. // gcc -Wall /home/laura/Desktop/Projects/t1/main.c -o s
  377. // ./s template 8 /home/laura/Desktop/Projects/t1/template_file_no1.bin /home/laura/Desktop/Projects/t1/random1k.bin
  378. // ./s list size_greater=1000 /home/laura/Desktop/Projects/t1
  379. // ./s list name_contains=an /home/laura/Desktop/Projects/t1
  380. // ./s list hist_random=1 /home/laura/Desktop/Projects/t1
  381. // ./s histogram 1 /home/laura/Desktop/Projects/t1/random1k.bin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement