Advertisement
falchikova_v

Untitled

May 6th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/mman.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/stat.h>
  9. #include <sys/mman.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15.  
  16.  
  17. int main(int argc, char * argv[]) {
  18.  
  19. int file = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
  20. S_IWUSR | S_IRUSR, 0);
  21. if (file == -1) {
  22. fprintf(stderr, "error opening file info: %s \n", strerror(errno));
  23. return 1;
  24. }
  25.  
  26. size_t rows = atoi(argv[2]);
  27. size_t cols = atoi(argv[3]);
  28.  
  29. off_t file_size = sizeof(uint32_t) * cols * rows;
  30. if(ftruncate(file, file_size)) {
  31. fprintf(stderr, "not enough space on disk\n");
  32. return 1;
  33. }
  34.  
  35. unsigned long matrix[100][100];
  36. unsigned long num = 1;
  37. /* k - starting row index
  38. m - ending row index
  39. l - starting column index
  40. n - ending column index */
  41. size_t k = 0, l = 0;
  42. while (k < rows && l < cols)
  43. {
  44. for (size_t i = l; i < cols; ++i)
  45. matrix[k][i] = num++;
  46.  
  47. k++;
  48.  
  49. for (size_t i = k; i < rows; ++i)
  50. matrix[i][cols - 1] = num++;
  51. cols--;
  52.  
  53. if (k < rows)
  54. {
  55. for (size_t i = cols - 1; i >= l; --i)
  56. matrix[rows - 1][i] = num++;
  57. rows--;
  58. }
  59.  
  60. if (l < cols)
  61. {
  62. for (size_t i = rows - 1; i >= k; --i)
  63. matrix[i][l] = num++;
  64. l++;
  65. }
  66. }
  67.  
  68.  
  69. void *mapped = mmap(
  70. NULL,
  71. file_size,
  72. PROT_READ,
  73. MAP_PRIVATE,
  74. file,
  75. 0
  76. );
  77.  
  78. uint32_t * data = mapped;
  79.  
  80. rows = atoi(argv[2]);
  81. cols = atoi(argv[3]);
  82. // matrix -> file ??
  83. for (size_t i = 0; i < rows; i++) {
  84. for (size_t j = 0; j < cols; j++) {
  85. data[i][j] = matrix[i][j];
  86. }
  87. }
  88.  
  89. munmap(mapped, file_size);
  90. close(file);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement