Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. int read_stdin();
  10. void reverse_string(char* str);
  11.  
  12.  
  13. int main(int argc, char **argv) {
  14.  
  15. for(int i = 1; i < argc; i++) {
  16.  
  17. char *filename = argv[i];
  18. int open_file = open(filename,O_RDONLY);
  19. char symbol;
  20. int lines = 0;
  21.  
  22.  
  23. if(open_file < 0) {
  24.  
  25. char first = strlen("tail: cannot open '");
  26. char second = strlen("' for reading");
  27. char* err = malloc(first + strlen(filename) + second);
  28.  
  29. strcat(err, "tail: cannot open '");
  30. strcat(err, filename);
  31. strcat(err, "' for reading");
  32. perror(err);
  33. free(err);
  34.  
  35. continue;
  36.  
  37.  
  38. }
  39.  
  40. /*if(argc > 2) {
  41. write(1, "==> ", 4);
  42. write(1, argv[i], strlen(argv[i]));
  43. write(1, " <==\n", 5);
  44. }*/
  45.  
  46. //char ch;
  47.  
  48. int read_file = read(open_file, &symbol, 1);
  49. if(read_file < 0) {
  50. char first = strlen("tail: error reading '");
  51. char second = strlen("'");
  52. char* err = malloc(first + strlen(filename) + second);
  53.  
  54. strcat(err, "tail: error reading '");
  55. strcat(err, filename);
  56. strcat(err, "'");
  57. perror(err);
  58. free(err);
  59. continue;
  60. //return 3;
  61.  
  62. }
  63.  
  64. /*int write_file = write(STDOUT_FILENO, &symbol, 1);
  65. if(write_file < 0) {
  66. perror("tail: error writing");
  67. return 5;
  68. }*/
  69.  
  70.  
  71. if(argc > 2) {
  72. write(1, "==> ", 4);
  73. write(1, argv[i], strlen(argv[i]));
  74. write(1, " <==\n", 5);
  75. }
  76.  
  77.  
  78. off_t size = lseek(open_file,0,SEEK_END);
  79. ssize_t rb;
  80.  
  81. for(int r = -2; lines < 10; r-- ) {
  82. size = lseek(open_file, r, SEEK_END);
  83. ssize_t already_read = read(open_file, &symbol ,1);
  84. if(symbol == '\n') lines++;
  85. }
  86.  
  87. lines = 0;
  88.  
  89. do{
  90. rb = read(open_file, &symbol, 1);
  91. ssize_t wb = write(STDOUT_FILENO,&symbol,1);
  92. if(symbol == '\n') lines++;
  93.  
  94. if(wb < 0) {
  95. char *first = "tail: error writing 'standard output'";
  96. perror(first);
  97. break;
  98.  
  99. }
  100. }while(rb > 0 && lines < 10);
  101. if(i != argc - 1) write(STDOUT_FILENO, "\n", 1);
  102. int close_file = close(open_file);
  103.  
  104. if(close_file < 0) {
  105. char first = strlen("tail: error reading '");
  106. char second = strlen("'");
  107. char* err = malloc(first + strlen(filename) + second);
  108.  
  109. strcat(err, "tail: error reading '");
  110. strcat(err, filename);
  111. strcat(err, "'");
  112. perror(err);
  113. free(err);
  114.  
  115.  
  116. }
  117. }
  118. //}
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement