Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <unistd.h>
- int main(int argc, char *argv[])
- {
- if (argc < 2) {
- printf("no file\n");
- return 1;
- }
- int fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- perror("file open error");
- return 1;
- }
- struct stat s;
- if (fstat(fd, &s) < 0) {
- perror("cannot stat file");
- return 1;
- }
- long a = 0;
- void *addr = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (addr == MAP_FAILED) {
- perror("cannot map file");
- return 1;
- }
- for (off_t i = 0; i < s.st_size; ++i) {
- if (((char *)addr)[i] == 'a')
- ++a;
- }
- printf("Result: %ld\n", a);
- munmap(addr, s.st_size);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement