Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.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[]) {
- for (size_t i = 1; i < argc; i++) {
- struct stat file_stat;
- if (stat(argv[i], &file_stat)) {
- printf("-1");
- return 1;
- }
- off_t file_size = file_stat.st_size;
- if (!file_size) {
- printf("0\n");
- break;
- }
- size_t data_len = file_size / sizeof(char);
- int file = open(argv[i], O_RDONLY, 0);
- if (file == -1) {
- printf("-1");
- return 1;
- }
- void *mapped = mmap(
- NULL,
- file_size,
- PROT_READ,
- MAP_PRIVATE,
- file,
- 0
- );
- char * data = mapped;
- size_t j = 0;
- unsigned long long num_of_lines = 0;
- while(data[j] != EOF) {
- if (data[j] == '\n')
- num_of_lines++;
- j++;
- }
- if (data[j - 1] != '\n')
- num_of_lines++;
- printf("%llu\n", num_of_lines);
- i++;
- munmap(mapped, file_size);
- close(file);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement