Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. int main (int argc,char* argv[]){
  9. char buff;
  10. int fd;
  11. char string[100]="==> ";
  12. char string1[7]=" <==\n";
  13. for(int j=1;j<argc;j++)
  14. { if(*argv[j]=='-'){
  15. int write_result=0,read_result=0,close_result=0;
  16. char*filename="tmp.txt";
  17. fd=open("tmp.txt",O_CREAT|O_WRONLY,S_IRWXU);
  18. while(read_result=read(STDIN_FILENO,&buff,1)>0){
  19. if(read_result<0){
  20. char mes1[100]="tail: error reading '\0";
  21. char mes2[14]="'\0";
  22. strcat(mes1,filename);
  23. strcat(mes1,mes2);
  24. perror(mes1);
  25. continue;
  26. }
  27. write_result=write(fd,&buff,1);
  28. if(write_result<0){
  29. //error write ;ne ba4ka
  30. char mes[100]="tail: error writing 'standart output': \0";
  31. write(STDERR_FILENO,mes,strlen(mes));
  32. perror("");
  33. continue;
  34. }
  35. close_result=close(fd);
  36. if(close_result<0)
  37. {
  38. char mes1[100]="tail: error reading '\0";
  39. char mes2[14]="'\0";
  40. strcat(mes1,filename);
  41. strcat(mes1,mes2);
  42. perror(mes1);
  43. continue;
  44. }
  45. }
  46. fd=open("tmp.txt", O_RDONLY);
  47. int l=-2;
  48. int i=0;
  49. off_t size=lseek(fd,0,SEEK_END);
  50. off_t current_position=0;
  51. int wr=0;
  52.  
  53. for(i=0;i!=10;l--)
  54. {
  55. lseek(fd,l,SEEK_END);
  56. current_position=lseek(fd,0,SEEK_CUR);
  57. if(current_position==0)break;
  58. read(fd,&buff,1);
  59. if(buff=='\n')i++;
  60.  
  61. }
  62. if(i==10)current_position=lseek(fd,0,SEEK_CUR);
  63. for(;current_position<size;current_position++){
  64. read(fd,&buff,1);
  65. wr=write(STDOUT_FILENO,&buff,1);
  66. }
  67. close_result=close(fd);
  68. if(close_result==-1)
  69. {
  70. char mes1[100]="tail: error reading '\0";
  71. char mes2[14]="'\0";
  72. strcat(mes1,filename);
  73. strcat(mes1,mes2);
  74. perror(mes1);
  75. continue;
  76. }
  77.  
  78. }
  79. else{
  80. fd=open(argv[j], O_RDONLY);//open file
  81. if(fd<0)//error opening
  82. {
  83. char mes1[100]="tail: cannnot open '\0";
  84. char mes2[14]="' for reading\0";
  85. strcat(mes1,argv[j]);
  86. strcat(mes1,mes2);
  87. perror(mes1);
  88. continue;
  89. }
  90. int read_result=read(fd,&buff,1);
  91. if(read_result<0)//error reading
  92. {
  93. char mes1[100]="tail: error reading '\0";
  94. char mes2[14]="'\0";
  95. strcat(mes1,argv[j]);
  96. strcat(mes1,mes2);
  97. perror(mes1);
  98. continue;
  99. }
  100. if(argc>2){
  101. write(STDOUT_FILENO,string,4);//print ==>
  102. write(STDOUT_FILENO,argv[j],strlen(argv[j]));//print name
  103. write(STDOUT_FILENO,string1,5);//print <==
  104. }
  105. int l=-2;
  106. int i=0;
  107. off_t size=lseek(fd,0,SEEK_END);
  108. off_t current_position=0;
  109. int wr=0;
  110.  
  111. for(i=0;i!=10;l--)
  112. {
  113. lseek(fd,l,SEEK_END);
  114. current_position=lseek(fd,0,SEEK_CUR);
  115. if(current_position==0)break;
  116. read(fd,&buff,1);
  117. if(buff=='\n')i++;
  118.  
  119. }
  120. if(i==10)current_position=lseek(fd,0,SEEK_CUR);
  121. for(;current_position<size;current_position++){
  122. read(fd,&buff,1);
  123. wr=write(STDOUT_FILENO,&buff,1);
  124. }
  125. int close_result=close(fd);
  126. if(close_result==-1)
  127. {
  128. char mes1[100]="tail: error reading '\0";
  129. char mes2[14]="'\0";
  130. strcat(mes1,argv[j]);
  131. strcat(mes1,mes2);
  132. perror(mes1);
  133. continue;
  134. }
  135. }
  136.  
  137. }
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement