Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- int main(int argc, char * argv[]) {
- int file = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
- S_IWUSR | S_IRUSR, 0);
- if (file == -1) {
- fprintf(stderr, "error opening file info: %s \n", strerror(errno));
- return 1;
- }
- size_t rows = atoi(argv[2]);
- size_t cols = atoi(argv[3]);
- off_t file_size = sizeof(uint32_t) * cols * rows;
- if(ftruncate(file, file_size)) {
- fprintf(stderr, "not enough space on disk\n");
- return 1;
- }
- unsigned long matrix[100][100];
- unsigned long num = 1;
- /* k - starting row index
- m - ending row index
- l - starting column index
- n - ending column index */
- size_t k = 0, l = 0;
- while (k < rows && l < cols)
- {
- for (size_t i = l; i < cols; ++i)
- matrix[k][i] = num++;
- k++;
- for (size_t i = k; i < rows; ++i)
- matrix[i][cols - 1] = num++;
- cols--;
- if (k < rows)
- {
- for (size_t i = cols - 1; i >= l; --i)
- matrix[rows - 1][i] = num++;
- rows--;
- }
- if (l < cols)
- {
- for (size_t i = rows - 1; i >= k; --i)
- matrix[i][l] = num++;
- l++;
- }
- }
- void *mapped = mmap(
- NULL,
- file_size,
- PROT_READ,
- MAP_PRIVATE,
- file,
- 0
- );
- uint32_t * data = mapped;
- rows = atoi(argv[2]);
- cols = atoi(argv[3]);
- // matrix -> file ??
- for (size_t i = 0; i < rows; i++) {
- for (size_t j = 0; j < cols; j++) {
- data[i][j] = matrix[i][j];
- }
- }
- munmap(mapped, file_size);
- close(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement