/zestaw2/zad1/main.c

https://github.com/osdnk/sysopy · C · 266 lines · 220 code · 43 blank · 3 comment · 79 complexity · 5bf838403444e5d9ea5dc66fba41bd0c MD5 · raw file

  1. //
  2. // Created by Michał Osadnik on 17/03/2018.
  3. //
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <time.h>
  12. #include <sys/times.h>
  13. #define sys_mode 1
  14. #define lib_mode 0
  15. double calculate_time(clock_t start, clock_t end) {
  16. return (double) (end - start) / sysconf(_SC_CLK_TCK);
  17. }
  18. int generate(char *path, int amount, int len) {
  19. FILE *file = fopen(path, "w+"); // write + read
  20. FILE *rnd = fopen("/dev/urandom", "r"); // open rand generator. urandom seems to be better
  21. char *tmp = malloc(len * sizeof(char) + 1);
  22. for (int i = 0; i < amount; ++i) {
  23. if (fread(tmp, sizeof(char), (size_t) len + 1, rnd) != len + 1) {
  24. return 1;
  25. }
  26. for (int j = 0; j < len; ++j) {
  27. tmp[j] = (char) (abs(tmp[j]) % 25 + 'a');
  28. }
  29. tmp[len] = 10;
  30. if (fwrite(tmp, sizeof(char), (size_t) len + 1, file) != len + 1) {
  31. return 1;
  32. }
  33. }
  34. fclose(file); // close file
  35. fclose(rnd); // ... and random buffer
  36. free(tmp);
  37. return 0;
  38. };
  39. void generate_wrapper(char *path, int amount, int len) {
  40. if (generate(path, amount, len) != 0) {
  41. printf("%s", "Oops! Something went with generating 🦖");
  42. }
  43. }
  44. int lib_sort(char *path, int amount, int len) {
  45. FILE *file = fopen(path, "r+");
  46. char *reg1 = malloc((len + 1) * sizeof(char));
  47. char *reg2 = malloc((len + 1) * sizeof(char));
  48. long int offset = (long int) ((len + 1) * sizeof(char));
  49. for (int i = 0; i < amount; i++) {
  50. fseek(file, i * offset, 0); // 0 offset from beg
  51. if (fread(reg1, sizeof(char), (size_t)(len + 1), file) != (len + 1)) { // fread -- where?,what?,how many?,from?
  52. return 1;
  53. }
  54. for (int j = 0; j < i; j++) {
  55. fseek(file, j * offset, 0);
  56. if (fread(reg2, sizeof(char), (size_t)(len + 1), file) != (len + 1)) {
  57. return 1;
  58. }
  59. if (reg2[0] > reg1[0]) {
  60. fseek(file, j * offset, 0);
  61. if (fwrite(reg1, sizeof(char), (size_t)(len + 1), file) != (len + 1)) {
  62. return 1;
  63. }
  64. fseek(file, i * offset, 0);
  65. if (fwrite(reg2, sizeof(char), (size_t)(len + 1), file) != (len + 1)) { // fwrite. Same params
  66. return 1;
  67. }
  68. char *tmp = reg1;
  69. reg1 = reg2;
  70. reg2 = tmp;
  71. }
  72. }
  73. }
  74. fclose(file);
  75. free(reg1);
  76. free(reg2);
  77. return 0;
  78. };
  79. int sys_sort(char *path, int amount, int len) {
  80. int file = open(path, O_RDWR);
  81. char *reg1 = malloc((len + 1) * sizeof(char));
  82. char *reg2 = malloc((len + 1) * sizeof(char));
  83. long int offset = (long int) ((len + 1) * sizeof(char));
  84. for (int i = 0; i < amount; i++) {
  85. lseek(file, i * offset, SEEK_SET); // SEEK_SET - from beg
  86. if (read(file, reg1, (size_t)(len + 1) * sizeof(char)) != (len + 1)) { //from, where, how many?
  87. return 1;
  88. }
  89. for (int j = 0; j < i; j++) {
  90. lseek(file, j * offset, SEEK_SET);
  91. if (read(file, reg2, sizeof(char) * (len + 1)) != (len + 1)) {
  92. return 1;
  93. }
  94. if (reg2[0] > reg1[0]) {
  95. lseek(file, j * offset, 0);
  96. if (write(file, reg1, sizeof(char) * (len + 1)) != (len + 1)) {
  97. return 1;
  98. }
  99. lseek(file, i * offset, 0);
  100. if (write(file, reg2, sizeof(char) * (len + 1)) != (len + 1)) {
  101. return 1;
  102. }
  103. char *tmp = reg1;
  104. reg1 = reg2;
  105. reg2 = tmp;
  106. }
  107. }
  108. }
  109. close(file);
  110. free(reg1);
  111. free(reg2);
  112. return 0;
  113. }
  114. void sort_wrapper(char *path, int amount, int len, int mode) {
  115. if (mode == lib_mode) {
  116. if (lib_sort(path, amount, len) == 1) {
  117. printf("%s", "Oops! Something went with sorting 🦖");
  118. }
  119. } else {
  120. if (sys_sort(path, amount, len) == 1) {
  121. printf("%s", "Oops! Something went with sorting 🦖");
  122. }
  123. }
  124. }
  125. int sys_copy(char *path, char *dest, int amount, int len){
  126. int source = open(path, O_RDONLY);
  127. int target = open(dest, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR); // create ifne, wr only, trunc to 0, args with OCR
  128. char *tmp = malloc(len * sizeof(char));
  129. for (int i = 0; i < amount; i++){
  130. if(read(source, tmp, (size_t) (len + 1) * sizeof(char)) != (len + 1)) {
  131. return 1;
  132. }
  133. if(write(target, tmp, (size_t) (len + 1) * sizeof(char)) != (len + 1)) {
  134. return 1;
  135. }
  136. }
  137. close(source);
  138. close(target);
  139. free(tmp);
  140. return 0;
  141. };
  142. int lib_copy(char *path, char *dest, int amount, int len) {
  143. FILE *source = fopen(path, "r");
  144. FILE *target = fopen(dest, "w+");
  145. char *tmp = malloc(len * sizeof(char));
  146. for (int i = 0; i < amount; i++){
  147. if(fread(tmp, sizeof(char), (size_t) (len + 1), source) != (len + 1)) {
  148. return 1;
  149. }
  150. if(fwrite(tmp, sizeof(char), (size_t) (len + 1), target) != (len + 1)) {
  151. return 1;
  152. }
  153. }
  154. fclose(source);
  155. fclose(target);
  156. free(tmp);
  157. return 0;
  158. };
  159. void copy_wrapper(char *source, char *destination, int amount, int buffer, int mode) {
  160. if (mode == lib_mode) {
  161. if (lib_copy(source, destination, amount, buffer) == 1) {
  162. printf("%s", "Oops! Something went with copying 🦖");
  163. }
  164. } else {
  165. if (sys_copy(source, destination, amount, buffer) == 1) {
  166. printf("%s", "Oops! Something went with copying 🦖");
  167. }
  168. }
  169. }
  170. int main(int argc, char **argv) {
  171. if (argc < 5) {
  172. printf("%s", "There's no enough argument! : ¯\\_(ツ)_/¯");
  173. return 1;
  174. }
  175. struct tms **tms_time = malloc(2 * sizeof(struct tms *));
  176. clock_t real_time[6];
  177. for (int i = 0; i < 2; i++) {
  178. tms_time[i] = (struct tms *) malloc(sizeof(struct tms *));
  179. }
  180. printf(" Real User System\n");
  181. real_time[0] = times(tms_time[0]);
  182. if (strcmp(argv[1], "generate") == 0) {
  183. int amount = (int) strtol(argv[3], NULL, 10);
  184. int len = (int) strtol(argv[4], NULL, 10);
  185. generate_wrapper(argv[2], amount, len);
  186. } else if (strcmp(argv[1], "sort") == 0) {
  187. if (argc < 6) {
  188. printf("%s", "There's no enough argument! : ¯\\_(ツ)_/¯");
  189. return 1;
  190. }
  191. int amount = (int) strtol(argv[3], NULL, 10);
  192. int len = (int) strtol(argv[4], NULL, 10);
  193. if (strcmp(argv[5], "sys") == 0) {
  194. sort_wrapper(argv[2], amount, len, sys_mode);
  195. } else if (strcmp(argv[5], "lib") == 0) {
  196. sort_wrapper(argv[2], amount, len, lib_mode);
  197. } else {
  198. printf("%s", "There's no such a mode 🙄");
  199. }
  200. } else if (strcmp(argv[1], "copy") == 0) {
  201. if (argc < 7) {
  202. printf("%s", "There's no enough argument! : ¯\\_(ツ)_/¯");
  203. return 1;
  204. }
  205. int amount = (int) strtol(argv[4], NULL, 10);
  206. int len = (int) strtol(argv[5], NULL, 10);
  207. if (strcmp(argv[6], "sys") == 0) {
  208. copy_wrapper(argv[2], argv[3], amount, len, sys_mode);
  209. } else if (strcmp(argv[6], "lib") == 0) {
  210. copy_wrapper(argv[2], argv[3], amount, len, lib_mode);
  211. } else {
  212. printf("%s", "There's no such a mode 🙄");
  213. }
  214. } else {
  215. printf("%s", "Are you pretty sure everything if ok?🤔");`
  216. }
  217. real_time[1] = times(tms_time[1]);
  218. printf("%lf ", calculate_time(real_time[0], real_time[1]));
  219. printf("%lf ", calculate_time(tms_time[0]->tms_utime, tms_time[1]->tms_utime));
  220. printf("%lf ", calculate_time(tms_time[0]->tms_stime, tms_time[1]->tms_stime));
  221. printf("\n");
  222. return 0;
  223. }