Advertisement
falchikova_v

Untitled

May 6th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/mman.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <sys/mman.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14.  
  15.  
  16. int main(int argc, char * argv[]) {
  17.  
  18. for (size_t i = 1; i < argc; i++) {
  19.  
  20. struct stat file_stat;
  21. if (stat(argv[i], &file_stat)) {
  22. printf("-1");
  23. return 1;
  24. }
  25.  
  26. off_t file_size = file_stat.st_size;
  27. if (!file_size) {
  28. printf("0\n");
  29. break;
  30. }
  31. size_t data_len = file_size / sizeof(char);
  32.  
  33. int file = open(argv[i], O_RDONLY, 0);
  34. if (file == -1) {
  35. printf("-1");
  36. return 1;
  37. }
  38.  
  39. void *mapped = mmap(
  40. NULL,
  41. file_size,
  42. PROT_READ,
  43. MAP_PRIVATE,
  44. file,
  45. 0
  46. );
  47.  
  48. char * data = mapped;
  49. size_t j = 0;
  50. unsigned long long num_of_lines = 0;
  51. while(data[j] != EOF) {
  52. if (data[j] == '\n')
  53. num_of_lines++;
  54. j++;
  55. }
  56. if (data[j - 1] != '\n')
  57. num_of_lines++;
  58. printf("%llu\n", num_of_lines);
  59. i++;
  60. munmap(mapped, file_size);
  61. close(file);
  62. }
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement